e63d9765c58d1bbbe0e764aa09f583d7a5a3a5b1
[gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "output.h"
35 #include "target.h"
36 #include "cgraph.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "plugin.h"
40
41 \f
42 /* The lexer. */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
46
47 /* A token's value and its associated deferred access checks and
48 qualifying scope. */
49
50 struct GTY(()) tree_check {
51 /* The value associated with the token. */
52 tree value;
53 /* The checks that have been associated with value. */
54 VEC (deferred_access_check, gc)* checks;
55 /* The token's qualifying scope (used when it is a
56 CPP_NESTED_NAME_SPECIFIER). */
57 tree qualifying_scope;
58 };
59
60 /* A C++ token. */
61
62 typedef struct GTY (()) cp_token {
63 /* The kind of token. */
64 ENUM_BITFIELD (cpp_ttype) type : 8;
65 /* If this token is a keyword, this value indicates which keyword.
66 Otherwise, this value is RID_MAX. */
67 ENUM_BITFIELD (rid) keyword : 8;
68 /* Token flags. */
69 unsigned char flags;
70 /* Identifier for the pragma. */
71 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
72 /* True if this token is from a context where it is implicitly extern "C" */
73 BOOL_BITFIELD implicit_extern_c : 1;
74 /* True for a CPP_NAME token that is not a keyword (i.e., for which
75 KEYWORD is RID_MAX) iff this name was looked up and found to be
76 ambiguous. An error has already been reported. */
77 BOOL_BITFIELD ambiguous_p : 1;
78 /* The location at which this token was found. */
79 location_t location;
80 /* The value associated with this token, if any. */
81 union cp_token_value {
82 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
83 struct tree_check* GTY((tag ("1"))) tree_check_value;
84 /* Use for all other tokens. */
85 tree GTY((tag ("0"))) value;
86 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87 } cp_token;
88
89 /* We use a stack of token pointer for saving token sets. */
90 typedef struct cp_token *cp_token_position;
91 DEF_VEC_P (cp_token_position);
92 DEF_VEC_ALLOC_P (cp_token_position,heap);
93
94 static cp_token eof_token =
95 {
96 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
97 };
98
99 /* The cp_lexer structure represents the C++ lexer. It is responsible
100 for managing the token stream from the preprocessor and supplying
101 it to the parser. Tokens are never added to the cp_lexer after
102 it is created. */
103
104 typedef struct GTY (()) cp_lexer {
105 /* The memory allocated for the buffer. NULL if this lexer does not
106 own the token buffer. */
107 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
108 /* If the lexer owns the buffer, this is the number of tokens in the
109 buffer. */
110 size_t buffer_length;
111
112 /* A pointer just past the last available token. The tokens
113 in this lexer are [buffer, last_token). */
114 cp_token_position GTY ((skip)) last_token;
115
116 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
117 no more available tokens. */
118 cp_token_position GTY ((skip)) next_token;
119
120 /* A stack indicating positions at which cp_lexer_save_tokens was
121 called. The top entry is the most recent position at which we
122 began saving tokens. If the stack is non-empty, we are saving
123 tokens. */
124 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
125
126 /* The next lexer in a linked list of lexers. */
127 struct cp_lexer *next;
128
129 /* True if we should output debugging information. */
130 bool debugging_p;
131
132 /* True if we're in the context of parsing a pragma, and should not
133 increment past the end-of-line marker. */
134 bool in_pragma;
135 } cp_lexer;
136
137 /* cp_token_cache is a range of tokens. There is no need to represent
138 allocate heap memory for it, since tokens are never removed from the
139 lexer's array. There is also no need for the GC to walk through
140 a cp_token_cache, since everything in here is referenced through
141 a lexer. */
142
143 typedef struct GTY(()) cp_token_cache {
144 /* The beginning of the token range. */
145 cp_token * GTY((skip)) first;
146
147 /* Points immediately after the last token in the range. */
148 cp_token * GTY ((skip)) last;
149 } cp_token_cache;
150
151 /* The various kinds of non integral constant we encounter. */
152 typedef enum non_integral_constant {
153 NIC_NONE,
154 /* floating-point literal */
155 NIC_FLOAT,
156 /* %<this%> */
157 NIC_THIS,
158 /* %<__FUNCTION__%> */
159 NIC_FUNC_NAME,
160 /* %<__PRETTY_FUNCTION__%> */
161 NIC_PRETTY_FUNC,
162 /* %<__func__%> */
163 NIC_C99_FUNC,
164 /* "%<va_arg%> */
165 NIC_VA_ARG,
166 /* a cast */
167 NIC_CAST,
168 /* %<typeid%> operator */
169 NIC_TYPEID,
170 /* non-constant compound literals */
171 NIC_NCC,
172 /* a function call */
173 NIC_FUNC_CALL,
174 /* an increment */
175 NIC_INC,
176 /* an decrement */
177 NIC_DEC,
178 /* an array reference */
179 NIC_ARRAY_REF,
180 /* %<->%> */
181 NIC_ARROW,
182 /* %<.%> */
183 NIC_POINT,
184 /* the address of a label */
185 NIC_ADDR_LABEL,
186 /* %<*%> */
187 NIC_STAR,
188 /* %<&%> */
189 NIC_ADDR,
190 /* %<++%> */
191 NIC_PREINCREMENT,
192 /* %<--%> */
193 NIC_PREDECREMENT,
194 /* %<new%> */
195 NIC_NEW,
196 /* %<delete%> */
197 NIC_DEL,
198 /* calls to overloaded operators */
199 NIC_OVERLOADED,
200 /* an assignment */
201 NIC_ASSIGNMENT,
202 /* a comma operator */
203 NIC_COMMA,
204 /* a call to a constructor */
205 NIC_CONSTRUCTOR
206 } non_integral_constant;
207
208 /* The various kinds of errors about name-lookup failing. */
209 typedef enum name_lookup_error {
210 /* NULL */
211 NLE_NULL,
212 /* is not a type */
213 NLE_TYPE,
214 /* is not a class or namespace */
215 NLE_CXX98,
216 /* is not a class, namespace, or enumeration */
217 NLE_NOT_CXX98
218 } name_lookup_error;
219
220 /* The various kinds of required token */
221 typedef enum required_token {
222 RT_NONE,
223 RT_SEMICOLON, /* ';' */
224 RT_OPEN_PAREN, /* '(' */
225 RT_CLOSE_BRACE, /* '}' */
226 RT_OPEN_BRACE, /* '{' */
227 RT_CLOSE_SQUARE, /* ']' */
228 RT_OPEN_SQUARE, /* '[' */
229 RT_COMMA, /* ',' */
230 RT_SCOPE, /* '::' */
231 RT_LESS, /* '<' */
232 RT_GREATER, /* '>' */
233 RT_EQ, /* '=' */
234 RT_ELLIPSIS, /* '...' */
235 RT_MULT, /* '*' */
236 RT_COMPL, /* '~' */
237 RT_COLON, /* ':' */
238 RT_COLON_SCOPE, /* ':' or '::' */
239 RT_CLOSE_PAREN, /* ')' */
240 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
241 RT_PRAGMA_EOL, /* end of line */
242 RT_NAME, /* identifier */
243
244 /* The type is CPP_KEYWORD */
245 RT_NEW, /* new */
246 RT_DELETE, /* delete */
247 RT_RETURN, /* return */
248 RT_WHILE, /* while */
249 RT_EXTERN, /* extern */
250 RT_STATIC_ASSERT, /* static_assert */
251 RT_DECLTYPE, /* decltype */
252 RT_OPERATOR, /* operator */
253 RT_CLASS, /* class */
254 RT_TEMPLATE, /* template */
255 RT_NAMESPACE, /* namespace */
256 RT_USING, /* using */
257 RT_ASM, /* asm */
258 RT_TRY, /* try */
259 RT_CATCH, /* catch */
260 RT_THROW, /* throw */
261 RT_LABEL, /* __label__ */
262 RT_AT_TRY, /* @try */
263 RT_AT_SYNCHRONIZED, /* @synchronized */
264 RT_AT_THROW, /* @throw */
265
266 RT_SELECT, /* selection-statement */
267 RT_INTERATION, /* iteration-statement */
268 RT_JUMP, /* jump-statement */
269 RT_CLASS_KEY, /* class-key */
270 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
271 } required_token;
272
273 /* Prototypes. */
274
275 static cp_lexer *cp_lexer_new_main
276 (void);
277 static cp_lexer *cp_lexer_new_from_tokens
278 (cp_token_cache *tokens);
279 static void cp_lexer_destroy
280 (cp_lexer *);
281 static int cp_lexer_saving_tokens
282 (const cp_lexer *);
283 static cp_token_position cp_lexer_token_position
284 (cp_lexer *, bool);
285 static cp_token *cp_lexer_token_at
286 (cp_lexer *, cp_token_position);
287 static void cp_lexer_get_preprocessor_token
288 (cp_lexer *, cp_token *);
289 static inline cp_token *cp_lexer_peek_token
290 (cp_lexer *);
291 static cp_token *cp_lexer_peek_nth_token
292 (cp_lexer *, size_t);
293 static inline bool cp_lexer_next_token_is
294 (cp_lexer *, enum cpp_ttype);
295 static bool cp_lexer_next_token_is_not
296 (cp_lexer *, enum cpp_ttype);
297 static bool cp_lexer_next_token_is_keyword
298 (cp_lexer *, enum rid);
299 static cp_token *cp_lexer_consume_token
300 (cp_lexer *);
301 static void cp_lexer_purge_token
302 (cp_lexer *);
303 static void cp_lexer_purge_tokens_after
304 (cp_lexer *, cp_token_position);
305 static void cp_lexer_save_tokens
306 (cp_lexer *);
307 static void cp_lexer_commit_tokens
308 (cp_lexer *);
309 static void cp_lexer_rollback_tokens
310 (cp_lexer *);
311 #ifdef ENABLE_CHECKING
312 static void cp_lexer_print_token
313 (FILE *, cp_token *);
314 static inline bool cp_lexer_debugging_p
315 (cp_lexer *);
316 static void cp_lexer_start_debugging
317 (cp_lexer *) ATTRIBUTE_UNUSED;
318 static void cp_lexer_stop_debugging
319 (cp_lexer *) ATTRIBUTE_UNUSED;
320 #else
321 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
322 about passing NULL to functions that require non-NULL arguments
323 (fputs, fprintf). It will never be used, so all we need is a value
324 of the right type that's guaranteed not to be NULL. */
325 #define cp_lexer_debug_stream stdout
326 #define cp_lexer_print_token(str, tok) (void) 0
327 #define cp_lexer_debugging_p(lexer) 0
328 #endif /* ENABLE_CHECKING */
329
330 static cp_token_cache *cp_token_cache_new
331 (cp_token *, cp_token *);
332
333 static void cp_parser_initial_pragma
334 (cp_token *);
335
336 /* Manifest constants. */
337 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
338 #define CP_SAVED_TOKEN_STACK 5
339
340 /* A token type for keywords, as opposed to ordinary identifiers. */
341 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
342
343 /* A token type for template-ids. If a template-id is processed while
344 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
345 the value of the CPP_TEMPLATE_ID is whatever was returned by
346 cp_parser_template_id. */
347 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
348
349 /* A token type for nested-name-specifiers. If a
350 nested-name-specifier is processed while parsing tentatively, it is
351 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
352 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
353 cp_parser_nested_name_specifier_opt. */
354 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
355
356 /* A token type for tokens that are not tokens at all; these are used
357 to represent slots in the array where there used to be a token
358 that has now been deleted. */
359 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
360
361 /* The number of token types, including C++-specific ones. */
362 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
363
364 /* Variables. */
365
366 #ifdef ENABLE_CHECKING
367 /* The stream to which debugging output should be written. */
368 static FILE *cp_lexer_debug_stream;
369 #endif /* ENABLE_CHECKING */
370
371 /* Nonzero if we are parsing an unevaluated operand: an operand to
372 sizeof, typeof, or alignof. */
373 int cp_unevaluated_operand;
374
375 /* Create a new main C++ lexer, the lexer that gets tokens from the
376 preprocessor. */
377
378 static cp_lexer *
379 cp_lexer_new_main (void)
380 {
381 cp_token first_token;
382 cp_lexer *lexer;
383 cp_token *pos;
384 size_t alloc;
385 size_t space;
386 cp_token *buffer;
387
388 /* It's possible that parsing the first pragma will load a PCH file,
389 which is a GC collection point. So we have to do that before
390 allocating any memory. */
391 cp_parser_initial_pragma (&first_token);
392
393 c_common_no_more_pch ();
394
395 /* Allocate the memory. */
396 lexer = ggc_alloc_cleared_cp_lexer ();
397
398 #ifdef ENABLE_CHECKING
399 /* Initially we are not debugging. */
400 lexer->debugging_p = false;
401 #endif /* ENABLE_CHECKING */
402 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
403 CP_SAVED_TOKEN_STACK);
404
405 /* Create the buffer. */
406 alloc = CP_LEXER_BUFFER_SIZE;
407 buffer = ggc_alloc_vec_cp_token (alloc);
408
409 /* Put the first token in the buffer. */
410 space = alloc;
411 pos = buffer;
412 *pos = first_token;
413
414 /* Get the remaining tokens from the preprocessor. */
415 while (pos->type != CPP_EOF)
416 {
417 pos++;
418 if (!--space)
419 {
420 space = alloc;
421 alloc *= 2;
422 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
423 pos = buffer + space;
424 }
425 cp_lexer_get_preprocessor_token (lexer, pos);
426 }
427 lexer->buffer = buffer;
428 lexer->buffer_length = alloc - space;
429 lexer->last_token = pos;
430 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
431
432 /* Subsequent preprocessor diagnostics should use compiler
433 diagnostic functions to get the compiler source location. */
434 done_lexing = true;
435
436 gcc_assert (lexer->next_token->type != CPP_PURGED);
437 return lexer;
438 }
439
440 /* Create a new lexer whose token stream is primed with the tokens in
441 CACHE. When these tokens are exhausted, no new tokens will be read. */
442
443 static cp_lexer *
444 cp_lexer_new_from_tokens (cp_token_cache *cache)
445 {
446 cp_token *first = cache->first;
447 cp_token *last = cache->last;
448 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
449
450 /* We do not own the buffer. */
451 lexer->buffer = NULL;
452 lexer->buffer_length = 0;
453 lexer->next_token = first == last ? &eof_token : first;
454 lexer->last_token = last;
455
456 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
457 CP_SAVED_TOKEN_STACK);
458
459 #ifdef ENABLE_CHECKING
460 /* Initially we are not debugging. */
461 lexer->debugging_p = false;
462 #endif
463
464 gcc_assert (lexer->next_token->type != CPP_PURGED);
465 return lexer;
466 }
467
468 /* Frees all resources associated with LEXER. */
469
470 static void
471 cp_lexer_destroy (cp_lexer *lexer)
472 {
473 if (lexer->buffer)
474 ggc_free (lexer->buffer);
475 VEC_free (cp_token_position, heap, lexer->saved_tokens);
476 ggc_free (lexer);
477 }
478
479 /* Returns nonzero if debugging information should be output. */
480
481 #ifdef ENABLE_CHECKING
482
483 static inline bool
484 cp_lexer_debugging_p (cp_lexer *lexer)
485 {
486 return lexer->debugging_p;
487 }
488
489 #endif /* ENABLE_CHECKING */
490
491 static inline cp_token_position
492 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
493 {
494 gcc_assert (!previous_p || lexer->next_token != &eof_token);
495
496 return lexer->next_token - previous_p;
497 }
498
499 static inline cp_token *
500 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
501 {
502 return pos;
503 }
504
505 static inline void
506 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
507 {
508 lexer->next_token = cp_lexer_token_at (lexer, pos);
509 }
510
511 static inline cp_token_position
512 cp_lexer_previous_token_position (cp_lexer *lexer)
513 {
514 if (lexer->next_token == &eof_token)
515 return lexer->last_token - 1;
516 else
517 return cp_lexer_token_position (lexer, true);
518 }
519
520 static inline cp_token *
521 cp_lexer_previous_token (cp_lexer *lexer)
522 {
523 cp_token_position tp = cp_lexer_previous_token_position (lexer);
524
525 return cp_lexer_token_at (lexer, tp);
526 }
527
528 /* nonzero if we are presently saving tokens. */
529
530 static inline int
531 cp_lexer_saving_tokens (const cp_lexer* lexer)
532 {
533 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
534 }
535
536 /* Store the next token from the preprocessor in *TOKEN. Return true
537 if we reach EOF. If LEXER is NULL, assume we are handling an
538 initial #pragma pch_preprocess, and thus want the lexer to return
539 processed strings. */
540
541 static void
542 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
543 {
544 static int is_extern_c = 0;
545
546 /* Get a new token from the preprocessor. */
547 token->type
548 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
549 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
550 token->keyword = RID_MAX;
551 token->pragma_kind = PRAGMA_NONE;
552
553 /* On some systems, some header files are surrounded by an
554 implicit extern "C" block. Set a flag in the token if it
555 comes from such a header. */
556 is_extern_c += pending_lang_change;
557 pending_lang_change = 0;
558 token->implicit_extern_c = is_extern_c > 0;
559
560 /* Check to see if this token is a keyword. */
561 if (token->type == CPP_NAME)
562 {
563 if (C_IS_RESERVED_WORD (token->u.value))
564 {
565 /* Mark this token as a keyword. */
566 token->type = CPP_KEYWORD;
567 /* Record which keyword. */
568 token->keyword = C_RID_CODE (token->u.value);
569 }
570 else
571 {
572 if (warn_cxx0x_compat
573 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
574 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
575 {
576 /* Warn about the C++0x keyword (but still treat it as
577 an identifier). */
578 warning (OPT_Wc__0x_compat,
579 "identifier %qE will become a keyword in C++0x",
580 token->u.value);
581
582 /* Clear out the C_RID_CODE so we don't warn about this
583 particular identifier-turned-keyword again. */
584 C_SET_RID_CODE (token->u.value, RID_MAX);
585 }
586
587 token->ambiguous_p = false;
588 token->keyword = RID_MAX;
589 }
590 }
591 else if (token->type == CPP_AT_NAME)
592 {
593 /* This only happens in Objective-C++; it must be a keyword. */
594 token->type = CPP_KEYWORD;
595 switch (C_RID_CODE (token->u.value))
596 {
597 /* Replace 'class' with '@class', 'private' with '@private',
598 etc. This prevents confusion with the C++ keyword
599 'class', and makes the tokens consistent with other
600 Objective-C 'AT' keywords. For example '@class' is
601 reported as RID_AT_CLASS which is consistent with
602 '@synchronized', which is reported as
603 RID_AT_SYNCHRONIZED.
604 */
605 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
606 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
607 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
608 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
609 case RID_THROW: token->keyword = RID_AT_THROW; break;
610 case RID_TRY: token->keyword = RID_AT_TRY; break;
611 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
612 default: token->keyword = C_RID_CODE (token->u.value);
613 }
614 }
615 else if (token->type == CPP_PRAGMA)
616 {
617 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
618 token->pragma_kind = ((enum pragma_kind)
619 TREE_INT_CST_LOW (token->u.value));
620 token->u.value = NULL_TREE;
621 }
622 }
623
624 /* Update the globals input_location and the input file stack from TOKEN. */
625 static inline void
626 cp_lexer_set_source_position_from_token (cp_token *token)
627 {
628 if (token->type != CPP_EOF)
629 {
630 input_location = token->location;
631 }
632 }
633
634 /* Return a pointer to the next token in the token stream, but do not
635 consume it. */
636
637 static inline cp_token *
638 cp_lexer_peek_token (cp_lexer *lexer)
639 {
640 if (cp_lexer_debugging_p (lexer))
641 {
642 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
643 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
644 putc ('\n', cp_lexer_debug_stream);
645 }
646 return lexer->next_token;
647 }
648
649 /* Return true if the next token has the indicated TYPE. */
650
651 static inline bool
652 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
653 {
654 return cp_lexer_peek_token (lexer)->type == type;
655 }
656
657 /* Return true if the next token does not have the indicated TYPE. */
658
659 static inline bool
660 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
661 {
662 return !cp_lexer_next_token_is (lexer, type);
663 }
664
665 /* Return true if the next token is the indicated KEYWORD. */
666
667 static inline bool
668 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
669 {
670 return cp_lexer_peek_token (lexer)->keyword == keyword;
671 }
672
673 /* Return true if the next token is not the indicated KEYWORD. */
674
675 static inline bool
676 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
677 {
678 return cp_lexer_peek_token (lexer)->keyword != keyword;
679 }
680
681 /* Return true if the next token is a keyword for a decl-specifier. */
682
683 static bool
684 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
685 {
686 cp_token *token;
687
688 token = cp_lexer_peek_token (lexer);
689 switch (token->keyword)
690 {
691 /* auto specifier: storage-class-specifier in C++,
692 simple-type-specifier in C++0x. */
693 case RID_AUTO:
694 /* Storage classes. */
695 case RID_REGISTER:
696 case RID_STATIC:
697 case RID_EXTERN:
698 case RID_MUTABLE:
699 case RID_THREAD:
700 /* Elaborated type specifiers. */
701 case RID_ENUM:
702 case RID_CLASS:
703 case RID_STRUCT:
704 case RID_UNION:
705 case RID_TYPENAME:
706 /* Simple type specifiers. */
707 case RID_CHAR:
708 case RID_CHAR16:
709 case RID_CHAR32:
710 case RID_WCHAR:
711 case RID_BOOL:
712 case RID_SHORT:
713 case RID_INT:
714 case RID_LONG:
715 case RID_INT128:
716 case RID_SIGNED:
717 case RID_UNSIGNED:
718 case RID_FLOAT:
719 case RID_DOUBLE:
720 case RID_VOID:
721 /* GNU extensions. */
722 case RID_ATTRIBUTE:
723 case RID_TYPEOF:
724 /* C++0x extensions. */
725 case RID_DECLTYPE:
726 return true;
727
728 default:
729 return false;
730 }
731 }
732
733 /* Return a pointer to the Nth token in the token stream. If N is 1,
734 then this is precisely equivalent to cp_lexer_peek_token (except
735 that it is not inline). One would like to disallow that case, but
736 there is one case (cp_parser_nth_token_starts_template_id) where
737 the caller passes a variable for N and it might be 1. */
738
739 static cp_token *
740 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
741 {
742 cp_token *token;
743
744 /* N is 1-based, not zero-based. */
745 gcc_assert (n > 0);
746
747 if (cp_lexer_debugging_p (lexer))
748 fprintf (cp_lexer_debug_stream,
749 "cp_lexer: peeking ahead %ld at token: ", (long)n);
750
751 --n;
752 token = lexer->next_token;
753 gcc_assert (!n || token != &eof_token);
754 while (n != 0)
755 {
756 ++token;
757 if (token == lexer->last_token)
758 {
759 token = &eof_token;
760 break;
761 }
762
763 if (token->type != CPP_PURGED)
764 --n;
765 }
766
767 if (cp_lexer_debugging_p (lexer))
768 {
769 cp_lexer_print_token (cp_lexer_debug_stream, token);
770 putc ('\n', cp_lexer_debug_stream);
771 }
772
773 return token;
774 }
775
776 /* Return the next token, and advance the lexer's next_token pointer
777 to point to the next non-purged token. */
778
779 static cp_token *
780 cp_lexer_consume_token (cp_lexer* lexer)
781 {
782 cp_token *token = lexer->next_token;
783
784 gcc_assert (token != &eof_token);
785 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
786
787 do
788 {
789 lexer->next_token++;
790 if (lexer->next_token == lexer->last_token)
791 {
792 lexer->next_token = &eof_token;
793 break;
794 }
795
796 }
797 while (lexer->next_token->type == CPP_PURGED);
798
799 cp_lexer_set_source_position_from_token (token);
800
801 /* Provide debugging output. */
802 if (cp_lexer_debugging_p (lexer))
803 {
804 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
805 cp_lexer_print_token (cp_lexer_debug_stream, token);
806 putc ('\n', cp_lexer_debug_stream);
807 }
808
809 return token;
810 }
811
812 /* Permanently remove the next token from the token stream, and
813 advance the next_token pointer to refer to the next non-purged
814 token. */
815
816 static void
817 cp_lexer_purge_token (cp_lexer *lexer)
818 {
819 cp_token *tok = lexer->next_token;
820
821 gcc_assert (tok != &eof_token);
822 tok->type = CPP_PURGED;
823 tok->location = UNKNOWN_LOCATION;
824 tok->u.value = NULL_TREE;
825 tok->keyword = RID_MAX;
826
827 do
828 {
829 tok++;
830 if (tok == lexer->last_token)
831 {
832 tok = &eof_token;
833 break;
834 }
835 }
836 while (tok->type == CPP_PURGED);
837 lexer->next_token = tok;
838 }
839
840 /* Permanently remove all tokens after TOK, up to, but not
841 including, the token that will be returned next by
842 cp_lexer_peek_token. */
843
844 static void
845 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
846 {
847 cp_token *peek = lexer->next_token;
848
849 if (peek == &eof_token)
850 peek = lexer->last_token;
851
852 gcc_assert (tok < peek);
853
854 for ( tok += 1; tok != peek; tok += 1)
855 {
856 tok->type = CPP_PURGED;
857 tok->location = UNKNOWN_LOCATION;
858 tok->u.value = NULL_TREE;
859 tok->keyword = RID_MAX;
860 }
861 }
862
863 /* Begin saving tokens. All tokens consumed after this point will be
864 preserved. */
865
866 static void
867 cp_lexer_save_tokens (cp_lexer* lexer)
868 {
869 /* Provide debugging output. */
870 if (cp_lexer_debugging_p (lexer))
871 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
872
873 VEC_safe_push (cp_token_position, heap,
874 lexer->saved_tokens, lexer->next_token);
875 }
876
877 /* Commit to the portion of the token stream most recently saved. */
878
879 static void
880 cp_lexer_commit_tokens (cp_lexer* lexer)
881 {
882 /* Provide debugging output. */
883 if (cp_lexer_debugging_p (lexer))
884 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
885
886 VEC_pop (cp_token_position, lexer->saved_tokens);
887 }
888
889 /* Return all tokens saved since the last call to cp_lexer_save_tokens
890 to the token stream. Stop saving tokens. */
891
892 static void
893 cp_lexer_rollback_tokens (cp_lexer* lexer)
894 {
895 /* Provide debugging output. */
896 if (cp_lexer_debugging_p (lexer))
897 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
898
899 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
900 }
901
902 /* Print a representation of the TOKEN on the STREAM. */
903
904 #ifdef ENABLE_CHECKING
905
906 static void
907 cp_lexer_print_token (FILE * stream, cp_token *token)
908 {
909 /* We don't use cpp_type2name here because the parser defines
910 a few tokens of its own. */
911 static const char *const token_names[] = {
912 /* cpplib-defined token types */
913 #define OP(e, s) #e,
914 #define TK(e, s) #e,
915 TTYPE_TABLE
916 #undef OP
917 #undef TK
918 /* C++ parser token types - see "Manifest constants", above. */
919 "KEYWORD",
920 "TEMPLATE_ID",
921 "NESTED_NAME_SPECIFIER",
922 "PURGED"
923 };
924
925 /* If we have a name for the token, print it out. Otherwise, we
926 simply give the numeric code. */
927 gcc_assert (token->type < ARRAY_SIZE(token_names));
928 fputs (token_names[token->type], stream);
929
930 /* For some tokens, print the associated data. */
931 switch (token->type)
932 {
933 case CPP_KEYWORD:
934 /* Some keywords have a value that is not an IDENTIFIER_NODE.
935 For example, `struct' is mapped to an INTEGER_CST. */
936 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
937 break;
938 /* else fall through */
939 case CPP_NAME:
940 fputs (IDENTIFIER_POINTER (token->u.value), stream);
941 break;
942
943 case CPP_STRING:
944 case CPP_STRING16:
945 case CPP_STRING32:
946 case CPP_WSTRING:
947 case CPP_UTF8STRING:
948 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
949 break;
950
951 default:
952 break;
953 }
954 }
955
956 /* Start emitting debugging information. */
957
958 static void
959 cp_lexer_start_debugging (cp_lexer* lexer)
960 {
961 lexer->debugging_p = true;
962 }
963
964 /* Stop emitting debugging information. */
965
966 static void
967 cp_lexer_stop_debugging (cp_lexer* lexer)
968 {
969 lexer->debugging_p = false;
970 }
971
972 #endif /* ENABLE_CHECKING */
973
974 /* Create a new cp_token_cache, representing a range of tokens. */
975
976 static cp_token_cache *
977 cp_token_cache_new (cp_token *first, cp_token *last)
978 {
979 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
980 cache->first = first;
981 cache->last = last;
982 return cache;
983 }
984
985 \f
986 /* Decl-specifiers. */
987
988 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
989
990 static void
991 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
992 {
993 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
994 }
995
996 /* Declarators. */
997
998 /* Nothing other than the parser should be creating declarators;
999 declarators are a semi-syntactic representation of C++ entities.
1000 Other parts of the front end that need to create entities (like
1001 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1002
1003 static cp_declarator *make_call_declarator
1004 (cp_declarator *, tree, cp_cv_quals, tree, tree);
1005 static cp_declarator *make_array_declarator
1006 (cp_declarator *, tree);
1007 static cp_declarator *make_pointer_declarator
1008 (cp_cv_quals, cp_declarator *);
1009 static cp_declarator *make_reference_declarator
1010 (cp_cv_quals, cp_declarator *, bool);
1011 static cp_parameter_declarator *make_parameter_declarator
1012 (cp_decl_specifier_seq *, cp_declarator *, tree);
1013 static cp_declarator *make_ptrmem_declarator
1014 (cp_cv_quals, tree, cp_declarator *);
1015
1016 /* An erroneous declarator. */
1017 static cp_declarator *cp_error_declarator;
1018
1019 /* The obstack on which declarators and related data structures are
1020 allocated. */
1021 static struct obstack declarator_obstack;
1022
1023 /* Alloc BYTES from the declarator memory pool. */
1024
1025 static inline void *
1026 alloc_declarator (size_t bytes)
1027 {
1028 return obstack_alloc (&declarator_obstack, bytes);
1029 }
1030
1031 /* Allocate a declarator of the indicated KIND. Clear fields that are
1032 common to all declarators. */
1033
1034 static cp_declarator *
1035 make_declarator (cp_declarator_kind kind)
1036 {
1037 cp_declarator *declarator;
1038
1039 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1040 declarator->kind = kind;
1041 declarator->attributes = NULL_TREE;
1042 declarator->declarator = NULL;
1043 declarator->parameter_pack_p = false;
1044 declarator->id_loc = UNKNOWN_LOCATION;
1045
1046 return declarator;
1047 }
1048
1049 /* Make a declarator for a generalized identifier. If
1050 QUALIFYING_SCOPE is non-NULL, the identifier is
1051 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1052 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1053 is, if any. */
1054
1055 static cp_declarator *
1056 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1057 special_function_kind sfk)
1058 {
1059 cp_declarator *declarator;
1060
1061 /* It is valid to write:
1062
1063 class C { void f(); };
1064 typedef C D;
1065 void D::f();
1066
1067 The standard is not clear about whether `typedef const C D' is
1068 legal; as of 2002-09-15 the committee is considering that
1069 question. EDG 3.0 allows that syntax. Therefore, we do as
1070 well. */
1071 if (qualifying_scope && TYPE_P (qualifying_scope))
1072 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1073
1074 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1075 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1076 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1077
1078 declarator = make_declarator (cdk_id);
1079 declarator->u.id.qualifying_scope = qualifying_scope;
1080 declarator->u.id.unqualified_name = unqualified_name;
1081 declarator->u.id.sfk = sfk;
1082
1083 return declarator;
1084 }
1085
1086 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1087 of modifiers such as const or volatile to apply to the pointer
1088 type, represented as identifiers. */
1089
1090 cp_declarator *
1091 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1092 {
1093 cp_declarator *declarator;
1094
1095 declarator = make_declarator (cdk_pointer);
1096 declarator->declarator = target;
1097 declarator->u.pointer.qualifiers = cv_qualifiers;
1098 declarator->u.pointer.class_type = NULL_TREE;
1099 if (target)
1100 {
1101 declarator->id_loc = target->id_loc;
1102 declarator->parameter_pack_p = target->parameter_pack_p;
1103 target->parameter_pack_p = false;
1104 }
1105 else
1106 declarator->parameter_pack_p = false;
1107
1108 return declarator;
1109 }
1110
1111 /* Like make_pointer_declarator -- but for references. */
1112
1113 cp_declarator *
1114 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1115 bool rvalue_ref)
1116 {
1117 cp_declarator *declarator;
1118
1119 declarator = make_declarator (cdk_reference);
1120 declarator->declarator = target;
1121 declarator->u.reference.qualifiers = cv_qualifiers;
1122 declarator->u.reference.rvalue_ref = rvalue_ref;
1123 if (target)
1124 {
1125 declarator->id_loc = target->id_loc;
1126 declarator->parameter_pack_p = target->parameter_pack_p;
1127 target->parameter_pack_p = false;
1128 }
1129 else
1130 declarator->parameter_pack_p = false;
1131
1132 return declarator;
1133 }
1134
1135 /* Like make_pointer_declarator -- but for a pointer to a non-static
1136 member of CLASS_TYPE. */
1137
1138 cp_declarator *
1139 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1140 cp_declarator *pointee)
1141 {
1142 cp_declarator *declarator;
1143
1144 declarator = make_declarator (cdk_ptrmem);
1145 declarator->declarator = pointee;
1146 declarator->u.pointer.qualifiers = cv_qualifiers;
1147 declarator->u.pointer.class_type = class_type;
1148
1149 if (pointee)
1150 {
1151 declarator->parameter_pack_p = pointee->parameter_pack_p;
1152 pointee->parameter_pack_p = false;
1153 }
1154 else
1155 declarator->parameter_pack_p = false;
1156
1157 return declarator;
1158 }
1159
1160 /* Make a declarator for the function given by TARGET, with the
1161 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1162 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1163 indicates what exceptions can be thrown. */
1164
1165 cp_declarator *
1166 make_call_declarator (cp_declarator *target,
1167 tree parms,
1168 cp_cv_quals cv_qualifiers,
1169 tree exception_specification,
1170 tree late_return_type)
1171 {
1172 cp_declarator *declarator;
1173
1174 declarator = make_declarator (cdk_function);
1175 declarator->declarator = target;
1176 declarator->u.function.parameters = parms;
1177 declarator->u.function.qualifiers = cv_qualifiers;
1178 declarator->u.function.exception_specification = exception_specification;
1179 declarator->u.function.late_return_type = late_return_type;
1180 if (target)
1181 {
1182 declarator->id_loc = target->id_loc;
1183 declarator->parameter_pack_p = target->parameter_pack_p;
1184 target->parameter_pack_p = false;
1185 }
1186 else
1187 declarator->parameter_pack_p = false;
1188
1189 return declarator;
1190 }
1191
1192 /* Make a declarator for an array of BOUNDS elements, each of which is
1193 defined by ELEMENT. */
1194
1195 cp_declarator *
1196 make_array_declarator (cp_declarator *element, tree bounds)
1197 {
1198 cp_declarator *declarator;
1199
1200 declarator = make_declarator (cdk_array);
1201 declarator->declarator = element;
1202 declarator->u.array.bounds = bounds;
1203 if (element)
1204 {
1205 declarator->id_loc = element->id_loc;
1206 declarator->parameter_pack_p = element->parameter_pack_p;
1207 element->parameter_pack_p = false;
1208 }
1209 else
1210 declarator->parameter_pack_p = false;
1211
1212 return declarator;
1213 }
1214
1215 /* Determine whether the declarator we've seen so far can be a
1216 parameter pack, when followed by an ellipsis. */
1217 static bool
1218 declarator_can_be_parameter_pack (cp_declarator *declarator)
1219 {
1220 /* Search for a declarator name, or any other declarator that goes
1221 after the point where the ellipsis could appear in a parameter
1222 pack. If we find any of these, then this declarator can not be
1223 made into a parameter pack. */
1224 bool found = false;
1225 while (declarator && !found)
1226 {
1227 switch ((int)declarator->kind)
1228 {
1229 case cdk_id:
1230 case cdk_array:
1231 found = true;
1232 break;
1233
1234 case cdk_error:
1235 return true;
1236
1237 default:
1238 declarator = declarator->declarator;
1239 break;
1240 }
1241 }
1242
1243 return !found;
1244 }
1245
1246 cp_parameter_declarator *no_parameters;
1247
1248 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1249 DECLARATOR and DEFAULT_ARGUMENT. */
1250
1251 cp_parameter_declarator *
1252 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1253 cp_declarator *declarator,
1254 tree default_argument)
1255 {
1256 cp_parameter_declarator *parameter;
1257
1258 parameter = ((cp_parameter_declarator *)
1259 alloc_declarator (sizeof (cp_parameter_declarator)));
1260 parameter->next = NULL;
1261 if (decl_specifiers)
1262 parameter->decl_specifiers = *decl_specifiers;
1263 else
1264 clear_decl_specs (&parameter->decl_specifiers);
1265 parameter->declarator = declarator;
1266 parameter->default_argument = default_argument;
1267 parameter->ellipsis_p = false;
1268
1269 return parameter;
1270 }
1271
1272 /* Returns true iff DECLARATOR is a declaration for a function. */
1273
1274 static bool
1275 function_declarator_p (const cp_declarator *declarator)
1276 {
1277 while (declarator)
1278 {
1279 if (declarator->kind == cdk_function
1280 && declarator->declarator->kind == cdk_id)
1281 return true;
1282 if (declarator->kind == cdk_id
1283 || declarator->kind == cdk_error)
1284 return false;
1285 declarator = declarator->declarator;
1286 }
1287 return false;
1288 }
1289
1290 /* The parser. */
1291
1292 /* Overview
1293 --------
1294
1295 A cp_parser parses the token stream as specified by the C++
1296 grammar. Its job is purely parsing, not semantic analysis. For
1297 example, the parser breaks the token stream into declarators,
1298 expressions, statements, and other similar syntactic constructs.
1299 It does not check that the types of the expressions on either side
1300 of an assignment-statement are compatible, or that a function is
1301 not declared with a parameter of type `void'.
1302
1303 The parser invokes routines elsewhere in the compiler to perform
1304 semantic analysis and to build up the abstract syntax tree for the
1305 code processed.
1306
1307 The parser (and the template instantiation code, which is, in a
1308 way, a close relative of parsing) are the only parts of the
1309 compiler that should be calling push_scope and pop_scope, or
1310 related functions. The parser (and template instantiation code)
1311 keeps track of what scope is presently active; everything else
1312 should simply honor that. (The code that generates static
1313 initializers may also need to set the scope, in order to check
1314 access control correctly when emitting the initializers.)
1315
1316 Methodology
1317 -----------
1318
1319 The parser is of the standard recursive-descent variety. Upcoming
1320 tokens in the token stream are examined in order to determine which
1321 production to use when parsing a non-terminal. Some C++ constructs
1322 require arbitrary look ahead to disambiguate. For example, it is
1323 impossible, in the general case, to tell whether a statement is an
1324 expression or declaration without scanning the entire statement.
1325 Therefore, the parser is capable of "parsing tentatively." When the
1326 parser is not sure what construct comes next, it enters this mode.
1327 Then, while we attempt to parse the construct, the parser queues up
1328 error messages, rather than issuing them immediately, and saves the
1329 tokens it consumes. If the construct is parsed successfully, the
1330 parser "commits", i.e., it issues any queued error messages and
1331 the tokens that were being preserved are permanently discarded.
1332 If, however, the construct is not parsed successfully, the parser
1333 rolls back its state completely so that it can resume parsing using
1334 a different alternative.
1335
1336 Future Improvements
1337 -------------------
1338
1339 The performance of the parser could probably be improved substantially.
1340 We could often eliminate the need to parse tentatively by looking ahead
1341 a little bit. In some places, this approach might not entirely eliminate
1342 the need to parse tentatively, but it might still speed up the average
1343 case. */
1344
1345 /* Flags that are passed to some parsing functions. These values can
1346 be bitwise-ored together. */
1347
1348 enum
1349 {
1350 /* No flags. */
1351 CP_PARSER_FLAGS_NONE = 0x0,
1352 /* The construct is optional. If it is not present, then no error
1353 should be issued. */
1354 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1355 /* When parsing a type-specifier, treat user-defined type-names
1356 as non-type identifiers. */
1357 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1358 /* When parsing a type-specifier, do not try to parse a class-specifier
1359 or enum-specifier. */
1360 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1361 /* When parsing a decl-specifier-seq, only allow type-specifier or
1362 constexpr. */
1363 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1364 };
1365
1366 /* This type is used for parameters and variables which hold
1367 combinations of the above flags. */
1368 typedef int cp_parser_flags;
1369
1370 /* The different kinds of declarators we want to parse. */
1371
1372 typedef enum cp_parser_declarator_kind
1373 {
1374 /* We want an abstract declarator. */
1375 CP_PARSER_DECLARATOR_ABSTRACT,
1376 /* We want a named declarator. */
1377 CP_PARSER_DECLARATOR_NAMED,
1378 /* We don't mind, but the name must be an unqualified-id. */
1379 CP_PARSER_DECLARATOR_EITHER
1380 } cp_parser_declarator_kind;
1381
1382 /* The precedence values used to parse binary expressions. The minimum value
1383 of PREC must be 1, because zero is reserved to quickly discriminate
1384 binary operators from other tokens. */
1385
1386 enum cp_parser_prec
1387 {
1388 PREC_NOT_OPERATOR,
1389 PREC_LOGICAL_OR_EXPRESSION,
1390 PREC_LOGICAL_AND_EXPRESSION,
1391 PREC_INCLUSIVE_OR_EXPRESSION,
1392 PREC_EXCLUSIVE_OR_EXPRESSION,
1393 PREC_AND_EXPRESSION,
1394 PREC_EQUALITY_EXPRESSION,
1395 PREC_RELATIONAL_EXPRESSION,
1396 PREC_SHIFT_EXPRESSION,
1397 PREC_ADDITIVE_EXPRESSION,
1398 PREC_MULTIPLICATIVE_EXPRESSION,
1399 PREC_PM_EXPRESSION,
1400 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1401 };
1402
1403 /* A mapping from a token type to a corresponding tree node type, with a
1404 precedence value. */
1405
1406 typedef struct cp_parser_binary_operations_map_node
1407 {
1408 /* The token type. */
1409 enum cpp_ttype token_type;
1410 /* The corresponding tree code. */
1411 enum tree_code tree_type;
1412 /* The precedence of this operator. */
1413 enum cp_parser_prec prec;
1414 } cp_parser_binary_operations_map_node;
1415
1416 /* The status of a tentative parse. */
1417
1418 typedef enum cp_parser_status_kind
1419 {
1420 /* No errors have occurred. */
1421 CP_PARSER_STATUS_KIND_NO_ERROR,
1422 /* An error has occurred. */
1423 CP_PARSER_STATUS_KIND_ERROR,
1424 /* We are committed to this tentative parse, whether or not an error
1425 has occurred. */
1426 CP_PARSER_STATUS_KIND_COMMITTED
1427 } cp_parser_status_kind;
1428
1429 typedef struct cp_parser_expression_stack_entry
1430 {
1431 /* Left hand side of the binary operation we are currently
1432 parsing. */
1433 tree lhs;
1434 /* Original tree code for left hand side, if it was a binary
1435 expression itself (used for -Wparentheses). */
1436 enum tree_code lhs_type;
1437 /* Tree code for the binary operation we are parsing. */
1438 enum tree_code tree_type;
1439 /* Precedence of the binary operation we are parsing. */
1440 enum cp_parser_prec prec;
1441 } cp_parser_expression_stack_entry;
1442
1443 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1444 entries because precedence levels on the stack are monotonically
1445 increasing. */
1446 typedef struct cp_parser_expression_stack_entry
1447 cp_parser_expression_stack[NUM_PREC_VALUES];
1448
1449 /* Context that is saved and restored when parsing tentatively. */
1450 typedef struct GTY (()) cp_parser_context {
1451 /* If this is a tentative parsing context, the status of the
1452 tentative parse. */
1453 enum cp_parser_status_kind status;
1454 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1455 that are looked up in this context must be looked up both in the
1456 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1457 the context of the containing expression. */
1458 tree object_type;
1459
1460 /* The next parsing context in the stack. */
1461 struct cp_parser_context *next;
1462 } cp_parser_context;
1463
1464 /* Prototypes. */
1465
1466 /* Constructors and destructors. */
1467
1468 static cp_parser_context *cp_parser_context_new
1469 (cp_parser_context *);
1470
1471 /* Class variables. */
1472
1473 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1474
1475 /* The operator-precedence table used by cp_parser_binary_expression.
1476 Transformed into an associative array (binops_by_token) by
1477 cp_parser_new. */
1478
1479 static const cp_parser_binary_operations_map_node binops[] = {
1480 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1481 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1482
1483 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1484 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1485 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1486
1487 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1488 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1489
1490 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1491 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1492
1493 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1494 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1495 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1496 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1497
1498 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1499 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1500
1501 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1502
1503 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1504
1505 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1506
1507 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1508
1509 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1510 };
1511
1512 /* The same as binops, but initialized by cp_parser_new so that
1513 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1514 for speed. */
1515 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1516
1517 /* Constructors and destructors. */
1518
1519 /* Construct a new context. The context below this one on the stack
1520 is given by NEXT. */
1521
1522 static cp_parser_context *
1523 cp_parser_context_new (cp_parser_context* next)
1524 {
1525 cp_parser_context *context;
1526
1527 /* Allocate the storage. */
1528 if (cp_parser_context_free_list != NULL)
1529 {
1530 /* Pull the first entry from the free list. */
1531 context = cp_parser_context_free_list;
1532 cp_parser_context_free_list = context->next;
1533 memset (context, 0, sizeof (*context));
1534 }
1535 else
1536 context = ggc_alloc_cleared_cp_parser_context ();
1537
1538 /* No errors have occurred yet in this context. */
1539 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1540 /* If this is not the bottommost context, copy information that we
1541 need from the previous context. */
1542 if (next)
1543 {
1544 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1545 expression, then we are parsing one in this context, too. */
1546 context->object_type = next->object_type;
1547 /* Thread the stack. */
1548 context->next = next;
1549 }
1550
1551 return context;
1552 }
1553
1554 /* An entry in a queue of function arguments that require post-processing. */
1555
1556 typedef struct GTY(()) cp_default_arg_entry_d {
1557 /* The current_class_type when we parsed this arg. */
1558 tree class_type;
1559
1560 /* The function decl itself. */
1561 tree decl;
1562 } cp_default_arg_entry;
1563
1564 DEF_VEC_O(cp_default_arg_entry);
1565 DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1566
1567 /* An entry in a stack for member functions of local classes. */
1568
1569 typedef struct GTY(()) cp_unparsed_functions_entry_d {
1570 /* Functions with default arguments that require post-processing.
1571 Functions appear in this list in declaration order. */
1572 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1573
1574 /* Functions with defintions that require post-processing. Functions
1575 appear in this list in declaration order. */
1576 VEC(tree,gc) *funs_with_definitions;
1577 } cp_unparsed_functions_entry;
1578
1579 DEF_VEC_O(cp_unparsed_functions_entry);
1580 DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1581
1582 /* The cp_parser structure represents the C++ parser. */
1583
1584 typedef struct GTY(()) cp_parser {
1585 /* The lexer from which we are obtaining tokens. */
1586 cp_lexer *lexer;
1587
1588 /* The scope in which names should be looked up. If NULL_TREE, then
1589 we look up names in the scope that is currently open in the
1590 source program. If non-NULL, this is either a TYPE or
1591 NAMESPACE_DECL for the scope in which we should look. It can
1592 also be ERROR_MARK, when we've parsed a bogus scope.
1593
1594 This value is not cleared automatically after a name is looked
1595 up, so we must be careful to clear it before starting a new look
1596 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1597 will look up `Z' in the scope of `X', rather than the current
1598 scope.) Unfortunately, it is difficult to tell when name lookup
1599 is complete, because we sometimes peek at a token, look it up,
1600 and then decide not to consume it. */
1601 tree scope;
1602
1603 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1604 last lookup took place. OBJECT_SCOPE is used if an expression
1605 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1606 respectively. QUALIFYING_SCOPE is used for an expression of the
1607 form "X::Y"; it refers to X. */
1608 tree object_scope;
1609 tree qualifying_scope;
1610
1611 /* A stack of parsing contexts. All but the bottom entry on the
1612 stack will be tentative contexts.
1613
1614 We parse tentatively in order to determine which construct is in
1615 use in some situations. For example, in order to determine
1616 whether a statement is an expression-statement or a
1617 declaration-statement we parse it tentatively as a
1618 declaration-statement. If that fails, we then reparse the same
1619 token stream as an expression-statement. */
1620 cp_parser_context *context;
1621
1622 /* True if we are parsing GNU C++. If this flag is not set, then
1623 GNU extensions are not recognized. */
1624 bool allow_gnu_extensions_p;
1625
1626 /* TRUE if the `>' token should be interpreted as the greater-than
1627 operator. FALSE if it is the end of a template-id or
1628 template-parameter-list. In C++0x mode, this flag also applies to
1629 `>>' tokens, which are viewed as two consecutive `>' tokens when
1630 this flag is FALSE. */
1631 bool greater_than_is_operator_p;
1632
1633 /* TRUE if default arguments are allowed within a parameter list
1634 that starts at this point. FALSE if only a gnu extension makes
1635 them permissible. */
1636 bool default_arg_ok_p;
1637
1638 /* TRUE if we are parsing an integral constant-expression. See
1639 [expr.const] for a precise definition. */
1640 bool integral_constant_expression_p;
1641
1642 /* TRUE if we are parsing an integral constant-expression -- but a
1643 non-constant expression should be permitted as well. This flag
1644 is used when parsing an array bound so that GNU variable-length
1645 arrays are tolerated. */
1646 bool allow_non_integral_constant_expression_p;
1647
1648 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1649 been seen that makes the expression non-constant. */
1650 bool non_integral_constant_expression_p;
1651
1652 /* TRUE if local variable names and `this' are forbidden in the
1653 current context. */
1654 bool local_variables_forbidden_p;
1655
1656 /* TRUE if the declaration we are parsing is part of a
1657 linkage-specification of the form `extern string-literal
1658 declaration'. */
1659 bool in_unbraced_linkage_specification_p;
1660
1661 /* TRUE if we are presently parsing a declarator, after the
1662 direct-declarator. */
1663 bool in_declarator_p;
1664
1665 /* TRUE if we are presently parsing a template-argument-list. */
1666 bool in_template_argument_list_p;
1667
1668 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1669 to IN_OMP_BLOCK if parsing OpenMP structured block and
1670 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1671 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1672 iteration-statement, OpenMP block or loop within that switch. */
1673 #define IN_SWITCH_STMT 1
1674 #define IN_ITERATION_STMT 2
1675 #define IN_OMP_BLOCK 4
1676 #define IN_OMP_FOR 8
1677 #define IN_IF_STMT 16
1678 unsigned char in_statement;
1679
1680 /* TRUE if we are presently parsing the body of a switch statement.
1681 Note that this doesn't quite overlap with in_statement above.
1682 The difference relates to giving the right sets of error messages:
1683 "case not in switch" vs "break statement used with OpenMP...". */
1684 bool in_switch_statement_p;
1685
1686 /* TRUE if we are parsing a type-id in an expression context. In
1687 such a situation, both "type (expr)" and "type (type)" are valid
1688 alternatives. */
1689 bool in_type_id_in_expr_p;
1690
1691 /* TRUE if we are currently in a header file where declarations are
1692 implicitly extern "C". */
1693 bool implicit_extern_c;
1694
1695 /* TRUE if strings in expressions should be translated to the execution
1696 character set. */
1697 bool translate_strings_p;
1698
1699 /* TRUE if we are presently parsing the body of a function, but not
1700 a local class. */
1701 bool in_function_body;
1702
1703 /* TRUE if we can auto-correct a colon to a scope operator. */
1704 bool colon_corrects_to_scope_p;
1705
1706 /* If non-NULL, then we are parsing a construct where new type
1707 definitions are not permitted. The string stored here will be
1708 issued as an error message if a type is defined. */
1709 const char *type_definition_forbidden_message;
1710
1711 /* A stack used for member functions of local classes. The lists
1712 contained in an individual entry can only be processed once the
1713 outermost class being defined is complete. */
1714 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
1715
1716 /* The number of classes whose definitions are currently in
1717 progress. */
1718 unsigned num_classes_being_defined;
1719
1720 /* The number of template parameter lists that apply directly to the
1721 current declaration. */
1722 unsigned num_template_parameter_lists;
1723 } cp_parser;
1724
1725 /* Managing the unparsed function queues. */
1726
1727 #define unparsed_funs_with_default_args \
1728 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1729 #define unparsed_funs_with_definitions \
1730 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1731
1732 static void
1733 push_unparsed_function_queues (cp_parser *parser)
1734 {
1735 VEC_safe_push (cp_unparsed_functions_entry, gc,
1736 parser->unparsed_queues, NULL);
1737 unparsed_funs_with_default_args = NULL;
1738 unparsed_funs_with_definitions = make_tree_vector ();
1739 }
1740
1741 static void
1742 pop_unparsed_function_queues (cp_parser *parser)
1743 {
1744 release_tree_vector (unparsed_funs_with_definitions);
1745 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1746 }
1747
1748 /* Prototypes. */
1749
1750 /* Constructors and destructors. */
1751
1752 static cp_parser *cp_parser_new
1753 (void);
1754
1755 /* Routines to parse various constructs.
1756
1757 Those that return `tree' will return the error_mark_node (rather
1758 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1759 Sometimes, they will return an ordinary node if error-recovery was
1760 attempted, even though a parse error occurred. So, to check
1761 whether or not a parse error occurred, you should always use
1762 cp_parser_error_occurred. If the construct is optional (indicated
1763 either by an `_opt' in the name of the function that does the
1764 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1765 the construct is not present. */
1766
1767 /* Lexical conventions [gram.lex] */
1768
1769 static tree cp_parser_identifier
1770 (cp_parser *);
1771 static tree cp_parser_string_literal
1772 (cp_parser *, bool, bool);
1773
1774 /* Basic concepts [gram.basic] */
1775
1776 static bool cp_parser_translation_unit
1777 (cp_parser *);
1778
1779 /* Expressions [gram.expr] */
1780
1781 static tree cp_parser_primary_expression
1782 (cp_parser *, bool, bool, bool, cp_id_kind *);
1783 static tree cp_parser_id_expression
1784 (cp_parser *, bool, bool, bool *, bool, bool);
1785 static tree cp_parser_unqualified_id
1786 (cp_parser *, bool, bool, bool, bool);
1787 static tree cp_parser_nested_name_specifier_opt
1788 (cp_parser *, bool, bool, bool, bool);
1789 static tree cp_parser_nested_name_specifier
1790 (cp_parser *, bool, bool, bool, bool);
1791 static tree cp_parser_qualifying_entity
1792 (cp_parser *, bool, bool, bool, bool, bool);
1793 static tree cp_parser_postfix_expression
1794 (cp_parser *, bool, bool, bool, cp_id_kind *);
1795 static tree cp_parser_postfix_open_square_expression
1796 (cp_parser *, tree, bool);
1797 static tree cp_parser_postfix_dot_deref_expression
1798 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1799 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1800 (cp_parser *, int, bool, bool, bool *);
1801 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1802 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1803 static void cp_parser_pseudo_destructor_name
1804 (cp_parser *, tree *, tree *);
1805 static tree cp_parser_unary_expression
1806 (cp_parser *, bool, bool, cp_id_kind *);
1807 static enum tree_code cp_parser_unary_operator
1808 (cp_token *);
1809 static tree cp_parser_new_expression
1810 (cp_parser *);
1811 static VEC(tree,gc) *cp_parser_new_placement
1812 (cp_parser *);
1813 static tree cp_parser_new_type_id
1814 (cp_parser *, tree *);
1815 static cp_declarator *cp_parser_new_declarator_opt
1816 (cp_parser *);
1817 static cp_declarator *cp_parser_direct_new_declarator
1818 (cp_parser *);
1819 static VEC(tree,gc) *cp_parser_new_initializer
1820 (cp_parser *);
1821 static tree cp_parser_delete_expression
1822 (cp_parser *);
1823 static tree cp_parser_cast_expression
1824 (cp_parser *, bool, bool, cp_id_kind *);
1825 static tree cp_parser_binary_expression
1826 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1827 static tree cp_parser_question_colon_clause
1828 (cp_parser *, tree);
1829 static tree cp_parser_assignment_expression
1830 (cp_parser *, bool, cp_id_kind *);
1831 static enum tree_code cp_parser_assignment_operator_opt
1832 (cp_parser *);
1833 static tree cp_parser_expression
1834 (cp_parser *, bool, cp_id_kind *);
1835 static tree cp_parser_constant_expression
1836 (cp_parser *, bool, bool *);
1837 static tree cp_parser_builtin_offsetof
1838 (cp_parser *);
1839 static tree cp_parser_lambda_expression
1840 (cp_parser *);
1841 static void cp_parser_lambda_introducer
1842 (cp_parser *, tree);
1843 static void cp_parser_lambda_declarator_opt
1844 (cp_parser *, tree);
1845 static void cp_parser_lambda_body
1846 (cp_parser *, tree);
1847
1848 /* Statements [gram.stmt.stmt] */
1849
1850 static void cp_parser_statement
1851 (cp_parser *, tree, bool, bool *);
1852 static void cp_parser_label_for_labeled_statement
1853 (cp_parser *);
1854 static tree cp_parser_expression_statement
1855 (cp_parser *, tree);
1856 static tree cp_parser_compound_statement
1857 (cp_parser *, tree, bool);
1858 static void cp_parser_statement_seq_opt
1859 (cp_parser *, tree);
1860 static tree cp_parser_selection_statement
1861 (cp_parser *, bool *);
1862 static tree cp_parser_condition
1863 (cp_parser *);
1864 static tree cp_parser_iteration_statement
1865 (cp_parser *);
1866 static void cp_parser_for_init_statement
1867 (cp_parser *);
1868 static tree cp_parser_c_for
1869 (cp_parser *);
1870 static tree cp_parser_range_for
1871 (cp_parser *);
1872 static tree cp_parser_jump_statement
1873 (cp_parser *);
1874 static void cp_parser_declaration_statement
1875 (cp_parser *);
1876
1877 static tree cp_parser_implicitly_scoped_statement
1878 (cp_parser *, bool *);
1879 static void cp_parser_already_scoped_statement
1880 (cp_parser *);
1881
1882 /* Declarations [gram.dcl.dcl] */
1883
1884 static void cp_parser_declaration_seq_opt
1885 (cp_parser *);
1886 static void cp_parser_declaration
1887 (cp_parser *);
1888 static void cp_parser_block_declaration
1889 (cp_parser *, bool);
1890 static void cp_parser_simple_declaration
1891 (cp_parser *, bool);
1892 static void cp_parser_decl_specifier_seq
1893 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1894 static tree cp_parser_storage_class_specifier_opt
1895 (cp_parser *);
1896 static tree cp_parser_function_specifier_opt
1897 (cp_parser *, cp_decl_specifier_seq *);
1898 static tree cp_parser_type_specifier
1899 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1900 int *, bool *);
1901 static tree cp_parser_simple_type_specifier
1902 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1903 static tree cp_parser_type_name
1904 (cp_parser *);
1905 static tree cp_parser_nonclass_name
1906 (cp_parser* parser);
1907 static tree cp_parser_elaborated_type_specifier
1908 (cp_parser *, bool, bool);
1909 static tree cp_parser_enum_specifier
1910 (cp_parser *);
1911 static void cp_parser_enumerator_list
1912 (cp_parser *, tree);
1913 static void cp_parser_enumerator_definition
1914 (cp_parser *, tree);
1915 static tree cp_parser_namespace_name
1916 (cp_parser *);
1917 static void cp_parser_namespace_definition
1918 (cp_parser *);
1919 static void cp_parser_namespace_body
1920 (cp_parser *);
1921 static tree cp_parser_qualified_namespace_specifier
1922 (cp_parser *);
1923 static void cp_parser_namespace_alias_definition
1924 (cp_parser *);
1925 static bool cp_parser_using_declaration
1926 (cp_parser *, bool);
1927 static void cp_parser_using_directive
1928 (cp_parser *);
1929 static void cp_parser_asm_definition
1930 (cp_parser *);
1931 static void cp_parser_linkage_specification
1932 (cp_parser *);
1933 static void cp_parser_static_assert
1934 (cp_parser *, bool);
1935 static tree cp_parser_decltype
1936 (cp_parser *);
1937
1938 /* Declarators [gram.dcl.decl] */
1939
1940 static tree cp_parser_init_declarator
1941 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1942 static cp_declarator *cp_parser_declarator
1943 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1944 static cp_declarator *cp_parser_direct_declarator
1945 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1946 static enum tree_code cp_parser_ptr_operator
1947 (cp_parser *, tree *, cp_cv_quals *);
1948 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1949 (cp_parser *);
1950 static tree cp_parser_late_return_type_opt
1951 (cp_parser *);
1952 static tree cp_parser_declarator_id
1953 (cp_parser *, bool);
1954 static tree cp_parser_type_id
1955 (cp_parser *);
1956 static tree cp_parser_template_type_arg
1957 (cp_parser *);
1958 static tree cp_parser_trailing_type_id (cp_parser *);
1959 static tree cp_parser_type_id_1
1960 (cp_parser *, bool, bool);
1961 static void cp_parser_type_specifier_seq
1962 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1963 static tree cp_parser_parameter_declaration_clause
1964 (cp_parser *);
1965 static tree cp_parser_parameter_declaration_list
1966 (cp_parser *, bool *);
1967 static cp_parameter_declarator *cp_parser_parameter_declaration
1968 (cp_parser *, bool, bool *);
1969 static tree cp_parser_default_argument
1970 (cp_parser *, bool);
1971 static void cp_parser_function_body
1972 (cp_parser *);
1973 static tree cp_parser_initializer
1974 (cp_parser *, bool *, bool *);
1975 static tree cp_parser_initializer_clause
1976 (cp_parser *, bool *);
1977 static tree cp_parser_braced_list
1978 (cp_parser*, bool*);
1979 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1980 (cp_parser *, bool *);
1981
1982 static bool cp_parser_ctor_initializer_opt_and_function_body
1983 (cp_parser *);
1984
1985 /* Classes [gram.class] */
1986
1987 static tree cp_parser_class_name
1988 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1989 static tree cp_parser_class_specifier
1990 (cp_parser *);
1991 static tree cp_parser_class_head
1992 (cp_parser *, bool *, tree *, tree *);
1993 static enum tag_types cp_parser_class_key
1994 (cp_parser *);
1995 static void cp_parser_member_specification_opt
1996 (cp_parser *);
1997 static void cp_parser_member_declaration
1998 (cp_parser *);
1999 static tree cp_parser_pure_specifier
2000 (cp_parser *);
2001 static tree cp_parser_constant_initializer
2002 (cp_parser *);
2003
2004 /* Derived classes [gram.class.derived] */
2005
2006 static tree cp_parser_base_clause
2007 (cp_parser *);
2008 static tree cp_parser_base_specifier
2009 (cp_parser *);
2010
2011 /* Special member functions [gram.special] */
2012
2013 static tree cp_parser_conversion_function_id
2014 (cp_parser *);
2015 static tree cp_parser_conversion_type_id
2016 (cp_parser *);
2017 static cp_declarator *cp_parser_conversion_declarator_opt
2018 (cp_parser *);
2019 static bool cp_parser_ctor_initializer_opt
2020 (cp_parser *);
2021 static void cp_parser_mem_initializer_list
2022 (cp_parser *);
2023 static tree cp_parser_mem_initializer
2024 (cp_parser *);
2025 static tree cp_parser_mem_initializer_id
2026 (cp_parser *);
2027
2028 /* Overloading [gram.over] */
2029
2030 static tree cp_parser_operator_function_id
2031 (cp_parser *);
2032 static tree cp_parser_operator
2033 (cp_parser *);
2034
2035 /* Templates [gram.temp] */
2036
2037 static void cp_parser_template_declaration
2038 (cp_parser *, bool);
2039 static tree cp_parser_template_parameter_list
2040 (cp_parser *);
2041 static tree cp_parser_template_parameter
2042 (cp_parser *, bool *, bool *);
2043 static tree cp_parser_type_parameter
2044 (cp_parser *, bool *);
2045 static tree cp_parser_template_id
2046 (cp_parser *, bool, bool, bool);
2047 static tree cp_parser_template_name
2048 (cp_parser *, bool, bool, bool, bool *);
2049 static tree cp_parser_template_argument_list
2050 (cp_parser *);
2051 static tree cp_parser_template_argument
2052 (cp_parser *);
2053 static void cp_parser_explicit_instantiation
2054 (cp_parser *);
2055 static void cp_parser_explicit_specialization
2056 (cp_parser *);
2057
2058 /* Exception handling [gram.exception] */
2059
2060 static tree cp_parser_try_block
2061 (cp_parser *);
2062 static bool cp_parser_function_try_block
2063 (cp_parser *);
2064 static void cp_parser_handler_seq
2065 (cp_parser *);
2066 static void cp_parser_handler
2067 (cp_parser *);
2068 static tree cp_parser_exception_declaration
2069 (cp_parser *);
2070 static tree cp_parser_throw_expression
2071 (cp_parser *);
2072 static tree cp_parser_exception_specification_opt
2073 (cp_parser *);
2074 static tree cp_parser_type_id_list
2075 (cp_parser *);
2076
2077 /* GNU Extensions */
2078
2079 static tree cp_parser_asm_specification_opt
2080 (cp_parser *);
2081 static tree cp_parser_asm_operand_list
2082 (cp_parser *);
2083 static tree cp_parser_asm_clobber_list
2084 (cp_parser *);
2085 static tree cp_parser_asm_label_list
2086 (cp_parser *);
2087 static tree cp_parser_attributes_opt
2088 (cp_parser *);
2089 static tree cp_parser_attribute_list
2090 (cp_parser *);
2091 static bool cp_parser_extension_opt
2092 (cp_parser *, int *);
2093 static void cp_parser_label_declaration
2094 (cp_parser *);
2095
2096 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2097 static bool cp_parser_pragma
2098 (cp_parser *, enum pragma_context);
2099
2100 /* Objective-C++ Productions */
2101
2102 static tree cp_parser_objc_message_receiver
2103 (cp_parser *);
2104 static tree cp_parser_objc_message_args
2105 (cp_parser *);
2106 static tree cp_parser_objc_message_expression
2107 (cp_parser *);
2108 static tree cp_parser_objc_encode_expression
2109 (cp_parser *);
2110 static tree cp_parser_objc_defs_expression
2111 (cp_parser *);
2112 static tree cp_parser_objc_protocol_expression
2113 (cp_parser *);
2114 static tree cp_parser_objc_selector_expression
2115 (cp_parser *);
2116 static tree cp_parser_objc_expression
2117 (cp_parser *);
2118 static bool cp_parser_objc_selector_p
2119 (enum cpp_ttype);
2120 static tree cp_parser_objc_selector
2121 (cp_parser *);
2122 static tree cp_parser_objc_protocol_refs_opt
2123 (cp_parser *);
2124 static void cp_parser_objc_declaration
2125 (cp_parser *, tree);
2126 static tree cp_parser_objc_statement
2127 (cp_parser *);
2128 static bool cp_parser_objc_valid_prefix_attributes
2129 (cp_parser *, tree *);
2130 static void cp_parser_objc_at_property_declaration
2131 (cp_parser *) ;
2132 static void cp_parser_objc_at_synthesize_declaration
2133 (cp_parser *) ;
2134 static void cp_parser_objc_at_dynamic_declaration
2135 (cp_parser *) ;
2136 static tree cp_parser_objc_struct_declaration
2137 (cp_parser *) ;
2138
2139 /* Utility Routines */
2140
2141 static tree cp_parser_lookup_name
2142 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2143 static tree cp_parser_lookup_name_simple
2144 (cp_parser *, tree, location_t);
2145 static tree cp_parser_maybe_treat_template_as_class
2146 (tree, bool);
2147 static bool cp_parser_check_declarator_template_parameters
2148 (cp_parser *, cp_declarator *, location_t);
2149 static bool cp_parser_check_template_parameters
2150 (cp_parser *, unsigned, location_t, cp_declarator *);
2151 static tree cp_parser_simple_cast_expression
2152 (cp_parser *);
2153 static tree cp_parser_global_scope_opt
2154 (cp_parser *, bool);
2155 static bool cp_parser_constructor_declarator_p
2156 (cp_parser *, bool);
2157 static tree cp_parser_function_definition_from_specifiers_and_declarator
2158 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2159 static tree cp_parser_function_definition_after_declarator
2160 (cp_parser *, bool);
2161 static void cp_parser_template_declaration_after_export
2162 (cp_parser *, bool);
2163 static void cp_parser_perform_template_parameter_access_checks
2164 (VEC (deferred_access_check,gc)*);
2165 static tree cp_parser_single_declaration
2166 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2167 static tree cp_parser_functional_cast
2168 (cp_parser *, tree);
2169 static tree cp_parser_save_member_function_body
2170 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2171 static tree cp_parser_enclosed_template_argument_list
2172 (cp_parser *);
2173 static void cp_parser_save_default_args
2174 (cp_parser *, tree);
2175 static void cp_parser_late_parsing_for_member
2176 (cp_parser *, tree);
2177 static void cp_parser_late_parsing_default_args
2178 (cp_parser *, tree);
2179 static tree cp_parser_sizeof_operand
2180 (cp_parser *, enum rid);
2181 static tree cp_parser_trait_expr
2182 (cp_parser *, enum rid);
2183 static bool cp_parser_declares_only_class_p
2184 (cp_parser *);
2185 static void cp_parser_set_storage_class
2186 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2187 static void cp_parser_set_decl_spec_type
2188 (cp_decl_specifier_seq *, tree, location_t, bool);
2189 static bool cp_parser_friend_p
2190 (const cp_decl_specifier_seq *);
2191 static void cp_parser_required_error
2192 (cp_parser *, required_token, bool);
2193 static cp_token *cp_parser_require
2194 (cp_parser *, enum cpp_ttype, required_token);
2195 static cp_token *cp_parser_require_keyword
2196 (cp_parser *, enum rid, required_token);
2197 static bool cp_parser_token_starts_function_definition_p
2198 (cp_token *);
2199 static bool cp_parser_next_token_starts_class_definition_p
2200 (cp_parser *);
2201 static bool cp_parser_next_token_ends_template_argument_p
2202 (cp_parser *);
2203 static bool cp_parser_nth_token_starts_template_argument_list_p
2204 (cp_parser *, size_t);
2205 static enum tag_types cp_parser_token_is_class_key
2206 (cp_token *);
2207 static void cp_parser_check_class_key
2208 (enum tag_types, tree type);
2209 static void cp_parser_check_access_in_redeclaration
2210 (tree type, location_t location);
2211 static bool cp_parser_optional_template_keyword
2212 (cp_parser *);
2213 static void cp_parser_pre_parsed_nested_name_specifier
2214 (cp_parser *);
2215 static bool cp_parser_cache_group
2216 (cp_parser *, enum cpp_ttype, unsigned);
2217 static void cp_parser_parse_tentatively
2218 (cp_parser *);
2219 static void cp_parser_commit_to_tentative_parse
2220 (cp_parser *);
2221 static void cp_parser_abort_tentative_parse
2222 (cp_parser *);
2223 static bool cp_parser_parse_definitely
2224 (cp_parser *);
2225 static inline bool cp_parser_parsing_tentatively
2226 (cp_parser *);
2227 static bool cp_parser_uncommitted_to_tentative_parse_p
2228 (cp_parser *);
2229 static void cp_parser_error
2230 (cp_parser *, const char *);
2231 static void cp_parser_name_lookup_error
2232 (cp_parser *, tree, tree, name_lookup_error, location_t);
2233 static bool cp_parser_simulate_error
2234 (cp_parser *);
2235 static bool cp_parser_check_type_definition
2236 (cp_parser *);
2237 static void cp_parser_check_for_definition_in_return_type
2238 (cp_declarator *, tree, location_t type_location);
2239 static void cp_parser_check_for_invalid_template_id
2240 (cp_parser *, tree, location_t location);
2241 static bool cp_parser_non_integral_constant_expression
2242 (cp_parser *, non_integral_constant);
2243 static void cp_parser_diagnose_invalid_type_name
2244 (cp_parser *, tree, tree, location_t);
2245 static bool cp_parser_parse_and_diagnose_invalid_type_name
2246 (cp_parser *);
2247 static int cp_parser_skip_to_closing_parenthesis
2248 (cp_parser *, bool, bool, bool);
2249 static void cp_parser_skip_to_end_of_statement
2250 (cp_parser *);
2251 static void cp_parser_consume_semicolon_at_end_of_statement
2252 (cp_parser *);
2253 static void cp_parser_skip_to_end_of_block_or_statement
2254 (cp_parser *);
2255 static bool cp_parser_skip_to_closing_brace
2256 (cp_parser *);
2257 static void cp_parser_skip_to_end_of_template_parameter_list
2258 (cp_parser *);
2259 static void cp_parser_skip_to_pragma_eol
2260 (cp_parser*, cp_token *);
2261 static bool cp_parser_error_occurred
2262 (cp_parser *);
2263 static bool cp_parser_allow_gnu_extensions_p
2264 (cp_parser *);
2265 static bool cp_parser_is_string_literal
2266 (cp_token *);
2267 static bool cp_parser_is_keyword
2268 (cp_token *, enum rid);
2269 static tree cp_parser_make_typename_type
2270 (cp_parser *, tree, tree, location_t location);
2271 static cp_declarator * cp_parser_make_indirect_declarator
2272 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2273
2274 /* Returns nonzero if we are parsing tentatively. */
2275
2276 static inline bool
2277 cp_parser_parsing_tentatively (cp_parser* parser)
2278 {
2279 return parser->context->next != NULL;
2280 }
2281
2282 /* Returns nonzero if TOKEN is a string literal. */
2283
2284 static bool
2285 cp_parser_is_string_literal (cp_token* token)
2286 {
2287 return (token->type == CPP_STRING ||
2288 token->type == CPP_STRING16 ||
2289 token->type == CPP_STRING32 ||
2290 token->type == CPP_WSTRING ||
2291 token->type == CPP_UTF8STRING);
2292 }
2293
2294 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2295
2296 static bool
2297 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2298 {
2299 return token->keyword == keyword;
2300 }
2301
2302 /* If not parsing tentatively, issue a diagnostic of the form
2303 FILE:LINE: MESSAGE before TOKEN
2304 where TOKEN is the next token in the input stream. MESSAGE
2305 (specified by the caller) is usually of the form "expected
2306 OTHER-TOKEN". */
2307
2308 static void
2309 cp_parser_error (cp_parser* parser, const char* gmsgid)
2310 {
2311 if (!cp_parser_simulate_error (parser))
2312 {
2313 cp_token *token = cp_lexer_peek_token (parser->lexer);
2314 /* This diagnostic makes more sense if it is tagged to the line
2315 of the token we just peeked at. */
2316 cp_lexer_set_source_position_from_token (token);
2317
2318 if (token->type == CPP_PRAGMA)
2319 {
2320 error_at (token->location,
2321 "%<#pragma%> is not allowed here");
2322 cp_parser_skip_to_pragma_eol (parser, token);
2323 return;
2324 }
2325
2326 c_parse_error (gmsgid,
2327 /* Because c_parser_error does not understand
2328 CPP_KEYWORD, keywords are treated like
2329 identifiers. */
2330 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2331 token->u.value, token->flags);
2332 }
2333 }
2334
2335 /* Issue an error about name-lookup failing. NAME is the
2336 IDENTIFIER_NODE DECL is the result of
2337 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2338 the thing that we hoped to find. */
2339
2340 static void
2341 cp_parser_name_lookup_error (cp_parser* parser,
2342 tree name,
2343 tree decl,
2344 name_lookup_error desired,
2345 location_t location)
2346 {
2347 /* If name lookup completely failed, tell the user that NAME was not
2348 declared. */
2349 if (decl == error_mark_node)
2350 {
2351 if (parser->scope && parser->scope != global_namespace)
2352 error_at (location, "%<%E::%E%> has not been declared",
2353 parser->scope, name);
2354 else if (parser->scope == global_namespace)
2355 error_at (location, "%<::%E%> has not been declared", name);
2356 else if (parser->object_scope
2357 && !CLASS_TYPE_P (parser->object_scope))
2358 error_at (location, "request for member %qE in non-class type %qT",
2359 name, parser->object_scope);
2360 else if (parser->object_scope)
2361 error_at (location, "%<%T::%E%> has not been declared",
2362 parser->object_scope, name);
2363 else
2364 error_at (location, "%qE has not been declared", name);
2365 }
2366 else if (parser->scope && parser->scope != global_namespace)
2367 {
2368 switch (desired)
2369 {
2370 case NLE_TYPE:
2371 error_at (location, "%<%E::%E%> is not a type",
2372 parser->scope, name);
2373 break;
2374 case NLE_CXX98:
2375 error_at (location, "%<%E::%E%> is not a class or namespace",
2376 parser->scope, name);
2377 break;
2378 case NLE_NOT_CXX98:
2379 error_at (location,
2380 "%<%E::%E%> is not a class, namespace, or enumeration",
2381 parser->scope, name);
2382 break;
2383 default:
2384 gcc_unreachable ();
2385
2386 }
2387 }
2388 else if (parser->scope == global_namespace)
2389 {
2390 switch (desired)
2391 {
2392 case NLE_TYPE:
2393 error_at (location, "%<::%E%> is not a type", name);
2394 break;
2395 case NLE_CXX98:
2396 error_at (location, "%<::%E%> is not a class or namespace", name);
2397 break;
2398 case NLE_NOT_CXX98:
2399 error_at (location,
2400 "%<::%E%> is not a class, namespace, or enumeration",
2401 name);
2402 break;
2403 default:
2404 gcc_unreachable ();
2405 }
2406 }
2407 else
2408 {
2409 switch (desired)
2410 {
2411 case NLE_TYPE:
2412 error_at (location, "%qE is not a type", name);
2413 break;
2414 case NLE_CXX98:
2415 error_at (location, "%qE is not a class or namespace", name);
2416 break;
2417 case NLE_NOT_CXX98:
2418 error_at (location,
2419 "%qE is not a class, namespace, or enumeration", name);
2420 break;
2421 default:
2422 gcc_unreachable ();
2423 }
2424 }
2425 }
2426
2427 /* If we are parsing tentatively, remember that an error has occurred
2428 during this tentative parse. Returns true if the error was
2429 simulated; false if a message should be issued by the caller. */
2430
2431 static bool
2432 cp_parser_simulate_error (cp_parser* parser)
2433 {
2434 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2435 {
2436 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2437 return true;
2438 }
2439 return false;
2440 }
2441
2442 /* Check for repeated decl-specifiers. */
2443
2444 static void
2445 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2446 location_t location)
2447 {
2448 int ds;
2449
2450 for (ds = ds_first; ds != ds_last; ++ds)
2451 {
2452 unsigned count = decl_specs->specs[ds];
2453 if (count < 2)
2454 continue;
2455 /* The "long" specifier is a special case because of "long long". */
2456 if (ds == ds_long)
2457 {
2458 if (count > 2)
2459 error_at (location, "%<long long long%> is too long for GCC");
2460 else
2461 pedwarn_cxx98 (location, OPT_Wlong_long,
2462 "ISO C++ 1998 does not support %<long long%>");
2463 }
2464 else if (count > 1)
2465 {
2466 static const char *const decl_spec_names[] = {
2467 "signed",
2468 "unsigned",
2469 "short",
2470 "long",
2471 "const",
2472 "volatile",
2473 "restrict",
2474 "inline",
2475 "virtual",
2476 "explicit",
2477 "friend",
2478 "typedef",
2479 "constexpr",
2480 "__complex",
2481 "__thread"
2482 };
2483 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2484 }
2485 }
2486 }
2487
2488 /* This function is called when a type is defined. If type
2489 definitions are forbidden at this point, an error message is
2490 issued. */
2491
2492 static bool
2493 cp_parser_check_type_definition (cp_parser* parser)
2494 {
2495 /* If types are forbidden here, issue a message. */
2496 if (parser->type_definition_forbidden_message)
2497 {
2498 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2499 in the message need to be interpreted. */
2500 error (parser->type_definition_forbidden_message);
2501 return false;
2502 }
2503 return true;
2504 }
2505
2506 /* This function is called when the DECLARATOR is processed. The TYPE
2507 was a type defined in the decl-specifiers. If it is invalid to
2508 define a type in the decl-specifiers for DECLARATOR, an error is
2509 issued. TYPE_LOCATION is the location of TYPE and is used
2510 for error reporting. */
2511
2512 static void
2513 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2514 tree type, location_t type_location)
2515 {
2516 /* [dcl.fct] forbids type definitions in return types.
2517 Unfortunately, it's not easy to know whether or not we are
2518 processing a return type until after the fact. */
2519 while (declarator
2520 && (declarator->kind == cdk_pointer
2521 || declarator->kind == cdk_reference
2522 || declarator->kind == cdk_ptrmem))
2523 declarator = declarator->declarator;
2524 if (declarator
2525 && declarator->kind == cdk_function)
2526 {
2527 error_at (type_location,
2528 "new types may not be defined in a return type");
2529 inform (type_location,
2530 "(perhaps a semicolon is missing after the definition of %qT)",
2531 type);
2532 }
2533 }
2534
2535 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2536 "<" in any valid C++ program. If the next token is indeed "<",
2537 issue a message warning the user about what appears to be an
2538 invalid attempt to form a template-id. LOCATION is the location
2539 of the type-specifier (TYPE) */
2540
2541 static void
2542 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2543 tree type, location_t location)
2544 {
2545 cp_token_position start = 0;
2546
2547 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2548 {
2549 if (TYPE_P (type))
2550 error_at (location, "%qT is not a template", type);
2551 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2552 error_at (location, "%qE is not a template", type);
2553 else
2554 error_at (location, "invalid template-id");
2555 /* Remember the location of the invalid "<". */
2556 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2557 start = cp_lexer_token_position (parser->lexer, true);
2558 /* Consume the "<". */
2559 cp_lexer_consume_token (parser->lexer);
2560 /* Parse the template arguments. */
2561 cp_parser_enclosed_template_argument_list (parser);
2562 /* Permanently remove the invalid template arguments so that
2563 this error message is not issued again. */
2564 if (start)
2565 cp_lexer_purge_tokens_after (parser->lexer, start);
2566 }
2567 }
2568
2569 /* If parsing an integral constant-expression, issue an error message
2570 about the fact that THING appeared and return true. Otherwise,
2571 return false. In either case, set
2572 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2573
2574 static bool
2575 cp_parser_non_integral_constant_expression (cp_parser *parser,
2576 non_integral_constant thing)
2577 {
2578 parser->non_integral_constant_expression_p = true;
2579 if (parser->integral_constant_expression_p)
2580 {
2581 if (!parser->allow_non_integral_constant_expression_p)
2582 {
2583 const char *msg = NULL;
2584 switch (thing)
2585 {
2586 case NIC_FLOAT:
2587 error ("floating-point literal "
2588 "cannot appear in a constant-expression");
2589 return true;
2590 case NIC_CAST:
2591 error ("a cast to a type other than an integral or "
2592 "enumeration type cannot appear in a "
2593 "constant-expression");
2594 return true;
2595 case NIC_TYPEID:
2596 error ("%<typeid%> operator "
2597 "cannot appear in a constant-expression");
2598 return true;
2599 case NIC_NCC:
2600 error ("non-constant compound literals "
2601 "cannot appear in a constant-expression");
2602 return true;
2603 case NIC_FUNC_CALL:
2604 error ("a function call "
2605 "cannot appear in a constant-expression");
2606 return true;
2607 case NIC_INC:
2608 error ("an increment "
2609 "cannot appear in a constant-expression");
2610 return true;
2611 case NIC_DEC:
2612 error ("an decrement "
2613 "cannot appear in a constant-expression");
2614 return true;
2615 case NIC_ARRAY_REF:
2616 error ("an array reference "
2617 "cannot appear in a constant-expression");
2618 return true;
2619 case NIC_ADDR_LABEL:
2620 error ("the address of a label "
2621 "cannot appear in a constant-expression");
2622 return true;
2623 case NIC_OVERLOADED:
2624 error ("calls to overloaded operators "
2625 "cannot appear in a constant-expression");
2626 return true;
2627 case NIC_ASSIGNMENT:
2628 error ("an assignment cannot appear in a constant-expression");
2629 return true;
2630 case NIC_COMMA:
2631 error ("a comma operator "
2632 "cannot appear in a constant-expression");
2633 return true;
2634 case NIC_CONSTRUCTOR:
2635 error ("a call to a constructor "
2636 "cannot appear in a constant-expression");
2637 return true;
2638 case NIC_THIS:
2639 msg = "this";
2640 break;
2641 case NIC_FUNC_NAME:
2642 msg = "__FUNCTION__";
2643 break;
2644 case NIC_PRETTY_FUNC:
2645 msg = "__PRETTY_FUNCTION__";
2646 break;
2647 case NIC_C99_FUNC:
2648 msg = "__func__";
2649 break;
2650 case NIC_VA_ARG:
2651 msg = "va_arg";
2652 break;
2653 case NIC_ARROW:
2654 msg = "->";
2655 break;
2656 case NIC_POINT:
2657 msg = ".";
2658 break;
2659 case NIC_STAR:
2660 msg = "*";
2661 break;
2662 case NIC_ADDR:
2663 msg = "&";
2664 break;
2665 case NIC_PREINCREMENT:
2666 msg = "++";
2667 break;
2668 case NIC_PREDECREMENT:
2669 msg = "--";
2670 break;
2671 case NIC_NEW:
2672 msg = "new";
2673 break;
2674 case NIC_DEL:
2675 msg = "delete";
2676 break;
2677 default:
2678 gcc_unreachable ();
2679 }
2680 if (msg)
2681 error ("%qs cannot appear in a constant-expression", msg);
2682 return true;
2683 }
2684 }
2685 return false;
2686 }
2687
2688 /* Emit a diagnostic for an invalid type name. SCOPE is the
2689 qualifying scope (or NULL, if none) for ID. This function commits
2690 to the current active tentative parse, if any. (Otherwise, the
2691 problematic construct might be encountered again later, resulting
2692 in duplicate error messages.) LOCATION is the location of ID. */
2693
2694 static void
2695 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2696 tree scope, tree id,
2697 location_t location)
2698 {
2699 tree decl, old_scope;
2700 /* Try to lookup the identifier. */
2701 old_scope = parser->scope;
2702 parser->scope = scope;
2703 decl = cp_parser_lookup_name_simple (parser, id, location);
2704 parser->scope = old_scope;
2705 /* If the lookup found a template-name, it means that the user forgot
2706 to specify an argument list. Emit a useful error message. */
2707 if (TREE_CODE (decl) == TEMPLATE_DECL)
2708 error_at (location,
2709 "invalid use of template-name %qE without an argument list",
2710 decl);
2711 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2712 error_at (location, "invalid use of destructor %qD as a type", id);
2713 else if (TREE_CODE (decl) == TYPE_DECL)
2714 /* Something like 'unsigned A a;' */
2715 error_at (location, "invalid combination of multiple type-specifiers");
2716 else if (!parser->scope)
2717 {
2718 /* Issue an error message. */
2719 error_at (location, "%qE does not name a type", id);
2720 /* If we're in a template class, it's possible that the user was
2721 referring to a type from a base class. For example:
2722
2723 template <typename T> struct A { typedef T X; };
2724 template <typename T> struct B : public A<T> { X x; };
2725
2726 The user should have said "typename A<T>::X". */
2727 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2728 inform (location, "C++0x %<constexpr%> only available with "
2729 "-std=c++0x or -std=gnu++0x");
2730 else if (processing_template_decl && current_class_type
2731 && TYPE_BINFO (current_class_type))
2732 {
2733 tree b;
2734
2735 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2736 b;
2737 b = TREE_CHAIN (b))
2738 {
2739 tree base_type = BINFO_TYPE (b);
2740 if (CLASS_TYPE_P (base_type)
2741 && dependent_type_p (base_type))
2742 {
2743 tree field;
2744 /* Go from a particular instantiation of the
2745 template (which will have an empty TYPE_FIELDs),
2746 to the main version. */
2747 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2748 for (field = TYPE_FIELDS (base_type);
2749 field;
2750 field = DECL_CHAIN (field))
2751 if (TREE_CODE (field) == TYPE_DECL
2752 && DECL_NAME (field) == id)
2753 {
2754 inform (location,
2755 "(perhaps %<typename %T::%E%> was intended)",
2756 BINFO_TYPE (b), id);
2757 break;
2758 }
2759 if (field)
2760 break;
2761 }
2762 }
2763 }
2764 }
2765 /* Here we diagnose qualified-ids where the scope is actually correct,
2766 but the identifier does not resolve to a valid type name. */
2767 else if (parser->scope != error_mark_node)
2768 {
2769 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2770 error_at (location, "%qE in namespace %qE does not name a type",
2771 id, parser->scope);
2772 else if (CLASS_TYPE_P (parser->scope)
2773 && constructor_name_p (id, parser->scope))
2774 {
2775 /* A<T>::A<T>() */
2776 error_at (location, "%<%T::%E%> names the constructor, not"
2777 " the type", parser->scope, id);
2778 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2779 error_at (location, "and %qT has no template constructors",
2780 parser->scope);
2781 }
2782 else if (TYPE_P (parser->scope)
2783 && dependent_scope_p (parser->scope))
2784 error_at (location, "need %<typename%> before %<%T::%E%> because "
2785 "%qT is a dependent scope",
2786 parser->scope, id, parser->scope);
2787 else if (TYPE_P (parser->scope))
2788 error_at (location, "%qE in class %qT does not name a type",
2789 id, parser->scope);
2790 else
2791 gcc_unreachable ();
2792 }
2793 cp_parser_commit_to_tentative_parse (parser);
2794 }
2795
2796 /* Check for a common situation where a type-name should be present,
2797 but is not, and issue a sensible error message. Returns true if an
2798 invalid type-name was detected.
2799
2800 The situation handled by this function are variable declarations of the
2801 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2802 Usually, `ID' should name a type, but if we got here it means that it
2803 does not. We try to emit the best possible error message depending on
2804 how exactly the id-expression looks like. */
2805
2806 static bool
2807 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2808 {
2809 tree id;
2810 cp_token *token = cp_lexer_peek_token (parser->lexer);
2811
2812 /* Avoid duplicate error about ambiguous lookup. */
2813 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2814 {
2815 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2816 if (next->type == CPP_NAME && next->ambiguous_p)
2817 goto out;
2818 }
2819
2820 cp_parser_parse_tentatively (parser);
2821 id = cp_parser_id_expression (parser,
2822 /*template_keyword_p=*/false,
2823 /*check_dependency_p=*/true,
2824 /*template_p=*/NULL,
2825 /*declarator_p=*/true,
2826 /*optional_p=*/false);
2827 /* If the next token is a (, this is a function with no explicit return
2828 type, i.e. constructor, destructor or conversion op. */
2829 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2830 || TREE_CODE (id) == TYPE_DECL)
2831 {
2832 cp_parser_abort_tentative_parse (parser);
2833 return false;
2834 }
2835 if (!cp_parser_parse_definitely (parser))
2836 return false;
2837
2838 /* Emit a diagnostic for the invalid type. */
2839 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2840 id, token->location);
2841 out:
2842 /* If we aren't in the middle of a declarator (i.e. in a
2843 parameter-declaration-clause), skip to the end of the declaration;
2844 there's no point in trying to process it. */
2845 if (!parser->in_declarator_p)
2846 cp_parser_skip_to_end_of_block_or_statement (parser);
2847 return true;
2848 }
2849
2850 /* Consume tokens up to, and including, the next non-nested closing `)'.
2851 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2852 are doing error recovery. Returns -1 if OR_COMMA is true and we
2853 found an unnested comma. */
2854
2855 static int
2856 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2857 bool recovering,
2858 bool or_comma,
2859 bool consume_paren)
2860 {
2861 unsigned paren_depth = 0;
2862 unsigned brace_depth = 0;
2863 unsigned square_depth = 0;
2864
2865 if (recovering && !or_comma
2866 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2867 return 0;
2868
2869 while (true)
2870 {
2871 cp_token * token = cp_lexer_peek_token (parser->lexer);
2872
2873 switch (token->type)
2874 {
2875 case CPP_EOF:
2876 case CPP_PRAGMA_EOL:
2877 /* If we've run out of tokens, then there is no closing `)'. */
2878 return 0;
2879
2880 /* This is good for lambda expression capture-lists. */
2881 case CPP_OPEN_SQUARE:
2882 ++square_depth;
2883 break;
2884 case CPP_CLOSE_SQUARE:
2885 if (!square_depth--)
2886 return 0;
2887 break;
2888
2889 case CPP_SEMICOLON:
2890 /* This matches the processing in skip_to_end_of_statement. */
2891 if (!brace_depth)
2892 return 0;
2893 break;
2894
2895 case CPP_OPEN_BRACE:
2896 ++brace_depth;
2897 break;
2898 case CPP_CLOSE_BRACE:
2899 if (!brace_depth--)
2900 return 0;
2901 break;
2902
2903 case CPP_COMMA:
2904 if (recovering && or_comma && !brace_depth && !paren_depth
2905 && !square_depth)
2906 return -1;
2907 break;
2908
2909 case CPP_OPEN_PAREN:
2910 if (!brace_depth)
2911 ++paren_depth;
2912 break;
2913
2914 case CPP_CLOSE_PAREN:
2915 if (!brace_depth && !paren_depth--)
2916 {
2917 if (consume_paren)
2918 cp_lexer_consume_token (parser->lexer);
2919 return 1;
2920 }
2921 break;
2922
2923 default:
2924 break;
2925 }
2926
2927 /* Consume the token. */
2928 cp_lexer_consume_token (parser->lexer);
2929 }
2930 }
2931
2932 /* Consume tokens until we reach the end of the current statement.
2933 Normally, that will be just before consuming a `;'. However, if a
2934 non-nested `}' comes first, then we stop before consuming that. */
2935
2936 static void
2937 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2938 {
2939 unsigned nesting_depth = 0;
2940
2941 while (true)
2942 {
2943 cp_token *token = cp_lexer_peek_token (parser->lexer);
2944
2945 switch (token->type)
2946 {
2947 case CPP_EOF:
2948 case CPP_PRAGMA_EOL:
2949 /* If we've run out of tokens, stop. */
2950 return;
2951
2952 case CPP_SEMICOLON:
2953 /* If the next token is a `;', we have reached the end of the
2954 statement. */
2955 if (!nesting_depth)
2956 return;
2957 break;
2958
2959 case CPP_CLOSE_BRACE:
2960 /* If this is a non-nested '}', stop before consuming it.
2961 That way, when confronted with something like:
2962
2963 { 3 + }
2964
2965 we stop before consuming the closing '}', even though we
2966 have not yet reached a `;'. */
2967 if (nesting_depth == 0)
2968 return;
2969
2970 /* If it is the closing '}' for a block that we have
2971 scanned, stop -- but only after consuming the token.
2972 That way given:
2973
2974 void f g () { ... }
2975 typedef int I;
2976
2977 we will stop after the body of the erroneously declared
2978 function, but before consuming the following `typedef'
2979 declaration. */
2980 if (--nesting_depth == 0)
2981 {
2982 cp_lexer_consume_token (parser->lexer);
2983 return;
2984 }
2985
2986 case CPP_OPEN_BRACE:
2987 ++nesting_depth;
2988 break;
2989
2990 default:
2991 break;
2992 }
2993
2994 /* Consume the token. */
2995 cp_lexer_consume_token (parser->lexer);
2996 }
2997 }
2998
2999 /* This function is called at the end of a statement or declaration.
3000 If the next token is a semicolon, it is consumed; otherwise, error
3001 recovery is attempted. */
3002
3003 static void
3004 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3005 {
3006 /* Look for the trailing `;'. */
3007 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3008 {
3009 /* If there is additional (erroneous) input, skip to the end of
3010 the statement. */
3011 cp_parser_skip_to_end_of_statement (parser);
3012 /* If the next token is now a `;', consume it. */
3013 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3014 cp_lexer_consume_token (parser->lexer);
3015 }
3016 }
3017
3018 /* Skip tokens until we have consumed an entire block, or until we
3019 have consumed a non-nested `;'. */
3020
3021 static void
3022 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3023 {
3024 int nesting_depth = 0;
3025
3026 while (nesting_depth >= 0)
3027 {
3028 cp_token *token = cp_lexer_peek_token (parser->lexer);
3029
3030 switch (token->type)
3031 {
3032 case CPP_EOF:
3033 case CPP_PRAGMA_EOL:
3034 /* If we've run out of tokens, stop. */
3035 return;
3036
3037 case CPP_SEMICOLON:
3038 /* Stop if this is an unnested ';'. */
3039 if (!nesting_depth)
3040 nesting_depth = -1;
3041 break;
3042
3043 case CPP_CLOSE_BRACE:
3044 /* Stop if this is an unnested '}', or closes the outermost
3045 nesting level. */
3046 nesting_depth--;
3047 if (nesting_depth < 0)
3048 return;
3049 if (!nesting_depth)
3050 nesting_depth = -1;
3051 break;
3052
3053 case CPP_OPEN_BRACE:
3054 /* Nest. */
3055 nesting_depth++;
3056 break;
3057
3058 default:
3059 break;
3060 }
3061
3062 /* Consume the token. */
3063 cp_lexer_consume_token (parser->lexer);
3064 }
3065 }
3066
3067 /* Skip tokens until a non-nested closing curly brace is the next
3068 token, or there are no more tokens. Return true in the first case,
3069 false otherwise. */
3070
3071 static bool
3072 cp_parser_skip_to_closing_brace (cp_parser *parser)
3073 {
3074 unsigned nesting_depth = 0;
3075
3076 while (true)
3077 {
3078 cp_token *token = cp_lexer_peek_token (parser->lexer);
3079
3080 switch (token->type)
3081 {
3082 case CPP_EOF:
3083 case CPP_PRAGMA_EOL:
3084 /* If we've run out of tokens, stop. */
3085 return false;
3086
3087 case CPP_CLOSE_BRACE:
3088 /* If the next token is a non-nested `}', then we have reached
3089 the end of the current block. */
3090 if (nesting_depth-- == 0)
3091 return true;
3092 break;
3093
3094 case CPP_OPEN_BRACE:
3095 /* If it the next token is a `{', then we are entering a new
3096 block. Consume the entire block. */
3097 ++nesting_depth;
3098 break;
3099
3100 default:
3101 break;
3102 }
3103
3104 /* Consume the token. */
3105 cp_lexer_consume_token (parser->lexer);
3106 }
3107 }
3108
3109 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3110 parameter is the PRAGMA token, allowing us to purge the entire pragma
3111 sequence. */
3112
3113 static void
3114 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3115 {
3116 cp_token *token;
3117
3118 parser->lexer->in_pragma = false;
3119
3120 do
3121 token = cp_lexer_consume_token (parser->lexer);
3122 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3123
3124 /* Ensure that the pragma is not parsed again. */
3125 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3126 }
3127
3128 /* Require pragma end of line, resyncing with it as necessary. The
3129 arguments are as for cp_parser_skip_to_pragma_eol. */
3130
3131 static void
3132 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3133 {
3134 parser->lexer->in_pragma = false;
3135 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3136 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3137 }
3138
3139 /* This is a simple wrapper around make_typename_type. When the id is
3140 an unresolved identifier node, we can provide a superior diagnostic
3141 using cp_parser_diagnose_invalid_type_name. */
3142
3143 static tree
3144 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3145 tree id, location_t id_location)
3146 {
3147 tree result;
3148 if (TREE_CODE (id) == IDENTIFIER_NODE)
3149 {
3150 result = make_typename_type (scope, id, typename_type,
3151 /*complain=*/tf_none);
3152 if (result == error_mark_node)
3153 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3154 return result;
3155 }
3156 return make_typename_type (scope, id, typename_type, tf_error);
3157 }
3158
3159 /* This is a wrapper around the
3160 make_{pointer,ptrmem,reference}_declarator functions that decides
3161 which one to call based on the CODE and CLASS_TYPE arguments. The
3162 CODE argument should be one of the values returned by
3163 cp_parser_ptr_operator. */
3164 static cp_declarator *
3165 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3166 cp_cv_quals cv_qualifiers,
3167 cp_declarator *target)
3168 {
3169 if (code == ERROR_MARK)
3170 return cp_error_declarator;
3171
3172 if (code == INDIRECT_REF)
3173 if (class_type == NULL_TREE)
3174 return make_pointer_declarator (cv_qualifiers, target);
3175 else
3176 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3177 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3178 return make_reference_declarator (cv_qualifiers, target, false);
3179 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3180 return make_reference_declarator (cv_qualifiers, target, true);
3181 gcc_unreachable ();
3182 }
3183
3184 /* Create a new C++ parser. */
3185
3186 static cp_parser *
3187 cp_parser_new (void)
3188 {
3189 cp_parser *parser;
3190 cp_lexer *lexer;
3191 unsigned i;
3192
3193 /* cp_lexer_new_main is called before doing GC allocation because
3194 cp_lexer_new_main might load a PCH file. */
3195 lexer = cp_lexer_new_main ();
3196
3197 /* Initialize the binops_by_token so that we can get the tree
3198 directly from the token. */
3199 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3200 binops_by_token[binops[i].token_type] = binops[i];
3201
3202 parser = ggc_alloc_cleared_cp_parser ();
3203 parser->lexer = lexer;
3204 parser->context = cp_parser_context_new (NULL);
3205
3206 /* For now, we always accept GNU extensions. */
3207 parser->allow_gnu_extensions_p = 1;
3208
3209 /* The `>' token is a greater-than operator, not the end of a
3210 template-id. */
3211 parser->greater_than_is_operator_p = true;
3212
3213 parser->default_arg_ok_p = true;
3214
3215 /* We are not parsing a constant-expression. */
3216 parser->integral_constant_expression_p = false;
3217 parser->allow_non_integral_constant_expression_p = false;
3218 parser->non_integral_constant_expression_p = false;
3219
3220 /* Local variable names are not forbidden. */
3221 parser->local_variables_forbidden_p = false;
3222
3223 /* We are not processing an `extern "C"' declaration. */
3224 parser->in_unbraced_linkage_specification_p = false;
3225
3226 /* We are not processing a declarator. */
3227 parser->in_declarator_p = false;
3228
3229 /* We are not processing a template-argument-list. */
3230 parser->in_template_argument_list_p = false;
3231
3232 /* We are not in an iteration statement. */
3233 parser->in_statement = 0;
3234
3235 /* We are not in a switch statement. */
3236 parser->in_switch_statement_p = false;
3237
3238 /* We are not parsing a type-id inside an expression. */
3239 parser->in_type_id_in_expr_p = false;
3240
3241 /* Declarations aren't implicitly extern "C". */
3242 parser->implicit_extern_c = false;
3243
3244 /* String literals should be translated to the execution character set. */
3245 parser->translate_strings_p = true;
3246
3247 /* We are not parsing a function body. */
3248 parser->in_function_body = false;
3249
3250 /* We can correct until told otherwise. */
3251 parser->colon_corrects_to_scope_p = true;
3252
3253 /* The unparsed function queue is empty. */
3254 push_unparsed_function_queues (parser);
3255
3256 /* There are no classes being defined. */
3257 parser->num_classes_being_defined = 0;
3258
3259 /* No template parameters apply. */
3260 parser->num_template_parameter_lists = 0;
3261
3262 return parser;
3263 }
3264
3265 /* Create a cp_lexer structure which will emit the tokens in CACHE
3266 and push it onto the parser's lexer stack. This is used for delayed
3267 parsing of in-class method bodies and default arguments, and should
3268 not be confused with tentative parsing. */
3269 static void
3270 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3271 {
3272 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3273 lexer->next = parser->lexer;
3274 parser->lexer = lexer;
3275
3276 /* Move the current source position to that of the first token in the
3277 new lexer. */
3278 cp_lexer_set_source_position_from_token (lexer->next_token);
3279 }
3280
3281 /* Pop the top lexer off the parser stack. This is never used for the
3282 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3283 static void
3284 cp_parser_pop_lexer (cp_parser *parser)
3285 {
3286 cp_lexer *lexer = parser->lexer;
3287 parser->lexer = lexer->next;
3288 cp_lexer_destroy (lexer);
3289
3290 /* Put the current source position back where it was before this
3291 lexer was pushed. */
3292 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3293 }
3294
3295 /* Lexical conventions [gram.lex] */
3296
3297 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3298 identifier. */
3299
3300 static tree
3301 cp_parser_identifier (cp_parser* parser)
3302 {
3303 cp_token *token;
3304
3305 /* Look for the identifier. */
3306 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3307 /* Return the value. */
3308 return token ? token->u.value : error_mark_node;
3309 }
3310
3311 /* Parse a sequence of adjacent string constants. Returns a
3312 TREE_STRING representing the combined, nul-terminated string
3313 constant. If TRANSLATE is true, translate the string to the
3314 execution character set. If WIDE_OK is true, a wide string is
3315 invalid here.
3316
3317 C++98 [lex.string] says that if a narrow string literal token is
3318 adjacent to a wide string literal token, the behavior is undefined.
3319 However, C99 6.4.5p4 says that this results in a wide string literal.
3320 We follow C99 here, for consistency with the C front end.
3321
3322 This code is largely lifted from lex_string() in c-lex.c.
3323
3324 FUTURE: ObjC++ will need to handle @-strings here. */
3325 static tree
3326 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3327 {
3328 tree value;
3329 size_t count;
3330 struct obstack str_ob;
3331 cpp_string str, istr, *strs;
3332 cp_token *tok;
3333 enum cpp_ttype type;
3334
3335 tok = cp_lexer_peek_token (parser->lexer);
3336 if (!cp_parser_is_string_literal (tok))
3337 {
3338 cp_parser_error (parser, "expected string-literal");
3339 return error_mark_node;
3340 }
3341
3342 type = tok->type;
3343
3344 /* Try to avoid the overhead of creating and destroying an obstack
3345 for the common case of just one string. */
3346 if (!cp_parser_is_string_literal
3347 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3348 {
3349 cp_lexer_consume_token (parser->lexer);
3350
3351 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3352 str.len = TREE_STRING_LENGTH (tok->u.value);
3353 count = 1;
3354
3355 strs = &str;
3356 }
3357 else
3358 {
3359 gcc_obstack_init (&str_ob);
3360 count = 0;
3361
3362 do
3363 {
3364 cp_lexer_consume_token (parser->lexer);
3365 count++;
3366 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3367 str.len = TREE_STRING_LENGTH (tok->u.value);
3368
3369 if (type != tok->type)
3370 {
3371 if (type == CPP_STRING)
3372 type = tok->type;
3373 else if (tok->type != CPP_STRING)
3374 error_at (tok->location,
3375 "unsupported non-standard concatenation "
3376 "of string literals");
3377 }
3378
3379 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3380
3381 tok = cp_lexer_peek_token (parser->lexer);
3382 }
3383 while (cp_parser_is_string_literal (tok));
3384
3385 strs = (cpp_string *) obstack_finish (&str_ob);
3386 }
3387
3388 if (type != CPP_STRING && !wide_ok)
3389 {
3390 cp_parser_error (parser, "a wide string is invalid in this context");
3391 type = CPP_STRING;
3392 }
3393
3394 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3395 (parse_in, strs, count, &istr, type))
3396 {
3397 value = build_string (istr.len, (const char *)istr.text);
3398 free (CONST_CAST (unsigned char *, istr.text));
3399
3400 switch (type)
3401 {
3402 default:
3403 case CPP_STRING:
3404 case CPP_UTF8STRING:
3405 TREE_TYPE (value) = char_array_type_node;
3406 break;
3407 case CPP_STRING16:
3408 TREE_TYPE (value) = char16_array_type_node;
3409 break;
3410 case CPP_STRING32:
3411 TREE_TYPE (value) = char32_array_type_node;
3412 break;
3413 case CPP_WSTRING:
3414 TREE_TYPE (value) = wchar_array_type_node;
3415 break;
3416 }
3417
3418 value = fix_string_type (value);
3419 }
3420 else
3421 /* cpp_interpret_string has issued an error. */
3422 value = error_mark_node;
3423
3424 if (count > 1)
3425 obstack_free (&str_ob, 0);
3426
3427 return value;
3428 }
3429
3430
3431 /* Basic concepts [gram.basic] */
3432
3433 /* Parse a translation-unit.
3434
3435 translation-unit:
3436 declaration-seq [opt]
3437
3438 Returns TRUE if all went well. */
3439
3440 static bool
3441 cp_parser_translation_unit (cp_parser* parser)
3442 {
3443 /* The address of the first non-permanent object on the declarator
3444 obstack. */
3445 static void *declarator_obstack_base;
3446
3447 bool success;
3448
3449 /* Create the declarator obstack, if necessary. */
3450 if (!cp_error_declarator)
3451 {
3452 gcc_obstack_init (&declarator_obstack);
3453 /* Create the error declarator. */
3454 cp_error_declarator = make_declarator (cdk_error);
3455 /* Create the empty parameter list. */
3456 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3457 /* Remember where the base of the declarator obstack lies. */
3458 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3459 }
3460
3461 cp_parser_declaration_seq_opt (parser);
3462
3463 /* If there are no tokens left then all went well. */
3464 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3465 {
3466 /* Get rid of the token array; we don't need it any more. */
3467 cp_lexer_destroy (parser->lexer);
3468 parser->lexer = NULL;
3469
3470 /* This file might have been a context that's implicitly extern
3471 "C". If so, pop the lang context. (Only relevant for PCH.) */
3472 if (parser->implicit_extern_c)
3473 {
3474 pop_lang_context ();
3475 parser->implicit_extern_c = false;
3476 }
3477
3478 /* Finish up. */
3479 finish_translation_unit ();
3480
3481 success = true;
3482 }
3483 else
3484 {
3485 cp_parser_error (parser, "expected declaration");
3486 success = false;
3487 }
3488
3489 /* Make sure the declarator obstack was fully cleaned up. */
3490 gcc_assert (obstack_next_free (&declarator_obstack)
3491 == declarator_obstack_base);
3492
3493 /* All went well. */
3494 return success;
3495 }
3496
3497 /* Expressions [gram.expr] */
3498
3499 /* Parse a primary-expression.
3500
3501 primary-expression:
3502 literal
3503 this
3504 ( expression )
3505 id-expression
3506
3507 GNU Extensions:
3508
3509 primary-expression:
3510 ( compound-statement )
3511 __builtin_va_arg ( assignment-expression , type-id )
3512 __builtin_offsetof ( type-id , offsetof-expression )
3513
3514 C++ Extensions:
3515 __has_nothrow_assign ( type-id )
3516 __has_nothrow_constructor ( type-id )
3517 __has_nothrow_copy ( type-id )
3518 __has_trivial_assign ( type-id )
3519 __has_trivial_constructor ( type-id )
3520 __has_trivial_copy ( type-id )
3521 __has_trivial_destructor ( type-id )
3522 __has_virtual_destructor ( type-id )
3523 __is_abstract ( type-id )
3524 __is_base_of ( type-id , type-id )
3525 __is_class ( type-id )
3526 __is_convertible_to ( type-id , type-id )
3527 __is_empty ( type-id )
3528 __is_enum ( type-id )
3529 __is_pod ( type-id )
3530 __is_polymorphic ( type-id )
3531 __is_union ( type-id )
3532
3533 Objective-C++ Extension:
3534
3535 primary-expression:
3536 objc-expression
3537
3538 literal:
3539 __null
3540
3541 ADDRESS_P is true iff this expression was immediately preceded by
3542 "&" and therefore might denote a pointer-to-member. CAST_P is true
3543 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3544 true iff this expression is a template argument.
3545
3546 Returns a representation of the expression. Upon return, *IDK
3547 indicates what kind of id-expression (if any) was present. */
3548
3549 static tree
3550 cp_parser_primary_expression (cp_parser *parser,
3551 bool address_p,
3552 bool cast_p,
3553 bool template_arg_p,
3554 cp_id_kind *idk)
3555 {
3556 cp_token *token = NULL;
3557
3558 /* Assume the primary expression is not an id-expression. */
3559 *idk = CP_ID_KIND_NONE;
3560
3561 /* Peek at the next token. */
3562 token = cp_lexer_peek_token (parser->lexer);
3563 switch (token->type)
3564 {
3565 /* literal:
3566 integer-literal
3567 character-literal
3568 floating-literal
3569 string-literal
3570 boolean-literal */
3571 case CPP_CHAR:
3572 case CPP_CHAR16:
3573 case CPP_CHAR32:
3574 case CPP_WCHAR:
3575 case CPP_NUMBER:
3576 token = cp_lexer_consume_token (parser->lexer);
3577 if (TREE_CODE (token->u.value) == FIXED_CST)
3578 {
3579 error_at (token->location,
3580 "fixed-point types not supported in C++");
3581 return error_mark_node;
3582 }
3583 /* Floating-point literals are only allowed in an integral
3584 constant expression if they are cast to an integral or
3585 enumeration type. */
3586 if (TREE_CODE (token->u.value) == REAL_CST
3587 && parser->integral_constant_expression_p
3588 && pedantic)
3589 {
3590 /* CAST_P will be set even in invalid code like "int(2.7 +
3591 ...)". Therefore, we have to check that the next token
3592 is sure to end the cast. */
3593 if (cast_p)
3594 {
3595 cp_token *next_token;
3596
3597 next_token = cp_lexer_peek_token (parser->lexer);
3598 if (/* The comma at the end of an
3599 enumerator-definition. */
3600 next_token->type != CPP_COMMA
3601 /* The curly brace at the end of an enum-specifier. */
3602 && next_token->type != CPP_CLOSE_BRACE
3603 /* The end of a statement. */
3604 && next_token->type != CPP_SEMICOLON
3605 /* The end of the cast-expression. */
3606 && next_token->type != CPP_CLOSE_PAREN
3607 /* The end of an array bound. */
3608 && next_token->type != CPP_CLOSE_SQUARE
3609 /* The closing ">" in a template-argument-list. */
3610 && (next_token->type != CPP_GREATER
3611 || parser->greater_than_is_operator_p)
3612 /* C++0x only: A ">>" treated like two ">" tokens,
3613 in a template-argument-list. */
3614 && (next_token->type != CPP_RSHIFT
3615 || (cxx_dialect == cxx98)
3616 || parser->greater_than_is_operator_p))
3617 cast_p = false;
3618 }
3619
3620 /* If we are within a cast, then the constraint that the
3621 cast is to an integral or enumeration type will be
3622 checked at that point. If we are not within a cast, then
3623 this code is invalid. */
3624 if (!cast_p)
3625 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3626 }
3627 return token->u.value;
3628
3629 case CPP_STRING:
3630 case CPP_STRING16:
3631 case CPP_STRING32:
3632 case CPP_WSTRING:
3633 case CPP_UTF8STRING:
3634 /* ??? Should wide strings be allowed when parser->translate_strings_p
3635 is false (i.e. in attributes)? If not, we can kill the third
3636 argument to cp_parser_string_literal. */
3637 return cp_parser_string_literal (parser,
3638 parser->translate_strings_p,
3639 true);
3640
3641 case CPP_OPEN_PAREN:
3642 {
3643 tree expr;
3644 bool saved_greater_than_is_operator_p;
3645
3646 /* Consume the `('. */
3647 cp_lexer_consume_token (parser->lexer);
3648 /* Within a parenthesized expression, a `>' token is always
3649 the greater-than operator. */
3650 saved_greater_than_is_operator_p
3651 = parser->greater_than_is_operator_p;
3652 parser->greater_than_is_operator_p = true;
3653 /* If we see `( { ' then we are looking at the beginning of
3654 a GNU statement-expression. */
3655 if (cp_parser_allow_gnu_extensions_p (parser)
3656 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3657 {
3658 /* Statement-expressions are not allowed by the standard. */
3659 pedwarn (token->location, OPT_pedantic,
3660 "ISO C++ forbids braced-groups within expressions");
3661
3662 /* And they're not allowed outside of a function-body; you
3663 cannot, for example, write:
3664
3665 int i = ({ int j = 3; j + 1; });
3666
3667 at class or namespace scope. */
3668 if (!parser->in_function_body
3669 || parser->in_template_argument_list_p)
3670 {
3671 error_at (token->location,
3672 "statement-expressions are not allowed outside "
3673 "functions nor in template-argument lists");
3674 cp_parser_skip_to_end_of_block_or_statement (parser);
3675 expr = error_mark_node;
3676 }
3677 else
3678 {
3679 /* Start the statement-expression. */
3680 expr = begin_stmt_expr ();
3681 /* Parse the compound-statement. */
3682 cp_parser_compound_statement (parser, expr, false);
3683 /* Finish up. */
3684 expr = finish_stmt_expr (expr, false);
3685 }
3686 }
3687 else
3688 {
3689 /* Parse the parenthesized expression. */
3690 expr = cp_parser_expression (parser, cast_p, idk);
3691 /* Let the front end know that this expression was
3692 enclosed in parentheses. This matters in case, for
3693 example, the expression is of the form `A::B', since
3694 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3695 not. */
3696 finish_parenthesized_expr (expr);
3697 }
3698 /* The `>' token might be the end of a template-id or
3699 template-parameter-list now. */
3700 parser->greater_than_is_operator_p
3701 = saved_greater_than_is_operator_p;
3702 /* Consume the `)'. */
3703 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3704 cp_parser_skip_to_end_of_statement (parser);
3705
3706 return expr;
3707 }
3708
3709 case CPP_OPEN_SQUARE:
3710 if (c_dialect_objc ())
3711 /* We have an Objective-C++ message. */
3712 return cp_parser_objc_expression (parser);
3713 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3714 return cp_parser_lambda_expression (parser);
3715
3716 case CPP_OBJC_STRING:
3717 if (c_dialect_objc ())
3718 /* We have an Objective-C++ string literal. */
3719 return cp_parser_objc_expression (parser);
3720 cp_parser_error (parser, "expected primary-expression");
3721 return error_mark_node;
3722
3723 case CPP_KEYWORD:
3724 switch (token->keyword)
3725 {
3726 /* These two are the boolean literals. */
3727 case RID_TRUE:
3728 cp_lexer_consume_token (parser->lexer);
3729 return boolean_true_node;
3730 case RID_FALSE:
3731 cp_lexer_consume_token (parser->lexer);
3732 return boolean_false_node;
3733
3734 /* The `__null' literal. */
3735 case RID_NULL:
3736 cp_lexer_consume_token (parser->lexer);
3737 return null_node;
3738
3739 /* The `nullptr' literal. */
3740 case RID_NULLPTR:
3741 cp_lexer_consume_token (parser->lexer);
3742 return nullptr_node;
3743
3744 /* Recognize the `this' keyword. */
3745 case RID_THIS:
3746 cp_lexer_consume_token (parser->lexer);
3747 if (parser->local_variables_forbidden_p)
3748 {
3749 error_at (token->location,
3750 "%<this%> may not be used in this context");
3751 return error_mark_node;
3752 }
3753 /* Pointers cannot appear in constant-expressions. */
3754 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3755 return error_mark_node;
3756 return finish_this_expr ();
3757
3758 /* The `operator' keyword can be the beginning of an
3759 id-expression. */
3760 case RID_OPERATOR:
3761 goto id_expression;
3762
3763 case RID_FUNCTION_NAME:
3764 case RID_PRETTY_FUNCTION_NAME:
3765 case RID_C99_FUNCTION_NAME:
3766 {
3767 non_integral_constant name;
3768
3769 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3770 __func__ are the names of variables -- but they are
3771 treated specially. Therefore, they are handled here,
3772 rather than relying on the generic id-expression logic
3773 below. Grammatically, these names are id-expressions.
3774
3775 Consume the token. */
3776 token = cp_lexer_consume_token (parser->lexer);
3777
3778 switch (token->keyword)
3779 {
3780 case RID_FUNCTION_NAME:
3781 name = NIC_FUNC_NAME;
3782 break;
3783 case RID_PRETTY_FUNCTION_NAME:
3784 name = NIC_PRETTY_FUNC;
3785 break;
3786 case RID_C99_FUNCTION_NAME:
3787 name = NIC_C99_FUNC;
3788 break;
3789 default:
3790 gcc_unreachable ();
3791 }
3792
3793 if (cp_parser_non_integral_constant_expression (parser, name))
3794 return error_mark_node;
3795
3796 /* Look up the name. */
3797 return finish_fname (token->u.value);
3798 }
3799
3800 case RID_VA_ARG:
3801 {
3802 tree expression;
3803 tree type;
3804
3805 /* The `__builtin_va_arg' construct is used to handle
3806 `va_arg'. Consume the `__builtin_va_arg' token. */
3807 cp_lexer_consume_token (parser->lexer);
3808 /* Look for the opening `('. */
3809 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3810 /* Now, parse the assignment-expression. */
3811 expression = cp_parser_assignment_expression (parser,
3812 /*cast_p=*/false, NULL);
3813 /* Look for the `,'. */
3814 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3815 /* Parse the type-id. */
3816 type = cp_parser_type_id (parser);
3817 /* Look for the closing `)'. */
3818 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3819 /* Using `va_arg' in a constant-expression is not
3820 allowed. */
3821 if (cp_parser_non_integral_constant_expression (parser,
3822 NIC_VA_ARG))
3823 return error_mark_node;
3824 return build_x_va_arg (expression, type);
3825 }
3826
3827 case RID_OFFSETOF:
3828 return cp_parser_builtin_offsetof (parser);
3829
3830 case RID_HAS_NOTHROW_ASSIGN:
3831 case RID_HAS_NOTHROW_CONSTRUCTOR:
3832 case RID_HAS_NOTHROW_COPY:
3833 case RID_HAS_TRIVIAL_ASSIGN:
3834 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3835 case RID_HAS_TRIVIAL_COPY:
3836 case RID_HAS_TRIVIAL_DESTRUCTOR:
3837 case RID_HAS_VIRTUAL_DESTRUCTOR:
3838 case RID_IS_ABSTRACT:
3839 case RID_IS_BASE_OF:
3840 case RID_IS_CLASS:
3841 case RID_IS_CONVERTIBLE_TO:
3842 case RID_IS_EMPTY:
3843 case RID_IS_ENUM:
3844 case RID_IS_POD:
3845 case RID_IS_POLYMORPHIC:
3846 case RID_IS_STD_LAYOUT:
3847 case RID_IS_TRIVIAL:
3848 case RID_IS_UNION:
3849 case RID_IS_LITERAL_TYPE:
3850 return cp_parser_trait_expr (parser, token->keyword);
3851
3852 /* Objective-C++ expressions. */
3853 case RID_AT_ENCODE:
3854 case RID_AT_PROTOCOL:
3855 case RID_AT_SELECTOR:
3856 return cp_parser_objc_expression (parser);
3857
3858 case RID_TEMPLATE:
3859 if (parser->in_function_body
3860 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3861 == CPP_LESS))
3862 {
3863 error_at (token->location,
3864 "a template declaration cannot appear at block scope");
3865 cp_parser_skip_to_end_of_block_or_statement (parser);
3866 return error_mark_node;
3867 }
3868 default:
3869 cp_parser_error (parser, "expected primary-expression");
3870 return error_mark_node;
3871 }
3872
3873 /* An id-expression can start with either an identifier, a
3874 `::' as the beginning of a qualified-id, or the "operator"
3875 keyword. */
3876 case CPP_NAME:
3877 case CPP_SCOPE:
3878 case CPP_TEMPLATE_ID:
3879 case CPP_NESTED_NAME_SPECIFIER:
3880 {
3881 tree id_expression;
3882 tree decl;
3883 const char *error_msg;
3884 bool template_p;
3885 bool done;
3886 cp_token *id_expr_token;
3887
3888 id_expression:
3889 /* Parse the id-expression. */
3890 id_expression
3891 = cp_parser_id_expression (parser,
3892 /*template_keyword_p=*/false,
3893 /*check_dependency_p=*/true,
3894 &template_p,
3895 /*declarator_p=*/false,
3896 /*optional_p=*/false);
3897 if (id_expression == error_mark_node)
3898 return error_mark_node;
3899 id_expr_token = token;
3900 token = cp_lexer_peek_token (parser->lexer);
3901 done = (token->type != CPP_OPEN_SQUARE
3902 && token->type != CPP_OPEN_PAREN
3903 && token->type != CPP_DOT
3904 && token->type != CPP_DEREF
3905 && token->type != CPP_PLUS_PLUS
3906 && token->type != CPP_MINUS_MINUS);
3907 /* If we have a template-id, then no further lookup is
3908 required. If the template-id was for a template-class, we
3909 will sometimes have a TYPE_DECL at this point. */
3910 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3911 || TREE_CODE (id_expression) == TYPE_DECL)
3912 decl = id_expression;
3913 /* Look up the name. */
3914 else
3915 {
3916 tree ambiguous_decls;
3917
3918 /* If we already know that this lookup is ambiguous, then
3919 we've already issued an error message; there's no reason
3920 to check again. */
3921 if (id_expr_token->type == CPP_NAME
3922 && id_expr_token->ambiguous_p)
3923 {
3924 cp_parser_simulate_error (parser);
3925 return error_mark_node;
3926 }
3927
3928 decl = cp_parser_lookup_name (parser, id_expression,
3929 none_type,
3930 template_p,
3931 /*is_namespace=*/false,
3932 /*check_dependency=*/true,
3933 &ambiguous_decls,
3934 id_expr_token->location);
3935 /* If the lookup was ambiguous, an error will already have
3936 been issued. */
3937 if (ambiguous_decls)
3938 return error_mark_node;
3939
3940 /* In Objective-C++, we may have an Objective-C 2.0
3941 dot-syntax for classes here. */
3942 if (c_dialect_objc ()
3943 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3944 && TREE_CODE (decl) == TYPE_DECL
3945 && objc_is_class_name (decl))
3946 {
3947 tree component;
3948 cp_lexer_consume_token (parser->lexer);
3949 component = cp_parser_identifier (parser);
3950 if (component == error_mark_node)
3951 return error_mark_node;
3952
3953 return objc_build_class_component_ref (id_expression, component);
3954 }
3955
3956 /* In Objective-C++, an instance variable (ivar) may be preferred
3957 to whatever cp_parser_lookup_name() found. */
3958 decl = objc_lookup_ivar (decl, id_expression);
3959
3960 /* If name lookup gives us a SCOPE_REF, then the
3961 qualifying scope was dependent. */
3962 if (TREE_CODE (decl) == SCOPE_REF)
3963 {
3964 /* At this point, we do not know if DECL is a valid
3965 integral constant expression. We assume that it is
3966 in fact such an expression, so that code like:
3967
3968 template <int N> struct A {
3969 int a[B<N>::i];
3970 };
3971
3972 is accepted. At template-instantiation time, we
3973 will check that B<N>::i is actually a constant. */
3974 return decl;
3975 }
3976 /* Check to see if DECL is a local variable in a context
3977 where that is forbidden. */
3978 if (parser->local_variables_forbidden_p
3979 && local_variable_p (decl))
3980 {
3981 /* It might be that we only found DECL because we are
3982 trying to be generous with pre-ISO scoping rules.
3983 For example, consider:
3984
3985 int i;
3986 void g() {
3987 for (int i = 0; i < 10; ++i) {}
3988 extern void f(int j = i);
3989 }
3990
3991 Here, name look up will originally find the out
3992 of scope `i'. We need to issue a warning message,
3993 but then use the global `i'. */
3994 decl = check_for_out_of_scope_variable (decl);
3995 if (local_variable_p (decl))
3996 {
3997 error_at (id_expr_token->location,
3998 "local variable %qD may not appear in this context",
3999 decl);
4000 return error_mark_node;
4001 }
4002 }
4003 }
4004
4005 decl = (finish_id_expression
4006 (id_expression, decl, parser->scope,
4007 idk,
4008 parser->integral_constant_expression_p,
4009 parser->allow_non_integral_constant_expression_p,
4010 &parser->non_integral_constant_expression_p,
4011 template_p, done, address_p,
4012 template_arg_p,
4013 &error_msg,
4014 id_expr_token->location));
4015 if (error_msg)
4016 cp_parser_error (parser, error_msg);
4017 return decl;
4018 }
4019
4020 /* Anything else is an error. */
4021 default:
4022 cp_parser_error (parser, "expected primary-expression");
4023 return error_mark_node;
4024 }
4025 }
4026
4027 /* Parse an id-expression.
4028
4029 id-expression:
4030 unqualified-id
4031 qualified-id
4032
4033 qualified-id:
4034 :: [opt] nested-name-specifier template [opt] unqualified-id
4035 :: identifier
4036 :: operator-function-id
4037 :: template-id
4038
4039 Return a representation of the unqualified portion of the
4040 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4041 a `::' or nested-name-specifier.
4042
4043 Often, if the id-expression was a qualified-id, the caller will
4044 want to make a SCOPE_REF to represent the qualified-id. This
4045 function does not do this in order to avoid wastefully creating
4046 SCOPE_REFs when they are not required.
4047
4048 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4049 `template' keyword.
4050
4051 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4052 uninstantiated templates.
4053
4054 If *TEMPLATE_P is non-NULL, it is set to true iff the
4055 `template' keyword is used to explicitly indicate that the entity
4056 named is a template.
4057
4058 If DECLARATOR_P is true, the id-expression is appearing as part of
4059 a declarator, rather than as part of an expression. */
4060
4061 static tree
4062 cp_parser_id_expression (cp_parser *parser,
4063 bool template_keyword_p,
4064 bool check_dependency_p,
4065 bool *template_p,
4066 bool declarator_p,
4067 bool optional_p)
4068 {
4069 bool global_scope_p;
4070 bool nested_name_specifier_p;
4071
4072 /* Assume the `template' keyword was not used. */
4073 if (template_p)
4074 *template_p = template_keyword_p;
4075
4076 /* Look for the optional `::' operator. */
4077 global_scope_p
4078 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4079 != NULL_TREE);
4080 /* Look for the optional nested-name-specifier. */
4081 nested_name_specifier_p
4082 = (cp_parser_nested_name_specifier_opt (parser,
4083 /*typename_keyword_p=*/false,
4084 check_dependency_p,
4085 /*type_p=*/false,
4086 declarator_p)
4087 != NULL_TREE);
4088 /* If there is a nested-name-specifier, then we are looking at
4089 the first qualified-id production. */
4090 if (nested_name_specifier_p)
4091 {
4092 tree saved_scope;
4093 tree saved_object_scope;
4094 tree saved_qualifying_scope;
4095 tree unqualified_id;
4096 bool is_template;
4097
4098 /* See if the next token is the `template' keyword. */
4099 if (!template_p)
4100 template_p = &is_template;
4101 *template_p = cp_parser_optional_template_keyword (parser);
4102 /* Name lookup we do during the processing of the
4103 unqualified-id might obliterate SCOPE. */
4104 saved_scope = parser->scope;
4105 saved_object_scope = parser->object_scope;
4106 saved_qualifying_scope = parser->qualifying_scope;
4107 /* Process the final unqualified-id. */
4108 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4109 check_dependency_p,
4110 declarator_p,
4111 /*optional_p=*/false);
4112 /* Restore the SAVED_SCOPE for our caller. */
4113 parser->scope = saved_scope;
4114 parser->object_scope = saved_object_scope;
4115 parser->qualifying_scope = saved_qualifying_scope;
4116
4117 return unqualified_id;
4118 }
4119 /* Otherwise, if we are in global scope, then we are looking at one
4120 of the other qualified-id productions. */
4121 else if (global_scope_p)
4122 {
4123 cp_token *token;
4124 tree id;
4125
4126 /* Peek at the next token. */
4127 token = cp_lexer_peek_token (parser->lexer);
4128
4129 /* If it's an identifier, and the next token is not a "<", then
4130 we can avoid the template-id case. This is an optimization
4131 for this common case. */
4132 if (token->type == CPP_NAME
4133 && !cp_parser_nth_token_starts_template_argument_list_p
4134 (parser, 2))
4135 return cp_parser_identifier (parser);
4136
4137 cp_parser_parse_tentatively (parser);
4138 /* Try a template-id. */
4139 id = cp_parser_template_id (parser,
4140 /*template_keyword_p=*/false,
4141 /*check_dependency_p=*/true,
4142 declarator_p);
4143 /* If that worked, we're done. */
4144 if (cp_parser_parse_definitely (parser))
4145 return id;
4146
4147 /* Peek at the next token. (Changes in the token buffer may
4148 have invalidated the pointer obtained above.) */
4149 token = cp_lexer_peek_token (parser->lexer);
4150
4151 switch (token->type)
4152 {
4153 case CPP_NAME:
4154 return cp_parser_identifier (parser);
4155
4156 case CPP_KEYWORD:
4157 if (token->keyword == RID_OPERATOR)
4158 return cp_parser_operator_function_id (parser);
4159 /* Fall through. */
4160
4161 default:
4162 cp_parser_error (parser, "expected id-expression");
4163 return error_mark_node;
4164 }
4165 }
4166 else
4167 return cp_parser_unqualified_id (parser, template_keyword_p,
4168 /*check_dependency_p=*/true,
4169 declarator_p,
4170 optional_p);
4171 }
4172
4173 /* Parse an unqualified-id.
4174
4175 unqualified-id:
4176 identifier
4177 operator-function-id
4178 conversion-function-id
4179 ~ class-name
4180 template-id
4181
4182 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4183 keyword, in a construct like `A::template ...'.
4184
4185 Returns a representation of unqualified-id. For the `identifier'
4186 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4187 production a BIT_NOT_EXPR is returned; the operand of the
4188 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4189 other productions, see the documentation accompanying the
4190 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4191 names are looked up in uninstantiated templates. If DECLARATOR_P
4192 is true, the unqualified-id is appearing as part of a declarator,
4193 rather than as part of an expression. */
4194
4195 static tree
4196 cp_parser_unqualified_id (cp_parser* parser,
4197 bool template_keyword_p,
4198 bool check_dependency_p,
4199 bool declarator_p,
4200 bool optional_p)
4201 {
4202 cp_token *token;
4203
4204 /* Peek at the next token. */
4205 token = cp_lexer_peek_token (parser->lexer);
4206
4207 switch (token->type)
4208 {
4209 case CPP_NAME:
4210 {
4211 tree id;
4212
4213 /* We don't know yet whether or not this will be a
4214 template-id. */
4215 cp_parser_parse_tentatively (parser);
4216 /* Try a template-id. */
4217 id = cp_parser_template_id (parser, template_keyword_p,
4218 check_dependency_p,
4219 declarator_p);
4220 /* If it worked, we're done. */
4221 if (cp_parser_parse_definitely (parser))
4222 return id;
4223 /* Otherwise, it's an ordinary identifier. */
4224 return cp_parser_identifier (parser);
4225 }
4226
4227 case CPP_TEMPLATE_ID:
4228 return cp_parser_template_id (parser, template_keyword_p,
4229 check_dependency_p,
4230 declarator_p);
4231
4232 case CPP_COMPL:
4233 {
4234 tree type_decl;
4235 tree qualifying_scope;
4236 tree object_scope;
4237 tree scope;
4238 bool done;
4239
4240 /* Consume the `~' token. */
4241 cp_lexer_consume_token (parser->lexer);
4242 /* Parse the class-name. The standard, as written, seems to
4243 say that:
4244
4245 template <typename T> struct S { ~S (); };
4246 template <typename T> S<T>::~S() {}
4247
4248 is invalid, since `~' must be followed by a class-name, but
4249 `S<T>' is dependent, and so not known to be a class.
4250 That's not right; we need to look in uninstantiated
4251 templates. A further complication arises from:
4252
4253 template <typename T> void f(T t) {
4254 t.T::~T();
4255 }
4256
4257 Here, it is not possible to look up `T' in the scope of `T'
4258 itself. We must look in both the current scope, and the
4259 scope of the containing complete expression.
4260
4261 Yet another issue is:
4262
4263 struct S {
4264 int S;
4265 ~S();
4266 };
4267
4268 S::~S() {}
4269
4270 The standard does not seem to say that the `S' in `~S'
4271 should refer to the type `S' and not the data member
4272 `S::S'. */
4273
4274 /* DR 244 says that we look up the name after the "~" in the
4275 same scope as we looked up the qualifying name. That idea
4276 isn't fully worked out; it's more complicated than that. */
4277 scope = parser->scope;
4278 object_scope = parser->object_scope;
4279 qualifying_scope = parser->qualifying_scope;
4280
4281 /* Check for invalid scopes. */
4282 if (scope == error_mark_node)
4283 {
4284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4285 cp_lexer_consume_token (parser->lexer);
4286 return error_mark_node;
4287 }
4288 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4289 {
4290 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4291 error_at (token->location,
4292 "scope %qT before %<~%> is not a class-name",
4293 scope);
4294 cp_parser_simulate_error (parser);
4295 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4296 cp_lexer_consume_token (parser->lexer);
4297 return error_mark_node;
4298 }
4299 gcc_assert (!scope || TYPE_P (scope));
4300
4301 /* If the name is of the form "X::~X" it's OK even if X is a
4302 typedef. */
4303 token = cp_lexer_peek_token (parser->lexer);
4304 if (scope
4305 && token->type == CPP_NAME
4306 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4307 != CPP_LESS)
4308 && (token->u.value == TYPE_IDENTIFIER (scope)
4309 || constructor_name_p (token->u.value, scope)))
4310 {
4311 cp_lexer_consume_token (parser->lexer);
4312 return build_nt (BIT_NOT_EXPR, scope);
4313 }
4314
4315 /* If there was an explicit qualification (S::~T), first look
4316 in the scope given by the qualification (i.e., S).
4317
4318 Note: in the calls to cp_parser_class_name below we pass
4319 typename_type so that lookup finds the injected-class-name
4320 rather than the constructor. */
4321 done = false;
4322 type_decl = NULL_TREE;
4323 if (scope)
4324 {
4325 cp_parser_parse_tentatively (parser);
4326 type_decl = cp_parser_class_name (parser,
4327 /*typename_keyword_p=*/false,
4328 /*template_keyword_p=*/false,
4329 typename_type,
4330 /*check_dependency=*/false,
4331 /*class_head_p=*/false,
4332 declarator_p);
4333 if (cp_parser_parse_definitely (parser))
4334 done = true;
4335 }
4336 /* In "N::S::~S", look in "N" as well. */
4337 if (!done && scope && qualifying_scope)
4338 {
4339 cp_parser_parse_tentatively (parser);
4340 parser->scope = qualifying_scope;
4341 parser->object_scope = NULL_TREE;
4342 parser->qualifying_scope = NULL_TREE;
4343 type_decl
4344 = cp_parser_class_name (parser,
4345 /*typename_keyword_p=*/false,
4346 /*template_keyword_p=*/false,
4347 typename_type,
4348 /*check_dependency=*/false,
4349 /*class_head_p=*/false,
4350 declarator_p);
4351 if (cp_parser_parse_definitely (parser))
4352 done = true;
4353 }
4354 /* In "p->S::~T", look in the scope given by "*p" as well. */
4355 else if (!done && object_scope)
4356 {
4357 cp_parser_parse_tentatively (parser);
4358 parser->scope = object_scope;
4359 parser->object_scope = NULL_TREE;
4360 parser->qualifying_scope = NULL_TREE;
4361 type_decl
4362 = cp_parser_class_name (parser,
4363 /*typename_keyword_p=*/false,
4364 /*template_keyword_p=*/false,
4365 typename_type,
4366 /*check_dependency=*/false,
4367 /*class_head_p=*/false,
4368 declarator_p);
4369 if (cp_parser_parse_definitely (parser))
4370 done = true;
4371 }
4372 /* Look in the surrounding context. */
4373 if (!done)
4374 {
4375 parser->scope = NULL_TREE;
4376 parser->object_scope = NULL_TREE;
4377 parser->qualifying_scope = NULL_TREE;
4378 if (processing_template_decl)
4379 cp_parser_parse_tentatively (parser);
4380 type_decl
4381 = cp_parser_class_name (parser,
4382 /*typename_keyword_p=*/false,
4383 /*template_keyword_p=*/false,
4384 typename_type,
4385 /*check_dependency=*/false,
4386 /*class_head_p=*/false,
4387 declarator_p);
4388 if (processing_template_decl
4389 && ! cp_parser_parse_definitely (parser))
4390 {
4391 /* We couldn't find a type with this name, so just accept
4392 it and check for a match at instantiation time. */
4393 type_decl = cp_parser_identifier (parser);
4394 if (type_decl != error_mark_node)
4395 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4396 return type_decl;
4397 }
4398 }
4399 /* If an error occurred, assume that the name of the
4400 destructor is the same as the name of the qualifying
4401 class. That allows us to keep parsing after running
4402 into ill-formed destructor names. */
4403 if (type_decl == error_mark_node && scope)
4404 return build_nt (BIT_NOT_EXPR, scope);
4405 else if (type_decl == error_mark_node)
4406 return error_mark_node;
4407
4408 /* Check that destructor name and scope match. */
4409 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4410 {
4411 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4412 error_at (token->location,
4413 "declaration of %<~%T%> as member of %qT",
4414 type_decl, scope);
4415 cp_parser_simulate_error (parser);
4416 return error_mark_node;
4417 }
4418
4419 /* [class.dtor]
4420
4421 A typedef-name that names a class shall not be used as the
4422 identifier in the declarator for a destructor declaration. */
4423 if (declarator_p
4424 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4425 && !DECL_SELF_REFERENCE_P (type_decl)
4426 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4427 error_at (token->location,
4428 "typedef-name %qD used as destructor declarator",
4429 type_decl);
4430
4431 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4432 }
4433
4434 case CPP_KEYWORD:
4435 if (token->keyword == RID_OPERATOR)
4436 {
4437 tree id;
4438
4439 /* This could be a template-id, so we try that first. */
4440 cp_parser_parse_tentatively (parser);
4441 /* Try a template-id. */
4442 id = cp_parser_template_id (parser, template_keyword_p,
4443 /*check_dependency_p=*/true,
4444 declarator_p);
4445 /* If that worked, we're done. */
4446 if (cp_parser_parse_definitely (parser))
4447 return id;
4448 /* We still don't know whether we're looking at an
4449 operator-function-id or a conversion-function-id. */
4450 cp_parser_parse_tentatively (parser);
4451 /* Try an operator-function-id. */
4452 id = cp_parser_operator_function_id (parser);
4453 /* If that didn't work, try a conversion-function-id. */
4454 if (!cp_parser_parse_definitely (parser))
4455 id = cp_parser_conversion_function_id (parser);
4456
4457 return id;
4458 }
4459 /* Fall through. */
4460
4461 default:
4462 if (optional_p)
4463 return NULL_TREE;
4464 cp_parser_error (parser, "expected unqualified-id");
4465 return error_mark_node;
4466 }
4467 }
4468
4469 /* Parse an (optional) nested-name-specifier.
4470
4471 nested-name-specifier: [C++98]
4472 class-or-namespace-name :: nested-name-specifier [opt]
4473 class-or-namespace-name :: template nested-name-specifier [opt]
4474
4475 nested-name-specifier: [C++0x]
4476 type-name ::
4477 namespace-name ::
4478 nested-name-specifier identifier ::
4479 nested-name-specifier template [opt] simple-template-id ::
4480
4481 PARSER->SCOPE should be set appropriately before this function is
4482 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4483 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4484 in name lookups.
4485
4486 Sets PARSER->SCOPE to the class (TYPE) or namespace
4487 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4488 it unchanged if there is no nested-name-specifier. Returns the new
4489 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4490
4491 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4492 part of a declaration and/or decl-specifier. */
4493
4494 static tree
4495 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4496 bool typename_keyword_p,
4497 bool check_dependency_p,
4498 bool type_p,
4499 bool is_declaration)
4500 {
4501 bool success = false;
4502 cp_token_position start = 0;
4503 cp_token *token;
4504
4505 /* Remember where the nested-name-specifier starts. */
4506 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4507 {
4508 start = cp_lexer_token_position (parser->lexer, false);
4509 push_deferring_access_checks (dk_deferred);
4510 }
4511
4512 while (true)
4513 {
4514 tree new_scope;
4515 tree old_scope;
4516 tree saved_qualifying_scope;
4517 bool template_keyword_p;
4518
4519 /* Spot cases that cannot be the beginning of a
4520 nested-name-specifier. */
4521 token = cp_lexer_peek_token (parser->lexer);
4522
4523 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4524 the already parsed nested-name-specifier. */
4525 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4526 {
4527 /* Grab the nested-name-specifier and continue the loop. */
4528 cp_parser_pre_parsed_nested_name_specifier (parser);
4529 /* If we originally encountered this nested-name-specifier
4530 with IS_DECLARATION set to false, we will not have
4531 resolved TYPENAME_TYPEs, so we must do so here. */
4532 if (is_declaration
4533 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4534 {
4535 new_scope = resolve_typename_type (parser->scope,
4536 /*only_current_p=*/false);
4537 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4538 parser->scope = new_scope;
4539 }
4540 success = true;
4541 continue;
4542 }
4543
4544 /* Spot cases that cannot be the beginning of a
4545 nested-name-specifier. On the second and subsequent times
4546 through the loop, we look for the `template' keyword. */
4547 if (success && token->keyword == RID_TEMPLATE)
4548 ;
4549 /* A template-id can start a nested-name-specifier. */
4550 else if (token->type == CPP_TEMPLATE_ID)
4551 ;
4552 else
4553 {
4554 /* If the next token is not an identifier, then it is
4555 definitely not a type-name or namespace-name. */
4556 if (token->type != CPP_NAME)
4557 break;
4558 /* If the following token is neither a `<' (to begin a
4559 template-id), nor a `::', then we are not looking at a
4560 nested-name-specifier. */
4561 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4562
4563 if (token->type == CPP_COLON
4564 && parser->colon_corrects_to_scope_p
4565 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4566 {
4567 error_at (token->location,
4568 "found %<:%> in nested-name-specifier, expected %<::%>");
4569 token->type = CPP_SCOPE;
4570 }
4571
4572 if (token->type != CPP_SCOPE
4573 && !cp_parser_nth_token_starts_template_argument_list_p
4574 (parser, 2))
4575 break;
4576 }
4577
4578 /* The nested-name-specifier is optional, so we parse
4579 tentatively. */
4580 cp_parser_parse_tentatively (parser);
4581
4582 /* Look for the optional `template' keyword, if this isn't the
4583 first time through the loop. */
4584 if (success)
4585 template_keyword_p = cp_parser_optional_template_keyword (parser);
4586 else
4587 template_keyword_p = false;
4588
4589 /* Save the old scope since the name lookup we are about to do
4590 might destroy it. */
4591 old_scope = parser->scope;
4592 saved_qualifying_scope = parser->qualifying_scope;
4593 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4594 look up names in "X<T>::I" in order to determine that "Y" is
4595 a template. So, if we have a typename at this point, we make
4596 an effort to look through it. */
4597 if (is_declaration
4598 && !typename_keyword_p
4599 && parser->scope
4600 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4601 parser->scope = resolve_typename_type (parser->scope,
4602 /*only_current_p=*/false);
4603 /* Parse the qualifying entity. */
4604 new_scope
4605 = cp_parser_qualifying_entity (parser,
4606 typename_keyword_p,
4607 template_keyword_p,
4608 check_dependency_p,
4609 type_p,
4610 is_declaration);
4611 /* Look for the `::' token. */
4612 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4613
4614 /* If we found what we wanted, we keep going; otherwise, we're
4615 done. */
4616 if (!cp_parser_parse_definitely (parser))
4617 {
4618 bool error_p = false;
4619
4620 /* Restore the OLD_SCOPE since it was valid before the
4621 failed attempt at finding the last
4622 class-or-namespace-name. */
4623 parser->scope = old_scope;
4624 parser->qualifying_scope = saved_qualifying_scope;
4625 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4626 break;
4627 /* If the next token is an identifier, and the one after
4628 that is a `::', then any valid interpretation would have
4629 found a class-or-namespace-name. */
4630 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4631 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4632 == CPP_SCOPE)
4633 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4634 != CPP_COMPL))
4635 {
4636 token = cp_lexer_consume_token (parser->lexer);
4637 if (!error_p)
4638 {
4639 if (!token->ambiguous_p)
4640 {
4641 tree decl;
4642 tree ambiguous_decls;
4643
4644 decl = cp_parser_lookup_name (parser, token->u.value,
4645 none_type,
4646 /*is_template=*/false,
4647 /*is_namespace=*/false,
4648 /*check_dependency=*/true,
4649 &ambiguous_decls,
4650 token->location);
4651 if (TREE_CODE (decl) == TEMPLATE_DECL)
4652 error_at (token->location,
4653 "%qD used without template parameters",
4654 decl);
4655 else if (ambiguous_decls)
4656 {
4657 error_at (token->location,
4658 "reference to %qD is ambiguous",
4659 token->u.value);
4660 print_candidates (ambiguous_decls);
4661 decl = error_mark_node;
4662 }
4663 else
4664 {
4665 if (cxx_dialect != cxx98)
4666 cp_parser_name_lookup_error
4667 (parser, token->u.value, decl, NLE_NOT_CXX98,
4668 token->location);
4669 else
4670 cp_parser_name_lookup_error
4671 (parser, token->u.value, decl, NLE_CXX98,
4672 token->location);
4673 }
4674 }
4675 parser->scope = error_mark_node;
4676 error_p = true;
4677 /* Treat this as a successful nested-name-specifier
4678 due to:
4679
4680 [basic.lookup.qual]
4681
4682 If the name found is not a class-name (clause
4683 _class_) or namespace-name (_namespace.def_), the
4684 program is ill-formed. */
4685 success = true;
4686 }
4687 cp_lexer_consume_token (parser->lexer);
4688 }
4689 break;
4690 }
4691 /* We've found one valid nested-name-specifier. */
4692 success = true;
4693 /* Name lookup always gives us a DECL. */
4694 if (TREE_CODE (new_scope) == TYPE_DECL)
4695 new_scope = TREE_TYPE (new_scope);
4696 /* Uses of "template" must be followed by actual templates. */
4697 if (template_keyword_p
4698 && !(CLASS_TYPE_P (new_scope)
4699 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4700 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4701 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4702 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4703 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4704 == TEMPLATE_ID_EXPR)))
4705 permerror (input_location, TYPE_P (new_scope)
4706 ? "%qT is not a template"
4707 : "%qD is not a template",
4708 new_scope);
4709 /* If it is a class scope, try to complete it; we are about to
4710 be looking up names inside the class. */
4711 if (TYPE_P (new_scope)
4712 /* Since checking types for dependency can be expensive,
4713 avoid doing it if the type is already complete. */
4714 && !COMPLETE_TYPE_P (new_scope)
4715 /* Do not try to complete dependent types. */
4716 && !dependent_type_p (new_scope))
4717 {
4718 new_scope = complete_type (new_scope);
4719 /* If it is a typedef to current class, use the current
4720 class instead, as the typedef won't have any names inside
4721 it yet. */
4722 if (!COMPLETE_TYPE_P (new_scope)
4723 && currently_open_class (new_scope))
4724 new_scope = TYPE_MAIN_VARIANT (new_scope);
4725 }
4726 /* Make sure we look in the right scope the next time through
4727 the loop. */
4728 parser->scope = new_scope;
4729 }
4730
4731 /* If parsing tentatively, replace the sequence of tokens that makes
4732 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4733 token. That way, should we re-parse the token stream, we will
4734 not have to repeat the effort required to do the parse, nor will
4735 we issue duplicate error messages. */
4736 if (success && start)
4737 {
4738 cp_token *token;
4739
4740 token = cp_lexer_token_at (parser->lexer, start);
4741 /* Reset the contents of the START token. */
4742 token->type = CPP_NESTED_NAME_SPECIFIER;
4743 /* Retrieve any deferred checks. Do not pop this access checks yet
4744 so the memory will not be reclaimed during token replacing below. */
4745 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4746 token->u.tree_check_value->value = parser->scope;
4747 token->u.tree_check_value->checks = get_deferred_access_checks ();
4748 token->u.tree_check_value->qualifying_scope =
4749 parser->qualifying_scope;
4750 token->keyword = RID_MAX;
4751
4752 /* Purge all subsequent tokens. */
4753 cp_lexer_purge_tokens_after (parser->lexer, start);
4754 }
4755
4756 if (start)
4757 pop_to_parent_deferring_access_checks ();
4758
4759 return success ? parser->scope : NULL_TREE;
4760 }
4761
4762 /* Parse a nested-name-specifier. See
4763 cp_parser_nested_name_specifier_opt for details. This function
4764 behaves identically, except that it will an issue an error if no
4765 nested-name-specifier is present. */
4766
4767 static tree
4768 cp_parser_nested_name_specifier (cp_parser *parser,
4769 bool typename_keyword_p,
4770 bool check_dependency_p,
4771 bool type_p,
4772 bool is_declaration)
4773 {
4774 tree scope;
4775
4776 /* Look for the nested-name-specifier. */
4777 scope = cp_parser_nested_name_specifier_opt (parser,
4778 typename_keyword_p,
4779 check_dependency_p,
4780 type_p,
4781 is_declaration);
4782 /* If it was not present, issue an error message. */
4783 if (!scope)
4784 {
4785 cp_parser_error (parser, "expected nested-name-specifier");
4786 parser->scope = NULL_TREE;
4787 }
4788
4789 return scope;
4790 }
4791
4792 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4793 this is either a class-name or a namespace-name (which corresponds
4794 to the class-or-namespace-name production in the grammar). For
4795 C++0x, it can also be a type-name that refers to an enumeration
4796 type.
4797
4798 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4799 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4800 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4801 TYPE_P is TRUE iff the next name should be taken as a class-name,
4802 even the same name is declared to be another entity in the same
4803 scope.
4804
4805 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4806 specified by the class-or-namespace-name. If neither is found the
4807 ERROR_MARK_NODE is returned. */
4808
4809 static tree
4810 cp_parser_qualifying_entity (cp_parser *parser,
4811 bool typename_keyword_p,
4812 bool template_keyword_p,
4813 bool check_dependency_p,
4814 bool type_p,
4815 bool is_declaration)
4816 {
4817 tree saved_scope;
4818 tree saved_qualifying_scope;
4819 tree saved_object_scope;
4820 tree scope;
4821 bool only_class_p;
4822 bool successful_parse_p;
4823
4824 /* Before we try to parse the class-name, we must save away the
4825 current PARSER->SCOPE since cp_parser_class_name will destroy
4826 it. */
4827 saved_scope = parser->scope;
4828 saved_qualifying_scope = parser->qualifying_scope;
4829 saved_object_scope = parser->object_scope;
4830 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4831 there is no need to look for a namespace-name. */
4832 only_class_p = template_keyword_p
4833 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4834 if (!only_class_p)
4835 cp_parser_parse_tentatively (parser);
4836 scope = cp_parser_class_name (parser,
4837 typename_keyword_p,
4838 template_keyword_p,
4839 type_p ? class_type : none_type,
4840 check_dependency_p,
4841 /*class_head_p=*/false,
4842 is_declaration);
4843 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4844 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4845 if (!only_class_p
4846 && cxx_dialect != cxx98
4847 && !successful_parse_p)
4848 {
4849 /* Restore the saved scope. */
4850 parser->scope = saved_scope;
4851 parser->qualifying_scope = saved_qualifying_scope;
4852 parser->object_scope = saved_object_scope;
4853
4854 /* Parse tentatively. */
4855 cp_parser_parse_tentatively (parser);
4856
4857 /* Parse a typedef-name or enum-name. */
4858 scope = cp_parser_nonclass_name (parser);
4859
4860 /* "If the name found does not designate a namespace or a class,
4861 enumeration, or dependent type, the program is ill-formed."
4862
4863 We cover classes and dependent types above and namespaces below,
4864 so this code is only looking for enums. */
4865 if (!scope || TREE_CODE (scope) != TYPE_DECL
4866 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4867 cp_parser_simulate_error (parser);
4868
4869 successful_parse_p = cp_parser_parse_definitely (parser);
4870 }
4871 /* If that didn't work, try for a namespace-name. */
4872 if (!only_class_p && !successful_parse_p)
4873 {
4874 /* Restore the saved scope. */
4875 parser->scope = saved_scope;
4876 parser->qualifying_scope = saved_qualifying_scope;
4877 parser->object_scope = saved_object_scope;
4878 /* If we are not looking at an identifier followed by the scope
4879 resolution operator, then this is not part of a
4880 nested-name-specifier. (Note that this function is only used
4881 to parse the components of a nested-name-specifier.) */
4882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4883 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4884 return error_mark_node;
4885 scope = cp_parser_namespace_name (parser);
4886 }
4887
4888 return scope;
4889 }
4890
4891 /* Parse a postfix-expression.
4892
4893 postfix-expression:
4894 primary-expression
4895 postfix-expression [ expression ]
4896 postfix-expression ( expression-list [opt] )
4897 simple-type-specifier ( expression-list [opt] )
4898 typename :: [opt] nested-name-specifier identifier
4899 ( expression-list [opt] )
4900 typename :: [opt] nested-name-specifier template [opt] template-id
4901 ( expression-list [opt] )
4902 postfix-expression . template [opt] id-expression
4903 postfix-expression -> template [opt] id-expression
4904 postfix-expression . pseudo-destructor-name
4905 postfix-expression -> pseudo-destructor-name
4906 postfix-expression ++
4907 postfix-expression --
4908 dynamic_cast < type-id > ( expression )
4909 static_cast < type-id > ( expression )
4910 reinterpret_cast < type-id > ( expression )
4911 const_cast < type-id > ( expression )
4912 typeid ( expression )
4913 typeid ( type-id )
4914
4915 GNU Extension:
4916
4917 postfix-expression:
4918 ( type-id ) { initializer-list , [opt] }
4919
4920 This extension is a GNU version of the C99 compound-literal
4921 construct. (The C99 grammar uses `type-name' instead of `type-id',
4922 but they are essentially the same concept.)
4923
4924 If ADDRESS_P is true, the postfix expression is the operand of the
4925 `&' operator. CAST_P is true if this expression is the target of a
4926 cast.
4927
4928 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4929 class member access expressions [expr.ref].
4930
4931 Returns a representation of the expression. */
4932
4933 static tree
4934 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4935 bool member_access_only_p,
4936 cp_id_kind * pidk_return)
4937 {
4938 cp_token *token;
4939 enum rid keyword;
4940 cp_id_kind idk = CP_ID_KIND_NONE;
4941 tree postfix_expression = NULL_TREE;
4942 bool is_member_access = false;
4943
4944 /* Peek at the next token. */
4945 token = cp_lexer_peek_token (parser->lexer);
4946 /* Some of the productions are determined by keywords. */
4947 keyword = token->keyword;
4948 switch (keyword)
4949 {
4950 case RID_DYNCAST:
4951 case RID_STATCAST:
4952 case RID_REINTCAST:
4953 case RID_CONSTCAST:
4954 {
4955 tree type;
4956 tree expression;
4957 const char *saved_message;
4958
4959 /* All of these can be handled in the same way from the point
4960 of view of parsing. Begin by consuming the token
4961 identifying the cast. */
4962 cp_lexer_consume_token (parser->lexer);
4963
4964 /* New types cannot be defined in the cast. */
4965 saved_message = parser->type_definition_forbidden_message;
4966 parser->type_definition_forbidden_message
4967 = G_("types may not be defined in casts");
4968
4969 /* Look for the opening `<'. */
4970 cp_parser_require (parser, CPP_LESS, RT_LESS);
4971 /* Parse the type to which we are casting. */
4972 type = cp_parser_type_id (parser);
4973 /* Look for the closing `>'. */
4974 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4975 /* Restore the old message. */
4976 parser->type_definition_forbidden_message = saved_message;
4977
4978 /* And the expression which is being cast. */
4979 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4980 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4981 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4982
4983 /* Only type conversions to integral or enumeration types
4984 can be used in constant-expressions. */
4985 if (!cast_valid_in_integral_constant_expression_p (type)
4986 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4987 return error_mark_node;
4988
4989 switch (keyword)
4990 {
4991 case RID_DYNCAST:
4992 postfix_expression
4993 = build_dynamic_cast (type, expression, tf_warning_or_error);
4994 break;
4995 case RID_STATCAST:
4996 postfix_expression
4997 = build_static_cast (type, expression, tf_warning_or_error);
4998 break;
4999 case RID_REINTCAST:
5000 postfix_expression
5001 = build_reinterpret_cast (type, expression,
5002 tf_warning_or_error);
5003 break;
5004 case RID_CONSTCAST:
5005 postfix_expression
5006 = build_const_cast (type, expression, tf_warning_or_error);
5007 break;
5008 default:
5009 gcc_unreachable ();
5010 }
5011 }
5012 break;
5013
5014 case RID_TYPEID:
5015 {
5016 tree type;
5017 const char *saved_message;
5018 bool saved_in_type_id_in_expr_p;
5019
5020 /* Consume the `typeid' token. */
5021 cp_lexer_consume_token (parser->lexer);
5022 /* Look for the `(' token. */
5023 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5024 /* Types cannot be defined in a `typeid' expression. */
5025 saved_message = parser->type_definition_forbidden_message;
5026 parser->type_definition_forbidden_message
5027 = G_("types may not be defined in a %<typeid%> expression");
5028 /* We can't be sure yet whether we're looking at a type-id or an
5029 expression. */
5030 cp_parser_parse_tentatively (parser);
5031 /* Try a type-id first. */
5032 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5033 parser->in_type_id_in_expr_p = true;
5034 type = cp_parser_type_id (parser);
5035 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5036 /* Look for the `)' token. Otherwise, we can't be sure that
5037 we're not looking at an expression: consider `typeid (int
5038 (3))', for example. */
5039 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5040 /* If all went well, simply lookup the type-id. */
5041 if (cp_parser_parse_definitely (parser))
5042 postfix_expression = get_typeid (type);
5043 /* Otherwise, fall back to the expression variant. */
5044 else
5045 {
5046 tree expression;
5047
5048 /* Look for an expression. */
5049 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5050 /* Compute its typeid. */
5051 postfix_expression = build_typeid (expression);
5052 /* Look for the `)' token. */
5053 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5054 }
5055 /* Restore the saved message. */
5056 parser->type_definition_forbidden_message = saved_message;
5057 /* `typeid' may not appear in an integral constant expression. */
5058 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
5059 return error_mark_node;
5060 }
5061 break;
5062
5063 case RID_TYPENAME:
5064 {
5065 tree type;
5066 /* The syntax permitted here is the same permitted for an
5067 elaborated-type-specifier. */
5068 type = cp_parser_elaborated_type_specifier (parser,
5069 /*is_friend=*/false,
5070 /*is_declaration=*/false);
5071 postfix_expression = cp_parser_functional_cast (parser, type);
5072 }
5073 break;
5074
5075 default:
5076 {
5077 tree type;
5078
5079 /* If the next thing is a simple-type-specifier, we may be
5080 looking at a functional cast. We could also be looking at
5081 an id-expression. So, we try the functional cast, and if
5082 that doesn't work we fall back to the primary-expression. */
5083 cp_parser_parse_tentatively (parser);
5084 /* Look for the simple-type-specifier. */
5085 type = cp_parser_simple_type_specifier (parser,
5086 /*decl_specs=*/NULL,
5087 CP_PARSER_FLAGS_NONE);
5088 /* Parse the cast itself. */
5089 if (!cp_parser_error_occurred (parser))
5090 postfix_expression
5091 = cp_parser_functional_cast (parser, type);
5092 /* If that worked, we're done. */
5093 if (cp_parser_parse_definitely (parser))
5094 break;
5095
5096 /* If the functional-cast didn't work out, try a
5097 compound-literal. */
5098 if (cp_parser_allow_gnu_extensions_p (parser)
5099 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5100 {
5101 VEC(constructor_elt,gc) *initializer_list = NULL;
5102 bool saved_in_type_id_in_expr_p;
5103
5104 cp_parser_parse_tentatively (parser);
5105 /* Consume the `('. */
5106 cp_lexer_consume_token (parser->lexer);
5107 /* Parse the type. */
5108 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5109 parser->in_type_id_in_expr_p = true;
5110 type = cp_parser_type_id (parser);
5111 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5112 /* Look for the `)'. */
5113 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5114 /* Look for the `{'. */
5115 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5116 /* If things aren't going well, there's no need to
5117 keep going. */
5118 if (!cp_parser_error_occurred (parser))
5119 {
5120 bool non_constant_p;
5121 /* Parse the initializer-list. */
5122 initializer_list
5123 = cp_parser_initializer_list (parser, &non_constant_p);
5124 /* Allow a trailing `,'. */
5125 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5126 cp_lexer_consume_token (parser->lexer);
5127 /* Look for the final `}'. */
5128 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5129 }
5130 /* If that worked, we're definitely looking at a
5131 compound-literal expression. */
5132 if (cp_parser_parse_definitely (parser))
5133 {
5134 /* Warn the user that a compound literal is not
5135 allowed in standard C++. */
5136 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5137 /* For simplicity, we disallow compound literals in
5138 constant-expressions. We could
5139 allow compound literals of integer type, whose
5140 initializer was a constant, in constant
5141 expressions. Permitting that usage, as a further
5142 extension, would not change the meaning of any
5143 currently accepted programs. (Of course, as
5144 compound literals are not part of ISO C++, the
5145 standard has nothing to say.) */
5146 if (cp_parser_non_integral_constant_expression (parser,
5147 NIC_NCC))
5148 {
5149 postfix_expression = error_mark_node;
5150 break;
5151 }
5152 /* Form the representation of the compound-literal. */
5153 postfix_expression
5154 = (finish_compound_literal
5155 (type, build_constructor (init_list_type_node,
5156 initializer_list)));
5157 break;
5158 }
5159 }
5160
5161 /* It must be a primary-expression. */
5162 postfix_expression
5163 = cp_parser_primary_expression (parser, address_p, cast_p,
5164 /*template_arg_p=*/false,
5165 &idk);
5166 }
5167 break;
5168 }
5169
5170 /* Keep looping until the postfix-expression is complete. */
5171 while (true)
5172 {
5173 if (idk == CP_ID_KIND_UNQUALIFIED
5174 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5175 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5176 /* It is not a Koenig lookup function call. */
5177 postfix_expression
5178 = unqualified_name_lookup_error (postfix_expression);
5179
5180 /* Peek at the next token. */
5181 token = cp_lexer_peek_token (parser->lexer);
5182
5183 switch (token->type)
5184 {
5185 case CPP_OPEN_SQUARE:
5186 postfix_expression
5187 = cp_parser_postfix_open_square_expression (parser,
5188 postfix_expression,
5189 false);
5190 idk = CP_ID_KIND_NONE;
5191 is_member_access = false;
5192 break;
5193
5194 case CPP_OPEN_PAREN:
5195 /* postfix-expression ( expression-list [opt] ) */
5196 {
5197 bool koenig_p;
5198 bool is_builtin_constant_p;
5199 bool saved_integral_constant_expression_p = false;
5200 bool saved_non_integral_constant_expression_p = false;
5201 VEC(tree,gc) *args;
5202
5203 is_member_access = false;
5204
5205 is_builtin_constant_p
5206 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5207 if (is_builtin_constant_p)
5208 {
5209 /* The whole point of __builtin_constant_p is to allow
5210 non-constant expressions to appear as arguments. */
5211 saved_integral_constant_expression_p
5212 = parser->integral_constant_expression_p;
5213 saved_non_integral_constant_expression_p
5214 = parser->non_integral_constant_expression_p;
5215 parser->integral_constant_expression_p = false;
5216 }
5217 args = (cp_parser_parenthesized_expression_list
5218 (parser, non_attr,
5219 /*cast_p=*/false, /*allow_expansion_p=*/true,
5220 /*non_constant_p=*/NULL));
5221 if (is_builtin_constant_p)
5222 {
5223 parser->integral_constant_expression_p
5224 = saved_integral_constant_expression_p;
5225 parser->non_integral_constant_expression_p
5226 = saved_non_integral_constant_expression_p;
5227 }
5228
5229 if (args == NULL)
5230 {
5231 postfix_expression = error_mark_node;
5232 break;
5233 }
5234
5235 /* Function calls are not permitted in
5236 constant-expressions. */
5237 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5238 && cp_parser_non_integral_constant_expression (parser,
5239 NIC_FUNC_CALL))
5240 {
5241 postfix_expression = error_mark_node;
5242 release_tree_vector (args);
5243 break;
5244 }
5245
5246 koenig_p = false;
5247 if (idk == CP_ID_KIND_UNQUALIFIED
5248 || idk == CP_ID_KIND_TEMPLATE_ID)
5249 {
5250 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5251 {
5252 if (!VEC_empty (tree, args))
5253 {
5254 koenig_p = true;
5255 if (!any_type_dependent_arguments_p (args))
5256 postfix_expression
5257 = perform_koenig_lookup (postfix_expression, args,
5258 /*include_std=*/false);
5259 }
5260 else
5261 postfix_expression
5262 = unqualified_fn_lookup_error (postfix_expression);
5263 }
5264 /* We do not perform argument-dependent lookup if
5265 normal lookup finds a non-function, in accordance
5266 with the expected resolution of DR 218. */
5267 else if (!VEC_empty (tree, args)
5268 && is_overloaded_fn (postfix_expression))
5269 {
5270 tree fn = get_first_fn (postfix_expression);
5271 fn = STRIP_TEMPLATE (fn);
5272
5273 /* Do not do argument dependent lookup if regular
5274 lookup finds a member function or a block-scope
5275 function declaration. [basic.lookup.argdep]/3 */
5276 if (!DECL_FUNCTION_MEMBER_P (fn)
5277 && !DECL_LOCAL_FUNCTION_P (fn))
5278 {
5279 koenig_p = true;
5280 if (!any_type_dependent_arguments_p (args))
5281 postfix_expression
5282 = perform_koenig_lookup (postfix_expression, args,
5283 /*include_std=*/false);
5284 }
5285 }
5286 }
5287
5288 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5289 {
5290 tree instance = TREE_OPERAND (postfix_expression, 0);
5291 tree fn = TREE_OPERAND (postfix_expression, 1);
5292
5293 if (processing_template_decl
5294 && (type_dependent_expression_p (instance)
5295 || (!BASELINK_P (fn)
5296 && TREE_CODE (fn) != FIELD_DECL)
5297 || type_dependent_expression_p (fn)
5298 || any_type_dependent_arguments_p (args)))
5299 {
5300 postfix_expression
5301 = build_nt_call_vec (postfix_expression, args);
5302 release_tree_vector (args);
5303 break;
5304 }
5305
5306 if (BASELINK_P (fn))
5307 {
5308 postfix_expression
5309 = (build_new_method_call
5310 (instance, fn, &args, NULL_TREE,
5311 (idk == CP_ID_KIND_QUALIFIED
5312 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5313 /*fn_p=*/NULL,
5314 tf_warning_or_error));
5315 }
5316 else
5317 postfix_expression
5318 = finish_call_expr (postfix_expression, &args,
5319 /*disallow_virtual=*/false,
5320 /*koenig_p=*/false,
5321 tf_warning_or_error);
5322 }
5323 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5324 || TREE_CODE (postfix_expression) == MEMBER_REF
5325 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5326 postfix_expression = (build_offset_ref_call_from_tree
5327 (postfix_expression, &args));
5328 else if (idk == CP_ID_KIND_QUALIFIED)
5329 /* A call to a static class member, or a namespace-scope
5330 function. */
5331 postfix_expression
5332 = finish_call_expr (postfix_expression, &args,
5333 /*disallow_virtual=*/true,
5334 koenig_p,
5335 tf_warning_or_error);
5336 else
5337 /* All other function calls. */
5338 postfix_expression
5339 = finish_call_expr (postfix_expression, &args,
5340 /*disallow_virtual=*/false,
5341 koenig_p,
5342 tf_warning_or_error);
5343
5344 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5345 idk = CP_ID_KIND_NONE;
5346
5347 release_tree_vector (args);
5348 }
5349 break;
5350
5351 case CPP_DOT:
5352 case CPP_DEREF:
5353 /* postfix-expression . template [opt] id-expression
5354 postfix-expression . pseudo-destructor-name
5355 postfix-expression -> template [opt] id-expression
5356 postfix-expression -> pseudo-destructor-name */
5357
5358 /* Consume the `.' or `->' operator. */
5359 cp_lexer_consume_token (parser->lexer);
5360
5361 postfix_expression
5362 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5363 postfix_expression,
5364 false, &idk,
5365 token->location);
5366
5367 is_member_access = true;
5368 break;
5369
5370 case CPP_PLUS_PLUS:
5371 /* postfix-expression ++ */
5372 /* Consume the `++' token. */
5373 cp_lexer_consume_token (parser->lexer);
5374 /* Generate a representation for the complete expression. */
5375 postfix_expression
5376 = finish_increment_expr (postfix_expression,
5377 POSTINCREMENT_EXPR);
5378 /* Increments may not appear in constant-expressions. */
5379 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5380 postfix_expression = error_mark_node;
5381 idk = CP_ID_KIND_NONE;
5382 is_member_access = false;
5383 break;
5384
5385 case CPP_MINUS_MINUS:
5386 /* postfix-expression -- */
5387 /* Consume the `--' token. */
5388 cp_lexer_consume_token (parser->lexer);
5389 /* Generate a representation for the complete expression. */
5390 postfix_expression
5391 = finish_increment_expr (postfix_expression,
5392 POSTDECREMENT_EXPR);
5393 /* Decrements may not appear in constant-expressions. */
5394 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5395 postfix_expression = error_mark_node;
5396 idk = CP_ID_KIND_NONE;
5397 is_member_access = false;
5398 break;
5399
5400 default:
5401 if (pidk_return != NULL)
5402 * pidk_return = idk;
5403 if (member_access_only_p)
5404 return is_member_access? postfix_expression : error_mark_node;
5405 else
5406 return postfix_expression;
5407 }
5408 }
5409
5410 /* We should never get here. */
5411 gcc_unreachable ();
5412 return error_mark_node;
5413 }
5414
5415 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5416 by cp_parser_builtin_offsetof. We're looking for
5417
5418 postfix-expression [ expression ]
5419
5420 FOR_OFFSETOF is set if we're being called in that context, which
5421 changes how we deal with integer constant expressions. */
5422
5423 static tree
5424 cp_parser_postfix_open_square_expression (cp_parser *parser,
5425 tree postfix_expression,
5426 bool for_offsetof)
5427 {
5428 tree index;
5429
5430 /* Consume the `[' token. */
5431 cp_lexer_consume_token (parser->lexer);
5432
5433 /* Parse the index expression. */
5434 /* ??? For offsetof, there is a question of what to allow here. If
5435 offsetof is not being used in an integral constant expression context,
5436 then we *could* get the right answer by computing the value at runtime.
5437 If we are in an integral constant expression context, then we might
5438 could accept any constant expression; hard to say without analysis.
5439 Rather than open the barn door too wide right away, allow only integer
5440 constant expressions here. */
5441 if (for_offsetof)
5442 index = cp_parser_constant_expression (parser, false, NULL);
5443 else
5444 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5445
5446 /* Look for the closing `]'. */
5447 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5448
5449 /* Build the ARRAY_REF. */
5450 postfix_expression = grok_array_decl (postfix_expression, index);
5451
5452 /* When not doing offsetof, array references are not permitted in
5453 constant-expressions. */
5454 if (!for_offsetof
5455 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5456 postfix_expression = error_mark_node;
5457
5458 return postfix_expression;
5459 }
5460
5461 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5462 by cp_parser_builtin_offsetof. We're looking for
5463
5464 postfix-expression . template [opt] id-expression
5465 postfix-expression . pseudo-destructor-name
5466 postfix-expression -> template [opt] id-expression
5467 postfix-expression -> pseudo-destructor-name
5468
5469 FOR_OFFSETOF is set if we're being called in that context. That sorta
5470 limits what of the above we'll actually accept, but nevermind.
5471 TOKEN_TYPE is the "." or "->" token, which will already have been
5472 removed from the stream. */
5473
5474 static tree
5475 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5476 enum cpp_ttype token_type,
5477 tree postfix_expression,
5478 bool for_offsetof, cp_id_kind *idk,
5479 location_t location)
5480 {
5481 tree name;
5482 bool dependent_p;
5483 bool pseudo_destructor_p;
5484 tree scope = NULL_TREE;
5485
5486 /* If this is a `->' operator, dereference the pointer. */
5487 if (token_type == CPP_DEREF)
5488 postfix_expression = build_x_arrow (postfix_expression);
5489 /* Check to see whether or not the expression is type-dependent. */
5490 dependent_p = type_dependent_expression_p (postfix_expression);
5491 /* The identifier following the `->' or `.' is not qualified. */
5492 parser->scope = NULL_TREE;
5493 parser->qualifying_scope = NULL_TREE;
5494 parser->object_scope = NULL_TREE;
5495 *idk = CP_ID_KIND_NONE;
5496
5497 /* Enter the scope corresponding to the type of the object
5498 given by the POSTFIX_EXPRESSION. */
5499 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5500 {
5501 scope = TREE_TYPE (postfix_expression);
5502 /* According to the standard, no expression should ever have
5503 reference type. Unfortunately, we do not currently match
5504 the standard in this respect in that our internal representation
5505 of an expression may have reference type even when the standard
5506 says it does not. Therefore, we have to manually obtain the
5507 underlying type here. */
5508 scope = non_reference (scope);
5509 /* The type of the POSTFIX_EXPRESSION must be complete. */
5510 if (scope == unknown_type_node)
5511 {
5512 error_at (location, "%qE does not have class type",
5513 postfix_expression);
5514 scope = NULL_TREE;
5515 }
5516 else
5517 scope = complete_type_or_else (scope, NULL_TREE);
5518 /* Let the name lookup machinery know that we are processing a
5519 class member access expression. */
5520 parser->context->object_type = scope;
5521 /* If something went wrong, we want to be able to discern that case,
5522 as opposed to the case where there was no SCOPE due to the type
5523 of expression being dependent. */
5524 if (!scope)
5525 scope = error_mark_node;
5526 /* If the SCOPE was erroneous, make the various semantic analysis
5527 functions exit quickly -- and without issuing additional error
5528 messages. */
5529 if (scope == error_mark_node)
5530 postfix_expression = error_mark_node;
5531 }
5532
5533 /* Assume this expression is not a pseudo-destructor access. */
5534 pseudo_destructor_p = false;
5535
5536 /* If the SCOPE is a scalar type, then, if this is a valid program,
5537 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5538 is type dependent, it can be pseudo-destructor-name or something else.
5539 Try to parse it as pseudo-destructor-name first. */
5540 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5541 {
5542 tree s;
5543 tree type;
5544
5545 cp_parser_parse_tentatively (parser);
5546 /* Parse the pseudo-destructor-name. */
5547 s = NULL_TREE;
5548 cp_parser_pseudo_destructor_name (parser, &s, &type);
5549 if (dependent_p
5550 && (cp_parser_error_occurred (parser)
5551 || TREE_CODE (type) != TYPE_DECL
5552 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5553 cp_parser_abort_tentative_parse (parser);
5554 else if (cp_parser_parse_definitely (parser))
5555 {
5556 pseudo_destructor_p = true;
5557 postfix_expression
5558 = finish_pseudo_destructor_expr (postfix_expression,
5559 s, TREE_TYPE (type));
5560 }
5561 }
5562
5563 if (!pseudo_destructor_p)
5564 {
5565 /* If the SCOPE is not a scalar type, we are looking at an
5566 ordinary class member access expression, rather than a
5567 pseudo-destructor-name. */
5568 bool template_p;
5569 cp_token *token = cp_lexer_peek_token (parser->lexer);
5570 /* Parse the id-expression. */
5571 name = (cp_parser_id_expression
5572 (parser,
5573 cp_parser_optional_template_keyword (parser),
5574 /*check_dependency_p=*/true,
5575 &template_p,
5576 /*declarator_p=*/false,
5577 /*optional_p=*/false));
5578 /* In general, build a SCOPE_REF if the member name is qualified.
5579 However, if the name was not dependent and has already been
5580 resolved; there is no need to build the SCOPE_REF. For example;
5581
5582 struct X { void f(); };
5583 template <typename T> void f(T* t) { t->X::f(); }
5584
5585 Even though "t" is dependent, "X::f" is not and has been resolved
5586 to a BASELINK; there is no need to include scope information. */
5587
5588 /* But we do need to remember that there was an explicit scope for
5589 virtual function calls. */
5590 if (parser->scope)
5591 *idk = CP_ID_KIND_QUALIFIED;
5592
5593 /* If the name is a template-id that names a type, we will get a
5594 TYPE_DECL here. That is invalid code. */
5595 if (TREE_CODE (name) == TYPE_DECL)
5596 {
5597 error_at (token->location, "invalid use of %qD", name);
5598 postfix_expression = error_mark_node;
5599 }
5600 else
5601 {
5602 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5603 {
5604 name = build_qualified_name (/*type=*/NULL_TREE,
5605 parser->scope,
5606 name,
5607 template_p);
5608 parser->scope = NULL_TREE;
5609 parser->qualifying_scope = NULL_TREE;
5610 parser->object_scope = NULL_TREE;
5611 }
5612 if (scope && name && BASELINK_P (name))
5613 adjust_result_of_qualified_name_lookup
5614 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5615 postfix_expression
5616 = finish_class_member_access_expr (postfix_expression, name,
5617 template_p,
5618 tf_warning_or_error);
5619 }
5620 }
5621
5622 /* We no longer need to look up names in the scope of the object on
5623 the left-hand side of the `.' or `->' operator. */
5624 parser->context->object_type = NULL_TREE;
5625
5626 /* Outside of offsetof, these operators may not appear in
5627 constant-expressions. */
5628 if (!for_offsetof
5629 && (cp_parser_non_integral_constant_expression
5630 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5631 postfix_expression = error_mark_node;
5632
5633 return postfix_expression;
5634 }
5635
5636 /* Parse a parenthesized expression-list.
5637
5638 expression-list:
5639 assignment-expression
5640 expression-list, assignment-expression
5641
5642 attribute-list:
5643 expression-list
5644 identifier
5645 identifier, expression-list
5646
5647 CAST_P is true if this expression is the target of a cast.
5648
5649 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5650 argument pack.
5651
5652 Returns a vector of trees. Each element is a representation of an
5653 assignment-expression. NULL is returned if the ( and or ) are
5654 missing. An empty, but allocated, vector is returned on no
5655 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5656 if we are parsing an attribute list for an attribute that wants a
5657 plain identifier argument, normal_attr for an attribute that wants
5658 an expression, or non_attr if we aren't parsing an attribute list. If
5659 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5660 not all of the expressions in the list were constant. */
5661
5662 static VEC(tree,gc) *
5663 cp_parser_parenthesized_expression_list (cp_parser* parser,
5664 int is_attribute_list,
5665 bool cast_p,
5666 bool allow_expansion_p,
5667 bool *non_constant_p)
5668 {
5669 VEC(tree,gc) *expression_list;
5670 bool fold_expr_p = is_attribute_list != non_attr;
5671 tree identifier = NULL_TREE;
5672 bool saved_greater_than_is_operator_p;
5673
5674 /* Assume all the expressions will be constant. */
5675 if (non_constant_p)
5676 *non_constant_p = false;
5677
5678 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5679 return NULL;
5680
5681 expression_list = make_tree_vector ();
5682
5683 /* Within a parenthesized expression, a `>' token is always
5684 the greater-than operator. */
5685 saved_greater_than_is_operator_p
5686 = parser->greater_than_is_operator_p;
5687 parser->greater_than_is_operator_p = true;
5688
5689 /* Consume expressions until there are no more. */
5690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5691 while (true)
5692 {
5693 tree expr;
5694
5695 /* At the beginning of attribute lists, check to see if the
5696 next token is an identifier. */
5697 if (is_attribute_list == id_attr
5698 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5699 {
5700 cp_token *token;
5701
5702 /* Consume the identifier. */
5703 token = cp_lexer_consume_token (parser->lexer);
5704 /* Save the identifier. */
5705 identifier = token->u.value;
5706 }
5707 else
5708 {
5709 bool expr_non_constant_p;
5710
5711 /* Parse the next assignment-expression. */
5712 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5713 {
5714 /* A braced-init-list. */
5715 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5716 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5717 if (non_constant_p && expr_non_constant_p)
5718 *non_constant_p = true;
5719 }
5720 else if (non_constant_p)
5721 {
5722 expr = (cp_parser_constant_expression
5723 (parser, /*allow_non_constant_p=*/true,
5724 &expr_non_constant_p));
5725 if (expr_non_constant_p)
5726 *non_constant_p = true;
5727 }
5728 else
5729 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5730
5731 if (fold_expr_p)
5732 expr = fold_non_dependent_expr (expr);
5733
5734 /* If we have an ellipsis, then this is an expression
5735 expansion. */
5736 if (allow_expansion_p
5737 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5738 {
5739 /* Consume the `...'. */
5740 cp_lexer_consume_token (parser->lexer);
5741
5742 /* Build the argument pack. */
5743 expr = make_pack_expansion (expr);
5744 }
5745
5746 /* Add it to the list. We add error_mark_node
5747 expressions to the list, so that we can still tell if
5748 the correct form for a parenthesized expression-list
5749 is found. That gives better errors. */
5750 VEC_safe_push (tree, gc, expression_list, expr);
5751
5752 if (expr == error_mark_node)
5753 goto skip_comma;
5754 }
5755
5756 /* After the first item, attribute lists look the same as
5757 expression lists. */
5758 is_attribute_list = non_attr;
5759
5760 get_comma:;
5761 /* If the next token isn't a `,', then we are done. */
5762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5763 break;
5764
5765 /* Otherwise, consume the `,' and keep going. */
5766 cp_lexer_consume_token (parser->lexer);
5767 }
5768
5769 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5770 {
5771 int ending;
5772
5773 skip_comma:;
5774 /* We try and resync to an unnested comma, as that will give the
5775 user better diagnostics. */
5776 ending = cp_parser_skip_to_closing_parenthesis (parser,
5777 /*recovering=*/true,
5778 /*or_comma=*/true,
5779 /*consume_paren=*/true);
5780 if (ending < 0)
5781 goto get_comma;
5782 if (!ending)
5783 {
5784 parser->greater_than_is_operator_p
5785 = saved_greater_than_is_operator_p;
5786 return NULL;
5787 }
5788 }
5789
5790 parser->greater_than_is_operator_p
5791 = saved_greater_than_is_operator_p;
5792
5793 if (identifier)
5794 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5795
5796 return expression_list;
5797 }
5798
5799 /* Parse a pseudo-destructor-name.
5800
5801 pseudo-destructor-name:
5802 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5803 :: [opt] nested-name-specifier template template-id :: ~ type-name
5804 :: [opt] nested-name-specifier [opt] ~ type-name
5805
5806 If either of the first two productions is used, sets *SCOPE to the
5807 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5808 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5809 or ERROR_MARK_NODE if the parse fails. */
5810
5811 static void
5812 cp_parser_pseudo_destructor_name (cp_parser* parser,
5813 tree* scope,
5814 tree* type)
5815 {
5816 bool nested_name_specifier_p;
5817
5818 /* Assume that things will not work out. */
5819 *type = error_mark_node;
5820
5821 /* Look for the optional `::' operator. */
5822 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5823 /* Look for the optional nested-name-specifier. */
5824 nested_name_specifier_p
5825 = (cp_parser_nested_name_specifier_opt (parser,
5826 /*typename_keyword_p=*/false,
5827 /*check_dependency_p=*/true,
5828 /*type_p=*/false,
5829 /*is_declaration=*/false)
5830 != NULL_TREE);
5831 /* Now, if we saw a nested-name-specifier, we might be doing the
5832 second production. */
5833 if (nested_name_specifier_p
5834 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5835 {
5836 /* Consume the `template' keyword. */
5837 cp_lexer_consume_token (parser->lexer);
5838 /* Parse the template-id. */
5839 cp_parser_template_id (parser,
5840 /*template_keyword_p=*/true,
5841 /*check_dependency_p=*/false,
5842 /*is_declaration=*/true);
5843 /* Look for the `::' token. */
5844 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5845 }
5846 /* If the next token is not a `~', then there might be some
5847 additional qualification. */
5848 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5849 {
5850 /* At this point, we're looking for "type-name :: ~". The type-name
5851 must not be a class-name, since this is a pseudo-destructor. So,
5852 it must be either an enum-name, or a typedef-name -- both of which
5853 are just identifiers. So, we peek ahead to check that the "::"
5854 and "~" tokens are present; if they are not, then we can avoid
5855 calling type_name. */
5856 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5857 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5858 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5859 {
5860 cp_parser_error (parser, "non-scalar type");
5861 return;
5862 }
5863
5864 /* Look for the type-name. */
5865 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5866 if (*scope == error_mark_node)
5867 return;
5868
5869 /* Look for the `::' token. */
5870 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5871 }
5872 else
5873 *scope = NULL_TREE;
5874
5875 /* Look for the `~'. */
5876 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5877 /* Look for the type-name again. We are not responsible for
5878 checking that it matches the first type-name. */
5879 *type = cp_parser_nonclass_name (parser);
5880 }
5881
5882 /* Parse a unary-expression.
5883
5884 unary-expression:
5885 postfix-expression
5886 ++ cast-expression
5887 -- cast-expression
5888 unary-operator cast-expression
5889 sizeof unary-expression
5890 sizeof ( type-id )
5891 alignof ( type-id ) [C++0x]
5892 new-expression
5893 delete-expression
5894
5895 GNU Extensions:
5896
5897 unary-expression:
5898 __extension__ cast-expression
5899 __alignof__ unary-expression
5900 __alignof__ ( type-id )
5901 alignof unary-expression [C++0x]
5902 __real__ cast-expression
5903 __imag__ cast-expression
5904 && identifier
5905
5906 ADDRESS_P is true iff the unary-expression is appearing as the
5907 operand of the `&' operator. CAST_P is true if this expression is
5908 the target of a cast.
5909
5910 Returns a representation of the expression. */
5911
5912 static tree
5913 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5914 cp_id_kind * pidk)
5915 {
5916 cp_token *token;
5917 enum tree_code unary_operator;
5918
5919 /* Peek at the next token. */
5920 token = cp_lexer_peek_token (parser->lexer);
5921 /* Some keywords give away the kind of expression. */
5922 if (token->type == CPP_KEYWORD)
5923 {
5924 enum rid keyword = token->keyword;
5925
5926 switch (keyword)
5927 {
5928 case RID_ALIGNOF:
5929 case RID_SIZEOF:
5930 {
5931 tree operand;
5932 enum tree_code op;
5933
5934 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5935 /* Consume the token. */
5936 cp_lexer_consume_token (parser->lexer);
5937 /* Parse the operand. */
5938 operand = cp_parser_sizeof_operand (parser, keyword);
5939
5940 if (TYPE_P (operand))
5941 return cxx_sizeof_or_alignof_type (operand, op, true);
5942 else
5943 {
5944 /* ISO C++ defines alignof only with types, not with
5945 expressions. So pedwarn if alignof is used with a non-
5946 type expression. However, __alignof__ is ok. */
5947 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5948 pedwarn (token->location, OPT_pedantic,
5949 "ISO C++ does not allow %<alignof%> "
5950 "with a non-type");
5951
5952 return cxx_sizeof_or_alignof_expr (operand, op, true);
5953 }
5954 }
5955
5956 case RID_NEW:
5957 return cp_parser_new_expression (parser);
5958
5959 case RID_DELETE:
5960 return cp_parser_delete_expression (parser);
5961
5962 case RID_EXTENSION:
5963 {
5964 /* The saved value of the PEDANTIC flag. */
5965 int saved_pedantic;
5966 tree expr;
5967
5968 /* Save away the PEDANTIC flag. */
5969 cp_parser_extension_opt (parser, &saved_pedantic);
5970 /* Parse the cast-expression. */
5971 expr = cp_parser_simple_cast_expression (parser);
5972 /* Restore the PEDANTIC flag. */
5973 pedantic = saved_pedantic;
5974
5975 return expr;
5976 }
5977
5978 case RID_REALPART:
5979 case RID_IMAGPART:
5980 {
5981 tree expression;
5982
5983 /* Consume the `__real__' or `__imag__' token. */
5984 cp_lexer_consume_token (parser->lexer);
5985 /* Parse the cast-expression. */
5986 expression = cp_parser_simple_cast_expression (parser);
5987 /* Create the complete representation. */
5988 return build_x_unary_op ((keyword == RID_REALPART
5989 ? REALPART_EXPR : IMAGPART_EXPR),
5990 expression,
5991 tf_warning_or_error);
5992 }
5993 break;
5994
5995 case RID_NOEXCEPT:
5996 {
5997 tree expr;
5998 const char *saved_message;
5999 bool saved_integral_constant_expression_p;
6000 bool saved_non_integral_constant_expression_p;
6001 bool saved_greater_than_is_operator_p;
6002
6003 cp_lexer_consume_token (parser->lexer);
6004 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6005
6006 saved_message = parser->type_definition_forbidden_message;
6007 parser->type_definition_forbidden_message
6008 = G_("types may not be defined in %<noexcept%> expressions");
6009
6010 saved_integral_constant_expression_p
6011 = parser->integral_constant_expression_p;
6012 saved_non_integral_constant_expression_p
6013 = parser->non_integral_constant_expression_p;
6014 parser->integral_constant_expression_p = false;
6015
6016 saved_greater_than_is_operator_p
6017 = parser->greater_than_is_operator_p;
6018 parser->greater_than_is_operator_p = true;
6019
6020 ++cp_unevaluated_operand;
6021 ++c_inhibit_evaluation_warnings;
6022 expr = cp_parser_expression (parser, false, NULL);
6023 --c_inhibit_evaluation_warnings;
6024 --cp_unevaluated_operand;
6025
6026 parser->greater_than_is_operator_p
6027 = saved_greater_than_is_operator_p;
6028
6029 parser->integral_constant_expression_p
6030 = saved_integral_constant_expression_p;
6031 parser->non_integral_constant_expression_p
6032 = saved_non_integral_constant_expression_p;
6033
6034 parser->type_definition_forbidden_message = saved_message;
6035
6036 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6037 return finish_noexcept_expr (expr, tf_warning_or_error);
6038 }
6039
6040 default:
6041 break;
6042 }
6043 }
6044
6045 /* Look for the `:: new' and `:: delete', which also signal the
6046 beginning of a new-expression, or delete-expression,
6047 respectively. If the next token is `::', then it might be one of
6048 these. */
6049 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6050 {
6051 enum rid keyword;
6052
6053 /* See if the token after the `::' is one of the keywords in
6054 which we're interested. */
6055 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6056 /* If it's `new', we have a new-expression. */
6057 if (keyword == RID_NEW)
6058 return cp_parser_new_expression (parser);
6059 /* Similarly, for `delete'. */
6060 else if (keyword == RID_DELETE)
6061 return cp_parser_delete_expression (parser);
6062 }
6063
6064 /* Look for a unary operator. */
6065 unary_operator = cp_parser_unary_operator (token);
6066 /* The `++' and `--' operators can be handled similarly, even though
6067 they are not technically unary-operators in the grammar. */
6068 if (unary_operator == ERROR_MARK)
6069 {
6070 if (token->type == CPP_PLUS_PLUS)
6071 unary_operator = PREINCREMENT_EXPR;
6072 else if (token->type == CPP_MINUS_MINUS)
6073 unary_operator = PREDECREMENT_EXPR;
6074 /* Handle the GNU address-of-label extension. */
6075 else if (cp_parser_allow_gnu_extensions_p (parser)
6076 && token->type == CPP_AND_AND)
6077 {
6078 tree identifier;
6079 tree expression;
6080 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6081
6082 /* Consume the '&&' token. */
6083 cp_lexer_consume_token (parser->lexer);
6084 /* Look for the identifier. */
6085 identifier = cp_parser_identifier (parser);
6086 /* Create an expression representing the address. */
6087 expression = finish_label_address_expr (identifier, loc);
6088 if (cp_parser_non_integral_constant_expression (parser,
6089 NIC_ADDR_LABEL))
6090 expression = error_mark_node;
6091 return expression;
6092 }
6093 }
6094 if (unary_operator != ERROR_MARK)
6095 {
6096 tree cast_expression;
6097 tree expression = error_mark_node;
6098 non_integral_constant non_constant_p = NIC_NONE;
6099
6100 /* Consume the operator token. */
6101 token = cp_lexer_consume_token (parser->lexer);
6102 /* Parse the cast-expression. */
6103 cast_expression
6104 = cp_parser_cast_expression (parser,
6105 unary_operator == ADDR_EXPR,
6106 /*cast_p=*/false, pidk);
6107 /* Now, build an appropriate representation. */
6108 switch (unary_operator)
6109 {
6110 case INDIRECT_REF:
6111 non_constant_p = NIC_STAR;
6112 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6113 tf_warning_or_error);
6114 break;
6115
6116 case ADDR_EXPR:
6117 non_constant_p = NIC_ADDR;
6118 /* Fall through. */
6119 case BIT_NOT_EXPR:
6120 expression = build_x_unary_op (unary_operator, cast_expression,
6121 tf_warning_or_error);
6122 break;
6123
6124 case PREINCREMENT_EXPR:
6125 case PREDECREMENT_EXPR:
6126 non_constant_p = unary_operator == PREINCREMENT_EXPR
6127 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6128 /* Fall through. */
6129 case UNARY_PLUS_EXPR:
6130 case NEGATE_EXPR:
6131 case TRUTH_NOT_EXPR:
6132 expression = finish_unary_op_expr (unary_operator, cast_expression);
6133 break;
6134
6135 default:
6136 gcc_unreachable ();
6137 }
6138
6139 if (non_constant_p != NIC_NONE
6140 && cp_parser_non_integral_constant_expression (parser,
6141 non_constant_p))
6142 expression = error_mark_node;
6143
6144 return expression;
6145 }
6146
6147 return cp_parser_postfix_expression (parser, address_p, cast_p,
6148 /*member_access_only_p=*/false,
6149 pidk);
6150 }
6151
6152 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6153 unary-operator, the corresponding tree code is returned. */
6154
6155 static enum tree_code
6156 cp_parser_unary_operator (cp_token* token)
6157 {
6158 switch (token->type)
6159 {
6160 case CPP_MULT:
6161 return INDIRECT_REF;
6162
6163 case CPP_AND:
6164 return ADDR_EXPR;
6165
6166 case CPP_PLUS:
6167 return UNARY_PLUS_EXPR;
6168
6169 case CPP_MINUS:
6170 return NEGATE_EXPR;
6171
6172 case CPP_NOT:
6173 return TRUTH_NOT_EXPR;
6174
6175 case CPP_COMPL:
6176 return BIT_NOT_EXPR;
6177
6178 default:
6179 return ERROR_MARK;
6180 }
6181 }
6182
6183 /* Parse a new-expression.
6184
6185 new-expression:
6186 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6187 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6188
6189 Returns a representation of the expression. */
6190
6191 static tree
6192 cp_parser_new_expression (cp_parser* parser)
6193 {
6194 bool global_scope_p;
6195 VEC(tree,gc) *placement;
6196 tree type;
6197 VEC(tree,gc) *initializer;
6198 tree nelts;
6199 tree ret;
6200
6201 /* Look for the optional `::' operator. */
6202 global_scope_p
6203 = (cp_parser_global_scope_opt (parser,
6204 /*current_scope_valid_p=*/false)
6205 != NULL_TREE);
6206 /* Look for the `new' operator. */
6207 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6208 /* There's no easy way to tell a new-placement from the
6209 `( type-id )' construct. */
6210 cp_parser_parse_tentatively (parser);
6211 /* Look for a new-placement. */
6212 placement = cp_parser_new_placement (parser);
6213 /* If that didn't work out, there's no new-placement. */
6214 if (!cp_parser_parse_definitely (parser))
6215 {
6216 if (placement != NULL)
6217 release_tree_vector (placement);
6218 placement = NULL;
6219 }
6220
6221 /* If the next token is a `(', then we have a parenthesized
6222 type-id. */
6223 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6224 {
6225 cp_token *token;
6226 /* Consume the `('. */
6227 cp_lexer_consume_token (parser->lexer);
6228 /* Parse the type-id. */
6229 type = cp_parser_type_id (parser);
6230 /* Look for the closing `)'. */
6231 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6232 token = cp_lexer_peek_token (parser->lexer);
6233 /* There should not be a direct-new-declarator in this production,
6234 but GCC used to allowed this, so we check and emit a sensible error
6235 message for this case. */
6236 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6237 {
6238 error_at (token->location,
6239 "array bound forbidden after parenthesized type-id");
6240 inform (token->location,
6241 "try removing the parentheses around the type-id");
6242 cp_parser_direct_new_declarator (parser);
6243 }
6244 nelts = NULL_TREE;
6245 }
6246 /* Otherwise, there must be a new-type-id. */
6247 else
6248 type = cp_parser_new_type_id (parser, &nelts);
6249
6250 /* If the next token is a `(' or '{', then we have a new-initializer. */
6251 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6252 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6253 initializer = cp_parser_new_initializer (parser);
6254 else
6255 initializer = NULL;
6256
6257 /* A new-expression may not appear in an integral constant
6258 expression. */
6259 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6260 ret = error_mark_node;
6261 else
6262 {
6263 /* Create a representation of the new-expression. */
6264 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6265 tf_warning_or_error);
6266 }
6267
6268 if (placement != NULL)
6269 release_tree_vector (placement);
6270 if (initializer != NULL)
6271 release_tree_vector (initializer);
6272
6273 return ret;
6274 }
6275
6276 /* Parse a new-placement.
6277
6278 new-placement:
6279 ( expression-list )
6280
6281 Returns the same representation as for an expression-list. */
6282
6283 static VEC(tree,gc) *
6284 cp_parser_new_placement (cp_parser* parser)
6285 {
6286 VEC(tree,gc) *expression_list;
6287
6288 /* Parse the expression-list. */
6289 expression_list = (cp_parser_parenthesized_expression_list
6290 (parser, non_attr, /*cast_p=*/false,
6291 /*allow_expansion_p=*/true,
6292 /*non_constant_p=*/NULL));
6293
6294 return expression_list;
6295 }
6296
6297 /* Parse a new-type-id.
6298
6299 new-type-id:
6300 type-specifier-seq new-declarator [opt]
6301
6302 Returns the TYPE allocated. If the new-type-id indicates an array
6303 type, *NELTS is set to the number of elements in the last array
6304 bound; the TYPE will not include the last array bound. */
6305
6306 static tree
6307 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6308 {
6309 cp_decl_specifier_seq type_specifier_seq;
6310 cp_declarator *new_declarator;
6311 cp_declarator *declarator;
6312 cp_declarator *outer_declarator;
6313 const char *saved_message;
6314 tree type;
6315
6316 /* The type-specifier sequence must not contain type definitions.
6317 (It cannot contain declarations of new types either, but if they
6318 are not definitions we will catch that because they are not
6319 complete.) */
6320 saved_message = parser->type_definition_forbidden_message;
6321 parser->type_definition_forbidden_message
6322 = G_("types may not be defined in a new-type-id");
6323 /* Parse the type-specifier-seq. */
6324 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6325 /*is_trailing_return=*/false,
6326 &type_specifier_seq);
6327 /* Restore the old message. */
6328 parser->type_definition_forbidden_message = saved_message;
6329 /* Parse the new-declarator. */
6330 new_declarator = cp_parser_new_declarator_opt (parser);
6331
6332 /* Determine the number of elements in the last array dimension, if
6333 any. */
6334 *nelts = NULL_TREE;
6335 /* Skip down to the last array dimension. */
6336 declarator = new_declarator;
6337 outer_declarator = NULL;
6338 while (declarator && (declarator->kind == cdk_pointer
6339 || declarator->kind == cdk_ptrmem))
6340 {
6341 outer_declarator = declarator;
6342 declarator = declarator->declarator;
6343 }
6344 while (declarator
6345 && declarator->kind == cdk_array
6346 && declarator->declarator
6347 && declarator->declarator->kind == cdk_array)
6348 {
6349 outer_declarator = declarator;
6350 declarator = declarator->declarator;
6351 }
6352
6353 if (declarator && declarator->kind == cdk_array)
6354 {
6355 *nelts = declarator->u.array.bounds;
6356 if (*nelts == error_mark_node)
6357 *nelts = integer_one_node;
6358
6359 if (outer_declarator)
6360 outer_declarator->declarator = declarator->declarator;
6361 else
6362 new_declarator = NULL;
6363 }
6364
6365 type = groktypename (&type_specifier_seq, new_declarator, false);
6366 return type;
6367 }
6368
6369 /* Parse an (optional) new-declarator.
6370
6371 new-declarator:
6372 ptr-operator new-declarator [opt]
6373 direct-new-declarator
6374
6375 Returns the declarator. */
6376
6377 static cp_declarator *
6378 cp_parser_new_declarator_opt (cp_parser* parser)
6379 {
6380 enum tree_code code;
6381 tree type;
6382 cp_cv_quals cv_quals;
6383
6384 /* We don't know if there's a ptr-operator next, or not. */
6385 cp_parser_parse_tentatively (parser);
6386 /* Look for a ptr-operator. */
6387 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6388 /* If that worked, look for more new-declarators. */
6389 if (cp_parser_parse_definitely (parser))
6390 {
6391 cp_declarator *declarator;
6392
6393 /* Parse another optional declarator. */
6394 declarator = cp_parser_new_declarator_opt (parser);
6395
6396 return cp_parser_make_indirect_declarator
6397 (code, type, cv_quals, declarator);
6398 }
6399
6400 /* If the next token is a `[', there is a direct-new-declarator. */
6401 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6402 return cp_parser_direct_new_declarator (parser);
6403
6404 return NULL;
6405 }
6406
6407 /* Parse a direct-new-declarator.
6408
6409 direct-new-declarator:
6410 [ expression ]
6411 direct-new-declarator [constant-expression]
6412
6413 */
6414
6415 static cp_declarator *
6416 cp_parser_direct_new_declarator (cp_parser* parser)
6417 {
6418 cp_declarator *declarator = NULL;
6419
6420 while (true)
6421 {
6422 tree expression;
6423
6424 /* Look for the opening `['. */
6425 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6426 /* The first expression is not required to be constant. */
6427 if (!declarator)
6428 {
6429 cp_token *token = cp_lexer_peek_token (parser->lexer);
6430 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6431 /* The standard requires that the expression have integral
6432 type. DR 74 adds enumeration types. We believe that the
6433 real intent is that these expressions be handled like the
6434 expression in a `switch' condition, which also allows
6435 classes with a single conversion to integral or
6436 enumeration type. */
6437 if (!processing_template_decl)
6438 {
6439 expression
6440 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6441 expression,
6442 /*complain=*/true);
6443 if (!expression)
6444 {
6445 error_at (token->location,
6446 "expression in new-declarator must have integral "
6447 "or enumeration type");
6448 expression = error_mark_node;
6449 }
6450 }
6451 }
6452 /* But all the other expressions must be. */
6453 else
6454 expression
6455 = cp_parser_constant_expression (parser,
6456 /*allow_non_constant=*/false,
6457 NULL);
6458 /* Look for the closing `]'. */
6459 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6460
6461 /* Add this bound to the declarator. */
6462 declarator = make_array_declarator (declarator, expression);
6463
6464 /* If the next token is not a `[', then there are no more
6465 bounds. */
6466 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6467 break;
6468 }
6469
6470 return declarator;
6471 }
6472
6473 /* Parse a new-initializer.
6474
6475 new-initializer:
6476 ( expression-list [opt] )
6477 braced-init-list
6478
6479 Returns a representation of the expression-list. */
6480
6481 static VEC(tree,gc) *
6482 cp_parser_new_initializer (cp_parser* parser)
6483 {
6484 VEC(tree,gc) *expression_list;
6485
6486 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6487 {
6488 tree t;
6489 bool expr_non_constant_p;
6490 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6491 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6492 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6493 expression_list = make_tree_vector_single (t);
6494 }
6495 else
6496 expression_list = (cp_parser_parenthesized_expression_list
6497 (parser, non_attr, /*cast_p=*/false,
6498 /*allow_expansion_p=*/true,
6499 /*non_constant_p=*/NULL));
6500
6501 return expression_list;
6502 }
6503
6504 /* Parse a delete-expression.
6505
6506 delete-expression:
6507 :: [opt] delete cast-expression
6508 :: [opt] delete [ ] cast-expression
6509
6510 Returns a representation of the expression. */
6511
6512 static tree
6513 cp_parser_delete_expression (cp_parser* parser)
6514 {
6515 bool global_scope_p;
6516 bool array_p;
6517 tree expression;
6518
6519 /* Look for the optional `::' operator. */
6520 global_scope_p
6521 = (cp_parser_global_scope_opt (parser,
6522 /*current_scope_valid_p=*/false)
6523 != NULL_TREE);
6524 /* Look for the `delete' keyword. */
6525 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6526 /* See if the array syntax is in use. */
6527 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6528 {
6529 /* Consume the `[' token. */
6530 cp_lexer_consume_token (parser->lexer);
6531 /* Look for the `]' token. */
6532 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6533 /* Remember that this is the `[]' construct. */
6534 array_p = true;
6535 }
6536 else
6537 array_p = false;
6538
6539 /* Parse the cast-expression. */
6540 expression = cp_parser_simple_cast_expression (parser);
6541
6542 /* A delete-expression may not appear in an integral constant
6543 expression. */
6544 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6545 return error_mark_node;
6546
6547 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6548 }
6549
6550 /* Returns true if TOKEN may start a cast-expression and false
6551 otherwise. */
6552
6553 static bool
6554 cp_parser_token_starts_cast_expression (cp_token *token)
6555 {
6556 switch (token->type)
6557 {
6558 case CPP_COMMA:
6559 case CPP_SEMICOLON:
6560 case CPP_QUERY:
6561 case CPP_COLON:
6562 case CPP_CLOSE_SQUARE:
6563 case CPP_CLOSE_PAREN:
6564 case CPP_CLOSE_BRACE:
6565 case CPP_DOT:
6566 case CPP_DOT_STAR:
6567 case CPP_DEREF:
6568 case CPP_DEREF_STAR:
6569 case CPP_DIV:
6570 case CPP_MOD:
6571 case CPP_LSHIFT:
6572 case CPP_RSHIFT:
6573 case CPP_LESS:
6574 case CPP_GREATER:
6575 case CPP_LESS_EQ:
6576 case CPP_GREATER_EQ:
6577 case CPP_EQ_EQ:
6578 case CPP_NOT_EQ:
6579 case CPP_EQ:
6580 case CPP_MULT_EQ:
6581 case CPP_DIV_EQ:
6582 case CPP_MOD_EQ:
6583 case CPP_PLUS_EQ:
6584 case CPP_MINUS_EQ:
6585 case CPP_RSHIFT_EQ:
6586 case CPP_LSHIFT_EQ:
6587 case CPP_AND_EQ:
6588 case CPP_XOR_EQ:
6589 case CPP_OR_EQ:
6590 case CPP_XOR:
6591 case CPP_OR:
6592 case CPP_OR_OR:
6593 case CPP_EOF:
6594 return false;
6595
6596 /* '[' may start a primary-expression in obj-c++. */
6597 case CPP_OPEN_SQUARE:
6598 return c_dialect_objc ();
6599
6600 default:
6601 return true;
6602 }
6603 }
6604
6605 /* Parse a cast-expression.
6606
6607 cast-expression:
6608 unary-expression
6609 ( type-id ) cast-expression
6610
6611 ADDRESS_P is true iff the unary-expression is appearing as the
6612 operand of the `&' operator. CAST_P is true if this expression is
6613 the target of a cast.
6614
6615 Returns a representation of the expression. */
6616
6617 static tree
6618 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6619 cp_id_kind * pidk)
6620 {
6621 /* If it's a `(', then we might be looking at a cast. */
6622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6623 {
6624 tree type = NULL_TREE;
6625 tree expr = NULL_TREE;
6626 bool compound_literal_p;
6627 const char *saved_message;
6628
6629 /* There's no way to know yet whether or not this is a cast.
6630 For example, `(int (3))' is a unary-expression, while `(int)
6631 3' is a cast. So, we resort to parsing tentatively. */
6632 cp_parser_parse_tentatively (parser);
6633 /* Types may not be defined in a cast. */
6634 saved_message = parser->type_definition_forbidden_message;
6635 parser->type_definition_forbidden_message
6636 = G_("types may not be defined in casts");
6637 /* Consume the `('. */
6638 cp_lexer_consume_token (parser->lexer);
6639 /* A very tricky bit is that `(struct S) { 3 }' is a
6640 compound-literal (which we permit in C++ as an extension).
6641 But, that construct is not a cast-expression -- it is a
6642 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6643 is legal; if the compound-literal were a cast-expression,
6644 you'd need an extra set of parentheses.) But, if we parse
6645 the type-id, and it happens to be a class-specifier, then we
6646 will commit to the parse at that point, because we cannot
6647 undo the action that is done when creating a new class. So,
6648 then we cannot back up and do a postfix-expression.
6649
6650 Therefore, we scan ahead to the closing `)', and check to see
6651 if the token after the `)' is a `{'. If so, we are not
6652 looking at a cast-expression.
6653
6654 Save tokens so that we can put them back. */
6655 cp_lexer_save_tokens (parser->lexer);
6656 /* Skip tokens until the next token is a closing parenthesis.
6657 If we find the closing `)', and the next token is a `{', then
6658 we are looking at a compound-literal. */
6659 compound_literal_p
6660 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6661 /*consume_paren=*/true)
6662 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6663 /* Roll back the tokens we skipped. */
6664 cp_lexer_rollback_tokens (parser->lexer);
6665 /* If we were looking at a compound-literal, simulate an error
6666 so that the call to cp_parser_parse_definitely below will
6667 fail. */
6668 if (compound_literal_p)
6669 cp_parser_simulate_error (parser);
6670 else
6671 {
6672 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6673 parser->in_type_id_in_expr_p = true;
6674 /* Look for the type-id. */
6675 type = cp_parser_type_id (parser);
6676 /* Look for the closing `)'. */
6677 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6678 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6679 }
6680
6681 /* Restore the saved message. */
6682 parser->type_definition_forbidden_message = saved_message;
6683
6684 /* At this point this can only be either a cast or a
6685 parenthesized ctor such as `(T ())' that looks like a cast to
6686 function returning T. */
6687 if (!cp_parser_error_occurred (parser)
6688 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6689 (parser->lexer)))
6690 {
6691 cp_parser_parse_definitely (parser);
6692 expr = cp_parser_cast_expression (parser,
6693 /*address_p=*/false,
6694 /*cast_p=*/true, pidk);
6695
6696 /* Warn about old-style casts, if so requested. */
6697 if (warn_old_style_cast
6698 && !in_system_header
6699 && !VOID_TYPE_P (type)
6700 && current_lang_name != lang_name_c)
6701 warning (OPT_Wold_style_cast, "use of old-style cast");
6702
6703 /* Only type conversions to integral or enumeration types
6704 can be used in constant-expressions. */
6705 if (!cast_valid_in_integral_constant_expression_p (type)
6706 && cp_parser_non_integral_constant_expression (parser,
6707 NIC_CAST))
6708 return error_mark_node;
6709
6710 /* Perform the cast. */
6711 expr = build_c_cast (input_location, type, expr);
6712 return expr;
6713 }
6714 else
6715 cp_parser_abort_tentative_parse (parser);
6716 }
6717
6718 /* If we get here, then it's not a cast, so it must be a
6719 unary-expression. */
6720 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6721 }
6722
6723 /* Parse a binary expression of the general form:
6724
6725 pm-expression:
6726 cast-expression
6727 pm-expression .* cast-expression
6728 pm-expression ->* cast-expression
6729
6730 multiplicative-expression:
6731 pm-expression
6732 multiplicative-expression * pm-expression
6733 multiplicative-expression / pm-expression
6734 multiplicative-expression % pm-expression
6735
6736 additive-expression:
6737 multiplicative-expression
6738 additive-expression + multiplicative-expression
6739 additive-expression - multiplicative-expression
6740
6741 shift-expression:
6742 additive-expression
6743 shift-expression << additive-expression
6744 shift-expression >> additive-expression
6745
6746 relational-expression:
6747 shift-expression
6748 relational-expression < shift-expression
6749 relational-expression > shift-expression
6750 relational-expression <= shift-expression
6751 relational-expression >= shift-expression
6752
6753 GNU Extension:
6754
6755 relational-expression:
6756 relational-expression <? shift-expression
6757 relational-expression >? shift-expression
6758
6759 equality-expression:
6760 relational-expression
6761 equality-expression == relational-expression
6762 equality-expression != relational-expression
6763
6764 and-expression:
6765 equality-expression
6766 and-expression & equality-expression
6767
6768 exclusive-or-expression:
6769 and-expression
6770 exclusive-or-expression ^ and-expression
6771
6772 inclusive-or-expression:
6773 exclusive-or-expression
6774 inclusive-or-expression | exclusive-or-expression
6775
6776 logical-and-expression:
6777 inclusive-or-expression
6778 logical-and-expression && inclusive-or-expression
6779
6780 logical-or-expression:
6781 logical-and-expression
6782 logical-or-expression || logical-and-expression
6783
6784 All these are implemented with a single function like:
6785
6786 binary-expression:
6787 simple-cast-expression
6788 binary-expression <token> binary-expression
6789
6790 CAST_P is true if this expression is the target of a cast.
6791
6792 The binops_by_token map is used to get the tree codes for each <token> type.
6793 binary-expressions are associated according to a precedence table. */
6794
6795 #define TOKEN_PRECEDENCE(token) \
6796 (((token->type == CPP_GREATER \
6797 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6798 && !parser->greater_than_is_operator_p) \
6799 ? PREC_NOT_OPERATOR \
6800 : binops_by_token[token->type].prec)
6801
6802 static tree
6803 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6804 bool no_toplevel_fold_p,
6805 enum cp_parser_prec prec,
6806 cp_id_kind * pidk)
6807 {
6808 cp_parser_expression_stack stack;
6809 cp_parser_expression_stack_entry *sp = &stack[0];
6810 tree lhs, rhs;
6811 cp_token *token;
6812 enum tree_code tree_type, lhs_type, rhs_type;
6813 enum cp_parser_prec new_prec, lookahead_prec;
6814 bool overloaded_p;
6815
6816 /* Parse the first expression. */
6817 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6818 lhs_type = ERROR_MARK;
6819
6820 for (;;)
6821 {
6822 /* Get an operator token. */
6823 token = cp_lexer_peek_token (parser->lexer);
6824
6825 if (warn_cxx0x_compat
6826 && token->type == CPP_RSHIFT
6827 && !parser->greater_than_is_operator_p)
6828 {
6829 if (warning_at (token->location, OPT_Wc__0x_compat,
6830 "%<>>%> operator will be treated as"
6831 " two right angle brackets in C++0x"))
6832 inform (token->location,
6833 "suggest parentheses around %<>>%> expression");
6834 }
6835
6836 new_prec = TOKEN_PRECEDENCE (token);
6837
6838 /* Popping an entry off the stack means we completed a subexpression:
6839 - either we found a token which is not an operator (`>' where it is not
6840 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6841 will happen repeatedly;
6842 - or, we found an operator which has lower priority. This is the case
6843 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6844 parsing `3 * 4'. */
6845 if (new_prec <= prec)
6846 {
6847 if (sp == stack)
6848 break;
6849 else
6850 goto pop;
6851 }
6852
6853 get_rhs:
6854 tree_type = binops_by_token[token->type].tree_type;
6855
6856 /* We used the operator token. */
6857 cp_lexer_consume_token (parser->lexer);
6858
6859 /* For "false && x" or "true || x", x will never be executed;
6860 disable warnings while evaluating it. */
6861 if (tree_type == TRUTH_ANDIF_EXPR)
6862 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6863 else if (tree_type == TRUTH_ORIF_EXPR)
6864 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6865
6866 /* Extract another operand. It may be the RHS of this expression
6867 or the LHS of a new, higher priority expression. */
6868 rhs = cp_parser_simple_cast_expression (parser);
6869 rhs_type = ERROR_MARK;
6870
6871 /* Get another operator token. Look up its precedence to avoid
6872 building a useless (immediately popped) stack entry for common
6873 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6874 token = cp_lexer_peek_token (parser->lexer);
6875 lookahead_prec = TOKEN_PRECEDENCE (token);
6876 if (lookahead_prec > new_prec)
6877 {
6878 /* ... and prepare to parse the RHS of the new, higher priority
6879 expression. Since precedence levels on the stack are
6880 monotonically increasing, we do not have to care about
6881 stack overflows. */
6882 sp->prec = prec;
6883 sp->tree_type = tree_type;
6884 sp->lhs = lhs;
6885 sp->lhs_type = lhs_type;
6886 sp++;
6887 lhs = rhs;
6888 lhs_type = rhs_type;
6889 prec = new_prec;
6890 new_prec = lookahead_prec;
6891 goto get_rhs;
6892
6893 pop:
6894 lookahead_prec = new_prec;
6895 /* If the stack is not empty, we have parsed into LHS the right side
6896 (`4' in the example above) of an expression we had suspended.
6897 We can use the information on the stack to recover the LHS (`3')
6898 from the stack together with the tree code (`MULT_EXPR'), and
6899 the precedence of the higher level subexpression
6900 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6901 which will be used to actually build the additive expression. */
6902 --sp;
6903 prec = sp->prec;
6904 tree_type = sp->tree_type;
6905 rhs = lhs;
6906 rhs_type = lhs_type;
6907 lhs = sp->lhs;
6908 lhs_type = sp->lhs_type;
6909 }
6910
6911 /* Undo the disabling of warnings done above. */
6912 if (tree_type == TRUTH_ANDIF_EXPR)
6913 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6914 else if (tree_type == TRUTH_ORIF_EXPR)
6915 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6916
6917 overloaded_p = false;
6918 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6919 ERROR_MARK for everything that is not a binary expression.
6920 This makes warn_about_parentheses miss some warnings that
6921 involve unary operators. For unary expressions we should
6922 pass the correct tree_code unless the unary expression was
6923 surrounded by parentheses.
6924 */
6925 if (no_toplevel_fold_p
6926 && lookahead_prec <= prec
6927 && sp == stack
6928 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6929 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6930 else
6931 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6932 &overloaded_p, tf_warning_or_error);
6933 lhs_type = tree_type;
6934
6935 /* If the binary operator required the use of an overloaded operator,
6936 then this expression cannot be an integral constant-expression.
6937 An overloaded operator can be used even if both operands are
6938 otherwise permissible in an integral constant-expression if at
6939 least one of the operands is of enumeration type. */
6940
6941 if (overloaded_p
6942 && cp_parser_non_integral_constant_expression (parser,
6943 NIC_OVERLOADED))
6944 return error_mark_node;
6945 }
6946
6947 return lhs;
6948 }
6949
6950
6951 /* Parse the `? expression : assignment-expression' part of a
6952 conditional-expression. The LOGICAL_OR_EXPR is the
6953 logical-or-expression that started the conditional-expression.
6954 Returns a representation of the entire conditional-expression.
6955
6956 This routine is used by cp_parser_assignment_expression.
6957
6958 ? expression : assignment-expression
6959
6960 GNU Extensions:
6961
6962 ? : assignment-expression */
6963
6964 static tree
6965 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6966 {
6967 tree expr;
6968 tree assignment_expr;
6969 struct cp_token *token;
6970
6971 /* Consume the `?' token. */
6972 cp_lexer_consume_token (parser->lexer);
6973 token = cp_lexer_peek_token (parser->lexer);
6974 if (cp_parser_allow_gnu_extensions_p (parser)
6975 && token->type == CPP_COLON)
6976 {
6977 pedwarn (token->location, OPT_pedantic,
6978 "ISO C++ does not allow ?: with omitted middle operand");
6979 /* Implicit true clause. */
6980 expr = NULL_TREE;
6981 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6982 warn_for_omitted_condop (token->location, logical_or_expr);
6983 }
6984 else
6985 {
6986 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6987 parser->colon_corrects_to_scope_p = false;
6988 /* Parse the expression. */
6989 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6990 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6991 c_inhibit_evaluation_warnings +=
6992 ((logical_or_expr == truthvalue_true_node)
6993 - (logical_or_expr == truthvalue_false_node));
6994 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6995 }
6996
6997 /* The next token should be a `:'. */
6998 cp_parser_require (parser, CPP_COLON, RT_COLON);
6999 /* Parse the assignment-expression. */
7000 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7001 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7002
7003 /* Build the conditional-expression. */
7004 return build_x_conditional_expr (logical_or_expr,
7005 expr,
7006 assignment_expr,
7007 tf_warning_or_error);
7008 }
7009
7010 /* Parse an assignment-expression.
7011
7012 assignment-expression:
7013 conditional-expression
7014 logical-or-expression assignment-operator assignment_expression
7015 throw-expression
7016
7017 CAST_P is true if this expression is the target of a cast.
7018
7019 Returns a representation for the expression. */
7020
7021 static tree
7022 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7023 cp_id_kind * pidk)
7024 {
7025 tree expr;
7026
7027 /* If the next token is the `throw' keyword, then we're looking at
7028 a throw-expression. */
7029 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7030 expr = cp_parser_throw_expression (parser);
7031 /* Otherwise, it must be that we are looking at a
7032 logical-or-expression. */
7033 else
7034 {
7035 /* Parse the binary expressions (logical-or-expression). */
7036 expr = cp_parser_binary_expression (parser, cast_p, false,
7037 PREC_NOT_OPERATOR, pidk);
7038 /* If the next token is a `?' then we're actually looking at a
7039 conditional-expression. */
7040 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7041 return cp_parser_question_colon_clause (parser, expr);
7042 else
7043 {
7044 enum tree_code assignment_operator;
7045
7046 /* If it's an assignment-operator, we're using the second
7047 production. */
7048 assignment_operator
7049 = cp_parser_assignment_operator_opt (parser);
7050 if (assignment_operator != ERROR_MARK)
7051 {
7052 bool non_constant_p;
7053
7054 /* Parse the right-hand side of the assignment. */
7055 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7056
7057 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7058 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7059
7060 /* An assignment may not appear in a
7061 constant-expression. */
7062 if (cp_parser_non_integral_constant_expression (parser,
7063 NIC_ASSIGNMENT))
7064 return error_mark_node;
7065 /* Build the assignment expression. */
7066 expr = build_x_modify_expr (expr,
7067 assignment_operator,
7068 rhs,
7069 tf_warning_or_error);
7070 }
7071 }
7072 }
7073
7074 return expr;
7075 }
7076
7077 /* Parse an (optional) assignment-operator.
7078
7079 assignment-operator: one of
7080 = *= /= %= += -= >>= <<= &= ^= |=
7081
7082 GNU Extension:
7083
7084 assignment-operator: one of
7085 <?= >?=
7086
7087 If the next token is an assignment operator, the corresponding tree
7088 code is returned, and the token is consumed. For example, for
7089 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7090 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7091 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7092 operator, ERROR_MARK is returned. */
7093
7094 static enum tree_code
7095 cp_parser_assignment_operator_opt (cp_parser* parser)
7096 {
7097 enum tree_code op;
7098 cp_token *token;
7099
7100 /* Peek at the next token. */
7101 token = cp_lexer_peek_token (parser->lexer);
7102
7103 switch (token->type)
7104 {
7105 case CPP_EQ:
7106 op = NOP_EXPR;
7107 break;
7108
7109 case CPP_MULT_EQ:
7110 op = MULT_EXPR;
7111 break;
7112
7113 case CPP_DIV_EQ:
7114 op = TRUNC_DIV_EXPR;
7115 break;
7116
7117 case CPP_MOD_EQ:
7118 op = TRUNC_MOD_EXPR;
7119 break;
7120
7121 case CPP_PLUS_EQ:
7122 op = PLUS_EXPR;
7123 break;
7124
7125 case CPP_MINUS_EQ:
7126 op = MINUS_EXPR;
7127 break;
7128
7129 case CPP_RSHIFT_EQ:
7130 op = RSHIFT_EXPR;
7131 break;
7132
7133 case CPP_LSHIFT_EQ:
7134 op = LSHIFT_EXPR;
7135 break;
7136
7137 case CPP_AND_EQ:
7138 op = BIT_AND_EXPR;
7139 break;
7140
7141 case CPP_XOR_EQ:
7142 op = BIT_XOR_EXPR;
7143 break;
7144
7145 case CPP_OR_EQ:
7146 op = BIT_IOR_EXPR;
7147 break;
7148
7149 default:
7150 /* Nothing else is an assignment operator. */
7151 op = ERROR_MARK;
7152 }
7153
7154 /* If it was an assignment operator, consume it. */
7155 if (op != ERROR_MARK)
7156 cp_lexer_consume_token (parser->lexer);
7157
7158 return op;
7159 }
7160
7161 /* Parse an expression.
7162
7163 expression:
7164 assignment-expression
7165 expression , assignment-expression
7166
7167 CAST_P is true if this expression is the target of a cast.
7168
7169 Returns a representation of the expression. */
7170
7171 static tree
7172 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7173 {
7174 tree expression = NULL_TREE;
7175
7176 while (true)
7177 {
7178 tree assignment_expression;
7179
7180 /* Parse the next assignment-expression. */
7181 assignment_expression
7182 = cp_parser_assignment_expression (parser, cast_p, pidk);
7183 /* If this is the first assignment-expression, we can just
7184 save it away. */
7185 if (!expression)
7186 expression = assignment_expression;
7187 else
7188 expression = build_x_compound_expr (expression,
7189 assignment_expression,
7190 tf_warning_or_error);
7191 /* If the next token is not a comma, then we are done with the
7192 expression. */
7193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7194 break;
7195 /* Consume the `,'. */
7196 cp_lexer_consume_token (parser->lexer);
7197 /* A comma operator cannot appear in a constant-expression. */
7198 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7199 expression = error_mark_node;
7200 }
7201
7202 return expression;
7203 }
7204
7205 /* Parse a constant-expression.
7206
7207 constant-expression:
7208 conditional-expression
7209
7210 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7211 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7212 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7213 is false, NON_CONSTANT_P should be NULL. */
7214
7215 static tree
7216 cp_parser_constant_expression (cp_parser* parser,
7217 bool allow_non_constant_p,
7218 bool *non_constant_p)
7219 {
7220 bool saved_integral_constant_expression_p;
7221 bool saved_allow_non_integral_constant_expression_p;
7222 bool saved_non_integral_constant_expression_p;
7223 tree expression;
7224
7225 /* It might seem that we could simply parse the
7226 conditional-expression, and then check to see if it were
7227 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7228 one that the compiler can figure out is constant, possibly after
7229 doing some simplifications or optimizations. The standard has a
7230 precise definition of constant-expression, and we must honor
7231 that, even though it is somewhat more restrictive.
7232
7233 For example:
7234
7235 int i[(2, 3)];
7236
7237 is not a legal declaration, because `(2, 3)' is not a
7238 constant-expression. The `,' operator is forbidden in a
7239 constant-expression. However, GCC's constant-folding machinery
7240 will fold this operation to an INTEGER_CST for `3'. */
7241
7242 /* Save the old settings. */
7243 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7244 saved_allow_non_integral_constant_expression_p
7245 = parser->allow_non_integral_constant_expression_p;
7246 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7247 /* We are now parsing a constant-expression. */
7248 parser->integral_constant_expression_p = true;
7249 parser->allow_non_integral_constant_expression_p
7250 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7251 parser->non_integral_constant_expression_p = false;
7252 /* Although the grammar says "conditional-expression", we parse an
7253 "assignment-expression", which also permits "throw-expression"
7254 and the use of assignment operators. In the case that
7255 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7256 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7257 actually essential that we look for an assignment-expression.
7258 For example, cp_parser_initializer_clauses uses this function to
7259 determine whether a particular assignment-expression is in fact
7260 constant. */
7261 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7262 /* Restore the old settings. */
7263 parser->integral_constant_expression_p
7264 = saved_integral_constant_expression_p;
7265 parser->allow_non_integral_constant_expression_p
7266 = saved_allow_non_integral_constant_expression_p;
7267 if (allow_non_constant_p)
7268 *non_constant_p = parser->non_integral_constant_expression_p;
7269 else if (parser->non_integral_constant_expression_p
7270 && cxx_dialect < cxx0x)
7271 expression = error_mark_node;
7272 parser->non_integral_constant_expression_p
7273 = saved_non_integral_constant_expression_p;
7274
7275 return expression;
7276 }
7277
7278 /* Parse __builtin_offsetof.
7279
7280 offsetof-expression:
7281 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7282
7283 offsetof-member-designator:
7284 id-expression
7285 | offsetof-member-designator "." id-expression
7286 | offsetof-member-designator "[" expression "]"
7287 | offsetof-member-designator "->" id-expression */
7288
7289 static tree
7290 cp_parser_builtin_offsetof (cp_parser *parser)
7291 {
7292 int save_ice_p, save_non_ice_p;
7293 tree type, expr;
7294 cp_id_kind dummy;
7295 cp_token *token;
7296
7297 /* We're about to accept non-integral-constant things, but will
7298 definitely yield an integral constant expression. Save and
7299 restore these values around our local parsing. */
7300 save_ice_p = parser->integral_constant_expression_p;
7301 save_non_ice_p = parser->non_integral_constant_expression_p;
7302
7303 /* Consume the "__builtin_offsetof" token. */
7304 cp_lexer_consume_token (parser->lexer);
7305 /* Consume the opening `('. */
7306 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7307 /* Parse the type-id. */
7308 type = cp_parser_type_id (parser);
7309 /* Look for the `,'. */
7310 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7311 token = cp_lexer_peek_token (parser->lexer);
7312
7313 /* Build the (type *)null that begins the traditional offsetof macro. */
7314 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7315 tf_warning_or_error);
7316
7317 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7318 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7319 true, &dummy, token->location);
7320 while (true)
7321 {
7322 token = cp_lexer_peek_token (parser->lexer);
7323 switch (token->type)
7324 {
7325 case CPP_OPEN_SQUARE:
7326 /* offsetof-member-designator "[" expression "]" */
7327 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7328 break;
7329
7330 case CPP_DEREF:
7331 /* offsetof-member-designator "->" identifier */
7332 expr = grok_array_decl (expr, integer_zero_node);
7333 /* FALLTHRU */
7334
7335 case CPP_DOT:
7336 /* offsetof-member-designator "." identifier */
7337 cp_lexer_consume_token (parser->lexer);
7338 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7339 expr, true, &dummy,
7340 token->location);
7341 break;
7342
7343 case CPP_CLOSE_PAREN:
7344 /* Consume the ")" token. */
7345 cp_lexer_consume_token (parser->lexer);
7346 goto success;
7347
7348 default:
7349 /* Error. We know the following require will fail, but
7350 that gives the proper error message. */
7351 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7352 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7353 expr = error_mark_node;
7354 goto failure;
7355 }
7356 }
7357
7358 success:
7359 /* If we're processing a template, we can't finish the semantics yet.
7360 Otherwise we can fold the entire expression now. */
7361 if (processing_template_decl)
7362 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7363 else
7364 expr = finish_offsetof (expr);
7365
7366 failure:
7367 parser->integral_constant_expression_p = save_ice_p;
7368 parser->non_integral_constant_expression_p = save_non_ice_p;
7369
7370 return expr;
7371 }
7372
7373 /* Parse a trait expression. */
7374
7375 static tree
7376 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7377 {
7378 cp_trait_kind kind;
7379 tree type1, type2 = NULL_TREE;
7380 bool binary = false;
7381 cp_decl_specifier_seq decl_specs;
7382
7383 switch (keyword)
7384 {
7385 case RID_HAS_NOTHROW_ASSIGN:
7386 kind = CPTK_HAS_NOTHROW_ASSIGN;
7387 break;
7388 case RID_HAS_NOTHROW_CONSTRUCTOR:
7389 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7390 break;
7391 case RID_HAS_NOTHROW_COPY:
7392 kind = CPTK_HAS_NOTHROW_COPY;
7393 break;
7394 case RID_HAS_TRIVIAL_ASSIGN:
7395 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7396 break;
7397 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7398 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7399 break;
7400 case RID_HAS_TRIVIAL_COPY:
7401 kind = CPTK_HAS_TRIVIAL_COPY;
7402 break;
7403 case RID_HAS_TRIVIAL_DESTRUCTOR:
7404 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7405 break;
7406 case RID_HAS_VIRTUAL_DESTRUCTOR:
7407 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7408 break;
7409 case RID_IS_ABSTRACT:
7410 kind = CPTK_IS_ABSTRACT;
7411 break;
7412 case RID_IS_BASE_OF:
7413 kind = CPTK_IS_BASE_OF;
7414 binary = true;
7415 break;
7416 case RID_IS_CLASS:
7417 kind = CPTK_IS_CLASS;
7418 break;
7419 case RID_IS_CONVERTIBLE_TO:
7420 kind = CPTK_IS_CONVERTIBLE_TO;
7421 binary = true;
7422 break;
7423 case RID_IS_EMPTY:
7424 kind = CPTK_IS_EMPTY;
7425 break;
7426 case RID_IS_ENUM:
7427 kind = CPTK_IS_ENUM;
7428 break;
7429 case RID_IS_POD:
7430 kind = CPTK_IS_POD;
7431 break;
7432 case RID_IS_POLYMORPHIC:
7433 kind = CPTK_IS_POLYMORPHIC;
7434 break;
7435 case RID_IS_STD_LAYOUT:
7436 kind = CPTK_IS_STD_LAYOUT;
7437 break;
7438 case RID_IS_TRIVIAL:
7439 kind = CPTK_IS_TRIVIAL;
7440 break;
7441 case RID_IS_UNION:
7442 kind = CPTK_IS_UNION;
7443 break;
7444 case RID_IS_LITERAL_TYPE:
7445 kind = CPTK_IS_LITERAL_TYPE;
7446 break;
7447 default:
7448 gcc_unreachable ();
7449 }
7450
7451 /* Consume the token. */
7452 cp_lexer_consume_token (parser->lexer);
7453
7454 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7455
7456 type1 = cp_parser_type_id (parser);
7457
7458 if (type1 == error_mark_node)
7459 return error_mark_node;
7460
7461 /* Build a trivial decl-specifier-seq. */
7462 clear_decl_specs (&decl_specs);
7463 decl_specs.type = type1;
7464
7465 /* Call grokdeclarator to figure out what type this is. */
7466 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7467 /*initialized=*/0, /*attrlist=*/NULL);
7468
7469 if (binary)
7470 {
7471 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7472
7473 type2 = cp_parser_type_id (parser);
7474
7475 if (type2 == error_mark_node)
7476 return error_mark_node;
7477
7478 /* Build a trivial decl-specifier-seq. */
7479 clear_decl_specs (&decl_specs);
7480 decl_specs.type = type2;
7481
7482 /* Call grokdeclarator to figure out what type this is. */
7483 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7484 /*initialized=*/0, /*attrlist=*/NULL);
7485 }
7486
7487 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7488
7489 /* Complete the trait expression, which may mean either processing
7490 the trait expr now or saving it for template instantiation. */
7491 return finish_trait_expr (kind, type1, type2);
7492 }
7493
7494 /* Lambdas that appear in variable initializer or default argument scope
7495 get that in their mangling, so we need to record it. We might as well
7496 use the count for function and namespace scopes as well. */
7497 static GTY(()) tree lambda_scope;
7498 static GTY(()) int lambda_count;
7499 typedef struct GTY(()) tree_int
7500 {
7501 tree t;
7502 int i;
7503 } tree_int;
7504 DEF_VEC_O(tree_int);
7505 DEF_VEC_ALLOC_O(tree_int,gc);
7506 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7507
7508 static void
7509 start_lambda_scope (tree decl)
7510 {
7511 tree_int ti;
7512 gcc_assert (decl);
7513 /* Once we're inside a function, we ignore other scopes and just push
7514 the function again so that popping works properly. */
7515 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7516 decl = current_function_decl;
7517 ti.t = lambda_scope;
7518 ti.i = lambda_count;
7519 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7520 if (lambda_scope != decl)
7521 {
7522 /* Don't reset the count if we're still in the same function. */
7523 lambda_scope = decl;
7524 lambda_count = 0;
7525 }
7526 }
7527
7528 static void
7529 record_lambda_scope (tree lambda)
7530 {
7531 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7532 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7533 }
7534
7535 static void
7536 finish_lambda_scope (void)
7537 {
7538 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7539 if (lambda_scope != p->t)
7540 {
7541 lambda_scope = p->t;
7542 lambda_count = p->i;
7543 }
7544 VEC_pop (tree_int, lambda_scope_stack);
7545 }
7546
7547 /* Parse a lambda expression.
7548
7549 lambda-expression:
7550 lambda-introducer lambda-declarator [opt] compound-statement
7551
7552 Returns a representation of the expression. */
7553
7554 static tree
7555 cp_parser_lambda_expression (cp_parser* parser)
7556 {
7557 tree lambda_expr = build_lambda_expr ();
7558 tree type;
7559
7560 LAMBDA_EXPR_LOCATION (lambda_expr)
7561 = cp_lexer_peek_token (parser->lexer)->location;
7562
7563 if (cp_unevaluated_operand)
7564 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7565 "lambda-expression in unevaluated context");
7566
7567 /* We may be in the middle of deferred access check. Disable
7568 it now. */
7569 push_deferring_access_checks (dk_no_deferred);
7570
7571 cp_parser_lambda_introducer (parser, lambda_expr);
7572
7573 type = begin_lambda_type (lambda_expr);
7574
7575 record_lambda_scope (lambda_expr);
7576
7577 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7578 determine_visibility (TYPE_NAME (type));
7579
7580 /* Now that we've started the type, add the capture fields for any
7581 explicit captures. */
7582 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7583
7584 {
7585 /* Inside the class, surrounding template-parameter-lists do not apply. */
7586 unsigned int saved_num_template_parameter_lists
7587 = parser->num_template_parameter_lists;
7588
7589 parser->num_template_parameter_lists = 0;
7590
7591 /* By virtue of defining a local class, a lambda expression has access to
7592 the private variables of enclosing classes. */
7593
7594 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7595
7596 cp_parser_lambda_body (parser, lambda_expr);
7597
7598 /* The capture list was built up in reverse order; fix that now. */
7599 {
7600 tree newlist = NULL_TREE;
7601 tree elt, next;
7602
7603 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7604 elt; elt = next)
7605 {
7606 tree field = TREE_PURPOSE (elt);
7607 char *buf;
7608
7609 next = TREE_CHAIN (elt);
7610 TREE_CHAIN (elt) = newlist;
7611 newlist = elt;
7612
7613 /* Also add __ to the beginning of the field name so that code
7614 outside the lambda body can't see the captured name. We could
7615 just remove the name entirely, but this is more useful for
7616 debugging. */
7617 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7618 /* The 'this' capture already starts with __. */
7619 continue;
7620
7621 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7622 buf[1] = buf[0] = '_';
7623 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7624 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7625 DECL_NAME (field) = get_identifier (buf);
7626 }
7627 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7628 }
7629
7630 maybe_add_lambda_conv_op (type);
7631
7632 type = finish_struct (type, /*attributes=*/NULL_TREE);
7633
7634 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7635 }
7636
7637 pop_deferring_access_checks ();
7638
7639 return build_lambda_object (lambda_expr);
7640 }
7641
7642 /* Parse the beginning of a lambda expression.
7643
7644 lambda-introducer:
7645 [ lambda-capture [opt] ]
7646
7647 LAMBDA_EXPR is the current representation of the lambda expression. */
7648
7649 static void
7650 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7651 {
7652 /* Need commas after the first capture. */
7653 bool first = true;
7654
7655 /* Eat the leading `['. */
7656 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7657
7658 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7659 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7660 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7661 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7662 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7663 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7664
7665 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7666 {
7667 cp_lexer_consume_token (parser->lexer);
7668 first = false;
7669 }
7670
7671 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7672 {
7673 cp_token* capture_token;
7674 tree capture_id;
7675 tree capture_init_expr;
7676 cp_id_kind idk = CP_ID_KIND_NONE;
7677 bool explicit_init_p = false;
7678
7679 enum capture_kind_type
7680 {
7681 BY_COPY,
7682 BY_REFERENCE
7683 };
7684 enum capture_kind_type capture_kind = BY_COPY;
7685
7686 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7687 {
7688 error ("expected end of capture-list");
7689 return;
7690 }
7691
7692 if (first)
7693 first = false;
7694 else
7695 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7696
7697 /* Possibly capture `this'. */
7698 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7699 {
7700 cp_lexer_consume_token (parser->lexer);
7701 add_capture (lambda_expr,
7702 /*id=*/get_identifier ("__this"),
7703 /*initializer=*/finish_this_expr(),
7704 /*by_reference_p=*/false,
7705 explicit_init_p);
7706 continue;
7707 }
7708
7709 /* Remember whether we want to capture as a reference or not. */
7710 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7711 {
7712 capture_kind = BY_REFERENCE;
7713 cp_lexer_consume_token (parser->lexer);
7714 }
7715
7716 /* Get the identifier. */
7717 capture_token = cp_lexer_peek_token (parser->lexer);
7718 capture_id = cp_parser_identifier (parser);
7719
7720 if (capture_id == error_mark_node)
7721 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7722 delimiters, but I modified this to stop on unnested ']' as well. It
7723 was already changed to stop on unnested '}', so the
7724 "closing_parenthesis" name is no more misleading with my change. */
7725 {
7726 cp_parser_skip_to_closing_parenthesis (parser,
7727 /*recovering=*/true,
7728 /*or_comma=*/true,
7729 /*consume_paren=*/true);
7730 break;
7731 }
7732
7733 /* Find the initializer for this capture. */
7734 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7735 {
7736 /* An explicit expression exists. */
7737 cp_lexer_consume_token (parser->lexer);
7738 pedwarn (input_location, OPT_pedantic,
7739 "ISO C++ does not allow initializers "
7740 "in lambda expression capture lists");
7741 capture_init_expr = cp_parser_assignment_expression (parser,
7742 /*cast_p=*/true,
7743 &idk);
7744 explicit_init_p = true;
7745 }
7746 else
7747 {
7748 const char* error_msg;
7749
7750 /* Turn the identifier into an id-expression. */
7751 capture_init_expr
7752 = cp_parser_lookup_name
7753 (parser,
7754 capture_id,
7755 none_type,
7756 /*is_template=*/false,
7757 /*is_namespace=*/false,
7758 /*check_dependency=*/true,
7759 /*ambiguous_decls=*/NULL,
7760 capture_token->location);
7761
7762 capture_init_expr
7763 = finish_id_expression
7764 (capture_id,
7765 capture_init_expr,
7766 parser->scope,
7767 &idk,
7768 /*integral_constant_expression_p=*/false,
7769 /*allow_non_integral_constant_expression_p=*/false,
7770 /*non_integral_constant_expression_p=*/NULL,
7771 /*template_p=*/false,
7772 /*done=*/true,
7773 /*address_p=*/false,
7774 /*template_arg_p=*/false,
7775 &error_msg,
7776 capture_token->location);
7777 }
7778
7779 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7780 capture_init_expr
7781 = unqualified_name_lookup_error (capture_init_expr);
7782
7783 add_capture (lambda_expr,
7784 capture_id,
7785 capture_init_expr,
7786 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7787 explicit_init_p);
7788 }
7789
7790 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7791 }
7792
7793 /* Parse the (optional) middle of a lambda expression.
7794
7795 lambda-declarator:
7796 ( parameter-declaration-clause [opt] )
7797 attribute-specifier [opt]
7798 mutable [opt]
7799 exception-specification [opt]
7800 lambda-return-type-clause [opt]
7801
7802 LAMBDA_EXPR is the current representation of the lambda expression. */
7803
7804 static void
7805 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7806 {
7807 /* 5.1.1.4 of the standard says:
7808 If a lambda-expression does not include a lambda-declarator, it is as if
7809 the lambda-declarator were ().
7810 This means an empty parameter list, no attributes, and no exception
7811 specification. */
7812 tree param_list = void_list_node;
7813 tree attributes = NULL_TREE;
7814 tree exception_spec = NULL_TREE;
7815 tree t;
7816
7817 /* The lambda-declarator is optional, but must begin with an opening
7818 parenthesis if present. */
7819 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7820 {
7821 cp_lexer_consume_token (parser->lexer);
7822
7823 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7824
7825 /* Parse parameters. */
7826 param_list = cp_parser_parameter_declaration_clause (parser);
7827
7828 /* Default arguments shall not be specified in the
7829 parameter-declaration-clause of a lambda-declarator. */
7830 for (t = param_list; t; t = TREE_CHAIN (t))
7831 if (TREE_PURPOSE (t))
7832 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7833 "default argument specified for lambda parameter");
7834
7835 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7836
7837 attributes = cp_parser_attributes_opt (parser);
7838
7839 /* Parse optional `mutable' keyword. */
7840 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7841 {
7842 cp_lexer_consume_token (parser->lexer);
7843 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7844 }
7845
7846 /* Parse optional exception specification. */
7847 exception_spec = cp_parser_exception_specification_opt (parser);
7848
7849 /* Parse optional trailing return type. */
7850 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7851 {
7852 cp_lexer_consume_token (parser->lexer);
7853 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7854 }
7855
7856 /* The function parameters must be in scope all the way until after the
7857 trailing-return-type in case of decltype. */
7858 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7859 pop_binding (DECL_NAME (t), t);
7860
7861 leave_scope ();
7862 }
7863
7864 /* Create the function call operator.
7865
7866 Messing with declarators like this is no uglier than building up the
7867 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7868 other code. */
7869 {
7870 cp_decl_specifier_seq return_type_specs;
7871 cp_declarator* declarator;
7872 tree fco;
7873 int quals;
7874 void *p;
7875
7876 clear_decl_specs (&return_type_specs);
7877 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7878 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7879 else
7880 /* Maybe we will deduce the return type later, but we can use void
7881 as a placeholder return type anyways. */
7882 return_type_specs.type = void_type_node;
7883
7884 p = obstack_alloc (&declarator_obstack, 0);
7885
7886 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7887 sfk_none);
7888
7889 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7890 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7891 declarator = make_call_declarator (declarator, param_list, quals,
7892 exception_spec,
7893 /*late_return_type=*/NULL_TREE);
7894 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7895
7896 fco = grokmethod (&return_type_specs,
7897 declarator,
7898 attributes);
7899 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7900 DECL_ARTIFICIAL (fco) = 1;
7901
7902 finish_member_declaration (fco);
7903
7904 obstack_free (&declarator_obstack, p);
7905 }
7906 }
7907
7908 /* Parse the body of a lambda expression, which is simply
7909
7910 compound-statement
7911
7912 but which requires special handling.
7913 LAMBDA_EXPR is the current representation of the lambda expression. */
7914
7915 static void
7916 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7917 {
7918 bool nested = (current_function_decl != NULL_TREE);
7919 if (nested)
7920 push_function_context ();
7921
7922 /* Finish the function call operator
7923 - class_specifier
7924 + late_parsing_for_member
7925 + function_definition_after_declarator
7926 + ctor_initializer_opt_and_function_body */
7927 {
7928 tree fco = lambda_function (lambda_expr);
7929 tree body;
7930 bool done = false;
7931
7932 /* Let the front end know that we are going to be defining this
7933 function. */
7934 start_preparsed_function (fco,
7935 NULL_TREE,
7936 SF_PRE_PARSED | SF_INCLASS_INLINE);
7937
7938 start_lambda_scope (fco);
7939 body = begin_function_body ();
7940
7941 /* 5.1.1.4 of the standard says:
7942 If a lambda-expression does not include a trailing-return-type, it
7943 is as if the trailing-return-type denotes the following type:
7944 * if the compound-statement is of the form
7945 { return attribute-specifier [opt] expression ; }
7946 the type of the returned expression after lvalue-to-rvalue
7947 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7948 (_conv.array_ 4.2), and function-to-pointer conversion
7949 (_conv.func_ 4.3);
7950 * otherwise, void. */
7951
7952 /* In a lambda that has neither a lambda-return-type-clause
7953 nor a deducible form, errors should be reported for return statements
7954 in the body. Since we used void as the placeholder return type, parsing
7955 the body as usual will give such desired behavior. */
7956 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7957 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7958 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7959 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7960 {
7961 tree compound_stmt;
7962 tree expr = NULL_TREE;
7963 cp_id_kind idk = CP_ID_KIND_NONE;
7964
7965 /* Parse tentatively in case there's more after the initial return
7966 statement. */
7967 cp_parser_parse_tentatively (parser);
7968
7969 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7970 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7971
7972 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7973
7974 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7975 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7976
7977 if (cp_parser_parse_definitely (parser))
7978 {
7979 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7980
7981 compound_stmt = begin_compound_stmt (0);
7982 /* Will get error here if type not deduced yet. */
7983 finish_return_stmt (expr);
7984 finish_compound_stmt (compound_stmt);
7985
7986 done = true;
7987 }
7988 }
7989
7990 if (!done)
7991 {
7992 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7993 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7994 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7995 cp_parser_compound_stmt does not pass it. */
7996 cp_parser_function_body (parser);
7997 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7998 }
7999
8000 finish_function_body (body);
8001 finish_lambda_scope ();
8002
8003 /* Finish the function and generate code for it if necessary. */
8004 expand_or_defer_fn (finish_function (/*inline*/2));
8005 }
8006
8007 if (nested)
8008 pop_function_context();
8009 }
8010
8011 /* Statements [gram.stmt.stmt] */
8012
8013 /* Parse a statement.
8014
8015 statement:
8016 labeled-statement
8017 expression-statement
8018 compound-statement
8019 selection-statement
8020 iteration-statement
8021 jump-statement
8022 declaration-statement
8023 try-block
8024
8025 IN_COMPOUND is true when the statement is nested inside a
8026 cp_parser_compound_statement; this matters for certain pragmas.
8027
8028 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8029 is a (possibly labeled) if statement which is not enclosed in braces
8030 and has an else clause. This is used to implement -Wparentheses. */
8031
8032 static void
8033 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8034 bool in_compound, bool *if_p)
8035 {
8036 tree statement;
8037 cp_token *token;
8038 location_t statement_location;
8039
8040 restart:
8041 if (if_p != NULL)
8042 *if_p = false;
8043 /* There is no statement yet. */
8044 statement = NULL_TREE;
8045 /* Peek at the next token. */
8046 token = cp_lexer_peek_token (parser->lexer);
8047 /* Remember the location of the first token in the statement. */
8048 statement_location = token->location;
8049 /* If this is a keyword, then that will often determine what kind of
8050 statement we have. */
8051 if (token->type == CPP_KEYWORD)
8052 {
8053 enum rid keyword = token->keyword;
8054
8055 switch (keyword)
8056 {
8057 case RID_CASE:
8058 case RID_DEFAULT:
8059 /* Looks like a labeled-statement with a case label.
8060 Parse the label, and then use tail recursion to parse
8061 the statement. */
8062 cp_parser_label_for_labeled_statement (parser);
8063 goto restart;
8064
8065 case RID_IF:
8066 case RID_SWITCH:
8067 statement = cp_parser_selection_statement (parser, if_p);
8068 break;
8069
8070 case RID_WHILE:
8071 case RID_DO:
8072 case RID_FOR:
8073 statement = cp_parser_iteration_statement (parser);
8074 break;
8075
8076 case RID_BREAK:
8077 case RID_CONTINUE:
8078 case RID_RETURN:
8079 case RID_GOTO:
8080 statement = cp_parser_jump_statement (parser);
8081 break;
8082
8083 /* Objective-C++ exception-handling constructs. */
8084 case RID_AT_TRY:
8085 case RID_AT_CATCH:
8086 case RID_AT_FINALLY:
8087 case RID_AT_SYNCHRONIZED:
8088 case RID_AT_THROW:
8089 statement = cp_parser_objc_statement (parser);
8090 break;
8091
8092 case RID_TRY:
8093 statement = cp_parser_try_block (parser);
8094 break;
8095
8096 case RID_NAMESPACE:
8097 /* This must be a namespace alias definition. */
8098 cp_parser_declaration_statement (parser);
8099 return;
8100
8101 default:
8102 /* It might be a keyword like `int' that can start a
8103 declaration-statement. */
8104 break;
8105 }
8106 }
8107 else if (token->type == CPP_NAME)
8108 {
8109 /* If the next token is a `:', then we are looking at a
8110 labeled-statement. */
8111 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8112 if (token->type == CPP_COLON)
8113 {
8114 /* Looks like a labeled-statement with an ordinary label.
8115 Parse the label, and then use tail recursion to parse
8116 the statement. */
8117 cp_parser_label_for_labeled_statement (parser);
8118 goto restart;
8119 }
8120 }
8121 /* Anything that starts with a `{' must be a compound-statement. */
8122 else if (token->type == CPP_OPEN_BRACE)
8123 statement = cp_parser_compound_statement (parser, NULL, false);
8124 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8125 a statement all its own. */
8126 else if (token->type == CPP_PRAGMA)
8127 {
8128 /* Only certain OpenMP pragmas are attached to statements, and thus
8129 are considered statements themselves. All others are not. In
8130 the context of a compound, accept the pragma as a "statement" and
8131 return so that we can check for a close brace. Otherwise we
8132 require a real statement and must go back and read one. */
8133 if (in_compound)
8134 cp_parser_pragma (parser, pragma_compound);
8135 else if (!cp_parser_pragma (parser, pragma_stmt))
8136 goto restart;
8137 return;
8138 }
8139 else if (token->type == CPP_EOF)
8140 {
8141 cp_parser_error (parser, "expected statement");
8142 return;
8143 }
8144
8145 /* Everything else must be a declaration-statement or an
8146 expression-statement. Try for the declaration-statement
8147 first, unless we are looking at a `;', in which case we know that
8148 we have an expression-statement. */
8149 if (!statement)
8150 {
8151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8152 {
8153 cp_parser_parse_tentatively (parser);
8154 /* Try to parse the declaration-statement. */
8155 cp_parser_declaration_statement (parser);
8156 /* If that worked, we're done. */
8157 if (cp_parser_parse_definitely (parser))
8158 return;
8159 }
8160 /* Look for an expression-statement instead. */
8161 statement = cp_parser_expression_statement (parser, in_statement_expr);
8162 }
8163
8164 /* Set the line number for the statement. */
8165 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8166 SET_EXPR_LOCATION (statement, statement_location);
8167 }
8168
8169 /* Parse the label for a labeled-statement, i.e.
8170
8171 identifier :
8172 case constant-expression :
8173 default :
8174
8175 GNU Extension:
8176 case constant-expression ... constant-expression : statement
8177
8178 When a label is parsed without errors, the label is added to the
8179 parse tree by the finish_* functions, so this function doesn't
8180 have to return the label. */
8181
8182 static void
8183 cp_parser_label_for_labeled_statement (cp_parser* parser)
8184 {
8185 cp_token *token;
8186 tree label = NULL_TREE;
8187 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8188
8189 /* The next token should be an identifier. */
8190 token = cp_lexer_peek_token (parser->lexer);
8191 if (token->type != CPP_NAME
8192 && token->type != CPP_KEYWORD)
8193 {
8194 cp_parser_error (parser, "expected labeled-statement");
8195 return;
8196 }
8197
8198 parser->colon_corrects_to_scope_p = false;
8199 switch (token->keyword)
8200 {
8201 case RID_CASE:
8202 {
8203 tree expr, expr_hi;
8204 cp_token *ellipsis;
8205
8206 /* Consume the `case' token. */
8207 cp_lexer_consume_token (parser->lexer);
8208 /* Parse the constant-expression. */
8209 expr = cp_parser_constant_expression (parser,
8210 /*allow_non_constant_p=*/false,
8211 NULL);
8212
8213 ellipsis = cp_lexer_peek_token (parser->lexer);
8214 if (ellipsis->type == CPP_ELLIPSIS)
8215 {
8216 /* Consume the `...' token. */
8217 cp_lexer_consume_token (parser->lexer);
8218 expr_hi =
8219 cp_parser_constant_expression (parser,
8220 /*allow_non_constant_p=*/false,
8221 NULL);
8222 /* We don't need to emit warnings here, as the common code
8223 will do this for us. */
8224 }
8225 else
8226 expr_hi = NULL_TREE;
8227
8228 if (parser->in_switch_statement_p)
8229 finish_case_label (token->location, expr, expr_hi);
8230 else
8231 error_at (token->location,
8232 "case label %qE not within a switch statement",
8233 expr);
8234 }
8235 break;
8236
8237 case RID_DEFAULT:
8238 /* Consume the `default' token. */
8239 cp_lexer_consume_token (parser->lexer);
8240
8241 if (parser->in_switch_statement_p)
8242 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8243 else
8244 error_at (token->location, "case label not within a switch statement");
8245 break;
8246
8247 default:
8248 /* Anything else must be an ordinary label. */
8249 label = finish_label_stmt (cp_parser_identifier (parser));
8250 break;
8251 }
8252
8253 /* Require the `:' token. */
8254 cp_parser_require (parser, CPP_COLON, RT_COLON);
8255
8256 /* An ordinary label may optionally be followed by attributes.
8257 However, this is only permitted if the attributes are then
8258 followed by a semicolon. This is because, for backward
8259 compatibility, when parsing
8260 lab: __attribute__ ((unused)) int i;
8261 we want the attribute to attach to "i", not "lab". */
8262 if (label != NULL_TREE
8263 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8264 {
8265 tree attrs;
8266
8267 cp_parser_parse_tentatively (parser);
8268 attrs = cp_parser_attributes_opt (parser);
8269 if (attrs == NULL_TREE
8270 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8271 cp_parser_abort_tentative_parse (parser);
8272 else if (!cp_parser_parse_definitely (parser))
8273 ;
8274 else
8275 cplus_decl_attributes (&label, attrs, 0);
8276 }
8277
8278 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8279 }
8280
8281 /* Parse an expression-statement.
8282
8283 expression-statement:
8284 expression [opt] ;
8285
8286 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8287 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8288 indicates whether this expression-statement is part of an
8289 expression statement. */
8290
8291 static tree
8292 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8293 {
8294 tree statement = NULL_TREE;
8295 cp_token *token = cp_lexer_peek_token (parser->lexer);
8296
8297 /* If the next token is a ';', then there is no expression
8298 statement. */
8299 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8300 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8301
8302 /* Give a helpful message for "A<T>::type t;" and the like. */
8303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8304 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8305 {
8306 if (TREE_CODE (statement) == SCOPE_REF)
8307 error_at (token->location, "need %<typename%> before %qE because "
8308 "%qT is a dependent scope",
8309 statement, TREE_OPERAND (statement, 0));
8310 else if (is_overloaded_fn (statement)
8311 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8312 {
8313 /* A::A a; */
8314 tree fn = get_first_fn (statement);
8315 error_at (token->location,
8316 "%<%T::%D%> names the constructor, not the type",
8317 DECL_CONTEXT (fn), DECL_NAME (fn));
8318 }
8319 }
8320
8321 /* Consume the final `;'. */
8322 cp_parser_consume_semicolon_at_end_of_statement (parser);
8323
8324 if (in_statement_expr
8325 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8326 /* This is the final expression statement of a statement
8327 expression. */
8328 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8329 else if (statement)
8330 statement = finish_expr_stmt (statement);
8331 else
8332 finish_stmt ();
8333
8334 return statement;
8335 }
8336
8337 /* Parse a compound-statement.
8338
8339 compound-statement:
8340 { statement-seq [opt] }
8341
8342 GNU extension:
8343
8344 compound-statement:
8345 { label-declaration-seq [opt] statement-seq [opt] }
8346
8347 label-declaration-seq:
8348 label-declaration
8349 label-declaration-seq label-declaration
8350
8351 Returns a tree representing the statement. */
8352
8353 static tree
8354 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8355 bool in_try)
8356 {
8357 tree compound_stmt;
8358
8359 /* Consume the `{'. */
8360 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8361 return error_mark_node;
8362 /* Begin the compound-statement. */
8363 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8364 /* If the next keyword is `__label__' we have a label declaration. */
8365 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8366 cp_parser_label_declaration (parser);
8367 /* Parse an (optional) statement-seq. */
8368 cp_parser_statement_seq_opt (parser, in_statement_expr);
8369 /* Finish the compound-statement. */
8370 finish_compound_stmt (compound_stmt);
8371 /* Consume the `}'. */
8372 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8373
8374 return compound_stmt;
8375 }
8376
8377 /* Parse an (optional) statement-seq.
8378
8379 statement-seq:
8380 statement
8381 statement-seq [opt] statement */
8382
8383 static void
8384 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8385 {
8386 /* Scan statements until there aren't any more. */
8387 while (true)
8388 {
8389 cp_token *token = cp_lexer_peek_token (parser->lexer);
8390
8391 /* If we are looking at a `}', then we have run out of
8392 statements; the same is true if we have reached the end
8393 of file, or have stumbled upon a stray '@end'. */
8394 if (token->type == CPP_CLOSE_BRACE
8395 || token->type == CPP_EOF
8396 || token->type == CPP_PRAGMA_EOL
8397 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8398 break;
8399
8400 /* If we are in a compound statement and find 'else' then
8401 something went wrong. */
8402 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8403 {
8404 if (parser->in_statement & IN_IF_STMT)
8405 break;
8406 else
8407 {
8408 token = cp_lexer_consume_token (parser->lexer);
8409 error_at (token->location, "%<else%> without a previous %<if%>");
8410 }
8411 }
8412
8413 /* Parse the statement. */
8414 cp_parser_statement (parser, in_statement_expr, true, NULL);
8415 }
8416 }
8417
8418 /* Parse a selection-statement.
8419
8420 selection-statement:
8421 if ( condition ) statement
8422 if ( condition ) statement else statement
8423 switch ( condition ) statement
8424
8425 Returns the new IF_STMT or SWITCH_STMT.
8426
8427 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8428 is a (possibly labeled) if statement which is not enclosed in
8429 braces and has an else clause. This is used to implement
8430 -Wparentheses. */
8431
8432 static tree
8433 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8434 {
8435 cp_token *token;
8436 enum rid keyword;
8437
8438 if (if_p != NULL)
8439 *if_p = false;
8440
8441 /* Peek at the next token. */
8442 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8443
8444 /* See what kind of keyword it is. */
8445 keyword = token->keyword;
8446 switch (keyword)
8447 {
8448 case RID_IF:
8449 case RID_SWITCH:
8450 {
8451 tree statement;
8452 tree condition;
8453
8454 /* Look for the `('. */
8455 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8456 {
8457 cp_parser_skip_to_end_of_statement (parser);
8458 return error_mark_node;
8459 }
8460
8461 /* Begin the selection-statement. */
8462 if (keyword == RID_IF)
8463 statement = begin_if_stmt ();
8464 else
8465 statement = begin_switch_stmt ();
8466
8467 /* Parse the condition. */
8468 condition = cp_parser_condition (parser);
8469 /* Look for the `)'. */
8470 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8471 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8472 /*consume_paren=*/true);
8473
8474 if (keyword == RID_IF)
8475 {
8476 bool nested_if;
8477 unsigned char in_statement;
8478
8479 /* Add the condition. */
8480 finish_if_stmt_cond (condition, statement);
8481
8482 /* Parse the then-clause. */
8483 in_statement = parser->in_statement;
8484 parser->in_statement |= IN_IF_STMT;
8485 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8486 {
8487 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8488 add_stmt (build_empty_stmt (loc));
8489 cp_lexer_consume_token (parser->lexer);
8490 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8491 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8492 "empty body in an %<if%> statement");
8493 nested_if = false;
8494 }
8495 else
8496 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8497 parser->in_statement = in_statement;
8498
8499 finish_then_clause (statement);
8500
8501 /* If the next token is `else', parse the else-clause. */
8502 if (cp_lexer_next_token_is_keyword (parser->lexer,
8503 RID_ELSE))
8504 {
8505 /* Consume the `else' keyword. */
8506 cp_lexer_consume_token (parser->lexer);
8507 begin_else_clause (statement);
8508 /* Parse the else-clause. */
8509 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8510 {
8511 location_t loc;
8512 loc = cp_lexer_peek_token (parser->lexer)->location;
8513 warning_at (loc,
8514 OPT_Wempty_body, "suggest braces around "
8515 "empty body in an %<else%> statement");
8516 add_stmt (build_empty_stmt (loc));
8517 cp_lexer_consume_token (parser->lexer);
8518 }
8519 else
8520 cp_parser_implicitly_scoped_statement (parser, NULL);
8521
8522 finish_else_clause (statement);
8523
8524 /* If we are currently parsing a then-clause, then
8525 IF_P will not be NULL. We set it to true to
8526 indicate that this if statement has an else clause.
8527 This may trigger the Wparentheses warning below
8528 when we get back up to the parent if statement. */
8529 if (if_p != NULL)
8530 *if_p = true;
8531 }
8532 else
8533 {
8534 /* This if statement does not have an else clause. If
8535 NESTED_IF is true, then the then-clause is an if
8536 statement which does have an else clause. We warn
8537 about the potential ambiguity. */
8538 if (nested_if)
8539 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8540 "suggest explicit braces to avoid ambiguous"
8541 " %<else%>");
8542 }
8543
8544 /* Now we're all done with the if-statement. */
8545 finish_if_stmt (statement);
8546 }
8547 else
8548 {
8549 bool in_switch_statement_p;
8550 unsigned char in_statement;
8551
8552 /* Add the condition. */
8553 finish_switch_cond (condition, statement);
8554
8555 /* Parse the body of the switch-statement. */
8556 in_switch_statement_p = parser->in_switch_statement_p;
8557 in_statement = parser->in_statement;
8558 parser->in_switch_statement_p = true;
8559 parser->in_statement |= IN_SWITCH_STMT;
8560 cp_parser_implicitly_scoped_statement (parser, NULL);
8561 parser->in_switch_statement_p = in_switch_statement_p;
8562 parser->in_statement = in_statement;
8563
8564 /* Now we're all done with the switch-statement. */
8565 finish_switch_stmt (statement);
8566 }
8567
8568 return statement;
8569 }
8570 break;
8571
8572 default:
8573 cp_parser_error (parser, "expected selection-statement");
8574 return error_mark_node;
8575 }
8576 }
8577
8578 /* Parse a condition.
8579
8580 condition:
8581 expression
8582 type-specifier-seq declarator = initializer-clause
8583 type-specifier-seq declarator braced-init-list
8584
8585 GNU Extension:
8586
8587 condition:
8588 type-specifier-seq declarator asm-specification [opt]
8589 attributes [opt] = assignment-expression
8590
8591 Returns the expression that should be tested. */
8592
8593 static tree
8594 cp_parser_condition (cp_parser* parser)
8595 {
8596 cp_decl_specifier_seq type_specifiers;
8597 const char *saved_message;
8598 int declares_class_or_enum;
8599
8600 /* Try the declaration first. */
8601 cp_parser_parse_tentatively (parser);
8602 /* New types are not allowed in the type-specifier-seq for a
8603 condition. */
8604 saved_message = parser->type_definition_forbidden_message;
8605 parser->type_definition_forbidden_message
8606 = G_("types may not be defined in conditions");
8607 /* Parse the type-specifier-seq. */
8608 cp_parser_decl_specifier_seq (parser,
8609 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8610 &type_specifiers,
8611 &declares_class_or_enum);
8612 /* Restore the saved message. */
8613 parser->type_definition_forbidden_message = saved_message;
8614 /* If all is well, we might be looking at a declaration. */
8615 if (!cp_parser_error_occurred (parser))
8616 {
8617 tree decl;
8618 tree asm_specification;
8619 tree attributes;
8620 cp_declarator *declarator;
8621 tree initializer = NULL_TREE;
8622
8623 /* Parse the declarator. */
8624 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8625 /*ctor_dtor_or_conv_p=*/NULL,
8626 /*parenthesized_p=*/NULL,
8627 /*member_p=*/false);
8628 /* Parse the attributes. */
8629 attributes = cp_parser_attributes_opt (parser);
8630 /* Parse the asm-specification. */
8631 asm_specification = cp_parser_asm_specification_opt (parser);
8632 /* If the next token is not an `=' or '{', then we might still be
8633 looking at an expression. For example:
8634
8635 if (A(a).x)
8636
8637 looks like a decl-specifier-seq and a declarator -- but then
8638 there is no `=', so this is an expression. */
8639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8640 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8641 cp_parser_simulate_error (parser);
8642
8643 /* If we did see an `=' or '{', then we are looking at a declaration
8644 for sure. */
8645 if (cp_parser_parse_definitely (parser))
8646 {
8647 tree pushed_scope;
8648 bool non_constant_p;
8649 bool flags = LOOKUP_ONLYCONVERTING;
8650
8651 /* Create the declaration. */
8652 decl = start_decl (declarator, &type_specifiers,
8653 /*initialized_p=*/true,
8654 attributes, /*prefix_attributes=*/NULL_TREE,
8655 &pushed_scope);
8656
8657 /* Parse the initializer. */
8658 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8659 {
8660 initializer = cp_parser_braced_list (parser, &non_constant_p);
8661 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8662 flags = 0;
8663 }
8664 else
8665 {
8666 /* Consume the `='. */
8667 cp_parser_require (parser, CPP_EQ, RT_EQ);
8668 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8669 }
8670 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8671 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8672
8673 if (!non_constant_p)
8674 initializer = fold_non_dependent_expr (initializer);
8675
8676 /* Process the initializer. */
8677 cp_finish_decl (decl,
8678 initializer, !non_constant_p,
8679 asm_specification,
8680 flags);
8681
8682 if (pushed_scope)
8683 pop_scope (pushed_scope);
8684
8685 return convert_from_reference (decl);
8686 }
8687 }
8688 /* If we didn't even get past the declarator successfully, we are
8689 definitely not looking at a declaration. */
8690 else
8691 cp_parser_abort_tentative_parse (parser);
8692
8693 /* Otherwise, we are looking at an expression. */
8694 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8695 }
8696
8697 /* Parses a traditional for-statement until the closing ')', not included. */
8698
8699 static tree
8700 cp_parser_c_for (cp_parser *parser)
8701 {
8702 /* Normal for loop */
8703 tree stmt;
8704 tree condition = NULL_TREE;
8705 tree expression = NULL_TREE;
8706
8707 /* Begin the for-statement. */
8708 stmt = begin_for_stmt ();
8709
8710 /* Parse the initialization. */
8711 cp_parser_for_init_statement (parser);
8712 finish_for_init_stmt (stmt);
8713
8714 /* If there's a condition, process it. */
8715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8716 condition = cp_parser_condition (parser);
8717 finish_for_cond (condition, stmt);
8718 /* Look for the `;'. */
8719 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8720
8721 /* If there's an expression, process it. */
8722 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8723 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8724 finish_for_expr (expression, stmt);
8725
8726 return stmt;
8727 }
8728
8729 /* Tries to parse a range-based for-statement:
8730
8731 range-based-for:
8732 type-specifier-seq declarator : expression
8733
8734 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8735 expression. Note that the *DECL is returned unfinished, so
8736 later you should call cp_finish_decl().
8737
8738 Returns TRUE iff a range-based for is parsed. */
8739
8740 static tree
8741 cp_parser_range_for (cp_parser *parser)
8742 {
8743 tree stmt, range_decl, range_expr;
8744 cp_decl_specifier_seq type_specifiers;
8745 cp_declarator *declarator;
8746 const char *saved_message;
8747 tree attributes, pushed_scope;
8748 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8749
8750 parser->colon_corrects_to_scope_p = false;
8751 cp_parser_parse_tentatively (parser);
8752 /* New types are not allowed in the type-specifier-seq for a
8753 range-based for loop. */
8754 saved_message = parser->type_definition_forbidden_message;
8755 parser->type_definition_forbidden_message
8756 = G_("types may not be defined in range-based for loops");
8757 /* Parse the type-specifier-seq. */
8758 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8759 /*is_trailing_return=*/false,
8760 &type_specifiers);
8761 /* Restore the saved message. */
8762 parser->type_definition_forbidden_message = saved_message;
8763 /* If all is well, we might be looking at a declaration. */
8764 if (cp_parser_error_occurred (parser))
8765 {
8766 cp_parser_abort_tentative_parse (parser);
8767 stmt = NULL_TREE;
8768 goto out;
8769 }
8770 /* Parse the declarator. */
8771 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8772 /*ctor_dtor_or_conv_p=*/NULL,
8773 /*parenthesized_p=*/NULL,
8774 /*member_p=*/false);
8775 /* Parse the attributes. */
8776 attributes = cp_parser_attributes_opt (parser);
8777 /* The next token should be `:'. */
8778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8779 cp_parser_simulate_error (parser);
8780
8781 /* Check if it is a range-based for */
8782 if (!cp_parser_parse_definitely (parser))
8783 return NULL_TREE;
8784
8785 cp_parser_require (parser, CPP_COLON, RT_COLON);
8786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8787 {
8788 bool expr_non_constant_p;
8789 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8790 }
8791 else
8792 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8793
8794 /* If in template, STMT is converted to a normal for-statements
8795 at instantiation. If not, it is done just ahead. */
8796 if (processing_template_decl)
8797 stmt = begin_range_for_stmt ();
8798 else
8799 stmt = begin_for_stmt ();
8800
8801 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8802 range_decl = start_decl (declarator, &type_specifiers,
8803 /*initialized_p=*/SD_INITIALIZED,
8804 attributes, /*prefix_attributes=*/NULL_TREE,
8805 &pushed_scope);
8806 /* No scope allowed here */
8807 pop_scope (pushed_scope);
8808
8809 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8810 finish_range_for_decl (stmt, range_decl, range_expr);
8811 else
8812 /* Convert the range-based for loop into a normal for-statement. */
8813 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8814
8815 out:
8816 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8817 return stmt;
8818 }
8819
8820 /* Converts a range-based for-statement into a normal
8821 for-statement, as per the definition.
8822
8823 for (RANGE_DECL : RANGE_EXPR)
8824 BLOCK
8825
8826 should be equivalent to:
8827
8828 {
8829 auto &&__range = RANGE_EXPR;
8830 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8831 __begin != __end;
8832 ++__begin)
8833 {
8834 RANGE_DECL = *__begin;
8835 BLOCK
8836 }
8837 }
8838
8839 If RANGE_EXPR is an array:
8840 BEGIN_EXPR = __range
8841 END_EXPR = __range + ARRAY_SIZE(__range)
8842 Else:
8843 BEGIN_EXPR = begin(__range)
8844 END_EXPR = end(__range);
8845
8846 When calling begin()/end() we must use argument dependent
8847 lookup, but always considering 'std' as an associated namespace. */
8848
8849 tree
8850 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8851 {
8852 tree range_type, range_temp;
8853 tree begin, end;
8854 tree iter_type, begin_expr, end_expr;
8855 tree condition, expression;
8856
8857 /* Find out the type deduced by the declaration
8858 * `auto &&__range = range_expr' */
8859 range_type = cp_build_reference_type (make_auto (), true);
8860 range_type = do_auto_deduction (range_type, range_expr,
8861 type_uses_auto (range_type));
8862
8863 /* Create the __range variable */
8864 range_temp = build_decl (input_location, VAR_DECL,
8865 get_identifier ("__for_range"), range_type);
8866 TREE_USED (range_temp) = 1;
8867 DECL_ARTIFICIAL (range_temp) = 1;
8868 pushdecl (range_temp);
8869 cp_finish_decl (range_temp, range_expr,
8870 /*is_constant_init*/false, NULL_TREE,
8871 LOOKUP_ONLYCONVERTING);
8872
8873 range_temp = convert_from_reference (range_temp);
8874
8875 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8876 {
8877 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8878 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8879 begin_expr = range_temp;
8880 end_expr
8881 = build_binary_op (input_location, PLUS_EXPR,
8882 range_temp,
8883 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8884 }
8885 else
8886 {
8887 /* If it is not an array, we must call begin(__range)/end__range() */
8888 VEC(tree,gc) *vec;
8889
8890 begin_expr = get_identifier ("begin");
8891 vec = make_tree_vector ();
8892 VEC_safe_push (tree, gc, vec, range_temp);
8893 begin_expr = perform_koenig_lookup (begin_expr, vec,
8894 /*include_std=*/true);
8895 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8896 tf_warning_or_error);
8897 release_tree_vector (vec);
8898
8899 end_expr = get_identifier ("end");
8900 vec = make_tree_vector ();
8901 VEC_safe_push (tree, gc, vec, range_temp);
8902 end_expr = perform_koenig_lookup (end_expr, vec,
8903 /*include_std=*/true);
8904 end_expr = finish_call_expr (end_expr, &vec, false, true,
8905 tf_warning_or_error);
8906 release_tree_vector (vec);
8907
8908 /* The unqualified type of the __begin and __end temporaries should
8909 * be the same as required by the multiple auto declaration */
8910 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8911 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8912 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8913 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8914 }
8915
8916 /* The new for initialization statement */
8917 begin = build_decl (input_location, VAR_DECL,
8918 get_identifier ("__for_begin"), iter_type);
8919 TREE_USED (begin) = 1;
8920 DECL_ARTIFICIAL (begin) = 1;
8921 pushdecl (begin);
8922 cp_finish_decl (begin, begin_expr,
8923 /*is_constant_init*/false, NULL_TREE,
8924 LOOKUP_ONLYCONVERTING);
8925
8926 end = build_decl (input_location, VAR_DECL,
8927 get_identifier ("__for_end"), iter_type);
8928 TREE_USED (end) = 1;
8929 DECL_ARTIFICIAL (end) = 1;
8930 pushdecl (end);
8931 cp_finish_decl (end, end_expr,
8932 /*is_constant_init*/false, NULL_TREE,
8933 LOOKUP_ONLYCONVERTING);
8934
8935 finish_for_init_stmt (statement);
8936
8937 /* The new for condition */
8938 condition = build_x_binary_op (NE_EXPR,
8939 begin, ERROR_MARK,
8940 end, ERROR_MARK,
8941 NULL, tf_warning_or_error);
8942 finish_for_cond (condition, statement);
8943
8944 /* The new increment expression */
8945 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8946 finish_for_expr (expression, statement);
8947
8948 /* The declaration is initialized with *__begin inside the loop body */
8949 cp_finish_decl (range_decl,
8950 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8951 /*is_constant_init*/false, NULL_TREE,
8952 LOOKUP_ONLYCONVERTING);
8953
8954 return statement;
8955 }
8956
8957
8958 /* Parse an iteration-statement.
8959
8960 iteration-statement:
8961 while ( condition ) statement
8962 do statement while ( expression ) ;
8963 for ( for-init-statement condition [opt] ; expression [opt] )
8964 statement
8965
8966 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8967
8968 static tree
8969 cp_parser_iteration_statement (cp_parser* parser)
8970 {
8971 cp_token *token;
8972 enum rid keyword;
8973 tree statement;
8974 unsigned char in_statement;
8975
8976 /* Peek at the next token. */
8977 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8978 if (!token)
8979 return error_mark_node;
8980
8981 /* Remember whether or not we are already within an iteration
8982 statement. */
8983 in_statement = parser->in_statement;
8984
8985 /* See what kind of keyword it is. */
8986 keyword = token->keyword;
8987 switch (keyword)
8988 {
8989 case RID_WHILE:
8990 {
8991 tree condition;
8992
8993 /* Begin the while-statement. */
8994 statement = begin_while_stmt ();
8995 /* Look for the `('. */
8996 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8997 /* Parse the condition. */
8998 condition = cp_parser_condition (parser);
8999 finish_while_stmt_cond (condition, statement);
9000 /* Look for the `)'. */
9001 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9002 /* Parse the dependent statement. */
9003 parser->in_statement = IN_ITERATION_STMT;
9004 cp_parser_already_scoped_statement (parser);
9005 parser->in_statement = in_statement;
9006 /* We're done with the while-statement. */
9007 finish_while_stmt (statement);
9008 }
9009 break;
9010
9011 case RID_DO:
9012 {
9013 tree expression;
9014
9015 /* Begin the do-statement. */
9016 statement = begin_do_stmt ();
9017 /* Parse the body of the do-statement. */
9018 parser->in_statement = IN_ITERATION_STMT;
9019 cp_parser_implicitly_scoped_statement (parser, NULL);
9020 parser->in_statement = in_statement;
9021 finish_do_body (statement);
9022 /* Look for the `while' keyword. */
9023 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9024 /* Look for the `('. */
9025 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9026 /* Parse the expression. */
9027 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9028 /* We're done with the do-statement. */
9029 finish_do_stmt (expression, statement);
9030 /* Look for the `)'. */
9031 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9032 /* Look for the `;'. */
9033 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9034 }
9035 break;
9036
9037 case RID_FOR:
9038 {
9039 /* Look for the `('. */
9040 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9041
9042 if (cxx_dialect == cxx0x)
9043 statement = cp_parser_range_for (parser);
9044 else
9045 statement = NULL_TREE;
9046 if (statement == NULL_TREE)
9047 statement = cp_parser_c_for (parser);
9048
9049 /* Look for the `)'. */
9050 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9051
9052 /* Parse the body of the for-statement. */
9053 parser->in_statement = IN_ITERATION_STMT;
9054 cp_parser_already_scoped_statement (parser);
9055 parser->in_statement = in_statement;
9056
9057 /* We're done with the for-statement. */
9058 finish_for_stmt (statement);
9059 }
9060 break;
9061
9062 default:
9063 cp_parser_error (parser, "expected iteration-statement");
9064 statement = error_mark_node;
9065 break;
9066 }
9067
9068 return statement;
9069 }
9070
9071 /* Parse a for-init-statement.
9072
9073 for-init-statement:
9074 expression-statement
9075 simple-declaration */
9076
9077 static void
9078 cp_parser_for_init_statement (cp_parser* parser)
9079 {
9080 /* If the next token is a `;', then we have an empty
9081 expression-statement. Grammatically, this is also a
9082 simple-declaration, but an invalid one, because it does not
9083 declare anything. Therefore, if we did not handle this case
9084 specially, we would issue an error message about an invalid
9085 declaration. */
9086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9087 {
9088 /* We're going to speculatively look for a declaration, falling back
9089 to an expression, if necessary. */
9090 cp_parser_parse_tentatively (parser);
9091 /* Parse the declaration. */
9092 cp_parser_simple_declaration (parser,
9093 /*function_definition_allowed_p=*/false);
9094 /* If the tentative parse failed, then we shall need to look for an
9095 expression-statement. */
9096 if (cp_parser_parse_definitely (parser))
9097 return;
9098 }
9099
9100 cp_parser_expression_statement (parser, NULL_TREE);
9101 }
9102
9103 /* Parse a jump-statement.
9104
9105 jump-statement:
9106 break ;
9107 continue ;
9108 return expression [opt] ;
9109 return braced-init-list ;
9110 goto identifier ;
9111
9112 GNU extension:
9113
9114 jump-statement:
9115 goto * expression ;
9116
9117 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9118
9119 static tree
9120 cp_parser_jump_statement (cp_parser* parser)
9121 {
9122 tree statement = error_mark_node;
9123 cp_token *token;
9124 enum rid keyword;
9125 unsigned char in_statement;
9126
9127 /* Peek at the next token. */
9128 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9129 if (!token)
9130 return error_mark_node;
9131
9132 /* See what kind of keyword it is. */
9133 keyword = token->keyword;
9134 switch (keyword)
9135 {
9136 case RID_BREAK:
9137 in_statement = parser->in_statement & ~IN_IF_STMT;
9138 switch (in_statement)
9139 {
9140 case 0:
9141 error_at (token->location, "break statement not within loop or switch");
9142 break;
9143 default:
9144 gcc_assert ((in_statement & IN_SWITCH_STMT)
9145 || in_statement == IN_ITERATION_STMT);
9146 statement = finish_break_stmt ();
9147 break;
9148 case IN_OMP_BLOCK:
9149 error_at (token->location, "invalid exit from OpenMP structured block");
9150 break;
9151 case IN_OMP_FOR:
9152 error_at (token->location, "break statement used with OpenMP for loop");
9153 break;
9154 }
9155 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9156 break;
9157
9158 case RID_CONTINUE:
9159 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9160 {
9161 case 0:
9162 error_at (token->location, "continue statement not within a loop");
9163 break;
9164 case IN_ITERATION_STMT:
9165 case IN_OMP_FOR:
9166 statement = finish_continue_stmt ();
9167 break;
9168 case IN_OMP_BLOCK:
9169 error_at (token->location, "invalid exit from OpenMP structured block");
9170 break;
9171 default:
9172 gcc_unreachable ();
9173 }
9174 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9175 break;
9176
9177 case RID_RETURN:
9178 {
9179 tree expr;
9180 bool expr_non_constant_p;
9181
9182 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9183 {
9184 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9185 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9186 }
9187 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9188 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9189 else
9190 /* If the next token is a `;', then there is no
9191 expression. */
9192 expr = NULL_TREE;
9193 /* Build the return-statement. */
9194 statement = finish_return_stmt (expr);
9195 /* Look for the final `;'. */
9196 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9197 }
9198 break;
9199
9200 case RID_GOTO:
9201 /* Create the goto-statement. */
9202 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9203 {
9204 /* Issue a warning about this use of a GNU extension. */
9205 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9206 /* Consume the '*' token. */
9207 cp_lexer_consume_token (parser->lexer);
9208 /* Parse the dependent expression. */
9209 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9210 }
9211 else
9212 finish_goto_stmt (cp_parser_identifier (parser));
9213 /* Look for the final `;'. */
9214 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9215 break;
9216
9217 default:
9218 cp_parser_error (parser, "expected jump-statement");
9219 break;
9220 }
9221
9222 return statement;
9223 }
9224
9225 /* Parse a declaration-statement.
9226
9227 declaration-statement:
9228 block-declaration */
9229
9230 static void
9231 cp_parser_declaration_statement (cp_parser* parser)
9232 {
9233 void *p;
9234
9235 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9236 p = obstack_alloc (&declarator_obstack, 0);
9237
9238 /* Parse the block-declaration. */
9239 cp_parser_block_declaration (parser, /*statement_p=*/true);
9240
9241 /* Free any declarators allocated. */
9242 obstack_free (&declarator_obstack, p);
9243
9244 /* Finish off the statement. */
9245 finish_stmt ();
9246 }
9247
9248 /* Some dependent statements (like `if (cond) statement'), are
9249 implicitly in their own scope. In other words, if the statement is
9250 a single statement (as opposed to a compound-statement), it is
9251 none-the-less treated as if it were enclosed in braces. Any
9252 declarations appearing in the dependent statement are out of scope
9253 after control passes that point. This function parses a statement,
9254 but ensures that is in its own scope, even if it is not a
9255 compound-statement.
9256
9257 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9258 is a (possibly labeled) if statement which is not enclosed in
9259 braces and has an else clause. This is used to implement
9260 -Wparentheses.
9261
9262 Returns the new statement. */
9263
9264 static tree
9265 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9266 {
9267 tree statement;
9268
9269 if (if_p != NULL)
9270 *if_p = false;
9271
9272 /* Mark if () ; with a special NOP_EXPR. */
9273 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9274 {
9275 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9276 cp_lexer_consume_token (parser->lexer);
9277 statement = add_stmt (build_empty_stmt (loc));
9278 }
9279 /* if a compound is opened, we simply parse the statement directly. */
9280 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9281 statement = cp_parser_compound_statement (parser, NULL, false);
9282 /* If the token is not a `{', then we must take special action. */
9283 else
9284 {
9285 /* Create a compound-statement. */
9286 statement = begin_compound_stmt (0);
9287 /* Parse the dependent-statement. */
9288 cp_parser_statement (parser, NULL_TREE, false, if_p);
9289 /* Finish the dummy compound-statement. */
9290 finish_compound_stmt (statement);
9291 }
9292
9293 /* Return the statement. */
9294 return statement;
9295 }
9296
9297 /* For some dependent statements (like `while (cond) statement'), we
9298 have already created a scope. Therefore, even if the dependent
9299 statement is a compound-statement, we do not want to create another
9300 scope. */
9301
9302 static void
9303 cp_parser_already_scoped_statement (cp_parser* parser)
9304 {
9305 /* If the token is a `{', then we must take special action. */
9306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9307 cp_parser_statement (parser, NULL_TREE, false, NULL);
9308 else
9309 {
9310 /* Avoid calling cp_parser_compound_statement, so that we
9311 don't create a new scope. Do everything else by hand. */
9312 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9313 /* If the next keyword is `__label__' we have a label declaration. */
9314 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9315 cp_parser_label_declaration (parser);
9316 /* Parse an (optional) statement-seq. */
9317 cp_parser_statement_seq_opt (parser, NULL_TREE);
9318 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9319 }
9320 }
9321
9322 /* Declarations [gram.dcl.dcl] */
9323
9324 /* Parse an optional declaration-sequence.
9325
9326 declaration-seq:
9327 declaration
9328 declaration-seq declaration */
9329
9330 static void
9331 cp_parser_declaration_seq_opt (cp_parser* parser)
9332 {
9333 while (true)
9334 {
9335 cp_token *token;
9336
9337 token = cp_lexer_peek_token (parser->lexer);
9338
9339 if (token->type == CPP_CLOSE_BRACE
9340 || token->type == CPP_EOF
9341 || token->type == CPP_PRAGMA_EOL)
9342 break;
9343
9344 if (token->type == CPP_SEMICOLON)
9345 {
9346 /* A declaration consisting of a single semicolon is
9347 invalid. Allow it unless we're being pedantic. */
9348 cp_lexer_consume_token (parser->lexer);
9349 if (!in_system_header)
9350 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9351 continue;
9352 }
9353
9354 /* If we're entering or exiting a region that's implicitly
9355 extern "C", modify the lang context appropriately. */
9356 if (!parser->implicit_extern_c && token->implicit_extern_c)
9357 {
9358 push_lang_context (lang_name_c);
9359 parser->implicit_extern_c = true;
9360 }
9361 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9362 {
9363 pop_lang_context ();
9364 parser->implicit_extern_c = false;
9365 }
9366
9367 if (token->type == CPP_PRAGMA)
9368 {
9369 /* A top-level declaration can consist solely of a #pragma.
9370 A nested declaration cannot, so this is done here and not
9371 in cp_parser_declaration. (A #pragma at block scope is
9372 handled in cp_parser_statement.) */
9373 cp_parser_pragma (parser, pragma_external);
9374 continue;
9375 }
9376
9377 /* Parse the declaration itself. */
9378 cp_parser_declaration (parser);
9379 }
9380 }
9381
9382 /* Parse a declaration.
9383
9384 declaration:
9385 block-declaration
9386 function-definition
9387 template-declaration
9388 explicit-instantiation
9389 explicit-specialization
9390 linkage-specification
9391 namespace-definition
9392
9393 GNU extension:
9394
9395 declaration:
9396 __extension__ declaration */
9397
9398 static void
9399 cp_parser_declaration (cp_parser* parser)
9400 {
9401 cp_token token1;
9402 cp_token token2;
9403 int saved_pedantic;
9404 void *p;
9405 tree attributes = NULL_TREE;
9406
9407 /* Check for the `__extension__' keyword. */
9408 if (cp_parser_extension_opt (parser, &saved_pedantic))
9409 {
9410 /* Parse the qualified declaration. */
9411 cp_parser_declaration (parser);
9412 /* Restore the PEDANTIC flag. */
9413 pedantic = saved_pedantic;
9414
9415 return;
9416 }
9417
9418 /* Try to figure out what kind of declaration is present. */
9419 token1 = *cp_lexer_peek_token (parser->lexer);
9420
9421 if (token1.type != CPP_EOF)
9422 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9423 else
9424 {
9425 token2.type = CPP_EOF;
9426 token2.keyword = RID_MAX;
9427 }
9428
9429 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9430 p = obstack_alloc (&declarator_obstack, 0);
9431
9432 /* If the next token is `extern' and the following token is a string
9433 literal, then we have a linkage specification. */
9434 if (token1.keyword == RID_EXTERN
9435 && cp_parser_is_string_literal (&token2))
9436 cp_parser_linkage_specification (parser);
9437 /* If the next token is `template', then we have either a template
9438 declaration, an explicit instantiation, or an explicit
9439 specialization. */
9440 else if (token1.keyword == RID_TEMPLATE)
9441 {
9442 /* `template <>' indicates a template specialization. */
9443 if (token2.type == CPP_LESS
9444 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9445 cp_parser_explicit_specialization (parser);
9446 /* `template <' indicates a template declaration. */
9447 else if (token2.type == CPP_LESS)
9448 cp_parser_template_declaration (parser, /*member_p=*/false);
9449 /* Anything else must be an explicit instantiation. */
9450 else
9451 cp_parser_explicit_instantiation (parser);
9452 }
9453 /* If the next token is `export', then we have a template
9454 declaration. */
9455 else if (token1.keyword == RID_EXPORT)
9456 cp_parser_template_declaration (parser, /*member_p=*/false);
9457 /* If the next token is `extern', 'static' or 'inline' and the one
9458 after that is `template', we have a GNU extended explicit
9459 instantiation directive. */
9460 else if (cp_parser_allow_gnu_extensions_p (parser)
9461 && (token1.keyword == RID_EXTERN
9462 || token1.keyword == RID_STATIC
9463 || token1.keyword == RID_INLINE)
9464 && token2.keyword == RID_TEMPLATE)
9465 cp_parser_explicit_instantiation (parser);
9466 /* If the next token is `namespace', check for a named or unnamed
9467 namespace definition. */
9468 else if (token1.keyword == RID_NAMESPACE
9469 && (/* A named namespace definition. */
9470 (token2.type == CPP_NAME
9471 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9472 != CPP_EQ))
9473 /* An unnamed namespace definition. */
9474 || token2.type == CPP_OPEN_BRACE
9475 || token2.keyword == RID_ATTRIBUTE))
9476 cp_parser_namespace_definition (parser);
9477 /* An inline (associated) namespace definition. */
9478 else if (token1.keyword == RID_INLINE
9479 && token2.keyword == RID_NAMESPACE)
9480 cp_parser_namespace_definition (parser);
9481 /* Objective-C++ declaration/definition. */
9482 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9483 cp_parser_objc_declaration (parser, NULL_TREE);
9484 else if (c_dialect_objc ()
9485 && token1.keyword == RID_ATTRIBUTE
9486 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9487 cp_parser_objc_declaration (parser, attributes);
9488 /* We must have either a block declaration or a function
9489 definition. */
9490 else
9491 /* Try to parse a block-declaration, or a function-definition. */
9492 cp_parser_block_declaration (parser, /*statement_p=*/false);
9493
9494 /* Free any declarators allocated. */
9495 obstack_free (&declarator_obstack, p);
9496 }
9497
9498 /* Parse a block-declaration.
9499
9500 block-declaration:
9501 simple-declaration
9502 asm-definition
9503 namespace-alias-definition
9504 using-declaration
9505 using-directive
9506
9507 GNU Extension:
9508
9509 block-declaration:
9510 __extension__ block-declaration
9511
9512 C++0x Extension:
9513
9514 block-declaration:
9515 static_assert-declaration
9516
9517 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9518 part of a declaration-statement. */
9519
9520 static void
9521 cp_parser_block_declaration (cp_parser *parser,
9522 bool statement_p)
9523 {
9524 cp_token *token1;
9525 int saved_pedantic;
9526
9527 /* Check for the `__extension__' keyword. */
9528 if (cp_parser_extension_opt (parser, &saved_pedantic))
9529 {
9530 /* Parse the qualified declaration. */
9531 cp_parser_block_declaration (parser, statement_p);
9532 /* Restore the PEDANTIC flag. */
9533 pedantic = saved_pedantic;
9534
9535 return;
9536 }
9537
9538 /* Peek at the next token to figure out which kind of declaration is
9539 present. */
9540 token1 = cp_lexer_peek_token (parser->lexer);
9541
9542 /* If the next keyword is `asm', we have an asm-definition. */
9543 if (token1->keyword == RID_ASM)
9544 {
9545 if (statement_p)
9546 cp_parser_commit_to_tentative_parse (parser);
9547 cp_parser_asm_definition (parser);
9548 }
9549 /* If the next keyword is `namespace', we have a
9550 namespace-alias-definition. */
9551 else if (token1->keyword == RID_NAMESPACE)
9552 cp_parser_namespace_alias_definition (parser);
9553 /* If the next keyword is `using', we have either a
9554 using-declaration or a using-directive. */
9555 else if (token1->keyword == RID_USING)
9556 {
9557 cp_token *token2;
9558
9559 if (statement_p)
9560 cp_parser_commit_to_tentative_parse (parser);
9561 /* If the token after `using' is `namespace', then we have a
9562 using-directive. */
9563 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9564 if (token2->keyword == RID_NAMESPACE)
9565 cp_parser_using_directive (parser);
9566 /* Otherwise, it's a using-declaration. */
9567 else
9568 cp_parser_using_declaration (parser,
9569 /*access_declaration_p=*/false);
9570 }
9571 /* If the next keyword is `__label__' we have a misplaced label
9572 declaration. */
9573 else if (token1->keyword == RID_LABEL)
9574 {
9575 cp_lexer_consume_token (parser->lexer);
9576 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9577 cp_parser_skip_to_end_of_statement (parser);
9578 /* If the next token is now a `;', consume it. */
9579 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9580 cp_lexer_consume_token (parser->lexer);
9581 }
9582 /* If the next token is `static_assert' we have a static assertion. */
9583 else if (token1->keyword == RID_STATIC_ASSERT)
9584 cp_parser_static_assert (parser, /*member_p=*/false);
9585 /* Anything else must be a simple-declaration. */
9586 else
9587 cp_parser_simple_declaration (parser, !statement_p);
9588 }
9589
9590 /* Parse a simple-declaration.
9591
9592 simple-declaration:
9593 decl-specifier-seq [opt] init-declarator-list [opt] ;
9594
9595 init-declarator-list:
9596 init-declarator
9597 init-declarator-list , init-declarator
9598
9599 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9600 function-definition as a simple-declaration. */
9601
9602 static void
9603 cp_parser_simple_declaration (cp_parser* parser,
9604 bool function_definition_allowed_p)
9605 {
9606 cp_decl_specifier_seq decl_specifiers;
9607 int declares_class_or_enum;
9608 bool saw_declarator;
9609
9610 /* Defer access checks until we know what is being declared; the
9611 checks for names appearing in the decl-specifier-seq should be
9612 done as if we were in the scope of the thing being declared. */
9613 push_deferring_access_checks (dk_deferred);
9614
9615 /* Parse the decl-specifier-seq. We have to keep track of whether
9616 or not the decl-specifier-seq declares a named class or
9617 enumeration type, since that is the only case in which the
9618 init-declarator-list is allowed to be empty.
9619
9620 [dcl.dcl]
9621
9622 In a simple-declaration, the optional init-declarator-list can be
9623 omitted only when declaring a class or enumeration, that is when
9624 the decl-specifier-seq contains either a class-specifier, an
9625 elaborated-type-specifier, or an enum-specifier. */
9626 cp_parser_decl_specifier_seq (parser,
9627 CP_PARSER_FLAGS_OPTIONAL,
9628 &decl_specifiers,
9629 &declares_class_or_enum);
9630 /* We no longer need to defer access checks. */
9631 stop_deferring_access_checks ();
9632
9633 /* In a block scope, a valid declaration must always have a
9634 decl-specifier-seq. By not trying to parse declarators, we can
9635 resolve the declaration/expression ambiguity more quickly. */
9636 if (!function_definition_allowed_p
9637 && !decl_specifiers.any_specifiers_p)
9638 {
9639 cp_parser_error (parser, "expected declaration");
9640 goto done;
9641 }
9642
9643 /* If the next two tokens are both identifiers, the code is
9644 erroneous. The usual cause of this situation is code like:
9645
9646 T t;
9647
9648 where "T" should name a type -- but does not. */
9649 if (!decl_specifiers.any_type_specifiers_p
9650 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9651 {
9652 /* If parsing tentatively, we should commit; we really are
9653 looking at a declaration. */
9654 cp_parser_commit_to_tentative_parse (parser);
9655 /* Give up. */
9656 goto done;
9657 }
9658
9659 /* If we have seen at least one decl-specifier, and the next token
9660 is not a parenthesis, then we must be looking at a declaration.
9661 (After "int (" we might be looking at a functional cast.) */
9662 if (decl_specifiers.any_specifiers_p
9663 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9664 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9665 && !cp_parser_error_occurred (parser))
9666 cp_parser_commit_to_tentative_parse (parser);
9667
9668 /* Keep going until we hit the `;' at the end of the simple
9669 declaration. */
9670 saw_declarator = false;
9671 while (cp_lexer_next_token_is_not (parser->lexer,
9672 CPP_SEMICOLON))
9673 {
9674 cp_token *token;
9675 bool function_definition_p;
9676 tree decl;
9677
9678 if (saw_declarator)
9679 {
9680 /* If we are processing next declarator, coma is expected */
9681 token = cp_lexer_peek_token (parser->lexer);
9682 gcc_assert (token->type == CPP_COMMA);
9683 cp_lexer_consume_token (parser->lexer);
9684 }
9685 else
9686 saw_declarator = true;
9687
9688 /* Parse the init-declarator. */
9689 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9690 /*checks=*/NULL,
9691 function_definition_allowed_p,
9692 /*member_p=*/false,
9693 declares_class_or_enum,
9694 &function_definition_p);
9695 /* If an error occurred while parsing tentatively, exit quickly.
9696 (That usually happens when in the body of a function; each
9697 statement is treated as a declaration-statement until proven
9698 otherwise.) */
9699 if (cp_parser_error_occurred (parser))
9700 goto done;
9701 /* Handle function definitions specially. */
9702 if (function_definition_p)
9703 {
9704 /* If the next token is a `,', then we are probably
9705 processing something like:
9706
9707 void f() {}, *p;
9708
9709 which is erroneous. */
9710 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9711 {
9712 cp_token *token = cp_lexer_peek_token (parser->lexer);
9713 error_at (token->location,
9714 "mixing"
9715 " declarations and function-definitions is forbidden");
9716 }
9717 /* Otherwise, we're done with the list of declarators. */
9718 else
9719 {
9720 pop_deferring_access_checks ();
9721 return;
9722 }
9723 }
9724 /* The next token should be either a `,' or a `;'. */
9725 token = cp_lexer_peek_token (parser->lexer);
9726 /* If it's a `,', there are more declarators to come. */
9727 if (token->type == CPP_COMMA)
9728 /* will be consumed next time around */;
9729 /* If it's a `;', we are done. */
9730 else if (token->type == CPP_SEMICOLON)
9731 break;
9732 /* Anything else is an error. */
9733 else
9734 {
9735 /* If we have already issued an error message we don't need
9736 to issue another one. */
9737 if (decl != error_mark_node
9738 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9739 cp_parser_error (parser, "expected %<,%> or %<;%>");
9740 /* Skip tokens until we reach the end of the statement. */
9741 cp_parser_skip_to_end_of_statement (parser);
9742 /* If the next token is now a `;', consume it. */
9743 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9744 cp_lexer_consume_token (parser->lexer);
9745 goto done;
9746 }
9747 /* After the first time around, a function-definition is not
9748 allowed -- even if it was OK at first. For example:
9749
9750 int i, f() {}
9751
9752 is not valid. */
9753 function_definition_allowed_p = false;
9754 }
9755
9756 /* Issue an error message if no declarators are present, and the
9757 decl-specifier-seq does not itself declare a class or
9758 enumeration. */
9759 if (!saw_declarator)
9760 {
9761 if (cp_parser_declares_only_class_p (parser))
9762 shadow_tag (&decl_specifiers);
9763 /* Perform any deferred access checks. */
9764 perform_deferred_access_checks ();
9765 }
9766
9767 /* Consume the `;'. */
9768 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9769
9770 done:
9771 pop_deferring_access_checks ();
9772 }
9773
9774 /* Parse a decl-specifier-seq.
9775
9776 decl-specifier-seq:
9777 decl-specifier-seq [opt] decl-specifier
9778
9779 decl-specifier:
9780 storage-class-specifier
9781 type-specifier
9782 function-specifier
9783 friend
9784 typedef
9785
9786 GNU Extension:
9787
9788 decl-specifier:
9789 attributes
9790
9791 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9792
9793 The parser flags FLAGS is used to control type-specifier parsing.
9794
9795 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9796 flags:
9797
9798 1: one of the decl-specifiers is an elaborated-type-specifier
9799 (i.e., a type declaration)
9800 2: one of the decl-specifiers is an enum-specifier or a
9801 class-specifier (i.e., a type definition)
9802
9803 */
9804
9805 static void
9806 cp_parser_decl_specifier_seq (cp_parser* parser,
9807 cp_parser_flags flags,
9808 cp_decl_specifier_seq *decl_specs,
9809 int* declares_class_or_enum)
9810 {
9811 bool constructor_possible_p = !parser->in_declarator_p;
9812 cp_token *start_token = NULL;
9813
9814 /* Clear DECL_SPECS. */
9815 clear_decl_specs (decl_specs);
9816
9817 /* Assume no class or enumeration type is declared. */
9818 *declares_class_or_enum = 0;
9819
9820 /* Keep reading specifiers until there are no more to read. */
9821 while (true)
9822 {
9823 bool constructor_p;
9824 bool found_decl_spec;
9825 cp_token *token;
9826
9827 /* Peek at the next token. */
9828 token = cp_lexer_peek_token (parser->lexer);
9829
9830 /* Save the first token of the decl spec list for error
9831 reporting. */
9832 if (!start_token)
9833 start_token = token;
9834 /* Handle attributes. */
9835 if (token->keyword == RID_ATTRIBUTE)
9836 {
9837 /* Parse the attributes. */
9838 decl_specs->attributes
9839 = chainon (decl_specs->attributes,
9840 cp_parser_attributes_opt (parser));
9841 continue;
9842 }
9843 /* Assume we will find a decl-specifier keyword. */
9844 found_decl_spec = true;
9845 /* If the next token is an appropriate keyword, we can simply
9846 add it to the list. */
9847 switch (token->keyword)
9848 {
9849 /* decl-specifier:
9850 friend
9851 constexpr */
9852 case RID_FRIEND:
9853 if (!at_class_scope_p ())
9854 {
9855 error_at (token->location, "%<friend%> used outside of class");
9856 cp_lexer_purge_token (parser->lexer);
9857 }
9858 else
9859 {
9860 ++decl_specs->specs[(int) ds_friend];
9861 /* Consume the token. */
9862 cp_lexer_consume_token (parser->lexer);
9863 }
9864 break;
9865
9866 case RID_CONSTEXPR:
9867 ++decl_specs->specs[(int) ds_constexpr];
9868 cp_lexer_consume_token (parser->lexer);
9869 break;
9870
9871 /* function-specifier:
9872 inline
9873 virtual
9874 explicit */
9875 case RID_INLINE:
9876 case RID_VIRTUAL:
9877 case RID_EXPLICIT:
9878 cp_parser_function_specifier_opt (parser, decl_specs);
9879 break;
9880
9881 /* decl-specifier:
9882 typedef */
9883 case RID_TYPEDEF:
9884 ++decl_specs->specs[(int) ds_typedef];
9885 /* Consume the token. */
9886 cp_lexer_consume_token (parser->lexer);
9887 /* A constructor declarator cannot appear in a typedef. */
9888 constructor_possible_p = false;
9889 /* The "typedef" keyword can only occur in a declaration; we
9890 may as well commit at this point. */
9891 cp_parser_commit_to_tentative_parse (parser);
9892
9893 if (decl_specs->storage_class != sc_none)
9894 decl_specs->conflicting_specifiers_p = true;
9895 break;
9896
9897 /* storage-class-specifier:
9898 auto
9899 register
9900 static
9901 extern
9902 mutable
9903
9904 GNU Extension:
9905 thread */
9906 case RID_AUTO:
9907 if (cxx_dialect == cxx98)
9908 {
9909 /* Consume the token. */
9910 cp_lexer_consume_token (parser->lexer);
9911
9912 /* Complain about `auto' as a storage specifier, if
9913 we're complaining about C++0x compatibility. */
9914 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9915 " will change meaning in C++0x; please remove it");
9916
9917 /* Set the storage class anyway. */
9918 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9919 token->location);
9920 }
9921 else
9922 /* C++0x auto type-specifier. */
9923 found_decl_spec = false;
9924 break;
9925
9926 case RID_REGISTER:
9927 case RID_STATIC:
9928 case RID_EXTERN:
9929 case RID_MUTABLE:
9930 /* Consume the token. */
9931 cp_lexer_consume_token (parser->lexer);
9932 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9933 token->location);
9934 break;
9935 case RID_THREAD:
9936 /* Consume the token. */
9937 cp_lexer_consume_token (parser->lexer);
9938 ++decl_specs->specs[(int) ds_thread];
9939 break;
9940
9941 default:
9942 /* We did not yet find a decl-specifier yet. */
9943 found_decl_spec = false;
9944 break;
9945 }
9946
9947 if (found_decl_spec
9948 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9949 && token->keyword != RID_CONSTEXPR)
9950 error ("decl-specifier invalid in condition");
9951
9952 /* Constructors are a special case. The `S' in `S()' is not a
9953 decl-specifier; it is the beginning of the declarator. */
9954 constructor_p
9955 = (!found_decl_spec
9956 && constructor_possible_p
9957 && (cp_parser_constructor_declarator_p
9958 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9959
9960 /* If we don't have a DECL_SPEC yet, then we must be looking at
9961 a type-specifier. */
9962 if (!found_decl_spec && !constructor_p)
9963 {
9964 int decl_spec_declares_class_or_enum;
9965 bool is_cv_qualifier;
9966 tree type_spec;
9967
9968 type_spec
9969 = cp_parser_type_specifier (parser, flags,
9970 decl_specs,
9971 /*is_declaration=*/true,
9972 &decl_spec_declares_class_or_enum,
9973 &is_cv_qualifier);
9974 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9975
9976 /* If this type-specifier referenced a user-defined type
9977 (a typedef, class-name, etc.), then we can't allow any
9978 more such type-specifiers henceforth.
9979
9980 [dcl.spec]
9981
9982 The longest sequence of decl-specifiers that could
9983 possibly be a type name is taken as the
9984 decl-specifier-seq of a declaration. The sequence shall
9985 be self-consistent as described below.
9986
9987 [dcl.type]
9988
9989 As a general rule, at most one type-specifier is allowed
9990 in the complete decl-specifier-seq of a declaration. The
9991 only exceptions are the following:
9992
9993 -- const or volatile can be combined with any other
9994 type-specifier.
9995
9996 -- signed or unsigned can be combined with char, long,
9997 short, or int.
9998
9999 -- ..
10000
10001 Example:
10002
10003 typedef char* Pc;
10004 void g (const int Pc);
10005
10006 Here, Pc is *not* part of the decl-specifier seq; it's
10007 the declarator. Therefore, once we see a type-specifier
10008 (other than a cv-qualifier), we forbid any additional
10009 user-defined types. We *do* still allow things like `int
10010 int' to be considered a decl-specifier-seq, and issue the
10011 error message later. */
10012 if (type_spec && !is_cv_qualifier)
10013 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10014 /* A constructor declarator cannot follow a type-specifier. */
10015 if (type_spec)
10016 {
10017 constructor_possible_p = false;
10018 found_decl_spec = true;
10019 if (!is_cv_qualifier)
10020 decl_specs->any_type_specifiers_p = true;
10021 }
10022 }
10023
10024 /* If we still do not have a DECL_SPEC, then there are no more
10025 decl-specifiers. */
10026 if (!found_decl_spec)
10027 break;
10028
10029 decl_specs->any_specifiers_p = true;
10030 /* After we see one decl-specifier, further decl-specifiers are
10031 always optional. */
10032 flags |= CP_PARSER_FLAGS_OPTIONAL;
10033 }
10034
10035 cp_parser_check_decl_spec (decl_specs, start_token->location);
10036
10037 /* Don't allow a friend specifier with a class definition. */
10038 if (decl_specs->specs[(int) ds_friend] != 0
10039 && (*declares_class_or_enum & 2))
10040 error_at (start_token->location,
10041 "class definition may not be declared a friend");
10042 }
10043
10044 /* Parse an (optional) storage-class-specifier.
10045
10046 storage-class-specifier:
10047 auto
10048 register
10049 static
10050 extern
10051 mutable
10052
10053 GNU Extension:
10054
10055 storage-class-specifier:
10056 thread
10057
10058 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10059
10060 static tree
10061 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10062 {
10063 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10064 {
10065 case RID_AUTO:
10066 if (cxx_dialect != cxx98)
10067 return NULL_TREE;
10068 /* Fall through for C++98. */
10069
10070 case RID_REGISTER:
10071 case RID_STATIC:
10072 case RID_EXTERN:
10073 case RID_MUTABLE:
10074 case RID_THREAD:
10075 /* Consume the token. */
10076 return cp_lexer_consume_token (parser->lexer)->u.value;
10077
10078 default:
10079 return NULL_TREE;
10080 }
10081 }
10082
10083 /* Parse an (optional) function-specifier.
10084
10085 function-specifier:
10086 inline
10087 virtual
10088 explicit
10089
10090 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10091 Updates DECL_SPECS, if it is non-NULL. */
10092
10093 static tree
10094 cp_parser_function_specifier_opt (cp_parser* parser,
10095 cp_decl_specifier_seq *decl_specs)
10096 {
10097 cp_token *token = cp_lexer_peek_token (parser->lexer);
10098 switch (token->keyword)
10099 {
10100 case RID_INLINE:
10101 if (decl_specs)
10102 ++decl_specs->specs[(int) ds_inline];
10103 break;
10104
10105 case RID_VIRTUAL:
10106 /* 14.5.2.3 [temp.mem]
10107
10108 A member function template shall not be virtual. */
10109 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10110 error_at (token->location, "templates may not be %<virtual%>");
10111 else if (decl_specs)
10112 ++decl_specs->specs[(int) ds_virtual];
10113 break;
10114
10115 case RID_EXPLICIT:
10116 if (decl_specs)
10117 ++decl_specs->specs[(int) ds_explicit];
10118 break;
10119
10120 default:
10121 return NULL_TREE;
10122 }
10123
10124 /* Consume the token. */
10125 return cp_lexer_consume_token (parser->lexer)->u.value;
10126 }
10127
10128 /* Parse a linkage-specification.
10129
10130 linkage-specification:
10131 extern string-literal { declaration-seq [opt] }
10132 extern string-literal declaration */
10133
10134 static void
10135 cp_parser_linkage_specification (cp_parser* parser)
10136 {
10137 tree linkage;
10138
10139 /* Look for the `extern' keyword. */
10140 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10141
10142 /* Look for the string-literal. */
10143 linkage = cp_parser_string_literal (parser, false, false);
10144
10145 /* Transform the literal into an identifier. If the literal is a
10146 wide-character string, or contains embedded NULs, then we can't
10147 handle it as the user wants. */
10148 if (strlen (TREE_STRING_POINTER (linkage))
10149 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10150 {
10151 cp_parser_error (parser, "invalid linkage-specification");
10152 /* Assume C++ linkage. */
10153 linkage = lang_name_cplusplus;
10154 }
10155 else
10156 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10157
10158 /* We're now using the new linkage. */
10159 push_lang_context (linkage);
10160
10161 /* If the next token is a `{', then we're using the first
10162 production. */
10163 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10164 {
10165 /* Consume the `{' token. */
10166 cp_lexer_consume_token (parser->lexer);
10167 /* Parse the declarations. */
10168 cp_parser_declaration_seq_opt (parser);
10169 /* Look for the closing `}'. */
10170 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10171 }
10172 /* Otherwise, there's just one declaration. */
10173 else
10174 {
10175 bool saved_in_unbraced_linkage_specification_p;
10176
10177 saved_in_unbraced_linkage_specification_p
10178 = parser->in_unbraced_linkage_specification_p;
10179 parser->in_unbraced_linkage_specification_p = true;
10180 cp_parser_declaration (parser);
10181 parser->in_unbraced_linkage_specification_p
10182 = saved_in_unbraced_linkage_specification_p;
10183 }
10184
10185 /* We're done with the linkage-specification. */
10186 pop_lang_context ();
10187 }
10188
10189 /* Parse a static_assert-declaration.
10190
10191 static_assert-declaration:
10192 static_assert ( constant-expression , string-literal ) ;
10193
10194 If MEMBER_P, this static_assert is a class member. */
10195
10196 static void
10197 cp_parser_static_assert(cp_parser *parser, bool member_p)
10198 {
10199 tree condition;
10200 tree message;
10201 cp_token *token;
10202 location_t saved_loc;
10203
10204 /* Peek at the `static_assert' token so we can keep track of exactly
10205 where the static assertion started. */
10206 token = cp_lexer_peek_token (parser->lexer);
10207 saved_loc = token->location;
10208
10209 /* Look for the `static_assert' keyword. */
10210 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10211 RT_STATIC_ASSERT))
10212 return;
10213
10214 /* We know we are in a static assertion; commit to any tentative
10215 parse. */
10216 if (cp_parser_parsing_tentatively (parser))
10217 cp_parser_commit_to_tentative_parse (parser);
10218
10219 /* Parse the `(' starting the static assertion condition. */
10220 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10221
10222 /* Parse the constant-expression. */
10223 condition =
10224 cp_parser_constant_expression (parser,
10225 /*allow_non_constant_p=*/false,
10226 /*non_constant_p=*/NULL);
10227
10228 /* Parse the separating `,'. */
10229 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10230
10231 /* Parse the string-literal message. */
10232 message = cp_parser_string_literal (parser,
10233 /*translate=*/false,
10234 /*wide_ok=*/true);
10235
10236 /* A `)' completes the static assertion. */
10237 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10238 cp_parser_skip_to_closing_parenthesis (parser,
10239 /*recovering=*/true,
10240 /*or_comma=*/false,
10241 /*consume_paren=*/true);
10242
10243 /* A semicolon terminates the declaration. */
10244 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10245
10246 /* Complete the static assertion, which may mean either processing
10247 the static assert now or saving it for template instantiation. */
10248 finish_static_assert (condition, message, saved_loc, member_p);
10249 }
10250
10251 /* Parse a `decltype' type. Returns the type.
10252
10253 simple-type-specifier:
10254 decltype ( expression ) */
10255
10256 static tree
10257 cp_parser_decltype (cp_parser *parser)
10258 {
10259 tree expr;
10260 bool id_expression_or_member_access_p = false;
10261 const char *saved_message;
10262 bool saved_integral_constant_expression_p;
10263 bool saved_non_integral_constant_expression_p;
10264 cp_token *id_expr_start_token;
10265
10266 /* Look for the `decltype' token. */
10267 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10268 return error_mark_node;
10269
10270 /* Types cannot be defined in a `decltype' expression. Save away the
10271 old message. */
10272 saved_message = parser->type_definition_forbidden_message;
10273
10274 /* And create the new one. */
10275 parser->type_definition_forbidden_message
10276 = G_("types may not be defined in %<decltype%> expressions");
10277
10278 /* The restrictions on constant-expressions do not apply inside
10279 decltype expressions. */
10280 saved_integral_constant_expression_p
10281 = parser->integral_constant_expression_p;
10282 saved_non_integral_constant_expression_p
10283 = parser->non_integral_constant_expression_p;
10284 parser->integral_constant_expression_p = false;
10285
10286 /* Do not actually evaluate the expression. */
10287 ++cp_unevaluated_operand;
10288
10289 /* Do not warn about problems with the expression. */
10290 ++c_inhibit_evaluation_warnings;
10291
10292 /* Parse the opening `('. */
10293 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10294 return error_mark_node;
10295
10296 /* First, try parsing an id-expression. */
10297 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10298 cp_parser_parse_tentatively (parser);
10299 expr = cp_parser_id_expression (parser,
10300 /*template_keyword_p=*/false,
10301 /*check_dependency_p=*/true,
10302 /*template_p=*/NULL,
10303 /*declarator_p=*/false,
10304 /*optional_p=*/false);
10305
10306 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10307 {
10308 bool non_integral_constant_expression_p = false;
10309 tree id_expression = expr;
10310 cp_id_kind idk;
10311 const char *error_msg;
10312
10313 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10314 /* Lookup the name we got back from the id-expression. */
10315 expr = cp_parser_lookup_name (parser, expr,
10316 none_type,
10317 /*is_template=*/false,
10318 /*is_namespace=*/false,
10319 /*check_dependency=*/true,
10320 /*ambiguous_decls=*/NULL,
10321 id_expr_start_token->location);
10322
10323 if (expr
10324 && expr != error_mark_node
10325 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10326 && TREE_CODE (expr) != TYPE_DECL
10327 && (TREE_CODE (expr) != BIT_NOT_EXPR
10328 || !TYPE_P (TREE_OPERAND (expr, 0)))
10329 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10330 {
10331 /* Complete lookup of the id-expression. */
10332 expr = (finish_id_expression
10333 (id_expression, expr, parser->scope, &idk,
10334 /*integral_constant_expression_p=*/false,
10335 /*allow_non_integral_constant_expression_p=*/true,
10336 &non_integral_constant_expression_p,
10337 /*template_p=*/false,
10338 /*done=*/true,
10339 /*address_p=*/false,
10340 /*template_arg_p=*/false,
10341 &error_msg,
10342 id_expr_start_token->location));
10343
10344 if (expr == error_mark_node)
10345 /* We found an id-expression, but it was something that we
10346 should not have found. This is an error, not something
10347 we can recover from, so note that we found an
10348 id-expression and we'll recover as gracefully as
10349 possible. */
10350 id_expression_or_member_access_p = true;
10351 }
10352
10353 if (expr
10354 && expr != error_mark_node
10355 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10356 /* We have an id-expression. */
10357 id_expression_or_member_access_p = true;
10358 }
10359
10360 if (!id_expression_or_member_access_p)
10361 {
10362 /* Abort the id-expression parse. */
10363 cp_parser_abort_tentative_parse (parser);
10364
10365 /* Parsing tentatively, again. */
10366 cp_parser_parse_tentatively (parser);
10367
10368 /* Parse a class member access. */
10369 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10370 /*cast_p=*/false,
10371 /*member_access_only_p=*/true, NULL);
10372
10373 if (expr
10374 && expr != error_mark_node
10375 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10376 /* We have an id-expression. */
10377 id_expression_or_member_access_p = true;
10378 }
10379
10380 if (id_expression_or_member_access_p)
10381 /* We have parsed the complete id-expression or member access. */
10382 cp_parser_parse_definitely (parser);
10383 else
10384 {
10385 bool saved_greater_than_is_operator_p;
10386
10387 /* Abort our attempt to parse an id-expression or member access
10388 expression. */
10389 cp_parser_abort_tentative_parse (parser);
10390
10391 /* Within a parenthesized expression, a `>' token is always
10392 the greater-than operator. */
10393 saved_greater_than_is_operator_p
10394 = parser->greater_than_is_operator_p;
10395 parser->greater_than_is_operator_p = true;
10396
10397 /* Parse a full expression. */
10398 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10399
10400 /* The `>' token might be the end of a template-id or
10401 template-parameter-list now. */
10402 parser->greater_than_is_operator_p
10403 = saved_greater_than_is_operator_p;
10404 }
10405
10406 /* Go back to evaluating expressions. */
10407 --cp_unevaluated_operand;
10408 --c_inhibit_evaluation_warnings;
10409
10410 /* Restore the old message and the integral constant expression
10411 flags. */
10412 parser->type_definition_forbidden_message = saved_message;
10413 parser->integral_constant_expression_p
10414 = saved_integral_constant_expression_p;
10415 parser->non_integral_constant_expression_p
10416 = saved_non_integral_constant_expression_p;
10417
10418 if (expr == error_mark_node)
10419 {
10420 /* Skip everything up to the closing `)'. */
10421 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10422 /*consume_paren=*/true);
10423 return error_mark_node;
10424 }
10425
10426 /* Parse to the closing `)'. */
10427 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10428 {
10429 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10430 /*consume_paren=*/true);
10431 return error_mark_node;
10432 }
10433
10434 return finish_decltype_type (expr, id_expression_or_member_access_p);
10435 }
10436
10437 /* Special member functions [gram.special] */
10438
10439 /* Parse a conversion-function-id.
10440
10441 conversion-function-id:
10442 operator conversion-type-id
10443
10444 Returns an IDENTIFIER_NODE representing the operator. */
10445
10446 static tree
10447 cp_parser_conversion_function_id (cp_parser* parser)
10448 {
10449 tree type;
10450 tree saved_scope;
10451 tree saved_qualifying_scope;
10452 tree saved_object_scope;
10453 tree pushed_scope = NULL_TREE;
10454
10455 /* Look for the `operator' token. */
10456 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10457 return error_mark_node;
10458 /* When we parse the conversion-type-id, the current scope will be
10459 reset. However, we need that information in able to look up the
10460 conversion function later, so we save it here. */
10461 saved_scope = parser->scope;
10462 saved_qualifying_scope = parser->qualifying_scope;
10463 saved_object_scope = parser->object_scope;
10464 /* We must enter the scope of the class so that the names of
10465 entities declared within the class are available in the
10466 conversion-type-id. For example, consider:
10467
10468 struct S {
10469 typedef int I;
10470 operator I();
10471 };
10472
10473 S::operator I() { ... }
10474
10475 In order to see that `I' is a type-name in the definition, we
10476 must be in the scope of `S'. */
10477 if (saved_scope)
10478 pushed_scope = push_scope (saved_scope);
10479 /* Parse the conversion-type-id. */
10480 type = cp_parser_conversion_type_id (parser);
10481 /* Leave the scope of the class, if any. */
10482 if (pushed_scope)
10483 pop_scope (pushed_scope);
10484 /* Restore the saved scope. */
10485 parser->scope = saved_scope;
10486 parser->qualifying_scope = saved_qualifying_scope;
10487 parser->object_scope = saved_object_scope;
10488 /* If the TYPE is invalid, indicate failure. */
10489 if (type == error_mark_node)
10490 return error_mark_node;
10491 return mangle_conv_op_name_for_type (type);
10492 }
10493
10494 /* Parse a conversion-type-id:
10495
10496 conversion-type-id:
10497 type-specifier-seq conversion-declarator [opt]
10498
10499 Returns the TYPE specified. */
10500
10501 static tree
10502 cp_parser_conversion_type_id (cp_parser* parser)
10503 {
10504 tree attributes;
10505 cp_decl_specifier_seq type_specifiers;
10506 cp_declarator *declarator;
10507 tree type_specified;
10508
10509 /* Parse the attributes. */
10510 attributes = cp_parser_attributes_opt (parser);
10511 /* Parse the type-specifiers. */
10512 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10513 /*is_trailing_return=*/false,
10514 &type_specifiers);
10515 /* If that didn't work, stop. */
10516 if (type_specifiers.type == error_mark_node)
10517 return error_mark_node;
10518 /* Parse the conversion-declarator. */
10519 declarator = cp_parser_conversion_declarator_opt (parser);
10520
10521 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10522 /*initialized=*/0, &attributes);
10523 if (attributes)
10524 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10525
10526 /* Don't give this error when parsing tentatively. This happens to
10527 work because we always parse this definitively once. */
10528 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10529 && type_uses_auto (type_specified))
10530 {
10531 error ("invalid use of %<auto%> in conversion operator");
10532 return error_mark_node;
10533 }
10534
10535 return type_specified;
10536 }
10537
10538 /* Parse an (optional) conversion-declarator.
10539
10540 conversion-declarator:
10541 ptr-operator conversion-declarator [opt]
10542
10543 */
10544
10545 static cp_declarator *
10546 cp_parser_conversion_declarator_opt (cp_parser* parser)
10547 {
10548 enum tree_code code;
10549 tree class_type;
10550 cp_cv_quals cv_quals;
10551
10552 /* We don't know if there's a ptr-operator next, or not. */
10553 cp_parser_parse_tentatively (parser);
10554 /* Try the ptr-operator. */
10555 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10556 /* If it worked, look for more conversion-declarators. */
10557 if (cp_parser_parse_definitely (parser))
10558 {
10559 cp_declarator *declarator;
10560
10561 /* Parse another optional declarator. */
10562 declarator = cp_parser_conversion_declarator_opt (parser);
10563
10564 return cp_parser_make_indirect_declarator
10565 (code, class_type, cv_quals, declarator);
10566 }
10567
10568 return NULL;
10569 }
10570
10571 /* Parse an (optional) ctor-initializer.
10572
10573 ctor-initializer:
10574 : mem-initializer-list
10575
10576 Returns TRUE iff the ctor-initializer was actually present. */
10577
10578 static bool
10579 cp_parser_ctor_initializer_opt (cp_parser* parser)
10580 {
10581 /* If the next token is not a `:', then there is no
10582 ctor-initializer. */
10583 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10584 {
10585 /* Do default initialization of any bases and members. */
10586 if (DECL_CONSTRUCTOR_P (current_function_decl))
10587 finish_mem_initializers (NULL_TREE);
10588
10589 return false;
10590 }
10591
10592 /* Consume the `:' token. */
10593 cp_lexer_consume_token (parser->lexer);
10594 /* And the mem-initializer-list. */
10595 cp_parser_mem_initializer_list (parser);
10596
10597 return true;
10598 }
10599
10600 /* Parse a mem-initializer-list.
10601
10602 mem-initializer-list:
10603 mem-initializer ... [opt]
10604 mem-initializer ... [opt] , mem-initializer-list */
10605
10606 static void
10607 cp_parser_mem_initializer_list (cp_parser* parser)
10608 {
10609 tree mem_initializer_list = NULL_TREE;
10610 cp_token *token = cp_lexer_peek_token (parser->lexer);
10611
10612 /* Let the semantic analysis code know that we are starting the
10613 mem-initializer-list. */
10614 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10615 error_at (token->location,
10616 "only constructors take member initializers");
10617
10618 /* Loop through the list. */
10619 while (true)
10620 {
10621 tree mem_initializer;
10622
10623 token = cp_lexer_peek_token (parser->lexer);
10624 /* Parse the mem-initializer. */
10625 mem_initializer = cp_parser_mem_initializer (parser);
10626 /* If the next token is a `...', we're expanding member initializers. */
10627 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10628 {
10629 /* Consume the `...'. */
10630 cp_lexer_consume_token (parser->lexer);
10631
10632 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10633 can be expanded but members cannot. */
10634 if (mem_initializer != error_mark_node
10635 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10636 {
10637 error_at (token->location,
10638 "cannot expand initializer for member %<%D%>",
10639 TREE_PURPOSE (mem_initializer));
10640 mem_initializer = error_mark_node;
10641 }
10642
10643 /* Construct the pack expansion type. */
10644 if (mem_initializer != error_mark_node)
10645 mem_initializer = make_pack_expansion (mem_initializer);
10646 }
10647 /* Add it to the list, unless it was erroneous. */
10648 if (mem_initializer != error_mark_node)
10649 {
10650 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10651 mem_initializer_list = mem_initializer;
10652 }
10653 /* If the next token is not a `,', we're done. */
10654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10655 break;
10656 /* Consume the `,' token. */
10657 cp_lexer_consume_token (parser->lexer);
10658 }
10659
10660 /* Perform semantic analysis. */
10661 if (DECL_CONSTRUCTOR_P (current_function_decl))
10662 finish_mem_initializers (mem_initializer_list);
10663 }
10664
10665 /* Parse a mem-initializer.
10666
10667 mem-initializer:
10668 mem-initializer-id ( expression-list [opt] )
10669 mem-initializer-id braced-init-list
10670
10671 GNU extension:
10672
10673 mem-initializer:
10674 ( expression-list [opt] )
10675
10676 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10677 class) or FIELD_DECL (for a non-static data member) to initialize;
10678 the TREE_VALUE is the expression-list. An empty initialization
10679 list is represented by void_list_node. */
10680
10681 static tree
10682 cp_parser_mem_initializer (cp_parser* parser)
10683 {
10684 tree mem_initializer_id;
10685 tree expression_list;
10686 tree member;
10687 cp_token *token = cp_lexer_peek_token (parser->lexer);
10688
10689 /* Find out what is being initialized. */
10690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10691 {
10692 permerror (token->location,
10693 "anachronistic old-style base class initializer");
10694 mem_initializer_id = NULL_TREE;
10695 }
10696 else
10697 {
10698 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10699 if (mem_initializer_id == error_mark_node)
10700 return mem_initializer_id;
10701 }
10702 member = expand_member_init (mem_initializer_id);
10703 if (member && !DECL_P (member))
10704 in_base_initializer = 1;
10705
10706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10707 {
10708 bool expr_non_constant_p;
10709 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10710 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10711 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10712 expression_list = build_tree_list (NULL_TREE, expression_list);
10713 }
10714 else
10715 {
10716 VEC(tree,gc)* vec;
10717 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10718 /*cast_p=*/false,
10719 /*allow_expansion_p=*/true,
10720 /*non_constant_p=*/NULL);
10721 if (vec == NULL)
10722 return error_mark_node;
10723 expression_list = build_tree_list_vec (vec);
10724 release_tree_vector (vec);
10725 }
10726
10727 if (expression_list == error_mark_node)
10728 return error_mark_node;
10729 if (!expression_list)
10730 expression_list = void_type_node;
10731
10732 in_base_initializer = 0;
10733
10734 return member ? build_tree_list (member, expression_list) : error_mark_node;
10735 }
10736
10737 /* Parse a mem-initializer-id.
10738
10739 mem-initializer-id:
10740 :: [opt] nested-name-specifier [opt] class-name
10741 identifier
10742
10743 Returns a TYPE indicating the class to be initializer for the first
10744 production. Returns an IDENTIFIER_NODE indicating the data member
10745 to be initialized for the second production. */
10746
10747 static tree
10748 cp_parser_mem_initializer_id (cp_parser* parser)
10749 {
10750 bool global_scope_p;
10751 bool nested_name_specifier_p;
10752 bool template_p = false;
10753 tree id;
10754
10755 cp_token *token = cp_lexer_peek_token (parser->lexer);
10756
10757 /* `typename' is not allowed in this context ([temp.res]). */
10758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10759 {
10760 error_at (token->location,
10761 "keyword %<typename%> not allowed in this context (a qualified "
10762 "member initializer is implicitly a type)");
10763 cp_lexer_consume_token (parser->lexer);
10764 }
10765 /* Look for the optional `::' operator. */
10766 global_scope_p
10767 = (cp_parser_global_scope_opt (parser,
10768 /*current_scope_valid_p=*/false)
10769 != NULL_TREE);
10770 /* Look for the optional nested-name-specifier. The simplest way to
10771 implement:
10772
10773 [temp.res]
10774
10775 The keyword `typename' is not permitted in a base-specifier or
10776 mem-initializer; in these contexts a qualified name that
10777 depends on a template-parameter is implicitly assumed to be a
10778 type name.
10779
10780 is to assume that we have seen the `typename' keyword at this
10781 point. */
10782 nested_name_specifier_p
10783 = (cp_parser_nested_name_specifier_opt (parser,
10784 /*typename_keyword_p=*/true,
10785 /*check_dependency_p=*/true,
10786 /*type_p=*/true,
10787 /*is_declaration=*/true)
10788 != NULL_TREE);
10789 if (nested_name_specifier_p)
10790 template_p = cp_parser_optional_template_keyword (parser);
10791 /* If there is a `::' operator or a nested-name-specifier, then we
10792 are definitely looking for a class-name. */
10793 if (global_scope_p || nested_name_specifier_p)
10794 return cp_parser_class_name (parser,
10795 /*typename_keyword_p=*/true,
10796 /*template_keyword_p=*/template_p,
10797 typename_type,
10798 /*check_dependency_p=*/true,
10799 /*class_head_p=*/false,
10800 /*is_declaration=*/true);
10801 /* Otherwise, we could also be looking for an ordinary identifier. */
10802 cp_parser_parse_tentatively (parser);
10803 /* Try a class-name. */
10804 id = cp_parser_class_name (parser,
10805 /*typename_keyword_p=*/true,
10806 /*template_keyword_p=*/false,
10807 none_type,
10808 /*check_dependency_p=*/true,
10809 /*class_head_p=*/false,
10810 /*is_declaration=*/true);
10811 /* If we found one, we're done. */
10812 if (cp_parser_parse_definitely (parser))
10813 return id;
10814 /* Otherwise, look for an ordinary identifier. */
10815 return cp_parser_identifier (parser);
10816 }
10817
10818 /* Overloading [gram.over] */
10819
10820 /* Parse an operator-function-id.
10821
10822 operator-function-id:
10823 operator operator
10824
10825 Returns an IDENTIFIER_NODE for the operator which is a
10826 human-readable spelling of the identifier, e.g., `operator +'. */
10827
10828 static tree
10829 cp_parser_operator_function_id (cp_parser* parser)
10830 {
10831 /* Look for the `operator' keyword. */
10832 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10833 return error_mark_node;
10834 /* And then the name of the operator itself. */
10835 return cp_parser_operator (parser);
10836 }
10837
10838 /* Parse an operator.
10839
10840 operator:
10841 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10842 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10843 || ++ -- , ->* -> () []
10844
10845 GNU Extensions:
10846
10847 operator:
10848 <? >? <?= >?=
10849
10850 Returns an IDENTIFIER_NODE for the operator which is a
10851 human-readable spelling of the identifier, e.g., `operator +'. */
10852
10853 static tree
10854 cp_parser_operator (cp_parser* parser)
10855 {
10856 tree id = NULL_TREE;
10857 cp_token *token;
10858
10859 /* Peek at the next token. */
10860 token = cp_lexer_peek_token (parser->lexer);
10861 /* Figure out which operator we have. */
10862 switch (token->type)
10863 {
10864 case CPP_KEYWORD:
10865 {
10866 enum tree_code op;
10867
10868 /* The keyword should be either `new' or `delete'. */
10869 if (token->keyword == RID_NEW)
10870 op = NEW_EXPR;
10871 else if (token->keyword == RID_DELETE)
10872 op = DELETE_EXPR;
10873 else
10874 break;
10875
10876 /* Consume the `new' or `delete' token. */
10877 cp_lexer_consume_token (parser->lexer);
10878
10879 /* Peek at the next token. */
10880 token = cp_lexer_peek_token (parser->lexer);
10881 /* If it's a `[' token then this is the array variant of the
10882 operator. */
10883 if (token->type == CPP_OPEN_SQUARE)
10884 {
10885 /* Consume the `[' token. */
10886 cp_lexer_consume_token (parser->lexer);
10887 /* Look for the `]' token. */
10888 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10889 id = ansi_opname (op == NEW_EXPR
10890 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10891 }
10892 /* Otherwise, we have the non-array variant. */
10893 else
10894 id = ansi_opname (op);
10895
10896 return id;
10897 }
10898
10899 case CPP_PLUS:
10900 id = ansi_opname (PLUS_EXPR);
10901 break;
10902
10903 case CPP_MINUS:
10904 id = ansi_opname (MINUS_EXPR);
10905 break;
10906
10907 case CPP_MULT:
10908 id = ansi_opname (MULT_EXPR);
10909 break;
10910
10911 case CPP_DIV:
10912 id = ansi_opname (TRUNC_DIV_EXPR);
10913 break;
10914
10915 case CPP_MOD:
10916 id = ansi_opname (TRUNC_MOD_EXPR);
10917 break;
10918
10919 case CPP_XOR:
10920 id = ansi_opname (BIT_XOR_EXPR);
10921 break;
10922
10923 case CPP_AND:
10924 id = ansi_opname (BIT_AND_EXPR);
10925 break;
10926
10927 case CPP_OR:
10928 id = ansi_opname (BIT_IOR_EXPR);
10929 break;
10930
10931 case CPP_COMPL:
10932 id = ansi_opname (BIT_NOT_EXPR);
10933 break;
10934
10935 case CPP_NOT:
10936 id = ansi_opname (TRUTH_NOT_EXPR);
10937 break;
10938
10939 case CPP_EQ:
10940 id = ansi_assopname (NOP_EXPR);
10941 break;
10942
10943 case CPP_LESS:
10944 id = ansi_opname (LT_EXPR);
10945 break;
10946
10947 case CPP_GREATER:
10948 id = ansi_opname (GT_EXPR);
10949 break;
10950
10951 case CPP_PLUS_EQ:
10952 id = ansi_assopname (PLUS_EXPR);
10953 break;
10954
10955 case CPP_MINUS_EQ:
10956 id = ansi_assopname (MINUS_EXPR);
10957 break;
10958
10959 case CPP_MULT_EQ:
10960 id = ansi_assopname (MULT_EXPR);
10961 break;
10962
10963 case CPP_DIV_EQ:
10964 id = ansi_assopname (TRUNC_DIV_EXPR);
10965 break;
10966
10967 case CPP_MOD_EQ:
10968 id = ansi_assopname (TRUNC_MOD_EXPR);
10969 break;
10970
10971 case CPP_XOR_EQ:
10972 id = ansi_assopname (BIT_XOR_EXPR);
10973 break;
10974
10975 case CPP_AND_EQ:
10976 id = ansi_assopname (BIT_AND_EXPR);
10977 break;
10978
10979 case CPP_OR_EQ:
10980 id = ansi_assopname (BIT_IOR_EXPR);
10981 break;
10982
10983 case CPP_LSHIFT:
10984 id = ansi_opname (LSHIFT_EXPR);
10985 break;
10986
10987 case CPP_RSHIFT:
10988 id = ansi_opname (RSHIFT_EXPR);
10989 break;
10990
10991 case CPP_LSHIFT_EQ:
10992 id = ansi_assopname (LSHIFT_EXPR);
10993 break;
10994
10995 case CPP_RSHIFT_EQ:
10996 id = ansi_assopname (RSHIFT_EXPR);
10997 break;
10998
10999 case CPP_EQ_EQ:
11000 id = ansi_opname (EQ_EXPR);
11001 break;
11002
11003 case CPP_NOT_EQ:
11004 id = ansi_opname (NE_EXPR);
11005 break;
11006
11007 case CPP_LESS_EQ:
11008 id = ansi_opname (LE_EXPR);
11009 break;
11010
11011 case CPP_GREATER_EQ:
11012 id = ansi_opname (GE_EXPR);
11013 break;
11014
11015 case CPP_AND_AND:
11016 id = ansi_opname (TRUTH_ANDIF_EXPR);
11017 break;
11018
11019 case CPP_OR_OR:
11020 id = ansi_opname (TRUTH_ORIF_EXPR);
11021 break;
11022
11023 case CPP_PLUS_PLUS:
11024 id = ansi_opname (POSTINCREMENT_EXPR);
11025 break;
11026
11027 case CPP_MINUS_MINUS:
11028 id = ansi_opname (PREDECREMENT_EXPR);
11029 break;
11030
11031 case CPP_COMMA:
11032 id = ansi_opname (COMPOUND_EXPR);
11033 break;
11034
11035 case CPP_DEREF_STAR:
11036 id = ansi_opname (MEMBER_REF);
11037 break;
11038
11039 case CPP_DEREF:
11040 id = ansi_opname (COMPONENT_REF);
11041 break;
11042
11043 case CPP_OPEN_PAREN:
11044 /* Consume the `('. */
11045 cp_lexer_consume_token (parser->lexer);
11046 /* Look for the matching `)'. */
11047 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11048 return ansi_opname (CALL_EXPR);
11049
11050 case CPP_OPEN_SQUARE:
11051 /* Consume the `['. */
11052 cp_lexer_consume_token (parser->lexer);
11053 /* Look for the matching `]'. */
11054 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11055 return ansi_opname (ARRAY_REF);
11056
11057 default:
11058 /* Anything else is an error. */
11059 break;
11060 }
11061
11062 /* If we have selected an identifier, we need to consume the
11063 operator token. */
11064 if (id)
11065 cp_lexer_consume_token (parser->lexer);
11066 /* Otherwise, no valid operator name was present. */
11067 else
11068 {
11069 cp_parser_error (parser, "expected operator");
11070 id = error_mark_node;
11071 }
11072
11073 return id;
11074 }
11075
11076 /* Parse a template-declaration.
11077
11078 template-declaration:
11079 export [opt] template < template-parameter-list > declaration
11080
11081 If MEMBER_P is TRUE, this template-declaration occurs within a
11082 class-specifier.
11083
11084 The grammar rule given by the standard isn't correct. What
11085 is really meant is:
11086
11087 template-declaration:
11088 export [opt] template-parameter-list-seq
11089 decl-specifier-seq [opt] init-declarator [opt] ;
11090 export [opt] template-parameter-list-seq
11091 function-definition
11092
11093 template-parameter-list-seq:
11094 template-parameter-list-seq [opt]
11095 template < template-parameter-list > */
11096
11097 static void
11098 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11099 {
11100 /* Check for `export'. */
11101 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11102 {
11103 /* Consume the `export' token. */
11104 cp_lexer_consume_token (parser->lexer);
11105 /* Warn that we do not support `export'. */
11106 warning (0, "keyword %<export%> not implemented, and will be ignored");
11107 }
11108
11109 cp_parser_template_declaration_after_export (parser, member_p);
11110 }
11111
11112 /* Parse a template-parameter-list.
11113
11114 template-parameter-list:
11115 template-parameter
11116 template-parameter-list , template-parameter
11117
11118 Returns a TREE_LIST. Each node represents a template parameter.
11119 The nodes are connected via their TREE_CHAINs. */
11120
11121 static tree
11122 cp_parser_template_parameter_list (cp_parser* parser)
11123 {
11124 tree parameter_list = NULL_TREE;
11125
11126 begin_template_parm_list ();
11127
11128 /* The loop below parses the template parms. We first need to know
11129 the total number of template parms to be able to compute proper
11130 canonical types of each dependent type. So after the loop, when
11131 we know the total number of template parms,
11132 end_template_parm_list computes the proper canonical types and
11133 fixes up the dependent types accordingly. */
11134 while (true)
11135 {
11136 tree parameter;
11137 bool is_non_type;
11138 bool is_parameter_pack;
11139 location_t parm_loc;
11140
11141 /* Parse the template-parameter. */
11142 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11143 parameter = cp_parser_template_parameter (parser,
11144 &is_non_type,
11145 &is_parameter_pack);
11146 /* Add it to the list. */
11147 if (parameter != error_mark_node)
11148 parameter_list = process_template_parm (parameter_list,
11149 parm_loc,
11150 parameter,
11151 is_non_type,
11152 is_parameter_pack,
11153 0);
11154 else
11155 {
11156 tree err_parm = build_tree_list (parameter, parameter);
11157 parameter_list = chainon (parameter_list, err_parm);
11158 }
11159
11160 /* If the next token is not a `,', we're done. */
11161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11162 break;
11163 /* Otherwise, consume the `,' token. */
11164 cp_lexer_consume_token (parser->lexer);
11165 }
11166
11167 return end_template_parm_list (parameter_list);
11168 }
11169
11170 /* Parse a template-parameter.
11171
11172 template-parameter:
11173 type-parameter
11174 parameter-declaration
11175
11176 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11177 the parameter. The TREE_PURPOSE is the default value, if any.
11178 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11179 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11180 set to true iff this parameter is a parameter pack. */
11181
11182 static tree
11183 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11184 bool *is_parameter_pack)
11185 {
11186 cp_token *token;
11187 cp_parameter_declarator *parameter_declarator;
11188 cp_declarator *id_declarator;
11189 tree parm;
11190
11191 /* Assume it is a type parameter or a template parameter. */
11192 *is_non_type = false;
11193 /* Assume it not a parameter pack. */
11194 *is_parameter_pack = false;
11195 /* Peek at the next token. */
11196 token = cp_lexer_peek_token (parser->lexer);
11197 /* If it is `class' or `template', we have a type-parameter. */
11198 if (token->keyword == RID_TEMPLATE)
11199 return cp_parser_type_parameter (parser, is_parameter_pack);
11200 /* If it is `class' or `typename' we do not know yet whether it is a
11201 type parameter or a non-type parameter. Consider:
11202
11203 template <typename T, typename T::X X> ...
11204
11205 or:
11206
11207 template <class C, class D*> ...
11208
11209 Here, the first parameter is a type parameter, and the second is
11210 a non-type parameter. We can tell by looking at the token after
11211 the identifier -- if it is a `,', `=', or `>' then we have a type
11212 parameter. */
11213 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11214 {
11215 /* Peek at the token after `class' or `typename'. */
11216 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11217 /* If it's an ellipsis, we have a template type parameter
11218 pack. */
11219 if (token->type == CPP_ELLIPSIS)
11220 return cp_parser_type_parameter (parser, is_parameter_pack);
11221 /* If it's an identifier, skip it. */
11222 if (token->type == CPP_NAME)
11223 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11224 /* Now, see if the token looks like the end of a template
11225 parameter. */
11226 if (token->type == CPP_COMMA
11227 || token->type == CPP_EQ
11228 || token->type == CPP_GREATER)
11229 return cp_parser_type_parameter (parser, is_parameter_pack);
11230 }
11231
11232 /* Otherwise, it is a non-type parameter.
11233
11234 [temp.param]
11235
11236 When parsing a default template-argument for a non-type
11237 template-parameter, the first non-nested `>' is taken as the end
11238 of the template parameter-list rather than a greater-than
11239 operator. */
11240 *is_non_type = true;
11241 parameter_declarator
11242 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11243 /*parenthesized_p=*/NULL);
11244
11245 /* If the parameter declaration is marked as a parameter pack, set
11246 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11247 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11248 grokdeclarator. */
11249 if (parameter_declarator
11250 && parameter_declarator->declarator
11251 && parameter_declarator->declarator->parameter_pack_p)
11252 {
11253 *is_parameter_pack = true;
11254 parameter_declarator->declarator->parameter_pack_p = false;
11255 }
11256
11257 /* If the next token is an ellipsis, and we don't already have it
11258 marked as a parameter pack, then we have a parameter pack (that
11259 has no declarator). */
11260 if (!*is_parameter_pack
11261 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11262 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11263 {
11264 /* Consume the `...'. */
11265 cp_lexer_consume_token (parser->lexer);
11266 maybe_warn_variadic_templates ();
11267
11268 *is_parameter_pack = true;
11269 }
11270 /* We might end up with a pack expansion as the type of the non-type
11271 template parameter, in which case this is a non-type template
11272 parameter pack. */
11273 else if (parameter_declarator
11274 && parameter_declarator->decl_specifiers.type
11275 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11276 {
11277 *is_parameter_pack = true;
11278 parameter_declarator->decl_specifiers.type =
11279 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11280 }
11281
11282 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11283 {
11284 /* Parameter packs cannot have default arguments. However, a
11285 user may try to do so, so we'll parse them and give an
11286 appropriate diagnostic here. */
11287
11288 /* Consume the `='. */
11289 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11290 cp_lexer_consume_token (parser->lexer);
11291
11292 /* Find the name of the parameter pack. */
11293 id_declarator = parameter_declarator->declarator;
11294 while (id_declarator && id_declarator->kind != cdk_id)
11295 id_declarator = id_declarator->declarator;
11296
11297 if (id_declarator && id_declarator->kind == cdk_id)
11298 error_at (start_token->location,
11299 "template parameter pack %qD cannot have a default argument",
11300 id_declarator->u.id.unqualified_name);
11301 else
11302 error_at (start_token->location,
11303 "template parameter pack cannot have a default argument");
11304
11305 /* Parse the default argument, but throw away the result. */
11306 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11307 }
11308
11309 parm = grokdeclarator (parameter_declarator->declarator,
11310 &parameter_declarator->decl_specifiers,
11311 TPARM, /*initialized=*/0,
11312 /*attrlist=*/NULL);
11313 if (parm == error_mark_node)
11314 return error_mark_node;
11315
11316 return build_tree_list (parameter_declarator->default_argument, parm);
11317 }
11318
11319 /* Parse a type-parameter.
11320
11321 type-parameter:
11322 class identifier [opt]
11323 class identifier [opt] = type-id
11324 typename identifier [opt]
11325 typename identifier [opt] = type-id
11326 template < template-parameter-list > class identifier [opt]
11327 template < template-parameter-list > class identifier [opt]
11328 = id-expression
11329
11330 GNU Extension (variadic templates):
11331
11332 type-parameter:
11333 class ... identifier [opt]
11334 typename ... identifier [opt]
11335
11336 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11337 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11338 the declaration of the parameter.
11339
11340 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11341
11342 static tree
11343 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11344 {
11345 cp_token *token;
11346 tree parameter;
11347
11348 /* Look for a keyword to tell us what kind of parameter this is. */
11349 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11350 if (!token)
11351 return error_mark_node;
11352
11353 switch (token->keyword)
11354 {
11355 case RID_CLASS:
11356 case RID_TYPENAME:
11357 {
11358 tree identifier;
11359 tree default_argument;
11360
11361 /* If the next token is an ellipsis, we have a template
11362 argument pack. */
11363 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11364 {
11365 /* Consume the `...' token. */
11366 cp_lexer_consume_token (parser->lexer);
11367 maybe_warn_variadic_templates ();
11368
11369 *is_parameter_pack = true;
11370 }
11371
11372 /* If the next token is an identifier, then it names the
11373 parameter. */
11374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11375 identifier = cp_parser_identifier (parser);
11376 else
11377 identifier = NULL_TREE;
11378
11379 /* Create the parameter. */
11380 parameter = finish_template_type_parm (class_type_node, identifier);
11381
11382 /* If the next token is an `=', we have a default argument. */
11383 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11384 {
11385 /* Consume the `=' token. */
11386 cp_lexer_consume_token (parser->lexer);
11387 /* Parse the default-argument. */
11388 push_deferring_access_checks (dk_no_deferred);
11389 default_argument = cp_parser_type_id (parser);
11390
11391 /* Template parameter packs cannot have default
11392 arguments. */
11393 if (*is_parameter_pack)
11394 {
11395 if (identifier)
11396 error_at (token->location,
11397 "template parameter pack %qD cannot have a "
11398 "default argument", identifier);
11399 else
11400 error_at (token->location,
11401 "template parameter packs cannot have "
11402 "default arguments");
11403 default_argument = NULL_TREE;
11404 }
11405 pop_deferring_access_checks ();
11406 }
11407 else
11408 default_argument = NULL_TREE;
11409
11410 /* Create the combined representation of the parameter and the
11411 default argument. */
11412 parameter = build_tree_list (default_argument, parameter);
11413 }
11414 break;
11415
11416 case RID_TEMPLATE:
11417 {
11418 tree identifier;
11419 tree default_argument;
11420
11421 /* Look for the `<'. */
11422 cp_parser_require (parser, CPP_LESS, RT_LESS);
11423 /* Parse the template-parameter-list. */
11424 cp_parser_template_parameter_list (parser);
11425 /* Look for the `>'. */
11426 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11427 /* Look for the `class' keyword. */
11428 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11429 /* If the next token is an ellipsis, we have a template
11430 argument pack. */
11431 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11432 {
11433 /* Consume the `...' token. */
11434 cp_lexer_consume_token (parser->lexer);
11435 maybe_warn_variadic_templates ();
11436
11437 *is_parameter_pack = true;
11438 }
11439 /* If the next token is an `=', then there is a
11440 default-argument. If the next token is a `>', we are at
11441 the end of the parameter-list. If the next token is a `,',
11442 then we are at the end of this parameter. */
11443 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11444 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11445 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11446 {
11447 identifier = cp_parser_identifier (parser);
11448 /* Treat invalid names as if the parameter were nameless. */
11449 if (identifier == error_mark_node)
11450 identifier = NULL_TREE;
11451 }
11452 else
11453 identifier = NULL_TREE;
11454
11455 /* Create the template parameter. */
11456 parameter = finish_template_template_parm (class_type_node,
11457 identifier);
11458
11459 /* If the next token is an `=', then there is a
11460 default-argument. */
11461 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11462 {
11463 bool is_template;
11464
11465 /* Consume the `='. */
11466 cp_lexer_consume_token (parser->lexer);
11467 /* Parse the id-expression. */
11468 push_deferring_access_checks (dk_no_deferred);
11469 /* save token before parsing the id-expression, for error
11470 reporting */
11471 token = cp_lexer_peek_token (parser->lexer);
11472 default_argument
11473 = cp_parser_id_expression (parser,
11474 /*template_keyword_p=*/false,
11475 /*check_dependency_p=*/true,
11476 /*template_p=*/&is_template,
11477 /*declarator_p=*/false,
11478 /*optional_p=*/false);
11479 if (TREE_CODE (default_argument) == TYPE_DECL)
11480 /* If the id-expression was a template-id that refers to
11481 a template-class, we already have the declaration here,
11482 so no further lookup is needed. */
11483 ;
11484 else
11485 /* Look up the name. */
11486 default_argument
11487 = cp_parser_lookup_name (parser, default_argument,
11488 none_type,
11489 /*is_template=*/is_template,
11490 /*is_namespace=*/false,
11491 /*check_dependency=*/true,
11492 /*ambiguous_decls=*/NULL,
11493 token->location);
11494 /* See if the default argument is valid. */
11495 default_argument
11496 = check_template_template_default_arg (default_argument);
11497
11498 /* Template parameter packs cannot have default
11499 arguments. */
11500 if (*is_parameter_pack)
11501 {
11502 if (identifier)
11503 error_at (token->location,
11504 "template parameter pack %qD cannot "
11505 "have a default argument",
11506 identifier);
11507 else
11508 error_at (token->location, "template parameter packs cannot "
11509 "have default arguments");
11510 default_argument = NULL_TREE;
11511 }
11512 pop_deferring_access_checks ();
11513 }
11514 else
11515 default_argument = NULL_TREE;
11516
11517 /* Create the combined representation of the parameter and the
11518 default argument. */
11519 parameter = build_tree_list (default_argument, parameter);
11520 }
11521 break;
11522
11523 default:
11524 gcc_unreachable ();
11525 break;
11526 }
11527
11528 return parameter;
11529 }
11530
11531 /* Parse a template-id.
11532
11533 template-id:
11534 template-name < template-argument-list [opt] >
11535
11536 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11537 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11538 returned. Otherwise, if the template-name names a function, or set
11539 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11540 names a class, returns a TYPE_DECL for the specialization.
11541
11542 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11543 uninstantiated templates. */
11544
11545 static tree
11546 cp_parser_template_id (cp_parser *parser,
11547 bool template_keyword_p,
11548 bool check_dependency_p,
11549 bool is_declaration)
11550 {
11551 int i;
11552 tree templ;
11553 tree arguments;
11554 tree template_id;
11555 cp_token_position start_of_id = 0;
11556 deferred_access_check *chk;
11557 VEC (deferred_access_check,gc) *access_check;
11558 cp_token *next_token = NULL, *next_token_2 = NULL;
11559 bool is_identifier;
11560
11561 /* If the next token corresponds to a template-id, there is no need
11562 to reparse it. */
11563 next_token = cp_lexer_peek_token (parser->lexer);
11564 if (next_token->type == CPP_TEMPLATE_ID)
11565 {
11566 struct tree_check *check_value;
11567
11568 /* Get the stored value. */
11569 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11570 /* Perform any access checks that were deferred. */
11571 access_check = check_value->checks;
11572 if (access_check)
11573 {
11574 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11575 perform_or_defer_access_check (chk->binfo,
11576 chk->decl,
11577 chk->diag_decl);
11578 }
11579 /* Return the stored value. */
11580 return check_value->value;
11581 }
11582
11583 /* Avoid performing name lookup if there is no possibility of
11584 finding a template-id. */
11585 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11586 || (next_token->type == CPP_NAME
11587 && !cp_parser_nth_token_starts_template_argument_list_p
11588 (parser, 2)))
11589 {
11590 cp_parser_error (parser, "expected template-id");
11591 return error_mark_node;
11592 }
11593
11594 /* Remember where the template-id starts. */
11595 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11596 start_of_id = cp_lexer_token_position (parser->lexer, false);
11597
11598 push_deferring_access_checks (dk_deferred);
11599
11600 /* Parse the template-name. */
11601 is_identifier = false;
11602 templ = cp_parser_template_name (parser, template_keyword_p,
11603 check_dependency_p,
11604 is_declaration,
11605 &is_identifier);
11606 if (templ == error_mark_node || is_identifier)
11607 {
11608 pop_deferring_access_checks ();
11609 return templ;
11610 }
11611
11612 /* If we find the sequence `[:' after a template-name, it's probably
11613 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11614 parse correctly the argument list. */
11615 next_token = cp_lexer_peek_token (parser->lexer);
11616 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11617 if (next_token->type == CPP_OPEN_SQUARE
11618 && next_token->flags & DIGRAPH
11619 && next_token_2->type == CPP_COLON
11620 && !(next_token_2->flags & PREV_WHITE))
11621 {
11622 cp_parser_parse_tentatively (parser);
11623 /* Change `:' into `::'. */
11624 next_token_2->type = CPP_SCOPE;
11625 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11626 CPP_LESS. */
11627 cp_lexer_consume_token (parser->lexer);
11628
11629 /* Parse the arguments. */
11630 arguments = cp_parser_enclosed_template_argument_list (parser);
11631 if (!cp_parser_parse_definitely (parser))
11632 {
11633 /* If we couldn't parse an argument list, then we revert our changes
11634 and return simply an error. Maybe this is not a template-id
11635 after all. */
11636 next_token_2->type = CPP_COLON;
11637 cp_parser_error (parser, "expected %<<%>");
11638 pop_deferring_access_checks ();
11639 return error_mark_node;
11640 }
11641 /* Otherwise, emit an error about the invalid digraph, but continue
11642 parsing because we got our argument list. */
11643 if (permerror (next_token->location,
11644 "%<<::%> cannot begin a template-argument list"))
11645 {
11646 static bool hint = false;
11647 inform (next_token->location,
11648 "%<<:%> is an alternate spelling for %<[%>."
11649 " Insert whitespace between %<<%> and %<::%>");
11650 if (!hint && !flag_permissive)
11651 {
11652 inform (next_token->location, "(if you use %<-fpermissive%>"
11653 " G++ will accept your code)");
11654 hint = true;
11655 }
11656 }
11657 }
11658 else
11659 {
11660 /* Look for the `<' that starts the template-argument-list. */
11661 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11662 {
11663 pop_deferring_access_checks ();
11664 return error_mark_node;
11665 }
11666 /* Parse the arguments. */
11667 arguments = cp_parser_enclosed_template_argument_list (parser);
11668 }
11669
11670 /* Build a representation of the specialization. */
11671 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11672 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11673 else if (DECL_CLASS_TEMPLATE_P (templ)
11674 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11675 {
11676 bool entering_scope;
11677 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11678 template (rather than some instantiation thereof) only if
11679 is not nested within some other construct. For example, in
11680 "template <typename T> void f(T) { A<T>::", A<T> is just an
11681 instantiation of A. */
11682 entering_scope = (template_parm_scope_p ()
11683 && cp_lexer_next_token_is (parser->lexer,
11684 CPP_SCOPE));
11685 template_id
11686 = finish_template_type (templ, arguments, entering_scope);
11687 }
11688 else
11689 {
11690 /* If it's not a class-template or a template-template, it should be
11691 a function-template. */
11692 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11693 || TREE_CODE (templ) == OVERLOAD
11694 || BASELINK_P (templ)));
11695
11696 template_id = lookup_template_function (templ, arguments);
11697 }
11698
11699 /* If parsing tentatively, replace the sequence of tokens that makes
11700 up the template-id with a CPP_TEMPLATE_ID token. That way,
11701 should we re-parse the token stream, we will not have to repeat
11702 the effort required to do the parse, nor will we issue duplicate
11703 error messages about problems during instantiation of the
11704 template. */
11705 if (start_of_id)
11706 {
11707 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11708
11709 /* Reset the contents of the START_OF_ID token. */
11710 token->type = CPP_TEMPLATE_ID;
11711 /* Retrieve any deferred checks. Do not pop this access checks yet
11712 so the memory will not be reclaimed during token replacing below. */
11713 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11714 token->u.tree_check_value->value = template_id;
11715 token->u.tree_check_value->checks = get_deferred_access_checks ();
11716 token->keyword = RID_MAX;
11717
11718 /* Purge all subsequent tokens. */
11719 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11720
11721 /* ??? Can we actually assume that, if template_id ==
11722 error_mark_node, we will have issued a diagnostic to the
11723 user, as opposed to simply marking the tentative parse as
11724 failed? */
11725 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11726 error_at (token->location, "parse error in template argument list");
11727 }
11728
11729 pop_deferring_access_checks ();
11730 return template_id;
11731 }
11732
11733 /* Parse a template-name.
11734
11735 template-name:
11736 identifier
11737
11738 The standard should actually say:
11739
11740 template-name:
11741 identifier
11742 operator-function-id
11743
11744 A defect report has been filed about this issue.
11745
11746 A conversion-function-id cannot be a template name because they cannot
11747 be part of a template-id. In fact, looking at this code:
11748
11749 a.operator K<int>()
11750
11751 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11752 It is impossible to call a templated conversion-function-id with an
11753 explicit argument list, since the only allowed template parameter is
11754 the type to which it is converting.
11755
11756 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11757 `template' keyword, in a construction like:
11758
11759 T::template f<3>()
11760
11761 In that case `f' is taken to be a template-name, even though there
11762 is no way of knowing for sure.
11763
11764 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11765 name refers to a set of overloaded functions, at least one of which
11766 is a template, or an IDENTIFIER_NODE with the name of the template,
11767 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11768 names are looked up inside uninstantiated templates. */
11769
11770 static tree
11771 cp_parser_template_name (cp_parser* parser,
11772 bool template_keyword_p,
11773 bool check_dependency_p,
11774 bool is_declaration,
11775 bool *is_identifier)
11776 {
11777 tree identifier;
11778 tree decl;
11779 tree fns;
11780 cp_token *token = cp_lexer_peek_token (parser->lexer);
11781
11782 /* If the next token is `operator', then we have either an
11783 operator-function-id or a conversion-function-id. */
11784 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11785 {
11786 /* We don't know whether we're looking at an
11787 operator-function-id or a conversion-function-id. */
11788 cp_parser_parse_tentatively (parser);
11789 /* Try an operator-function-id. */
11790 identifier = cp_parser_operator_function_id (parser);
11791 /* If that didn't work, try a conversion-function-id. */
11792 if (!cp_parser_parse_definitely (parser))
11793 {
11794 cp_parser_error (parser, "expected template-name");
11795 return error_mark_node;
11796 }
11797 }
11798 /* Look for the identifier. */
11799 else
11800 identifier = cp_parser_identifier (parser);
11801
11802 /* If we didn't find an identifier, we don't have a template-id. */
11803 if (identifier == error_mark_node)
11804 return error_mark_node;
11805
11806 /* If the name immediately followed the `template' keyword, then it
11807 is a template-name. However, if the next token is not `<', then
11808 we do not treat it as a template-name, since it is not being used
11809 as part of a template-id. This enables us to handle constructs
11810 like:
11811
11812 template <typename T> struct S { S(); };
11813 template <typename T> S<T>::S();
11814
11815 correctly. We would treat `S' as a template -- if it were `S<T>'
11816 -- but we do not if there is no `<'. */
11817
11818 if (processing_template_decl
11819 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11820 {
11821 /* In a declaration, in a dependent context, we pretend that the
11822 "template" keyword was present in order to improve error
11823 recovery. For example, given:
11824
11825 template <typename T> void f(T::X<int>);
11826
11827 we want to treat "X<int>" as a template-id. */
11828 if (is_declaration
11829 && !template_keyword_p
11830 && parser->scope && TYPE_P (parser->scope)
11831 && check_dependency_p
11832 && dependent_scope_p (parser->scope)
11833 /* Do not do this for dtors (or ctors), since they never
11834 need the template keyword before their name. */
11835 && !constructor_name_p (identifier, parser->scope))
11836 {
11837 cp_token_position start = 0;
11838
11839 /* Explain what went wrong. */
11840 error_at (token->location, "non-template %qD used as template",
11841 identifier);
11842 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11843 parser->scope, identifier);
11844 /* If parsing tentatively, find the location of the "<" token. */
11845 if (cp_parser_simulate_error (parser))
11846 start = cp_lexer_token_position (parser->lexer, true);
11847 /* Parse the template arguments so that we can issue error
11848 messages about them. */
11849 cp_lexer_consume_token (parser->lexer);
11850 cp_parser_enclosed_template_argument_list (parser);
11851 /* Skip tokens until we find a good place from which to
11852 continue parsing. */
11853 cp_parser_skip_to_closing_parenthesis (parser,
11854 /*recovering=*/true,
11855 /*or_comma=*/true,
11856 /*consume_paren=*/false);
11857 /* If parsing tentatively, permanently remove the
11858 template argument list. That will prevent duplicate
11859 error messages from being issued about the missing
11860 "template" keyword. */
11861 if (start)
11862 cp_lexer_purge_tokens_after (parser->lexer, start);
11863 if (is_identifier)
11864 *is_identifier = true;
11865 return identifier;
11866 }
11867
11868 /* If the "template" keyword is present, then there is generally
11869 no point in doing name-lookup, so we just return IDENTIFIER.
11870 But, if the qualifying scope is non-dependent then we can
11871 (and must) do name-lookup normally. */
11872 if (template_keyword_p
11873 && (!parser->scope
11874 || (TYPE_P (parser->scope)
11875 && dependent_type_p (parser->scope))))
11876 return identifier;
11877 }
11878
11879 /* Look up the name. */
11880 decl = cp_parser_lookup_name (parser, identifier,
11881 none_type,
11882 /*is_template=*/true,
11883 /*is_namespace=*/false,
11884 check_dependency_p,
11885 /*ambiguous_decls=*/NULL,
11886 token->location);
11887
11888 /* If DECL is a template, then the name was a template-name. */
11889 if (TREE_CODE (decl) == TEMPLATE_DECL)
11890 ;
11891 else
11892 {
11893 tree fn = NULL_TREE;
11894
11895 /* The standard does not explicitly indicate whether a name that
11896 names a set of overloaded declarations, some of which are
11897 templates, is a template-name. However, such a name should
11898 be a template-name; otherwise, there is no way to form a
11899 template-id for the overloaded templates. */
11900 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11901 if (TREE_CODE (fns) == OVERLOAD)
11902 for (fn = fns; fn; fn = OVL_NEXT (fn))
11903 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11904 break;
11905
11906 if (!fn)
11907 {
11908 /* The name does not name a template. */
11909 cp_parser_error (parser, "expected template-name");
11910 return error_mark_node;
11911 }
11912 }
11913
11914 /* If DECL is dependent, and refers to a function, then just return
11915 its name; we will look it up again during template instantiation. */
11916 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11917 {
11918 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11919 if (TYPE_P (scope) && dependent_type_p (scope))
11920 return identifier;
11921 }
11922
11923 return decl;
11924 }
11925
11926 /* Parse a template-argument-list.
11927
11928 template-argument-list:
11929 template-argument ... [opt]
11930 template-argument-list , template-argument ... [opt]
11931
11932 Returns a TREE_VEC containing the arguments. */
11933
11934 static tree
11935 cp_parser_template_argument_list (cp_parser* parser)
11936 {
11937 tree fixed_args[10];
11938 unsigned n_args = 0;
11939 unsigned alloced = 10;
11940 tree *arg_ary = fixed_args;
11941 tree vec;
11942 bool saved_in_template_argument_list_p;
11943 bool saved_ice_p;
11944 bool saved_non_ice_p;
11945
11946 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11947 parser->in_template_argument_list_p = true;
11948 /* Even if the template-id appears in an integral
11949 constant-expression, the contents of the argument list do
11950 not. */
11951 saved_ice_p = parser->integral_constant_expression_p;
11952 parser->integral_constant_expression_p = false;
11953 saved_non_ice_p = parser->non_integral_constant_expression_p;
11954 parser->non_integral_constant_expression_p = false;
11955 /* Parse the arguments. */
11956 do
11957 {
11958 tree argument;
11959
11960 if (n_args)
11961 /* Consume the comma. */
11962 cp_lexer_consume_token (parser->lexer);
11963
11964 /* Parse the template-argument. */
11965 argument = cp_parser_template_argument (parser);
11966
11967 /* If the next token is an ellipsis, we're expanding a template
11968 argument pack. */
11969 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11970 {
11971 if (argument == error_mark_node)
11972 {
11973 cp_token *token = cp_lexer_peek_token (parser->lexer);
11974 error_at (token->location,
11975 "expected parameter pack before %<...%>");
11976 }
11977 /* Consume the `...' token. */
11978 cp_lexer_consume_token (parser->lexer);
11979
11980 /* Make the argument into a TYPE_PACK_EXPANSION or
11981 EXPR_PACK_EXPANSION. */
11982 argument = make_pack_expansion (argument);
11983 }
11984
11985 if (n_args == alloced)
11986 {
11987 alloced *= 2;
11988
11989 if (arg_ary == fixed_args)
11990 {
11991 arg_ary = XNEWVEC (tree, alloced);
11992 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11993 }
11994 else
11995 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11996 }
11997 arg_ary[n_args++] = argument;
11998 }
11999 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12000
12001 vec = make_tree_vec (n_args);
12002
12003 while (n_args--)
12004 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12005
12006 if (arg_ary != fixed_args)
12007 free (arg_ary);
12008 parser->non_integral_constant_expression_p = saved_non_ice_p;
12009 parser->integral_constant_expression_p = saved_ice_p;
12010 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12011 #ifdef ENABLE_CHECKING
12012 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12013 #endif
12014 return vec;
12015 }
12016
12017 /* Parse a template-argument.
12018
12019 template-argument:
12020 assignment-expression
12021 type-id
12022 id-expression
12023
12024 The representation is that of an assignment-expression, type-id, or
12025 id-expression -- except that the qualified id-expression is
12026 evaluated, so that the value returned is either a DECL or an
12027 OVERLOAD.
12028
12029 Although the standard says "assignment-expression", it forbids
12030 throw-expressions or assignments in the template argument.
12031 Therefore, we use "conditional-expression" instead. */
12032
12033 static tree
12034 cp_parser_template_argument (cp_parser* parser)
12035 {
12036 tree argument;
12037 bool template_p;
12038 bool address_p;
12039 bool maybe_type_id = false;
12040 cp_token *token = NULL, *argument_start_token = NULL;
12041 cp_id_kind idk;
12042
12043 /* There's really no way to know what we're looking at, so we just
12044 try each alternative in order.
12045
12046 [temp.arg]
12047
12048 In a template-argument, an ambiguity between a type-id and an
12049 expression is resolved to a type-id, regardless of the form of
12050 the corresponding template-parameter.
12051
12052 Therefore, we try a type-id first. */
12053 cp_parser_parse_tentatively (parser);
12054 argument = cp_parser_template_type_arg (parser);
12055 /* If there was no error parsing the type-id but the next token is a
12056 '>>', our behavior depends on which dialect of C++ we're
12057 parsing. In C++98, we probably found a typo for '> >'. But there
12058 are type-id which are also valid expressions. For instance:
12059
12060 struct X { int operator >> (int); };
12061 template <int V> struct Foo {};
12062 Foo<X () >> 5> r;
12063
12064 Here 'X()' is a valid type-id of a function type, but the user just
12065 wanted to write the expression "X() >> 5". Thus, we remember that we
12066 found a valid type-id, but we still try to parse the argument as an
12067 expression to see what happens.
12068
12069 In C++0x, the '>>' will be considered two separate '>'
12070 tokens. */
12071 if (!cp_parser_error_occurred (parser)
12072 && cxx_dialect == cxx98
12073 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12074 {
12075 maybe_type_id = true;
12076 cp_parser_abort_tentative_parse (parser);
12077 }
12078 else
12079 {
12080 /* If the next token isn't a `,' or a `>', then this argument wasn't
12081 really finished. This means that the argument is not a valid
12082 type-id. */
12083 if (!cp_parser_next_token_ends_template_argument_p (parser))
12084 cp_parser_error (parser, "expected template-argument");
12085 /* If that worked, we're done. */
12086 if (cp_parser_parse_definitely (parser))
12087 return argument;
12088 }
12089 /* We're still not sure what the argument will be. */
12090 cp_parser_parse_tentatively (parser);
12091 /* Try a template. */
12092 argument_start_token = cp_lexer_peek_token (parser->lexer);
12093 argument = cp_parser_id_expression (parser,
12094 /*template_keyword_p=*/false,
12095 /*check_dependency_p=*/true,
12096 &template_p,
12097 /*declarator_p=*/false,
12098 /*optional_p=*/false);
12099 /* If the next token isn't a `,' or a `>', then this argument wasn't
12100 really finished. */
12101 if (!cp_parser_next_token_ends_template_argument_p (parser))
12102 cp_parser_error (parser, "expected template-argument");
12103 if (!cp_parser_error_occurred (parser))
12104 {
12105 /* Figure out what is being referred to. If the id-expression
12106 was for a class template specialization, then we will have a
12107 TYPE_DECL at this point. There is no need to do name lookup
12108 at this point in that case. */
12109 if (TREE_CODE (argument) != TYPE_DECL)
12110 argument = cp_parser_lookup_name (parser, argument,
12111 none_type,
12112 /*is_template=*/template_p,
12113 /*is_namespace=*/false,
12114 /*check_dependency=*/true,
12115 /*ambiguous_decls=*/NULL,
12116 argument_start_token->location);
12117 if (TREE_CODE (argument) != TEMPLATE_DECL
12118 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12119 cp_parser_error (parser, "expected template-name");
12120 }
12121 if (cp_parser_parse_definitely (parser))
12122 return argument;
12123 /* It must be a non-type argument. There permitted cases are given
12124 in [temp.arg.nontype]:
12125
12126 -- an integral constant-expression of integral or enumeration
12127 type; or
12128
12129 -- the name of a non-type template-parameter; or
12130
12131 -- the name of an object or function with external linkage...
12132
12133 -- the address of an object or function with external linkage...
12134
12135 -- a pointer to member... */
12136 /* Look for a non-type template parameter. */
12137 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12138 {
12139 cp_parser_parse_tentatively (parser);
12140 argument = cp_parser_primary_expression (parser,
12141 /*address_p=*/false,
12142 /*cast_p=*/false,
12143 /*template_arg_p=*/true,
12144 &idk);
12145 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12146 || !cp_parser_next_token_ends_template_argument_p (parser))
12147 cp_parser_simulate_error (parser);
12148 if (cp_parser_parse_definitely (parser))
12149 return argument;
12150 }
12151
12152 /* If the next token is "&", the argument must be the address of an
12153 object or function with external linkage. */
12154 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12155 if (address_p)
12156 cp_lexer_consume_token (parser->lexer);
12157 /* See if we might have an id-expression. */
12158 token = cp_lexer_peek_token (parser->lexer);
12159 if (token->type == CPP_NAME
12160 || token->keyword == RID_OPERATOR
12161 || token->type == CPP_SCOPE
12162 || token->type == CPP_TEMPLATE_ID
12163 || token->type == CPP_NESTED_NAME_SPECIFIER)
12164 {
12165 cp_parser_parse_tentatively (parser);
12166 argument = cp_parser_primary_expression (parser,
12167 address_p,
12168 /*cast_p=*/false,
12169 /*template_arg_p=*/true,
12170 &idk);
12171 if (cp_parser_error_occurred (parser)
12172 || !cp_parser_next_token_ends_template_argument_p (parser))
12173 cp_parser_abort_tentative_parse (parser);
12174 else
12175 {
12176 tree probe;
12177
12178 if (TREE_CODE (argument) == INDIRECT_REF)
12179 {
12180 gcc_assert (REFERENCE_REF_P (argument));
12181 argument = TREE_OPERAND (argument, 0);
12182 }
12183
12184 /* If we're in a template, we represent a qualified-id referring
12185 to a static data member as a SCOPE_REF even if the scope isn't
12186 dependent so that we can check access control later. */
12187 probe = argument;
12188 if (TREE_CODE (probe) == SCOPE_REF)
12189 probe = TREE_OPERAND (probe, 1);
12190 if (TREE_CODE (probe) == VAR_DECL)
12191 {
12192 /* A variable without external linkage might still be a
12193 valid constant-expression, so no error is issued here
12194 if the external-linkage check fails. */
12195 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12196 cp_parser_simulate_error (parser);
12197 }
12198 else if (is_overloaded_fn (argument))
12199 /* All overloaded functions are allowed; if the external
12200 linkage test does not pass, an error will be issued
12201 later. */
12202 ;
12203 else if (address_p
12204 && (TREE_CODE (argument) == OFFSET_REF
12205 || TREE_CODE (argument) == SCOPE_REF))
12206 /* A pointer-to-member. */
12207 ;
12208 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12209 ;
12210 else
12211 cp_parser_simulate_error (parser);
12212
12213 if (cp_parser_parse_definitely (parser))
12214 {
12215 if (address_p)
12216 argument = build_x_unary_op (ADDR_EXPR, argument,
12217 tf_warning_or_error);
12218 return argument;
12219 }
12220 }
12221 }
12222 /* If the argument started with "&", there are no other valid
12223 alternatives at this point. */
12224 if (address_p)
12225 {
12226 cp_parser_error (parser, "invalid non-type template argument");
12227 return error_mark_node;
12228 }
12229
12230 /* If the argument wasn't successfully parsed as a type-id followed
12231 by '>>', the argument can only be a constant expression now.
12232 Otherwise, we try parsing the constant-expression tentatively,
12233 because the argument could really be a type-id. */
12234 if (maybe_type_id)
12235 cp_parser_parse_tentatively (parser);
12236 argument = cp_parser_constant_expression (parser,
12237 /*allow_non_constant_p=*/false,
12238 /*non_constant_p=*/NULL);
12239 argument = fold_non_dependent_expr (argument);
12240 if (!maybe_type_id)
12241 return argument;
12242 if (!cp_parser_next_token_ends_template_argument_p (parser))
12243 cp_parser_error (parser, "expected template-argument");
12244 if (cp_parser_parse_definitely (parser))
12245 return argument;
12246 /* We did our best to parse the argument as a non type-id, but that
12247 was the only alternative that matched (albeit with a '>' after
12248 it). We can assume it's just a typo from the user, and a
12249 diagnostic will then be issued. */
12250 return cp_parser_template_type_arg (parser);
12251 }
12252
12253 /* Parse an explicit-instantiation.
12254
12255 explicit-instantiation:
12256 template declaration
12257
12258 Although the standard says `declaration', what it really means is:
12259
12260 explicit-instantiation:
12261 template decl-specifier-seq [opt] declarator [opt] ;
12262
12263 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12264 supposed to be allowed. A defect report has been filed about this
12265 issue.
12266
12267 GNU Extension:
12268
12269 explicit-instantiation:
12270 storage-class-specifier template
12271 decl-specifier-seq [opt] declarator [opt] ;
12272 function-specifier template
12273 decl-specifier-seq [opt] declarator [opt] ; */
12274
12275 static void
12276 cp_parser_explicit_instantiation (cp_parser* parser)
12277 {
12278 int declares_class_or_enum;
12279 cp_decl_specifier_seq decl_specifiers;
12280 tree extension_specifier = NULL_TREE;
12281
12282 /* Look for an (optional) storage-class-specifier or
12283 function-specifier. */
12284 if (cp_parser_allow_gnu_extensions_p (parser))
12285 {
12286 extension_specifier
12287 = cp_parser_storage_class_specifier_opt (parser);
12288 if (!extension_specifier)
12289 extension_specifier
12290 = cp_parser_function_specifier_opt (parser,
12291 /*decl_specs=*/NULL);
12292 }
12293
12294 /* Look for the `template' keyword. */
12295 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12296 /* Let the front end know that we are processing an explicit
12297 instantiation. */
12298 begin_explicit_instantiation ();
12299 /* [temp.explicit] says that we are supposed to ignore access
12300 control while processing explicit instantiation directives. */
12301 push_deferring_access_checks (dk_no_check);
12302 /* Parse a decl-specifier-seq. */
12303 cp_parser_decl_specifier_seq (parser,
12304 CP_PARSER_FLAGS_OPTIONAL,
12305 &decl_specifiers,
12306 &declares_class_or_enum);
12307 /* If there was exactly one decl-specifier, and it declared a class,
12308 and there's no declarator, then we have an explicit type
12309 instantiation. */
12310 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12311 {
12312 tree type;
12313
12314 type = check_tag_decl (&decl_specifiers);
12315 /* Turn access control back on for names used during
12316 template instantiation. */
12317 pop_deferring_access_checks ();
12318 if (type)
12319 do_type_instantiation (type, extension_specifier,
12320 /*complain=*/tf_error);
12321 }
12322 else
12323 {
12324 cp_declarator *declarator;
12325 tree decl;
12326
12327 /* Parse the declarator. */
12328 declarator
12329 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12330 /*ctor_dtor_or_conv_p=*/NULL,
12331 /*parenthesized_p=*/NULL,
12332 /*member_p=*/false);
12333 if (declares_class_or_enum & 2)
12334 cp_parser_check_for_definition_in_return_type (declarator,
12335 decl_specifiers.type,
12336 decl_specifiers.type_location);
12337 if (declarator != cp_error_declarator)
12338 {
12339 if (decl_specifiers.specs[(int)ds_inline])
12340 permerror (input_location, "explicit instantiation shall not use"
12341 " %<inline%> specifier");
12342 if (decl_specifiers.specs[(int)ds_constexpr])
12343 permerror (input_location, "explicit instantiation shall not use"
12344 " %<constexpr%> specifier");
12345
12346 decl = grokdeclarator (declarator, &decl_specifiers,
12347 NORMAL, 0, &decl_specifiers.attributes);
12348 /* Turn access control back on for names used during
12349 template instantiation. */
12350 pop_deferring_access_checks ();
12351 /* Do the explicit instantiation. */
12352 do_decl_instantiation (decl, extension_specifier);
12353 }
12354 else
12355 {
12356 pop_deferring_access_checks ();
12357 /* Skip the body of the explicit instantiation. */
12358 cp_parser_skip_to_end_of_statement (parser);
12359 }
12360 }
12361 /* We're done with the instantiation. */
12362 end_explicit_instantiation ();
12363
12364 cp_parser_consume_semicolon_at_end_of_statement (parser);
12365 }
12366
12367 /* Parse an explicit-specialization.
12368
12369 explicit-specialization:
12370 template < > declaration
12371
12372 Although the standard says `declaration', what it really means is:
12373
12374 explicit-specialization:
12375 template <> decl-specifier [opt] init-declarator [opt] ;
12376 template <> function-definition
12377 template <> explicit-specialization
12378 template <> template-declaration */
12379
12380 static void
12381 cp_parser_explicit_specialization (cp_parser* parser)
12382 {
12383 bool need_lang_pop;
12384 cp_token *token = cp_lexer_peek_token (parser->lexer);
12385
12386 /* Look for the `template' keyword. */
12387 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12388 /* Look for the `<'. */
12389 cp_parser_require (parser, CPP_LESS, RT_LESS);
12390 /* Look for the `>'. */
12391 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12392 /* We have processed another parameter list. */
12393 ++parser->num_template_parameter_lists;
12394 /* [temp]
12395
12396 A template ... explicit specialization ... shall not have C
12397 linkage. */
12398 if (current_lang_name == lang_name_c)
12399 {
12400 error_at (token->location, "template specialization with C linkage");
12401 /* Give it C++ linkage to avoid confusing other parts of the
12402 front end. */
12403 push_lang_context (lang_name_cplusplus);
12404 need_lang_pop = true;
12405 }
12406 else
12407 need_lang_pop = false;
12408 /* Let the front end know that we are beginning a specialization. */
12409 if (!begin_specialization ())
12410 {
12411 end_specialization ();
12412 return;
12413 }
12414
12415 /* If the next keyword is `template', we need to figure out whether
12416 or not we're looking a template-declaration. */
12417 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12418 {
12419 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12420 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12421 cp_parser_template_declaration_after_export (parser,
12422 /*member_p=*/false);
12423 else
12424 cp_parser_explicit_specialization (parser);
12425 }
12426 else
12427 /* Parse the dependent declaration. */
12428 cp_parser_single_declaration (parser,
12429 /*checks=*/NULL,
12430 /*member_p=*/false,
12431 /*explicit_specialization_p=*/true,
12432 /*friend_p=*/NULL);
12433 /* We're done with the specialization. */
12434 end_specialization ();
12435 /* For the erroneous case of a template with C linkage, we pushed an
12436 implicit C++ linkage scope; exit that scope now. */
12437 if (need_lang_pop)
12438 pop_lang_context ();
12439 /* We're done with this parameter list. */
12440 --parser->num_template_parameter_lists;
12441 }
12442
12443 /* Parse a type-specifier.
12444
12445 type-specifier:
12446 simple-type-specifier
12447 class-specifier
12448 enum-specifier
12449 elaborated-type-specifier
12450 cv-qualifier
12451
12452 GNU Extension:
12453
12454 type-specifier:
12455 __complex__
12456
12457 Returns a representation of the type-specifier. For a
12458 class-specifier, enum-specifier, or elaborated-type-specifier, a
12459 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12460
12461 The parser flags FLAGS is used to control type-specifier parsing.
12462
12463 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12464 in a decl-specifier-seq.
12465
12466 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12467 class-specifier, enum-specifier, or elaborated-type-specifier, then
12468 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12469 if a type is declared; 2 if it is defined. Otherwise, it is set to
12470 zero.
12471
12472 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12473 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12474 is set to FALSE. */
12475
12476 static tree
12477 cp_parser_type_specifier (cp_parser* parser,
12478 cp_parser_flags flags,
12479 cp_decl_specifier_seq *decl_specs,
12480 bool is_declaration,
12481 int* declares_class_or_enum,
12482 bool* is_cv_qualifier)
12483 {
12484 tree type_spec = NULL_TREE;
12485 cp_token *token;
12486 enum rid keyword;
12487 cp_decl_spec ds = ds_last;
12488
12489 /* Assume this type-specifier does not declare a new type. */
12490 if (declares_class_or_enum)
12491 *declares_class_or_enum = 0;
12492 /* And that it does not specify a cv-qualifier. */
12493 if (is_cv_qualifier)
12494 *is_cv_qualifier = false;
12495 /* Peek at the next token. */
12496 token = cp_lexer_peek_token (parser->lexer);
12497
12498 /* If we're looking at a keyword, we can use that to guide the
12499 production we choose. */
12500 keyword = token->keyword;
12501 switch (keyword)
12502 {
12503 case RID_ENUM:
12504 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12505 goto elaborated_type_specifier;
12506
12507 /* Look for the enum-specifier. */
12508 type_spec = cp_parser_enum_specifier (parser);
12509 /* If that worked, we're done. */
12510 if (type_spec)
12511 {
12512 if (declares_class_or_enum)
12513 *declares_class_or_enum = 2;
12514 if (decl_specs)
12515 cp_parser_set_decl_spec_type (decl_specs,
12516 type_spec,
12517 token->location,
12518 /*user_defined_p=*/true);
12519 return type_spec;
12520 }
12521 else
12522 goto elaborated_type_specifier;
12523
12524 /* Any of these indicate either a class-specifier, or an
12525 elaborated-type-specifier. */
12526 case RID_CLASS:
12527 case RID_STRUCT:
12528 case RID_UNION:
12529 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12530 goto elaborated_type_specifier;
12531
12532 /* Parse tentatively so that we can back up if we don't find a
12533 class-specifier. */
12534 cp_parser_parse_tentatively (parser);
12535 /* Look for the class-specifier. */
12536 type_spec = cp_parser_class_specifier (parser);
12537 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12538 /* If that worked, we're done. */
12539 if (cp_parser_parse_definitely (parser))
12540 {
12541 if (declares_class_or_enum)
12542 *declares_class_or_enum = 2;
12543 if (decl_specs)
12544 cp_parser_set_decl_spec_type (decl_specs,
12545 type_spec,
12546 token->location,
12547 /*user_defined_p=*/true);
12548 return type_spec;
12549 }
12550
12551 /* Fall through. */
12552 elaborated_type_specifier:
12553 /* We're declaring (not defining) a class or enum. */
12554 if (declares_class_or_enum)
12555 *declares_class_or_enum = 1;
12556
12557 /* Fall through. */
12558 case RID_TYPENAME:
12559 /* Look for an elaborated-type-specifier. */
12560 type_spec
12561 = (cp_parser_elaborated_type_specifier
12562 (parser,
12563 decl_specs && decl_specs->specs[(int) ds_friend],
12564 is_declaration));
12565 if (decl_specs)
12566 cp_parser_set_decl_spec_type (decl_specs,
12567 type_spec,
12568 token->location,
12569 /*user_defined_p=*/true);
12570 return type_spec;
12571
12572 case RID_CONST:
12573 ds = ds_const;
12574 if (is_cv_qualifier)
12575 *is_cv_qualifier = true;
12576 break;
12577
12578 case RID_VOLATILE:
12579 ds = ds_volatile;
12580 if (is_cv_qualifier)
12581 *is_cv_qualifier = true;
12582 break;
12583
12584 case RID_RESTRICT:
12585 ds = ds_restrict;
12586 if (is_cv_qualifier)
12587 *is_cv_qualifier = true;
12588 break;
12589
12590 case RID_COMPLEX:
12591 /* The `__complex__' keyword is a GNU extension. */
12592 ds = ds_complex;
12593 break;
12594
12595 default:
12596 break;
12597 }
12598
12599 /* Handle simple keywords. */
12600 if (ds != ds_last)
12601 {
12602 if (decl_specs)
12603 {
12604 ++decl_specs->specs[(int)ds];
12605 decl_specs->any_specifiers_p = true;
12606 }
12607 return cp_lexer_consume_token (parser->lexer)->u.value;
12608 }
12609
12610 /* If we do not already have a type-specifier, assume we are looking
12611 at a simple-type-specifier. */
12612 type_spec = cp_parser_simple_type_specifier (parser,
12613 decl_specs,
12614 flags);
12615
12616 /* If we didn't find a type-specifier, and a type-specifier was not
12617 optional in this context, issue an error message. */
12618 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12619 {
12620 cp_parser_error (parser, "expected type specifier");
12621 return error_mark_node;
12622 }
12623
12624 return type_spec;
12625 }
12626
12627 /* Parse a simple-type-specifier.
12628
12629 simple-type-specifier:
12630 :: [opt] nested-name-specifier [opt] type-name
12631 :: [opt] nested-name-specifier template template-id
12632 char
12633 wchar_t
12634 bool
12635 short
12636 int
12637 long
12638 signed
12639 unsigned
12640 float
12641 double
12642 void
12643
12644 C++0x Extension:
12645
12646 simple-type-specifier:
12647 auto
12648 decltype ( expression )
12649 char16_t
12650 char32_t
12651
12652 GNU Extension:
12653
12654 simple-type-specifier:
12655 __int128
12656 __typeof__ unary-expression
12657 __typeof__ ( type-id )
12658
12659 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12660 appropriately updated. */
12661
12662 static tree
12663 cp_parser_simple_type_specifier (cp_parser* parser,
12664 cp_decl_specifier_seq *decl_specs,
12665 cp_parser_flags flags)
12666 {
12667 tree type = NULL_TREE;
12668 cp_token *token;
12669
12670 /* Peek at the next token. */
12671 token = cp_lexer_peek_token (parser->lexer);
12672
12673 /* If we're looking at a keyword, things are easy. */
12674 switch (token->keyword)
12675 {
12676 case RID_CHAR:
12677 if (decl_specs)
12678 decl_specs->explicit_char_p = true;
12679 type = char_type_node;
12680 break;
12681 case RID_CHAR16:
12682 type = char16_type_node;
12683 break;
12684 case RID_CHAR32:
12685 type = char32_type_node;
12686 break;
12687 case RID_WCHAR:
12688 type = wchar_type_node;
12689 break;
12690 case RID_BOOL:
12691 type = boolean_type_node;
12692 break;
12693 case RID_SHORT:
12694 if (decl_specs)
12695 ++decl_specs->specs[(int) ds_short];
12696 type = short_integer_type_node;
12697 break;
12698 case RID_INT:
12699 if (decl_specs)
12700 decl_specs->explicit_int_p = true;
12701 type = integer_type_node;
12702 break;
12703 case RID_INT128:
12704 if (!int128_integer_type_node)
12705 break;
12706 if (decl_specs)
12707 decl_specs->explicit_int128_p = true;
12708 type = int128_integer_type_node;
12709 break;
12710 case RID_LONG:
12711 if (decl_specs)
12712 ++decl_specs->specs[(int) ds_long];
12713 type = long_integer_type_node;
12714 break;
12715 case RID_SIGNED:
12716 if (decl_specs)
12717 ++decl_specs->specs[(int) ds_signed];
12718 type = integer_type_node;
12719 break;
12720 case RID_UNSIGNED:
12721 if (decl_specs)
12722 ++decl_specs->specs[(int) ds_unsigned];
12723 type = unsigned_type_node;
12724 break;
12725 case RID_FLOAT:
12726 type = float_type_node;
12727 break;
12728 case RID_DOUBLE:
12729 type = double_type_node;
12730 break;
12731 case RID_VOID:
12732 type = void_type_node;
12733 break;
12734
12735 case RID_AUTO:
12736 maybe_warn_cpp0x (CPP0X_AUTO);
12737 type = make_auto ();
12738 break;
12739
12740 case RID_DECLTYPE:
12741 /* Parse the `decltype' type. */
12742 type = cp_parser_decltype (parser);
12743
12744 if (decl_specs)
12745 cp_parser_set_decl_spec_type (decl_specs, type,
12746 token->location,
12747 /*user_defined_p=*/true);
12748
12749 return type;
12750
12751 case RID_TYPEOF:
12752 /* Consume the `typeof' token. */
12753 cp_lexer_consume_token (parser->lexer);
12754 /* Parse the operand to `typeof'. */
12755 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12756 /* If it is not already a TYPE, take its type. */
12757 if (!TYPE_P (type))
12758 type = finish_typeof (type);
12759
12760 if (decl_specs)
12761 cp_parser_set_decl_spec_type (decl_specs, type,
12762 token->location,
12763 /*user_defined_p=*/true);
12764
12765 return type;
12766
12767 default:
12768 break;
12769 }
12770
12771 /* If the type-specifier was for a built-in type, we're done. */
12772 if (type)
12773 {
12774 /* Record the type. */
12775 if (decl_specs
12776 && (token->keyword != RID_SIGNED
12777 && token->keyword != RID_UNSIGNED
12778 && token->keyword != RID_SHORT
12779 && token->keyword != RID_LONG))
12780 cp_parser_set_decl_spec_type (decl_specs,
12781 type,
12782 token->location,
12783 /*user_defined=*/false);
12784 if (decl_specs)
12785 decl_specs->any_specifiers_p = true;
12786
12787 /* Consume the token. */
12788 cp_lexer_consume_token (parser->lexer);
12789
12790 /* There is no valid C++ program where a non-template type is
12791 followed by a "<". That usually indicates that the user thought
12792 that the type was a template. */
12793 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12794
12795 return TYPE_NAME (type);
12796 }
12797
12798 /* The type-specifier must be a user-defined type. */
12799 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12800 {
12801 bool qualified_p;
12802 bool global_p;
12803
12804 /* Don't gobble tokens or issue error messages if this is an
12805 optional type-specifier. */
12806 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12807 cp_parser_parse_tentatively (parser);
12808
12809 /* Look for the optional `::' operator. */
12810 global_p
12811 = (cp_parser_global_scope_opt (parser,
12812 /*current_scope_valid_p=*/false)
12813 != NULL_TREE);
12814 /* Look for the nested-name specifier. */
12815 qualified_p
12816 = (cp_parser_nested_name_specifier_opt (parser,
12817 /*typename_keyword_p=*/false,
12818 /*check_dependency_p=*/true,
12819 /*type_p=*/false,
12820 /*is_declaration=*/false)
12821 != NULL_TREE);
12822 token = cp_lexer_peek_token (parser->lexer);
12823 /* If we have seen a nested-name-specifier, and the next token
12824 is `template', then we are using the template-id production. */
12825 if (parser->scope
12826 && cp_parser_optional_template_keyword (parser))
12827 {
12828 /* Look for the template-id. */
12829 type = cp_parser_template_id (parser,
12830 /*template_keyword_p=*/true,
12831 /*check_dependency_p=*/true,
12832 /*is_declaration=*/false);
12833 /* If the template-id did not name a type, we are out of
12834 luck. */
12835 if (TREE_CODE (type) != TYPE_DECL)
12836 {
12837 cp_parser_error (parser, "expected template-id for type");
12838 type = NULL_TREE;
12839 }
12840 }
12841 /* Otherwise, look for a type-name. */
12842 else
12843 type = cp_parser_type_name (parser);
12844 /* Keep track of all name-lookups performed in class scopes. */
12845 if (type
12846 && !global_p
12847 && !qualified_p
12848 && TREE_CODE (type) == TYPE_DECL
12849 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12850 maybe_note_name_used_in_class (DECL_NAME (type), type);
12851 /* If it didn't work out, we don't have a TYPE. */
12852 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12853 && !cp_parser_parse_definitely (parser))
12854 type = NULL_TREE;
12855 if (type && decl_specs)
12856 cp_parser_set_decl_spec_type (decl_specs, type,
12857 token->location,
12858 /*user_defined=*/true);
12859 }
12860
12861 /* If we didn't get a type-name, issue an error message. */
12862 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12863 {
12864 cp_parser_error (parser, "expected type-name");
12865 return error_mark_node;
12866 }
12867
12868 if (type && type != error_mark_node)
12869 {
12870 /* See if TYPE is an Objective-C type, and if so, parse and
12871 accept any protocol references following it. Do this before
12872 the cp_parser_check_for_invalid_template_id() call, because
12873 Objective-C types can be followed by '<...>' which would
12874 enclose protocol names rather than template arguments, and so
12875 everything is fine. */
12876 if (c_dialect_objc () && !parser->scope
12877 && (objc_is_id (type) || objc_is_class_name (type)))
12878 {
12879 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12880 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12881
12882 /* Clobber the "unqualified" type previously entered into
12883 DECL_SPECS with the new, improved protocol-qualified version. */
12884 if (decl_specs)
12885 decl_specs->type = qual_type;
12886
12887 return qual_type;
12888 }
12889
12890 /* There is no valid C++ program where a non-template type is
12891 followed by a "<". That usually indicates that the user
12892 thought that the type was a template. */
12893 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12894 token->location);
12895 }
12896
12897 return type;
12898 }
12899
12900 /* Parse a type-name.
12901
12902 type-name:
12903 class-name
12904 enum-name
12905 typedef-name
12906
12907 enum-name:
12908 identifier
12909
12910 typedef-name:
12911 identifier
12912
12913 Returns a TYPE_DECL for the type. */
12914
12915 static tree
12916 cp_parser_type_name (cp_parser* parser)
12917 {
12918 tree type_decl;
12919
12920 /* We can't know yet whether it is a class-name or not. */
12921 cp_parser_parse_tentatively (parser);
12922 /* Try a class-name. */
12923 type_decl = cp_parser_class_name (parser,
12924 /*typename_keyword_p=*/false,
12925 /*template_keyword_p=*/false,
12926 none_type,
12927 /*check_dependency_p=*/true,
12928 /*class_head_p=*/false,
12929 /*is_declaration=*/false);
12930 /* If it's not a class-name, keep looking. */
12931 if (!cp_parser_parse_definitely (parser))
12932 {
12933 /* It must be a typedef-name or an enum-name. */
12934 return cp_parser_nonclass_name (parser);
12935 }
12936
12937 return type_decl;
12938 }
12939
12940 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12941
12942 enum-name:
12943 identifier
12944
12945 typedef-name:
12946 identifier
12947
12948 Returns a TYPE_DECL for the type. */
12949
12950 static tree
12951 cp_parser_nonclass_name (cp_parser* parser)
12952 {
12953 tree type_decl;
12954 tree identifier;
12955
12956 cp_token *token = cp_lexer_peek_token (parser->lexer);
12957 identifier = cp_parser_identifier (parser);
12958 if (identifier == error_mark_node)
12959 return error_mark_node;
12960
12961 /* Look up the type-name. */
12962 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12963
12964 if (TREE_CODE (type_decl) != TYPE_DECL
12965 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12966 {
12967 /* See if this is an Objective-C type. */
12968 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12969 tree type = objc_get_protocol_qualified_type (identifier, protos);
12970 if (type)
12971 type_decl = TYPE_NAME (type);
12972 }
12973
12974 /* Issue an error if we did not find a type-name. */
12975 if (TREE_CODE (type_decl) != TYPE_DECL
12976 /* In Objective-C, we have the complication that class names are
12977 normally type names and start declarations (eg, the
12978 "NSObject" in "NSObject *object;"), but can be used in an
12979 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12980 is an expression. So, a classname followed by a dot is not a
12981 valid type-name. */
12982 || (objc_is_class_name (TREE_TYPE (type_decl))
12983 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12984 {
12985 if (!cp_parser_simulate_error (parser))
12986 cp_parser_name_lookup_error (parser, identifier, type_decl,
12987 NLE_TYPE, token->location);
12988 return error_mark_node;
12989 }
12990 /* Remember that the name was used in the definition of the
12991 current class so that we can check later to see if the
12992 meaning would have been different after the class was
12993 entirely defined. */
12994 else if (type_decl != error_mark_node
12995 && !parser->scope)
12996 maybe_note_name_used_in_class (identifier, type_decl);
12997
12998 return type_decl;
12999 }
13000
13001 /* Parse an elaborated-type-specifier. Note that the grammar given
13002 here incorporates the resolution to DR68.
13003
13004 elaborated-type-specifier:
13005 class-key :: [opt] nested-name-specifier [opt] identifier
13006 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13007 enum-key :: [opt] nested-name-specifier [opt] identifier
13008 typename :: [opt] nested-name-specifier identifier
13009 typename :: [opt] nested-name-specifier template [opt]
13010 template-id
13011
13012 GNU extension:
13013
13014 elaborated-type-specifier:
13015 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13016 class-key attributes :: [opt] nested-name-specifier [opt]
13017 template [opt] template-id
13018 enum attributes :: [opt] nested-name-specifier [opt] identifier
13019
13020 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13021 declared `friend'. If IS_DECLARATION is TRUE, then this
13022 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13023 something is being declared.
13024
13025 Returns the TYPE specified. */
13026
13027 static tree
13028 cp_parser_elaborated_type_specifier (cp_parser* parser,
13029 bool is_friend,
13030 bool is_declaration)
13031 {
13032 enum tag_types tag_type;
13033 tree identifier;
13034 tree type = NULL_TREE;
13035 tree attributes = NULL_TREE;
13036 tree globalscope;
13037 cp_token *token = NULL;
13038
13039 /* See if we're looking at the `enum' keyword. */
13040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13041 {
13042 /* Consume the `enum' token. */
13043 cp_lexer_consume_token (parser->lexer);
13044 /* Remember that it's an enumeration type. */
13045 tag_type = enum_type;
13046 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13047 enums) is used here. */
13048 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13049 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13050 {
13051 pedwarn (input_location, 0, "elaborated-type-specifier "
13052 "for a scoped enum must not use the %<%D%> keyword",
13053 cp_lexer_peek_token (parser->lexer)->u.value);
13054 /* Consume the `struct' or `class' and parse it anyway. */
13055 cp_lexer_consume_token (parser->lexer);
13056 }
13057 /* Parse the attributes. */
13058 attributes = cp_parser_attributes_opt (parser);
13059 }
13060 /* Or, it might be `typename'. */
13061 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13062 RID_TYPENAME))
13063 {
13064 /* Consume the `typename' token. */
13065 cp_lexer_consume_token (parser->lexer);
13066 /* Remember that it's a `typename' type. */
13067 tag_type = typename_type;
13068 }
13069 /* Otherwise it must be a class-key. */
13070 else
13071 {
13072 tag_type = cp_parser_class_key (parser);
13073 if (tag_type == none_type)
13074 return error_mark_node;
13075 /* Parse the attributes. */
13076 attributes = cp_parser_attributes_opt (parser);
13077 }
13078
13079 /* Look for the `::' operator. */
13080 globalscope = cp_parser_global_scope_opt (parser,
13081 /*current_scope_valid_p=*/false);
13082 /* Look for the nested-name-specifier. */
13083 if (tag_type == typename_type && !globalscope)
13084 {
13085 if (!cp_parser_nested_name_specifier (parser,
13086 /*typename_keyword_p=*/true,
13087 /*check_dependency_p=*/true,
13088 /*type_p=*/true,
13089 is_declaration))
13090 return error_mark_node;
13091 }
13092 else
13093 /* Even though `typename' is not present, the proposed resolution
13094 to Core Issue 180 says that in `class A<T>::B', `B' should be
13095 considered a type-name, even if `A<T>' is dependent. */
13096 cp_parser_nested_name_specifier_opt (parser,
13097 /*typename_keyword_p=*/true,
13098 /*check_dependency_p=*/true,
13099 /*type_p=*/true,
13100 is_declaration);
13101 /* For everything but enumeration types, consider a template-id.
13102 For an enumeration type, consider only a plain identifier. */
13103 if (tag_type != enum_type)
13104 {
13105 bool template_p = false;
13106 tree decl;
13107
13108 /* Allow the `template' keyword. */
13109 template_p = cp_parser_optional_template_keyword (parser);
13110 /* If we didn't see `template', we don't know if there's a
13111 template-id or not. */
13112 if (!template_p)
13113 cp_parser_parse_tentatively (parser);
13114 /* Parse the template-id. */
13115 token = cp_lexer_peek_token (parser->lexer);
13116 decl = cp_parser_template_id (parser, template_p,
13117 /*check_dependency_p=*/true,
13118 is_declaration);
13119 /* If we didn't find a template-id, look for an ordinary
13120 identifier. */
13121 if (!template_p && !cp_parser_parse_definitely (parser))
13122 ;
13123 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13124 in effect, then we must assume that, upon instantiation, the
13125 template will correspond to a class. */
13126 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13127 && tag_type == typename_type)
13128 type = make_typename_type (parser->scope, decl,
13129 typename_type,
13130 /*complain=*/tf_error);
13131 /* If the `typename' keyword is in effect and DECL is not a type
13132 decl. Then type is non existant. */
13133 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13134 type = NULL_TREE;
13135 else
13136 type = TREE_TYPE (decl);
13137 }
13138
13139 if (!type)
13140 {
13141 token = cp_lexer_peek_token (parser->lexer);
13142 identifier = cp_parser_identifier (parser);
13143
13144 if (identifier == error_mark_node)
13145 {
13146 parser->scope = NULL_TREE;
13147 return error_mark_node;
13148 }
13149
13150 /* For a `typename', we needn't call xref_tag. */
13151 if (tag_type == typename_type
13152 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13153 return cp_parser_make_typename_type (parser, parser->scope,
13154 identifier,
13155 token->location);
13156 /* Look up a qualified name in the usual way. */
13157 if (parser->scope)
13158 {
13159 tree decl;
13160 tree ambiguous_decls;
13161
13162 decl = cp_parser_lookup_name (parser, identifier,
13163 tag_type,
13164 /*is_template=*/false,
13165 /*is_namespace=*/false,
13166 /*check_dependency=*/true,
13167 &ambiguous_decls,
13168 token->location);
13169
13170 /* If the lookup was ambiguous, an error will already have been
13171 issued. */
13172 if (ambiguous_decls)
13173 return error_mark_node;
13174
13175 /* If we are parsing friend declaration, DECL may be a
13176 TEMPLATE_DECL tree node here. However, we need to check
13177 whether this TEMPLATE_DECL results in valid code. Consider
13178 the following example:
13179
13180 namespace N {
13181 template <class T> class C {};
13182 }
13183 class X {
13184 template <class T> friend class N::C; // #1, valid code
13185 };
13186 template <class T> class Y {
13187 friend class N::C; // #2, invalid code
13188 };
13189
13190 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13191 name lookup of `N::C'. We see that friend declaration must
13192 be template for the code to be valid. Note that
13193 processing_template_decl does not work here since it is
13194 always 1 for the above two cases. */
13195
13196 decl = (cp_parser_maybe_treat_template_as_class
13197 (decl, /*tag_name_p=*/is_friend
13198 && parser->num_template_parameter_lists));
13199
13200 if (TREE_CODE (decl) != TYPE_DECL)
13201 {
13202 cp_parser_diagnose_invalid_type_name (parser,
13203 parser->scope,
13204 identifier,
13205 token->location);
13206 return error_mark_node;
13207 }
13208
13209 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13210 {
13211 bool allow_template = (parser->num_template_parameter_lists
13212 || DECL_SELF_REFERENCE_P (decl));
13213 type = check_elaborated_type_specifier (tag_type, decl,
13214 allow_template);
13215
13216 if (type == error_mark_node)
13217 return error_mark_node;
13218 }
13219
13220 /* Forward declarations of nested types, such as
13221
13222 class C1::C2;
13223 class C1::C2::C3;
13224
13225 are invalid unless all components preceding the final '::'
13226 are complete. If all enclosing types are complete, these
13227 declarations become merely pointless.
13228
13229 Invalid forward declarations of nested types are errors
13230 caught elsewhere in parsing. Those that are pointless arrive
13231 here. */
13232
13233 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13234 && !is_friend && !processing_explicit_instantiation)
13235 warning (0, "declaration %qD does not declare anything", decl);
13236
13237 type = TREE_TYPE (decl);
13238 }
13239 else
13240 {
13241 /* An elaborated-type-specifier sometimes introduces a new type and
13242 sometimes names an existing type. Normally, the rule is that it
13243 introduces a new type only if there is not an existing type of
13244 the same name already in scope. For example, given:
13245
13246 struct S {};
13247 void f() { struct S s; }
13248
13249 the `struct S' in the body of `f' is the same `struct S' as in
13250 the global scope; the existing definition is used. However, if
13251 there were no global declaration, this would introduce a new
13252 local class named `S'.
13253
13254 An exception to this rule applies to the following code:
13255
13256 namespace N { struct S; }
13257
13258 Here, the elaborated-type-specifier names a new type
13259 unconditionally; even if there is already an `S' in the
13260 containing scope this declaration names a new type.
13261 This exception only applies if the elaborated-type-specifier
13262 forms the complete declaration:
13263
13264 [class.name]
13265
13266 A declaration consisting solely of `class-key identifier ;' is
13267 either a redeclaration of the name in the current scope or a
13268 forward declaration of the identifier as a class name. It
13269 introduces the name into the current scope.
13270
13271 We are in this situation precisely when the next token is a `;'.
13272
13273 An exception to the exception is that a `friend' declaration does
13274 *not* name a new type; i.e., given:
13275
13276 struct S { friend struct T; };
13277
13278 `T' is not a new type in the scope of `S'.
13279
13280 Also, `new struct S' or `sizeof (struct S)' never results in the
13281 definition of a new type; a new type can only be declared in a
13282 declaration context. */
13283
13284 tag_scope ts;
13285 bool template_p;
13286
13287 if (is_friend)
13288 /* Friends have special name lookup rules. */
13289 ts = ts_within_enclosing_non_class;
13290 else if (is_declaration
13291 && cp_lexer_next_token_is (parser->lexer,
13292 CPP_SEMICOLON))
13293 /* This is a `class-key identifier ;' */
13294 ts = ts_current;
13295 else
13296 ts = ts_global;
13297
13298 template_p =
13299 (parser->num_template_parameter_lists
13300 && (cp_parser_next_token_starts_class_definition_p (parser)
13301 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13302 /* An unqualified name was used to reference this type, so
13303 there were no qualifying templates. */
13304 if (!cp_parser_check_template_parameters (parser,
13305 /*num_templates=*/0,
13306 token->location,
13307 /*declarator=*/NULL))
13308 return error_mark_node;
13309 type = xref_tag (tag_type, identifier, ts, template_p);
13310 }
13311 }
13312
13313 if (type == error_mark_node)
13314 return error_mark_node;
13315
13316 /* Allow attributes on forward declarations of classes. */
13317 if (attributes)
13318 {
13319 if (TREE_CODE (type) == TYPENAME_TYPE)
13320 warning (OPT_Wattributes,
13321 "attributes ignored on uninstantiated type");
13322 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13323 && ! processing_explicit_instantiation)
13324 warning (OPT_Wattributes,
13325 "attributes ignored on template instantiation");
13326 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13327 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13328 else
13329 warning (OPT_Wattributes,
13330 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13331 }
13332
13333 if (tag_type != enum_type)
13334 cp_parser_check_class_key (tag_type, type);
13335
13336 /* A "<" cannot follow an elaborated type specifier. If that
13337 happens, the user was probably trying to form a template-id. */
13338 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13339
13340 return type;
13341 }
13342
13343 /* Parse an enum-specifier.
13344
13345 enum-specifier:
13346 enum-head { enumerator-list [opt] }
13347
13348 enum-head:
13349 enum-key identifier [opt] enum-base [opt]
13350 enum-key nested-name-specifier identifier enum-base [opt]
13351
13352 enum-key:
13353 enum
13354 enum class [C++0x]
13355 enum struct [C++0x]
13356
13357 enum-base: [C++0x]
13358 : type-specifier-seq
13359
13360 opaque-enum-specifier:
13361 enum-key identifier enum-base [opt] ;
13362
13363 GNU Extensions:
13364 enum-key attributes[opt] identifier [opt] enum-base [opt]
13365 { enumerator-list [opt] }attributes[opt]
13366
13367 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13368 if the token stream isn't an enum-specifier after all. */
13369
13370 static tree
13371 cp_parser_enum_specifier (cp_parser* parser)
13372 {
13373 tree identifier;
13374 tree type = NULL_TREE;
13375 tree prev_scope;
13376 tree nested_name_specifier = NULL_TREE;
13377 tree attributes;
13378 bool scoped_enum_p = false;
13379 bool has_underlying_type = false;
13380 bool nested_being_defined = false;
13381 bool new_value_list = false;
13382 bool is_new_type = false;
13383 bool is_anonymous = false;
13384 tree underlying_type = NULL_TREE;
13385 cp_token *type_start_token = NULL;
13386 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13387
13388 parser->colon_corrects_to_scope_p = false;
13389
13390 /* Parse tentatively so that we can back up if we don't find a
13391 enum-specifier. */
13392 cp_parser_parse_tentatively (parser);
13393
13394 /* Caller guarantees that the current token is 'enum', an identifier
13395 possibly follows, and the token after that is an opening brace.
13396 If we don't have an identifier, fabricate an anonymous name for
13397 the enumeration being defined. */
13398 cp_lexer_consume_token (parser->lexer);
13399
13400 /* Parse the "class" or "struct", which indicates a scoped
13401 enumeration type in C++0x. */
13402 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13403 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13404 {
13405 if (cxx_dialect < cxx0x)
13406 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13407
13408 /* Consume the `struct' or `class' token. */
13409 cp_lexer_consume_token (parser->lexer);
13410
13411 scoped_enum_p = true;
13412 }
13413
13414 attributes = cp_parser_attributes_opt (parser);
13415
13416 /* Clear the qualification. */
13417 parser->scope = NULL_TREE;
13418 parser->qualifying_scope = NULL_TREE;
13419 parser->object_scope = NULL_TREE;
13420
13421 /* Figure out in what scope the declaration is being placed. */
13422 prev_scope = current_scope ();
13423
13424 type_start_token = cp_lexer_peek_token (parser->lexer);
13425
13426 push_deferring_access_checks (dk_no_check);
13427 nested_name_specifier
13428 = cp_parser_nested_name_specifier_opt (parser,
13429 /*typename_keyword_p=*/true,
13430 /*check_dependency_p=*/false,
13431 /*type_p=*/false,
13432 /*is_declaration=*/false);
13433
13434 if (nested_name_specifier)
13435 {
13436 tree name;
13437
13438 identifier = cp_parser_identifier (parser);
13439 name = cp_parser_lookup_name (parser, identifier,
13440 enum_type,
13441 /*is_template=*/false,
13442 /*is_namespace=*/false,
13443 /*check_dependency=*/true,
13444 /*ambiguous_decls=*/NULL,
13445 input_location);
13446 if (name)
13447 {
13448 type = TREE_TYPE (name);
13449 if (TREE_CODE (type) == TYPENAME_TYPE)
13450 {
13451 /* Are template enums allowed in ISO? */
13452 if (template_parm_scope_p ())
13453 pedwarn (type_start_token->location, OPT_pedantic,
13454 "%qD is an enumeration template", name);
13455 /* ignore a typename reference, for it will be solved by name
13456 in start_enum. */
13457 type = NULL_TREE;
13458 }
13459 }
13460 else
13461 error_at (type_start_token->location,
13462 "%qD is not an enumerator-name", identifier);
13463 }
13464 else
13465 {
13466 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13467 identifier = cp_parser_identifier (parser);
13468 else
13469 {
13470 identifier = make_anon_name ();
13471 is_anonymous = true;
13472 }
13473 }
13474 pop_deferring_access_checks ();
13475
13476 /* Check for the `:' that denotes a specified underlying type in C++0x.
13477 Note that a ':' could also indicate a bitfield width, however. */
13478 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13479 {
13480 cp_decl_specifier_seq type_specifiers;
13481
13482 /* Consume the `:'. */
13483 cp_lexer_consume_token (parser->lexer);
13484
13485 /* Parse the type-specifier-seq. */
13486 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13487 /*is_trailing_return=*/false,
13488 &type_specifiers);
13489
13490 /* At this point this is surely not elaborated type specifier. */
13491 if (!cp_parser_parse_definitely (parser))
13492 return NULL_TREE;
13493
13494 if (cxx_dialect < cxx0x)
13495 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13496
13497 has_underlying_type = true;
13498
13499 /* If that didn't work, stop. */
13500 if (type_specifiers.type != error_mark_node)
13501 {
13502 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13503 /*initialized=*/0, NULL);
13504 if (underlying_type == error_mark_node)
13505 underlying_type = NULL_TREE;
13506 }
13507 }
13508
13509 /* Look for the `{' but don't consume it yet. */
13510 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13511 {
13512 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13513 {
13514 cp_parser_error (parser, "expected %<{%>");
13515 if (has_underlying_type)
13516 {
13517 type = NULL_TREE;
13518 goto out;
13519 }
13520 }
13521 /* An opaque-enum-specifier must have a ';' here. */
13522 if ((scoped_enum_p || underlying_type)
13523 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13524 {
13525 cp_parser_error (parser, "expected %<;%> or %<{%>");
13526 if (has_underlying_type)
13527 {
13528 type = NULL_TREE;
13529 goto out;
13530 }
13531 }
13532 }
13533
13534 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13535 return NULL_TREE;
13536
13537 if (nested_name_specifier)
13538 {
13539 if (CLASS_TYPE_P (nested_name_specifier))
13540 {
13541 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13542 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13543 push_scope (nested_name_specifier);
13544 }
13545 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13546 {
13547 push_nested_namespace (nested_name_specifier);
13548 }
13549 }
13550
13551 /* Issue an error message if type-definitions are forbidden here. */
13552 if (!cp_parser_check_type_definition (parser))
13553 type = error_mark_node;
13554 else
13555 /* Create the new type. We do this before consuming the opening
13556 brace so the enum will be recorded as being on the line of its
13557 tag (or the 'enum' keyword, if there is no tag). */
13558 type = start_enum (identifier, type, underlying_type,
13559 scoped_enum_p, &is_new_type);
13560
13561 /* If the next token is not '{' it is an opaque-enum-specifier or an
13562 elaborated-type-specifier. */
13563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13564 {
13565 if (nested_name_specifier)
13566 {
13567 /* The following catches invalid code such as:
13568 enum class S<int>::E { A, B, C }; */
13569 if (!processing_specialization
13570 && CLASS_TYPE_P (nested_name_specifier)
13571 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13572 error_at (type_start_token->location, "cannot add an enumerator "
13573 "list to a template instantiation");
13574
13575 /* If that scope does not contain the scope in which the
13576 class was originally declared, the program is invalid. */
13577 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13578 {
13579 if (at_namespace_scope_p ())
13580 error_at (type_start_token->location,
13581 "declaration of %qD in namespace %qD which does not "
13582 "enclose %qD",
13583 type, prev_scope, nested_name_specifier);
13584 else
13585 error_at (type_start_token->location,
13586 "declaration of %qD in %qD which does not enclose %qD",
13587 type, prev_scope, nested_name_specifier);
13588 type = error_mark_node;
13589 }
13590 }
13591
13592 if (scoped_enum_p)
13593 begin_scope (sk_scoped_enum, type);
13594
13595 /* Consume the opening brace. */
13596 cp_lexer_consume_token (parser->lexer);
13597
13598 if (type == error_mark_node)
13599 ; /* Nothing to add */
13600 else if (OPAQUE_ENUM_P (type)
13601 || (cxx_dialect > cxx98 && processing_specialization))
13602 {
13603 new_value_list = true;
13604 SET_OPAQUE_ENUM_P (type, false);
13605 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13606 }
13607 else
13608 {
13609 error_at (type_start_token->location, "multiple definition of %q#T", type);
13610 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13611 "previous definition here");
13612 type = error_mark_node;
13613 }
13614
13615 if (type == error_mark_node)
13616 cp_parser_skip_to_end_of_block_or_statement (parser);
13617 /* If the next token is not '}', then there are some enumerators. */
13618 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13619 cp_parser_enumerator_list (parser, type);
13620
13621 /* Consume the final '}'. */
13622 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13623
13624 if (scoped_enum_p)
13625 finish_scope ();
13626 }
13627 else
13628 {
13629 /* If a ';' follows, then it is an opaque-enum-specifier
13630 and additional restrictions apply. */
13631 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13632 {
13633 if (is_anonymous)
13634 error_at (type_start_token->location,
13635 "opaque-enum-specifier without name");
13636 else if (nested_name_specifier)
13637 error_at (type_start_token->location,
13638 "opaque-enum-specifier must use a simple identifier");
13639 }
13640 }
13641
13642 /* Look for trailing attributes to apply to this enumeration, and
13643 apply them if appropriate. */
13644 if (cp_parser_allow_gnu_extensions_p (parser))
13645 {
13646 tree trailing_attr = cp_parser_attributes_opt (parser);
13647 trailing_attr = chainon (trailing_attr, attributes);
13648 cplus_decl_attributes (&type,
13649 trailing_attr,
13650 (int) ATTR_FLAG_TYPE_IN_PLACE);
13651 }
13652
13653 /* Finish up the enumeration. */
13654 if (type != error_mark_node)
13655 {
13656 if (new_value_list)
13657 finish_enum_value_list (type);
13658 if (is_new_type)
13659 finish_enum (type);
13660 }
13661
13662 if (nested_name_specifier)
13663 {
13664 if (CLASS_TYPE_P (nested_name_specifier))
13665 {
13666 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13667 pop_scope (nested_name_specifier);
13668 }
13669 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13670 {
13671 pop_nested_namespace (nested_name_specifier);
13672 }
13673 }
13674 out:
13675 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13676 return type;
13677 }
13678
13679 /* Parse an enumerator-list. The enumerators all have the indicated
13680 TYPE.
13681
13682 enumerator-list:
13683 enumerator-definition
13684 enumerator-list , enumerator-definition */
13685
13686 static void
13687 cp_parser_enumerator_list (cp_parser* parser, tree type)
13688 {
13689 while (true)
13690 {
13691 /* Parse an enumerator-definition. */
13692 cp_parser_enumerator_definition (parser, type);
13693
13694 /* If the next token is not a ',', we've reached the end of
13695 the list. */
13696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13697 break;
13698 /* Otherwise, consume the `,' and keep going. */
13699 cp_lexer_consume_token (parser->lexer);
13700 /* If the next token is a `}', there is a trailing comma. */
13701 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13702 {
13703 if (!in_system_header)
13704 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13705 break;
13706 }
13707 }
13708 }
13709
13710 /* Parse an enumerator-definition. The enumerator has the indicated
13711 TYPE.
13712
13713 enumerator-definition:
13714 enumerator
13715 enumerator = constant-expression
13716
13717 enumerator:
13718 identifier */
13719
13720 static void
13721 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13722 {
13723 tree identifier;
13724 tree value;
13725 location_t loc;
13726
13727 /* Save the input location because we are interested in the location
13728 of the identifier and not the location of the explicit value. */
13729 loc = cp_lexer_peek_token (parser->lexer)->location;
13730
13731 /* Look for the identifier. */
13732 identifier = cp_parser_identifier (parser);
13733 if (identifier == error_mark_node)
13734 return;
13735
13736 /* If the next token is an '=', then there is an explicit value. */
13737 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13738 {
13739 /* Consume the `=' token. */
13740 cp_lexer_consume_token (parser->lexer);
13741 /* Parse the value. */
13742 value = cp_parser_constant_expression (parser,
13743 /*allow_non_constant_p=*/false,
13744 NULL);
13745 }
13746 else
13747 value = NULL_TREE;
13748
13749 /* If we are processing a template, make sure the initializer of the
13750 enumerator doesn't contain any bare template parameter pack. */
13751 if (check_for_bare_parameter_packs (value))
13752 value = error_mark_node;
13753
13754 /* Create the enumerator. */
13755 build_enumerator (identifier, value, type, loc);
13756 }
13757
13758 /* Parse a namespace-name.
13759
13760 namespace-name:
13761 original-namespace-name
13762 namespace-alias
13763
13764 Returns the NAMESPACE_DECL for the namespace. */
13765
13766 static tree
13767 cp_parser_namespace_name (cp_parser* parser)
13768 {
13769 tree identifier;
13770 tree namespace_decl;
13771
13772 cp_token *token = cp_lexer_peek_token (parser->lexer);
13773
13774 /* Get the name of the namespace. */
13775 identifier = cp_parser_identifier (parser);
13776 if (identifier == error_mark_node)
13777 return error_mark_node;
13778
13779 /* Look up the identifier in the currently active scope. Look only
13780 for namespaces, due to:
13781
13782 [basic.lookup.udir]
13783
13784 When looking up a namespace-name in a using-directive or alias
13785 definition, only namespace names are considered.
13786
13787 And:
13788
13789 [basic.lookup.qual]
13790
13791 During the lookup of a name preceding the :: scope resolution
13792 operator, object, function, and enumerator names are ignored.
13793
13794 (Note that cp_parser_qualifying_entity only calls this
13795 function if the token after the name is the scope resolution
13796 operator.) */
13797 namespace_decl = cp_parser_lookup_name (parser, identifier,
13798 none_type,
13799 /*is_template=*/false,
13800 /*is_namespace=*/true,
13801 /*check_dependency=*/true,
13802 /*ambiguous_decls=*/NULL,
13803 token->location);
13804 /* If it's not a namespace, issue an error. */
13805 if (namespace_decl == error_mark_node
13806 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13807 {
13808 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13809 error_at (token->location, "%qD is not a namespace-name", identifier);
13810 cp_parser_error (parser, "expected namespace-name");
13811 namespace_decl = error_mark_node;
13812 }
13813
13814 return namespace_decl;
13815 }
13816
13817 /* Parse a namespace-definition.
13818
13819 namespace-definition:
13820 named-namespace-definition
13821 unnamed-namespace-definition
13822
13823 named-namespace-definition:
13824 original-namespace-definition
13825 extension-namespace-definition
13826
13827 original-namespace-definition:
13828 namespace identifier { namespace-body }
13829
13830 extension-namespace-definition:
13831 namespace original-namespace-name { namespace-body }
13832
13833 unnamed-namespace-definition:
13834 namespace { namespace-body } */
13835
13836 static void
13837 cp_parser_namespace_definition (cp_parser* parser)
13838 {
13839 tree identifier, attribs;
13840 bool has_visibility;
13841 bool is_inline;
13842
13843 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13844 {
13845 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13846 is_inline = true;
13847 cp_lexer_consume_token (parser->lexer);
13848 }
13849 else
13850 is_inline = false;
13851
13852 /* Look for the `namespace' keyword. */
13853 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13854
13855 /* Get the name of the namespace. We do not attempt to distinguish
13856 between an original-namespace-definition and an
13857 extension-namespace-definition at this point. The semantic
13858 analysis routines are responsible for that. */
13859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13860 identifier = cp_parser_identifier (parser);
13861 else
13862 identifier = NULL_TREE;
13863
13864 /* Parse any specified attributes. */
13865 attribs = cp_parser_attributes_opt (parser);
13866
13867 /* Look for the `{' to start the namespace. */
13868 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13869 /* Start the namespace. */
13870 push_namespace (identifier);
13871
13872 /* "inline namespace" is equivalent to a stub namespace definition
13873 followed by a strong using directive. */
13874 if (is_inline)
13875 {
13876 tree name_space = current_namespace;
13877 /* Set up namespace association. */
13878 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13879 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13880 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13881 /* Import the contents of the inline namespace. */
13882 pop_namespace ();
13883 do_using_directive (name_space);
13884 push_namespace (identifier);
13885 }
13886
13887 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13888
13889 /* Parse the body of the namespace. */
13890 cp_parser_namespace_body (parser);
13891
13892 if (has_visibility)
13893 pop_visibility (1);
13894
13895 /* Finish the namespace. */
13896 pop_namespace ();
13897 /* Look for the final `}'. */
13898 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13899 }
13900
13901 /* Parse a namespace-body.
13902
13903 namespace-body:
13904 declaration-seq [opt] */
13905
13906 static void
13907 cp_parser_namespace_body (cp_parser* parser)
13908 {
13909 cp_parser_declaration_seq_opt (parser);
13910 }
13911
13912 /* Parse a namespace-alias-definition.
13913
13914 namespace-alias-definition:
13915 namespace identifier = qualified-namespace-specifier ; */
13916
13917 static void
13918 cp_parser_namespace_alias_definition (cp_parser* parser)
13919 {
13920 tree identifier;
13921 tree namespace_specifier;
13922
13923 cp_token *token = cp_lexer_peek_token (parser->lexer);
13924
13925 /* Look for the `namespace' keyword. */
13926 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13927 /* Look for the identifier. */
13928 identifier = cp_parser_identifier (parser);
13929 if (identifier == error_mark_node)
13930 return;
13931 /* Look for the `=' token. */
13932 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13933 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13934 {
13935 error_at (token->location, "%<namespace%> definition is not allowed here");
13936 /* Skip the definition. */
13937 cp_lexer_consume_token (parser->lexer);
13938 if (cp_parser_skip_to_closing_brace (parser))
13939 cp_lexer_consume_token (parser->lexer);
13940 return;
13941 }
13942 cp_parser_require (parser, CPP_EQ, RT_EQ);
13943 /* Look for the qualified-namespace-specifier. */
13944 namespace_specifier
13945 = cp_parser_qualified_namespace_specifier (parser);
13946 /* Look for the `;' token. */
13947 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13948
13949 /* Register the alias in the symbol table. */
13950 do_namespace_alias (identifier, namespace_specifier);
13951 }
13952
13953 /* Parse a qualified-namespace-specifier.
13954
13955 qualified-namespace-specifier:
13956 :: [opt] nested-name-specifier [opt] namespace-name
13957
13958 Returns a NAMESPACE_DECL corresponding to the specified
13959 namespace. */
13960
13961 static tree
13962 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13963 {
13964 /* Look for the optional `::'. */
13965 cp_parser_global_scope_opt (parser,
13966 /*current_scope_valid_p=*/false);
13967
13968 /* Look for the optional nested-name-specifier. */
13969 cp_parser_nested_name_specifier_opt (parser,
13970 /*typename_keyword_p=*/false,
13971 /*check_dependency_p=*/true,
13972 /*type_p=*/false,
13973 /*is_declaration=*/true);
13974
13975 return cp_parser_namespace_name (parser);
13976 }
13977
13978 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13979 access declaration.
13980
13981 using-declaration:
13982 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13983 using :: unqualified-id ;
13984
13985 access-declaration:
13986 qualified-id ;
13987
13988 */
13989
13990 static bool
13991 cp_parser_using_declaration (cp_parser* parser,
13992 bool access_declaration_p)
13993 {
13994 cp_token *token;
13995 bool typename_p = false;
13996 bool global_scope_p;
13997 tree decl;
13998 tree identifier;
13999 tree qscope;
14000
14001 if (access_declaration_p)
14002 cp_parser_parse_tentatively (parser);
14003 else
14004 {
14005 /* Look for the `using' keyword. */
14006 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14007
14008 /* Peek at the next token. */
14009 token = cp_lexer_peek_token (parser->lexer);
14010 /* See if it's `typename'. */
14011 if (token->keyword == RID_TYPENAME)
14012 {
14013 /* Remember that we've seen it. */
14014 typename_p = true;
14015 /* Consume the `typename' token. */
14016 cp_lexer_consume_token (parser->lexer);
14017 }
14018 }
14019
14020 /* Look for the optional global scope qualification. */
14021 global_scope_p
14022 = (cp_parser_global_scope_opt (parser,
14023 /*current_scope_valid_p=*/false)
14024 != NULL_TREE);
14025
14026 /* If we saw `typename', or didn't see `::', then there must be a
14027 nested-name-specifier present. */
14028 if (typename_p || !global_scope_p)
14029 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14030 /*check_dependency_p=*/true,
14031 /*type_p=*/false,
14032 /*is_declaration=*/true);
14033 /* Otherwise, we could be in either of the two productions. In that
14034 case, treat the nested-name-specifier as optional. */
14035 else
14036 qscope = cp_parser_nested_name_specifier_opt (parser,
14037 /*typename_keyword_p=*/false,
14038 /*check_dependency_p=*/true,
14039 /*type_p=*/false,
14040 /*is_declaration=*/true);
14041 if (!qscope)
14042 qscope = global_namespace;
14043
14044 if (access_declaration_p && cp_parser_error_occurred (parser))
14045 /* Something has already gone wrong; there's no need to parse
14046 further. Since an error has occurred, the return value of
14047 cp_parser_parse_definitely will be false, as required. */
14048 return cp_parser_parse_definitely (parser);
14049
14050 token = cp_lexer_peek_token (parser->lexer);
14051 /* Parse the unqualified-id. */
14052 identifier = cp_parser_unqualified_id (parser,
14053 /*template_keyword_p=*/false,
14054 /*check_dependency_p=*/true,
14055 /*declarator_p=*/true,
14056 /*optional_p=*/false);
14057
14058 if (access_declaration_p)
14059 {
14060 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14061 cp_parser_simulate_error (parser);
14062 if (!cp_parser_parse_definitely (parser))
14063 return false;
14064 }
14065
14066 /* The function we call to handle a using-declaration is different
14067 depending on what scope we are in. */
14068 if (qscope == error_mark_node || identifier == error_mark_node)
14069 ;
14070 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14071 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14072 /* [namespace.udecl]
14073
14074 A using declaration shall not name a template-id. */
14075 error_at (token->location,
14076 "a template-id may not appear in a using-declaration");
14077 else
14078 {
14079 if (at_class_scope_p ())
14080 {
14081 /* Create the USING_DECL. */
14082 decl = do_class_using_decl (parser->scope, identifier);
14083
14084 if (check_for_bare_parameter_packs (decl))
14085 return false;
14086 else
14087 /* Add it to the list of members in this class. */
14088 finish_member_declaration (decl);
14089 }
14090 else
14091 {
14092 decl = cp_parser_lookup_name_simple (parser,
14093 identifier,
14094 token->location);
14095 if (decl == error_mark_node)
14096 cp_parser_name_lookup_error (parser, identifier,
14097 decl, NLE_NULL,
14098 token->location);
14099 else if (check_for_bare_parameter_packs (decl))
14100 return false;
14101 else if (!at_namespace_scope_p ())
14102 do_local_using_decl (decl, qscope, identifier);
14103 else
14104 do_toplevel_using_decl (decl, qscope, identifier);
14105 }
14106 }
14107
14108 /* Look for the final `;'. */
14109 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14110
14111 return true;
14112 }
14113
14114 /* Parse a using-directive.
14115
14116 using-directive:
14117 using namespace :: [opt] nested-name-specifier [opt]
14118 namespace-name ; */
14119
14120 static void
14121 cp_parser_using_directive (cp_parser* parser)
14122 {
14123 tree namespace_decl;
14124 tree attribs;
14125
14126 /* Look for the `using' keyword. */
14127 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14128 /* And the `namespace' keyword. */
14129 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14130 /* Look for the optional `::' operator. */
14131 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14132 /* And the optional nested-name-specifier. */
14133 cp_parser_nested_name_specifier_opt (parser,
14134 /*typename_keyword_p=*/false,
14135 /*check_dependency_p=*/true,
14136 /*type_p=*/false,
14137 /*is_declaration=*/true);
14138 /* Get the namespace being used. */
14139 namespace_decl = cp_parser_namespace_name (parser);
14140 /* And any specified attributes. */
14141 attribs = cp_parser_attributes_opt (parser);
14142 /* Update the symbol table. */
14143 parse_using_directive (namespace_decl, attribs);
14144 /* Look for the final `;'. */
14145 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14146 }
14147
14148 /* Parse an asm-definition.
14149
14150 asm-definition:
14151 asm ( string-literal ) ;
14152
14153 GNU Extension:
14154
14155 asm-definition:
14156 asm volatile [opt] ( string-literal ) ;
14157 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14158 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14159 : asm-operand-list [opt] ) ;
14160 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14161 : asm-operand-list [opt]
14162 : asm-clobber-list [opt] ) ;
14163 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14164 : asm-clobber-list [opt]
14165 : asm-goto-list ) ; */
14166
14167 static void
14168 cp_parser_asm_definition (cp_parser* parser)
14169 {
14170 tree string;
14171 tree outputs = NULL_TREE;
14172 tree inputs = NULL_TREE;
14173 tree clobbers = NULL_TREE;
14174 tree labels = NULL_TREE;
14175 tree asm_stmt;
14176 bool volatile_p = false;
14177 bool extended_p = false;
14178 bool invalid_inputs_p = false;
14179 bool invalid_outputs_p = false;
14180 bool goto_p = false;
14181 required_token missing = RT_NONE;
14182
14183 /* Look for the `asm' keyword. */
14184 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14185 /* See if the next token is `volatile'. */
14186 if (cp_parser_allow_gnu_extensions_p (parser)
14187 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14188 {
14189 /* Remember that we saw the `volatile' keyword. */
14190 volatile_p = true;
14191 /* Consume the token. */
14192 cp_lexer_consume_token (parser->lexer);
14193 }
14194 if (cp_parser_allow_gnu_extensions_p (parser)
14195 && parser->in_function_body
14196 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14197 {
14198 /* Remember that we saw the `goto' keyword. */
14199 goto_p = true;
14200 /* Consume the token. */
14201 cp_lexer_consume_token (parser->lexer);
14202 }
14203 /* Look for the opening `('. */
14204 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14205 return;
14206 /* Look for the string. */
14207 string = cp_parser_string_literal (parser, false, false);
14208 if (string == error_mark_node)
14209 {
14210 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14211 /*consume_paren=*/true);
14212 return;
14213 }
14214
14215 /* If we're allowing GNU extensions, check for the extended assembly
14216 syntax. Unfortunately, the `:' tokens need not be separated by
14217 a space in C, and so, for compatibility, we tolerate that here
14218 too. Doing that means that we have to treat the `::' operator as
14219 two `:' tokens. */
14220 if (cp_parser_allow_gnu_extensions_p (parser)
14221 && parser->in_function_body
14222 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14223 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14224 {
14225 bool inputs_p = false;
14226 bool clobbers_p = false;
14227 bool labels_p = false;
14228
14229 /* The extended syntax was used. */
14230 extended_p = true;
14231
14232 /* Look for outputs. */
14233 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14234 {
14235 /* Consume the `:'. */
14236 cp_lexer_consume_token (parser->lexer);
14237 /* Parse the output-operands. */
14238 if (cp_lexer_next_token_is_not (parser->lexer,
14239 CPP_COLON)
14240 && cp_lexer_next_token_is_not (parser->lexer,
14241 CPP_SCOPE)
14242 && cp_lexer_next_token_is_not (parser->lexer,
14243 CPP_CLOSE_PAREN)
14244 && !goto_p)
14245 outputs = cp_parser_asm_operand_list (parser);
14246
14247 if (outputs == error_mark_node)
14248 invalid_outputs_p = true;
14249 }
14250 /* If the next token is `::', there are no outputs, and the
14251 next token is the beginning of the inputs. */
14252 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14253 /* The inputs are coming next. */
14254 inputs_p = true;
14255
14256 /* Look for inputs. */
14257 if (inputs_p
14258 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14259 {
14260 /* Consume the `:' or `::'. */
14261 cp_lexer_consume_token (parser->lexer);
14262 /* Parse the output-operands. */
14263 if (cp_lexer_next_token_is_not (parser->lexer,
14264 CPP_COLON)
14265 && cp_lexer_next_token_is_not (parser->lexer,
14266 CPP_SCOPE)
14267 && cp_lexer_next_token_is_not (parser->lexer,
14268 CPP_CLOSE_PAREN))
14269 inputs = cp_parser_asm_operand_list (parser);
14270
14271 if (inputs == error_mark_node)
14272 invalid_inputs_p = true;
14273 }
14274 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14275 /* The clobbers are coming next. */
14276 clobbers_p = true;
14277
14278 /* Look for clobbers. */
14279 if (clobbers_p
14280 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14281 {
14282 clobbers_p = true;
14283 /* Consume the `:' or `::'. */
14284 cp_lexer_consume_token (parser->lexer);
14285 /* Parse the clobbers. */
14286 if (cp_lexer_next_token_is_not (parser->lexer,
14287 CPP_COLON)
14288 && cp_lexer_next_token_is_not (parser->lexer,
14289 CPP_CLOSE_PAREN))
14290 clobbers = cp_parser_asm_clobber_list (parser);
14291 }
14292 else if (goto_p
14293 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14294 /* The labels are coming next. */
14295 labels_p = true;
14296
14297 /* Look for labels. */
14298 if (labels_p
14299 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14300 {
14301 labels_p = true;
14302 /* Consume the `:' or `::'. */
14303 cp_lexer_consume_token (parser->lexer);
14304 /* Parse the labels. */
14305 labels = cp_parser_asm_label_list (parser);
14306 }
14307
14308 if (goto_p && !labels_p)
14309 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14310 }
14311 else if (goto_p)
14312 missing = RT_COLON_SCOPE;
14313
14314 /* Look for the closing `)'. */
14315 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14316 missing ? missing : RT_CLOSE_PAREN))
14317 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14318 /*consume_paren=*/true);
14319 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14320
14321 if (!invalid_inputs_p && !invalid_outputs_p)
14322 {
14323 /* Create the ASM_EXPR. */
14324 if (parser->in_function_body)
14325 {
14326 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14327 inputs, clobbers, labels);
14328 /* If the extended syntax was not used, mark the ASM_EXPR. */
14329 if (!extended_p)
14330 {
14331 tree temp = asm_stmt;
14332 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14333 temp = TREE_OPERAND (temp, 0);
14334
14335 ASM_INPUT_P (temp) = 1;
14336 }
14337 }
14338 else
14339 cgraph_add_asm_node (string);
14340 }
14341 }
14342
14343 /* Declarators [gram.dcl.decl] */
14344
14345 /* Parse an init-declarator.
14346
14347 init-declarator:
14348 declarator initializer [opt]
14349
14350 GNU Extension:
14351
14352 init-declarator:
14353 declarator asm-specification [opt] attributes [opt] initializer [opt]
14354
14355 function-definition:
14356 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14357 function-body
14358 decl-specifier-seq [opt] declarator function-try-block
14359
14360 GNU Extension:
14361
14362 function-definition:
14363 __extension__ function-definition
14364
14365 The DECL_SPECIFIERS apply to this declarator. Returns a
14366 representation of the entity declared. If MEMBER_P is TRUE, then
14367 this declarator appears in a class scope. The new DECL created by
14368 this declarator is returned.
14369
14370 The CHECKS are access checks that should be performed once we know
14371 what entity is being declared (and, therefore, what classes have
14372 befriended it).
14373
14374 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14375 for a function-definition here as well. If the declarator is a
14376 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14377 be TRUE upon return. By that point, the function-definition will
14378 have been completely parsed.
14379
14380 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14381 is FALSE. */
14382
14383 static tree
14384 cp_parser_init_declarator (cp_parser* parser,
14385 cp_decl_specifier_seq *decl_specifiers,
14386 VEC (deferred_access_check,gc)* checks,
14387 bool function_definition_allowed_p,
14388 bool member_p,
14389 int declares_class_or_enum,
14390 bool* function_definition_p)
14391 {
14392 cp_token *token = NULL, *asm_spec_start_token = NULL,
14393 *attributes_start_token = NULL;
14394 cp_declarator *declarator;
14395 tree prefix_attributes;
14396 tree attributes;
14397 tree asm_specification;
14398 tree initializer;
14399 tree decl = NULL_TREE;
14400 tree scope;
14401 int is_initialized;
14402 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14403 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14404 "(...)". */
14405 enum cpp_ttype initialization_kind;
14406 bool is_direct_init = false;
14407 bool is_non_constant_init;
14408 int ctor_dtor_or_conv_p;
14409 bool friend_p;
14410 tree pushed_scope = NULL;
14411
14412 /* Gather the attributes that were provided with the
14413 decl-specifiers. */
14414 prefix_attributes = decl_specifiers->attributes;
14415
14416 /* Assume that this is not the declarator for a function
14417 definition. */
14418 if (function_definition_p)
14419 *function_definition_p = false;
14420
14421 /* Defer access checks while parsing the declarator; we cannot know
14422 what names are accessible until we know what is being
14423 declared. */
14424 resume_deferring_access_checks ();
14425
14426 /* Parse the declarator. */
14427 token = cp_lexer_peek_token (parser->lexer);
14428 declarator
14429 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14430 &ctor_dtor_or_conv_p,
14431 /*parenthesized_p=*/NULL,
14432 /*member_p=*/false);
14433 /* Gather up the deferred checks. */
14434 stop_deferring_access_checks ();
14435
14436 /* If the DECLARATOR was erroneous, there's no need to go
14437 further. */
14438 if (declarator == cp_error_declarator)
14439 return error_mark_node;
14440
14441 /* Check that the number of template-parameter-lists is OK. */
14442 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14443 token->location))
14444 return error_mark_node;
14445
14446 if (declares_class_or_enum & 2)
14447 cp_parser_check_for_definition_in_return_type (declarator,
14448 decl_specifiers->type,
14449 decl_specifiers->type_location);
14450
14451 /* Figure out what scope the entity declared by the DECLARATOR is
14452 located in. `grokdeclarator' sometimes changes the scope, so
14453 we compute it now. */
14454 scope = get_scope_of_declarator (declarator);
14455
14456 /* Perform any lookups in the declared type which were thought to be
14457 dependent, but are not in the scope of the declarator. */
14458 decl_specifiers->type
14459 = maybe_update_decl_type (decl_specifiers->type, scope);
14460
14461 /* If we're allowing GNU extensions, look for an asm-specification
14462 and attributes. */
14463 if (cp_parser_allow_gnu_extensions_p (parser))
14464 {
14465 /* Look for an asm-specification. */
14466 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14467 asm_specification = cp_parser_asm_specification_opt (parser);
14468 /* And attributes. */
14469 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14470 attributes = cp_parser_attributes_opt (parser);
14471 }
14472 else
14473 {
14474 asm_specification = NULL_TREE;
14475 attributes = NULL_TREE;
14476 }
14477
14478 /* Peek at the next token. */
14479 token = cp_lexer_peek_token (parser->lexer);
14480 /* Check to see if the token indicates the start of a
14481 function-definition. */
14482 if (function_declarator_p (declarator)
14483 && cp_parser_token_starts_function_definition_p (token))
14484 {
14485 if (!function_definition_allowed_p)
14486 {
14487 /* If a function-definition should not appear here, issue an
14488 error message. */
14489 cp_parser_error (parser,
14490 "a function-definition is not allowed here");
14491 return error_mark_node;
14492 }
14493 else
14494 {
14495 location_t func_brace_location
14496 = cp_lexer_peek_token (parser->lexer)->location;
14497
14498 /* Neither attributes nor an asm-specification are allowed
14499 on a function-definition. */
14500 if (asm_specification)
14501 error_at (asm_spec_start_token->location,
14502 "an asm-specification is not allowed "
14503 "on a function-definition");
14504 if (attributes)
14505 error_at (attributes_start_token->location,
14506 "attributes are not allowed on a function-definition");
14507 /* This is a function-definition. */
14508 *function_definition_p = true;
14509
14510 /* Parse the function definition. */
14511 if (member_p)
14512 decl = cp_parser_save_member_function_body (parser,
14513 decl_specifiers,
14514 declarator,
14515 prefix_attributes);
14516 else
14517 decl
14518 = (cp_parser_function_definition_from_specifiers_and_declarator
14519 (parser, decl_specifiers, prefix_attributes, declarator));
14520
14521 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14522 {
14523 /* This is where the prologue starts... */
14524 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14525 = func_brace_location;
14526 }
14527
14528 return decl;
14529 }
14530 }
14531
14532 /* [dcl.dcl]
14533
14534 Only in function declarations for constructors, destructors, and
14535 type conversions can the decl-specifier-seq be omitted.
14536
14537 We explicitly postpone this check past the point where we handle
14538 function-definitions because we tolerate function-definitions
14539 that are missing their return types in some modes. */
14540 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14541 {
14542 cp_parser_error (parser,
14543 "expected constructor, destructor, or type conversion");
14544 return error_mark_node;
14545 }
14546
14547 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14548 if (token->type == CPP_EQ
14549 || token->type == CPP_OPEN_PAREN
14550 || token->type == CPP_OPEN_BRACE)
14551 {
14552 is_initialized = SD_INITIALIZED;
14553 initialization_kind = token->type;
14554
14555 if (token->type == CPP_EQ
14556 && function_declarator_p (declarator))
14557 {
14558 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14559 if (t2->keyword == RID_DEFAULT)
14560 is_initialized = SD_DEFAULTED;
14561 else if (t2->keyword == RID_DELETE)
14562 is_initialized = SD_DELETED;
14563 }
14564 }
14565 else
14566 {
14567 /* If the init-declarator isn't initialized and isn't followed by a
14568 `,' or `;', it's not a valid init-declarator. */
14569 if (token->type != CPP_COMMA
14570 && token->type != CPP_SEMICOLON)
14571 {
14572 cp_parser_error (parser, "expected initializer");
14573 return error_mark_node;
14574 }
14575 is_initialized = SD_UNINITIALIZED;
14576 initialization_kind = CPP_EOF;
14577 }
14578
14579 /* Because start_decl has side-effects, we should only call it if we
14580 know we're going ahead. By this point, we know that we cannot
14581 possibly be looking at any other construct. */
14582 cp_parser_commit_to_tentative_parse (parser);
14583
14584 /* If the decl specifiers were bad, issue an error now that we're
14585 sure this was intended to be a declarator. Then continue
14586 declaring the variable(s), as int, to try to cut down on further
14587 errors. */
14588 if (decl_specifiers->any_specifiers_p
14589 && decl_specifiers->type == error_mark_node)
14590 {
14591 cp_parser_error (parser, "invalid type in declaration");
14592 decl_specifiers->type = integer_type_node;
14593 }
14594
14595 /* Check to see whether or not this declaration is a friend. */
14596 friend_p = cp_parser_friend_p (decl_specifiers);
14597
14598 /* Enter the newly declared entry in the symbol table. If we're
14599 processing a declaration in a class-specifier, we wait until
14600 after processing the initializer. */
14601 if (!member_p)
14602 {
14603 if (parser->in_unbraced_linkage_specification_p)
14604 decl_specifiers->storage_class = sc_extern;
14605 decl = start_decl (declarator, decl_specifiers,
14606 is_initialized, attributes, prefix_attributes,
14607 &pushed_scope);
14608 /* Adjust location of decl if declarator->id_loc is more appropriate:
14609 set, and decl wasn't merged with another decl, in which case its
14610 location would be different from input_location, and more accurate. */
14611 if (DECL_P (decl)
14612 && declarator->id_loc != UNKNOWN_LOCATION
14613 && DECL_SOURCE_LOCATION (decl) == input_location)
14614 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14615 }
14616 else if (scope)
14617 /* Enter the SCOPE. That way unqualified names appearing in the
14618 initializer will be looked up in SCOPE. */
14619 pushed_scope = push_scope (scope);
14620
14621 /* Perform deferred access control checks, now that we know in which
14622 SCOPE the declared entity resides. */
14623 if (!member_p && decl)
14624 {
14625 tree saved_current_function_decl = NULL_TREE;
14626
14627 /* If the entity being declared is a function, pretend that we
14628 are in its scope. If it is a `friend', it may have access to
14629 things that would not otherwise be accessible. */
14630 if (TREE_CODE (decl) == FUNCTION_DECL)
14631 {
14632 saved_current_function_decl = current_function_decl;
14633 current_function_decl = decl;
14634 }
14635
14636 /* Perform access checks for template parameters. */
14637 cp_parser_perform_template_parameter_access_checks (checks);
14638
14639 /* Perform the access control checks for the declarator and the
14640 decl-specifiers. */
14641 perform_deferred_access_checks ();
14642
14643 /* Restore the saved value. */
14644 if (TREE_CODE (decl) == FUNCTION_DECL)
14645 current_function_decl = saved_current_function_decl;
14646 }
14647
14648 /* Parse the initializer. */
14649 initializer = NULL_TREE;
14650 is_direct_init = false;
14651 is_non_constant_init = true;
14652 if (is_initialized)
14653 {
14654 if (function_declarator_p (declarator))
14655 {
14656 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14657 if (initialization_kind == CPP_EQ)
14658 initializer = cp_parser_pure_specifier (parser);
14659 else
14660 {
14661 /* If the declaration was erroneous, we don't really
14662 know what the user intended, so just silently
14663 consume the initializer. */
14664 if (decl != error_mark_node)
14665 error_at (initializer_start_token->location,
14666 "initializer provided for function");
14667 cp_parser_skip_to_closing_parenthesis (parser,
14668 /*recovering=*/true,
14669 /*or_comma=*/false,
14670 /*consume_paren=*/true);
14671 }
14672 }
14673 else
14674 {
14675 /* We want to record the extra mangling scope for in-class
14676 initializers of class members and initializers of static data
14677 member templates. The former is a C++0x feature which isn't
14678 implemented yet, and I expect it will involve deferring
14679 parsing of the initializer until end of class as with default
14680 arguments. So right here we only handle the latter. */
14681 if (!member_p && processing_template_decl)
14682 start_lambda_scope (decl);
14683 initializer = cp_parser_initializer (parser,
14684 &is_direct_init,
14685 &is_non_constant_init);
14686 if (!member_p && processing_template_decl)
14687 finish_lambda_scope ();
14688 }
14689 }
14690
14691 /* The old parser allows attributes to appear after a parenthesized
14692 initializer. Mark Mitchell proposed removing this functionality
14693 on the GCC mailing lists on 2002-08-13. This parser accepts the
14694 attributes -- but ignores them. */
14695 if (cp_parser_allow_gnu_extensions_p (parser)
14696 && initialization_kind == CPP_OPEN_PAREN)
14697 if (cp_parser_attributes_opt (parser))
14698 warning (OPT_Wattributes,
14699 "attributes after parenthesized initializer ignored");
14700
14701 /* For an in-class declaration, use `grokfield' to create the
14702 declaration. */
14703 if (member_p)
14704 {
14705 if (pushed_scope)
14706 {
14707 pop_scope (pushed_scope);
14708 pushed_scope = false;
14709 }
14710 decl = grokfield (declarator, decl_specifiers,
14711 initializer, !is_non_constant_init,
14712 /*asmspec=*/NULL_TREE,
14713 prefix_attributes);
14714 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14715 cp_parser_save_default_args (parser, decl);
14716 }
14717
14718 /* Finish processing the declaration. But, skip friend
14719 declarations. */
14720 if (!friend_p && decl && decl != error_mark_node)
14721 {
14722 cp_finish_decl (decl,
14723 initializer, !is_non_constant_init,
14724 asm_specification,
14725 /* If the initializer is in parentheses, then this is
14726 a direct-initialization, which means that an
14727 `explicit' constructor is OK. Otherwise, an
14728 `explicit' constructor cannot be used. */
14729 ((is_direct_init || !is_initialized)
14730 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14731 }
14732 else if ((cxx_dialect != cxx98) && friend_p
14733 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14734 /* Core issue #226 (C++0x only): A default template-argument
14735 shall not be specified in a friend class template
14736 declaration. */
14737 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14738 /*is_partial=*/0, /*is_friend_decl=*/1);
14739
14740 if (!friend_p && pushed_scope)
14741 pop_scope (pushed_scope);
14742
14743 return decl;
14744 }
14745
14746 /* Parse a declarator.
14747
14748 declarator:
14749 direct-declarator
14750 ptr-operator declarator
14751
14752 abstract-declarator:
14753 ptr-operator abstract-declarator [opt]
14754 direct-abstract-declarator
14755
14756 GNU Extensions:
14757
14758 declarator:
14759 attributes [opt] direct-declarator
14760 attributes [opt] ptr-operator declarator
14761
14762 abstract-declarator:
14763 attributes [opt] ptr-operator abstract-declarator [opt]
14764 attributes [opt] direct-abstract-declarator
14765
14766 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14767 detect constructor, destructor or conversion operators. It is set
14768 to -1 if the declarator is a name, and +1 if it is a
14769 function. Otherwise it is set to zero. Usually you just want to
14770 test for >0, but internally the negative value is used.
14771
14772 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14773 a decl-specifier-seq unless it declares a constructor, destructor,
14774 or conversion. It might seem that we could check this condition in
14775 semantic analysis, rather than parsing, but that makes it difficult
14776 to handle something like `f()'. We want to notice that there are
14777 no decl-specifiers, and therefore realize that this is an
14778 expression, not a declaration.)
14779
14780 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14781 the declarator is a direct-declarator of the form "(...)".
14782
14783 MEMBER_P is true iff this declarator is a member-declarator. */
14784
14785 static cp_declarator *
14786 cp_parser_declarator (cp_parser* parser,
14787 cp_parser_declarator_kind dcl_kind,
14788 int* ctor_dtor_or_conv_p,
14789 bool* parenthesized_p,
14790 bool member_p)
14791 {
14792 cp_declarator *declarator;
14793 enum tree_code code;
14794 cp_cv_quals cv_quals;
14795 tree class_type;
14796 tree attributes = NULL_TREE;
14797
14798 /* Assume this is not a constructor, destructor, or type-conversion
14799 operator. */
14800 if (ctor_dtor_or_conv_p)
14801 *ctor_dtor_or_conv_p = 0;
14802
14803 if (cp_parser_allow_gnu_extensions_p (parser))
14804 attributes = cp_parser_attributes_opt (parser);
14805
14806 /* Check for the ptr-operator production. */
14807 cp_parser_parse_tentatively (parser);
14808 /* Parse the ptr-operator. */
14809 code = cp_parser_ptr_operator (parser,
14810 &class_type,
14811 &cv_quals);
14812 /* If that worked, then we have a ptr-operator. */
14813 if (cp_parser_parse_definitely (parser))
14814 {
14815 /* If a ptr-operator was found, then this declarator was not
14816 parenthesized. */
14817 if (parenthesized_p)
14818 *parenthesized_p = true;
14819 /* The dependent declarator is optional if we are parsing an
14820 abstract-declarator. */
14821 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14822 cp_parser_parse_tentatively (parser);
14823
14824 /* Parse the dependent declarator. */
14825 declarator = cp_parser_declarator (parser, dcl_kind,
14826 /*ctor_dtor_or_conv_p=*/NULL,
14827 /*parenthesized_p=*/NULL,
14828 /*member_p=*/false);
14829
14830 /* If we are parsing an abstract-declarator, we must handle the
14831 case where the dependent declarator is absent. */
14832 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14833 && !cp_parser_parse_definitely (parser))
14834 declarator = NULL;
14835
14836 declarator = cp_parser_make_indirect_declarator
14837 (code, class_type, cv_quals, declarator);
14838 }
14839 /* Everything else is a direct-declarator. */
14840 else
14841 {
14842 if (parenthesized_p)
14843 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14844 CPP_OPEN_PAREN);
14845 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14846 ctor_dtor_or_conv_p,
14847 member_p);
14848 }
14849
14850 if (attributes && declarator && declarator != cp_error_declarator)
14851 declarator->attributes = attributes;
14852
14853 return declarator;
14854 }
14855
14856 /* Parse a direct-declarator or direct-abstract-declarator.
14857
14858 direct-declarator:
14859 declarator-id
14860 direct-declarator ( parameter-declaration-clause )
14861 cv-qualifier-seq [opt]
14862 exception-specification [opt]
14863 direct-declarator [ constant-expression [opt] ]
14864 ( declarator )
14865
14866 direct-abstract-declarator:
14867 direct-abstract-declarator [opt]
14868 ( parameter-declaration-clause )
14869 cv-qualifier-seq [opt]
14870 exception-specification [opt]
14871 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14872 ( abstract-declarator )
14873
14874 Returns a representation of the declarator. DCL_KIND is
14875 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14876 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14877 we are parsing a direct-declarator. It is
14878 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14879 of ambiguity we prefer an abstract declarator, as per
14880 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14881 cp_parser_declarator. */
14882
14883 static cp_declarator *
14884 cp_parser_direct_declarator (cp_parser* parser,
14885 cp_parser_declarator_kind dcl_kind,
14886 int* ctor_dtor_or_conv_p,
14887 bool member_p)
14888 {
14889 cp_token *token;
14890 cp_declarator *declarator = NULL;
14891 tree scope = NULL_TREE;
14892 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14893 bool saved_in_declarator_p = parser->in_declarator_p;
14894 bool first = true;
14895 tree pushed_scope = NULL_TREE;
14896
14897 while (true)
14898 {
14899 /* Peek at the next token. */
14900 token = cp_lexer_peek_token (parser->lexer);
14901 if (token->type == CPP_OPEN_PAREN)
14902 {
14903 /* This is either a parameter-declaration-clause, or a
14904 parenthesized declarator. When we know we are parsing a
14905 named declarator, it must be a parenthesized declarator
14906 if FIRST is true. For instance, `(int)' is a
14907 parameter-declaration-clause, with an omitted
14908 direct-abstract-declarator. But `((*))', is a
14909 parenthesized abstract declarator. Finally, when T is a
14910 template parameter `(T)' is a
14911 parameter-declaration-clause, and not a parenthesized
14912 named declarator.
14913
14914 We first try and parse a parameter-declaration-clause,
14915 and then try a nested declarator (if FIRST is true).
14916
14917 It is not an error for it not to be a
14918 parameter-declaration-clause, even when FIRST is
14919 false. Consider,
14920
14921 int i (int);
14922 int i (3);
14923
14924 The first is the declaration of a function while the
14925 second is the definition of a variable, including its
14926 initializer.
14927
14928 Having seen only the parenthesis, we cannot know which of
14929 these two alternatives should be selected. Even more
14930 complex are examples like:
14931
14932 int i (int (a));
14933 int i (int (3));
14934
14935 The former is a function-declaration; the latter is a
14936 variable initialization.
14937
14938 Thus again, we try a parameter-declaration-clause, and if
14939 that fails, we back out and return. */
14940
14941 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14942 {
14943 tree params;
14944 unsigned saved_num_template_parameter_lists;
14945 bool is_declarator = false;
14946 tree t;
14947
14948 /* In a member-declarator, the only valid interpretation
14949 of a parenthesis is the start of a
14950 parameter-declaration-clause. (It is invalid to
14951 initialize a static data member with a parenthesized
14952 initializer; only the "=" form of initialization is
14953 permitted.) */
14954 if (!member_p)
14955 cp_parser_parse_tentatively (parser);
14956
14957 /* Consume the `('. */
14958 cp_lexer_consume_token (parser->lexer);
14959 if (first)
14960 {
14961 /* If this is going to be an abstract declarator, we're
14962 in a declarator and we can't have default args. */
14963 parser->default_arg_ok_p = false;
14964 parser->in_declarator_p = true;
14965 }
14966
14967 /* Inside the function parameter list, surrounding
14968 template-parameter-lists do not apply. */
14969 saved_num_template_parameter_lists
14970 = parser->num_template_parameter_lists;
14971 parser->num_template_parameter_lists = 0;
14972
14973 begin_scope (sk_function_parms, NULL_TREE);
14974
14975 /* Parse the parameter-declaration-clause. */
14976 params = cp_parser_parameter_declaration_clause (parser);
14977
14978 parser->num_template_parameter_lists
14979 = saved_num_template_parameter_lists;
14980
14981 /* If all went well, parse the cv-qualifier-seq and the
14982 exception-specification. */
14983 if (member_p || cp_parser_parse_definitely (parser))
14984 {
14985 cp_cv_quals cv_quals;
14986 tree exception_specification;
14987 tree late_return;
14988
14989 is_declarator = true;
14990
14991 if (ctor_dtor_or_conv_p)
14992 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14993 first = false;
14994 /* Consume the `)'. */
14995 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14996
14997 /* Parse the cv-qualifier-seq. */
14998 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14999 /* And the exception-specification. */
15000 exception_specification
15001 = cp_parser_exception_specification_opt (parser);
15002
15003 late_return
15004 = cp_parser_late_return_type_opt (parser);
15005
15006 /* Create the function-declarator. */
15007 declarator = make_call_declarator (declarator,
15008 params,
15009 cv_quals,
15010 exception_specification,
15011 late_return);
15012 /* Any subsequent parameter lists are to do with
15013 return type, so are not those of the declared
15014 function. */
15015 parser->default_arg_ok_p = false;
15016 }
15017
15018 /* Remove the function parms from scope. */
15019 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15020 pop_binding (DECL_NAME (t), t);
15021 leave_scope();
15022
15023 if (is_declarator)
15024 /* Repeat the main loop. */
15025 continue;
15026 }
15027
15028 /* If this is the first, we can try a parenthesized
15029 declarator. */
15030 if (first)
15031 {
15032 bool saved_in_type_id_in_expr_p;
15033
15034 parser->default_arg_ok_p = saved_default_arg_ok_p;
15035 parser->in_declarator_p = saved_in_declarator_p;
15036
15037 /* Consume the `('. */
15038 cp_lexer_consume_token (parser->lexer);
15039 /* Parse the nested declarator. */
15040 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15041 parser->in_type_id_in_expr_p = true;
15042 declarator
15043 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15044 /*parenthesized_p=*/NULL,
15045 member_p);
15046 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15047 first = false;
15048 /* Expect a `)'. */
15049 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15050 declarator = cp_error_declarator;
15051 if (declarator == cp_error_declarator)
15052 break;
15053
15054 goto handle_declarator;
15055 }
15056 /* Otherwise, we must be done. */
15057 else
15058 break;
15059 }
15060 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15061 && token->type == CPP_OPEN_SQUARE)
15062 {
15063 /* Parse an array-declarator. */
15064 tree bounds;
15065
15066 if (ctor_dtor_or_conv_p)
15067 *ctor_dtor_or_conv_p = 0;
15068
15069 first = false;
15070 parser->default_arg_ok_p = false;
15071 parser->in_declarator_p = true;
15072 /* Consume the `['. */
15073 cp_lexer_consume_token (parser->lexer);
15074 /* Peek at the next token. */
15075 token = cp_lexer_peek_token (parser->lexer);
15076 /* If the next token is `]', then there is no
15077 constant-expression. */
15078 if (token->type != CPP_CLOSE_SQUARE)
15079 {
15080 bool non_constant_p;
15081
15082 bounds
15083 = cp_parser_constant_expression (parser,
15084 /*allow_non_constant=*/true,
15085 &non_constant_p);
15086 if (!non_constant_p || cxx_dialect >= cxx0x)
15087 /* OK */;
15088 /* Normally, the array bound must be an integral constant
15089 expression. However, as an extension, we allow VLAs
15090 in function scopes as long as they aren't part of a
15091 parameter declaration. */
15092 else if (!parser->in_function_body
15093 || current_binding_level->kind == sk_function_parms)
15094 {
15095 cp_parser_error (parser,
15096 "array bound is not an integer constant");
15097 bounds = error_mark_node;
15098 }
15099 else if (processing_template_decl && !error_operand_p (bounds))
15100 {
15101 /* Remember this wasn't a constant-expression. */
15102 bounds = build_nop (TREE_TYPE (bounds), bounds);
15103 TREE_SIDE_EFFECTS (bounds) = 1;
15104 }
15105 }
15106 else
15107 bounds = NULL_TREE;
15108 /* Look for the closing `]'. */
15109 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15110 {
15111 declarator = cp_error_declarator;
15112 break;
15113 }
15114
15115 declarator = make_array_declarator (declarator, bounds);
15116 }
15117 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15118 {
15119 {
15120 tree qualifying_scope;
15121 tree unqualified_name;
15122 special_function_kind sfk;
15123 bool abstract_ok;
15124 bool pack_expansion_p = false;
15125 cp_token *declarator_id_start_token;
15126
15127 /* Parse a declarator-id */
15128 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15129 if (abstract_ok)
15130 {
15131 cp_parser_parse_tentatively (parser);
15132
15133 /* If we see an ellipsis, we should be looking at a
15134 parameter pack. */
15135 if (token->type == CPP_ELLIPSIS)
15136 {
15137 /* Consume the `...' */
15138 cp_lexer_consume_token (parser->lexer);
15139
15140 pack_expansion_p = true;
15141 }
15142 }
15143
15144 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15145 unqualified_name
15146 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15147 qualifying_scope = parser->scope;
15148 if (abstract_ok)
15149 {
15150 bool okay = false;
15151
15152 if (!unqualified_name && pack_expansion_p)
15153 {
15154 /* Check whether an error occurred. */
15155 okay = !cp_parser_error_occurred (parser);
15156
15157 /* We already consumed the ellipsis to mark a
15158 parameter pack, but we have no way to report it,
15159 so abort the tentative parse. We will be exiting
15160 immediately anyway. */
15161 cp_parser_abort_tentative_parse (parser);
15162 }
15163 else
15164 okay = cp_parser_parse_definitely (parser);
15165
15166 if (!okay)
15167 unqualified_name = error_mark_node;
15168 else if (unqualified_name
15169 && (qualifying_scope
15170 || (TREE_CODE (unqualified_name)
15171 != IDENTIFIER_NODE)))
15172 {
15173 cp_parser_error (parser, "expected unqualified-id");
15174 unqualified_name = error_mark_node;
15175 }
15176 }
15177
15178 if (!unqualified_name)
15179 return NULL;
15180 if (unqualified_name == error_mark_node)
15181 {
15182 declarator = cp_error_declarator;
15183 pack_expansion_p = false;
15184 declarator->parameter_pack_p = false;
15185 break;
15186 }
15187
15188 if (qualifying_scope && at_namespace_scope_p ()
15189 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15190 {
15191 /* In the declaration of a member of a template class
15192 outside of the class itself, the SCOPE will sometimes
15193 be a TYPENAME_TYPE. For example, given:
15194
15195 template <typename T>
15196 int S<T>::R::i = 3;
15197
15198 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15199 this context, we must resolve S<T>::R to an ordinary
15200 type, rather than a typename type.
15201
15202 The reason we normally avoid resolving TYPENAME_TYPEs
15203 is that a specialization of `S' might render
15204 `S<T>::R' not a type. However, if `S' is
15205 specialized, then this `i' will not be used, so there
15206 is no harm in resolving the types here. */
15207 tree type;
15208
15209 /* Resolve the TYPENAME_TYPE. */
15210 type = resolve_typename_type (qualifying_scope,
15211 /*only_current_p=*/false);
15212 /* If that failed, the declarator is invalid. */
15213 if (TREE_CODE (type) == TYPENAME_TYPE)
15214 {
15215 if (typedef_variant_p (type))
15216 error_at (declarator_id_start_token->location,
15217 "cannot define member of dependent typedef "
15218 "%qT", type);
15219 else
15220 error_at (declarator_id_start_token->location,
15221 "%<%T::%E%> is not a type",
15222 TYPE_CONTEXT (qualifying_scope),
15223 TYPE_IDENTIFIER (qualifying_scope));
15224 }
15225 qualifying_scope = type;
15226 }
15227
15228 sfk = sfk_none;
15229
15230 if (unqualified_name)
15231 {
15232 tree class_type;
15233
15234 if (qualifying_scope
15235 && CLASS_TYPE_P (qualifying_scope))
15236 class_type = qualifying_scope;
15237 else
15238 class_type = current_class_type;
15239
15240 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15241 {
15242 tree name_type = TREE_TYPE (unqualified_name);
15243 if (class_type && same_type_p (name_type, class_type))
15244 {
15245 if (qualifying_scope
15246 && CLASSTYPE_USE_TEMPLATE (name_type))
15247 {
15248 error_at (declarator_id_start_token->location,
15249 "invalid use of constructor as a template");
15250 inform (declarator_id_start_token->location,
15251 "use %<%T::%D%> instead of %<%T::%D%> to "
15252 "name the constructor in a qualified name",
15253 class_type,
15254 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15255 class_type, name_type);
15256 declarator = cp_error_declarator;
15257 break;
15258 }
15259 else
15260 unqualified_name = constructor_name (class_type);
15261 }
15262 else
15263 {
15264 /* We do not attempt to print the declarator
15265 here because we do not have enough
15266 information about its original syntactic
15267 form. */
15268 cp_parser_error (parser, "invalid declarator");
15269 declarator = cp_error_declarator;
15270 break;
15271 }
15272 }
15273
15274 if (class_type)
15275 {
15276 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15277 sfk = sfk_destructor;
15278 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15279 sfk = sfk_conversion;
15280 else if (/* There's no way to declare a constructor
15281 for an anonymous type, even if the type
15282 got a name for linkage purposes. */
15283 !TYPE_WAS_ANONYMOUS (class_type)
15284 && constructor_name_p (unqualified_name,
15285 class_type))
15286 {
15287 unqualified_name = constructor_name (class_type);
15288 sfk = sfk_constructor;
15289 }
15290 else if (is_overloaded_fn (unqualified_name)
15291 && DECL_CONSTRUCTOR_P (get_first_fn
15292 (unqualified_name)))
15293 sfk = sfk_constructor;
15294
15295 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15296 *ctor_dtor_or_conv_p = -1;
15297 }
15298 }
15299 declarator = make_id_declarator (qualifying_scope,
15300 unqualified_name,
15301 sfk);
15302 declarator->id_loc = token->location;
15303 declarator->parameter_pack_p = pack_expansion_p;
15304
15305 if (pack_expansion_p)
15306 maybe_warn_variadic_templates ();
15307 }
15308
15309 handle_declarator:;
15310 scope = get_scope_of_declarator (declarator);
15311 if (scope)
15312 /* Any names that appear after the declarator-id for a
15313 member are looked up in the containing scope. */
15314 pushed_scope = push_scope (scope);
15315 parser->in_declarator_p = true;
15316 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15317 || (declarator && declarator->kind == cdk_id))
15318 /* Default args are only allowed on function
15319 declarations. */
15320 parser->default_arg_ok_p = saved_default_arg_ok_p;
15321 else
15322 parser->default_arg_ok_p = false;
15323
15324 first = false;
15325 }
15326 /* We're done. */
15327 else
15328 break;
15329 }
15330
15331 /* For an abstract declarator, we might wind up with nothing at this
15332 point. That's an error; the declarator is not optional. */
15333 if (!declarator)
15334 cp_parser_error (parser, "expected declarator");
15335
15336 /* If we entered a scope, we must exit it now. */
15337 if (pushed_scope)
15338 pop_scope (pushed_scope);
15339
15340 parser->default_arg_ok_p = saved_default_arg_ok_p;
15341 parser->in_declarator_p = saved_in_declarator_p;
15342
15343 return declarator;
15344 }
15345
15346 /* Parse a ptr-operator.
15347
15348 ptr-operator:
15349 * cv-qualifier-seq [opt]
15350 &
15351 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15352
15353 GNU Extension:
15354
15355 ptr-operator:
15356 & cv-qualifier-seq [opt]
15357
15358 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15359 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15360 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15361 filled in with the TYPE containing the member. *CV_QUALS is
15362 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15363 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15364 Note that the tree codes returned by this function have nothing
15365 to do with the types of trees that will be eventually be created
15366 to represent the pointer or reference type being parsed. They are
15367 just constants with suggestive names. */
15368 static enum tree_code
15369 cp_parser_ptr_operator (cp_parser* parser,
15370 tree* type,
15371 cp_cv_quals *cv_quals)
15372 {
15373 enum tree_code code = ERROR_MARK;
15374 cp_token *token;
15375
15376 /* Assume that it's not a pointer-to-member. */
15377 *type = NULL_TREE;
15378 /* And that there are no cv-qualifiers. */
15379 *cv_quals = TYPE_UNQUALIFIED;
15380
15381 /* Peek at the next token. */
15382 token = cp_lexer_peek_token (parser->lexer);
15383
15384 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15385 if (token->type == CPP_MULT)
15386 code = INDIRECT_REF;
15387 else if (token->type == CPP_AND)
15388 code = ADDR_EXPR;
15389 else if ((cxx_dialect != cxx98) &&
15390 token->type == CPP_AND_AND) /* C++0x only */
15391 code = NON_LVALUE_EXPR;
15392
15393 if (code != ERROR_MARK)
15394 {
15395 /* Consume the `*', `&' or `&&'. */
15396 cp_lexer_consume_token (parser->lexer);
15397
15398 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15399 `&', if we are allowing GNU extensions. (The only qualifier
15400 that can legally appear after `&' is `restrict', but that is
15401 enforced during semantic analysis. */
15402 if (code == INDIRECT_REF
15403 || cp_parser_allow_gnu_extensions_p (parser))
15404 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15405 }
15406 else
15407 {
15408 /* Try the pointer-to-member case. */
15409 cp_parser_parse_tentatively (parser);
15410 /* Look for the optional `::' operator. */
15411 cp_parser_global_scope_opt (parser,
15412 /*current_scope_valid_p=*/false);
15413 /* Look for the nested-name specifier. */
15414 token = cp_lexer_peek_token (parser->lexer);
15415 cp_parser_nested_name_specifier (parser,
15416 /*typename_keyword_p=*/false,
15417 /*check_dependency_p=*/true,
15418 /*type_p=*/false,
15419 /*is_declaration=*/false);
15420 /* If we found it, and the next token is a `*', then we are
15421 indeed looking at a pointer-to-member operator. */
15422 if (!cp_parser_error_occurred (parser)
15423 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15424 {
15425 /* Indicate that the `*' operator was used. */
15426 code = INDIRECT_REF;
15427
15428 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15429 error_at (token->location, "%qD is a namespace", parser->scope);
15430 else
15431 {
15432 /* The type of which the member is a member is given by the
15433 current SCOPE. */
15434 *type = parser->scope;
15435 /* The next name will not be qualified. */
15436 parser->scope = NULL_TREE;
15437 parser->qualifying_scope = NULL_TREE;
15438 parser->object_scope = NULL_TREE;
15439 /* Look for the optional cv-qualifier-seq. */
15440 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15441 }
15442 }
15443 /* If that didn't work we don't have a ptr-operator. */
15444 if (!cp_parser_parse_definitely (parser))
15445 cp_parser_error (parser, "expected ptr-operator");
15446 }
15447
15448 return code;
15449 }
15450
15451 /* Parse an (optional) cv-qualifier-seq.
15452
15453 cv-qualifier-seq:
15454 cv-qualifier cv-qualifier-seq [opt]
15455
15456 cv-qualifier:
15457 const
15458 volatile
15459
15460 GNU Extension:
15461
15462 cv-qualifier:
15463 __restrict__
15464
15465 Returns a bitmask representing the cv-qualifiers. */
15466
15467 static cp_cv_quals
15468 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15469 {
15470 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15471
15472 while (true)
15473 {
15474 cp_token *token;
15475 cp_cv_quals cv_qualifier;
15476
15477 /* Peek at the next token. */
15478 token = cp_lexer_peek_token (parser->lexer);
15479 /* See if it's a cv-qualifier. */
15480 switch (token->keyword)
15481 {
15482 case RID_CONST:
15483 cv_qualifier = TYPE_QUAL_CONST;
15484 break;
15485
15486 case RID_VOLATILE:
15487 cv_qualifier = TYPE_QUAL_VOLATILE;
15488 break;
15489
15490 case RID_RESTRICT:
15491 cv_qualifier = TYPE_QUAL_RESTRICT;
15492 break;
15493
15494 default:
15495 cv_qualifier = TYPE_UNQUALIFIED;
15496 break;
15497 }
15498
15499 if (!cv_qualifier)
15500 break;
15501
15502 if (cv_quals & cv_qualifier)
15503 {
15504 error_at (token->location, "duplicate cv-qualifier");
15505 cp_lexer_purge_token (parser->lexer);
15506 }
15507 else
15508 {
15509 cp_lexer_consume_token (parser->lexer);
15510 cv_quals |= cv_qualifier;
15511 }
15512 }
15513
15514 return cv_quals;
15515 }
15516
15517 /* Parse a late-specified return type, if any. This is not a separate
15518 non-terminal, but part of a function declarator, which looks like
15519
15520 -> trailing-type-specifier-seq abstract-declarator(opt)
15521
15522 Returns the type indicated by the type-id. */
15523
15524 static tree
15525 cp_parser_late_return_type_opt (cp_parser* parser)
15526 {
15527 cp_token *token;
15528
15529 /* Peek at the next token. */
15530 token = cp_lexer_peek_token (parser->lexer);
15531 /* A late-specified return type is indicated by an initial '->'. */
15532 if (token->type != CPP_DEREF)
15533 return NULL_TREE;
15534
15535 /* Consume the ->. */
15536 cp_lexer_consume_token (parser->lexer);
15537
15538 return cp_parser_trailing_type_id (parser);
15539 }
15540
15541 /* Parse a declarator-id.
15542
15543 declarator-id:
15544 id-expression
15545 :: [opt] nested-name-specifier [opt] type-name
15546
15547 In the `id-expression' case, the value returned is as for
15548 cp_parser_id_expression if the id-expression was an unqualified-id.
15549 If the id-expression was a qualified-id, then a SCOPE_REF is
15550 returned. The first operand is the scope (either a NAMESPACE_DECL
15551 or TREE_TYPE), but the second is still just a representation of an
15552 unqualified-id. */
15553
15554 static tree
15555 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15556 {
15557 tree id;
15558 /* The expression must be an id-expression. Assume that qualified
15559 names are the names of types so that:
15560
15561 template <class T>
15562 int S<T>::R::i = 3;
15563
15564 will work; we must treat `S<T>::R' as the name of a type.
15565 Similarly, assume that qualified names are templates, where
15566 required, so that:
15567
15568 template <class T>
15569 int S<T>::R<T>::i = 3;
15570
15571 will work, too. */
15572 id = cp_parser_id_expression (parser,
15573 /*template_keyword_p=*/false,
15574 /*check_dependency_p=*/false,
15575 /*template_p=*/NULL,
15576 /*declarator_p=*/true,
15577 optional_p);
15578 if (id && BASELINK_P (id))
15579 id = BASELINK_FUNCTIONS (id);
15580 return id;
15581 }
15582
15583 /* Parse a type-id.
15584
15585 type-id:
15586 type-specifier-seq abstract-declarator [opt]
15587
15588 Returns the TYPE specified. */
15589
15590 static tree
15591 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15592 bool is_trailing_return)
15593 {
15594 cp_decl_specifier_seq type_specifier_seq;
15595 cp_declarator *abstract_declarator;
15596
15597 /* Parse the type-specifier-seq. */
15598 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15599 is_trailing_return,
15600 &type_specifier_seq);
15601 if (type_specifier_seq.type == error_mark_node)
15602 return error_mark_node;
15603
15604 /* There might or might not be an abstract declarator. */
15605 cp_parser_parse_tentatively (parser);
15606 /* Look for the declarator. */
15607 abstract_declarator
15608 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15609 /*parenthesized_p=*/NULL,
15610 /*member_p=*/false);
15611 /* Check to see if there really was a declarator. */
15612 if (!cp_parser_parse_definitely (parser))
15613 abstract_declarator = NULL;
15614
15615 if (type_specifier_seq.type
15616 && type_uses_auto (type_specifier_seq.type))
15617 {
15618 /* A type-id with type 'auto' is only ok if the abstract declarator
15619 is a function declarator with a late-specified return type. */
15620 if (abstract_declarator
15621 && abstract_declarator->kind == cdk_function
15622 && abstract_declarator->u.function.late_return_type)
15623 /* OK */;
15624 else
15625 {
15626 error ("invalid use of %<auto%>");
15627 return error_mark_node;
15628 }
15629 }
15630
15631 return groktypename (&type_specifier_seq, abstract_declarator,
15632 is_template_arg);
15633 }
15634
15635 static tree cp_parser_type_id (cp_parser *parser)
15636 {
15637 return cp_parser_type_id_1 (parser, false, false);
15638 }
15639
15640 static tree cp_parser_template_type_arg (cp_parser *parser)
15641 {
15642 return cp_parser_type_id_1 (parser, true, false);
15643 }
15644
15645 static tree cp_parser_trailing_type_id (cp_parser *parser)
15646 {
15647 return cp_parser_type_id_1 (parser, false, true);
15648 }
15649
15650 /* Parse a type-specifier-seq.
15651
15652 type-specifier-seq:
15653 type-specifier type-specifier-seq [opt]
15654
15655 GNU extension:
15656
15657 type-specifier-seq:
15658 attributes type-specifier-seq [opt]
15659
15660 If IS_DECLARATION is true, we are at the start of a "condition" or
15661 exception-declaration, so we might be followed by a declarator-id.
15662
15663 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15664 i.e. we've just seen "->".
15665
15666 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15667
15668 static void
15669 cp_parser_type_specifier_seq (cp_parser* parser,
15670 bool is_declaration,
15671 bool is_trailing_return,
15672 cp_decl_specifier_seq *type_specifier_seq)
15673 {
15674 bool seen_type_specifier = false;
15675 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15676 cp_token *start_token = NULL;
15677
15678 /* Clear the TYPE_SPECIFIER_SEQ. */
15679 clear_decl_specs (type_specifier_seq);
15680
15681 /* In the context of a trailing return type, enum E { } is an
15682 elaborated-type-specifier followed by a function-body, not an
15683 enum-specifier. */
15684 if (is_trailing_return)
15685 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15686
15687 /* Parse the type-specifiers and attributes. */
15688 while (true)
15689 {
15690 tree type_specifier;
15691 bool is_cv_qualifier;
15692
15693 /* Check for attributes first. */
15694 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15695 {
15696 type_specifier_seq->attributes =
15697 chainon (type_specifier_seq->attributes,
15698 cp_parser_attributes_opt (parser));
15699 continue;
15700 }
15701
15702 /* record the token of the beginning of the type specifier seq,
15703 for error reporting purposes*/
15704 if (!start_token)
15705 start_token = cp_lexer_peek_token (parser->lexer);
15706
15707 /* Look for the type-specifier. */
15708 type_specifier = cp_parser_type_specifier (parser,
15709 flags,
15710 type_specifier_seq,
15711 /*is_declaration=*/false,
15712 NULL,
15713 &is_cv_qualifier);
15714 if (!type_specifier)
15715 {
15716 /* If the first type-specifier could not be found, this is not a
15717 type-specifier-seq at all. */
15718 if (!seen_type_specifier)
15719 {
15720 cp_parser_error (parser, "expected type-specifier");
15721 type_specifier_seq->type = error_mark_node;
15722 return;
15723 }
15724 /* If subsequent type-specifiers could not be found, the
15725 type-specifier-seq is complete. */
15726 break;
15727 }
15728
15729 seen_type_specifier = true;
15730 /* The standard says that a condition can be:
15731
15732 type-specifier-seq declarator = assignment-expression
15733
15734 However, given:
15735
15736 struct S {};
15737 if (int S = ...)
15738
15739 we should treat the "S" as a declarator, not as a
15740 type-specifier. The standard doesn't say that explicitly for
15741 type-specifier-seq, but it does say that for
15742 decl-specifier-seq in an ordinary declaration. Perhaps it
15743 would be clearer just to allow a decl-specifier-seq here, and
15744 then add a semantic restriction that if any decl-specifiers
15745 that are not type-specifiers appear, the program is invalid. */
15746 if (is_declaration && !is_cv_qualifier)
15747 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15748 }
15749
15750 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15751 }
15752
15753 /* Parse a parameter-declaration-clause.
15754
15755 parameter-declaration-clause:
15756 parameter-declaration-list [opt] ... [opt]
15757 parameter-declaration-list , ...
15758
15759 Returns a representation for the parameter declarations. A return
15760 value of NULL indicates a parameter-declaration-clause consisting
15761 only of an ellipsis. */
15762
15763 static tree
15764 cp_parser_parameter_declaration_clause (cp_parser* parser)
15765 {
15766 tree parameters;
15767 cp_token *token;
15768 bool ellipsis_p;
15769 bool is_error;
15770
15771 /* Peek at the next token. */
15772 token = cp_lexer_peek_token (parser->lexer);
15773 /* Check for trivial parameter-declaration-clauses. */
15774 if (token->type == CPP_ELLIPSIS)
15775 {
15776 /* Consume the `...' token. */
15777 cp_lexer_consume_token (parser->lexer);
15778 return NULL_TREE;
15779 }
15780 else if (token->type == CPP_CLOSE_PAREN)
15781 /* There are no parameters. */
15782 {
15783 #ifndef NO_IMPLICIT_EXTERN_C
15784 if (in_system_header && current_class_type == NULL
15785 && current_lang_name == lang_name_c)
15786 return NULL_TREE;
15787 else
15788 #endif
15789 return void_list_node;
15790 }
15791 /* Check for `(void)', too, which is a special case. */
15792 else if (token->keyword == RID_VOID
15793 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15794 == CPP_CLOSE_PAREN))
15795 {
15796 /* Consume the `void' token. */
15797 cp_lexer_consume_token (parser->lexer);
15798 /* There are no parameters. */
15799 return void_list_node;
15800 }
15801
15802 /* Parse the parameter-declaration-list. */
15803 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15804 /* If a parse error occurred while parsing the
15805 parameter-declaration-list, then the entire
15806 parameter-declaration-clause is erroneous. */
15807 if (is_error)
15808 return NULL;
15809
15810 /* Peek at the next token. */
15811 token = cp_lexer_peek_token (parser->lexer);
15812 /* If it's a `,', the clause should terminate with an ellipsis. */
15813 if (token->type == CPP_COMMA)
15814 {
15815 /* Consume the `,'. */
15816 cp_lexer_consume_token (parser->lexer);
15817 /* Expect an ellipsis. */
15818 ellipsis_p
15819 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15820 }
15821 /* It might also be `...' if the optional trailing `,' was
15822 omitted. */
15823 else if (token->type == CPP_ELLIPSIS)
15824 {
15825 /* Consume the `...' token. */
15826 cp_lexer_consume_token (parser->lexer);
15827 /* And remember that we saw it. */
15828 ellipsis_p = true;
15829 }
15830 else
15831 ellipsis_p = false;
15832
15833 /* Finish the parameter list. */
15834 if (!ellipsis_p)
15835 parameters = chainon (parameters, void_list_node);
15836
15837 return parameters;
15838 }
15839
15840 /* Parse a parameter-declaration-list.
15841
15842 parameter-declaration-list:
15843 parameter-declaration
15844 parameter-declaration-list , parameter-declaration
15845
15846 Returns a representation of the parameter-declaration-list, as for
15847 cp_parser_parameter_declaration_clause. However, the
15848 `void_list_node' is never appended to the list. Upon return,
15849 *IS_ERROR will be true iff an error occurred. */
15850
15851 static tree
15852 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15853 {
15854 tree parameters = NULL_TREE;
15855 tree *tail = &parameters;
15856 bool saved_in_unbraced_linkage_specification_p;
15857 int index = 0;
15858
15859 /* Assume all will go well. */
15860 *is_error = false;
15861 /* The special considerations that apply to a function within an
15862 unbraced linkage specifications do not apply to the parameters
15863 to the function. */
15864 saved_in_unbraced_linkage_specification_p
15865 = parser->in_unbraced_linkage_specification_p;
15866 parser->in_unbraced_linkage_specification_p = false;
15867
15868 /* Look for more parameters. */
15869 while (true)
15870 {
15871 cp_parameter_declarator *parameter;
15872 tree decl = error_mark_node;
15873 bool parenthesized_p;
15874 /* Parse the parameter. */
15875 parameter
15876 = cp_parser_parameter_declaration (parser,
15877 /*template_parm_p=*/false,
15878 &parenthesized_p);
15879
15880 /* We don't know yet if the enclosing context is deprecated, so wait
15881 and warn in grokparms if appropriate. */
15882 deprecated_state = DEPRECATED_SUPPRESS;
15883
15884 if (parameter)
15885 decl = grokdeclarator (parameter->declarator,
15886 &parameter->decl_specifiers,
15887 PARM,
15888 parameter->default_argument != NULL_TREE,
15889 &parameter->decl_specifiers.attributes);
15890
15891 deprecated_state = DEPRECATED_NORMAL;
15892
15893 /* If a parse error occurred parsing the parameter declaration,
15894 then the entire parameter-declaration-list is erroneous. */
15895 if (decl == error_mark_node)
15896 {
15897 *is_error = true;
15898 parameters = error_mark_node;
15899 break;
15900 }
15901
15902 if (parameter->decl_specifiers.attributes)
15903 cplus_decl_attributes (&decl,
15904 parameter->decl_specifiers.attributes,
15905 0);
15906 if (DECL_NAME (decl))
15907 decl = pushdecl (decl);
15908
15909 if (decl != error_mark_node)
15910 {
15911 retrofit_lang_decl (decl);
15912 DECL_PARM_INDEX (decl) = ++index;
15913 }
15914
15915 /* Add the new parameter to the list. */
15916 *tail = build_tree_list (parameter->default_argument, decl);
15917 tail = &TREE_CHAIN (*tail);
15918
15919 /* Peek at the next token. */
15920 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15921 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15922 /* These are for Objective-C++ */
15923 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15924 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15925 /* The parameter-declaration-list is complete. */
15926 break;
15927 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15928 {
15929 cp_token *token;
15930
15931 /* Peek at the next token. */
15932 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15933 /* If it's an ellipsis, then the list is complete. */
15934 if (token->type == CPP_ELLIPSIS)
15935 break;
15936 /* Otherwise, there must be more parameters. Consume the
15937 `,'. */
15938 cp_lexer_consume_token (parser->lexer);
15939 /* When parsing something like:
15940
15941 int i(float f, double d)
15942
15943 we can tell after seeing the declaration for "f" that we
15944 are not looking at an initialization of a variable "i",
15945 but rather at the declaration of a function "i".
15946
15947 Due to the fact that the parsing of template arguments
15948 (as specified to a template-id) requires backtracking we
15949 cannot use this technique when inside a template argument
15950 list. */
15951 if (!parser->in_template_argument_list_p
15952 && !parser->in_type_id_in_expr_p
15953 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15954 /* However, a parameter-declaration of the form
15955 "foat(f)" (which is a valid declaration of a
15956 parameter "f") can also be interpreted as an
15957 expression (the conversion of "f" to "float"). */
15958 && !parenthesized_p)
15959 cp_parser_commit_to_tentative_parse (parser);
15960 }
15961 else
15962 {
15963 cp_parser_error (parser, "expected %<,%> or %<...%>");
15964 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15965 cp_parser_skip_to_closing_parenthesis (parser,
15966 /*recovering=*/true,
15967 /*or_comma=*/false,
15968 /*consume_paren=*/false);
15969 break;
15970 }
15971 }
15972
15973 parser->in_unbraced_linkage_specification_p
15974 = saved_in_unbraced_linkage_specification_p;
15975
15976 return parameters;
15977 }
15978
15979 /* Parse a parameter declaration.
15980
15981 parameter-declaration:
15982 decl-specifier-seq ... [opt] declarator
15983 decl-specifier-seq declarator = assignment-expression
15984 decl-specifier-seq ... [opt] abstract-declarator [opt]
15985 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15986
15987 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15988 declares a template parameter. (In that case, a non-nested `>'
15989 token encountered during the parsing of the assignment-expression
15990 is not interpreted as a greater-than operator.)
15991
15992 Returns a representation of the parameter, or NULL if an error
15993 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15994 true iff the declarator is of the form "(p)". */
15995
15996 static cp_parameter_declarator *
15997 cp_parser_parameter_declaration (cp_parser *parser,
15998 bool template_parm_p,
15999 bool *parenthesized_p)
16000 {
16001 int declares_class_or_enum;
16002 cp_decl_specifier_seq decl_specifiers;
16003 cp_declarator *declarator;
16004 tree default_argument;
16005 cp_token *token = NULL, *declarator_token_start = NULL;
16006 const char *saved_message;
16007
16008 /* In a template parameter, `>' is not an operator.
16009
16010 [temp.param]
16011
16012 When parsing a default template-argument for a non-type
16013 template-parameter, the first non-nested `>' is taken as the end
16014 of the template parameter-list rather than a greater-than
16015 operator. */
16016
16017 /* Type definitions may not appear in parameter types. */
16018 saved_message = parser->type_definition_forbidden_message;
16019 parser->type_definition_forbidden_message
16020 = G_("types may not be defined in parameter types");
16021
16022 /* Parse the declaration-specifiers. */
16023 cp_parser_decl_specifier_seq (parser,
16024 CP_PARSER_FLAGS_NONE,
16025 &decl_specifiers,
16026 &declares_class_or_enum);
16027
16028 /* Complain about missing 'typename' or other invalid type names. */
16029 if (!decl_specifiers.any_type_specifiers_p)
16030 cp_parser_parse_and_diagnose_invalid_type_name (parser);
16031
16032 /* If an error occurred, there's no reason to attempt to parse the
16033 rest of the declaration. */
16034 if (cp_parser_error_occurred (parser))
16035 {
16036 parser->type_definition_forbidden_message = saved_message;
16037 return NULL;
16038 }
16039
16040 /* Peek at the next token. */
16041 token = cp_lexer_peek_token (parser->lexer);
16042
16043 /* If the next token is a `)', `,', `=', `>', or `...', then there
16044 is no declarator. However, when variadic templates are enabled,
16045 there may be a declarator following `...'. */
16046 if (token->type == CPP_CLOSE_PAREN
16047 || token->type == CPP_COMMA
16048 || token->type == CPP_EQ
16049 || token->type == CPP_GREATER)
16050 {
16051 declarator = NULL;
16052 if (parenthesized_p)
16053 *parenthesized_p = false;
16054 }
16055 /* Otherwise, there should be a declarator. */
16056 else
16057 {
16058 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16059 parser->default_arg_ok_p = false;
16060
16061 /* After seeing a decl-specifier-seq, if the next token is not a
16062 "(", there is no possibility that the code is a valid
16063 expression. Therefore, if parsing tentatively, we commit at
16064 this point. */
16065 if (!parser->in_template_argument_list_p
16066 /* In an expression context, having seen:
16067
16068 (int((char ...
16069
16070 we cannot be sure whether we are looking at a
16071 function-type (taking a "char" as a parameter) or a cast
16072 of some object of type "char" to "int". */
16073 && !parser->in_type_id_in_expr_p
16074 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16075 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16076 cp_parser_commit_to_tentative_parse (parser);
16077 /* Parse the declarator. */
16078 declarator_token_start = token;
16079 declarator = cp_parser_declarator (parser,
16080 CP_PARSER_DECLARATOR_EITHER,
16081 /*ctor_dtor_or_conv_p=*/NULL,
16082 parenthesized_p,
16083 /*member_p=*/false);
16084 parser->default_arg_ok_p = saved_default_arg_ok_p;
16085 /* After the declarator, allow more attributes. */
16086 decl_specifiers.attributes
16087 = chainon (decl_specifiers.attributes,
16088 cp_parser_attributes_opt (parser));
16089 }
16090
16091 /* If the next token is an ellipsis, and we have not seen a
16092 declarator name, and the type of the declarator contains parameter
16093 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16094 a parameter pack expansion expression. Otherwise, leave the
16095 ellipsis for a C-style variadic function. */
16096 token = cp_lexer_peek_token (parser->lexer);
16097 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16098 {
16099 tree type = decl_specifiers.type;
16100
16101 if (type && DECL_P (type))
16102 type = TREE_TYPE (type);
16103
16104 if (type
16105 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16106 && declarator_can_be_parameter_pack (declarator)
16107 && (!declarator || !declarator->parameter_pack_p)
16108 && uses_parameter_packs (type))
16109 {
16110 /* Consume the `...'. */
16111 cp_lexer_consume_token (parser->lexer);
16112 maybe_warn_variadic_templates ();
16113
16114 /* Build a pack expansion type */
16115 if (declarator)
16116 declarator->parameter_pack_p = true;
16117 else
16118 decl_specifiers.type = make_pack_expansion (type);
16119 }
16120 }
16121
16122 /* The restriction on defining new types applies only to the type
16123 of the parameter, not to the default argument. */
16124 parser->type_definition_forbidden_message = saved_message;
16125
16126 /* If the next token is `=', then process a default argument. */
16127 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16128 {
16129 /* Consume the `='. */
16130 cp_lexer_consume_token (parser->lexer);
16131
16132 /* If we are defining a class, then the tokens that make up the
16133 default argument must be saved and processed later. */
16134 if (!template_parm_p && at_class_scope_p ()
16135 && TYPE_BEING_DEFINED (current_class_type)
16136 && !LAMBDA_TYPE_P (current_class_type))
16137 {
16138 unsigned depth = 0;
16139 int maybe_template_id = 0;
16140 cp_token *first_token;
16141 cp_token *token;
16142
16143 /* Add tokens until we have processed the entire default
16144 argument. We add the range [first_token, token). */
16145 first_token = cp_lexer_peek_token (parser->lexer);
16146 while (true)
16147 {
16148 bool done = false;
16149
16150 /* Peek at the next token. */
16151 token = cp_lexer_peek_token (parser->lexer);
16152 /* What we do depends on what token we have. */
16153 switch (token->type)
16154 {
16155 /* In valid code, a default argument must be
16156 immediately followed by a `,' `)', or `...'. */
16157 case CPP_COMMA:
16158 if (depth == 0 && maybe_template_id)
16159 {
16160 /* If we've seen a '<', we might be in a
16161 template-argument-list. Until Core issue 325 is
16162 resolved, we don't know how this situation ought
16163 to be handled, so try to DTRT. We check whether
16164 what comes after the comma is a valid parameter
16165 declaration list. If it is, then the comma ends
16166 the default argument; otherwise the default
16167 argument continues. */
16168 bool error = false;
16169 tree t;
16170
16171 /* Set ITALP so cp_parser_parameter_declaration_list
16172 doesn't decide to commit to this parse. */
16173 bool saved_italp = parser->in_template_argument_list_p;
16174 parser->in_template_argument_list_p = true;
16175
16176 cp_parser_parse_tentatively (parser);
16177 cp_lexer_consume_token (parser->lexer);
16178 begin_scope (sk_function_parms, NULL_TREE);
16179 cp_parser_parameter_declaration_list (parser, &error);
16180 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16181 pop_binding (DECL_NAME (t), t);
16182 leave_scope ();
16183 if (!cp_parser_error_occurred (parser) && !error)
16184 done = true;
16185 cp_parser_abort_tentative_parse (parser);
16186
16187 parser->in_template_argument_list_p = saved_italp;
16188 break;
16189 }
16190 case CPP_CLOSE_PAREN:
16191 case CPP_ELLIPSIS:
16192 /* If we run into a non-nested `;', `}', or `]',
16193 then the code is invalid -- but the default
16194 argument is certainly over. */
16195 case CPP_SEMICOLON:
16196 case CPP_CLOSE_BRACE:
16197 case CPP_CLOSE_SQUARE:
16198 if (depth == 0)
16199 done = true;
16200 /* Update DEPTH, if necessary. */
16201 else if (token->type == CPP_CLOSE_PAREN
16202 || token->type == CPP_CLOSE_BRACE
16203 || token->type == CPP_CLOSE_SQUARE)
16204 --depth;
16205 break;
16206
16207 case CPP_OPEN_PAREN:
16208 case CPP_OPEN_SQUARE:
16209 case CPP_OPEN_BRACE:
16210 ++depth;
16211 break;
16212
16213 case CPP_LESS:
16214 if (depth == 0)
16215 /* This might be the comparison operator, or it might
16216 start a template argument list. */
16217 ++maybe_template_id;
16218 break;
16219
16220 case CPP_RSHIFT:
16221 if (cxx_dialect == cxx98)
16222 break;
16223 /* Fall through for C++0x, which treats the `>>'
16224 operator like two `>' tokens in certain
16225 cases. */
16226
16227 case CPP_GREATER:
16228 if (depth == 0)
16229 {
16230 /* This might be an operator, or it might close a
16231 template argument list. But if a previous '<'
16232 started a template argument list, this will have
16233 closed it, so we can't be in one anymore. */
16234 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16235 if (maybe_template_id < 0)
16236 maybe_template_id = 0;
16237 }
16238 break;
16239
16240 /* If we run out of tokens, issue an error message. */
16241 case CPP_EOF:
16242 case CPP_PRAGMA_EOL:
16243 error_at (token->location, "file ends in default argument");
16244 done = true;
16245 break;
16246
16247 case CPP_NAME:
16248 case CPP_SCOPE:
16249 /* In these cases, we should look for template-ids.
16250 For example, if the default argument is
16251 `X<int, double>()', we need to do name lookup to
16252 figure out whether or not `X' is a template; if
16253 so, the `,' does not end the default argument.
16254
16255 That is not yet done. */
16256 break;
16257
16258 default:
16259 break;
16260 }
16261
16262 /* If we've reached the end, stop. */
16263 if (done)
16264 break;
16265
16266 /* Add the token to the token block. */
16267 token = cp_lexer_consume_token (parser->lexer);
16268 }
16269
16270 /* Create a DEFAULT_ARG to represent the unparsed default
16271 argument. */
16272 default_argument = make_node (DEFAULT_ARG);
16273 DEFARG_TOKENS (default_argument)
16274 = cp_token_cache_new (first_token, token);
16275 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16276 }
16277 /* Outside of a class definition, we can just parse the
16278 assignment-expression. */
16279 else
16280 {
16281 token = cp_lexer_peek_token (parser->lexer);
16282 default_argument
16283 = cp_parser_default_argument (parser, template_parm_p);
16284 }
16285
16286 if (!parser->default_arg_ok_p)
16287 {
16288 if (flag_permissive)
16289 warning (0, "deprecated use of default argument for parameter of non-function");
16290 else
16291 {
16292 error_at (token->location,
16293 "default arguments are only "
16294 "permitted for function parameters");
16295 default_argument = NULL_TREE;
16296 }
16297 }
16298 else if ((declarator && declarator->parameter_pack_p)
16299 || (decl_specifiers.type
16300 && PACK_EXPANSION_P (decl_specifiers.type)))
16301 {
16302 /* Find the name of the parameter pack. */
16303 cp_declarator *id_declarator = declarator;
16304 while (id_declarator && id_declarator->kind != cdk_id)
16305 id_declarator = id_declarator->declarator;
16306
16307 if (id_declarator && id_declarator->kind == cdk_id)
16308 error_at (declarator_token_start->location,
16309 template_parm_p
16310 ? "template parameter pack %qD"
16311 " cannot have a default argument"
16312 : "parameter pack %qD cannot have a default argument",
16313 id_declarator->u.id.unqualified_name);
16314 else
16315 error_at (declarator_token_start->location,
16316 template_parm_p
16317 ? "template parameter pack cannot have a default argument"
16318 : "parameter pack cannot have a default argument");
16319
16320 default_argument = NULL_TREE;
16321 }
16322 }
16323 else
16324 default_argument = NULL_TREE;
16325
16326 return make_parameter_declarator (&decl_specifiers,
16327 declarator,
16328 default_argument);
16329 }
16330
16331 /* Parse a default argument and return it.
16332
16333 TEMPLATE_PARM_P is true if this is a default argument for a
16334 non-type template parameter. */
16335 static tree
16336 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16337 {
16338 tree default_argument = NULL_TREE;
16339 bool saved_greater_than_is_operator_p;
16340 bool saved_local_variables_forbidden_p;
16341
16342 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16343 set correctly. */
16344 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16345 parser->greater_than_is_operator_p = !template_parm_p;
16346 /* Local variable names (and the `this' keyword) may not
16347 appear in a default argument. */
16348 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16349 parser->local_variables_forbidden_p = true;
16350 /* Parse the assignment-expression. */
16351 if (template_parm_p)
16352 push_deferring_access_checks (dk_no_deferred);
16353 default_argument
16354 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16355 if (template_parm_p)
16356 pop_deferring_access_checks ();
16357 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16358 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16359
16360 return default_argument;
16361 }
16362
16363 /* Parse a function-body.
16364
16365 function-body:
16366 compound_statement */
16367
16368 static void
16369 cp_parser_function_body (cp_parser *parser)
16370 {
16371 cp_parser_compound_statement (parser, NULL, false);
16372 }
16373
16374 /* Parse a ctor-initializer-opt followed by a function-body. Return
16375 true if a ctor-initializer was present. */
16376
16377 static bool
16378 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16379 {
16380 tree body, list;
16381 bool ctor_initializer_p;
16382 const bool check_body_p =
16383 DECL_CONSTRUCTOR_P (current_function_decl)
16384 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16385 tree last = NULL;
16386
16387 /* Begin the function body. */
16388 body = begin_function_body ();
16389 /* Parse the optional ctor-initializer. */
16390 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16391
16392 /* If we're parsing a constexpr constructor definition, we need
16393 to check that the constructor body is indeed empty. However,
16394 before we get to cp_parser_function_body lot of junk has been
16395 generated, so we can't just check that we have an empty block.
16396 Rather we take a snapshot of the outermost block, and check whether
16397 cp_parser_function_body changed its state. */
16398 if (check_body_p)
16399 {
16400 list = body;
16401 if (TREE_CODE (list) == BIND_EXPR)
16402 list = BIND_EXPR_BODY (list);
16403 if (TREE_CODE (list) == STATEMENT_LIST
16404 && STATEMENT_LIST_TAIL (list) != NULL)
16405 last = STATEMENT_LIST_TAIL (list)->stmt;
16406 }
16407 /* Parse the function-body. */
16408 cp_parser_function_body (parser);
16409 if (check_body_p)
16410 check_constexpr_ctor_body (last, list);
16411 /* Finish the function body. */
16412 finish_function_body (body);
16413
16414 return ctor_initializer_p;
16415 }
16416
16417 /* Parse an initializer.
16418
16419 initializer:
16420 = initializer-clause
16421 ( expression-list )
16422
16423 Returns an expression representing the initializer. If no
16424 initializer is present, NULL_TREE is returned.
16425
16426 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16427 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16428 set to TRUE if there is no initializer present. If there is an
16429 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16430 is set to true; otherwise it is set to false. */
16431
16432 static tree
16433 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16434 bool* non_constant_p)
16435 {
16436 cp_token *token;
16437 tree init;
16438
16439 /* Peek at the next token. */
16440 token = cp_lexer_peek_token (parser->lexer);
16441
16442 /* Let our caller know whether or not this initializer was
16443 parenthesized. */
16444 *is_direct_init = (token->type != CPP_EQ);
16445 /* Assume that the initializer is constant. */
16446 *non_constant_p = false;
16447
16448 if (token->type == CPP_EQ)
16449 {
16450 /* Consume the `='. */
16451 cp_lexer_consume_token (parser->lexer);
16452 /* Parse the initializer-clause. */
16453 init = cp_parser_initializer_clause (parser, non_constant_p);
16454 }
16455 else if (token->type == CPP_OPEN_PAREN)
16456 {
16457 VEC(tree,gc) *vec;
16458 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16459 /*cast_p=*/false,
16460 /*allow_expansion_p=*/true,
16461 non_constant_p);
16462 if (vec == NULL)
16463 return error_mark_node;
16464 init = build_tree_list_vec (vec);
16465 release_tree_vector (vec);
16466 }
16467 else if (token->type == CPP_OPEN_BRACE)
16468 {
16469 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16470 init = cp_parser_braced_list (parser, non_constant_p);
16471 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16472 }
16473 else
16474 {
16475 /* Anything else is an error. */
16476 cp_parser_error (parser, "expected initializer");
16477 init = error_mark_node;
16478 }
16479
16480 return init;
16481 }
16482
16483 /* Parse an initializer-clause.
16484
16485 initializer-clause:
16486 assignment-expression
16487 braced-init-list
16488
16489 Returns an expression representing the initializer.
16490
16491 If the `assignment-expression' production is used the value
16492 returned is simply a representation for the expression.
16493
16494 Otherwise, calls cp_parser_braced_list. */
16495
16496 static tree
16497 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16498 {
16499 tree initializer;
16500
16501 /* Assume the expression is constant. */
16502 *non_constant_p = false;
16503
16504 /* If it is not a `{', then we are looking at an
16505 assignment-expression. */
16506 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16507 {
16508 initializer
16509 = cp_parser_constant_expression (parser,
16510 /*allow_non_constant_p=*/true,
16511 non_constant_p);
16512 if (!*non_constant_p)
16513 {
16514 /* We only want to fold if this is really a constant
16515 expression. FIXME Actually, we don't want to fold here, but in
16516 cp_finish_decl. */
16517 tree folded = fold_non_dependent_expr (initializer);
16518 folded = maybe_constant_value (folded);
16519 if (TREE_CONSTANT (folded))
16520 initializer = folded;
16521 }
16522 }
16523 else
16524 initializer = cp_parser_braced_list (parser, non_constant_p);
16525
16526 return initializer;
16527 }
16528
16529 /* Parse a brace-enclosed initializer list.
16530
16531 braced-init-list:
16532 { initializer-list , [opt] }
16533 { }
16534
16535 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16536 the elements of the initializer-list (or NULL, if the last
16537 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16538 NULL_TREE. There is no way to detect whether or not the optional
16539 trailing `,' was provided. NON_CONSTANT_P is as for
16540 cp_parser_initializer. */
16541
16542 static tree
16543 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16544 {
16545 tree initializer;
16546
16547 /* Consume the `{' token. */
16548 cp_lexer_consume_token (parser->lexer);
16549 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16550 initializer = make_node (CONSTRUCTOR);
16551 /* If it's not a `}', then there is a non-trivial initializer. */
16552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16553 {
16554 /* Parse the initializer list. */
16555 CONSTRUCTOR_ELTS (initializer)
16556 = cp_parser_initializer_list (parser, non_constant_p);
16557 /* A trailing `,' token is allowed. */
16558 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16559 cp_lexer_consume_token (parser->lexer);
16560 }
16561 /* Now, there should be a trailing `}'. */
16562 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16563 TREE_TYPE (initializer) = init_list_type_node;
16564 return initializer;
16565 }
16566
16567 /* Parse an initializer-list.
16568
16569 initializer-list:
16570 initializer-clause ... [opt]
16571 initializer-list , initializer-clause ... [opt]
16572
16573 GNU Extension:
16574
16575 initializer-list:
16576 identifier : initializer-clause
16577 initializer-list, identifier : initializer-clause
16578
16579 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16580 for the initializer. If the INDEX of the elt is non-NULL, it is the
16581 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16582 as for cp_parser_initializer. */
16583
16584 static VEC(constructor_elt,gc) *
16585 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16586 {
16587 VEC(constructor_elt,gc) *v = NULL;
16588
16589 /* Assume all of the expressions are constant. */
16590 *non_constant_p = false;
16591
16592 /* Parse the rest of the list. */
16593 while (true)
16594 {
16595 cp_token *token;
16596 tree identifier;
16597 tree initializer;
16598 bool clause_non_constant_p;
16599
16600 /* If the next token is an identifier and the following one is a
16601 colon, we are looking at the GNU designated-initializer
16602 syntax. */
16603 if (cp_parser_allow_gnu_extensions_p (parser)
16604 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16605 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16606 {
16607 /* Warn the user that they are using an extension. */
16608 pedwarn (input_location, OPT_pedantic,
16609 "ISO C++ does not allow designated initializers");
16610 /* Consume the identifier. */
16611 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16612 /* Consume the `:'. */
16613 cp_lexer_consume_token (parser->lexer);
16614 }
16615 else
16616 identifier = NULL_TREE;
16617
16618 /* Parse the initializer. */
16619 initializer = cp_parser_initializer_clause (parser,
16620 &clause_non_constant_p);
16621 /* If any clause is non-constant, so is the entire initializer. */
16622 if (clause_non_constant_p)
16623 *non_constant_p = true;
16624
16625 /* If we have an ellipsis, this is an initializer pack
16626 expansion. */
16627 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16628 {
16629 /* Consume the `...'. */
16630 cp_lexer_consume_token (parser->lexer);
16631
16632 /* Turn the initializer into an initializer expansion. */
16633 initializer = make_pack_expansion (initializer);
16634 }
16635
16636 /* Add it to the vector. */
16637 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16638
16639 /* If the next token is not a comma, we have reached the end of
16640 the list. */
16641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16642 break;
16643
16644 /* Peek at the next token. */
16645 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16646 /* If the next token is a `}', then we're still done. An
16647 initializer-clause can have a trailing `,' after the
16648 initializer-list and before the closing `}'. */
16649 if (token->type == CPP_CLOSE_BRACE)
16650 break;
16651
16652 /* Consume the `,' token. */
16653 cp_lexer_consume_token (parser->lexer);
16654 }
16655
16656 return v;
16657 }
16658
16659 /* Classes [gram.class] */
16660
16661 /* Parse a class-name.
16662
16663 class-name:
16664 identifier
16665 template-id
16666
16667 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16668 to indicate that names looked up in dependent types should be
16669 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16670 keyword has been used to indicate that the name that appears next
16671 is a template. TAG_TYPE indicates the explicit tag given before
16672 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16673 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16674 is the class being defined in a class-head.
16675
16676 Returns the TYPE_DECL representing the class. */
16677
16678 static tree
16679 cp_parser_class_name (cp_parser *parser,
16680 bool typename_keyword_p,
16681 bool template_keyword_p,
16682 enum tag_types tag_type,
16683 bool check_dependency_p,
16684 bool class_head_p,
16685 bool is_declaration)
16686 {
16687 tree decl;
16688 tree scope;
16689 bool typename_p;
16690 cp_token *token;
16691 tree identifier = NULL_TREE;
16692
16693 /* All class-names start with an identifier. */
16694 token = cp_lexer_peek_token (parser->lexer);
16695 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16696 {
16697 cp_parser_error (parser, "expected class-name");
16698 return error_mark_node;
16699 }
16700
16701 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16702 to a template-id, so we save it here. */
16703 scope = parser->scope;
16704 if (scope == error_mark_node)
16705 return error_mark_node;
16706
16707 /* Any name names a type if we're following the `typename' keyword
16708 in a qualified name where the enclosing scope is type-dependent. */
16709 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16710 && dependent_type_p (scope));
16711 /* Handle the common case (an identifier, but not a template-id)
16712 efficiently. */
16713 if (token->type == CPP_NAME
16714 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16715 {
16716 cp_token *identifier_token;
16717 bool ambiguous_p;
16718
16719 /* Look for the identifier. */
16720 identifier_token = cp_lexer_peek_token (parser->lexer);
16721 ambiguous_p = identifier_token->ambiguous_p;
16722 identifier = cp_parser_identifier (parser);
16723 /* If the next token isn't an identifier, we are certainly not
16724 looking at a class-name. */
16725 if (identifier == error_mark_node)
16726 decl = error_mark_node;
16727 /* If we know this is a type-name, there's no need to look it
16728 up. */
16729 else if (typename_p)
16730 decl = identifier;
16731 else
16732 {
16733 tree ambiguous_decls;
16734 /* If we already know that this lookup is ambiguous, then
16735 we've already issued an error message; there's no reason
16736 to check again. */
16737 if (ambiguous_p)
16738 {
16739 cp_parser_simulate_error (parser);
16740 return error_mark_node;
16741 }
16742 /* If the next token is a `::', then the name must be a type
16743 name.
16744
16745 [basic.lookup.qual]
16746
16747 During the lookup for a name preceding the :: scope
16748 resolution operator, object, function, and enumerator
16749 names are ignored. */
16750 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16751 tag_type = typename_type;
16752 /* Look up the name. */
16753 decl = cp_parser_lookup_name (parser, identifier,
16754 tag_type,
16755 /*is_template=*/false,
16756 /*is_namespace=*/false,
16757 check_dependency_p,
16758 &ambiguous_decls,
16759 identifier_token->location);
16760 if (ambiguous_decls)
16761 {
16762 if (cp_parser_parsing_tentatively (parser))
16763 cp_parser_simulate_error (parser);
16764 return error_mark_node;
16765 }
16766 }
16767 }
16768 else
16769 {
16770 /* Try a template-id. */
16771 decl = cp_parser_template_id (parser, template_keyword_p,
16772 check_dependency_p,
16773 is_declaration);
16774 if (decl == error_mark_node)
16775 return error_mark_node;
16776 }
16777
16778 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16779
16780 /* If this is a typename, create a TYPENAME_TYPE. */
16781 if (typename_p && decl != error_mark_node)
16782 {
16783 decl = make_typename_type (scope, decl, typename_type,
16784 /*complain=*/tf_error);
16785 if (decl != error_mark_node)
16786 decl = TYPE_NAME (decl);
16787 }
16788
16789 /* Check to see that it is really the name of a class. */
16790 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16791 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16792 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16793 /* Situations like this:
16794
16795 template <typename T> struct A {
16796 typename T::template X<int>::I i;
16797 };
16798
16799 are problematic. Is `T::template X<int>' a class-name? The
16800 standard does not seem to be definitive, but there is no other
16801 valid interpretation of the following `::'. Therefore, those
16802 names are considered class-names. */
16803 {
16804 decl = make_typename_type (scope, decl, tag_type, tf_error);
16805 if (decl != error_mark_node)
16806 decl = TYPE_NAME (decl);
16807 }
16808 else if (TREE_CODE (decl) != TYPE_DECL
16809 || TREE_TYPE (decl) == error_mark_node
16810 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16811 /* In Objective-C 2.0, a classname followed by '.' starts a
16812 dot-syntax expression, and it's not a type-name. */
16813 || (c_dialect_objc ()
16814 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
16815 && objc_is_class_name (decl)))
16816 decl = error_mark_node;
16817
16818 if (decl == error_mark_node)
16819 cp_parser_error (parser, "expected class-name");
16820 else if (identifier && !parser->scope)
16821 maybe_note_name_used_in_class (identifier, decl);
16822
16823 return decl;
16824 }
16825
16826 /* Parse a class-specifier.
16827
16828 class-specifier:
16829 class-head { member-specification [opt] }
16830
16831 Returns the TREE_TYPE representing the class. */
16832
16833 static tree
16834 cp_parser_class_specifier (cp_parser* parser)
16835 {
16836 tree type;
16837 tree attributes = NULL_TREE;
16838 bool nested_name_specifier_p;
16839 unsigned saved_num_template_parameter_lists;
16840 bool saved_in_function_body;
16841 bool saved_in_unbraced_linkage_specification_p;
16842 tree old_scope = NULL_TREE;
16843 tree scope = NULL_TREE;
16844 tree bases;
16845
16846 push_deferring_access_checks (dk_no_deferred);
16847
16848 /* Parse the class-head. */
16849 type = cp_parser_class_head (parser,
16850 &nested_name_specifier_p,
16851 &attributes,
16852 &bases);
16853 /* If the class-head was a semantic disaster, skip the entire body
16854 of the class. */
16855 if (!type)
16856 {
16857 cp_parser_skip_to_end_of_block_or_statement (parser);
16858 pop_deferring_access_checks ();
16859 return error_mark_node;
16860 }
16861
16862 /* Look for the `{'. */
16863 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16864 {
16865 pop_deferring_access_checks ();
16866 return error_mark_node;
16867 }
16868
16869 /* Process the base classes. If they're invalid, skip the
16870 entire class body. */
16871 if (!xref_basetypes (type, bases))
16872 {
16873 /* Consuming the closing brace yields better error messages
16874 later on. */
16875 if (cp_parser_skip_to_closing_brace (parser))
16876 cp_lexer_consume_token (parser->lexer);
16877 pop_deferring_access_checks ();
16878 return error_mark_node;
16879 }
16880
16881 /* Issue an error message if type-definitions are forbidden here. */
16882 cp_parser_check_type_definition (parser);
16883 /* Remember that we are defining one more class. */
16884 ++parser->num_classes_being_defined;
16885 /* Inside the class, surrounding template-parameter-lists do not
16886 apply. */
16887 saved_num_template_parameter_lists
16888 = parser->num_template_parameter_lists;
16889 parser->num_template_parameter_lists = 0;
16890 /* We are not in a function body. */
16891 saved_in_function_body = parser->in_function_body;
16892 parser->in_function_body = false;
16893 /* We are not immediately inside an extern "lang" block. */
16894 saved_in_unbraced_linkage_specification_p
16895 = parser->in_unbraced_linkage_specification_p;
16896 parser->in_unbraced_linkage_specification_p = false;
16897
16898 /* Start the class. */
16899 if (nested_name_specifier_p)
16900 {
16901 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16902 old_scope = push_inner_scope (scope);
16903 }
16904 type = begin_class_definition (type, attributes);
16905
16906 if (type == error_mark_node)
16907 /* If the type is erroneous, skip the entire body of the class. */
16908 cp_parser_skip_to_closing_brace (parser);
16909 else
16910 /* Parse the member-specification. */
16911 cp_parser_member_specification_opt (parser);
16912
16913 /* Look for the trailing `}'. */
16914 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16915 /* Look for trailing attributes to apply to this class. */
16916 if (cp_parser_allow_gnu_extensions_p (parser))
16917 attributes = cp_parser_attributes_opt (parser);
16918 if (type != error_mark_node)
16919 type = finish_struct (type, attributes);
16920 if (nested_name_specifier_p)
16921 pop_inner_scope (old_scope, scope);
16922
16923 /* We've finished a type definition. Check for the common syntax
16924 error of forgetting a semicolon after the definition. We need to
16925 be careful, as we can't just check for not-a-semicolon and be done
16926 with it; the user might have typed:
16927
16928 class X { } c = ...;
16929 class X { } *p = ...;
16930
16931 and so forth. Instead, enumerate all the possible tokens that
16932 might follow this production; if we don't see one of them, then
16933 complain and silently insert the semicolon. */
16934 {
16935 cp_token *token = cp_lexer_peek_token (parser->lexer);
16936 bool want_semicolon = true;
16937
16938 switch (token->type)
16939 {
16940 case CPP_NAME:
16941 case CPP_SEMICOLON:
16942 case CPP_MULT:
16943 case CPP_AND:
16944 case CPP_OPEN_PAREN:
16945 case CPP_CLOSE_PAREN:
16946 case CPP_COMMA:
16947 want_semicolon = false;
16948 break;
16949
16950 /* While it's legal for type qualifiers and storage class
16951 specifiers to follow type definitions in the grammar, only
16952 compiler testsuites contain code like that. Assume that if
16953 we see such code, then what we're really seeing is a case
16954 like:
16955
16956 class X { }
16957 const <type> var = ...;
16958
16959 or
16960
16961 class Y { }
16962 static <type> func (...) ...
16963
16964 i.e. the qualifier or specifier applies to the next
16965 declaration. To do so, however, we need to look ahead one
16966 more token to see if *that* token is a type specifier.
16967
16968 This code could be improved to handle:
16969
16970 class Z { }
16971 static const <type> var = ...; */
16972 case CPP_KEYWORD:
16973 if (keyword_is_storage_class_specifier (token->keyword)
16974 || keyword_is_type_qualifier (token->keyword))
16975 {
16976 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16977
16978 if (lookahead->type == CPP_KEYWORD
16979 && !keyword_begins_type_specifier (lookahead->keyword))
16980 want_semicolon = false;
16981 else if (lookahead->type == CPP_NAME)
16982 /* Handling user-defined types here would be nice, but
16983 very tricky. */
16984 want_semicolon = false;
16985 }
16986 break;
16987 default:
16988 break;
16989 }
16990
16991 /* If we don't have a type, then something is very wrong and we
16992 shouldn't try to do anything clever. */
16993 if (TYPE_P (type) && want_semicolon)
16994 {
16995 cp_token_position prev
16996 = cp_lexer_previous_token_position (parser->lexer);
16997 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16998 location_t loc = prev_token->location;
16999
17000 if (CLASSTYPE_DECLARED_CLASS (type))
17001 error_at (loc, "expected %<;%> after class definition");
17002 else if (TREE_CODE (type) == RECORD_TYPE)
17003 error_at (loc, "expected %<;%> after struct definition");
17004 else if (TREE_CODE (type) == UNION_TYPE)
17005 error_at (loc, "expected %<;%> after union definition");
17006 else
17007 gcc_unreachable ();
17008
17009 /* Unget one token and smash it to look as though we encountered
17010 a semicolon in the input stream. */
17011 cp_lexer_set_token_position (parser->lexer, prev);
17012 token = cp_lexer_peek_token (parser->lexer);
17013 token->type = CPP_SEMICOLON;
17014 token->keyword = RID_MAX;
17015 }
17016 }
17017
17018 /* If this class is not itself within the scope of another class,
17019 then we need to parse the bodies of all of the queued function
17020 definitions. Note that the queued functions defined in a class
17021 are not always processed immediately following the
17022 class-specifier for that class. Consider:
17023
17024 struct A {
17025 struct B { void f() { sizeof (A); } };
17026 };
17027
17028 If `f' were processed before the processing of `A' were
17029 completed, there would be no way to compute the size of `A'.
17030 Note that the nesting we are interested in here is lexical --
17031 not the semantic nesting given by TYPE_CONTEXT. In particular,
17032 for:
17033
17034 struct A { struct B; };
17035 struct A::B { void f() { } };
17036
17037 there is no need to delay the parsing of `A::B::f'. */
17038 if (--parser->num_classes_being_defined == 0)
17039 {
17040 tree fn;
17041 tree class_type = NULL_TREE;
17042 tree pushed_scope = NULL_TREE;
17043 unsigned ix;
17044 cp_default_arg_entry *e;
17045
17046 /* In a first pass, parse default arguments to the functions.
17047 Then, in a second pass, parse the bodies of the functions.
17048 This two-phased approach handles cases like:
17049
17050 struct S {
17051 void f() { g(); }
17052 void g(int i = 3);
17053 };
17054
17055 */
17056 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17057 ix, e)
17058 {
17059 fn = e->decl;
17060 /* If there are default arguments that have not yet been processed,
17061 take care of them now. */
17062 if (class_type != e->class_type)
17063 {
17064 if (pushed_scope)
17065 pop_scope (pushed_scope);
17066 class_type = e->class_type;
17067 pushed_scope = push_scope (class_type);
17068 }
17069 /* Make sure that any template parameters are in scope. */
17070 maybe_begin_member_template_processing (fn);
17071 /* Parse the default argument expressions. */
17072 cp_parser_late_parsing_default_args (parser, fn);
17073 /* Remove any template parameters from the symbol table. */
17074 maybe_end_member_template_processing ();
17075 }
17076 if (pushed_scope)
17077 pop_scope (pushed_scope);
17078 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17079 /* Now parse the body of the functions. */
17080 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17081 cp_parser_late_parsing_for_member (parser, fn);
17082 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17083 }
17084
17085 /* Put back any saved access checks. */
17086 pop_deferring_access_checks ();
17087
17088 /* Restore saved state. */
17089 parser->in_function_body = saved_in_function_body;
17090 parser->num_template_parameter_lists
17091 = saved_num_template_parameter_lists;
17092 parser->in_unbraced_linkage_specification_p
17093 = saved_in_unbraced_linkage_specification_p;
17094
17095 return type;
17096 }
17097
17098 /* Parse a class-head.
17099
17100 class-head:
17101 class-key identifier [opt] base-clause [opt]
17102 class-key nested-name-specifier identifier base-clause [opt]
17103 class-key nested-name-specifier [opt] template-id
17104 base-clause [opt]
17105
17106 GNU Extensions:
17107 class-key attributes identifier [opt] base-clause [opt]
17108 class-key attributes nested-name-specifier identifier base-clause [opt]
17109 class-key attributes nested-name-specifier [opt] template-id
17110 base-clause [opt]
17111
17112 Upon return BASES is initialized to the list of base classes (or
17113 NULL, if there are none) in the same form returned by
17114 cp_parser_base_clause.
17115
17116 Returns the TYPE of the indicated class. Sets
17117 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17118 involving a nested-name-specifier was used, and FALSE otherwise.
17119
17120 Returns error_mark_node if this is not a class-head.
17121
17122 Returns NULL_TREE if the class-head is syntactically valid, but
17123 semantically invalid in a way that means we should skip the entire
17124 body of the class. */
17125
17126 static tree
17127 cp_parser_class_head (cp_parser* parser,
17128 bool* nested_name_specifier_p,
17129 tree *attributes_p,
17130 tree *bases)
17131 {
17132 tree nested_name_specifier;
17133 enum tag_types class_key;
17134 tree id = NULL_TREE;
17135 tree type = NULL_TREE;
17136 tree attributes;
17137 bool template_id_p = false;
17138 bool qualified_p = false;
17139 bool invalid_nested_name_p = false;
17140 bool invalid_explicit_specialization_p = false;
17141 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17142 tree pushed_scope = NULL_TREE;
17143 unsigned num_templates;
17144 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17145 /* Assume no nested-name-specifier will be present. */
17146 *nested_name_specifier_p = false;
17147 /* Assume no template parameter lists will be used in defining the
17148 type. */
17149 num_templates = 0;
17150 parser->colon_corrects_to_scope_p = false;
17151
17152 *bases = NULL_TREE;
17153
17154 /* Look for the class-key. */
17155 class_key = cp_parser_class_key (parser);
17156 if (class_key == none_type)
17157 return error_mark_node;
17158
17159 /* Parse the attributes. */
17160 attributes = cp_parser_attributes_opt (parser);
17161
17162 /* If the next token is `::', that is invalid -- but sometimes
17163 people do try to write:
17164
17165 struct ::S {};
17166
17167 Handle this gracefully by accepting the extra qualifier, and then
17168 issuing an error about it later if this really is a
17169 class-head. If it turns out just to be an elaborated type
17170 specifier, remain silent. */
17171 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17172 qualified_p = true;
17173
17174 push_deferring_access_checks (dk_no_check);
17175
17176 /* Determine the name of the class. Begin by looking for an
17177 optional nested-name-specifier. */
17178 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17179 nested_name_specifier
17180 = cp_parser_nested_name_specifier_opt (parser,
17181 /*typename_keyword_p=*/false,
17182 /*check_dependency_p=*/false,
17183 /*type_p=*/false,
17184 /*is_declaration=*/false);
17185 /* If there was a nested-name-specifier, then there *must* be an
17186 identifier. */
17187 if (nested_name_specifier)
17188 {
17189 type_start_token = cp_lexer_peek_token (parser->lexer);
17190 /* Although the grammar says `identifier', it really means
17191 `class-name' or `template-name'. You are only allowed to
17192 define a class that has already been declared with this
17193 syntax.
17194
17195 The proposed resolution for Core Issue 180 says that wherever
17196 you see `class T::X' you should treat `X' as a type-name.
17197
17198 It is OK to define an inaccessible class; for example:
17199
17200 class A { class B; };
17201 class A::B {};
17202
17203 We do not know if we will see a class-name, or a
17204 template-name. We look for a class-name first, in case the
17205 class-name is a template-id; if we looked for the
17206 template-name first we would stop after the template-name. */
17207 cp_parser_parse_tentatively (parser);
17208 type = cp_parser_class_name (parser,
17209 /*typename_keyword_p=*/false,
17210 /*template_keyword_p=*/false,
17211 class_type,
17212 /*check_dependency_p=*/false,
17213 /*class_head_p=*/true,
17214 /*is_declaration=*/false);
17215 /* If that didn't work, ignore the nested-name-specifier. */
17216 if (!cp_parser_parse_definitely (parser))
17217 {
17218 invalid_nested_name_p = true;
17219 type_start_token = cp_lexer_peek_token (parser->lexer);
17220 id = cp_parser_identifier (parser);
17221 if (id == error_mark_node)
17222 id = NULL_TREE;
17223 }
17224 /* If we could not find a corresponding TYPE, treat this
17225 declaration like an unqualified declaration. */
17226 if (type == error_mark_node)
17227 nested_name_specifier = NULL_TREE;
17228 /* Otherwise, count the number of templates used in TYPE and its
17229 containing scopes. */
17230 else
17231 {
17232 tree scope;
17233
17234 for (scope = TREE_TYPE (type);
17235 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17236 scope = (TYPE_P (scope)
17237 ? TYPE_CONTEXT (scope)
17238 : DECL_CONTEXT (scope)))
17239 if (TYPE_P (scope)
17240 && CLASS_TYPE_P (scope)
17241 && CLASSTYPE_TEMPLATE_INFO (scope)
17242 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17243 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17244 ++num_templates;
17245 }
17246 }
17247 /* Otherwise, the identifier is optional. */
17248 else
17249 {
17250 /* We don't know whether what comes next is a template-id,
17251 an identifier, or nothing at all. */
17252 cp_parser_parse_tentatively (parser);
17253 /* Check for a template-id. */
17254 type_start_token = cp_lexer_peek_token (parser->lexer);
17255 id = cp_parser_template_id (parser,
17256 /*template_keyword_p=*/false,
17257 /*check_dependency_p=*/true,
17258 /*is_declaration=*/true);
17259 /* If that didn't work, it could still be an identifier. */
17260 if (!cp_parser_parse_definitely (parser))
17261 {
17262 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17263 {
17264 type_start_token = cp_lexer_peek_token (parser->lexer);
17265 id = cp_parser_identifier (parser);
17266 }
17267 else
17268 id = NULL_TREE;
17269 }
17270 else
17271 {
17272 template_id_p = true;
17273 ++num_templates;
17274 }
17275 }
17276
17277 pop_deferring_access_checks ();
17278
17279 if (id)
17280 cp_parser_check_for_invalid_template_id (parser, id,
17281 type_start_token->location);
17282
17283 /* If it's not a `:' or a `{' then we can't really be looking at a
17284 class-head, since a class-head only appears as part of a
17285 class-specifier. We have to detect this situation before calling
17286 xref_tag, since that has irreversible side-effects. */
17287 if (!cp_parser_next_token_starts_class_definition_p (parser))
17288 {
17289 cp_parser_error (parser, "expected %<{%> or %<:%>");
17290 type = error_mark_node;
17291 goto out;
17292 }
17293
17294 /* At this point, we're going ahead with the class-specifier, even
17295 if some other problem occurs. */
17296 cp_parser_commit_to_tentative_parse (parser);
17297 /* Issue the error about the overly-qualified name now. */
17298 if (qualified_p)
17299 {
17300 cp_parser_error (parser,
17301 "global qualification of class name is invalid");
17302 type = error_mark_node;
17303 goto out;
17304 }
17305 else if (invalid_nested_name_p)
17306 {
17307 cp_parser_error (parser,
17308 "qualified name does not name a class");
17309 type = error_mark_node;
17310 goto out;
17311 }
17312 else if (nested_name_specifier)
17313 {
17314 tree scope;
17315
17316 /* Reject typedef-names in class heads. */
17317 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17318 {
17319 error_at (type_start_token->location,
17320 "invalid class name in declaration of %qD",
17321 type);
17322 type = NULL_TREE;
17323 goto done;
17324 }
17325
17326 /* Figure out in what scope the declaration is being placed. */
17327 scope = current_scope ();
17328 /* If that scope does not contain the scope in which the
17329 class was originally declared, the program is invalid. */
17330 if (scope && !is_ancestor (scope, nested_name_specifier))
17331 {
17332 if (at_namespace_scope_p ())
17333 error_at (type_start_token->location,
17334 "declaration of %qD in namespace %qD which does not "
17335 "enclose %qD",
17336 type, scope, nested_name_specifier);
17337 else
17338 error_at (type_start_token->location,
17339 "declaration of %qD in %qD which does not enclose %qD",
17340 type, scope, nested_name_specifier);
17341 type = NULL_TREE;
17342 goto done;
17343 }
17344 /* [dcl.meaning]
17345
17346 A declarator-id shall not be qualified except for the
17347 definition of a ... nested class outside of its class
17348 ... [or] the definition or explicit instantiation of a
17349 class member of a namespace outside of its namespace. */
17350 if (scope == nested_name_specifier)
17351 {
17352 permerror (nested_name_specifier_token_start->location,
17353 "extra qualification not allowed");
17354 nested_name_specifier = NULL_TREE;
17355 num_templates = 0;
17356 }
17357 }
17358 /* An explicit-specialization must be preceded by "template <>". If
17359 it is not, try to recover gracefully. */
17360 if (at_namespace_scope_p ()
17361 && parser->num_template_parameter_lists == 0
17362 && template_id_p)
17363 {
17364 error_at (type_start_token->location,
17365 "an explicit specialization must be preceded by %<template <>%>");
17366 invalid_explicit_specialization_p = true;
17367 /* Take the same action that would have been taken by
17368 cp_parser_explicit_specialization. */
17369 ++parser->num_template_parameter_lists;
17370 begin_specialization ();
17371 }
17372 /* There must be no "return" statements between this point and the
17373 end of this function; set "type "to the correct return value and
17374 use "goto done;" to return. */
17375 /* Make sure that the right number of template parameters were
17376 present. */
17377 if (!cp_parser_check_template_parameters (parser, num_templates,
17378 type_start_token->location,
17379 /*declarator=*/NULL))
17380 {
17381 /* If something went wrong, there is no point in even trying to
17382 process the class-definition. */
17383 type = NULL_TREE;
17384 goto done;
17385 }
17386
17387 /* Look up the type. */
17388 if (template_id_p)
17389 {
17390 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17391 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17392 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17393 {
17394 error_at (type_start_token->location,
17395 "function template %qD redeclared as a class template", id);
17396 type = error_mark_node;
17397 }
17398 else
17399 {
17400 type = TREE_TYPE (id);
17401 type = maybe_process_partial_specialization (type);
17402 }
17403 if (nested_name_specifier)
17404 pushed_scope = push_scope (nested_name_specifier);
17405 }
17406 else if (nested_name_specifier)
17407 {
17408 tree class_type;
17409
17410 /* Given:
17411
17412 template <typename T> struct S { struct T };
17413 template <typename T> struct S<T>::T { };
17414
17415 we will get a TYPENAME_TYPE when processing the definition of
17416 `S::T'. We need to resolve it to the actual type before we
17417 try to define it. */
17418 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17419 {
17420 class_type = resolve_typename_type (TREE_TYPE (type),
17421 /*only_current_p=*/false);
17422 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17423 type = TYPE_NAME (class_type);
17424 else
17425 {
17426 cp_parser_error (parser, "could not resolve typename type");
17427 type = error_mark_node;
17428 }
17429 }
17430
17431 if (maybe_process_partial_specialization (TREE_TYPE (type))
17432 == error_mark_node)
17433 {
17434 type = NULL_TREE;
17435 goto done;
17436 }
17437
17438 class_type = current_class_type;
17439 /* Enter the scope indicated by the nested-name-specifier. */
17440 pushed_scope = push_scope (nested_name_specifier);
17441 /* Get the canonical version of this type. */
17442 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17443 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17444 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17445 {
17446 type = push_template_decl (type);
17447 if (type == error_mark_node)
17448 {
17449 type = NULL_TREE;
17450 goto done;
17451 }
17452 }
17453
17454 type = TREE_TYPE (type);
17455 *nested_name_specifier_p = true;
17456 }
17457 else /* The name is not a nested name. */
17458 {
17459 /* If the class was unnamed, create a dummy name. */
17460 if (!id)
17461 id = make_anon_name ();
17462 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17463 parser->num_template_parameter_lists);
17464 }
17465
17466 /* Indicate whether this class was declared as a `class' or as a
17467 `struct'. */
17468 if (TREE_CODE (type) == RECORD_TYPE)
17469 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17470 cp_parser_check_class_key (class_key, type);
17471
17472 /* If this type was already complete, and we see another definition,
17473 that's an error. */
17474 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17475 {
17476 error_at (type_start_token->location, "redefinition of %q#T",
17477 type);
17478 error_at (type_start_token->location, "previous definition of %q+#T",
17479 type);
17480 type = NULL_TREE;
17481 goto done;
17482 }
17483 else if (type == error_mark_node)
17484 type = NULL_TREE;
17485
17486 /* We will have entered the scope containing the class; the names of
17487 base classes should be looked up in that context. For example:
17488
17489 struct A { struct B {}; struct C; };
17490 struct A::C : B {};
17491
17492 is valid. */
17493
17494 /* Get the list of base-classes, if there is one. */
17495 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17496 *bases = cp_parser_base_clause (parser);
17497
17498 done:
17499 /* Leave the scope given by the nested-name-specifier. We will
17500 enter the class scope itself while processing the members. */
17501 if (pushed_scope)
17502 pop_scope (pushed_scope);
17503
17504 if (invalid_explicit_specialization_p)
17505 {
17506 end_specialization ();
17507 --parser->num_template_parameter_lists;
17508 }
17509
17510 if (type)
17511 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17512 *attributes_p = attributes;
17513 out:
17514 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17515 return type;
17516 }
17517
17518 /* Parse a class-key.
17519
17520 class-key:
17521 class
17522 struct
17523 union
17524
17525 Returns the kind of class-key specified, or none_type to indicate
17526 error. */
17527
17528 static enum tag_types
17529 cp_parser_class_key (cp_parser* parser)
17530 {
17531 cp_token *token;
17532 enum tag_types tag_type;
17533
17534 /* Look for the class-key. */
17535 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17536 if (!token)
17537 return none_type;
17538
17539 /* Check to see if the TOKEN is a class-key. */
17540 tag_type = cp_parser_token_is_class_key (token);
17541 if (!tag_type)
17542 cp_parser_error (parser, "expected class-key");
17543 return tag_type;
17544 }
17545
17546 /* Parse an (optional) member-specification.
17547
17548 member-specification:
17549 member-declaration member-specification [opt]
17550 access-specifier : member-specification [opt] */
17551
17552 static void
17553 cp_parser_member_specification_opt (cp_parser* parser)
17554 {
17555 while (true)
17556 {
17557 cp_token *token;
17558 enum rid keyword;
17559
17560 /* Peek at the next token. */
17561 token = cp_lexer_peek_token (parser->lexer);
17562 /* If it's a `}', or EOF then we've seen all the members. */
17563 if (token->type == CPP_CLOSE_BRACE
17564 || token->type == CPP_EOF
17565 || token->type == CPP_PRAGMA_EOL)
17566 break;
17567
17568 /* See if this token is a keyword. */
17569 keyword = token->keyword;
17570 switch (keyword)
17571 {
17572 case RID_PUBLIC:
17573 case RID_PROTECTED:
17574 case RID_PRIVATE:
17575 /* Consume the access-specifier. */
17576 cp_lexer_consume_token (parser->lexer);
17577 /* Remember which access-specifier is active. */
17578 current_access_specifier = token->u.value;
17579 /* Look for the `:'. */
17580 cp_parser_require (parser, CPP_COLON, RT_COLON);
17581 break;
17582
17583 default:
17584 /* Accept #pragmas at class scope. */
17585 if (token->type == CPP_PRAGMA)
17586 {
17587 cp_parser_pragma (parser, pragma_external);
17588 break;
17589 }
17590
17591 /* Otherwise, the next construction must be a
17592 member-declaration. */
17593 cp_parser_member_declaration (parser);
17594 }
17595 }
17596 }
17597
17598 /* Parse a member-declaration.
17599
17600 member-declaration:
17601 decl-specifier-seq [opt] member-declarator-list [opt] ;
17602 function-definition ; [opt]
17603 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17604 using-declaration
17605 template-declaration
17606
17607 member-declarator-list:
17608 member-declarator
17609 member-declarator-list , member-declarator
17610
17611 member-declarator:
17612 declarator pure-specifier [opt]
17613 declarator constant-initializer [opt]
17614 identifier [opt] : constant-expression
17615
17616 GNU Extensions:
17617
17618 member-declaration:
17619 __extension__ member-declaration
17620
17621 member-declarator:
17622 declarator attributes [opt] pure-specifier [opt]
17623 declarator attributes [opt] constant-initializer [opt]
17624 identifier [opt] attributes [opt] : constant-expression
17625
17626 C++0x Extensions:
17627
17628 member-declaration:
17629 static_assert-declaration */
17630
17631 static void
17632 cp_parser_member_declaration (cp_parser* parser)
17633 {
17634 cp_decl_specifier_seq decl_specifiers;
17635 tree prefix_attributes;
17636 tree decl;
17637 int declares_class_or_enum;
17638 bool friend_p;
17639 cp_token *token = NULL;
17640 cp_token *decl_spec_token_start = NULL;
17641 cp_token *initializer_token_start = NULL;
17642 int saved_pedantic;
17643 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17644
17645 /* Check for the `__extension__' keyword. */
17646 if (cp_parser_extension_opt (parser, &saved_pedantic))
17647 {
17648 /* Recurse. */
17649 cp_parser_member_declaration (parser);
17650 /* Restore the old value of the PEDANTIC flag. */
17651 pedantic = saved_pedantic;
17652
17653 return;
17654 }
17655
17656 /* Check for a template-declaration. */
17657 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17658 {
17659 /* An explicit specialization here is an error condition, and we
17660 expect the specialization handler to detect and report this. */
17661 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17662 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17663 cp_parser_explicit_specialization (parser);
17664 else
17665 cp_parser_template_declaration (parser, /*member_p=*/true);
17666
17667 return;
17668 }
17669
17670 /* Check for a using-declaration. */
17671 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17672 {
17673 /* Parse the using-declaration. */
17674 cp_parser_using_declaration (parser,
17675 /*access_declaration_p=*/false);
17676 return;
17677 }
17678
17679 /* Check for @defs. */
17680 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17681 {
17682 tree ivar, member;
17683 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17684 ivar = ivar_chains;
17685 while (ivar)
17686 {
17687 member = ivar;
17688 ivar = TREE_CHAIN (member);
17689 TREE_CHAIN (member) = NULL_TREE;
17690 finish_member_declaration (member);
17691 }
17692 return;
17693 }
17694
17695 /* If the next token is `static_assert' we have a static assertion. */
17696 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17697 {
17698 cp_parser_static_assert (parser, /*member_p=*/true);
17699 return;
17700 }
17701
17702 parser->colon_corrects_to_scope_p = false;
17703
17704 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17705 goto out;
17706
17707 /* Parse the decl-specifier-seq. */
17708 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17709 cp_parser_decl_specifier_seq (parser,
17710 CP_PARSER_FLAGS_OPTIONAL,
17711 &decl_specifiers,
17712 &declares_class_or_enum);
17713 prefix_attributes = decl_specifiers.attributes;
17714 decl_specifiers.attributes = NULL_TREE;
17715 /* Check for an invalid type-name. */
17716 if (!decl_specifiers.any_type_specifiers_p
17717 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17718 goto out;
17719 /* If there is no declarator, then the decl-specifier-seq should
17720 specify a type. */
17721 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17722 {
17723 /* If there was no decl-specifier-seq, and the next token is a
17724 `;', then we have something like:
17725
17726 struct S { ; };
17727
17728 [class.mem]
17729
17730 Each member-declaration shall declare at least one member
17731 name of the class. */
17732 if (!decl_specifiers.any_specifiers_p)
17733 {
17734 cp_token *token = cp_lexer_peek_token (parser->lexer);
17735 if (!in_system_header_at (token->location))
17736 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17737 }
17738 else
17739 {
17740 tree type;
17741
17742 /* See if this declaration is a friend. */
17743 friend_p = cp_parser_friend_p (&decl_specifiers);
17744 /* If there were decl-specifiers, check to see if there was
17745 a class-declaration. */
17746 type = check_tag_decl (&decl_specifiers);
17747 /* Nested classes have already been added to the class, but
17748 a `friend' needs to be explicitly registered. */
17749 if (friend_p)
17750 {
17751 /* If the `friend' keyword was present, the friend must
17752 be introduced with a class-key. */
17753 if (!declares_class_or_enum)
17754 error_at (decl_spec_token_start->location,
17755 "a class-key must be used when declaring a friend");
17756 /* In this case:
17757
17758 template <typename T> struct A {
17759 friend struct A<T>::B;
17760 };
17761
17762 A<T>::B will be represented by a TYPENAME_TYPE, and
17763 therefore not recognized by check_tag_decl. */
17764 if (!type
17765 && decl_specifiers.type
17766 && TYPE_P (decl_specifiers.type))
17767 type = decl_specifiers.type;
17768 if (!type || !TYPE_P (type))
17769 error_at (decl_spec_token_start->location,
17770 "friend declaration does not name a class or "
17771 "function");
17772 else
17773 make_friend_class (current_class_type, type,
17774 /*complain=*/true);
17775 }
17776 /* If there is no TYPE, an error message will already have
17777 been issued. */
17778 else if (!type || type == error_mark_node)
17779 ;
17780 /* An anonymous aggregate has to be handled specially; such
17781 a declaration really declares a data member (with a
17782 particular type), as opposed to a nested class. */
17783 else if (ANON_AGGR_TYPE_P (type))
17784 {
17785 /* Remove constructors and such from TYPE, now that we
17786 know it is an anonymous aggregate. */
17787 fixup_anonymous_aggr (type);
17788 /* And make the corresponding data member. */
17789 decl = build_decl (decl_spec_token_start->location,
17790 FIELD_DECL, NULL_TREE, type);
17791 /* Add it to the class. */
17792 finish_member_declaration (decl);
17793 }
17794 else
17795 cp_parser_check_access_in_redeclaration
17796 (TYPE_NAME (type),
17797 decl_spec_token_start->location);
17798 }
17799 }
17800 else
17801 {
17802 bool assume_semicolon = false;
17803
17804 /* See if these declarations will be friends. */
17805 friend_p = cp_parser_friend_p (&decl_specifiers);
17806
17807 /* Keep going until we hit the `;' at the end of the
17808 declaration. */
17809 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17810 {
17811 tree attributes = NULL_TREE;
17812 tree first_attribute;
17813
17814 /* Peek at the next token. */
17815 token = cp_lexer_peek_token (parser->lexer);
17816
17817 /* Check for a bitfield declaration. */
17818 if (token->type == CPP_COLON
17819 || (token->type == CPP_NAME
17820 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17821 == CPP_COLON))
17822 {
17823 tree identifier;
17824 tree width;
17825
17826 /* Get the name of the bitfield. Note that we cannot just
17827 check TOKEN here because it may have been invalidated by
17828 the call to cp_lexer_peek_nth_token above. */
17829 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17830 identifier = cp_parser_identifier (parser);
17831 else
17832 identifier = NULL_TREE;
17833
17834 /* Consume the `:' token. */
17835 cp_lexer_consume_token (parser->lexer);
17836 /* Get the width of the bitfield. */
17837 width
17838 = cp_parser_constant_expression (parser,
17839 /*allow_non_constant=*/false,
17840 NULL);
17841
17842 /* Look for attributes that apply to the bitfield. */
17843 attributes = cp_parser_attributes_opt (parser);
17844 /* Remember which attributes are prefix attributes and
17845 which are not. */
17846 first_attribute = attributes;
17847 /* Combine the attributes. */
17848 attributes = chainon (prefix_attributes, attributes);
17849
17850 /* Create the bitfield declaration. */
17851 decl = grokbitfield (identifier
17852 ? make_id_declarator (NULL_TREE,
17853 identifier,
17854 sfk_none)
17855 : NULL,
17856 &decl_specifiers,
17857 width,
17858 attributes);
17859 }
17860 else
17861 {
17862 cp_declarator *declarator;
17863 tree initializer;
17864 tree asm_specification;
17865 int ctor_dtor_or_conv_p;
17866
17867 /* Parse the declarator. */
17868 declarator
17869 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17870 &ctor_dtor_or_conv_p,
17871 /*parenthesized_p=*/NULL,
17872 /*member_p=*/true);
17873
17874 /* If something went wrong parsing the declarator, make sure
17875 that we at least consume some tokens. */
17876 if (declarator == cp_error_declarator)
17877 {
17878 /* Skip to the end of the statement. */
17879 cp_parser_skip_to_end_of_statement (parser);
17880 /* If the next token is not a semicolon, that is
17881 probably because we just skipped over the body of
17882 a function. So, we consume a semicolon if
17883 present, but do not issue an error message if it
17884 is not present. */
17885 if (cp_lexer_next_token_is (parser->lexer,
17886 CPP_SEMICOLON))
17887 cp_lexer_consume_token (parser->lexer);
17888 goto out;
17889 }
17890
17891 if (declares_class_or_enum & 2)
17892 cp_parser_check_for_definition_in_return_type
17893 (declarator, decl_specifiers.type,
17894 decl_specifiers.type_location);
17895
17896 /* Look for an asm-specification. */
17897 asm_specification = cp_parser_asm_specification_opt (parser);
17898 /* Look for attributes that apply to the declaration. */
17899 attributes = cp_parser_attributes_opt (parser);
17900 /* Remember which attributes are prefix attributes and
17901 which are not. */
17902 first_attribute = attributes;
17903 /* Combine the attributes. */
17904 attributes = chainon (prefix_attributes, attributes);
17905
17906 /* If it's an `=', then we have a constant-initializer or a
17907 pure-specifier. It is not correct to parse the
17908 initializer before registering the member declaration
17909 since the member declaration should be in scope while
17910 its initializer is processed. However, the rest of the
17911 front end does not yet provide an interface that allows
17912 us to handle this correctly. */
17913 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17914 {
17915 /* In [class.mem]:
17916
17917 A pure-specifier shall be used only in the declaration of
17918 a virtual function.
17919
17920 A member-declarator can contain a constant-initializer
17921 only if it declares a static member of integral or
17922 enumeration type.
17923
17924 Therefore, if the DECLARATOR is for a function, we look
17925 for a pure-specifier; otherwise, we look for a
17926 constant-initializer. When we call `grokfield', it will
17927 perform more stringent semantics checks. */
17928 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17929 if (function_declarator_p (declarator))
17930 initializer = cp_parser_pure_specifier (parser);
17931 else
17932 /* Parse the initializer. */
17933 initializer = cp_parser_constant_initializer (parser);
17934 }
17935 /* Otherwise, there is no initializer. */
17936 else
17937 initializer = NULL_TREE;
17938
17939 /* See if we are probably looking at a function
17940 definition. We are certainly not looking at a
17941 member-declarator. Calling `grokfield' has
17942 side-effects, so we must not do it unless we are sure
17943 that we are looking at a member-declarator. */
17944 if (cp_parser_token_starts_function_definition_p
17945 (cp_lexer_peek_token (parser->lexer)))
17946 {
17947 /* The grammar does not allow a pure-specifier to be
17948 used when a member function is defined. (It is
17949 possible that this fact is an oversight in the
17950 standard, since a pure function may be defined
17951 outside of the class-specifier. */
17952 if (initializer)
17953 error_at (initializer_token_start->location,
17954 "pure-specifier on function-definition");
17955 decl = cp_parser_save_member_function_body (parser,
17956 &decl_specifiers,
17957 declarator,
17958 attributes);
17959 /* If the member was not a friend, declare it here. */
17960 if (!friend_p)
17961 finish_member_declaration (decl);
17962 /* Peek at the next token. */
17963 token = cp_lexer_peek_token (parser->lexer);
17964 /* If the next token is a semicolon, consume it. */
17965 if (token->type == CPP_SEMICOLON)
17966 cp_lexer_consume_token (parser->lexer);
17967 goto out;
17968 }
17969 else
17970 if (declarator->kind == cdk_function)
17971 declarator->id_loc = token->location;
17972 /* Create the declaration. */
17973 decl = grokfield (declarator, &decl_specifiers,
17974 initializer, /*init_const_expr_p=*/true,
17975 asm_specification,
17976 attributes);
17977 }
17978
17979 /* Reset PREFIX_ATTRIBUTES. */
17980 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17981 attributes = TREE_CHAIN (attributes);
17982 if (attributes)
17983 TREE_CHAIN (attributes) = NULL_TREE;
17984
17985 /* If there is any qualification still in effect, clear it
17986 now; we will be starting fresh with the next declarator. */
17987 parser->scope = NULL_TREE;
17988 parser->qualifying_scope = NULL_TREE;
17989 parser->object_scope = NULL_TREE;
17990 /* If it's a `,', then there are more declarators. */
17991 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17992 cp_lexer_consume_token (parser->lexer);
17993 /* If the next token isn't a `;', then we have a parse error. */
17994 else if (cp_lexer_next_token_is_not (parser->lexer,
17995 CPP_SEMICOLON))
17996 {
17997 /* The next token might be a ways away from where the
17998 actual semicolon is missing. Find the previous token
17999 and use that for our error position. */
18000 cp_token *token = cp_lexer_previous_token (parser->lexer);
18001 error_at (token->location,
18002 "expected %<;%> at end of member declaration");
18003
18004 /* Assume that the user meant to provide a semicolon. If
18005 we were to cp_parser_skip_to_end_of_statement, we might
18006 skip to a semicolon inside a member function definition
18007 and issue nonsensical error messages. */
18008 assume_semicolon = true;
18009 }
18010
18011 if (decl)
18012 {
18013 /* Add DECL to the list of members. */
18014 if (!friend_p)
18015 finish_member_declaration (decl);
18016
18017 if (TREE_CODE (decl) == FUNCTION_DECL)
18018 cp_parser_save_default_args (parser, decl);
18019 }
18020
18021 if (assume_semicolon)
18022 goto out;
18023 }
18024 }
18025
18026 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18027 out:
18028 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18029 }
18030
18031 /* Parse a pure-specifier.
18032
18033 pure-specifier:
18034 = 0
18035
18036 Returns INTEGER_ZERO_NODE if a pure specifier is found.
18037 Otherwise, ERROR_MARK_NODE is returned. */
18038
18039 static tree
18040 cp_parser_pure_specifier (cp_parser* parser)
18041 {
18042 cp_token *token;
18043
18044 /* Look for the `=' token. */
18045 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18046 return error_mark_node;
18047 /* Look for the `0' token. */
18048 token = cp_lexer_peek_token (parser->lexer);
18049
18050 if (token->type == CPP_EOF
18051 || token->type == CPP_PRAGMA_EOL)
18052 return error_mark_node;
18053
18054 cp_lexer_consume_token (parser->lexer);
18055
18056 /* Accept = default or = delete in c++0x mode. */
18057 if (token->keyword == RID_DEFAULT
18058 || token->keyword == RID_DELETE)
18059 {
18060 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18061 return token->u.value;
18062 }
18063
18064 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
18065 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18066 {
18067 cp_parser_error (parser,
18068 "invalid pure specifier (only %<= 0%> is allowed)");
18069 cp_parser_skip_to_end_of_statement (parser);
18070 return error_mark_node;
18071 }
18072 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18073 {
18074 error_at (token->location, "templates may not be %<virtual%>");
18075 return error_mark_node;
18076 }
18077
18078 return integer_zero_node;
18079 }
18080
18081 /* Parse a constant-initializer.
18082
18083 constant-initializer:
18084 = constant-expression
18085
18086 Returns a representation of the constant-expression. */
18087
18088 static tree
18089 cp_parser_constant_initializer (cp_parser* parser)
18090 {
18091 /* Look for the `=' token. */
18092 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18093 return error_mark_node;
18094
18095 /* It is invalid to write:
18096
18097 struct S { static const int i = { 7 }; };
18098
18099 */
18100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18101 {
18102 cp_parser_error (parser,
18103 "a brace-enclosed initializer is not allowed here");
18104 /* Consume the opening brace. */
18105 cp_lexer_consume_token (parser->lexer);
18106 /* Skip the initializer. */
18107 cp_parser_skip_to_closing_brace (parser);
18108 /* Look for the trailing `}'. */
18109 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18110
18111 return error_mark_node;
18112 }
18113
18114 return cp_parser_constant_expression (parser,
18115 /*allow_non_constant=*/false,
18116 NULL);
18117 }
18118
18119 /* Derived classes [gram.class.derived] */
18120
18121 /* Parse a base-clause.
18122
18123 base-clause:
18124 : base-specifier-list
18125
18126 base-specifier-list:
18127 base-specifier ... [opt]
18128 base-specifier-list , base-specifier ... [opt]
18129
18130 Returns a TREE_LIST representing the base-classes, in the order in
18131 which they were declared. The representation of each node is as
18132 described by cp_parser_base_specifier.
18133
18134 In the case that no bases are specified, this function will return
18135 NULL_TREE, not ERROR_MARK_NODE. */
18136
18137 static tree
18138 cp_parser_base_clause (cp_parser* parser)
18139 {
18140 tree bases = NULL_TREE;
18141
18142 /* Look for the `:' that begins the list. */
18143 cp_parser_require (parser, CPP_COLON, RT_COLON);
18144
18145 /* Scan the base-specifier-list. */
18146 while (true)
18147 {
18148 cp_token *token;
18149 tree base;
18150 bool pack_expansion_p = false;
18151
18152 /* Look for the base-specifier. */
18153 base = cp_parser_base_specifier (parser);
18154 /* Look for the (optional) ellipsis. */
18155 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18156 {
18157 /* Consume the `...'. */
18158 cp_lexer_consume_token (parser->lexer);
18159
18160 pack_expansion_p = true;
18161 }
18162
18163 /* Add BASE to the front of the list. */
18164 if (base != error_mark_node)
18165 {
18166 if (pack_expansion_p)
18167 /* Make this a pack expansion type. */
18168 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18169
18170
18171 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18172 {
18173 TREE_CHAIN (base) = bases;
18174 bases = base;
18175 }
18176 }
18177 /* Peek at the next token. */
18178 token = cp_lexer_peek_token (parser->lexer);
18179 /* If it's not a comma, then the list is complete. */
18180 if (token->type != CPP_COMMA)
18181 break;
18182 /* Consume the `,'. */
18183 cp_lexer_consume_token (parser->lexer);
18184 }
18185
18186 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18187 base class had a qualified name. However, the next name that
18188 appears is certainly not qualified. */
18189 parser->scope = NULL_TREE;
18190 parser->qualifying_scope = NULL_TREE;
18191 parser->object_scope = NULL_TREE;
18192
18193 return nreverse (bases);
18194 }
18195
18196 /* Parse a base-specifier.
18197
18198 base-specifier:
18199 :: [opt] nested-name-specifier [opt] class-name
18200 virtual access-specifier [opt] :: [opt] nested-name-specifier
18201 [opt] class-name
18202 access-specifier virtual [opt] :: [opt] nested-name-specifier
18203 [opt] class-name
18204
18205 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18206 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18207 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18208 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18209
18210 static tree
18211 cp_parser_base_specifier (cp_parser* parser)
18212 {
18213 cp_token *token;
18214 bool done = false;
18215 bool virtual_p = false;
18216 bool duplicate_virtual_error_issued_p = false;
18217 bool duplicate_access_error_issued_p = false;
18218 bool class_scope_p, template_p;
18219 tree access = access_default_node;
18220 tree type;
18221
18222 /* Process the optional `virtual' and `access-specifier'. */
18223 while (!done)
18224 {
18225 /* Peek at the next token. */
18226 token = cp_lexer_peek_token (parser->lexer);
18227 /* Process `virtual'. */
18228 switch (token->keyword)
18229 {
18230 case RID_VIRTUAL:
18231 /* If `virtual' appears more than once, issue an error. */
18232 if (virtual_p && !duplicate_virtual_error_issued_p)
18233 {
18234 cp_parser_error (parser,
18235 "%<virtual%> specified more than once in base-specified");
18236 duplicate_virtual_error_issued_p = true;
18237 }
18238
18239 virtual_p = true;
18240
18241 /* Consume the `virtual' token. */
18242 cp_lexer_consume_token (parser->lexer);
18243
18244 break;
18245
18246 case RID_PUBLIC:
18247 case RID_PROTECTED:
18248 case RID_PRIVATE:
18249 /* If more than one access specifier appears, issue an
18250 error. */
18251 if (access != access_default_node
18252 && !duplicate_access_error_issued_p)
18253 {
18254 cp_parser_error (parser,
18255 "more than one access specifier in base-specified");
18256 duplicate_access_error_issued_p = true;
18257 }
18258
18259 access = ridpointers[(int) token->keyword];
18260
18261 /* Consume the access-specifier. */
18262 cp_lexer_consume_token (parser->lexer);
18263
18264 break;
18265
18266 default:
18267 done = true;
18268 break;
18269 }
18270 }
18271 /* It is not uncommon to see programs mechanically, erroneously, use
18272 the 'typename' keyword to denote (dependent) qualified types
18273 as base classes. */
18274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18275 {
18276 token = cp_lexer_peek_token (parser->lexer);
18277 if (!processing_template_decl)
18278 error_at (token->location,
18279 "keyword %<typename%> not allowed outside of templates");
18280 else
18281 error_at (token->location,
18282 "keyword %<typename%> not allowed in this context "
18283 "(the base class is implicitly a type)");
18284 cp_lexer_consume_token (parser->lexer);
18285 }
18286
18287 /* Look for the optional `::' operator. */
18288 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18289 /* Look for the nested-name-specifier. The simplest way to
18290 implement:
18291
18292 [temp.res]
18293
18294 The keyword `typename' is not permitted in a base-specifier or
18295 mem-initializer; in these contexts a qualified name that
18296 depends on a template-parameter is implicitly assumed to be a
18297 type name.
18298
18299 is to pretend that we have seen the `typename' keyword at this
18300 point. */
18301 cp_parser_nested_name_specifier_opt (parser,
18302 /*typename_keyword_p=*/true,
18303 /*check_dependency_p=*/true,
18304 typename_type,
18305 /*is_declaration=*/true);
18306 /* If the base class is given by a qualified name, assume that names
18307 we see are type names or templates, as appropriate. */
18308 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18309 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18310
18311 /* Finally, look for the class-name. */
18312 type = cp_parser_class_name (parser,
18313 class_scope_p,
18314 template_p,
18315 typename_type,
18316 /*check_dependency_p=*/true,
18317 /*class_head_p=*/false,
18318 /*is_declaration=*/true);
18319
18320 if (type == error_mark_node)
18321 return error_mark_node;
18322
18323 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18324 }
18325
18326 /* Exception handling [gram.exception] */
18327
18328 /* Parse an (optional) exception-specification.
18329
18330 exception-specification:
18331 throw ( type-id-list [opt] )
18332
18333 Returns a TREE_LIST representing the exception-specification. The
18334 TREE_VALUE of each node is a type. */
18335
18336 static tree
18337 cp_parser_exception_specification_opt (cp_parser* parser)
18338 {
18339 cp_token *token;
18340 tree type_id_list;
18341 const char *saved_message;
18342
18343 /* Peek at the next token. */
18344 token = cp_lexer_peek_token (parser->lexer);
18345
18346 /* Is it a noexcept-specification? */
18347 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18348 {
18349 tree expr;
18350 cp_lexer_consume_token (parser->lexer);
18351
18352 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18353 {
18354 cp_lexer_consume_token (parser->lexer);
18355
18356 /* Types may not be defined in an exception-specification. */
18357 saved_message = parser->type_definition_forbidden_message;
18358 parser->type_definition_forbidden_message
18359 = G_("types may not be defined in an exception-specification");
18360
18361 expr = cp_parser_constant_expression (parser, false, NULL);
18362
18363 /* Restore the saved message. */
18364 parser->type_definition_forbidden_message = saved_message;
18365
18366 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18367 }
18368 else
18369 expr = boolean_true_node;
18370
18371 return build_noexcept_spec (expr, tf_warning_or_error);
18372 }
18373
18374 /* If it's not `throw', then there's no exception-specification. */
18375 if (!cp_parser_is_keyword (token, RID_THROW))
18376 return NULL_TREE;
18377
18378 #if 0
18379 /* Enable this once a lot of code has transitioned to noexcept? */
18380 if (cxx_dialect == cxx0x && !in_system_header)
18381 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18382 "deprecated in C++0x; use %<noexcept%> instead");
18383 #endif
18384
18385 /* Consume the `throw'. */
18386 cp_lexer_consume_token (parser->lexer);
18387
18388 /* Look for the `('. */
18389 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18390
18391 /* Peek at the next token. */
18392 token = cp_lexer_peek_token (parser->lexer);
18393 /* If it's not a `)', then there is a type-id-list. */
18394 if (token->type != CPP_CLOSE_PAREN)
18395 {
18396 /* Types may not be defined in an exception-specification. */
18397 saved_message = parser->type_definition_forbidden_message;
18398 parser->type_definition_forbidden_message
18399 = G_("types may not be defined in an exception-specification");
18400 /* Parse the type-id-list. */
18401 type_id_list = cp_parser_type_id_list (parser);
18402 /* Restore the saved message. */
18403 parser->type_definition_forbidden_message = saved_message;
18404 }
18405 else
18406 type_id_list = empty_except_spec;
18407
18408 /* Look for the `)'. */
18409 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18410
18411 return type_id_list;
18412 }
18413
18414 /* Parse an (optional) type-id-list.
18415
18416 type-id-list:
18417 type-id ... [opt]
18418 type-id-list , type-id ... [opt]
18419
18420 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18421 in the order that the types were presented. */
18422
18423 static tree
18424 cp_parser_type_id_list (cp_parser* parser)
18425 {
18426 tree types = NULL_TREE;
18427
18428 while (true)
18429 {
18430 cp_token *token;
18431 tree type;
18432
18433 /* Get the next type-id. */
18434 type = cp_parser_type_id (parser);
18435 /* Parse the optional ellipsis. */
18436 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18437 {
18438 /* Consume the `...'. */
18439 cp_lexer_consume_token (parser->lexer);
18440
18441 /* Turn the type into a pack expansion expression. */
18442 type = make_pack_expansion (type);
18443 }
18444 /* Add it to the list. */
18445 types = add_exception_specifier (types, type, /*complain=*/1);
18446 /* Peek at the next token. */
18447 token = cp_lexer_peek_token (parser->lexer);
18448 /* If it is not a `,', we are done. */
18449 if (token->type != CPP_COMMA)
18450 break;
18451 /* Consume the `,'. */
18452 cp_lexer_consume_token (parser->lexer);
18453 }
18454
18455 return nreverse (types);
18456 }
18457
18458 /* Parse a try-block.
18459
18460 try-block:
18461 try compound-statement handler-seq */
18462
18463 static tree
18464 cp_parser_try_block (cp_parser* parser)
18465 {
18466 tree try_block;
18467
18468 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18469 try_block = begin_try_block ();
18470 cp_parser_compound_statement (parser, NULL, true);
18471 finish_try_block (try_block);
18472 cp_parser_handler_seq (parser);
18473 finish_handler_sequence (try_block);
18474
18475 return try_block;
18476 }
18477
18478 /* Parse a function-try-block.
18479
18480 function-try-block:
18481 try ctor-initializer [opt] function-body handler-seq */
18482
18483 static bool
18484 cp_parser_function_try_block (cp_parser* parser)
18485 {
18486 tree compound_stmt;
18487 tree try_block;
18488 bool ctor_initializer_p;
18489
18490 /* Look for the `try' keyword. */
18491 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18492 return false;
18493 /* Let the rest of the front end know where we are. */
18494 try_block = begin_function_try_block (&compound_stmt);
18495 /* Parse the function-body. */
18496 ctor_initializer_p
18497 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18498 /* We're done with the `try' part. */
18499 finish_function_try_block (try_block);
18500 /* Parse the handlers. */
18501 cp_parser_handler_seq (parser);
18502 /* We're done with the handlers. */
18503 finish_function_handler_sequence (try_block, compound_stmt);
18504
18505 return ctor_initializer_p;
18506 }
18507
18508 /* Parse a handler-seq.
18509
18510 handler-seq:
18511 handler handler-seq [opt] */
18512
18513 static void
18514 cp_parser_handler_seq (cp_parser* parser)
18515 {
18516 while (true)
18517 {
18518 cp_token *token;
18519
18520 /* Parse the handler. */
18521 cp_parser_handler (parser);
18522 /* Peek at the next token. */
18523 token = cp_lexer_peek_token (parser->lexer);
18524 /* If it's not `catch' then there are no more handlers. */
18525 if (!cp_parser_is_keyword (token, RID_CATCH))
18526 break;
18527 }
18528 }
18529
18530 /* Parse a handler.
18531
18532 handler:
18533 catch ( exception-declaration ) compound-statement */
18534
18535 static void
18536 cp_parser_handler (cp_parser* parser)
18537 {
18538 tree handler;
18539 tree declaration;
18540
18541 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18542 handler = begin_handler ();
18543 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18544 declaration = cp_parser_exception_declaration (parser);
18545 finish_handler_parms (declaration, handler);
18546 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18547 cp_parser_compound_statement (parser, NULL, false);
18548 finish_handler (handler);
18549 }
18550
18551 /* Parse an exception-declaration.
18552
18553 exception-declaration:
18554 type-specifier-seq declarator
18555 type-specifier-seq abstract-declarator
18556 type-specifier-seq
18557 ...
18558
18559 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18560 ellipsis variant is used. */
18561
18562 static tree
18563 cp_parser_exception_declaration (cp_parser* parser)
18564 {
18565 cp_decl_specifier_seq type_specifiers;
18566 cp_declarator *declarator;
18567 const char *saved_message;
18568
18569 /* If it's an ellipsis, it's easy to handle. */
18570 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18571 {
18572 /* Consume the `...' token. */
18573 cp_lexer_consume_token (parser->lexer);
18574 return NULL_TREE;
18575 }
18576
18577 /* Types may not be defined in exception-declarations. */
18578 saved_message = parser->type_definition_forbidden_message;
18579 parser->type_definition_forbidden_message
18580 = G_("types may not be defined in exception-declarations");
18581
18582 /* Parse the type-specifier-seq. */
18583 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18584 /*is_trailing_return=*/false,
18585 &type_specifiers);
18586 /* If it's a `)', then there is no declarator. */
18587 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18588 declarator = NULL;
18589 else
18590 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18591 /*ctor_dtor_or_conv_p=*/NULL,
18592 /*parenthesized_p=*/NULL,
18593 /*member_p=*/false);
18594
18595 /* Restore the saved message. */
18596 parser->type_definition_forbidden_message = saved_message;
18597
18598 if (!type_specifiers.any_specifiers_p)
18599 return error_mark_node;
18600
18601 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18602 }
18603
18604 /* Parse a throw-expression.
18605
18606 throw-expression:
18607 throw assignment-expression [opt]
18608
18609 Returns a THROW_EXPR representing the throw-expression. */
18610
18611 static tree
18612 cp_parser_throw_expression (cp_parser* parser)
18613 {
18614 tree expression;
18615 cp_token* token;
18616
18617 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18618 token = cp_lexer_peek_token (parser->lexer);
18619 /* Figure out whether or not there is an assignment-expression
18620 following the "throw" keyword. */
18621 if (token->type == CPP_COMMA
18622 || token->type == CPP_SEMICOLON
18623 || token->type == CPP_CLOSE_PAREN
18624 || token->type == CPP_CLOSE_SQUARE
18625 || token->type == CPP_CLOSE_BRACE
18626 || token->type == CPP_COLON)
18627 expression = NULL_TREE;
18628 else
18629 expression = cp_parser_assignment_expression (parser,
18630 /*cast_p=*/false, NULL);
18631
18632 return build_throw (expression);
18633 }
18634
18635 /* GNU Extensions */
18636
18637 /* Parse an (optional) asm-specification.
18638
18639 asm-specification:
18640 asm ( string-literal )
18641
18642 If the asm-specification is present, returns a STRING_CST
18643 corresponding to the string-literal. Otherwise, returns
18644 NULL_TREE. */
18645
18646 static tree
18647 cp_parser_asm_specification_opt (cp_parser* parser)
18648 {
18649 cp_token *token;
18650 tree asm_specification;
18651
18652 /* Peek at the next token. */
18653 token = cp_lexer_peek_token (parser->lexer);
18654 /* If the next token isn't the `asm' keyword, then there's no
18655 asm-specification. */
18656 if (!cp_parser_is_keyword (token, RID_ASM))
18657 return NULL_TREE;
18658
18659 /* Consume the `asm' token. */
18660 cp_lexer_consume_token (parser->lexer);
18661 /* Look for the `('. */
18662 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18663
18664 /* Look for the string-literal. */
18665 asm_specification = cp_parser_string_literal (parser, false, false);
18666
18667 /* Look for the `)'. */
18668 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18669
18670 return asm_specification;
18671 }
18672
18673 /* Parse an asm-operand-list.
18674
18675 asm-operand-list:
18676 asm-operand
18677 asm-operand-list , asm-operand
18678
18679 asm-operand:
18680 string-literal ( expression )
18681 [ string-literal ] string-literal ( expression )
18682
18683 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18684 each node is the expression. The TREE_PURPOSE is itself a
18685 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18686 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18687 is a STRING_CST for the string literal before the parenthesis. Returns
18688 ERROR_MARK_NODE if any of the operands are invalid. */
18689
18690 static tree
18691 cp_parser_asm_operand_list (cp_parser* parser)
18692 {
18693 tree asm_operands = NULL_TREE;
18694 bool invalid_operands = false;
18695
18696 while (true)
18697 {
18698 tree string_literal;
18699 tree expression;
18700 tree name;
18701
18702 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18703 {
18704 /* Consume the `[' token. */
18705 cp_lexer_consume_token (parser->lexer);
18706 /* Read the operand name. */
18707 name = cp_parser_identifier (parser);
18708 if (name != error_mark_node)
18709 name = build_string (IDENTIFIER_LENGTH (name),
18710 IDENTIFIER_POINTER (name));
18711 /* Look for the closing `]'. */
18712 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18713 }
18714 else
18715 name = NULL_TREE;
18716 /* Look for the string-literal. */
18717 string_literal = cp_parser_string_literal (parser, false, false);
18718
18719 /* Look for the `('. */
18720 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18721 /* Parse the expression. */
18722 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18723 /* Look for the `)'. */
18724 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18725
18726 if (name == error_mark_node
18727 || string_literal == error_mark_node
18728 || expression == error_mark_node)
18729 invalid_operands = true;
18730
18731 /* Add this operand to the list. */
18732 asm_operands = tree_cons (build_tree_list (name, string_literal),
18733 expression,
18734 asm_operands);
18735 /* If the next token is not a `,', there are no more
18736 operands. */
18737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18738 break;
18739 /* Consume the `,'. */
18740 cp_lexer_consume_token (parser->lexer);
18741 }
18742
18743 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18744 }
18745
18746 /* Parse an asm-clobber-list.
18747
18748 asm-clobber-list:
18749 string-literal
18750 asm-clobber-list , string-literal
18751
18752 Returns a TREE_LIST, indicating the clobbers in the order that they
18753 appeared. The TREE_VALUE of each node is a STRING_CST. */
18754
18755 static tree
18756 cp_parser_asm_clobber_list (cp_parser* parser)
18757 {
18758 tree clobbers = NULL_TREE;
18759
18760 while (true)
18761 {
18762 tree string_literal;
18763
18764 /* Look for the string literal. */
18765 string_literal = cp_parser_string_literal (parser, false, false);
18766 /* Add it to the list. */
18767 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18768 /* If the next token is not a `,', then the list is
18769 complete. */
18770 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18771 break;
18772 /* Consume the `,' token. */
18773 cp_lexer_consume_token (parser->lexer);
18774 }
18775
18776 return clobbers;
18777 }
18778
18779 /* Parse an asm-label-list.
18780
18781 asm-label-list:
18782 identifier
18783 asm-label-list , identifier
18784
18785 Returns a TREE_LIST, indicating the labels in the order that they
18786 appeared. The TREE_VALUE of each node is a label. */
18787
18788 static tree
18789 cp_parser_asm_label_list (cp_parser* parser)
18790 {
18791 tree labels = NULL_TREE;
18792
18793 while (true)
18794 {
18795 tree identifier, label, name;
18796
18797 /* Look for the identifier. */
18798 identifier = cp_parser_identifier (parser);
18799 if (!error_operand_p (identifier))
18800 {
18801 label = lookup_label (identifier);
18802 if (TREE_CODE (label) == LABEL_DECL)
18803 {
18804 TREE_USED (label) = 1;
18805 check_goto (label);
18806 name = build_string (IDENTIFIER_LENGTH (identifier),
18807 IDENTIFIER_POINTER (identifier));
18808 labels = tree_cons (name, label, labels);
18809 }
18810 }
18811 /* If the next token is not a `,', then the list is
18812 complete. */
18813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18814 break;
18815 /* Consume the `,' token. */
18816 cp_lexer_consume_token (parser->lexer);
18817 }
18818
18819 return nreverse (labels);
18820 }
18821
18822 /* Parse an (optional) series of attributes.
18823
18824 attributes:
18825 attributes attribute
18826
18827 attribute:
18828 __attribute__ (( attribute-list [opt] ))
18829
18830 The return value is as for cp_parser_attribute_list. */
18831
18832 static tree
18833 cp_parser_attributes_opt (cp_parser* parser)
18834 {
18835 tree attributes = NULL_TREE;
18836
18837 while (true)
18838 {
18839 cp_token *token;
18840 tree attribute_list;
18841
18842 /* Peek at the next token. */
18843 token = cp_lexer_peek_token (parser->lexer);
18844 /* If it's not `__attribute__', then we're done. */
18845 if (token->keyword != RID_ATTRIBUTE)
18846 break;
18847
18848 /* Consume the `__attribute__' keyword. */
18849 cp_lexer_consume_token (parser->lexer);
18850 /* Look for the two `(' tokens. */
18851 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18852 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18853
18854 /* Peek at the next token. */
18855 token = cp_lexer_peek_token (parser->lexer);
18856 if (token->type != CPP_CLOSE_PAREN)
18857 /* Parse the attribute-list. */
18858 attribute_list = cp_parser_attribute_list (parser);
18859 else
18860 /* If the next token is a `)', then there is no attribute
18861 list. */
18862 attribute_list = NULL;
18863
18864 /* Look for the two `)' tokens. */
18865 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18866 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18867
18868 /* Add these new attributes to the list. */
18869 attributes = chainon (attributes, attribute_list);
18870 }
18871
18872 return attributes;
18873 }
18874
18875 /* Parse an attribute-list.
18876
18877 attribute-list:
18878 attribute
18879 attribute-list , attribute
18880
18881 attribute:
18882 identifier
18883 identifier ( identifier )
18884 identifier ( identifier , expression-list )
18885 identifier ( expression-list )
18886
18887 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18888 to an attribute. The TREE_PURPOSE of each node is the identifier
18889 indicating which attribute is in use. The TREE_VALUE represents
18890 the arguments, if any. */
18891
18892 static tree
18893 cp_parser_attribute_list (cp_parser* parser)
18894 {
18895 tree attribute_list = NULL_TREE;
18896 bool save_translate_strings_p = parser->translate_strings_p;
18897
18898 parser->translate_strings_p = false;
18899 while (true)
18900 {
18901 cp_token *token;
18902 tree identifier;
18903 tree attribute;
18904
18905 /* Look for the identifier. We also allow keywords here; for
18906 example `__attribute__ ((const))' is legal. */
18907 token = cp_lexer_peek_token (parser->lexer);
18908 if (token->type == CPP_NAME
18909 || token->type == CPP_KEYWORD)
18910 {
18911 tree arguments = NULL_TREE;
18912
18913 /* Consume the token. */
18914 token = cp_lexer_consume_token (parser->lexer);
18915
18916 /* Save away the identifier that indicates which attribute
18917 this is. */
18918 identifier = (token->type == CPP_KEYWORD)
18919 /* For keywords, use the canonical spelling, not the
18920 parsed identifier. */
18921 ? ridpointers[(int) token->keyword]
18922 : token->u.value;
18923
18924 attribute = build_tree_list (identifier, NULL_TREE);
18925
18926 /* Peek at the next token. */
18927 token = cp_lexer_peek_token (parser->lexer);
18928 /* If it's an `(', then parse the attribute arguments. */
18929 if (token->type == CPP_OPEN_PAREN)
18930 {
18931 VEC(tree,gc) *vec;
18932 int attr_flag = (attribute_takes_identifier_p (identifier)
18933 ? id_attr : normal_attr);
18934 vec = cp_parser_parenthesized_expression_list
18935 (parser, attr_flag, /*cast_p=*/false,
18936 /*allow_expansion_p=*/false,
18937 /*non_constant_p=*/NULL);
18938 if (vec == NULL)
18939 arguments = error_mark_node;
18940 else
18941 {
18942 arguments = build_tree_list_vec (vec);
18943 release_tree_vector (vec);
18944 }
18945 /* Save the arguments away. */
18946 TREE_VALUE (attribute) = arguments;
18947 }
18948
18949 if (arguments != error_mark_node)
18950 {
18951 /* Add this attribute to the list. */
18952 TREE_CHAIN (attribute) = attribute_list;
18953 attribute_list = attribute;
18954 }
18955
18956 token = cp_lexer_peek_token (parser->lexer);
18957 }
18958 /* Now, look for more attributes. If the next token isn't a
18959 `,', we're done. */
18960 if (token->type != CPP_COMMA)
18961 break;
18962
18963 /* Consume the comma and keep going. */
18964 cp_lexer_consume_token (parser->lexer);
18965 }
18966 parser->translate_strings_p = save_translate_strings_p;
18967
18968 /* We built up the list in reverse order. */
18969 return nreverse (attribute_list);
18970 }
18971
18972 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18973 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18974 current value of the PEDANTIC flag, regardless of whether or not
18975 the `__extension__' keyword is present. The caller is responsible
18976 for restoring the value of the PEDANTIC flag. */
18977
18978 static bool
18979 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18980 {
18981 /* Save the old value of the PEDANTIC flag. */
18982 *saved_pedantic = pedantic;
18983
18984 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18985 {
18986 /* Consume the `__extension__' token. */
18987 cp_lexer_consume_token (parser->lexer);
18988 /* We're not being pedantic while the `__extension__' keyword is
18989 in effect. */
18990 pedantic = 0;
18991
18992 return true;
18993 }
18994
18995 return false;
18996 }
18997
18998 /* Parse a label declaration.
18999
19000 label-declaration:
19001 __label__ label-declarator-seq ;
19002
19003 label-declarator-seq:
19004 identifier , label-declarator-seq
19005 identifier */
19006
19007 static void
19008 cp_parser_label_declaration (cp_parser* parser)
19009 {
19010 /* Look for the `__label__' keyword. */
19011 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19012
19013 while (true)
19014 {
19015 tree identifier;
19016
19017 /* Look for an identifier. */
19018 identifier = cp_parser_identifier (parser);
19019 /* If we failed, stop. */
19020 if (identifier == error_mark_node)
19021 break;
19022 /* Declare it as a label. */
19023 finish_label_decl (identifier);
19024 /* If the next token is a `;', stop. */
19025 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19026 break;
19027 /* Look for the `,' separating the label declarations. */
19028 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19029 }
19030
19031 /* Look for the final `;'. */
19032 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19033 }
19034
19035 /* Support Functions */
19036
19037 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19038 NAME should have one of the representations used for an
19039 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19040 is returned. If PARSER->SCOPE is a dependent type, then a
19041 SCOPE_REF is returned.
19042
19043 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19044 returned; the name was already resolved when the TEMPLATE_ID_EXPR
19045 was formed. Abstractly, such entities should not be passed to this
19046 function, because they do not need to be looked up, but it is
19047 simpler to check for this special case here, rather than at the
19048 call-sites.
19049
19050 In cases not explicitly covered above, this function returns a
19051 DECL, OVERLOAD, or baselink representing the result of the lookup.
19052 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19053 is returned.
19054
19055 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19056 (e.g., "struct") that was used. In that case bindings that do not
19057 refer to types are ignored.
19058
19059 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19060 ignored.
19061
19062 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19063 are ignored.
19064
19065 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19066 types.
19067
19068 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19069 TREE_LIST of candidates if name-lookup results in an ambiguity, and
19070 NULL_TREE otherwise. */
19071
19072 static tree
19073 cp_parser_lookup_name (cp_parser *parser, tree name,
19074 enum tag_types tag_type,
19075 bool is_template,
19076 bool is_namespace,
19077 bool check_dependency,
19078 tree *ambiguous_decls,
19079 location_t name_location)
19080 {
19081 int flags = 0;
19082 tree decl;
19083 tree object_type = parser->context->object_type;
19084
19085 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19086 flags |= LOOKUP_COMPLAIN;
19087
19088 /* Assume that the lookup will be unambiguous. */
19089 if (ambiguous_decls)
19090 *ambiguous_decls = NULL_TREE;
19091
19092 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19093 no longer valid. Note that if we are parsing tentatively, and
19094 the parse fails, OBJECT_TYPE will be automatically restored. */
19095 parser->context->object_type = NULL_TREE;
19096
19097 if (name == error_mark_node)
19098 return error_mark_node;
19099
19100 /* A template-id has already been resolved; there is no lookup to
19101 do. */
19102 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19103 return name;
19104 if (BASELINK_P (name))
19105 {
19106 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19107 == TEMPLATE_ID_EXPR);
19108 return name;
19109 }
19110
19111 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
19112 it should already have been checked to make sure that the name
19113 used matches the type being destroyed. */
19114 if (TREE_CODE (name) == BIT_NOT_EXPR)
19115 {
19116 tree type;
19117
19118 /* Figure out to which type this destructor applies. */
19119 if (parser->scope)
19120 type = parser->scope;
19121 else if (object_type)
19122 type = object_type;
19123 else
19124 type = current_class_type;
19125 /* If that's not a class type, there is no destructor. */
19126 if (!type || !CLASS_TYPE_P (type))
19127 return error_mark_node;
19128 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19129 lazily_declare_fn (sfk_destructor, type);
19130 if (!CLASSTYPE_DESTRUCTORS (type))
19131 return error_mark_node;
19132 /* If it was a class type, return the destructor. */
19133 return CLASSTYPE_DESTRUCTORS (type);
19134 }
19135
19136 /* By this point, the NAME should be an ordinary identifier. If
19137 the id-expression was a qualified name, the qualifying scope is
19138 stored in PARSER->SCOPE at this point. */
19139 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19140
19141 /* Perform the lookup. */
19142 if (parser->scope)
19143 {
19144 bool dependent_p;
19145
19146 if (parser->scope == error_mark_node)
19147 return error_mark_node;
19148
19149 /* If the SCOPE is dependent, the lookup must be deferred until
19150 the template is instantiated -- unless we are explicitly
19151 looking up names in uninstantiated templates. Even then, we
19152 cannot look up the name if the scope is not a class type; it
19153 might, for example, be a template type parameter. */
19154 dependent_p = (TYPE_P (parser->scope)
19155 && dependent_scope_p (parser->scope));
19156 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19157 && dependent_p)
19158 /* Defer lookup. */
19159 decl = error_mark_node;
19160 else
19161 {
19162 tree pushed_scope = NULL_TREE;
19163
19164 /* If PARSER->SCOPE is a dependent type, then it must be a
19165 class type, and we must not be checking dependencies;
19166 otherwise, we would have processed this lookup above. So
19167 that PARSER->SCOPE is not considered a dependent base by
19168 lookup_member, we must enter the scope here. */
19169 if (dependent_p)
19170 pushed_scope = push_scope (parser->scope);
19171
19172 /* If the PARSER->SCOPE is a template specialization, it
19173 may be instantiated during name lookup. In that case,
19174 errors may be issued. Even if we rollback the current
19175 tentative parse, those errors are valid. */
19176 decl = lookup_qualified_name (parser->scope, name,
19177 tag_type != none_type,
19178 /*complain=*/true);
19179
19180 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19181 lookup result and the nested-name-specifier nominates a class C:
19182 * if the name specified after the nested-name-specifier, when
19183 looked up in C, is the injected-class-name of C (Clause 9), or
19184 * if the name specified after the nested-name-specifier is the
19185 same as the identifier or the simple-template-id's template-
19186 name in the last component of the nested-name-specifier,
19187 the name is instead considered to name the constructor of
19188 class C. [ Note: for example, the constructor is not an
19189 acceptable lookup result in an elaborated-type-specifier so
19190 the constructor would not be used in place of the
19191 injected-class-name. --end note ] Such a constructor name
19192 shall be used only in the declarator-id of a declaration that
19193 names a constructor or in a using-declaration. */
19194 if (tag_type == none_type
19195 && DECL_SELF_REFERENCE_P (decl)
19196 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19197 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19198 tag_type != none_type,
19199 /*complain=*/true);
19200
19201 /* If we have a single function from a using decl, pull it out. */
19202 if (TREE_CODE (decl) == OVERLOAD
19203 && !really_overloaded_fn (decl))
19204 decl = OVL_FUNCTION (decl);
19205
19206 if (pushed_scope)
19207 pop_scope (pushed_scope);
19208 }
19209
19210 /* If the scope is a dependent type and either we deferred lookup or
19211 we did lookup but didn't find the name, rememeber the name. */
19212 if (decl == error_mark_node && TYPE_P (parser->scope)
19213 && dependent_type_p (parser->scope))
19214 {
19215 if (tag_type)
19216 {
19217 tree type;
19218
19219 /* The resolution to Core Issue 180 says that `struct
19220 A::B' should be considered a type-name, even if `A'
19221 is dependent. */
19222 type = make_typename_type (parser->scope, name, tag_type,
19223 /*complain=*/tf_error);
19224 decl = TYPE_NAME (type);
19225 }
19226 else if (is_template
19227 && (cp_parser_next_token_ends_template_argument_p (parser)
19228 || cp_lexer_next_token_is (parser->lexer,
19229 CPP_CLOSE_PAREN)))
19230 decl = make_unbound_class_template (parser->scope,
19231 name, NULL_TREE,
19232 /*complain=*/tf_error);
19233 else
19234 decl = build_qualified_name (/*type=*/NULL_TREE,
19235 parser->scope, name,
19236 is_template);
19237 }
19238 parser->qualifying_scope = parser->scope;
19239 parser->object_scope = NULL_TREE;
19240 }
19241 else if (object_type)
19242 {
19243 tree object_decl = NULL_TREE;
19244 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19245 OBJECT_TYPE is not a class. */
19246 if (CLASS_TYPE_P (object_type))
19247 /* If the OBJECT_TYPE is a template specialization, it may
19248 be instantiated during name lookup. In that case, errors
19249 may be issued. Even if we rollback the current tentative
19250 parse, those errors are valid. */
19251 object_decl = lookup_member (object_type,
19252 name,
19253 /*protect=*/0,
19254 tag_type != none_type);
19255 /* Look it up in the enclosing context, too. */
19256 decl = lookup_name_real (name, tag_type != none_type,
19257 /*nonclass=*/0,
19258 /*block_p=*/true, is_namespace, flags);
19259 parser->object_scope = object_type;
19260 parser->qualifying_scope = NULL_TREE;
19261 if (object_decl)
19262 decl = object_decl;
19263 }
19264 else
19265 {
19266 decl = lookup_name_real (name, tag_type != none_type,
19267 /*nonclass=*/0,
19268 /*block_p=*/true, is_namespace, flags);
19269 parser->qualifying_scope = NULL_TREE;
19270 parser->object_scope = NULL_TREE;
19271 }
19272
19273 /* If the lookup failed, let our caller know. */
19274 if (!decl || decl == error_mark_node)
19275 return error_mark_node;
19276
19277 /* Pull out the template from an injected-class-name (or multiple). */
19278 if (is_template)
19279 decl = maybe_get_template_decl_from_type_decl (decl);
19280
19281 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19282 if (TREE_CODE (decl) == TREE_LIST)
19283 {
19284 if (ambiguous_decls)
19285 *ambiguous_decls = decl;
19286 /* The error message we have to print is too complicated for
19287 cp_parser_error, so we incorporate its actions directly. */
19288 if (!cp_parser_simulate_error (parser))
19289 {
19290 error_at (name_location, "reference to %qD is ambiguous",
19291 name);
19292 print_candidates (decl);
19293 }
19294 return error_mark_node;
19295 }
19296
19297 gcc_assert (DECL_P (decl)
19298 || TREE_CODE (decl) == OVERLOAD
19299 || TREE_CODE (decl) == SCOPE_REF
19300 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19301 || BASELINK_P (decl));
19302
19303 /* If we have resolved the name of a member declaration, check to
19304 see if the declaration is accessible. When the name resolves to
19305 set of overloaded functions, accessibility is checked when
19306 overload resolution is done.
19307
19308 During an explicit instantiation, access is not checked at all,
19309 as per [temp.explicit]. */
19310 if (DECL_P (decl))
19311 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19312
19313 return decl;
19314 }
19315
19316 /* Like cp_parser_lookup_name, but for use in the typical case where
19317 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19318 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19319
19320 static tree
19321 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19322 {
19323 return cp_parser_lookup_name (parser, name,
19324 none_type,
19325 /*is_template=*/false,
19326 /*is_namespace=*/false,
19327 /*check_dependency=*/true,
19328 /*ambiguous_decls=*/NULL,
19329 location);
19330 }
19331
19332 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19333 the current context, return the TYPE_DECL. If TAG_NAME_P is
19334 true, the DECL indicates the class being defined in a class-head,
19335 or declared in an elaborated-type-specifier.
19336
19337 Otherwise, return DECL. */
19338
19339 static tree
19340 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19341 {
19342 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19343 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19344
19345 struct A {
19346 template <typename T> struct B;
19347 };
19348
19349 template <typename T> struct A::B {};
19350
19351 Similarly, in an elaborated-type-specifier:
19352
19353 namespace N { struct X{}; }
19354
19355 struct A {
19356 template <typename T> friend struct N::X;
19357 };
19358
19359 However, if the DECL refers to a class type, and we are in
19360 the scope of the class, then the name lookup automatically
19361 finds the TYPE_DECL created by build_self_reference rather
19362 than a TEMPLATE_DECL. For example, in:
19363
19364 template <class T> struct S {
19365 S s;
19366 };
19367
19368 there is no need to handle such case. */
19369
19370 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19371 return DECL_TEMPLATE_RESULT (decl);
19372
19373 return decl;
19374 }
19375
19376 /* If too many, or too few, template-parameter lists apply to the
19377 declarator, issue an error message. Returns TRUE if all went well,
19378 and FALSE otherwise. */
19379
19380 static bool
19381 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19382 cp_declarator *declarator,
19383 location_t declarator_location)
19384 {
19385 unsigned num_templates;
19386
19387 /* We haven't seen any classes that involve template parameters yet. */
19388 num_templates = 0;
19389
19390 switch (declarator->kind)
19391 {
19392 case cdk_id:
19393 if (declarator->u.id.qualifying_scope)
19394 {
19395 tree scope;
19396
19397 scope = declarator->u.id.qualifying_scope;
19398
19399 while (scope && CLASS_TYPE_P (scope))
19400 {
19401 /* You're supposed to have one `template <...>'
19402 for every template class, but you don't need one
19403 for a full specialization. For example:
19404
19405 template <class T> struct S{};
19406 template <> struct S<int> { void f(); };
19407 void S<int>::f () {}
19408
19409 is correct; there shouldn't be a `template <>' for
19410 the definition of `S<int>::f'. */
19411 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19412 /* If SCOPE does not have template information of any
19413 kind, then it is not a template, nor is it nested
19414 within a template. */
19415 break;
19416 if (explicit_class_specialization_p (scope))
19417 break;
19418 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19419 ++num_templates;
19420
19421 scope = TYPE_CONTEXT (scope);
19422 }
19423 }
19424 else if (TREE_CODE (declarator->u.id.unqualified_name)
19425 == TEMPLATE_ID_EXPR)
19426 /* If the DECLARATOR has the form `X<y>' then it uses one
19427 additional level of template parameters. */
19428 ++num_templates;
19429
19430 return cp_parser_check_template_parameters
19431 (parser, num_templates, declarator_location, declarator);
19432
19433
19434 case cdk_function:
19435 case cdk_array:
19436 case cdk_pointer:
19437 case cdk_reference:
19438 case cdk_ptrmem:
19439 return (cp_parser_check_declarator_template_parameters
19440 (parser, declarator->declarator, declarator_location));
19441
19442 case cdk_error:
19443 return true;
19444
19445 default:
19446 gcc_unreachable ();
19447 }
19448 return false;
19449 }
19450
19451 /* NUM_TEMPLATES were used in the current declaration. If that is
19452 invalid, return FALSE and issue an error messages. Otherwise,
19453 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19454 declarator and we can print more accurate diagnostics. */
19455
19456 static bool
19457 cp_parser_check_template_parameters (cp_parser* parser,
19458 unsigned num_templates,
19459 location_t location,
19460 cp_declarator *declarator)
19461 {
19462 /* If there are the same number of template classes and parameter
19463 lists, that's OK. */
19464 if (parser->num_template_parameter_lists == num_templates)
19465 return true;
19466 /* If there are more, but only one more, then we are referring to a
19467 member template. That's OK too. */
19468 if (parser->num_template_parameter_lists == num_templates + 1)
19469 return true;
19470 /* If there are more template classes than parameter lists, we have
19471 something like:
19472
19473 template <class T> void S<T>::R<T>::f (); */
19474 if (parser->num_template_parameter_lists < num_templates)
19475 {
19476 if (declarator && !current_function_decl)
19477 error_at (location, "specializing member %<%T::%E%> "
19478 "requires %<template<>%> syntax",
19479 declarator->u.id.qualifying_scope,
19480 declarator->u.id.unqualified_name);
19481 else if (declarator)
19482 error_at (location, "invalid declaration of %<%T::%E%>",
19483 declarator->u.id.qualifying_scope,
19484 declarator->u.id.unqualified_name);
19485 else
19486 error_at (location, "too few template-parameter-lists");
19487 return false;
19488 }
19489 /* Otherwise, there are too many template parameter lists. We have
19490 something like:
19491
19492 template <class T> template <class U> void S::f(); */
19493 error_at (location, "too many template-parameter-lists");
19494 return false;
19495 }
19496
19497 /* Parse an optional `::' token indicating that the following name is
19498 from the global namespace. If so, PARSER->SCOPE is set to the
19499 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19500 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19501 Returns the new value of PARSER->SCOPE, if the `::' token is
19502 present, and NULL_TREE otherwise. */
19503
19504 static tree
19505 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19506 {
19507 cp_token *token;
19508
19509 /* Peek at the next token. */
19510 token = cp_lexer_peek_token (parser->lexer);
19511 /* If we're looking at a `::' token then we're starting from the
19512 global namespace, not our current location. */
19513 if (token->type == CPP_SCOPE)
19514 {
19515 /* Consume the `::' token. */
19516 cp_lexer_consume_token (parser->lexer);
19517 /* Set the SCOPE so that we know where to start the lookup. */
19518 parser->scope = global_namespace;
19519 parser->qualifying_scope = global_namespace;
19520 parser->object_scope = NULL_TREE;
19521
19522 return parser->scope;
19523 }
19524 else if (!current_scope_valid_p)
19525 {
19526 parser->scope = NULL_TREE;
19527 parser->qualifying_scope = NULL_TREE;
19528 parser->object_scope = NULL_TREE;
19529 }
19530
19531 return NULL_TREE;
19532 }
19533
19534 /* Returns TRUE if the upcoming token sequence is the start of a
19535 constructor declarator. If FRIEND_P is true, the declarator is
19536 preceded by the `friend' specifier. */
19537
19538 static bool
19539 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19540 {
19541 bool constructor_p;
19542 tree nested_name_specifier;
19543 cp_token *next_token;
19544
19545 /* The common case is that this is not a constructor declarator, so
19546 try to avoid doing lots of work if at all possible. It's not
19547 valid declare a constructor at function scope. */
19548 if (parser->in_function_body)
19549 return false;
19550 /* And only certain tokens can begin a constructor declarator. */
19551 next_token = cp_lexer_peek_token (parser->lexer);
19552 if (next_token->type != CPP_NAME
19553 && next_token->type != CPP_SCOPE
19554 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19555 && next_token->type != CPP_TEMPLATE_ID)
19556 return false;
19557
19558 /* Parse tentatively; we are going to roll back all of the tokens
19559 consumed here. */
19560 cp_parser_parse_tentatively (parser);
19561 /* Assume that we are looking at a constructor declarator. */
19562 constructor_p = true;
19563
19564 /* Look for the optional `::' operator. */
19565 cp_parser_global_scope_opt (parser,
19566 /*current_scope_valid_p=*/false);
19567 /* Look for the nested-name-specifier. */
19568 nested_name_specifier
19569 = (cp_parser_nested_name_specifier_opt (parser,
19570 /*typename_keyword_p=*/false,
19571 /*check_dependency_p=*/false,
19572 /*type_p=*/false,
19573 /*is_declaration=*/false));
19574 /* Outside of a class-specifier, there must be a
19575 nested-name-specifier. */
19576 if (!nested_name_specifier &&
19577 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19578 || friend_p))
19579 constructor_p = false;
19580 else if (nested_name_specifier == error_mark_node)
19581 constructor_p = false;
19582
19583 /* If we have a class scope, this is easy; DR 147 says that S::S always
19584 names the constructor, and no other qualified name could. */
19585 if (constructor_p && nested_name_specifier
19586 && TYPE_P (nested_name_specifier))
19587 {
19588 tree id = cp_parser_unqualified_id (parser,
19589 /*template_keyword_p=*/false,
19590 /*check_dependency_p=*/false,
19591 /*declarator_p=*/true,
19592 /*optional_p=*/false);
19593 if (is_overloaded_fn (id))
19594 id = DECL_NAME (get_first_fn (id));
19595 if (!constructor_name_p (id, nested_name_specifier))
19596 constructor_p = false;
19597 }
19598 /* If we still think that this might be a constructor-declarator,
19599 look for a class-name. */
19600 else if (constructor_p)
19601 {
19602 /* If we have:
19603
19604 template <typename T> struct S {
19605 S();
19606 };
19607
19608 we must recognize that the nested `S' names a class. */
19609 tree type_decl;
19610 type_decl = cp_parser_class_name (parser,
19611 /*typename_keyword_p=*/false,
19612 /*template_keyword_p=*/false,
19613 none_type,
19614 /*check_dependency_p=*/false,
19615 /*class_head_p=*/false,
19616 /*is_declaration=*/false);
19617 /* If there was no class-name, then this is not a constructor. */
19618 constructor_p = !cp_parser_error_occurred (parser);
19619
19620 /* If we're still considering a constructor, we have to see a `(',
19621 to begin the parameter-declaration-clause, followed by either a
19622 `)', an `...', or a decl-specifier. We need to check for a
19623 type-specifier to avoid being fooled into thinking that:
19624
19625 S (f) (int);
19626
19627 is a constructor. (It is actually a function named `f' that
19628 takes one parameter (of type `int') and returns a value of type
19629 `S'. */
19630 if (constructor_p
19631 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19632 constructor_p = false;
19633
19634 if (constructor_p
19635 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19636 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19637 /* A parameter declaration begins with a decl-specifier,
19638 which is either the "attribute" keyword, a storage class
19639 specifier, or (usually) a type-specifier. */
19640 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19641 {
19642 tree type;
19643 tree pushed_scope = NULL_TREE;
19644 unsigned saved_num_template_parameter_lists;
19645
19646 /* Names appearing in the type-specifier should be looked up
19647 in the scope of the class. */
19648 if (current_class_type)
19649 type = NULL_TREE;
19650 else
19651 {
19652 type = TREE_TYPE (type_decl);
19653 if (TREE_CODE (type) == TYPENAME_TYPE)
19654 {
19655 type = resolve_typename_type (type,
19656 /*only_current_p=*/false);
19657 if (TREE_CODE (type) == TYPENAME_TYPE)
19658 {
19659 cp_parser_abort_tentative_parse (parser);
19660 return false;
19661 }
19662 }
19663 pushed_scope = push_scope (type);
19664 }
19665
19666 /* Inside the constructor parameter list, surrounding
19667 template-parameter-lists do not apply. */
19668 saved_num_template_parameter_lists
19669 = parser->num_template_parameter_lists;
19670 parser->num_template_parameter_lists = 0;
19671
19672 /* Look for the type-specifier. */
19673 cp_parser_type_specifier (parser,
19674 CP_PARSER_FLAGS_NONE,
19675 /*decl_specs=*/NULL,
19676 /*is_declarator=*/true,
19677 /*declares_class_or_enum=*/NULL,
19678 /*is_cv_qualifier=*/NULL);
19679
19680 parser->num_template_parameter_lists
19681 = saved_num_template_parameter_lists;
19682
19683 /* Leave the scope of the class. */
19684 if (pushed_scope)
19685 pop_scope (pushed_scope);
19686
19687 constructor_p = !cp_parser_error_occurred (parser);
19688 }
19689 }
19690
19691 /* We did not really want to consume any tokens. */
19692 cp_parser_abort_tentative_parse (parser);
19693
19694 return constructor_p;
19695 }
19696
19697 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19698 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19699 they must be performed once we are in the scope of the function.
19700
19701 Returns the function defined. */
19702
19703 static tree
19704 cp_parser_function_definition_from_specifiers_and_declarator
19705 (cp_parser* parser,
19706 cp_decl_specifier_seq *decl_specifiers,
19707 tree attributes,
19708 const cp_declarator *declarator)
19709 {
19710 tree fn;
19711 bool success_p;
19712
19713 /* Begin the function-definition. */
19714 success_p = start_function (decl_specifiers, declarator, attributes);
19715
19716 /* The things we're about to see are not directly qualified by any
19717 template headers we've seen thus far. */
19718 reset_specialization ();
19719
19720 /* If there were names looked up in the decl-specifier-seq that we
19721 did not check, check them now. We must wait until we are in the
19722 scope of the function to perform the checks, since the function
19723 might be a friend. */
19724 perform_deferred_access_checks ();
19725
19726 if (!success_p)
19727 {
19728 /* Skip the entire function. */
19729 cp_parser_skip_to_end_of_block_or_statement (parser);
19730 fn = error_mark_node;
19731 }
19732 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19733 {
19734 /* Seen already, skip it. An error message has already been output. */
19735 cp_parser_skip_to_end_of_block_or_statement (parser);
19736 fn = current_function_decl;
19737 current_function_decl = NULL_TREE;
19738 /* If this is a function from a class, pop the nested class. */
19739 if (current_class_name)
19740 pop_nested_class ();
19741 }
19742 else
19743 fn = cp_parser_function_definition_after_declarator (parser,
19744 /*inline_p=*/false);
19745
19746 return fn;
19747 }
19748
19749 /* Parse the part of a function-definition that follows the
19750 declarator. INLINE_P is TRUE iff this function is an inline
19751 function defined within a class-specifier.
19752
19753 Returns the function defined. */
19754
19755 static tree
19756 cp_parser_function_definition_after_declarator (cp_parser* parser,
19757 bool inline_p)
19758 {
19759 tree fn;
19760 bool ctor_initializer_p = false;
19761 bool saved_in_unbraced_linkage_specification_p;
19762 bool saved_in_function_body;
19763 unsigned saved_num_template_parameter_lists;
19764 cp_token *token;
19765
19766 saved_in_function_body = parser->in_function_body;
19767 parser->in_function_body = true;
19768 /* If the next token is `return', then the code may be trying to
19769 make use of the "named return value" extension that G++ used to
19770 support. */
19771 token = cp_lexer_peek_token (parser->lexer);
19772 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19773 {
19774 /* Consume the `return' keyword. */
19775 cp_lexer_consume_token (parser->lexer);
19776 /* Look for the identifier that indicates what value is to be
19777 returned. */
19778 cp_parser_identifier (parser);
19779 /* Issue an error message. */
19780 error_at (token->location,
19781 "named return values are no longer supported");
19782 /* Skip tokens until we reach the start of the function body. */
19783 while (true)
19784 {
19785 cp_token *token = cp_lexer_peek_token (parser->lexer);
19786 if (token->type == CPP_OPEN_BRACE
19787 || token->type == CPP_EOF
19788 || token->type == CPP_PRAGMA_EOL)
19789 break;
19790 cp_lexer_consume_token (parser->lexer);
19791 }
19792 }
19793 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19794 anything declared inside `f'. */
19795 saved_in_unbraced_linkage_specification_p
19796 = parser->in_unbraced_linkage_specification_p;
19797 parser->in_unbraced_linkage_specification_p = false;
19798 /* Inside the function, surrounding template-parameter-lists do not
19799 apply. */
19800 saved_num_template_parameter_lists
19801 = parser->num_template_parameter_lists;
19802 parser->num_template_parameter_lists = 0;
19803
19804 start_lambda_scope (current_function_decl);
19805
19806 /* If the next token is `try', then we are looking at a
19807 function-try-block. */
19808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19809 ctor_initializer_p = cp_parser_function_try_block (parser);
19810 /* A function-try-block includes the function-body, so we only do
19811 this next part if we're not processing a function-try-block. */
19812 else
19813 ctor_initializer_p
19814 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19815
19816 finish_lambda_scope ();
19817
19818 /* Finish the function. */
19819 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19820 (inline_p ? 2 : 0));
19821 /* Generate code for it, if necessary. */
19822 expand_or_defer_fn (fn);
19823 /* Restore the saved values. */
19824 parser->in_unbraced_linkage_specification_p
19825 = saved_in_unbraced_linkage_specification_p;
19826 parser->num_template_parameter_lists
19827 = saved_num_template_parameter_lists;
19828 parser->in_function_body = saved_in_function_body;
19829
19830 return fn;
19831 }
19832
19833 /* Parse a template-declaration, assuming that the `export' (and
19834 `extern') keywords, if present, has already been scanned. MEMBER_P
19835 is as for cp_parser_template_declaration. */
19836
19837 static void
19838 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19839 {
19840 tree decl = NULL_TREE;
19841 VEC (deferred_access_check,gc) *checks;
19842 tree parameter_list;
19843 bool friend_p = false;
19844 bool need_lang_pop;
19845 cp_token *token;
19846
19847 /* Look for the `template' keyword. */
19848 token = cp_lexer_peek_token (parser->lexer);
19849 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19850 return;
19851
19852 /* And the `<'. */
19853 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19854 return;
19855 if (at_class_scope_p () && current_function_decl)
19856 {
19857 /* 14.5.2.2 [temp.mem]
19858
19859 A local class shall not have member templates. */
19860 error_at (token->location,
19861 "invalid declaration of member template in local class");
19862 cp_parser_skip_to_end_of_block_or_statement (parser);
19863 return;
19864 }
19865 /* [temp]
19866
19867 A template ... shall not have C linkage. */
19868 if (current_lang_name == lang_name_c)
19869 {
19870 error_at (token->location, "template with C linkage");
19871 /* Give it C++ linkage to avoid confusing other parts of the
19872 front end. */
19873 push_lang_context (lang_name_cplusplus);
19874 need_lang_pop = true;
19875 }
19876 else
19877 need_lang_pop = false;
19878
19879 /* We cannot perform access checks on the template parameter
19880 declarations until we know what is being declared, just as we
19881 cannot check the decl-specifier list. */
19882 push_deferring_access_checks (dk_deferred);
19883
19884 /* If the next token is `>', then we have an invalid
19885 specialization. Rather than complain about an invalid template
19886 parameter, issue an error message here. */
19887 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19888 {
19889 cp_parser_error (parser, "invalid explicit specialization");
19890 begin_specialization ();
19891 parameter_list = NULL_TREE;
19892 }
19893 else
19894 /* Parse the template parameters. */
19895 parameter_list = cp_parser_template_parameter_list (parser);
19896
19897 /* Get the deferred access checks from the parameter list. These
19898 will be checked once we know what is being declared, as for a
19899 member template the checks must be performed in the scope of the
19900 class containing the member. */
19901 checks = get_deferred_access_checks ();
19902
19903 /* Look for the `>'. */
19904 cp_parser_skip_to_end_of_template_parameter_list (parser);
19905 /* We just processed one more parameter list. */
19906 ++parser->num_template_parameter_lists;
19907 /* If the next token is `template', there are more template
19908 parameters. */
19909 if (cp_lexer_next_token_is_keyword (parser->lexer,
19910 RID_TEMPLATE))
19911 cp_parser_template_declaration_after_export (parser, member_p);
19912 else
19913 {
19914 /* There are no access checks when parsing a template, as we do not
19915 know if a specialization will be a friend. */
19916 push_deferring_access_checks (dk_no_check);
19917 token = cp_lexer_peek_token (parser->lexer);
19918 decl = cp_parser_single_declaration (parser,
19919 checks,
19920 member_p,
19921 /*explicit_specialization_p=*/false,
19922 &friend_p);
19923 pop_deferring_access_checks ();
19924
19925 /* If this is a member template declaration, let the front
19926 end know. */
19927 if (member_p && !friend_p && decl)
19928 {
19929 if (TREE_CODE (decl) == TYPE_DECL)
19930 cp_parser_check_access_in_redeclaration (decl, token->location);
19931
19932 decl = finish_member_template_decl (decl);
19933 }
19934 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19935 make_friend_class (current_class_type, TREE_TYPE (decl),
19936 /*complain=*/true);
19937 }
19938 /* We are done with the current parameter list. */
19939 --parser->num_template_parameter_lists;
19940
19941 pop_deferring_access_checks ();
19942
19943 /* Finish up. */
19944 finish_template_decl (parameter_list);
19945
19946 /* Register member declarations. */
19947 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19948 finish_member_declaration (decl);
19949 /* For the erroneous case of a template with C linkage, we pushed an
19950 implicit C++ linkage scope; exit that scope now. */
19951 if (need_lang_pop)
19952 pop_lang_context ();
19953 /* If DECL is a function template, we must return to parse it later.
19954 (Even though there is no definition, there might be default
19955 arguments that need handling.) */
19956 if (member_p && decl
19957 && (TREE_CODE (decl) == FUNCTION_DECL
19958 || DECL_FUNCTION_TEMPLATE_P (decl)))
19959 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19960 }
19961
19962 /* Perform the deferred access checks from a template-parameter-list.
19963 CHECKS is a TREE_LIST of access checks, as returned by
19964 get_deferred_access_checks. */
19965
19966 static void
19967 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19968 {
19969 ++processing_template_parmlist;
19970 perform_access_checks (checks);
19971 --processing_template_parmlist;
19972 }
19973
19974 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19975 `function-definition' sequence. MEMBER_P is true, this declaration
19976 appears in a class scope.
19977
19978 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19979 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19980
19981 static tree
19982 cp_parser_single_declaration (cp_parser* parser,
19983 VEC (deferred_access_check,gc)* checks,
19984 bool member_p,
19985 bool explicit_specialization_p,
19986 bool* friend_p)
19987 {
19988 int declares_class_or_enum;
19989 tree decl = NULL_TREE;
19990 cp_decl_specifier_seq decl_specifiers;
19991 bool function_definition_p = false;
19992 cp_token *decl_spec_token_start;
19993
19994 /* This function is only used when processing a template
19995 declaration. */
19996 gcc_assert (innermost_scope_kind () == sk_template_parms
19997 || innermost_scope_kind () == sk_template_spec);
19998
19999 /* Defer access checks until we know what is being declared. */
20000 push_deferring_access_checks (dk_deferred);
20001
20002 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20003 alternative. */
20004 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20005 cp_parser_decl_specifier_seq (parser,
20006 CP_PARSER_FLAGS_OPTIONAL,
20007 &decl_specifiers,
20008 &declares_class_or_enum);
20009 if (friend_p)
20010 *friend_p = cp_parser_friend_p (&decl_specifiers);
20011
20012 /* There are no template typedefs. */
20013 if (decl_specifiers.specs[(int) ds_typedef])
20014 {
20015 error_at (decl_spec_token_start->location,
20016 "template declaration of %<typedef%>");
20017 decl = error_mark_node;
20018 }
20019
20020 /* Gather up the access checks that occurred the
20021 decl-specifier-seq. */
20022 stop_deferring_access_checks ();
20023
20024 /* Check for the declaration of a template class. */
20025 if (declares_class_or_enum)
20026 {
20027 if (cp_parser_declares_only_class_p (parser))
20028 {
20029 decl = shadow_tag (&decl_specifiers);
20030
20031 /* In this case:
20032
20033 struct C {
20034 friend template <typename T> struct A<T>::B;
20035 };
20036
20037 A<T>::B will be represented by a TYPENAME_TYPE, and
20038 therefore not recognized by shadow_tag. */
20039 if (friend_p && *friend_p
20040 && !decl
20041 && decl_specifiers.type
20042 && TYPE_P (decl_specifiers.type))
20043 decl = decl_specifiers.type;
20044
20045 if (decl && decl != error_mark_node)
20046 decl = TYPE_NAME (decl);
20047 else
20048 decl = error_mark_node;
20049
20050 /* Perform access checks for template parameters. */
20051 cp_parser_perform_template_parameter_access_checks (checks);
20052 }
20053 }
20054
20055 /* Complain about missing 'typename' or other invalid type names. */
20056 if (!decl_specifiers.any_type_specifiers_p)
20057 cp_parser_parse_and_diagnose_invalid_type_name (parser);
20058
20059 /* If it's not a template class, try for a template function. If
20060 the next token is a `;', then this declaration does not declare
20061 anything. But, if there were errors in the decl-specifiers, then
20062 the error might well have come from an attempted class-specifier.
20063 In that case, there's no need to warn about a missing declarator. */
20064 if (!decl
20065 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20066 || decl_specifiers.type != error_mark_node))
20067 {
20068 decl = cp_parser_init_declarator (parser,
20069 &decl_specifiers,
20070 checks,
20071 /*function_definition_allowed_p=*/true,
20072 member_p,
20073 declares_class_or_enum,
20074 &function_definition_p);
20075
20076 /* 7.1.1-1 [dcl.stc]
20077
20078 A storage-class-specifier shall not be specified in an explicit
20079 specialization... */
20080 if (decl
20081 && explicit_specialization_p
20082 && decl_specifiers.storage_class != sc_none)
20083 {
20084 error_at (decl_spec_token_start->location,
20085 "explicit template specialization cannot have a storage class");
20086 decl = error_mark_node;
20087 }
20088 }
20089
20090 pop_deferring_access_checks ();
20091
20092 /* Clear any current qualification; whatever comes next is the start
20093 of something new. */
20094 parser->scope = NULL_TREE;
20095 parser->qualifying_scope = NULL_TREE;
20096 parser->object_scope = NULL_TREE;
20097 /* Look for a trailing `;' after the declaration. */
20098 if (!function_definition_p
20099 && (decl == error_mark_node
20100 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20101 cp_parser_skip_to_end_of_block_or_statement (parser);
20102
20103 return decl;
20104 }
20105
20106 /* Parse a cast-expression that is not the operand of a unary "&". */
20107
20108 static tree
20109 cp_parser_simple_cast_expression (cp_parser *parser)
20110 {
20111 return cp_parser_cast_expression (parser, /*address_p=*/false,
20112 /*cast_p=*/false, NULL);
20113 }
20114
20115 /* Parse a functional cast to TYPE. Returns an expression
20116 representing the cast. */
20117
20118 static tree
20119 cp_parser_functional_cast (cp_parser* parser, tree type)
20120 {
20121 VEC(tree,gc) *vec;
20122 tree expression_list;
20123 tree cast;
20124 bool nonconst_p;
20125
20126 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20127 {
20128 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20129 expression_list = cp_parser_braced_list (parser, &nonconst_p);
20130 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20131 if (TREE_CODE (type) == TYPE_DECL)
20132 type = TREE_TYPE (type);
20133 return finish_compound_literal (type, expression_list);
20134 }
20135
20136
20137 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20138 /*cast_p=*/true,
20139 /*allow_expansion_p=*/true,
20140 /*non_constant_p=*/NULL);
20141 if (vec == NULL)
20142 expression_list = error_mark_node;
20143 else
20144 {
20145 expression_list = build_tree_list_vec (vec);
20146 release_tree_vector (vec);
20147 }
20148
20149 cast = build_functional_cast (type, expression_list,
20150 tf_warning_or_error);
20151 /* [expr.const]/1: In an integral constant expression "only type
20152 conversions to integral or enumeration type can be used". */
20153 if (TREE_CODE (type) == TYPE_DECL)
20154 type = TREE_TYPE (type);
20155 if (cast != error_mark_node
20156 && !cast_valid_in_integral_constant_expression_p (type)
20157 && cp_parser_non_integral_constant_expression (parser,
20158 NIC_CONSTRUCTOR))
20159 return error_mark_node;
20160 return cast;
20161 }
20162
20163 /* Save the tokens that make up the body of a member function defined
20164 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
20165 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
20166 specifiers applied to the declaration. Returns the FUNCTION_DECL
20167 for the member function. */
20168
20169 static tree
20170 cp_parser_save_member_function_body (cp_parser* parser,
20171 cp_decl_specifier_seq *decl_specifiers,
20172 cp_declarator *declarator,
20173 tree attributes)
20174 {
20175 cp_token *first;
20176 cp_token *last;
20177 tree fn;
20178
20179 /* Create the FUNCTION_DECL. */
20180 fn = grokmethod (decl_specifiers, declarator, attributes);
20181 /* If something went badly wrong, bail out now. */
20182 if (fn == error_mark_node)
20183 {
20184 /* If there's a function-body, skip it. */
20185 if (cp_parser_token_starts_function_definition_p
20186 (cp_lexer_peek_token (parser->lexer)))
20187 cp_parser_skip_to_end_of_block_or_statement (parser);
20188 return error_mark_node;
20189 }
20190
20191 /* Remember it, if there default args to post process. */
20192 cp_parser_save_default_args (parser, fn);
20193
20194 /* Save away the tokens that make up the body of the
20195 function. */
20196 first = parser->lexer->next_token;
20197 /* We can have braced-init-list mem-initializers before the fn body. */
20198 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20199 {
20200 cp_lexer_consume_token (parser->lexer);
20201 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20202 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20203 {
20204 /* cache_group will stop after an un-nested { } pair, too. */
20205 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20206 break;
20207
20208 /* variadic mem-inits have ... after the ')'. */
20209 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20210 cp_lexer_consume_token (parser->lexer);
20211 }
20212 }
20213 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20214 /* Handle function try blocks. */
20215 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20216 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20217 last = parser->lexer->next_token;
20218
20219 /* Save away the inline definition; we will process it when the
20220 class is complete. */
20221 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20222 DECL_PENDING_INLINE_P (fn) = 1;
20223
20224 /* We need to know that this was defined in the class, so that
20225 friend templates are handled correctly. */
20226 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20227
20228 /* Add FN to the queue of functions to be parsed later. */
20229 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20230
20231 return fn;
20232 }
20233
20234 /* Parse a template-argument-list, as well as the trailing ">" (but
20235 not the opening ">"). See cp_parser_template_argument_list for the
20236 return value. */
20237
20238 static tree
20239 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20240 {
20241 tree arguments;
20242 tree saved_scope;
20243 tree saved_qualifying_scope;
20244 tree saved_object_scope;
20245 bool saved_greater_than_is_operator_p;
20246 int saved_unevaluated_operand;
20247 int saved_inhibit_evaluation_warnings;
20248
20249 /* [temp.names]
20250
20251 When parsing a template-id, the first non-nested `>' is taken as
20252 the end of the template-argument-list rather than a greater-than
20253 operator. */
20254 saved_greater_than_is_operator_p
20255 = parser->greater_than_is_operator_p;
20256 parser->greater_than_is_operator_p = false;
20257 /* Parsing the argument list may modify SCOPE, so we save it
20258 here. */
20259 saved_scope = parser->scope;
20260 saved_qualifying_scope = parser->qualifying_scope;
20261 saved_object_scope = parser->object_scope;
20262 /* We need to evaluate the template arguments, even though this
20263 template-id may be nested within a "sizeof". */
20264 saved_unevaluated_operand = cp_unevaluated_operand;
20265 cp_unevaluated_operand = 0;
20266 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20267 c_inhibit_evaluation_warnings = 0;
20268 /* Parse the template-argument-list itself. */
20269 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20270 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20271 arguments = NULL_TREE;
20272 else
20273 arguments = cp_parser_template_argument_list (parser);
20274 /* Look for the `>' that ends the template-argument-list. If we find
20275 a '>>' instead, it's probably just a typo. */
20276 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20277 {
20278 if (cxx_dialect != cxx98)
20279 {
20280 /* In C++0x, a `>>' in a template argument list or cast
20281 expression is considered to be two separate `>'
20282 tokens. So, change the current token to a `>', but don't
20283 consume it: it will be consumed later when the outer
20284 template argument list (or cast expression) is parsed.
20285 Note that this replacement of `>' for `>>' is necessary
20286 even if we are parsing tentatively: in the tentative
20287 case, after calling
20288 cp_parser_enclosed_template_argument_list we will always
20289 throw away all of the template arguments and the first
20290 closing `>', either because the template argument list
20291 was erroneous or because we are replacing those tokens
20292 with a CPP_TEMPLATE_ID token. The second `>' (which will
20293 not have been thrown away) is needed either to close an
20294 outer template argument list or to complete a new-style
20295 cast. */
20296 cp_token *token = cp_lexer_peek_token (parser->lexer);
20297 token->type = CPP_GREATER;
20298 }
20299 else if (!saved_greater_than_is_operator_p)
20300 {
20301 /* If we're in a nested template argument list, the '>>' has
20302 to be a typo for '> >'. We emit the error message, but we
20303 continue parsing and we push a '>' as next token, so that
20304 the argument list will be parsed correctly. Note that the
20305 global source location is still on the token before the
20306 '>>', so we need to say explicitly where we want it. */
20307 cp_token *token = cp_lexer_peek_token (parser->lexer);
20308 error_at (token->location, "%<>>%> should be %<> >%> "
20309 "within a nested template argument list");
20310
20311 token->type = CPP_GREATER;
20312 }
20313 else
20314 {
20315 /* If this is not a nested template argument list, the '>>'
20316 is a typo for '>'. Emit an error message and continue.
20317 Same deal about the token location, but here we can get it
20318 right by consuming the '>>' before issuing the diagnostic. */
20319 cp_token *token = cp_lexer_consume_token (parser->lexer);
20320 error_at (token->location,
20321 "spurious %<>>%>, use %<>%> to terminate "
20322 "a template argument list");
20323 }
20324 }
20325 else
20326 cp_parser_skip_to_end_of_template_parameter_list (parser);
20327 /* The `>' token might be a greater-than operator again now. */
20328 parser->greater_than_is_operator_p
20329 = saved_greater_than_is_operator_p;
20330 /* Restore the SAVED_SCOPE. */
20331 parser->scope = saved_scope;
20332 parser->qualifying_scope = saved_qualifying_scope;
20333 parser->object_scope = saved_object_scope;
20334 cp_unevaluated_operand = saved_unevaluated_operand;
20335 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20336
20337 return arguments;
20338 }
20339
20340 /* MEMBER_FUNCTION is a member function, or a friend. If default
20341 arguments, or the body of the function have not yet been parsed,
20342 parse them now. */
20343
20344 static void
20345 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20346 {
20347 /* If this member is a template, get the underlying
20348 FUNCTION_DECL. */
20349 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20350 member_function = DECL_TEMPLATE_RESULT (member_function);
20351
20352 /* There should not be any class definitions in progress at this
20353 point; the bodies of members are only parsed outside of all class
20354 definitions. */
20355 gcc_assert (parser->num_classes_being_defined == 0);
20356 /* While we're parsing the member functions we might encounter more
20357 classes. We want to handle them right away, but we don't want
20358 them getting mixed up with functions that are currently in the
20359 queue. */
20360 push_unparsed_function_queues (parser);
20361
20362 /* Make sure that any template parameters are in scope. */
20363 maybe_begin_member_template_processing (member_function);
20364
20365 /* If the body of the function has not yet been parsed, parse it
20366 now. */
20367 if (DECL_PENDING_INLINE_P (member_function))
20368 {
20369 tree function_scope;
20370 cp_token_cache *tokens;
20371
20372 /* The function is no longer pending; we are processing it. */
20373 tokens = DECL_PENDING_INLINE_INFO (member_function);
20374 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20375 DECL_PENDING_INLINE_P (member_function) = 0;
20376
20377 /* If this is a local class, enter the scope of the containing
20378 function. */
20379 function_scope = current_function_decl;
20380 if (function_scope)
20381 push_function_context ();
20382
20383 /* Push the body of the function onto the lexer stack. */
20384 cp_parser_push_lexer_for_tokens (parser, tokens);
20385
20386 /* Let the front end know that we going to be defining this
20387 function. */
20388 start_preparsed_function (member_function, NULL_TREE,
20389 SF_PRE_PARSED | SF_INCLASS_INLINE);
20390
20391 /* Don't do access checking if it is a templated function. */
20392 if (processing_template_decl)
20393 push_deferring_access_checks (dk_no_check);
20394
20395 /* Now, parse the body of the function. */
20396 cp_parser_function_definition_after_declarator (parser,
20397 /*inline_p=*/true);
20398
20399 if (processing_template_decl)
20400 pop_deferring_access_checks ();
20401
20402 /* Leave the scope of the containing function. */
20403 if (function_scope)
20404 pop_function_context ();
20405 cp_parser_pop_lexer (parser);
20406 }
20407
20408 /* Remove any template parameters from the symbol table. */
20409 maybe_end_member_template_processing ();
20410
20411 /* Restore the queue. */
20412 pop_unparsed_function_queues (parser);
20413 }
20414
20415 /* If DECL contains any default args, remember it on the unparsed
20416 functions queue. */
20417
20418 static void
20419 cp_parser_save_default_args (cp_parser* parser, tree decl)
20420 {
20421 tree probe;
20422
20423 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20424 probe;
20425 probe = TREE_CHAIN (probe))
20426 if (TREE_PURPOSE (probe))
20427 {
20428 cp_default_arg_entry *entry
20429 = VEC_safe_push (cp_default_arg_entry, gc,
20430 unparsed_funs_with_default_args, NULL);
20431 entry->class_type = current_class_type;
20432 entry->decl = decl;
20433 break;
20434 }
20435 }
20436
20437 /* FN is a FUNCTION_DECL which may contains a parameter with an
20438 unparsed DEFAULT_ARG. Parse the default args now. This function
20439 assumes that the current scope is the scope in which the default
20440 argument should be processed. */
20441
20442 static void
20443 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20444 {
20445 bool saved_local_variables_forbidden_p;
20446 tree parm, parmdecl;
20447
20448 /* While we're parsing the default args, we might (due to the
20449 statement expression extension) encounter more classes. We want
20450 to handle them right away, but we don't want them getting mixed
20451 up with default args that are currently in the queue. */
20452 push_unparsed_function_queues (parser);
20453
20454 /* Local variable names (and the `this' keyword) may not appear
20455 in a default argument. */
20456 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20457 parser->local_variables_forbidden_p = true;
20458
20459 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20460 parmdecl = DECL_ARGUMENTS (fn);
20461 parm && parm != void_list_node;
20462 parm = TREE_CHAIN (parm),
20463 parmdecl = DECL_CHAIN (parmdecl))
20464 {
20465 cp_token_cache *tokens;
20466 tree default_arg = TREE_PURPOSE (parm);
20467 tree parsed_arg;
20468 VEC(tree,gc) *insts;
20469 tree copy;
20470 unsigned ix;
20471
20472 if (!default_arg)
20473 continue;
20474
20475 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20476 /* This can happen for a friend declaration for a function
20477 already declared with default arguments. */
20478 continue;
20479
20480 /* Push the saved tokens for the default argument onto the parser's
20481 lexer stack. */
20482 tokens = DEFARG_TOKENS (default_arg);
20483 cp_parser_push_lexer_for_tokens (parser, tokens);
20484
20485 start_lambda_scope (parmdecl);
20486
20487 /* Parse the assignment-expression. */
20488 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20489 if (parsed_arg == error_mark_node)
20490 {
20491 cp_parser_pop_lexer (parser);
20492 continue;
20493 }
20494
20495 if (!processing_template_decl)
20496 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20497
20498 TREE_PURPOSE (parm) = parsed_arg;
20499
20500 /* Update any instantiations we've already created. */
20501 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20502 VEC_iterate (tree, insts, ix, copy); ix++)
20503 TREE_PURPOSE (copy) = parsed_arg;
20504
20505 finish_lambda_scope ();
20506
20507 /* If the token stream has not been completely used up, then
20508 there was extra junk after the end of the default
20509 argument. */
20510 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20511 cp_parser_error (parser, "expected %<,%>");
20512
20513 /* Revert to the main lexer. */
20514 cp_parser_pop_lexer (parser);
20515 }
20516
20517 /* Make sure no default arg is missing. */
20518 check_default_args (fn);
20519
20520 /* Restore the state of local_variables_forbidden_p. */
20521 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20522
20523 /* Restore the queue. */
20524 pop_unparsed_function_queues (parser);
20525 }
20526
20527 /* Parse the operand of `sizeof' (or a similar operator). Returns
20528 either a TYPE or an expression, depending on the form of the
20529 input. The KEYWORD indicates which kind of expression we have
20530 encountered. */
20531
20532 static tree
20533 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20534 {
20535 tree expr = NULL_TREE;
20536 const char *saved_message;
20537 char *tmp;
20538 bool saved_integral_constant_expression_p;
20539 bool saved_non_integral_constant_expression_p;
20540 bool pack_expansion_p = false;
20541
20542 /* Types cannot be defined in a `sizeof' expression. Save away the
20543 old message. */
20544 saved_message = parser->type_definition_forbidden_message;
20545 /* And create the new one. */
20546 tmp = concat ("types may not be defined in %<",
20547 IDENTIFIER_POINTER (ridpointers[keyword]),
20548 "%> expressions", NULL);
20549 parser->type_definition_forbidden_message = tmp;
20550
20551 /* The restrictions on constant-expressions do not apply inside
20552 sizeof expressions. */
20553 saved_integral_constant_expression_p
20554 = parser->integral_constant_expression_p;
20555 saved_non_integral_constant_expression_p
20556 = parser->non_integral_constant_expression_p;
20557 parser->integral_constant_expression_p = false;
20558
20559 /* If it's a `...', then we are computing the length of a parameter
20560 pack. */
20561 if (keyword == RID_SIZEOF
20562 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20563 {
20564 /* Consume the `...'. */
20565 cp_lexer_consume_token (parser->lexer);
20566 maybe_warn_variadic_templates ();
20567
20568 /* Note that this is an expansion. */
20569 pack_expansion_p = true;
20570 }
20571
20572 /* Do not actually evaluate the expression. */
20573 ++cp_unevaluated_operand;
20574 ++c_inhibit_evaluation_warnings;
20575 /* If it's a `(', then we might be looking at the type-id
20576 construction. */
20577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20578 {
20579 tree type;
20580 bool saved_in_type_id_in_expr_p;
20581
20582 /* We can't be sure yet whether we're looking at a type-id or an
20583 expression. */
20584 cp_parser_parse_tentatively (parser);
20585 /* Consume the `('. */
20586 cp_lexer_consume_token (parser->lexer);
20587 /* Parse the type-id. */
20588 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20589 parser->in_type_id_in_expr_p = true;
20590 type = cp_parser_type_id (parser);
20591 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20592 /* Now, look for the trailing `)'. */
20593 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20594 /* If all went well, then we're done. */
20595 if (cp_parser_parse_definitely (parser))
20596 {
20597 cp_decl_specifier_seq decl_specs;
20598
20599 /* Build a trivial decl-specifier-seq. */
20600 clear_decl_specs (&decl_specs);
20601 decl_specs.type = type;
20602
20603 /* Call grokdeclarator to figure out what type this is. */
20604 expr = grokdeclarator (NULL,
20605 &decl_specs,
20606 TYPENAME,
20607 /*initialized=*/0,
20608 /*attrlist=*/NULL);
20609 }
20610 }
20611
20612 /* If the type-id production did not work out, then we must be
20613 looking at the unary-expression production. */
20614 if (!expr)
20615 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20616 /*cast_p=*/false, NULL);
20617
20618 if (pack_expansion_p)
20619 /* Build a pack expansion. */
20620 expr = make_pack_expansion (expr);
20621
20622 /* Go back to evaluating expressions. */
20623 --cp_unevaluated_operand;
20624 --c_inhibit_evaluation_warnings;
20625
20626 /* Free the message we created. */
20627 free (tmp);
20628 /* And restore the old one. */
20629 parser->type_definition_forbidden_message = saved_message;
20630 parser->integral_constant_expression_p
20631 = saved_integral_constant_expression_p;
20632 parser->non_integral_constant_expression_p
20633 = saved_non_integral_constant_expression_p;
20634
20635 return expr;
20636 }
20637
20638 /* If the current declaration has no declarator, return true. */
20639
20640 static bool
20641 cp_parser_declares_only_class_p (cp_parser *parser)
20642 {
20643 /* If the next token is a `;' or a `,' then there is no
20644 declarator. */
20645 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20646 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20647 }
20648
20649 /* Update the DECL_SPECS to reflect the storage class indicated by
20650 KEYWORD. */
20651
20652 static void
20653 cp_parser_set_storage_class (cp_parser *parser,
20654 cp_decl_specifier_seq *decl_specs,
20655 enum rid keyword,
20656 location_t location)
20657 {
20658 cp_storage_class storage_class;
20659
20660 if (parser->in_unbraced_linkage_specification_p)
20661 {
20662 error_at (location, "invalid use of %qD in linkage specification",
20663 ridpointers[keyword]);
20664 return;
20665 }
20666 else if (decl_specs->storage_class != sc_none)
20667 {
20668 decl_specs->conflicting_specifiers_p = true;
20669 return;
20670 }
20671
20672 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20673 && decl_specs->specs[(int) ds_thread])
20674 {
20675 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20676 decl_specs->specs[(int) ds_thread] = 0;
20677 }
20678
20679 switch (keyword)
20680 {
20681 case RID_AUTO:
20682 storage_class = sc_auto;
20683 break;
20684 case RID_REGISTER:
20685 storage_class = sc_register;
20686 break;
20687 case RID_STATIC:
20688 storage_class = sc_static;
20689 break;
20690 case RID_EXTERN:
20691 storage_class = sc_extern;
20692 break;
20693 case RID_MUTABLE:
20694 storage_class = sc_mutable;
20695 break;
20696 default:
20697 gcc_unreachable ();
20698 }
20699 decl_specs->storage_class = storage_class;
20700
20701 /* A storage class specifier cannot be applied alongside a typedef
20702 specifier. If there is a typedef specifier present then set
20703 conflicting_specifiers_p which will trigger an error later
20704 on in grokdeclarator. */
20705 if (decl_specs->specs[(int)ds_typedef])
20706 decl_specs->conflicting_specifiers_p = true;
20707 }
20708
20709 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20710 is true, the type is a user-defined type; otherwise it is a
20711 built-in type specified by a keyword. */
20712
20713 static void
20714 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20715 tree type_spec,
20716 location_t location,
20717 bool user_defined_p)
20718 {
20719 decl_specs->any_specifiers_p = true;
20720
20721 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20722 (with, for example, in "typedef int wchar_t;") we remember that
20723 this is what happened. In system headers, we ignore these
20724 declarations so that G++ can work with system headers that are not
20725 C++-safe. */
20726 if (decl_specs->specs[(int) ds_typedef]
20727 && !user_defined_p
20728 && (type_spec == boolean_type_node
20729 || type_spec == char16_type_node
20730 || type_spec == char32_type_node
20731 || type_spec == wchar_type_node)
20732 && (decl_specs->type
20733 || decl_specs->specs[(int) ds_long]
20734 || decl_specs->specs[(int) ds_short]
20735 || decl_specs->specs[(int) ds_unsigned]
20736 || decl_specs->specs[(int) ds_signed]))
20737 {
20738 decl_specs->redefined_builtin_type = type_spec;
20739 if (!decl_specs->type)
20740 {
20741 decl_specs->type = type_spec;
20742 decl_specs->user_defined_type_p = false;
20743 decl_specs->type_location = location;
20744 }
20745 }
20746 else if (decl_specs->type)
20747 decl_specs->multiple_types_p = true;
20748 else
20749 {
20750 decl_specs->type = type_spec;
20751 decl_specs->user_defined_type_p = user_defined_p;
20752 decl_specs->redefined_builtin_type = NULL_TREE;
20753 decl_specs->type_location = location;
20754 }
20755 }
20756
20757 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20758 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20759
20760 static bool
20761 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20762 {
20763 return decl_specifiers->specs[(int) ds_friend] != 0;
20764 }
20765
20766 /* Issue an error message indicating that TOKEN_DESC was expected.
20767 If KEYWORD is true, it indicated this function is called by
20768 cp_parser_require_keword and the required token can only be
20769 a indicated keyword. */
20770
20771 static void
20772 cp_parser_required_error (cp_parser *parser,
20773 required_token token_desc,
20774 bool keyword)
20775 {
20776 switch (token_desc)
20777 {
20778 case RT_NEW:
20779 cp_parser_error (parser, "expected %<new%>");
20780 return;
20781 case RT_DELETE:
20782 cp_parser_error (parser, "expected %<delete%>");
20783 return;
20784 case RT_RETURN:
20785 cp_parser_error (parser, "expected %<return%>");
20786 return;
20787 case RT_WHILE:
20788 cp_parser_error (parser, "expected %<while%>");
20789 return;
20790 case RT_EXTERN:
20791 cp_parser_error (parser, "expected %<extern%>");
20792 return;
20793 case RT_STATIC_ASSERT:
20794 cp_parser_error (parser, "expected %<static_assert%>");
20795 return;
20796 case RT_DECLTYPE:
20797 cp_parser_error (parser, "expected %<decltype%>");
20798 return;
20799 case RT_OPERATOR:
20800 cp_parser_error (parser, "expected %<operator%>");
20801 return;
20802 case RT_CLASS:
20803 cp_parser_error (parser, "expected %<class%>");
20804 return;
20805 case RT_TEMPLATE:
20806 cp_parser_error (parser, "expected %<template%>");
20807 return;
20808 case RT_NAMESPACE:
20809 cp_parser_error (parser, "expected %<namespace%>");
20810 return;
20811 case RT_USING:
20812 cp_parser_error (parser, "expected %<using%>");
20813 return;
20814 case RT_ASM:
20815 cp_parser_error (parser, "expected %<asm%>");
20816 return;
20817 case RT_TRY:
20818 cp_parser_error (parser, "expected %<try%>");
20819 return;
20820 case RT_CATCH:
20821 cp_parser_error (parser, "expected %<catch%>");
20822 return;
20823 case RT_THROW:
20824 cp_parser_error (parser, "expected %<throw%>");
20825 return;
20826 case RT_LABEL:
20827 cp_parser_error (parser, "expected %<__label__%>");
20828 return;
20829 case RT_AT_TRY:
20830 cp_parser_error (parser, "expected %<@try%>");
20831 return;
20832 case RT_AT_SYNCHRONIZED:
20833 cp_parser_error (parser, "expected %<@synchronized%>");
20834 return;
20835 case RT_AT_THROW:
20836 cp_parser_error (parser, "expected %<@throw%>");
20837 return;
20838 default:
20839 break;
20840 }
20841 if (!keyword)
20842 {
20843 switch (token_desc)
20844 {
20845 case RT_SEMICOLON:
20846 cp_parser_error (parser, "expected %<;%>");
20847 return;
20848 case RT_OPEN_PAREN:
20849 cp_parser_error (parser, "expected %<(%>");
20850 return;
20851 case RT_CLOSE_BRACE:
20852 cp_parser_error (parser, "expected %<}%>");
20853 return;
20854 case RT_OPEN_BRACE:
20855 cp_parser_error (parser, "expected %<{%>");
20856 return;
20857 case RT_CLOSE_SQUARE:
20858 cp_parser_error (parser, "expected %<]%>");
20859 return;
20860 case RT_OPEN_SQUARE:
20861 cp_parser_error (parser, "expected %<[%>");
20862 return;
20863 case RT_COMMA:
20864 cp_parser_error (parser, "expected %<,%>");
20865 return;
20866 case RT_SCOPE:
20867 cp_parser_error (parser, "expected %<::%>");
20868 return;
20869 case RT_LESS:
20870 cp_parser_error (parser, "expected %<<%>");
20871 return;
20872 case RT_GREATER:
20873 cp_parser_error (parser, "expected %<>%>");
20874 return;
20875 case RT_EQ:
20876 cp_parser_error (parser, "expected %<=%>");
20877 return;
20878 case RT_ELLIPSIS:
20879 cp_parser_error (parser, "expected %<...%>");
20880 return;
20881 case RT_MULT:
20882 cp_parser_error (parser, "expected %<*%>");
20883 return;
20884 case RT_COMPL:
20885 cp_parser_error (parser, "expected %<~%>");
20886 return;
20887 case RT_COLON:
20888 cp_parser_error (parser, "expected %<:%>");
20889 return;
20890 case RT_COLON_SCOPE:
20891 cp_parser_error (parser, "expected %<:%> or %<::%>");
20892 return;
20893 case RT_CLOSE_PAREN:
20894 cp_parser_error (parser, "expected %<)%>");
20895 return;
20896 case RT_COMMA_CLOSE_PAREN:
20897 cp_parser_error (parser, "expected %<,%> or %<)%>");
20898 return;
20899 case RT_PRAGMA_EOL:
20900 cp_parser_error (parser, "expected end of line");
20901 return;
20902 case RT_NAME:
20903 cp_parser_error (parser, "expected identifier");
20904 return;
20905 case RT_SELECT:
20906 cp_parser_error (parser, "expected selection-statement");
20907 return;
20908 case RT_INTERATION:
20909 cp_parser_error (parser, "expected iteration-statement");
20910 return;
20911 case RT_JUMP:
20912 cp_parser_error (parser, "expected jump-statement");
20913 return;
20914 case RT_CLASS_KEY:
20915 cp_parser_error (parser, "expected class-key");
20916 return;
20917 case RT_CLASS_TYPENAME_TEMPLATE:
20918 cp_parser_error (parser,
20919 "expected %<class%>, %<typename%>, or %<template%>");
20920 return;
20921 default:
20922 gcc_unreachable ();
20923 }
20924 }
20925 else
20926 gcc_unreachable ();
20927 }
20928
20929
20930
20931 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20932 issue an error message indicating that TOKEN_DESC was expected.
20933
20934 Returns the token consumed, if the token had the appropriate type.
20935 Otherwise, returns NULL. */
20936
20937 static cp_token *
20938 cp_parser_require (cp_parser* parser,
20939 enum cpp_ttype type,
20940 required_token token_desc)
20941 {
20942 if (cp_lexer_next_token_is (parser->lexer, type))
20943 return cp_lexer_consume_token (parser->lexer);
20944 else
20945 {
20946 /* Output the MESSAGE -- unless we're parsing tentatively. */
20947 if (!cp_parser_simulate_error (parser))
20948 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20949 return NULL;
20950 }
20951 }
20952
20953 /* An error message is produced if the next token is not '>'.
20954 All further tokens are skipped until the desired token is
20955 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20956
20957 static void
20958 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20959 {
20960 /* Current level of '< ... >'. */
20961 unsigned level = 0;
20962 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20963 unsigned nesting_depth = 0;
20964
20965 /* Are we ready, yet? If not, issue error message. */
20966 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20967 return;
20968
20969 /* Skip tokens until the desired token is found. */
20970 while (true)
20971 {
20972 /* Peek at the next token. */
20973 switch (cp_lexer_peek_token (parser->lexer)->type)
20974 {
20975 case CPP_LESS:
20976 if (!nesting_depth)
20977 ++level;
20978 break;
20979
20980 case CPP_RSHIFT:
20981 if (cxx_dialect == cxx98)
20982 /* C++0x views the `>>' operator as two `>' tokens, but
20983 C++98 does not. */
20984 break;
20985 else if (!nesting_depth && level-- == 0)
20986 {
20987 /* We've hit a `>>' where the first `>' closes the
20988 template argument list, and the second `>' is
20989 spurious. Just consume the `>>' and stop; we've
20990 already produced at least one error. */
20991 cp_lexer_consume_token (parser->lexer);
20992 return;
20993 }
20994 /* Fall through for C++0x, so we handle the second `>' in
20995 the `>>'. */
20996
20997 case CPP_GREATER:
20998 if (!nesting_depth && level-- == 0)
20999 {
21000 /* We've reached the token we want, consume it and stop. */
21001 cp_lexer_consume_token (parser->lexer);
21002 return;
21003 }
21004 break;
21005
21006 case CPP_OPEN_PAREN:
21007 case CPP_OPEN_SQUARE:
21008 ++nesting_depth;
21009 break;
21010
21011 case CPP_CLOSE_PAREN:
21012 case CPP_CLOSE_SQUARE:
21013 if (nesting_depth-- == 0)
21014 return;
21015 break;
21016
21017 case CPP_EOF:
21018 case CPP_PRAGMA_EOL:
21019 case CPP_SEMICOLON:
21020 case CPP_OPEN_BRACE:
21021 case CPP_CLOSE_BRACE:
21022 /* The '>' was probably forgotten, don't look further. */
21023 return;
21024
21025 default:
21026 break;
21027 }
21028
21029 /* Consume this token. */
21030 cp_lexer_consume_token (parser->lexer);
21031 }
21032 }
21033
21034 /* If the next token is the indicated keyword, consume it. Otherwise,
21035 issue an error message indicating that TOKEN_DESC was expected.
21036
21037 Returns the token consumed, if the token had the appropriate type.
21038 Otherwise, returns NULL. */
21039
21040 static cp_token *
21041 cp_parser_require_keyword (cp_parser* parser,
21042 enum rid keyword,
21043 required_token token_desc)
21044 {
21045 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21046
21047 if (token && token->keyword != keyword)
21048 {
21049 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
21050 return NULL;
21051 }
21052
21053 return token;
21054 }
21055
21056 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21057 function-definition. */
21058
21059 static bool
21060 cp_parser_token_starts_function_definition_p (cp_token* token)
21061 {
21062 return (/* An ordinary function-body begins with an `{'. */
21063 token->type == CPP_OPEN_BRACE
21064 /* A ctor-initializer begins with a `:'. */
21065 || token->type == CPP_COLON
21066 /* A function-try-block begins with `try'. */
21067 || token->keyword == RID_TRY
21068 /* The named return value extension begins with `return'. */
21069 || token->keyword == RID_RETURN);
21070 }
21071
21072 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21073 definition. */
21074
21075 static bool
21076 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21077 {
21078 cp_token *token;
21079
21080 token = cp_lexer_peek_token (parser->lexer);
21081 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21082 }
21083
21084 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21085 C++0x) ending a template-argument. */
21086
21087 static bool
21088 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21089 {
21090 cp_token *token;
21091
21092 token = cp_lexer_peek_token (parser->lexer);
21093 return (token->type == CPP_COMMA
21094 || token->type == CPP_GREATER
21095 || token->type == CPP_ELLIPSIS
21096 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21097 }
21098
21099 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21100 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
21101
21102 static bool
21103 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21104 size_t n)
21105 {
21106 cp_token *token;
21107
21108 token = cp_lexer_peek_nth_token (parser->lexer, n);
21109 if (token->type == CPP_LESS)
21110 return true;
21111 /* Check for the sequence `<::' in the original code. It would be lexed as
21112 `[:', where `[' is a digraph, and there is no whitespace before
21113 `:'. */
21114 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21115 {
21116 cp_token *token2;
21117 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21118 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21119 return true;
21120 }
21121 return false;
21122 }
21123
21124 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21125 or none_type otherwise. */
21126
21127 static enum tag_types
21128 cp_parser_token_is_class_key (cp_token* token)
21129 {
21130 switch (token->keyword)
21131 {
21132 case RID_CLASS:
21133 return class_type;
21134 case RID_STRUCT:
21135 return record_type;
21136 case RID_UNION:
21137 return union_type;
21138
21139 default:
21140 return none_type;
21141 }
21142 }
21143
21144 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
21145
21146 static void
21147 cp_parser_check_class_key (enum tag_types class_key, tree type)
21148 {
21149 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21150 permerror (input_location, "%qs tag used in naming %q#T",
21151 class_key == union_type ? "union"
21152 : class_key == record_type ? "struct" : "class",
21153 type);
21154 }
21155
21156 /* Issue an error message if DECL is redeclared with different
21157 access than its original declaration [class.access.spec/3].
21158 This applies to nested classes and nested class templates.
21159 [class.mem/1]. */
21160
21161 static void
21162 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21163 {
21164 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21165 return;
21166
21167 if ((TREE_PRIVATE (decl)
21168 != (current_access_specifier == access_private_node))
21169 || (TREE_PROTECTED (decl)
21170 != (current_access_specifier == access_protected_node)))
21171 error_at (location, "%qD redeclared with different access", decl);
21172 }
21173
21174 /* Look for the `template' keyword, as a syntactic disambiguator.
21175 Return TRUE iff it is present, in which case it will be
21176 consumed. */
21177
21178 static bool
21179 cp_parser_optional_template_keyword (cp_parser *parser)
21180 {
21181 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21182 {
21183 /* The `template' keyword can only be used within templates;
21184 outside templates the parser can always figure out what is a
21185 template and what is not. */
21186 if (!processing_template_decl)
21187 {
21188 cp_token *token = cp_lexer_peek_token (parser->lexer);
21189 error_at (token->location,
21190 "%<template%> (as a disambiguator) is only allowed "
21191 "within templates");
21192 /* If this part of the token stream is rescanned, the same
21193 error message would be generated. So, we purge the token
21194 from the stream. */
21195 cp_lexer_purge_token (parser->lexer);
21196 return false;
21197 }
21198 else
21199 {
21200 /* Consume the `template' keyword. */
21201 cp_lexer_consume_token (parser->lexer);
21202 return true;
21203 }
21204 }
21205
21206 return false;
21207 }
21208
21209 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21210 set PARSER->SCOPE, and perform other related actions. */
21211
21212 static void
21213 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21214 {
21215 int i;
21216 struct tree_check *check_value;
21217 deferred_access_check *chk;
21218 VEC (deferred_access_check,gc) *checks;
21219
21220 /* Get the stored value. */
21221 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21222 /* Perform any access checks that were deferred. */
21223 checks = check_value->checks;
21224 if (checks)
21225 {
21226 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21227 perform_or_defer_access_check (chk->binfo,
21228 chk->decl,
21229 chk->diag_decl);
21230 }
21231 /* Set the scope from the stored value. */
21232 parser->scope = check_value->value;
21233 parser->qualifying_scope = check_value->qualifying_scope;
21234 parser->object_scope = NULL_TREE;
21235 }
21236
21237 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21238 encounter the end of a block before what we were looking for. */
21239
21240 static bool
21241 cp_parser_cache_group (cp_parser *parser,
21242 enum cpp_ttype end,
21243 unsigned depth)
21244 {
21245 while (true)
21246 {
21247 cp_token *token = cp_lexer_peek_token (parser->lexer);
21248
21249 /* Abort a parenthesized expression if we encounter a semicolon. */
21250 if ((end == CPP_CLOSE_PAREN || depth == 0)
21251 && token->type == CPP_SEMICOLON)
21252 return true;
21253 /* If we've reached the end of the file, stop. */
21254 if (token->type == CPP_EOF
21255 || (end != CPP_PRAGMA_EOL
21256 && token->type == CPP_PRAGMA_EOL))
21257 return true;
21258 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21259 /* We've hit the end of an enclosing block, so there's been some
21260 kind of syntax error. */
21261 return true;
21262
21263 /* Consume the token. */
21264 cp_lexer_consume_token (parser->lexer);
21265 /* See if it starts a new group. */
21266 if (token->type == CPP_OPEN_BRACE)
21267 {
21268 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21269 /* In theory this should probably check end == '}', but
21270 cp_parser_save_member_function_body needs it to exit
21271 after either '}' or ')' when called with ')'. */
21272 if (depth == 0)
21273 return false;
21274 }
21275 else if (token->type == CPP_OPEN_PAREN)
21276 {
21277 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21278 if (depth == 0 && end == CPP_CLOSE_PAREN)
21279 return false;
21280 }
21281 else if (token->type == CPP_PRAGMA)
21282 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21283 else if (token->type == end)
21284 return false;
21285 }
21286 }
21287
21288 /* Begin parsing tentatively. We always save tokens while parsing
21289 tentatively so that if the tentative parsing fails we can restore the
21290 tokens. */
21291
21292 static void
21293 cp_parser_parse_tentatively (cp_parser* parser)
21294 {
21295 /* Enter a new parsing context. */
21296 parser->context = cp_parser_context_new (parser->context);
21297 /* Begin saving tokens. */
21298 cp_lexer_save_tokens (parser->lexer);
21299 /* In order to avoid repetitive access control error messages,
21300 access checks are queued up until we are no longer parsing
21301 tentatively. */
21302 push_deferring_access_checks (dk_deferred);
21303 }
21304
21305 /* Commit to the currently active tentative parse. */
21306
21307 static void
21308 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21309 {
21310 cp_parser_context *context;
21311 cp_lexer *lexer;
21312
21313 /* Mark all of the levels as committed. */
21314 lexer = parser->lexer;
21315 for (context = parser->context; context->next; context = context->next)
21316 {
21317 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21318 break;
21319 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21320 while (!cp_lexer_saving_tokens (lexer))
21321 lexer = lexer->next;
21322 cp_lexer_commit_tokens (lexer);
21323 }
21324 }
21325
21326 /* Abort the currently active tentative parse. All consumed tokens
21327 will be rolled back, and no diagnostics will be issued. */
21328
21329 static void
21330 cp_parser_abort_tentative_parse (cp_parser* parser)
21331 {
21332 cp_parser_simulate_error (parser);
21333 /* Now, pretend that we want to see if the construct was
21334 successfully parsed. */
21335 cp_parser_parse_definitely (parser);
21336 }
21337
21338 /* Stop parsing tentatively. If a parse error has occurred, restore the
21339 token stream. Otherwise, commit to the tokens we have consumed.
21340 Returns true if no error occurred; false otherwise. */
21341
21342 static bool
21343 cp_parser_parse_definitely (cp_parser* parser)
21344 {
21345 bool error_occurred;
21346 cp_parser_context *context;
21347
21348 /* Remember whether or not an error occurred, since we are about to
21349 destroy that information. */
21350 error_occurred = cp_parser_error_occurred (parser);
21351 /* Remove the topmost context from the stack. */
21352 context = parser->context;
21353 parser->context = context->next;
21354 /* If no parse errors occurred, commit to the tentative parse. */
21355 if (!error_occurred)
21356 {
21357 /* Commit to the tokens read tentatively, unless that was
21358 already done. */
21359 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21360 cp_lexer_commit_tokens (parser->lexer);
21361
21362 pop_to_parent_deferring_access_checks ();
21363 }
21364 /* Otherwise, if errors occurred, roll back our state so that things
21365 are just as they were before we began the tentative parse. */
21366 else
21367 {
21368 cp_lexer_rollback_tokens (parser->lexer);
21369 pop_deferring_access_checks ();
21370 }
21371 /* Add the context to the front of the free list. */
21372 context->next = cp_parser_context_free_list;
21373 cp_parser_context_free_list = context;
21374
21375 return !error_occurred;
21376 }
21377
21378 /* Returns true if we are parsing tentatively and are not committed to
21379 this tentative parse. */
21380
21381 static bool
21382 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21383 {
21384 return (cp_parser_parsing_tentatively (parser)
21385 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21386 }
21387
21388 /* Returns nonzero iff an error has occurred during the most recent
21389 tentative parse. */
21390
21391 static bool
21392 cp_parser_error_occurred (cp_parser* parser)
21393 {
21394 return (cp_parser_parsing_tentatively (parser)
21395 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21396 }
21397
21398 /* Returns nonzero if GNU extensions are allowed. */
21399
21400 static bool
21401 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21402 {
21403 return parser->allow_gnu_extensions_p;
21404 }
21405 \f
21406 /* Objective-C++ Productions */
21407
21408
21409 /* Parse an Objective-C expression, which feeds into a primary-expression
21410 above.
21411
21412 objc-expression:
21413 objc-message-expression
21414 objc-string-literal
21415 objc-encode-expression
21416 objc-protocol-expression
21417 objc-selector-expression
21418
21419 Returns a tree representation of the expression. */
21420
21421 static tree
21422 cp_parser_objc_expression (cp_parser* parser)
21423 {
21424 /* Try to figure out what kind of declaration is present. */
21425 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21426
21427 switch (kwd->type)
21428 {
21429 case CPP_OPEN_SQUARE:
21430 return cp_parser_objc_message_expression (parser);
21431
21432 case CPP_OBJC_STRING:
21433 kwd = cp_lexer_consume_token (parser->lexer);
21434 return objc_build_string_object (kwd->u.value);
21435
21436 case CPP_KEYWORD:
21437 switch (kwd->keyword)
21438 {
21439 case RID_AT_ENCODE:
21440 return cp_parser_objc_encode_expression (parser);
21441
21442 case RID_AT_PROTOCOL:
21443 return cp_parser_objc_protocol_expression (parser);
21444
21445 case RID_AT_SELECTOR:
21446 return cp_parser_objc_selector_expression (parser);
21447
21448 default:
21449 break;
21450 }
21451 default:
21452 error_at (kwd->location,
21453 "misplaced %<@%D%> Objective-C++ construct",
21454 kwd->u.value);
21455 cp_parser_skip_to_end_of_block_or_statement (parser);
21456 }
21457
21458 return error_mark_node;
21459 }
21460
21461 /* Parse an Objective-C message expression.
21462
21463 objc-message-expression:
21464 [ objc-message-receiver objc-message-args ]
21465
21466 Returns a representation of an Objective-C message. */
21467
21468 static tree
21469 cp_parser_objc_message_expression (cp_parser* parser)
21470 {
21471 tree receiver, messageargs;
21472
21473 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21474 receiver = cp_parser_objc_message_receiver (parser);
21475 messageargs = cp_parser_objc_message_args (parser);
21476 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21477
21478 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21479 }
21480
21481 /* Parse an objc-message-receiver.
21482
21483 objc-message-receiver:
21484 expression
21485 simple-type-specifier
21486
21487 Returns a representation of the type or expression. */
21488
21489 static tree
21490 cp_parser_objc_message_receiver (cp_parser* parser)
21491 {
21492 tree rcv;
21493
21494 /* An Objective-C message receiver may be either (1) a type
21495 or (2) an expression. */
21496 cp_parser_parse_tentatively (parser);
21497 rcv = cp_parser_expression (parser, false, NULL);
21498
21499 if (cp_parser_parse_definitely (parser))
21500 return rcv;
21501
21502 rcv = cp_parser_simple_type_specifier (parser,
21503 /*decl_specs=*/NULL,
21504 CP_PARSER_FLAGS_NONE);
21505
21506 return objc_get_class_reference (rcv);
21507 }
21508
21509 /* Parse the arguments and selectors comprising an Objective-C message.
21510
21511 objc-message-args:
21512 objc-selector
21513 objc-selector-args
21514 objc-selector-args , objc-comma-args
21515
21516 objc-selector-args:
21517 objc-selector [opt] : assignment-expression
21518 objc-selector-args objc-selector [opt] : assignment-expression
21519
21520 objc-comma-args:
21521 assignment-expression
21522 objc-comma-args , assignment-expression
21523
21524 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21525 selector arguments and TREE_VALUE containing a list of comma
21526 arguments. */
21527
21528 static tree
21529 cp_parser_objc_message_args (cp_parser* parser)
21530 {
21531 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21532 bool maybe_unary_selector_p = true;
21533 cp_token *token = cp_lexer_peek_token (parser->lexer);
21534
21535 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21536 {
21537 tree selector = NULL_TREE, arg;
21538
21539 if (token->type != CPP_COLON)
21540 selector = cp_parser_objc_selector (parser);
21541
21542 /* Detect if we have a unary selector. */
21543 if (maybe_unary_selector_p
21544 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21545 return build_tree_list (selector, NULL_TREE);
21546
21547 maybe_unary_selector_p = false;
21548 cp_parser_require (parser, CPP_COLON, RT_COLON);
21549 arg = cp_parser_assignment_expression (parser, false, NULL);
21550
21551 sel_args
21552 = chainon (sel_args,
21553 build_tree_list (selector, arg));
21554
21555 token = cp_lexer_peek_token (parser->lexer);
21556 }
21557
21558 /* Handle non-selector arguments, if any. */
21559 while (token->type == CPP_COMMA)
21560 {
21561 tree arg;
21562
21563 cp_lexer_consume_token (parser->lexer);
21564 arg = cp_parser_assignment_expression (parser, false, NULL);
21565
21566 addl_args
21567 = chainon (addl_args,
21568 build_tree_list (NULL_TREE, arg));
21569
21570 token = cp_lexer_peek_token (parser->lexer);
21571 }
21572
21573 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21574 {
21575 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21576 return build_tree_list (error_mark_node, error_mark_node);
21577 }
21578
21579 return build_tree_list (sel_args, addl_args);
21580 }
21581
21582 /* Parse an Objective-C encode expression.
21583
21584 objc-encode-expression:
21585 @encode objc-typename
21586
21587 Returns an encoded representation of the type argument. */
21588
21589 static tree
21590 cp_parser_objc_encode_expression (cp_parser* parser)
21591 {
21592 tree type;
21593 cp_token *token;
21594
21595 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21596 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21597 token = cp_lexer_peek_token (parser->lexer);
21598 type = complete_type (cp_parser_type_id (parser));
21599 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21600
21601 if (!type)
21602 {
21603 error_at (token->location,
21604 "%<@encode%> must specify a type as an argument");
21605 return error_mark_node;
21606 }
21607
21608 /* This happens if we find @encode(T) (where T is a template
21609 typename or something dependent on a template typename) when
21610 parsing a template. In that case, we can't compile it
21611 immediately, but we rather create an AT_ENCODE_EXPR which will
21612 need to be instantiated when the template is used.
21613 */
21614 if (dependent_type_p (type))
21615 {
21616 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21617 TREE_READONLY (value) = 1;
21618 return value;
21619 }
21620
21621 return objc_build_encode_expr (type);
21622 }
21623
21624 /* Parse an Objective-C @defs expression. */
21625
21626 static tree
21627 cp_parser_objc_defs_expression (cp_parser *parser)
21628 {
21629 tree name;
21630
21631 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21632 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21633 name = cp_parser_identifier (parser);
21634 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21635
21636 return objc_get_class_ivars (name);
21637 }
21638
21639 /* Parse an Objective-C protocol expression.
21640
21641 objc-protocol-expression:
21642 @protocol ( identifier )
21643
21644 Returns a representation of the protocol expression. */
21645
21646 static tree
21647 cp_parser_objc_protocol_expression (cp_parser* parser)
21648 {
21649 tree proto;
21650
21651 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21652 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21653 proto = cp_parser_identifier (parser);
21654 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21655
21656 return objc_build_protocol_expr (proto);
21657 }
21658
21659 /* Parse an Objective-C selector expression.
21660
21661 objc-selector-expression:
21662 @selector ( objc-method-signature )
21663
21664 objc-method-signature:
21665 objc-selector
21666 objc-selector-seq
21667
21668 objc-selector-seq:
21669 objc-selector :
21670 objc-selector-seq objc-selector :
21671
21672 Returns a representation of the method selector. */
21673
21674 static tree
21675 cp_parser_objc_selector_expression (cp_parser* parser)
21676 {
21677 tree sel_seq = NULL_TREE;
21678 bool maybe_unary_selector_p = true;
21679 cp_token *token;
21680 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21681
21682 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21683 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21684 token = cp_lexer_peek_token (parser->lexer);
21685
21686 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21687 || token->type == CPP_SCOPE)
21688 {
21689 tree selector = NULL_TREE;
21690
21691 if (token->type != CPP_COLON
21692 || token->type == CPP_SCOPE)
21693 selector = cp_parser_objc_selector (parser);
21694
21695 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21696 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21697 {
21698 /* Detect if we have a unary selector. */
21699 if (maybe_unary_selector_p)
21700 {
21701 sel_seq = selector;
21702 goto finish_selector;
21703 }
21704 else
21705 {
21706 cp_parser_error (parser, "expected %<:%>");
21707 }
21708 }
21709 maybe_unary_selector_p = false;
21710 token = cp_lexer_consume_token (parser->lexer);
21711
21712 if (token->type == CPP_SCOPE)
21713 {
21714 sel_seq
21715 = chainon (sel_seq,
21716 build_tree_list (selector, NULL_TREE));
21717 sel_seq
21718 = chainon (sel_seq,
21719 build_tree_list (NULL_TREE, NULL_TREE));
21720 }
21721 else
21722 sel_seq
21723 = chainon (sel_seq,
21724 build_tree_list (selector, NULL_TREE));
21725
21726 token = cp_lexer_peek_token (parser->lexer);
21727 }
21728
21729 finish_selector:
21730 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21731
21732 return objc_build_selector_expr (loc, sel_seq);
21733 }
21734
21735 /* Parse a list of identifiers.
21736
21737 objc-identifier-list:
21738 identifier
21739 objc-identifier-list , identifier
21740
21741 Returns a TREE_LIST of identifier nodes. */
21742
21743 static tree
21744 cp_parser_objc_identifier_list (cp_parser* parser)
21745 {
21746 tree identifier;
21747 tree list;
21748 cp_token *sep;
21749
21750 identifier = cp_parser_identifier (parser);
21751 if (identifier == error_mark_node)
21752 return error_mark_node;
21753
21754 list = build_tree_list (NULL_TREE, identifier);
21755 sep = cp_lexer_peek_token (parser->lexer);
21756
21757 while (sep->type == CPP_COMMA)
21758 {
21759 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21760 identifier = cp_parser_identifier (parser);
21761 if (identifier == error_mark_node)
21762 return list;
21763
21764 list = chainon (list, build_tree_list (NULL_TREE,
21765 identifier));
21766 sep = cp_lexer_peek_token (parser->lexer);
21767 }
21768
21769 return list;
21770 }
21771
21772 /* Parse an Objective-C alias declaration.
21773
21774 objc-alias-declaration:
21775 @compatibility_alias identifier identifier ;
21776
21777 This function registers the alias mapping with the Objective-C front end.
21778 It returns nothing. */
21779
21780 static void
21781 cp_parser_objc_alias_declaration (cp_parser* parser)
21782 {
21783 tree alias, orig;
21784
21785 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21786 alias = cp_parser_identifier (parser);
21787 orig = cp_parser_identifier (parser);
21788 objc_declare_alias (alias, orig);
21789 cp_parser_consume_semicolon_at_end_of_statement (parser);
21790 }
21791
21792 /* Parse an Objective-C class forward-declaration.
21793
21794 objc-class-declaration:
21795 @class objc-identifier-list ;
21796
21797 The function registers the forward declarations with the Objective-C
21798 front end. It returns nothing. */
21799
21800 static void
21801 cp_parser_objc_class_declaration (cp_parser* parser)
21802 {
21803 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21804 objc_declare_class (cp_parser_objc_identifier_list (parser));
21805 cp_parser_consume_semicolon_at_end_of_statement (parser);
21806 }
21807
21808 /* Parse a list of Objective-C protocol references.
21809
21810 objc-protocol-refs-opt:
21811 objc-protocol-refs [opt]
21812
21813 objc-protocol-refs:
21814 < objc-identifier-list >
21815
21816 Returns a TREE_LIST of identifiers, if any. */
21817
21818 static tree
21819 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21820 {
21821 tree protorefs = NULL_TREE;
21822
21823 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21824 {
21825 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21826 protorefs = cp_parser_objc_identifier_list (parser);
21827 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21828 }
21829
21830 return protorefs;
21831 }
21832
21833 /* Parse a Objective-C visibility specification. */
21834
21835 static void
21836 cp_parser_objc_visibility_spec (cp_parser* parser)
21837 {
21838 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21839
21840 switch (vis->keyword)
21841 {
21842 case RID_AT_PRIVATE:
21843 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21844 break;
21845 case RID_AT_PROTECTED:
21846 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21847 break;
21848 case RID_AT_PUBLIC:
21849 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21850 break;
21851 case RID_AT_PACKAGE:
21852 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21853 break;
21854 default:
21855 return;
21856 }
21857
21858 /* Eat '@private'/'@protected'/'@public'. */
21859 cp_lexer_consume_token (parser->lexer);
21860 }
21861
21862 /* Parse an Objective-C method type. Return 'true' if it is a class
21863 (+) method, and 'false' if it is an instance (-) method. */
21864
21865 static inline bool
21866 cp_parser_objc_method_type (cp_parser* parser)
21867 {
21868 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21869 return true;
21870 else
21871 return false;
21872 }
21873
21874 /* Parse an Objective-C protocol qualifier. */
21875
21876 static tree
21877 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21878 {
21879 tree quals = NULL_TREE, node;
21880 cp_token *token = cp_lexer_peek_token (parser->lexer);
21881
21882 node = token->u.value;
21883
21884 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21885 && (node == ridpointers [(int) RID_IN]
21886 || node == ridpointers [(int) RID_OUT]
21887 || node == ridpointers [(int) RID_INOUT]
21888 || node == ridpointers [(int) RID_BYCOPY]
21889 || node == ridpointers [(int) RID_BYREF]
21890 || node == ridpointers [(int) RID_ONEWAY]))
21891 {
21892 quals = tree_cons (NULL_TREE, node, quals);
21893 cp_lexer_consume_token (parser->lexer);
21894 token = cp_lexer_peek_token (parser->lexer);
21895 node = token->u.value;
21896 }
21897
21898 return quals;
21899 }
21900
21901 /* Parse an Objective-C typename. */
21902
21903 static tree
21904 cp_parser_objc_typename (cp_parser* parser)
21905 {
21906 tree type_name = NULL_TREE;
21907
21908 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21909 {
21910 tree proto_quals, cp_type = NULL_TREE;
21911
21912 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21913 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21914
21915 /* An ObjC type name may consist of just protocol qualifiers, in which
21916 case the type shall default to 'id'. */
21917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21918 {
21919 cp_type = cp_parser_type_id (parser);
21920
21921 /* If the type could not be parsed, an error has already
21922 been produced. For error recovery, behave as if it had
21923 not been specified, which will use the default type
21924 'id'. */
21925 if (cp_type == error_mark_node)
21926 {
21927 cp_type = NULL_TREE;
21928 /* We need to skip to the closing parenthesis as
21929 cp_parser_type_id() does not seem to do it for
21930 us. */
21931 cp_parser_skip_to_closing_parenthesis (parser,
21932 /*recovering=*/true,
21933 /*or_comma=*/false,
21934 /*consume_paren=*/false);
21935 }
21936 }
21937
21938 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21939 type_name = build_tree_list (proto_quals, cp_type);
21940 }
21941
21942 return type_name;
21943 }
21944
21945 /* Check to see if TYPE refers to an Objective-C selector name. */
21946
21947 static bool
21948 cp_parser_objc_selector_p (enum cpp_ttype type)
21949 {
21950 return (type == CPP_NAME || type == CPP_KEYWORD
21951 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21952 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21953 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21954 || type == CPP_XOR || type == CPP_XOR_EQ);
21955 }
21956
21957 /* Parse an Objective-C selector. */
21958
21959 static tree
21960 cp_parser_objc_selector (cp_parser* parser)
21961 {
21962 cp_token *token = cp_lexer_consume_token (parser->lexer);
21963
21964 if (!cp_parser_objc_selector_p (token->type))
21965 {
21966 error_at (token->location, "invalid Objective-C++ selector name");
21967 return error_mark_node;
21968 }
21969
21970 /* C++ operator names are allowed to appear in ObjC selectors. */
21971 switch (token->type)
21972 {
21973 case CPP_AND_AND: return get_identifier ("and");
21974 case CPP_AND_EQ: return get_identifier ("and_eq");
21975 case CPP_AND: return get_identifier ("bitand");
21976 case CPP_OR: return get_identifier ("bitor");
21977 case CPP_COMPL: return get_identifier ("compl");
21978 case CPP_NOT: return get_identifier ("not");
21979 case CPP_NOT_EQ: return get_identifier ("not_eq");
21980 case CPP_OR_OR: return get_identifier ("or");
21981 case CPP_OR_EQ: return get_identifier ("or_eq");
21982 case CPP_XOR: return get_identifier ("xor");
21983 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21984 default: return token->u.value;
21985 }
21986 }
21987
21988 /* Parse an Objective-C params list. */
21989
21990 static tree
21991 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21992 {
21993 tree params = NULL_TREE;
21994 bool maybe_unary_selector_p = true;
21995 cp_token *token = cp_lexer_peek_token (parser->lexer);
21996
21997 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21998 {
21999 tree selector = NULL_TREE, type_name, identifier;
22000 tree parm_attr = NULL_TREE;
22001
22002 if (token->keyword == RID_ATTRIBUTE)
22003 break;
22004
22005 if (token->type != CPP_COLON)
22006 selector = cp_parser_objc_selector (parser);
22007
22008 /* Detect if we have a unary selector. */
22009 if (maybe_unary_selector_p
22010 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22011 {
22012 params = selector; /* Might be followed by attributes. */
22013 break;
22014 }
22015
22016 maybe_unary_selector_p = false;
22017 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22018 {
22019 /* Something went quite wrong. There should be a colon
22020 here, but there is not. Stop parsing parameters. */
22021 break;
22022 }
22023 type_name = cp_parser_objc_typename (parser);
22024 /* New ObjC allows attributes on parameters too. */
22025 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22026 parm_attr = cp_parser_attributes_opt (parser);
22027 identifier = cp_parser_identifier (parser);
22028
22029 params
22030 = chainon (params,
22031 objc_build_keyword_decl (selector,
22032 type_name,
22033 identifier,
22034 parm_attr));
22035
22036 token = cp_lexer_peek_token (parser->lexer);
22037 }
22038
22039 if (params == NULL_TREE)
22040 {
22041 cp_parser_error (parser, "objective-c++ method declaration is expected");
22042 return error_mark_node;
22043 }
22044
22045 /* We allow tail attributes for the method. */
22046 if (token->keyword == RID_ATTRIBUTE)
22047 {
22048 *attributes = cp_parser_attributes_opt (parser);
22049 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22050 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22051 return params;
22052 cp_parser_error (parser,
22053 "method attributes must be specified at the end");
22054 return error_mark_node;
22055 }
22056
22057 if (params == NULL_TREE)
22058 {
22059 cp_parser_error (parser, "objective-c++ method declaration is expected");
22060 return error_mark_node;
22061 }
22062 return params;
22063 }
22064
22065 /* Parse the non-keyword Objective-C params. */
22066
22067 static tree
22068 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
22069 tree* attributes)
22070 {
22071 tree params = make_node (TREE_LIST);
22072 cp_token *token = cp_lexer_peek_token (parser->lexer);
22073 *ellipsisp = false; /* Initially, assume no ellipsis. */
22074
22075 while (token->type == CPP_COMMA)
22076 {
22077 cp_parameter_declarator *parmdecl;
22078 tree parm;
22079
22080 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22081 token = cp_lexer_peek_token (parser->lexer);
22082
22083 if (token->type == CPP_ELLIPSIS)
22084 {
22085 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
22086 *ellipsisp = true;
22087 token = cp_lexer_peek_token (parser->lexer);
22088 break;
22089 }
22090
22091 /* TODO: parse attributes for tail parameters. */
22092 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22093 parm = grokdeclarator (parmdecl->declarator,
22094 &parmdecl->decl_specifiers,
22095 PARM, /*initialized=*/0,
22096 /*attrlist=*/NULL);
22097
22098 chainon (params, build_tree_list (NULL_TREE, parm));
22099 token = cp_lexer_peek_token (parser->lexer);
22100 }
22101
22102 /* We allow tail attributes for the method. */
22103 if (token->keyword == RID_ATTRIBUTE)
22104 {
22105 if (*attributes == NULL_TREE)
22106 {
22107 *attributes = cp_parser_attributes_opt (parser);
22108 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22109 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22110 return params;
22111 }
22112 else
22113 /* We have an error, but parse the attributes, so that we can
22114 carry on. */
22115 *attributes = cp_parser_attributes_opt (parser);
22116
22117 cp_parser_error (parser,
22118 "method attributes must be specified at the end");
22119 return error_mark_node;
22120 }
22121
22122 return params;
22123 }
22124
22125 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
22126
22127 static void
22128 cp_parser_objc_interstitial_code (cp_parser* parser)
22129 {
22130 cp_token *token = cp_lexer_peek_token (parser->lexer);
22131
22132 /* If the next token is `extern' and the following token is a string
22133 literal, then we have a linkage specification. */
22134 if (token->keyword == RID_EXTERN
22135 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22136 cp_parser_linkage_specification (parser);
22137 /* Handle #pragma, if any. */
22138 else if (token->type == CPP_PRAGMA)
22139 cp_parser_pragma (parser, pragma_external);
22140 /* Allow stray semicolons. */
22141 else if (token->type == CPP_SEMICOLON)
22142 cp_lexer_consume_token (parser->lexer);
22143 /* Mark methods as optional or required, when building protocols. */
22144 else if (token->keyword == RID_AT_OPTIONAL)
22145 {
22146 cp_lexer_consume_token (parser->lexer);
22147 objc_set_method_opt (true);
22148 }
22149 else if (token->keyword == RID_AT_REQUIRED)
22150 {
22151 cp_lexer_consume_token (parser->lexer);
22152 objc_set_method_opt (false);
22153 }
22154 else if (token->keyword == RID_NAMESPACE)
22155 cp_parser_namespace_definition (parser);
22156 /* Other stray characters must generate errors. */
22157 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22158 {
22159 cp_lexer_consume_token (parser->lexer);
22160 error ("stray %qs between Objective-C++ methods",
22161 token->type == CPP_OPEN_BRACE ? "{" : "}");
22162 }
22163 /* Finally, try to parse a block-declaration, or a function-definition. */
22164 else
22165 cp_parser_block_declaration (parser, /*statement_p=*/false);
22166 }
22167
22168 /* Parse a method signature. */
22169
22170 static tree
22171 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22172 {
22173 tree rettype, kwdparms, optparms;
22174 bool ellipsis = false;
22175 bool is_class_method;
22176
22177 is_class_method = cp_parser_objc_method_type (parser);
22178 rettype = cp_parser_objc_typename (parser);
22179 *attributes = NULL_TREE;
22180 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22181 if (kwdparms == error_mark_node)
22182 return error_mark_node;
22183 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22184 if (optparms == error_mark_node)
22185 return error_mark_node;
22186
22187 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22188 }
22189
22190 static bool
22191 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22192 {
22193 tree tattr;
22194 cp_lexer_save_tokens (parser->lexer);
22195 tattr = cp_parser_attributes_opt (parser);
22196 gcc_assert (tattr) ;
22197
22198 /* If the attributes are followed by a method introducer, this is not allowed.
22199 Dump the attributes and flag the situation. */
22200 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22201 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22202 return true;
22203
22204 /* Otherwise, the attributes introduce some interstitial code, possibly so
22205 rewind to allow that check. */
22206 cp_lexer_rollback_tokens (parser->lexer);
22207 return false;
22208 }
22209
22210 /* Parse an Objective-C method prototype list. */
22211
22212 static void
22213 cp_parser_objc_method_prototype_list (cp_parser* parser)
22214 {
22215 cp_token *token = cp_lexer_peek_token (parser->lexer);
22216
22217 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22218 {
22219 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22220 {
22221 tree attributes, sig;
22222 bool is_class_method;
22223 if (token->type == CPP_PLUS)
22224 is_class_method = true;
22225 else
22226 is_class_method = false;
22227 sig = cp_parser_objc_method_signature (parser, &attributes);
22228 if (sig == error_mark_node)
22229 {
22230 cp_parser_skip_to_end_of_block_or_statement (parser);
22231 token = cp_lexer_peek_token (parser->lexer);
22232 continue;
22233 }
22234 objc_add_method_declaration (is_class_method, sig, attributes);
22235 cp_parser_consume_semicolon_at_end_of_statement (parser);
22236 }
22237 else if (token->keyword == RID_AT_PROPERTY)
22238 cp_parser_objc_at_property_declaration (parser);
22239 else if (token->keyword == RID_ATTRIBUTE
22240 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22241 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22242 OPT_Wattributes,
22243 "prefix attributes are ignored for methods");
22244 else
22245 /* Allow for interspersed non-ObjC++ code. */
22246 cp_parser_objc_interstitial_code (parser);
22247
22248 token = cp_lexer_peek_token (parser->lexer);
22249 }
22250
22251 if (token->type != CPP_EOF)
22252 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22253 else
22254 cp_parser_error (parser, "expected %<@end%>");
22255
22256 objc_finish_interface ();
22257 }
22258
22259 /* Parse an Objective-C method definition list. */
22260
22261 static void
22262 cp_parser_objc_method_definition_list (cp_parser* parser)
22263 {
22264 cp_token *token = cp_lexer_peek_token (parser->lexer);
22265
22266 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22267 {
22268 tree meth;
22269
22270 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22271 {
22272 cp_token *ptk;
22273 tree sig, attribute;
22274 bool is_class_method;
22275 if (token->type == CPP_PLUS)
22276 is_class_method = true;
22277 else
22278 is_class_method = false;
22279 push_deferring_access_checks (dk_deferred);
22280 sig = cp_parser_objc_method_signature (parser, &attribute);
22281 if (sig == error_mark_node)
22282 {
22283 cp_parser_skip_to_end_of_block_or_statement (parser);
22284 token = cp_lexer_peek_token (parser->lexer);
22285 continue;
22286 }
22287 objc_start_method_definition (is_class_method, sig, attribute);
22288
22289 /* For historical reasons, we accept an optional semicolon. */
22290 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22291 cp_lexer_consume_token (parser->lexer);
22292
22293 ptk = cp_lexer_peek_token (parser->lexer);
22294 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22295 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22296 {
22297 perform_deferred_access_checks ();
22298 stop_deferring_access_checks ();
22299 meth = cp_parser_function_definition_after_declarator (parser,
22300 false);
22301 pop_deferring_access_checks ();
22302 objc_finish_method_definition (meth);
22303 }
22304 }
22305 /* The following case will be removed once @synthesize is
22306 completely implemented. */
22307 else if (token->keyword == RID_AT_PROPERTY)
22308 cp_parser_objc_at_property_declaration (parser);
22309 else if (token->keyword == RID_AT_SYNTHESIZE)
22310 cp_parser_objc_at_synthesize_declaration (parser);
22311 else if (token->keyword == RID_AT_DYNAMIC)
22312 cp_parser_objc_at_dynamic_declaration (parser);
22313 else if (token->keyword == RID_ATTRIBUTE
22314 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22315 warning_at (token->location, OPT_Wattributes,
22316 "prefix attributes are ignored for methods");
22317 else
22318 /* Allow for interspersed non-ObjC++ code. */
22319 cp_parser_objc_interstitial_code (parser);
22320
22321 token = cp_lexer_peek_token (parser->lexer);
22322 }
22323
22324 if (token->type != CPP_EOF)
22325 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22326 else
22327 cp_parser_error (parser, "expected %<@end%>");
22328
22329 objc_finish_implementation ();
22330 }
22331
22332 /* Parse Objective-C ivars. */
22333
22334 static void
22335 cp_parser_objc_class_ivars (cp_parser* parser)
22336 {
22337 cp_token *token = cp_lexer_peek_token (parser->lexer);
22338
22339 if (token->type != CPP_OPEN_BRACE)
22340 return; /* No ivars specified. */
22341
22342 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22343 token = cp_lexer_peek_token (parser->lexer);
22344
22345 while (token->type != CPP_CLOSE_BRACE
22346 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22347 {
22348 cp_decl_specifier_seq declspecs;
22349 int decl_class_or_enum_p;
22350 tree prefix_attributes;
22351
22352 cp_parser_objc_visibility_spec (parser);
22353
22354 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22355 break;
22356
22357 cp_parser_decl_specifier_seq (parser,
22358 CP_PARSER_FLAGS_OPTIONAL,
22359 &declspecs,
22360 &decl_class_or_enum_p);
22361
22362 /* auto, register, static, extern, mutable. */
22363 if (declspecs.storage_class != sc_none)
22364 {
22365 cp_parser_error (parser, "invalid type for instance variable");
22366 declspecs.storage_class = sc_none;
22367 }
22368
22369 /* __thread. */
22370 if (declspecs.specs[(int) ds_thread])
22371 {
22372 cp_parser_error (parser, "invalid type for instance variable");
22373 declspecs.specs[(int) ds_thread] = 0;
22374 }
22375
22376 /* typedef. */
22377 if (declspecs.specs[(int) ds_typedef])
22378 {
22379 cp_parser_error (parser, "invalid type for instance variable");
22380 declspecs.specs[(int) ds_typedef] = 0;
22381 }
22382
22383 prefix_attributes = declspecs.attributes;
22384 declspecs.attributes = NULL_TREE;
22385
22386 /* Keep going until we hit the `;' at the end of the
22387 declaration. */
22388 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22389 {
22390 tree width = NULL_TREE, attributes, first_attribute, decl;
22391 cp_declarator *declarator = NULL;
22392 int ctor_dtor_or_conv_p;
22393
22394 /* Check for a (possibly unnamed) bitfield declaration. */
22395 token = cp_lexer_peek_token (parser->lexer);
22396 if (token->type == CPP_COLON)
22397 goto eat_colon;
22398
22399 if (token->type == CPP_NAME
22400 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22401 == CPP_COLON))
22402 {
22403 /* Get the name of the bitfield. */
22404 declarator = make_id_declarator (NULL_TREE,
22405 cp_parser_identifier (parser),
22406 sfk_none);
22407
22408 eat_colon:
22409 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22410 /* Get the width of the bitfield. */
22411 width
22412 = cp_parser_constant_expression (parser,
22413 /*allow_non_constant=*/false,
22414 NULL);
22415 }
22416 else
22417 {
22418 /* Parse the declarator. */
22419 declarator
22420 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22421 &ctor_dtor_or_conv_p,
22422 /*parenthesized_p=*/NULL,
22423 /*member_p=*/false);
22424 }
22425
22426 /* Look for attributes that apply to the ivar. */
22427 attributes = cp_parser_attributes_opt (parser);
22428 /* Remember which attributes are prefix attributes and
22429 which are not. */
22430 first_attribute = attributes;
22431 /* Combine the attributes. */
22432 attributes = chainon (prefix_attributes, attributes);
22433
22434 if (width)
22435 /* Create the bitfield declaration. */
22436 decl = grokbitfield (declarator, &declspecs,
22437 width,
22438 attributes);
22439 else
22440 decl = grokfield (declarator, &declspecs,
22441 NULL_TREE, /*init_const_expr_p=*/false,
22442 NULL_TREE, attributes);
22443
22444 /* Add the instance variable. */
22445 objc_add_instance_variable (decl);
22446
22447 /* Reset PREFIX_ATTRIBUTES. */
22448 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22449 attributes = TREE_CHAIN (attributes);
22450 if (attributes)
22451 TREE_CHAIN (attributes) = NULL_TREE;
22452
22453 token = cp_lexer_peek_token (parser->lexer);
22454
22455 if (token->type == CPP_COMMA)
22456 {
22457 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22458 continue;
22459 }
22460 break;
22461 }
22462
22463 cp_parser_consume_semicolon_at_end_of_statement (parser);
22464 token = cp_lexer_peek_token (parser->lexer);
22465 }
22466
22467 if (token->keyword == RID_AT_END)
22468 cp_parser_error (parser, "expected %<}%>");
22469
22470 /* Do not consume the RID_AT_END, so it will be read again as terminating
22471 the @interface of @implementation. */
22472 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22473 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22474
22475 /* For historical reasons, we accept an optional semicolon. */
22476 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22477 cp_lexer_consume_token (parser->lexer);
22478 }
22479
22480 /* Parse an Objective-C protocol declaration. */
22481
22482 static void
22483 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22484 {
22485 tree proto, protorefs;
22486 cp_token *tok;
22487
22488 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22490 {
22491 tok = cp_lexer_peek_token (parser->lexer);
22492 error_at (tok->location, "identifier expected after %<@protocol%>");
22493 goto finish;
22494 }
22495
22496 /* See if we have a forward declaration or a definition. */
22497 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22498
22499 /* Try a forward declaration first. */
22500 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22501 {
22502 objc_declare_protocols (cp_parser_objc_identifier_list (parser),
22503 attributes);
22504 finish:
22505 cp_parser_consume_semicolon_at_end_of_statement (parser);
22506 }
22507
22508 /* Ok, we got a full-fledged definition (or at least should). */
22509 else
22510 {
22511 proto = cp_parser_identifier (parser);
22512 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22513 objc_start_protocol (proto, protorefs, attributes);
22514 cp_parser_objc_method_prototype_list (parser);
22515 }
22516 }
22517
22518 /* Parse an Objective-C superclass or category. */
22519
22520 static void
22521 cp_parser_objc_superclass_or_category (cp_parser *parser,
22522 bool iface_p,
22523 tree *super,
22524 tree *categ, bool *is_class_extension)
22525 {
22526 cp_token *next = cp_lexer_peek_token (parser->lexer);
22527
22528 *super = *categ = NULL_TREE;
22529 *is_class_extension = false;
22530 if (next->type == CPP_COLON)
22531 {
22532 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22533 *super = cp_parser_identifier (parser);
22534 }
22535 else if (next->type == CPP_OPEN_PAREN)
22536 {
22537 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22538
22539 /* If there is no category name, and this is an @interface, we
22540 have a class extension. */
22541 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22542 {
22543 *categ = NULL_TREE;
22544 *is_class_extension = true;
22545 }
22546 else
22547 *categ = cp_parser_identifier (parser);
22548
22549 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22550 }
22551 }
22552
22553 /* Parse an Objective-C class interface. */
22554
22555 static void
22556 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22557 {
22558 tree name, super, categ, protos;
22559 bool is_class_extension;
22560
22561 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22562 name = cp_parser_identifier (parser);
22563 if (name == error_mark_node)
22564 {
22565 /* It's hard to recover because even if valid @interface stuff
22566 is to follow, we can't compile it (or validate it) if we
22567 don't even know which class it refers to. Let's assume this
22568 was a stray '@interface' token in the stream and skip it.
22569 */
22570 return;
22571 }
22572 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22573 &is_class_extension);
22574 protos = cp_parser_objc_protocol_refs_opt (parser);
22575
22576 /* We have either a class or a category on our hands. */
22577 if (categ || is_class_extension)
22578 objc_start_category_interface (name, categ, protos, attributes);
22579 else
22580 {
22581 objc_start_class_interface (name, super, protos, attributes);
22582 /* Handle instance variable declarations, if any. */
22583 cp_parser_objc_class_ivars (parser);
22584 objc_continue_interface ();
22585 }
22586
22587 cp_parser_objc_method_prototype_list (parser);
22588 }
22589
22590 /* Parse an Objective-C class implementation. */
22591
22592 static void
22593 cp_parser_objc_class_implementation (cp_parser* parser)
22594 {
22595 tree name, super, categ;
22596 bool is_class_extension;
22597
22598 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22599 name = cp_parser_identifier (parser);
22600 if (name == error_mark_node)
22601 {
22602 /* It's hard to recover because even if valid @implementation
22603 stuff is to follow, we can't compile it (or validate it) if
22604 we don't even know which class it refers to. Let's assume
22605 this was a stray '@implementation' token in the stream and
22606 skip it.
22607 */
22608 return;
22609 }
22610 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22611 &is_class_extension);
22612
22613 /* We have either a class or a category on our hands. */
22614 if (categ)
22615 objc_start_category_implementation (name, categ);
22616 else
22617 {
22618 objc_start_class_implementation (name, super);
22619 /* Handle instance variable declarations, if any. */
22620 cp_parser_objc_class_ivars (parser);
22621 objc_continue_implementation ();
22622 }
22623
22624 cp_parser_objc_method_definition_list (parser);
22625 }
22626
22627 /* Consume the @end token and finish off the implementation. */
22628
22629 static void
22630 cp_parser_objc_end_implementation (cp_parser* parser)
22631 {
22632 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22633 objc_finish_implementation ();
22634 }
22635
22636 /* Parse an Objective-C declaration. */
22637
22638 static void
22639 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22640 {
22641 /* Try to figure out what kind of declaration is present. */
22642 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22643
22644 if (attributes)
22645 switch (kwd->keyword)
22646 {
22647 case RID_AT_ALIAS:
22648 case RID_AT_CLASS:
22649 case RID_AT_END:
22650 error_at (kwd->location, "attributes may not be specified before"
22651 " the %<@%D%> Objective-C++ keyword",
22652 kwd->u.value);
22653 attributes = NULL;
22654 break;
22655 case RID_AT_IMPLEMENTATION:
22656 warning_at (kwd->location, OPT_Wattributes,
22657 "prefix attributes are ignored before %<@%D%>",
22658 kwd->u.value);
22659 attributes = NULL;
22660 default:
22661 break;
22662 }
22663
22664 switch (kwd->keyword)
22665 {
22666 case RID_AT_ALIAS:
22667 cp_parser_objc_alias_declaration (parser);
22668 break;
22669 case RID_AT_CLASS:
22670 cp_parser_objc_class_declaration (parser);
22671 break;
22672 case RID_AT_PROTOCOL:
22673 cp_parser_objc_protocol_declaration (parser, attributes);
22674 break;
22675 case RID_AT_INTERFACE:
22676 cp_parser_objc_class_interface (parser, attributes);
22677 break;
22678 case RID_AT_IMPLEMENTATION:
22679 cp_parser_objc_class_implementation (parser);
22680 break;
22681 case RID_AT_END:
22682 cp_parser_objc_end_implementation (parser);
22683 break;
22684 default:
22685 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22686 kwd->u.value);
22687 cp_parser_skip_to_end_of_block_or_statement (parser);
22688 }
22689 }
22690
22691 /* Parse an Objective-C try-catch-finally statement.
22692
22693 objc-try-catch-finally-stmt:
22694 @try compound-statement objc-catch-clause-seq [opt]
22695 objc-finally-clause [opt]
22696
22697 objc-catch-clause-seq:
22698 objc-catch-clause objc-catch-clause-seq [opt]
22699
22700 objc-catch-clause:
22701 @catch ( objc-exception-declaration ) compound-statement
22702
22703 objc-finally-clause:
22704 @finally compound-statement
22705
22706 objc-exception-declaration:
22707 parameter-declaration
22708 '...'
22709
22710 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22711
22712 Returns NULL_TREE.
22713
22714 PS: This function is identical to c_parser_objc_try_catch_finally_statement
22715 for C. Keep them in sync. */
22716
22717 static tree
22718 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22719 {
22720 location_t location;
22721 tree stmt;
22722
22723 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22724 location = cp_lexer_peek_token (parser->lexer)->location;
22725 objc_maybe_warn_exceptions (location);
22726 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22727 node, lest it get absorbed into the surrounding block. */
22728 stmt = push_stmt_list ();
22729 cp_parser_compound_statement (parser, NULL, false);
22730 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22731
22732 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22733 {
22734 cp_parameter_declarator *parm;
22735 tree parameter_declaration = error_mark_node;
22736 bool seen_open_paren = false;
22737
22738 cp_lexer_consume_token (parser->lexer);
22739 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22740 seen_open_paren = true;
22741 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22742 {
22743 /* We have "@catch (...)" (where the '...' are literally
22744 what is in the code). Skip the '...'.
22745 parameter_declaration is set to NULL_TREE, and
22746 objc_being_catch_clauses() knows that that means
22747 '...'. */
22748 cp_lexer_consume_token (parser->lexer);
22749 parameter_declaration = NULL_TREE;
22750 }
22751 else
22752 {
22753 /* We have "@catch (NSException *exception)" or something
22754 like that. Parse the parameter declaration. */
22755 parm = cp_parser_parameter_declaration (parser, false, NULL);
22756 if (parm == NULL)
22757 parameter_declaration = error_mark_node;
22758 else
22759 parameter_declaration = grokdeclarator (parm->declarator,
22760 &parm->decl_specifiers,
22761 PARM, /*initialized=*/0,
22762 /*attrlist=*/NULL);
22763 }
22764 if (seen_open_paren)
22765 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22766 else
22767 {
22768 /* If there was no open parenthesis, we are recovering from
22769 an error, and we are trying to figure out what mistake
22770 the user has made. */
22771
22772 /* If there is an immediate closing parenthesis, the user
22773 probably forgot the opening one (ie, they typed "@catch
22774 NSException *e)". Parse the closing parenthesis and keep
22775 going. */
22776 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22777 cp_lexer_consume_token (parser->lexer);
22778
22779 /* If these is no immediate closing parenthesis, the user
22780 probably doesn't know that parenthesis are required at
22781 all (ie, they typed "@catch NSException *e"). So, just
22782 forget about the closing parenthesis and keep going. */
22783 }
22784 objc_begin_catch_clause (parameter_declaration);
22785 cp_parser_compound_statement (parser, NULL, false);
22786 objc_finish_catch_clause ();
22787 }
22788 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22789 {
22790 cp_lexer_consume_token (parser->lexer);
22791 location = cp_lexer_peek_token (parser->lexer)->location;
22792 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22793 node, lest it get absorbed into the surrounding block. */
22794 stmt = push_stmt_list ();
22795 cp_parser_compound_statement (parser, NULL, false);
22796 objc_build_finally_clause (location, pop_stmt_list (stmt));
22797 }
22798
22799 return objc_finish_try_stmt ();
22800 }
22801
22802 /* Parse an Objective-C synchronized statement.
22803
22804 objc-synchronized-stmt:
22805 @synchronized ( expression ) compound-statement
22806
22807 Returns NULL_TREE. */
22808
22809 static tree
22810 cp_parser_objc_synchronized_statement (cp_parser *parser)
22811 {
22812 location_t location;
22813 tree lock, stmt;
22814
22815 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22816
22817 location = cp_lexer_peek_token (parser->lexer)->location;
22818 objc_maybe_warn_exceptions (location);
22819 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22820 lock = cp_parser_expression (parser, false, NULL);
22821 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22822
22823 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22824 node, lest it get absorbed into the surrounding block. */
22825 stmt = push_stmt_list ();
22826 cp_parser_compound_statement (parser, NULL, false);
22827
22828 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22829 }
22830
22831 /* Parse an Objective-C throw statement.
22832
22833 objc-throw-stmt:
22834 @throw assignment-expression [opt] ;
22835
22836 Returns a constructed '@throw' statement. */
22837
22838 static tree
22839 cp_parser_objc_throw_statement (cp_parser *parser)
22840 {
22841 tree expr = NULL_TREE;
22842 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22843
22844 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22845
22846 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22847 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22848
22849 cp_parser_consume_semicolon_at_end_of_statement (parser);
22850
22851 return objc_build_throw_stmt (loc, expr);
22852 }
22853
22854 /* Parse an Objective-C statement. */
22855
22856 static tree
22857 cp_parser_objc_statement (cp_parser * parser)
22858 {
22859 /* Try to figure out what kind of declaration is present. */
22860 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22861
22862 switch (kwd->keyword)
22863 {
22864 case RID_AT_TRY:
22865 return cp_parser_objc_try_catch_finally_statement (parser);
22866 case RID_AT_SYNCHRONIZED:
22867 return cp_parser_objc_synchronized_statement (parser);
22868 case RID_AT_THROW:
22869 return cp_parser_objc_throw_statement (parser);
22870 default:
22871 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22872 kwd->u.value);
22873 cp_parser_skip_to_end_of_block_or_statement (parser);
22874 }
22875
22876 return error_mark_node;
22877 }
22878
22879 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22880 look ahead to see if an objc keyword follows the attributes. This
22881 is to detect the use of prefix attributes on ObjC @interface and
22882 @protocol. */
22883
22884 static bool
22885 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22886 {
22887 cp_lexer_save_tokens (parser->lexer);
22888 *attrib = cp_parser_attributes_opt (parser);
22889 gcc_assert (*attrib);
22890 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22891 {
22892 cp_lexer_commit_tokens (parser->lexer);
22893 return true;
22894 }
22895 cp_lexer_rollback_tokens (parser->lexer);
22896 return false;
22897 }
22898
22899 /* This routine is a minimal replacement for
22900 c_parser_struct_declaration () used when parsing the list of
22901 types/names or ObjC++ properties. For example, when parsing the
22902 code
22903
22904 @property (readonly) int a, b, c;
22905
22906 this function is responsible for parsing "int a, int b, int c" and
22907 returning the declarations as CHAIN of DECLs.
22908
22909 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22910 similar parsing. */
22911 static tree
22912 cp_parser_objc_struct_declaration (cp_parser *parser)
22913 {
22914 tree decls = NULL_TREE;
22915 cp_decl_specifier_seq declspecs;
22916 int decl_class_or_enum_p;
22917 tree prefix_attributes;
22918
22919 cp_parser_decl_specifier_seq (parser,
22920 CP_PARSER_FLAGS_NONE,
22921 &declspecs,
22922 &decl_class_or_enum_p);
22923
22924 if (declspecs.type == error_mark_node)
22925 return error_mark_node;
22926
22927 /* auto, register, static, extern, mutable. */
22928 if (declspecs.storage_class != sc_none)
22929 {
22930 cp_parser_error (parser, "invalid type for property");
22931 declspecs.storage_class = sc_none;
22932 }
22933
22934 /* __thread. */
22935 if (declspecs.specs[(int) ds_thread])
22936 {
22937 cp_parser_error (parser, "invalid type for property");
22938 declspecs.specs[(int) ds_thread] = 0;
22939 }
22940
22941 /* typedef. */
22942 if (declspecs.specs[(int) ds_typedef])
22943 {
22944 cp_parser_error (parser, "invalid type for property");
22945 declspecs.specs[(int) ds_typedef] = 0;
22946 }
22947
22948 prefix_attributes = declspecs.attributes;
22949 declspecs.attributes = NULL_TREE;
22950
22951 /* Keep going until we hit the `;' at the end of the declaration. */
22952 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22953 {
22954 tree attributes, first_attribute, decl;
22955 cp_declarator *declarator;
22956 cp_token *token;
22957
22958 /* Parse the declarator. */
22959 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22960 NULL, NULL, false);
22961
22962 /* Look for attributes that apply to the ivar. */
22963 attributes = cp_parser_attributes_opt (parser);
22964 /* Remember which attributes are prefix attributes and
22965 which are not. */
22966 first_attribute = attributes;
22967 /* Combine the attributes. */
22968 attributes = chainon (prefix_attributes, attributes);
22969
22970 decl = grokfield (declarator, &declspecs,
22971 NULL_TREE, /*init_const_expr_p=*/false,
22972 NULL_TREE, attributes);
22973
22974 if (decl == error_mark_node || decl == NULL_TREE)
22975 return error_mark_node;
22976
22977 /* Reset PREFIX_ATTRIBUTES. */
22978 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22979 attributes = TREE_CHAIN (attributes);
22980 if (attributes)
22981 TREE_CHAIN (attributes) = NULL_TREE;
22982
22983 DECL_CHAIN (decl) = decls;
22984 decls = decl;
22985
22986 token = cp_lexer_peek_token (parser->lexer);
22987 if (token->type == CPP_COMMA)
22988 {
22989 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22990 continue;
22991 }
22992 else
22993 break;
22994 }
22995 return decls;
22996 }
22997
22998 /* Parse an Objective-C @property declaration. The syntax is:
22999
23000 objc-property-declaration:
23001 '@property' objc-property-attributes[opt] struct-declaration ;
23002
23003 objc-property-attributes:
23004 '(' objc-property-attribute-list ')'
23005
23006 objc-property-attribute-list:
23007 objc-property-attribute
23008 objc-property-attribute-list, objc-property-attribute
23009
23010 objc-property-attribute
23011 'getter' = identifier
23012 'setter' = identifier
23013 'readonly'
23014 'readwrite'
23015 'assign'
23016 'retain'
23017 'copy'
23018 'nonatomic'
23019
23020 For example:
23021 @property NSString *name;
23022 @property (readonly) id object;
23023 @property (retain, nonatomic, getter=getTheName) id name;
23024 @property int a, b, c;
23025
23026 PS: This function is identical to
23027 c_parser_objc_at_property_declaration for C. Keep them in sync. */
23028 static void
23029 cp_parser_objc_at_property_declaration (cp_parser *parser)
23030 {
23031 /* The following variables hold the attributes of the properties as
23032 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
23033 seen. When we see an attribute, we set them to 'true' (if they
23034 are boolean properties) or to the identifier (if they have an
23035 argument, ie, for getter and setter). Note that here we only
23036 parse the list of attributes, check the syntax and accumulate the
23037 attributes that we find. objc_add_property_declaration() will
23038 then process the information. */
23039 bool property_assign = false;
23040 bool property_copy = false;
23041 tree property_getter_ident = NULL_TREE;
23042 bool property_nonatomic = false;
23043 bool property_readonly = false;
23044 bool property_readwrite = false;
23045 bool property_retain = false;
23046 tree property_setter_ident = NULL_TREE;
23047
23048 /* 'properties' is the list of properties that we read. Usually a
23049 single one, but maybe more (eg, in "@property int a, b, c;" there
23050 are three). */
23051 tree properties;
23052 location_t loc;
23053
23054 loc = cp_lexer_peek_token (parser->lexer)->location;
23055
23056 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
23057
23058 /* Parse the optional attribute list... */
23059 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23060 {
23061 /* Eat the '('. */
23062 cp_lexer_consume_token (parser->lexer);
23063
23064 while (true)
23065 {
23066 bool syntax_error = false;
23067 cp_token *token = cp_lexer_peek_token (parser->lexer);
23068 enum rid keyword;
23069
23070 if (token->type != CPP_NAME)
23071 {
23072 cp_parser_error (parser, "expected identifier");
23073 break;
23074 }
23075 keyword = C_RID_CODE (token->u.value);
23076 cp_lexer_consume_token (parser->lexer);
23077 switch (keyword)
23078 {
23079 case RID_ASSIGN: property_assign = true; break;
23080 case RID_COPY: property_copy = true; break;
23081 case RID_NONATOMIC: property_nonatomic = true; break;
23082 case RID_READONLY: property_readonly = true; break;
23083 case RID_READWRITE: property_readwrite = true; break;
23084 case RID_RETAIN: property_retain = true; break;
23085
23086 case RID_GETTER:
23087 case RID_SETTER:
23088 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23089 {
23090 if (keyword == RID_GETTER)
23091 cp_parser_error (parser,
23092 "missing %<=%> (after %<getter%> attribute)");
23093 else
23094 cp_parser_error (parser,
23095 "missing %<=%> (after %<setter%> attribute)");
23096 syntax_error = true;
23097 break;
23098 }
23099 cp_lexer_consume_token (parser->lexer); /* eat the = */
23100 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23101 {
23102 cp_parser_error (parser, "expected identifier");
23103 syntax_error = true;
23104 break;
23105 }
23106 if (keyword == RID_SETTER)
23107 {
23108 if (property_setter_ident != NULL_TREE)
23109 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23110 else
23111 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23112 cp_lexer_consume_token (parser->lexer);
23113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23114 cp_parser_error (parser, "setter name must terminate with %<:%>");
23115 else
23116 cp_lexer_consume_token (parser->lexer);
23117 }
23118 else
23119 {
23120 if (property_getter_ident != NULL_TREE)
23121 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23122 else
23123 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23124 cp_lexer_consume_token (parser->lexer);
23125 }
23126 break;
23127 default:
23128 cp_parser_error (parser, "unknown property attribute");
23129 syntax_error = true;
23130 break;
23131 }
23132
23133 if (syntax_error)
23134 break;
23135
23136 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23137 cp_lexer_consume_token (parser->lexer);
23138 else
23139 break;
23140 }
23141
23142 /* FIXME: "@property (setter, assign);" will generate a spurious
23143 "error: expected ‘)’ before ‘,’ token". This is because
23144 cp_parser_require, unlike the C counterpart, will produce an
23145 error even if we are in error recovery. */
23146 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23147 {
23148 cp_parser_skip_to_closing_parenthesis (parser,
23149 /*recovering=*/true,
23150 /*or_comma=*/false,
23151 /*consume_paren=*/true);
23152 }
23153 }
23154
23155 /* ... and the property declaration(s). */
23156 properties = cp_parser_objc_struct_declaration (parser);
23157
23158 if (properties == error_mark_node)
23159 {
23160 cp_parser_skip_to_end_of_statement (parser);
23161 /* If the next token is now a `;', consume it. */
23162 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23163 cp_lexer_consume_token (parser->lexer);
23164 return;
23165 }
23166
23167 if (properties == NULL_TREE)
23168 cp_parser_error (parser, "expected identifier");
23169 else
23170 {
23171 /* Comma-separated properties are chained together in
23172 reverse order; add them one by one. */
23173 properties = nreverse (properties);
23174
23175 for (; properties; properties = TREE_CHAIN (properties))
23176 objc_add_property_declaration (loc, copy_node (properties),
23177 property_readonly, property_readwrite,
23178 property_assign, property_retain,
23179 property_copy, property_nonatomic,
23180 property_getter_ident, property_setter_ident);
23181 }
23182
23183 cp_parser_consume_semicolon_at_end_of_statement (parser);
23184 }
23185
23186 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
23187
23188 objc-synthesize-declaration:
23189 @synthesize objc-synthesize-identifier-list ;
23190
23191 objc-synthesize-identifier-list:
23192 objc-synthesize-identifier
23193 objc-synthesize-identifier-list, objc-synthesize-identifier
23194
23195 objc-synthesize-identifier
23196 identifier
23197 identifier = identifier
23198
23199 For example:
23200 @synthesize MyProperty;
23201 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23202
23203 PS: This function is identical to c_parser_objc_at_synthesize_declaration
23204 for C. Keep them in sync.
23205 */
23206 static void
23207 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23208 {
23209 tree list = NULL_TREE;
23210 location_t loc;
23211 loc = cp_lexer_peek_token (parser->lexer)->location;
23212
23213 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
23214 while (true)
23215 {
23216 tree property, ivar;
23217 property = cp_parser_identifier (parser);
23218 if (property == error_mark_node)
23219 {
23220 cp_parser_consume_semicolon_at_end_of_statement (parser);
23221 return;
23222 }
23223 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23224 {
23225 cp_lexer_consume_token (parser->lexer);
23226 ivar = cp_parser_identifier (parser);
23227 if (ivar == error_mark_node)
23228 {
23229 cp_parser_consume_semicolon_at_end_of_statement (parser);
23230 return;
23231 }
23232 }
23233 else
23234 ivar = NULL_TREE;
23235 list = chainon (list, build_tree_list (ivar, property));
23236 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23237 cp_lexer_consume_token (parser->lexer);
23238 else
23239 break;
23240 }
23241 cp_parser_consume_semicolon_at_end_of_statement (parser);
23242 objc_add_synthesize_declaration (loc, list);
23243 }
23244
23245 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
23246
23247 objc-dynamic-declaration:
23248 @dynamic identifier-list ;
23249
23250 For example:
23251 @dynamic MyProperty;
23252 @dynamic MyProperty, AnotherProperty;
23253
23254 PS: This function is identical to c_parser_objc_at_dynamic_declaration
23255 for C. Keep them in sync.
23256 */
23257 static void
23258 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23259 {
23260 tree list = NULL_TREE;
23261 location_t loc;
23262 loc = cp_lexer_peek_token (parser->lexer)->location;
23263
23264 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23265 while (true)
23266 {
23267 tree property;
23268 property = cp_parser_identifier (parser);
23269 if (property == error_mark_node)
23270 {
23271 cp_parser_consume_semicolon_at_end_of_statement (parser);
23272 return;
23273 }
23274 list = chainon (list, build_tree_list (NULL, property));
23275 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23276 cp_lexer_consume_token (parser->lexer);
23277 else
23278 break;
23279 }
23280 cp_parser_consume_semicolon_at_end_of_statement (parser);
23281 objc_add_dynamic_declaration (loc, list);
23282 }
23283
23284 \f
23285 /* OpenMP 2.5 parsing routines. */
23286
23287 /* Returns name of the next clause.
23288 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23289 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23290 returned and the token is consumed. */
23291
23292 static pragma_omp_clause
23293 cp_parser_omp_clause_name (cp_parser *parser)
23294 {
23295 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23296
23297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23298 result = PRAGMA_OMP_CLAUSE_IF;
23299 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23300 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23301 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23302 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23303 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23304 {
23305 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23306 const char *p = IDENTIFIER_POINTER (id);
23307
23308 switch (p[0])
23309 {
23310 case 'c':
23311 if (!strcmp ("collapse", p))
23312 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23313 else if (!strcmp ("copyin", p))
23314 result = PRAGMA_OMP_CLAUSE_COPYIN;
23315 else if (!strcmp ("copyprivate", p))
23316 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23317 break;
23318 case 'f':
23319 if (!strcmp ("firstprivate", p))
23320 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23321 break;
23322 case 'l':
23323 if (!strcmp ("lastprivate", p))
23324 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23325 break;
23326 case 'n':
23327 if (!strcmp ("nowait", p))
23328 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23329 else if (!strcmp ("num_threads", p))
23330 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23331 break;
23332 case 'o':
23333 if (!strcmp ("ordered", p))
23334 result = PRAGMA_OMP_CLAUSE_ORDERED;
23335 break;
23336 case 'r':
23337 if (!strcmp ("reduction", p))
23338 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23339 break;
23340 case 's':
23341 if (!strcmp ("schedule", p))
23342 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23343 else if (!strcmp ("shared", p))
23344 result = PRAGMA_OMP_CLAUSE_SHARED;
23345 break;
23346 case 'u':
23347 if (!strcmp ("untied", p))
23348 result = PRAGMA_OMP_CLAUSE_UNTIED;
23349 break;
23350 }
23351 }
23352
23353 if (result != PRAGMA_OMP_CLAUSE_NONE)
23354 cp_lexer_consume_token (parser->lexer);
23355
23356 return result;
23357 }
23358
23359 /* Validate that a clause of the given type does not already exist. */
23360
23361 static void
23362 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23363 const char *name, location_t location)
23364 {
23365 tree c;
23366
23367 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23368 if (OMP_CLAUSE_CODE (c) == code)
23369 {
23370 error_at (location, "too many %qs clauses", name);
23371 break;
23372 }
23373 }
23374
23375 /* OpenMP 2.5:
23376 variable-list:
23377 identifier
23378 variable-list , identifier
23379
23380 In addition, we match a closing parenthesis. An opening parenthesis
23381 will have been consumed by the caller.
23382
23383 If KIND is nonzero, create the appropriate node and install the decl
23384 in OMP_CLAUSE_DECL and add the node to the head of the list.
23385
23386 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23387 return the list created. */
23388
23389 static tree
23390 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23391 tree list)
23392 {
23393 cp_token *token;
23394 while (1)
23395 {
23396 tree name, decl;
23397
23398 token = cp_lexer_peek_token (parser->lexer);
23399 name = cp_parser_id_expression (parser, /*template_p=*/false,
23400 /*check_dependency_p=*/true,
23401 /*template_p=*/NULL,
23402 /*declarator_p=*/false,
23403 /*optional_p=*/false);
23404 if (name == error_mark_node)
23405 goto skip_comma;
23406
23407 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23408 if (decl == error_mark_node)
23409 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23410 token->location);
23411 else if (kind != 0)
23412 {
23413 tree u = build_omp_clause (token->location, kind);
23414 OMP_CLAUSE_DECL (u) = decl;
23415 OMP_CLAUSE_CHAIN (u) = list;
23416 list = u;
23417 }
23418 else
23419 list = tree_cons (decl, NULL_TREE, list);
23420
23421 get_comma:
23422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23423 break;
23424 cp_lexer_consume_token (parser->lexer);
23425 }
23426
23427 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23428 {
23429 int ending;
23430
23431 /* Try to resync to an unnested comma. Copied from
23432 cp_parser_parenthesized_expression_list. */
23433 skip_comma:
23434 ending = cp_parser_skip_to_closing_parenthesis (parser,
23435 /*recovering=*/true,
23436 /*or_comma=*/true,
23437 /*consume_paren=*/true);
23438 if (ending < 0)
23439 goto get_comma;
23440 }
23441
23442 return list;
23443 }
23444
23445 /* Similarly, but expect leading and trailing parenthesis. This is a very
23446 common case for omp clauses. */
23447
23448 static tree
23449 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23450 {
23451 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23452 return cp_parser_omp_var_list_no_open (parser, kind, list);
23453 return list;
23454 }
23455
23456 /* OpenMP 3.0:
23457 collapse ( constant-expression ) */
23458
23459 static tree
23460 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23461 {
23462 tree c, num;
23463 location_t loc;
23464 HOST_WIDE_INT n;
23465
23466 loc = cp_lexer_peek_token (parser->lexer)->location;
23467 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23468 return list;
23469
23470 num = cp_parser_constant_expression (parser, false, NULL);
23471
23472 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23474 /*or_comma=*/false,
23475 /*consume_paren=*/true);
23476
23477 if (num == error_mark_node)
23478 return list;
23479 num = fold_non_dependent_expr (num);
23480 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23481 || !host_integerp (num, 0)
23482 || (n = tree_low_cst (num, 0)) <= 0
23483 || (int) n != n)
23484 {
23485 error_at (loc, "collapse argument needs positive constant integer expression");
23486 return list;
23487 }
23488
23489 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23490 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23491 OMP_CLAUSE_CHAIN (c) = list;
23492 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23493
23494 return c;
23495 }
23496
23497 /* OpenMP 2.5:
23498 default ( shared | none ) */
23499
23500 static tree
23501 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23502 {
23503 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23504 tree c;
23505
23506 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23507 return list;
23508 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23509 {
23510 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23511 const char *p = IDENTIFIER_POINTER (id);
23512
23513 switch (p[0])
23514 {
23515 case 'n':
23516 if (strcmp ("none", p) != 0)
23517 goto invalid_kind;
23518 kind = OMP_CLAUSE_DEFAULT_NONE;
23519 break;
23520
23521 case 's':
23522 if (strcmp ("shared", p) != 0)
23523 goto invalid_kind;
23524 kind = OMP_CLAUSE_DEFAULT_SHARED;
23525 break;
23526
23527 default:
23528 goto invalid_kind;
23529 }
23530
23531 cp_lexer_consume_token (parser->lexer);
23532 }
23533 else
23534 {
23535 invalid_kind:
23536 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23537 }
23538
23539 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23540 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23541 /*or_comma=*/false,
23542 /*consume_paren=*/true);
23543
23544 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23545 return list;
23546
23547 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23548 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23549 OMP_CLAUSE_CHAIN (c) = list;
23550 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23551
23552 return c;
23553 }
23554
23555 /* OpenMP 2.5:
23556 if ( expression ) */
23557
23558 static tree
23559 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23560 {
23561 tree t, c;
23562
23563 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23564 return list;
23565
23566 t = cp_parser_condition (parser);
23567
23568 if (t == error_mark_node
23569 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23570 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23571 /*or_comma=*/false,
23572 /*consume_paren=*/true);
23573
23574 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23575
23576 c = build_omp_clause (location, OMP_CLAUSE_IF);
23577 OMP_CLAUSE_IF_EXPR (c) = t;
23578 OMP_CLAUSE_CHAIN (c) = list;
23579
23580 return c;
23581 }
23582
23583 /* OpenMP 2.5:
23584 nowait */
23585
23586 static tree
23587 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23588 tree list, location_t location)
23589 {
23590 tree c;
23591
23592 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23593
23594 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23595 OMP_CLAUSE_CHAIN (c) = list;
23596 return c;
23597 }
23598
23599 /* OpenMP 2.5:
23600 num_threads ( expression ) */
23601
23602 static tree
23603 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23604 location_t location)
23605 {
23606 tree t, c;
23607
23608 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23609 return list;
23610
23611 t = cp_parser_expression (parser, false, NULL);
23612
23613 if (t == error_mark_node
23614 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23615 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23616 /*or_comma=*/false,
23617 /*consume_paren=*/true);
23618
23619 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23620 "num_threads", location);
23621
23622 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23623 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23624 OMP_CLAUSE_CHAIN (c) = list;
23625
23626 return c;
23627 }
23628
23629 /* OpenMP 2.5:
23630 ordered */
23631
23632 static tree
23633 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23634 tree list, location_t location)
23635 {
23636 tree c;
23637
23638 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23639 "ordered", location);
23640
23641 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23642 OMP_CLAUSE_CHAIN (c) = list;
23643 return c;
23644 }
23645
23646 /* OpenMP 2.5:
23647 reduction ( reduction-operator : variable-list )
23648
23649 reduction-operator:
23650 One of: + * - & ^ | && || */
23651
23652 static tree
23653 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23654 {
23655 enum tree_code code;
23656 tree nlist, c;
23657
23658 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23659 return list;
23660
23661 switch (cp_lexer_peek_token (parser->lexer)->type)
23662 {
23663 case CPP_PLUS:
23664 code = PLUS_EXPR;
23665 break;
23666 case CPP_MULT:
23667 code = MULT_EXPR;
23668 break;
23669 case CPP_MINUS:
23670 code = MINUS_EXPR;
23671 break;
23672 case CPP_AND:
23673 code = BIT_AND_EXPR;
23674 break;
23675 case CPP_XOR:
23676 code = BIT_XOR_EXPR;
23677 break;
23678 case CPP_OR:
23679 code = BIT_IOR_EXPR;
23680 break;
23681 case CPP_AND_AND:
23682 code = TRUTH_ANDIF_EXPR;
23683 break;
23684 case CPP_OR_OR:
23685 code = TRUTH_ORIF_EXPR;
23686 break;
23687 default:
23688 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23689 "%<|%>, %<&&%>, or %<||%>");
23690 resync_fail:
23691 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23692 /*or_comma=*/false,
23693 /*consume_paren=*/true);
23694 return list;
23695 }
23696 cp_lexer_consume_token (parser->lexer);
23697
23698 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23699 goto resync_fail;
23700
23701 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23702 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23703 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23704
23705 return nlist;
23706 }
23707
23708 /* OpenMP 2.5:
23709 schedule ( schedule-kind )
23710 schedule ( schedule-kind , expression )
23711
23712 schedule-kind:
23713 static | dynamic | guided | runtime | auto */
23714
23715 static tree
23716 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23717 {
23718 tree c, t;
23719
23720 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23721 return list;
23722
23723 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23724
23725 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23726 {
23727 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23728 const char *p = IDENTIFIER_POINTER (id);
23729
23730 switch (p[0])
23731 {
23732 case 'd':
23733 if (strcmp ("dynamic", p) != 0)
23734 goto invalid_kind;
23735 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23736 break;
23737
23738 case 'g':
23739 if (strcmp ("guided", p) != 0)
23740 goto invalid_kind;
23741 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23742 break;
23743
23744 case 'r':
23745 if (strcmp ("runtime", p) != 0)
23746 goto invalid_kind;
23747 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23748 break;
23749
23750 default:
23751 goto invalid_kind;
23752 }
23753 }
23754 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23755 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23756 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23757 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23758 else
23759 goto invalid_kind;
23760 cp_lexer_consume_token (parser->lexer);
23761
23762 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23763 {
23764 cp_token *token;
23765 cp_lexer_consume_token (parser->lexer);
23766
23767 token = cp_lexer_peek_token (parser->lexer);
23768 t = cp_parser_assignment_expression (parser, false, NULL);
23769
23770 if (t == error_mark_node)
23771 goto resync_fail;
23772 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23773 error_at (token->location, "schedule %<runtime%> does not take "
23774 "a %<chunk_size%> parameter");
23775 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23776 error_at (token->location, "schedule %<auto%> does not take "
23777 "a %<chunk_size%> parameter");
23778 else
23779 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23780
23781 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23782 goto resync_fail;
23783 }
23784 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23785 goto resync_fail;
23786
23787 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23788 OMP_CLAUSE_CHAIN (c) = list;
23789 return c;
23790
23791 invalid_kind:
23792 cp_parser_error (parser, "invalid schedule kind");
23793 resync_fail:
23794 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23795 /*or_comma=*/false,
23796 /*consume_paren=*/true);
23797 return list;
23798 }
23799
23800 /* OpenMP 3.0:
23801 untied */
23802
23803 static tree
23804 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23805 tree list, location_t location)
23806 {
23807 tree c;
23808
23809 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23810
23811 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23812 OMP_CLAUSE_CHAIN (c) = list;
23813 return c;
23814 }
23815
23816 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23817 is a bitmask in MASK. Return the list of clauses found; the result
23818 of clause default goes in *pdefault. */
23819
23820 static tree
23821 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23822 const char *where, cp_token *pragma_tok)
23823 {
23824 tree clauses = NULL;
23825 bool first = true;
23826 cp_token *token = NULL;
23827
23828 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23829 {
23830 pragma_omp_clause c_kind;
23831 const char *c_name;
23832 tree prev = clauses;
23833
23834 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23835 cp_lexer_consume_token (parser->lexer);
23836
23837 token = cp_lexer_peek_token (parser->lexer);
23838 c_kind = cp_parser_omp_clause_name (parser);
23839 first = false;
23840
23841 switch (c_kind)
23842 {
23843 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23844 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23845 token->location);
23846 c_name = "collapse";
23847 break;
23848 case PRAGMA_OMP_CLAUSE_COPYIN:
23849 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23850 c_name = "copyin";
23851 break;
23852 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23853 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23854 clauses);
23855 c_name = "copyprivate";
23856 break;
23857 case PRAGMA_OMP_CLAUSE_DEFAULT:
23858 clauses = cp_parser_omp_clause_default (parser, clauses,
23859 token->location);
23860 c_name = "default";
23861 break;
23862 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23863 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23864 clauses);
23865 c_name = "firstprivate";
23866 break;
23867 case PRAGMA_OMP_CLAUSE_IF:
23868 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23869 c_name = "if";
23870 break;
23871 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23872 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23873 clauses);
23874 c_name = "lastprivate";
23875 break;
23876 case PRAGMA_OMP_CLAUSE_NOWAIT:
23877 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23878 c_name = "nowait";
23879 break;
23880 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23881 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23882 token->location);
23883 c_name = "num_threads";
23884 break;
23885 case PRAGMA_OMP_CLAUSE_ORDERED:
23886 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23887 token->location);
23888 c_name = "ordered";
23889 break;
23890 case PRAGMA_OMP_CLAUSE_PRIVATE:
23891 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23892 clauses);
23893 c_name = "private";
23894 break;
23895 case PRAGMA_OMP_CLAUSE_REDUCTION:
23896 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23897 c_name = "reduction";
23898 break;
23899 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23900 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23901 token->location);
23902 c_name = "schedule";
23903 break;
23904 case PRAGMA_OMP_CLAUSE_SHARED:
23905 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23906 clauses);
23907 c_name = "shared";
23908 break;
23909 case PRAGMA_OMP_CLAUSE_UNTIED:
23910 clauses = cp_parser_omp_clause_untied (parser, clauses,
23911 token->location);
23912 c_name = "nowait";
23913 break;
23914 default:
23915 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23916 goto saw_error;
23917 }
23918
23919 if (((mask >> c_kind) & 1) == 0)
23920 {
23921 /* Remove the invalid clause(s) from the list to avoid
23922 confusing the rest of the compiler. */
23923 clauses = prev;
23924 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23925 }
23926 }
23927 saw_error:
23928 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23929 return finish_omp_clauses (clauses);
23930 }
23931
23932 /* OpenMP 2.5:
23933 structured-block:
23934 statement
23935
23936 In practice, we're also interested in adding the statement to an
23937 outer node. So it is convenient if we work around the fact that
23938 cp_parser_statement calls add_stmt. */
23939
23940 static unsigned
23941 cp_parser_begin_omp_structured_block (cp_parser *parser)
23942 {
23943 unsigned save = parser->in_statement;
23944
23945 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23946 This preserves the "not within loop or switch" style error messages
23947 for nonsense cases like
23948 void foo() {
23949 #pragma omp single
23950 break;
23951 }
23952 */
23953 if (parser->in_statement)
23954 parser->in_statement = IN_OMP_BLOCK;
23955
23956 return save;
23957 }
23958
23959 static void
23960 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23961 {
23962 parser->in_statement = save;
23963 }
23964
23965 static tree
23966 cp_parser_omp_structured_block (cp_parser *parser)
23967 {
23968 tree stmt = begin_omp_structured_block ();
23969 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23970
23971 cp_parser_statement (parser, NULL_TREE, false, NULL);
23972
23973 cp_parser_end_omp_structured_block (parser, save);
23974 return finish_omp_structured_block (stmt);
23975 }
23976
23977 /* OpenMP 2.5:
23978 # pragma omp atomic new-line
23979 expression-stmt
23980
23981 expression-stmt:
23982 x binop= expr | x++ | ++x | x-- | --x
23983 binop:
23984 +, *, -, /, &, ^, |, <<, >>
23985
23986 where x is an lvalue expression with scalar type. */
23987
23988 static void
23989 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23990 {
23991 tree lhs, rhs;
23992 enum tree_code code;
23993
23994 cp_parser_require_pragma_eol (parser, pragma_tok);
23995
23996 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23997 /*cast_p=*/false, NULL);
23998 switch (TREE_CODE (lhs))
23999 {
24000 case ERROR_MARK:
24001 goto saw_error;
24002
24003 case PREINCREMENT_EXPR:
24004 case POSTINCREMENT_EXPR:
24005 lhs = TREE_OPERAND (lhs, 0);
24006 code = PLUS_EXPR;
24007 rhs = integer_one_node;
24008 break;
24009
24010 case PREDECREMENT_EXPR:
24011 case POSTDECREMENT_EXPR:
24012 lhs = TREE_OPERAND (lhs, 0);
24013 code = MINUS_EXPR;
24014 rhs = integer_one_node;
24015 break;
24016
24017 case COMPOUND_EXPR:
24018 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24019 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24020 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24021 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24022 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24023 (TREE_OPERAND (lhs, 1), 0), 0)))
24024 == BOOLEAN_TYPE)
24025 /* Undo effects of boolean_increment for post {in,de}crement. */
24026 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24027 /* FALLTHRU */
24028 case MODIFY_EXPR:
24029 if (TREE_CODE (lhs) == MODIFY_EXPR
24030 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24031 {
24032 /* Undo effects of boolean_increment. */
24033 if (integer_onep (TREE_OPERAND (lhs, 1)))
24034 {
24035 /* This is pre or post increment. */
24036 rhs = TREE_OPERAND (lhs, 1);
24037 lhs = TREE_OPERAND (lhs, 0);
24038 code = NOP_EXPR;
24039 break;
24040 }
24041 }
24042 /* FALLTHRU */
24043 default:
24044 switch (cp_lexer_peek_token (parser->lexer)->type)
24045 {
24046 case CPP_MULT_EQ:
24047 code = MULT_EXPR;
24048 break;
24049 case CPP_DIV_EQ:
24050 code = TRUNC_DIV_EXPR;
24051 break;
24052 case CPP_PLUS_EQ:
24053 code = PLUS_EXPR;
24054 break;
24055 case CPP_MINUS_EQ:
24056 code = MINUS_EXPR;
24057 break;
24058 case CPP_LSHIFT_EQ:
24059 code = LSHIFT_EXPR;
24060 break;
24061 case CPP_RSHIFT_EQ:
24062 code = RSHIFT_EXPR;
24063 break;
24064 case CPP_AND_EQ:
24065 code = BIT_AND_EXPR;
24066 break;
24067 case CPP_OR_EQ:
24068 code = BIT_IOR_EXPR;
24069 break;
24070 case CPP_XOR_EQ:
24071 code = BIT_XOR_EXPR;
24072 break;
24073 default:
24074 cp_parser_error (parser,
24075 "invalid operator for %<#pragma omp atomic%>");
24076 goto saw_error;
24077 }
24078 cp_lexer_consume_token (parser->lexer);
24079
24080 rhs = cp_parser_expression (parser, false, NULL);
24081 if (rhs == error_mark_node)
24082 goto saw_error;
24083 break;
24084 }
24085 finish_omp_atomic (code, lhs, rhs);
24086 cp_parser_consume_semicolon_at_end_of_statement (parser);
24087 return;
24088
24089 saw_error:
24090 cp_parser_skip_to_end_of_block_or_statement (parser);
24091 }
24092
24093
24094 /* OpenMP 2.5:
24095 # pragma omp barrier new-line */
24096
24097 static void
24098 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24099 {
24100 cp_parser_require_pragma_eol (parser, pragma_tok);
24101 finish_omp_barrier ();
24102 }
24103
24104 /* OpenMP 2.5:
24105 # pragma omp critical [(name)] new-line
24106 structured-block */
24107
24108 static tree
24109 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24110 {
24111 tree stmt, name = NULL;
24112
24113 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24114 {
24115 cp_lexer_consume_token (parser->lexer);
24116
24117 name = cp_parser_identifier (parser);
24118
24119 if (name == error_mark_node
24120 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24121 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24122 /*or_comma=*/false,
24123 /*consume_paren=*/true);
24124 if (name == error_mark_node)
24125 name = NULL;
24126 }
24127 cp_parser_require_pragma_eol (parser, pragma_tok);
24128
24129 stmt = cp_parser_omp_structured_block (parser);
24130 return c_finish_omp_critical (input_location, stmt, name);
24131 }
24132
24133 /* OpenMP 2.5:
24134 # pragma omp flush flush-vars[opt] new-line
24135
24136 flush-vars:
24137 ( variable-list ) */
24138
24139 static void
24140 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24141 {
24142 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24143 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24144 cp_parser_require_pragma_eol (parser, pragma_tok);
24145
24146 finish_omp_flush ();
24147 }
24148
24149 /* Helper function, to parse omp for increment expression. */
24150
24151 static tree
24152 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24153 {
24154 tree cond = cp_parser_binary_expression (parser, false, true,
24155 PREC_NOT_OPERATOR, NULL);
24156 bool overloaded_p;
24157
24158 if (cond == error_mark_node
24159 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24160 {
24161 cp_parser_skip_to_end_of_statement (parser);
24162 return error_mark_node;
24163 }
24164
24165 switch (TREE_CODE (cond))
24166 {
24167 case GT_EXPR:
24168 case GE_EXPR:
24169 case LT_EXPR:
24170 case LE_EXPR:
24171 break;
24172 default:
24173 return error_mark_node;
24174 }
24175
24176 /* If decl is an iterator, preserve LHS and RHS of the relational
24177 expr until finish_omp_for. */
24178 if (decl
24179 && (type_dependent_expression_p (decl)
24180 || CLASS_TYPE_P (TREE_TYPE (decl))))
24181 return cond;
24182
24183 return build_x_binary_op (TREE_CODE (cond),
24184 TREE_OPERAND (cond, 0), ERROR_MARK,
24185 TREE_OPERAND (cond, 1), ERROR_MARK,
24186 &overloaded_p, tf_warning_or_error);
24187 }
24188
24189 /* Helper function, to parse omp for increment expression. */
24190
24191 static tree
24192 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24193 {
24194 cp_token *token = cp_lexer_peek_token (parser->lexer);
24195 enum tree_code op;
24196 tree lhs, rhs;
24197 cp_id_kind idk;
24198 bool decl_first;
24199
24200 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24201 {
24202 op = (token->type == CPP_PLUS_PLUS
24203 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24204 cp_lexer_consume_token (parser->lexer);
24205 lhs = cp_parser_cast_expression (parser, false, false, NULL);
24206 if (lhs != decl)
24207 return error_mark_node;
24208 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24209 }
24210
24211 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24212 if (lhs != decl)
24213 return error_mark_node;
24214
24215 token = cp_lexer_peek_token (parser->lexer);
24216 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24217 {
24218 op = (token->type == CPP_PLUS_PLUS
24219 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24220 cp_lexer_consume_token (parser->lexer);
24221 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24222 }
24223
24224 op = cp_parser_assignment_operator_opt (parser);
24225 if (op == ERROR_MARK)
24226 return error_mark_node;
24227
24228 if (op != NOP_EXPR)
24229 {
24230 rhs = cp_parser_assignment_expression (parser, false, NULL);
24231 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24232 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24233 }
24234
24235 lhs = cp_parser_binary_expression (parser, false, false,
24236 PREC_ADDITIVE_EXPRESSION, NULL);
24237 token = cp_lexer_peek_token (parser->lexer);
24238 decl_first = lhs == decl;
24239 if (decl_first)
24240 lhs = NULL_TREE;
24241 if (token->type != CPP_PLUS
24242 && token->type != CPP_MINUS)
24243 return error_mark_node;
24244
24245 do
24246 {
24247 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24248 cp_lexer_consume_token (parser->lexer);
24249 rhs = cp_parser_binary_expression (parser, false, false,
24250 PREC_ADDITIVE_EXPRESSION, NULL);
24251 token = cp_lexer_peek_token (parser->lexer);
24252 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24253 {
24254 if (lhs == NULL_TREE)
24255 {
24256 if (op == PLUS_EXPR)
24257 lhs = rhs;
24258 else
24259 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24260 }
24261 else
24262 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24263 NULL, tf_warning_or_error);
24264 }
24265 }
24266 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24267
24268 if (!decl_first)
24269 {
24270 if (rhs != decl || op == MINUS_EXPR)
24271 return error_mark_node;
24272 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24273 }
24274 else
24275 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24276
24277 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24278 }
24279
24280 /* Parse the restricted form of the for statement allowed by OpenMP. */
24281
24282 static tree
24283 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24284 {
24285 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24286 tree real_decl, initv, condv, incrv, declv;
24287 tree this_pre_body, cl;
24288 location_t loc_first;
24289 bool collapse_err = false;
24290 int i, collapse = 1, nbraces = 0;
24291 VEC(tree,gc) *for_block = make_tree_vector ();
24292
24293 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24294 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24295 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24296
24297 gcc_assert (collapse >= 1);
24298
24299 declv = make_tree_vec (collapse);
24300 initv = make_tree_vec (collapse);
24301 condv = make_tree_vec (collapse);
24302 incrv = make_tree_vec (collapse);
24303
24304 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24305
24306 for (i = 0; i < collapse; i++)
24307 {
24308 int bracecount = 0;
24309 bool add_private_clause = false;
24310 location_t loc;
24311
24312 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24313 {
24314 cp_parser_error (parser, "for statement expected");
24315 return NULL;
24316 }
24317 loc = cp_lexer_consume_token (parser->lexer)->location;
24318
24319 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24320 return NULL;
24321
24322 init = decl = real_decl = NULL;
24323 this_pre_body = push_stmt_list ();
24324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24325 {
24326 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24327
24328 init-expr:
24329 var = lb
24330 integer-type var = lb
24331 random-access-iterator-type var = lb
24332 pointer-type var = lb
24333 */
24334 cp_decl_specifier_seq type_specifiers;
24335
24336 /* First, try to parse as an initialized declaration. See
24337 cp_parser_condition, from whence the bulk of this is copied. */
24338
24339 cp_parser_parse_tentatively (parser);
24340 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24341 /*is_trailing_return=*/false,
24342 &type_specifiers);
24343 if (cp_parser_parse_definitely (parser))
24344 {
24345 /* If parsing a type specifier seq succeeded, then this
24346 MUST be a initialized declaration. */
24347 tree asm_specification, attributes;
24348 cp_declarator *declarator;
24349
24350 declarator = cp_parser_declarator (parser,
24351 CP_PARSER_DECLARATOR_NAMED,
24352 /*ctor_dtor_or_conv_p=*/NULL,
24353 /*parenthesized_p=*/NULL,
24354 /*member_p=*/false);
24355 attributes = cp_parser_attributes_opt (parser);
24356 asm_specification = cp_parser_asm_specification_opt (parser);
24357
24358 if (declarator == cp_error_declarator)
24359 cp_parser_skip_to_end_of_statement (parser);
24360
24361 else
24362 {
24363 tree pushed_scope, auto_node;
24364
24365 decl = start_decl (declarator, &type_specifiers,
24366 SD_INITIALIZED, attributes,
24367 /*prefix_attributes=*/NULL_TREE,
24368 &pushed_scope);
24369
24370 auto_node = type_uses_auto (TREE_TYPE (decl));
24371 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24372 {
24373 if (cp_lexer_next_token_is (parser->lexer,
24374 CPP_OPEN_PAREN))
24375 error ("parenthesized initialization is not allowed in "
24376 "OpenMP %<for%> loop");
24377 else
24378 /* Trigger an error. */
24379 cp_parser_require (parser, CPP_EQ, RT_EQ);
24380
24381 init = error_mark_node;
24382 cp_parser_skip_to_end_of_statement (parser);
24383 }
24384 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24385 || type_dependent_expression_p (decl)
24386 || auto_node)
24387 {
24388 bool is_direct_init, is_non_constant_init;
24389
24390 init = cp_parser_initializer (parser,
24391 &is_direct_init,
24392 &is_non_constant_init);
24393
24394 if (auto_node && describable_type (init))
24395 {
24396 TREE_TYPE (decl)
24397 = do_auto_deduction (TREE_TYPE (decl), init,
24398 auto_node);
24399
24400 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24401 && !type_dependent_expression_p (decl))
24402 goto non_class;
24403 }
24404
24405 cp_finish_decl (decl, init, !is_non_constant_init,
24406 asm_specification,
24407 LOOKUP_ONLYCONVERTING);
24408 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24409 {
24410 VEC_safe_push (tree, gc, for_block, this_pre_body);
24411 init = NULL_TREE;
24412 }
24413 else
24414 init = pop_stmt_list (this_pre_body);
24415 this_pre_body = NULL_TREE;
24416 }
24417 else
24418 {
24419 /* Consume '='. */
24420 cp_lexer_consume_token (parser->lexer);
24421 init = cp_parser_assignment_expression (parser, false, NULL);
24422
24423 non_class:
24424 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24425 init = error_mark_node;
24426 else
24427 cp_finish_decl (decl, NULL_TREE,
24428 /*init_const_expr_p=*/false,
24429 asm_specification,
24430 LOOKUP_ONLYCONVERTING);
24431 }
24432
24433 if (pushed_scope)
24434 pop_scope (pushed_scope);
24435 }
24436 }
24437 else
24438 {
24439 cp_id_kind idk;
24440 /* If parsing a type specifier sequence failed, then
24441 this MUST be a simple expression. */
24442 cp_parser_parse_tentatively (parser);
24443 decl = cp_parser_primary_expression (parser, false, false,
24444 false, &idk);
24445 if (!cp_parser_error_occurred (parser)
24446 && decl
24447 && DECL_P (decl)
24448 && CLASS_TYPE_P (TREE_TYPE (decl)))
24449 {
24450 tree rhs;
24451
24452 cp_parser_parse_definitely (parser);
24453 cp_parser_require (parser, CPP_EQ, RT_EQ);
24454 rhs = cp_parser_assignment_expression (parser, false, NULL);
24455 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24456 rhs,
24457 tf_warning_or_error));
24458 add_private_clause = true;
24459 }
24460 else
24461 {
24462 decl = NULL;
24463 cp_parser_abort_tentative_parse (parser);
24464 init = cp_parser_expression (parser, false, NULL);
24465 if (init)
24466 {
24467 if (TREE_CODE (init) == MODIFY_EXPR
24468 || TREE_CODE (init) == MODOP_EXPR)
24469 real_decl = TREE_OPERAND (init, 0);
24470 }
24471 }
24472 }
24473 }
24474 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24475 if (this_pre_body)
24476 {
24477 this_pre_body = pop_stmt_list (this_pre_body);
24478 if (pre_body)
24479 {
24480 tree t = pre_body;
24481 pre_body = push_stmt_list ();
24482 add_stmt (t);
24483 add_stmt (this_pre_body);
24484 pre_body = pop_stmt_list (pre_body);
24485 }
24486 else
24487 pre_body = this_pre_body;
24488 }
24489
24490 if (decl)
24491 real_decl = decl;
24492 if (par_clauses != NULL && real_decl != NULL_TREE)
24493 {
24494 tree *c;
24495 for (c = par_clauses; *c ; )
24496 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24497 && OMP_CLAUSE_DECL (*c) == real_decl)
24498 {
24499 error_at (loc, "iteration variable %qD"
24500 " should not be firstprivate", real_decl);
24501 *c = OMP_CLAUSE_CHAIN (*c);
24502 }
24503 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24504 && OMP_CLAUSE_DECL (*c) == real_decl)
24505 {
24506 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24507 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24508 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24509 OMP_CLAUSE_DECL (l) = real_decl;
24510 OMP_CLAUSE_CHAIN (l) = clauses;
24511 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24512 clauses = l;
24513 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24514 CP_OMP_CLAUSE_INFO (*c) = NULL;
24515 add_private_clause = false;
24516 }
24517 else
24518 {
24519 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24520 && OMP_CLAUSE_DECL (*c) == real_decl)
24521 add_private_clause = false;
24522 c = &OMP_CLAUSE_CHAIN (*c);
24523 }
24524 }
24525
24526 if (add_private_clause)
24527 {
24528 tree c;
24529 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24530 {
24531 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24532 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24533 && OMP_CLAUSE_DECL (c) == decl)
24534 break;
24535 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24536 && OMP_CLAUSE_DECL (c) == decl)
24537 error_at (loc, "iteration variable %qD "
24538 "should not be firstprivate",
24539 decl);
24540 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24541 && OMP_CLAUSE_DECL (c) == decl)
24542 error_at (loc, "iteration variable %qD should not be reduction",
24543 decl);
24544 }
24545 if (c == NULL)
24546 {
24547 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24548 OMP_CLAUSE_DECL (c) = decl;
24549 c = finish_omp_clauses (c);
24550 if (c)
24551 {
24552 OMP_CLAUSE_CHAIN (c) = clauses;
24553 clauses = c;
24554 }
24555 }
24556 }
24557
24558 cond = NULL;
24559 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24560 cond = cp_parser_omp_for_cond (parser, decl);
24561 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24562
24563 incr = NULL;
24564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24565 {
24566 /* If decl is an iterator, preserve the operator on decl
24567 until finish_omp_for. */
24568 if (decl
24569 && (type_dependent_expression_p (decl)
24570 || CLASS_TYPE_P (TREE_TYPE (decl))))
24571 incr = cp_parser_omp_for_incr (parser, decl);
24572 else
24573 incr = cp_parser_expression (parser, false, NULL);
24574 }
24575
24576 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24577 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24578 /*or_comma=*/false,
24579 /*consume_paren=*/true);
24580
24581 TREE_VEC_ELT (declv, i) = decl;
24582 TREE_VEC_ELT (initv, i) = init;
24583 TREE_VEC_ELT (condv, i) = cond;
24584 TREE_VEC_ELT (incrv, i) = incr;
24585
24586 if (i == collapse - 1)
24587 break;
24588
24589 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24590 in between the collapsed for loops to be still considered perfectly
24591 nested. Hopefully the final version clarifies this.
24592 For now handle (multiple) {'s and empty statements. */
24593 cp_parser_parse_tentatively (parser);
24594 do
24595 {
24596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24597 break;
24598 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24599 {
24600 cp_lexer_consume_token (parser->lexer);
24601 bracecount++;
24602 }
24603 else if (bracecount
24604 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24605 cp_lexer_consume_token (parser->lexer);
24606 else
24607 {
24608 loc = cp_lexer_peek_token (parser->lexer)->location;
24609 error_at (loc, "not enough collapsed for loops");
24610 collapse_err = true;
24611 cp_parser_abort_tentative_parse (parser);
24612 declv = NULL_TREE;
24613 break;
24614 }
24615 }
24616 while (1);
24617
24618 if (declv)
24619 {
24620 cp_parser_parse_definitely (parser);
24621 nbraces += bracecount;
24622 }
24623 }
24624
24625 /* Note that we saved the original contents of this flag when we entered
24626 the structured block, and so we don't need to re-save it here. */
24627 parser->in_statement = IN_OMP_FOR;
24628
24629 /* Note that the grammar doesn't call for a structured block here,
24630 though the loop as a whole is a structured block. */
24631 body = push_stmt_list ();
24632 cp_parser_statement (parser, NULL_TREE, false, NULL);
24633 body = pop_stmt_list (body);
24634
24635 if (declv == NULL_TREE)
24636 ret = NULL_TREE;
24637 else
24638 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24639 pre_body, clauses);
24640
24641 while (nbraces)
24642 {
24643 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24644 {
24645 cp_lexer_consume_token (parser->lexer);
24646 nbraces--;
24647 }
24648 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24649 cp_lexer_consume_token (parser->lexer);
24650 else
24651 {
24652 if (!collapse_err)
24653 {
24654 error_at (cp_lexer_peek_token (parser->lexer)->location,
24655 "collapsed loops not perfectly nested");
24656 }
24657 collapse_err = true;
24658 cp_parser_statement_seq_opt (parser, NULL);
24659 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24660 break;
24661 }
24662 }
24663
24664 while (!VEC_empty (tree, for_block))
24665 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24666 release_tree_vector (for_block);
24667
24668 return ret;
24669 }
24670
24671 /* OpenMP 2.5:
24672 #pragma omp for for-clause[optseq] new-line
24673 for-loop */
24674
24675 #define OMP_FOR_CLAUSE_MASK \
24676 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24677 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24678 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24679 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24680 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24681 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24682 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24683 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24684
24685 static tree
24686 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24687 {
24688 tree clauses, sb, ret;
24689 unsigned int save;
24690
24691 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24692 "#pragma omp for", pragma_tok);
24693
24694 sb = begin_omp_structured_block ();
24695 save = cp_parser_begin_omp_structured_block (parser);
24696
24697 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24698
24699 cp_parser_end_omp_structured_block (parser, save);
24700 add_stmt (finish_omp_structured_block (sb));
24701
24702 return ret;
24703 }
24704
24705 /* OpenMP 2.5:
24706 # pragma omp master new-line
24707 structured-block */
24708
24709 static tree
24710 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24711 {
24712 cp_parser_require_pragma_eol (parser, pragma_tok);
24713 return c_finish_omp_master (input_location,
24714 cp_parser_omp_structured_block (parser));
24715 }
24716
24717 /* OpenMP 2.5:
24718 # pragma omp ordered new-line
24719 structured-block */
24720
24721 static tree
24722 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24723 {
24724 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24725 cp_parser_require_pragma_eol (parser, pragma_tok);
24726 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24727 }
24728
24729 /* OpenMP 2.5:
24730
24731 section-scope:
24732 { section-sequence }
24733
24734 section-sequence:
24735 section-directive[opt] structured-block
24736 section-sequence section-directive structured-block */
24737
24738 static tree
24739 cp_parser_omp_sections_scope (cp_parser *parser)
24740 {
24741 tree stmt, substmt;
24742 bool error_suppress = false;
24743 cp_token *tok;
24744
24745 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24746 return NULL_TREE;
24747
24748 stmt = push_stmt_list ();
24749
24750 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24751 {
24752 unsigned save;
24753
24754 substmt = begin_omp_structured_block ();
24755 save = cp_parser_begin_omp_structured_block (parser);
24756
24757 while (1)
24758 {
24759 cp_parser_statement (parser, NULL_TREE, false, NULL);
24760
24761 tok = cp_lexer_peek_token (parser->lexer);
24762 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24763 break;
24764 if (tok->type == CPP_CLOSE_BRACE)
24765 break;
24766 if (tok->type == CPP_EOF)
24767 break;
24768 }
24769
24770 cp_parser_end_omp_structured_block (parser, save);
24771 substmt = finish_omp_structured_block (substmt);
24772 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24773 add_stmt (substmt);
24774 }
24775
24776 while (1)
24777 {
24778 tok = cp_lexer_peek_token (parser->lexer);
24779 if (tok->type == CPP_CLOSE_BRACE)
24780 break;
24781 if (tok->type == CPP_EOF)
24782 break;
24783
24784 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24785 {
24786 cp_lexer_consume_token (parser->lexer);
24787 cp_parser_require_pragma_eol (parser, tok);
24788 error_suppress = false;
24789 }
24790 else if (!error_suppress)
24791 {
24792 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24793 error_suppress = true;
24794 }
24795
24796 substmt = cp_parser_omp_structured_block (parser);
24797 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24798 add_stmt (substmt);
24799 }
24800 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24801
24802 substmt = pop_stmt_list (stmt);
24803
24804 stmt = make_node (OMP_SECTIONS);
24805 TREE_TYPE (stmt) = void_type_node;
24806 OMP_SECTIONS_BODY (stmt) = substmt;
24807
24808 add_stmt (stmt);
24809 return stmt;
24810 }
24811
24812 /* OpenMP 2.5:
24813 # pragma omp sections sections-clause[optseq] newline
24814 sections-scope */
24815
24816 #define OMP_SECTIONS_CLAUSE_MASK \
24817 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24818 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24819 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24820 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24821 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24822
24823 static tree
24824 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24825 {
24826 tree clauses, ret;
24827
24828 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24829 "#pragma omp sections", pragma_tok);
24830
24831 ret = cp_parser_omp_sections_scope (parser);
24832 if (ret)
24833 OMP_SECTIONS_CLAUSES (ret) = clauses;
24834
24835 return ret;
24836 }
24837
24838 /* OpenMP 2.5:
24839 # pragma parallel parallel-clause new-line
24840 # pragma parallel for parallel-for-clause new-line
24841 # pragma parallel sections parallel-sections-clause new-line */
24842
24843 #define OMP_PARALLEL_CLAUSE_MASK \
24844 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24845 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24846 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24847 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24848 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24849 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24850 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24851 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24852
24853 static tree
24854 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24855 {
24856 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24857 const char *p_name = "#pragma omp parallel";
24858 tree stmt, clauses, par_clause, ws_clause, block;
24859 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24860 unsigned int save;
24861 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24862
24863 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24864 {
24865 cp_lexer_consume_token (parser->lexer);
24866 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24867 p_name = "#pragma omp parallel for";
24868 mask |= OMP_FOR_CLAUSE_MASK;
24869 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24870 }
24871 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24872 {
24873 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24874 const char *p = IDENTIFIER_POINTER (id);
24875 if (strcmp (p, "sections") == 0)
24876 {
24877 cp_lexer_consume_token (parser->lexer);
24878 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24879 p_name = "#pragma omp parallel sections";
24880 mask |= OMP_SECTIONS_CLAUSE_MASK;
24881 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24882 }
24883 }
24884
24885 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24886 block = begin_omp_parallel ();
24887 save = cp_parser_begin_omp_structured_block (parser);
24888
24889 switch (p_kind)
24890 {
24891 case PRAGMA_OMP_PARALLEL:
24892 cp_parser_statement (parser, NULL_TREE, false, NULL);
24893 par_clause = clauses;
24894 break;
24895
24896 case PRAGMA_OMP_PARALLEL_FOR:
24897 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24898 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24899 break;
24900
24901 case PRAGMA_OMP_PARALLEL_SECTIONS:
24902 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24903 stmt = cp_parser_omp_sections_scope (parser);
24904 if (stmt)
24905 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24906 break;
24907
24908 default:
24909 gcc_unreachable ();
24910 }
24911
24912 cp_parser_end_omp_structured_block (parser, save);
24913 stmt = finish_omp_parallel (par_clause, block);
24914 if (p_kind != PRAGMA_OMP_PARALLEL)
24915 OMP_PARALLEL_COMBINED (stmt) = 1;
24916 return stmt;
24917 }
24918
24919 /* OpenMP 2.5:
24920 # pragma omp single single-clause[optseq] new-line
24921 structured-block */
24922
24923 #define OMP_SINGLE_CLAUSE_MASK \
24924 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24925 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24926 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24927 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24928
24929 static tree
24930 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24931 {
24932 tree stmt = make_node (OMP_SINGLE);
24933 TREE_TYPE (stmt) = void_type_node;
24934
24935 OMP_SINGLE_CLAUSES (stmt)
24936 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24937 "#pragma omp single", pragma_tok);
24938 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24939
24940 return add_stmt (stmt);
24941 }
24942
24943 /* OpenMP 3.0:
24944 # pragma omp task task-clause[optseq] new-line
24945 structured-block */
24946
24947 #define OMP_TASK_CLAUSE_MASK \
24948 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24949 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24950 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24951 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24952 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24953 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24954
24955 static tree
24956 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24957 {
24958 tree clauses, block;
24959 unsigned int save;
24960
24961 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24962 "#pragma omp task", pragma_tok);
24963 block = begin_omp_task ();
24964 save = cp_parser_begin_omp_structured_block (parser);
24965 cp_parser_statement (parser, NULL_TREE, false, NULL);
24966 cp_parser_end_omp_structured_block (parser, save);
24967 return finish_omp_task (clauses, block);
24968 }
24969
24970 /* OpenMP 3.0:
24971 # pragma omp taskwait new-line */
24972
24973 static void
24974 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24975 {
24976 cp_parser_require_pragma_eol (parser, pragma_tok);
24977 finish_omp_taskwait ();
24978 }
24979
24980 /* OpenMP 2.5:
24981 # pragma omp threadprivate (variable-list) */
24982
24983 static void
24984 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24985 {
24986 tree vars;
24987
24988 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24989 cp_parser_require_pragma_eol (parser, pragma_tok);
24990
24991 finish_omp_threadprivate (vars);
24992 }
24993
24994 /* Main entry point to OpenMP statement pragmas. */
24995
24996 static void
24997 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24998 {
24999 tree stmt;
25000
25001 switch (pragma_tok->pragma_kind)
25002 {
25003 case PRAGMA_OMP_ATOMIC:
25004 cp_parser_omp_atomic (parser, pragma_tok);
25005 return;
25006 case PRAGMA_OMP_CRITICAL:
25007 stmt = cp_parser_omp_critical (parser, pragma_tok);
25008 break;
25009 case PRAGMA_OMP_FOR:
25010 stmt = cp_parser_omp_for (parser, pragma_tok);
25011 break;
25012 case PRAGMA_OMP_MASTER:
25013 stmt = cp_parser_omp_master (parser, pragma_tok);
25014 break;
25015 case PRAGMA_OMP_ORDERED:
25016 stmt = cp_parser_omp_ordered (parser, pragma_tok);
25017 break;
25018 case PRAGMA_OMP_PARALLEL:
25019 stmt = cp_parser_omp_parallel (parser, pragma_tok);
25020 break;
25021 case PRAGMA_OMP_SECTIONS:
25022 stmt = cp_parser_omp_sections (parser, pragma_tok);
25023 break;
25024 case PRAGMA_OMP_SINGLE:
25025 stmt = cp_parser_omp_single (parser, pragma_tok);
25026 break;
25027 case PRAGMA_OMP_TASK:
25028 stmt = cp_parser_omp_task (parser, pragma_tok);
25029 break;
25030 default:
25031 gcc_unreachable ();
25032 }
25033
25034 if (stmt)
25035 SET_EXPR_LOCATION (stmt, pragma_tok->location);
25036 }
25037 \f
25038 /* The parser. */
25039
25040 static GTY (()) cp_parser *the_parser;
25041
25042 \f
25043 /* Special handling for the first token or line in the file. The first
25044 thing in the file might be #pragma GCC pch_preprocess, which loads a
25045 PCH file, which is a GC collection point. So we need to handle this
25046 first pragma without benefit of an existing lexer structure.
25047
25048 Always returns one token to the caller in *FIRST_TOKEN. This is
25049 either the true first token of the file, or the first token after
25050 the initial pragma. */
25051
25052 static void
25053 cp_parser_initial_pragma (cp_token *first_token)
25054 {
25055 tree name = NULL;
25056
25057 cp_lexer_get_preprocessor_token (NULL, first_token);
25058 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25059 return;
25060
25061 cp_lexer_get_preprocessor_token (NULL, first_token);
25062 if (first_token->type == CPP_STRING)
25063 {
25064 name = first_token->u.value;
25065
25066 cp_lexer_get_preprocessor_token (NULL, first_token);
25067 if (first_token->type != CPP_PRAGMA_EOL)
25068 error_at (first_token->location,
25069 "junk at end of %<#pragma GCC pch_preprocess%>");
25070 }
25071 else
25072 error_at (first_token->location, "expected string literal");
25073
25074 /* Skip to the end of the pragma. */
25075 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25076 cp_lexer_get_preprocessor_token (NULL, first_token);
25077
25078 /* Now actually load the PCH file. */
25079 if (name)
25080 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25081
25082 /* Read one more token to return to our caller. We have to do this
25083 after reading the PCH file in, since its pointers have to be
25084 live. */
25085 cp_lexer_get_preprocessor_token (NULL, first_token);
25086 }
25087
25088 /* Normal parsing of a pragma token. Here we can (and must) use the
25089 regular lexer. */
25090
25091 static bool
25092 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25093 {
25094 cp_token *pragma_tok;
25095 unsigned int id;
25096
25097 pragma_tok = cp_lexer_consume_token (parser->lexer);
25098 gcc_assert (pragma_tok->type == CPP_PRAGMA);
25099 parser->lexer->in_pragma = true;
25100
25101 id = pragma_tok->pragma_kind;
25102 switch (id)
25103 {
25104 case PRAGMA_GCC_PCH_PREPROCESS:
25105 error_at (pragma_tok->location,
25106 "%<#pragma GCC pch_preprocess%> must be first");
25107 break;
25108
25109 case PRAGMA_OMP_BARRIER:
25110 switch (context)
25111 {
25112 case pragma_compound:
25113 cp_parser_omp_barrier (parser, pragma_tok);
25114 return false;
25115 case pragma_stmt:
25116 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25117 "used in compound statements");
25118 break;
25119 default:
25120 goto bad_stmt;
25121 }
25122 break;
25123
25124 case PRAGMA_OMP_FLUSH:
25125 switch (context)
25126 {
25127 case pragma_compound:
25128 cp_parser_omp_flush (parser, pragma_tok);
25129 return false;
25130 case pragma_stmt:
25131 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25132 "used in compound statements");
25133 break;
25134 default:
25135 goto bad_stmt;
25136 }
25137 break;
25138
25139 case PRAGMA_OMP_TASKWAIT:
25140 switch (context)
25141 {
25142 case pragma_compound:
25143 cp_parser_omp_taskwait (parser, pragma_tok);
25144 return false;
25145 case pragma_stmt:
25146 error_at (pragma_tok->location,
25147 "%<#pragma omp taskwait%> may only be "
25148 "used in compound statements");
25149 break;
25150 default:
25151 goto bad_stmt;
25152 }
25153 break;
25154
25155 case PRAGMA_OMP_THREADPRIVATE:
25156 cp_parser_omp_threadprivate (parser, pragma_tok);
25157 return false;
25158
25159 case PRAGMA_OMP_ATOMIC:
25160 case PRAGMA_OMP_CRITICAL:
25161 case PRAGMA_OMP_FOR:
25162 case PRAGMA_OMP_MASTER:
25163 case PRAGMA_OMP_ORDERED:
25164 case PRAGMA_OMP_PARALLEL:
25165 case PRAGMA_OMP_SECTIONS:
25166 case PRAGMA_OMP_SINGLE:
25167 case PRAGMA_OMP_TASK:
25168 if (context == pragma_external)
25169 goto bad_stmt;
25170 cp_parser_omp_construct (parser, pragma_tok);
25171 return true;
25172
25173 case PRAGMA_OMP_SECTION:
25174 error_at (pragma_tok->location,
25175 "%<#pragma omp section%> may only be used in "
25176 "%<#pragma omp sections%> construct");
25177 break;
25178
25179 default:
25180 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25181 c_invoke_pragma_handler (id);
25182 break;
25183
25184 bad_stmt:
25185 cp_parser_error (parser, "expected declaration specifiers");
25186 break;
25187 }
25188
25189 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25190 return false;
25191 }
25192
25193 /* The interface the pragma parsers have to the lexer. */
25194
25195 enum cpp_ttype
25196 pragma_lex (tree *value)
25197 {
25198 cp_token *tok;
25199 enum cpp_ttype ret;
25200
25201 tok = cp_lexer_peek_token (the_parser->lexer);
25202
25203 ret = tok->type;
25204 *value = tok->u.value;
25205
25206 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25207 ret = CPP_EOF;
25208 else if (ret == CPP_STRING)
25209 *value = cp_parser_string_literal (the_parser, false, false);
25210 else
25211 {
25212 cp_lexer_consume_token (the_parser->lexer);
25213 if (ret == CPP_KEYWORD)
25214 ret = CPP_NAME;
25215 }
25216
25217 return ret;
25218 }
25219
25220 \f
25221 /* External interface. */
25222
25223 /* Parse one entire translation unit. */
25224
25225 void
25226 c_parse_file (void)
25227 {
25228 static bool already_called = false;
25229
25230 if (already_called)
25231 {
25232 sorry ("inter-module optimizations not implemented for C++");
25233 return;
25234 }
25235 already_called = true;
25236
25237 the_parser = cp_parser_new ();
25238 push_deferring_access_checks (flag_access_control
25239 ? dk_no_deferred : dk_no_check);
25240 cp_parser_translation_unit (the_parser);
25241 the_parser = NULL;
25242 }
25243
25244 #include "gt-cp-parser.h"