In gcc/: 2011-04-14 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / cp / parser.c
blob17f5850f894909596d45ef0be776d638ef66dfcd
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>.
6 This file is part of GCC.
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.
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.
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/>. */
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 #include "tree-pretty-print.h"
41 #include "parser.h"
44 /* The lexer. */
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token =
51 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
54 /* The various kinds of non integral constant we encounter. */
55 typedef enum non_integral_constant {
56 NIC_NONE,
57 /* floating-point literal */
58 NIC_FLOAT,
59 /* %<this%> */
60 NIC_THIS,
61 /* %<__FUNCTION__%> */
62 NIC_FUNC_NAME,
63 /* %<__PRETTY_FUNCTION__%> */
64 NIC_PRETTY_FUNC,
65 /* %<__func__%> */
66 NIC_C99_FUNC,
67 /* "%<va_arg%> */
68 NIC_VA_ARG,
69 /* a cast */
70 NIC_CAST,
71 /* %<typeid%> operator */
72 NIC_TYPEID,
73 /* non-constant compound literals */
74 NIC_NCC,
75 /* a function call */
76 NIC_FUNC_CALL,
77 /* an increment */
78 NIC_INC,
79 /* an decrement */
80 NIC_DEC,
81 /* an array reference */
82 NIC_ARRAY_REF,
83 /* %<->%> */
84 NIC_ARROW,
85 /* %<.%> */
86 NIC_POINT,
87 /* the address of a label */
88 NIC_ADDR_LABEL,
89 /* %<*%> */
90 NIC_STAR,
91 /* %<&%> */
92 NIC_ADDR,
93 /* %<++%> */
94 NIC_PREINCREMENT,
95 /* %<--%> */
96 NIC_PREDECREMENT,
97 /* %<new%> */
98 NIC_NEW,
99 /* %<delete%> */
100 NIC_DEL,
101 /* calls to overloaded operators */
102 NIC_OVERLOADED,
103 /* an assignment */
104 NIC_ASSIGNMENT,
105 /* a comma operator */
106 NIC_COMMA,
107 /* a call to a constructor */
108 NIC_CONSTRUCTOR
109 } non_integral_constant;
111 /* The various kinds of errors about name-lookup failing. */
112 typedef enum name_lookup_error {
113 /* NULL */
114 NLE_NULL,
115 /* is not a type */
116 NLE_TYPE,
117 /* is not a class or namespace */
118 NLE_CXX98,
119 /* is not a class, namespace, or enumeration */
120 NLE_NOT_CXX98
121 } name_lookup_error;
123 /* The various kinds of required token */
124 typedef enum required_token {
125 RT_NONE,
126 RT_SEMICOLON, /* ';' */
127 RT_OPEN_PAREN, /* '(' */
128 RT_CLOSE_BRACE, /* '}' */
129 RT_OPEN_BRACE, /* '{' */
130 RT_CLOSE_SQUARE, /* ']' */
131 RT_OPEN_SQUARE, /* '[' */
132 RT_COMMA, /* ',' */
133 RT_SCOPE, /* '::' */
134 RT_LESS, /* '<' */
135 RT_GREATER, /* '>' */
136 RT_EQ, /* '=' */
137 RT_ELLIPSIS, /* '...' */
138 RT_MULT, /* '*' */
139 RT_COMPL, /* '~' */
140 RT_COLON, /* ':' */
141 RT_COLON_SCOPE, /* ':' or '::' */
142 RT_CLOSE_PAREN, /* ')' */
143 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
144 RT_PRAGMA_EOL, /* end of line */
145 RT_NAME, /* identifier */
147 /* The type is CPP_KEYWORD */
148 RT_NEW, /* new */
149 RT_DELETE, /* delete */
150 RT_RETURN, /* return */
151 RT_WHILE, /* while */
152 RT_EXTERN, /* extern */
153 RT_STATIC_ASSERT, /* static_assert */
154 RT_DECLTYPE, /* decltype */
155 RT_OPERATOR, /* operator */
156 RT_CLASS, /* class */
157 RT_TEMPLATE, /* template */
158 RT_NAMESPACE, /* namespace */
159 RT_USING, /* using */
160 RT_ASM, /* asm */
161 RT_TRY, /* try */
162 RT_CATCH, /* catch */
163 RT_THROW, /* throw */
164 RT_LABEL, /* __label__ */
165 RT_AT_TRY, /* @try */
166 RT_AT_SYNCHRONIZED, /* @synchronized */
167 RT_AT_THROW, /* @throw */
169 RT_SELECT, /* selection-statement */
170 RT_INTERATION, /* iteration-statement */
171 RT_JUMP, /* jump-statement */
172 RT_CLASS_KEY, /* class-key */
173 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
174 } required_token;
176 /* Prototypes. */
178 static cp_lexer *cp_lexer_new_main
179 (void);
180 static cp_lexer *cp_lexer_new_from_tokens
181 (cp_token_cache *tokens);
182 static void cp_lexer_destroy
183 (cp_lexer *);
184 static int cp_lexer_saving_tokens
185 (const cp_lexer *);
186 static cp_token *cp_lexer_token_at
187 (cp_lexer *, cp_token_position);
188 static void cp_lexer_get_preprocessor_token
189 (cp_lexer *, cp_token *);
190 static inline cp_token *cp_lexer_peek_token
191 (cp_lexer *);
192 static cp_token *cp_lexer_peek_nth_token
193 (cp_lexer *, size_t);
194 static inline bool cp_lexer_next_token_is
195 (cp_lexer *, enum cpp_ttype);
196 static bool cp_lexer_next_token_is_not
197 (cp_lexer *, enum cpp_ttype);
198 static bool cp_lexer_next_token_is_keyword
199 (cp_lexer *, enum rid);
200 static cp_token *cp_lexer_consume_token
201 (cp_lexer *);
202 static void cp_lexer_purge_token
203 (cp_lexer *);
204 static void cp_lexer_purge_tokens_after
205 (cp_lexer *, cp_token_position);
206 static void cp_lexer_save_tokens
207 (cp_lexer *);
208 static void cp_lexer_commit_tokens
209 (cp_lexer *);
210 static void cp_lexer_rollback_tokens
211 (cp_lexer *);
212 #ifdef ENABLE_CHECKING
213 static void cp_lexer_print_token
214 (FILE *, cp_token *);
215 static inline bool cp_lexer_debugging_p
216 (cp_lexer *);
217 static void cp_lexer_start_debugging
218 (cp_lexer *) ATTRIBUTE_UNUSED;
219 static void cp_lexer_stop_debugging
220 (cp_lexer *) ATTRIBUTE_UNUSED;
221 #else
222 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
223 about passing NULL to functions that require non-NULL arguments
224 (fputs, fprintf). It will never be used, so all we need is a value
225 of the right type that's guaranteed not to be NULL. */
226 #define cp_lexer_debug_stream stdout
227 #define cp_lexer_print_token(str, tok) (void) 0
228 #define cp_lexer_debugging_p(lexer) 0
229 #endif /* ENABLE_CHECKING */
231 static cp_token_cache *cp_token_cache_new
232 (cp_token *, cp_token *);
234 static void cp_parser_initial_pragma
235 (cp_token *);
237 /* Manifest constants. */
238 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
239 #define CP_SAVED_TOKEN_STACK 5
241 /* Variables. */
243 #ifdef ENABLE_CHECKING
244 /* The stream to which debugging output should be written. */
245 static FILE *cp_lexer_debug_stream;
246 #endif /* ENABLE_CHECKING */
248 /* Nonzero if we are parsing an unevaluated operand: an operand to
249 sizeof, typeof, or alignof. */
250 int cp_unevaluated_operand;
252 #ifdef ENABLE_CHECKING
253 /* Dump up to NUM tokens in BUFFER to FILE. If NUM is 0, dump all the
254 tokens. */
256 void
257 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, unsigned num)
259 unsigned i;
260 cp_token *token;
262 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
264 if (num == 0)
265 num = VEC_length (cp_token, buffer);
267 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && i < num; i++)
269 cp_lexer_print_token (file, token);
270 switch (token->type)
272 case CPP_SEMICOLON:
273 case CPP_OPEN_BRACE:
274 case CPP_CLOSE_BRACE:
275 case CPP_EOF:
276 fputc ('\n', file);
277 break;
279 default:
280 fputc (' ', file);
284 if (i == num && i < VEC_length (cp_token, buffer))
286 fprintf (file, " ... ");
287 cp_lexer_print_token (file, VEC_index (cp_token, buffer,
288 VEC_length (cp_token, buffer) - 1));
291 fprintf (file, "\n");
295 /* Dump all tokens in BUFFER to stderr. */
297 void
298 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
300 cp_lexer_dump_tokens (stderr, buffer, 0);
302 #endif
305 /* Allocate memory for a new lexer object and return it. */
307 static cp_lexer *
308 cp_lexer_alloc (void)
310 cp_lexer *lexer;
312 c_common_no_more_pch ();
314 /* Allocate the memory. */
315 lexer = ggc_alloc_cleared_cp_lexer ();
317 #ifdef ENABLE_CHECKING
318 /* Initially we are not debugging. */
319 lexer->debugging_p = false;
320 #endif /* ENABLE_CHECKING */
321 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
322 CP_SAVED_TOKEN_STACK);
324 /* Create the buffer. */
325 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
327 return lexer;
331 /* Create a new main C++ lexer, the lexer that gets tokens from the
332 preprocessor. */
334 static cp_lexer *
335 cp_lexer_new_main (void)
337 cp_lexer *lexer;
338 cp_token token;
340 /* It's possible that parsing the first pragma will load a PCH file,
341 which is a GC collection point. So we have to do that before
342 allocating any memory. */
343 cp_parser_initial_pragma (&token);
345 lexer = cp_lexer_alloc ();
347 /* Put the first token in the buffer. */
348 VEC_quick_push (cp_token, lexer->buffer, &token);
350 /* Get the remaining tokens from the preprocessor. */
351 while (token.type != CPP_EOF)
353 cp_lexer_get_preprocessor_token (lexer, &token);
354 VEC_safe_push (cp_token, gc, lexer->buffer, &token);
357 lexer->last_token = VEC_address (cp_token, lexer->buffer)
358 + VEC_length (cp_token, lexer->buffer)
359 - 1;
360 lexer->next_token = VEC_length (cp_token, lexer->buffer)
361 ? VEC_address (cp_token, lexer->buffer)
362 : &eof_token;
364 /* Subsequent preprocessor diagnostics should use compiler
365 diagnostic functions to get the compiler source location. */
366 done_lexing = true;
368 gcc_assert (!lexer->next_token->purged_p);
369 return lexer;
372 /* Create a new lexer whose token stream is primed with the tokens in
373 CACHE. When these tokens are exhausted, no new tokens will be read. */
375 static cp_lexer *
376 cp_lexer_new_from_tokens (cp_token_cache *cache)
378 cp_token *first = cache->first;
379 cp_token *last = cache->last;
380 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
382 /* We do not own the buffer. */
383 lexer->buffer = NULL;
384 lexer->next_token = first == last ? &eof_token : first;
385 lexer->last_token = last;
387 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
388 CP_SAVED_TOKEN_STACK);
390 #ifdef ENABLE_CHECKING
391 /* Initially we are not debugging. */
392 lexer->debugging_p = false;
393 #endif
395 gcc_assert (!lexer->next_token->purged_p);
396 return lexer;
399 /* Frees all resources associated with LEXER. */
401 static void
402 cp_lexer_destroy (cp_lexer *lexer)
404 VEC_free (cp_token, gc, lexer->buffer);
405 VEC_free (cp_token_position, heap, lexer->saved_tokens);
406 ggc_free (lexer);
409 /* Returns nonzero if debugging information should be output. */
411 #ifdef ENABLE_CHECKING
413 static inline bool
414 cp_lexer_debugging_p (cp_lexer *lexer)
416 return lexer->debugging_p;
419 #endif /* ENABLE_CHECKING */
421 static inline cp_token_position
422 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
424 gcc_assert (!previous_p || lexer->next_token != &eof_token);
426 return lexer->next_token - previous_p;
429 static inline cp_token *
430 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
432 return pos;
435 static inline void
436 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
438 lexer->next_token = cp_lexer_token_at (lexer, pos);
441 static inline cp_token_position
442 cp_lexer_previous_token_position (cp_lexer *lexer)
444 if (lexer->next_token == &eof_token)
445 return lexer->last_token - 1;
446 else
447 return cp_lexer_token_position (lexer, true);
450 static inline cp_token *
451 cp_lexer_previous_token (cp_lexer *lexer)
453 cp_token_position tp = cp_lexer_previous_token_position (lexer);
455 return cp_lexer_token_at (lexer, tp);
458 /* nonzero if we are presently saving tokens. */
460 static inline int
461 cp_lexer_saving_tokens (const cp_lexer* lexer)
463 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
466 /* Store the next token from the preprocessor in *TOKEN. Return true
467 if we reach EOF. If LEXER is NULL, assume we are handling an
468 initial #pragma pch_preprocess, and thus want the lexer to return
469 processed strings. */
471 static void
472 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
474 static int is_extern_c = 0;
476 /* Get a new token from the preprocessor. */
477 token->type
478 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
479 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
480 token->keyword = RID_MAX;
481 token->pragma_kind = PRAGMA_NONE;
482 token->purged_p = false;
484 /* On some systems, some header files are surrounded by an
485 implicit extern "C" block. Set a flag in the token if it
486 comes from such a header. */
487 is_extern_c += pending_lang_change;
488 pending_lang_change = 0;
489 token->implicit_extern_c = is_extern_c > 0;
491 /* Check to see if this token is a keyword. */
492 if (token->type == CPP_NAME)
494 if (C_IS_RESERVED_WORD (token->u.value))
496 /* Mark this token as a keyword. */
497 token->type = CPP_KEYWORD;
498 /* Record which keyword. */
499 token->keyword = C_RID_CODE (token->u.value);
501 else
503 if (warn_cxx0x_compat
504 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
505 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
507 /* Warn about the C++0x keyword (but still treat it as
508 an identifier). */
509 warning (OPT_Wc__0x_compat,
510 "identifier %qE will become a keyword in C++0x",
511 token->u.value);
513 /* Clear out the C_RID_CODE so we don't warn about this
514 particular identifier-turned-keyword again. */
515 C_SET_RID_CODE (token->u.value, RID_MAX);
518 token->ambiguous_p = false;
519 token->keyword = RID_MAX;
522 else if (token->type == CPP_AT_NAME)
524 /* This only happens in Objective-C++; it must be a keyword. */
525 token->type = CPP_KEYWORD;
526 switch (C_RID_CODE (token->u.value))
528 /* Replace 'class' with '@class', 'private' with '@private',
529 etc. This prevents confusion with the C++ keyword
530 'class', and makes the tokens consistent with other
531 Objective-C 'AT' keywords. For example '@class' is
532 reported as RID_AT_CLASS which is consistent with
533 '@synchronized', which is reported as
534 RID_AT_SYNCHRONIZED.
536 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
537 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
538 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
539 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
540 case RID_THROW: token->keyword = RID_AT_THROW; break;
541 case RID_TRY: token->keyword = RID_AT_TRY; break;
542 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
543 default: token->keyword = C_RID_CODE (token->u.value);
546 else if (token->type == CPP_PRAGMA)
548 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
549 token->pragma_kind = ((enum pragma_kind)
550 TREE_INT_CST_LOW (token->u.value));
551 token->u.value = NULL_TREE;
555 /* Update the globals input_location and the input file stack from TOKEN. */
556 static inline void
557 cp_lexer_set_source_position_from_token (cp_token *token)
559 if (token->type != CPP_EOF)
561 input_location = token->location;
565 /* Return a pointer to the next token in the token stream, but do not
566 consume it. */
568 static inline cp_token *
569 cp_lexer_peek_token (cp_lexer *lexer)
571 if (cp_lexer_debugging_p (lexer))
573 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
574 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
575 putc ('\n', cp_lexer_debug_stream);
577 return lexer->next_token;
580 /* Return true if the next token has the indicated TYPE. */
582 static inline bool
583 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
585 return cp_lexer_peek_token (lexer)->type == type;
588 /* Return true if the next token does not have the indicated TYPE. */
590 static inline bool
591 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
593 return !cp_lexer_next_token_is (lexer, type);
596 /* Return true if the next token is the indicated KEYWORD. */
598 static inline bool
599 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
601 return cp_lexer_peek_token (lexer)->keyword == keyword;
604 /* Return true if the next token is not the indicated KEYWORD. */
606 static inline bool
607 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
609 return cp_lexer_peek_token (lexer)->keyword != keyword;
612 /* Return true if the next token is a keyword for a decl-specifier. */
614 static bool
615 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
617 cp_token *token;
619 token = cp_lexer_peek_token (lexer);
620 switch (token->keyword)
622 /* auto specifier: storage-class-specifier in C++,
623 simple-type-specifier in C++0x. */
624 case RID_AUTO:
625 /* Storage classes. */
626 case RID_REGISTER:
627 case RID_STATIC:
628 case RID_EXTERN:
629 case RID_MUTABLE:
630 case RID_THREAD:
631 /* Elaborated type specifiers. */
632 case RID_ENUM:
633 case RID_CLASS:
634 case RID_STRUCT:
635 case RID_UNION:
636 case RID_TYPENAME:
637 /* Simple type specifiers. */
638 case RID_CHAR:
639 case RID_CHAR16:
640 case RID_CHAR32:
641 case RID_WCHAR:
642 case RID_BOOL:
643 case RID_SHORT:
644 case RID_INT:
645 case RID_LONG:
646 case RID_INT128:
647 case RID_SIGNED:
648 case RID_UNSIGNED:
649 case RID_FLOAT:
650 case RID_DOUBLE:
651 case RID_VOID:
652 /* GNU extensions. */
653 case RID_ATTRIBUTE:
654 case RID_TYPEOF:
655 /* C++0x extensions. */
656 case RID_DECLTYPE:
657 return true;
659 default:
660 return false;
664 /* Return a pointer to the Nth token in the token stream. If N is 1,
665 then this is precisely equivalent to cp_lexer_peek_token (except
666 that it is not inline). One would like to disallow that case, but
667 there is one case (cp_parser_nth_token_starts_template_id) where
668 the caller passes a variable for N and it might be 1. */
670 static cp_token *
671 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
673 cp_token *token;
675 /* N is 1-based, not zero-based. */
676 gcc_assert (n > 0);
678 if (cp_lexer_debugging_p (lexer))
679 fprintf (cp_lexer_debug_stream,
680 "cp_lexer: peeking ahead %ld at token: ", (long)n);
682 --n;
683 token = lexer->next_token;
684 gcc_assert (!n || token != &eof_token);
685 while (n != 0)
687 ++token;
688 if (token == lexer->last_token)
690 token = &eof_token;
691 break;
694 if (!token->purged_p)
695 --n;
698 if (cp_lexer_debugging_p (lexer))
700 cp_lexer_print_token (cp_lexer_debug_stream, token);
701 putc ('\n', cp_lexer_debug_stream);
704 return token;
707 /* Return the next token, and advance the lexer's next_token pointer
708 to point to the next non-purged token. */
710 static cp_token *
711 cp_lexer_consume_token (cp_lexer* lexer)
713 cp_token *token = lexer->next_token;
715 gcc_assert (token != &eof_token);
716 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
720 lexer->next_token++;
721 if (lexer->next_token == lexer->last_token)
723 lexer->next_token = &eof_token;
724 break;
728 while (lexer->next_token->purged_p);
730 cp_lexer_set_source_position_from_token (token);
732 /* Provide debugging output. */
733 if (cp_lexer_debugging_p (lexer))
735 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
736 cp_lexer_print_token (cp_lexer_debug_stream, token);
737 putc ('\n', cp_lexer_debug_stream);
740 return token;
743 /* Permanently remove the next token from the token stream, and
744 advance the next_token pointer to refer to the next non-purged
745 token. */
747 static void
748 cp_lexer_purge_token (cp_lexer *lexer)
750 cp_token *tok = lexer->next_token;
752 gcc_assert (tok != &eof_token);
753 tok->purged_p = true;
754 tok->location = UNKNOWN_LOCATION;
755 tok->u.value = NULL_TREE;
756 tok->keyword = RID_MAX;
760 tok++;
761 if (tok == lexer->last_token)
763 tok = &eof_token;
764 break;
767 while (tok->purged_p);
768 lexer->next_token = tok;
771 /* Permanently remove all tokens after TOK, up to, but not
772 including, the token that will be returned next by
773 cp_lexer_peek_token. */
775 static void
776 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
778 cp_token *peek = lexer->next_token;
780 if (peek == &eof_token)
781 peek = lexer->last_token;
783 gcc_assert (tok < peek);
785 for ( tok += 1; tok != peek; tok += 1)
787 tok->purged_p = true;
788 tok->location = UNKNOWN_LOCATION;
789 tok->u.value = NULL_TREE;
790 tok->keyword = RID_MAX;
794 /* Begin saving tokens. All tokens consumed after this point will be
795 preserved. */
797 static void
798 cp_lexer_save_tokens (cp_lexer* lexer)
800 /* Provide debugging output. */
801 if (cp_lexer_debugging_p (lexer))
802 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
804 VEC_safe_push (cp_token_position, heap,
805 lexer->saved_tokens, lexer->next_token);
808 /* Commit to the portion of the token stream most recently saved. */
810 static void
811 cp_lexer_commit_tokens (cp_lexer* lexer)
813 /* Provide debugging output. */
814 if (cp_lexer_debugging_p (lexer))
815 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
817 VEC_pop (cp_token_position, lexer->saved_tokens);
820 /* Return all tokens saved since the last call to cp_lexer_save_tokens
821 to the token stream. Stop saving tokens. */
823 static void
824 cp_lexer_rollback_tokens (cp_lexer* lexer)
826 /* Provide debugging output. */
827 if (cp_lexer_debugging_p (lexer))
828 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
830 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
833 /* Print a representation of the TOKEN on the STREAM. */
835 #ifdef ENABLE_CHECKING
837 static void
838 cp_lexer_print_token (FILE * stream, cp_token *token)
840 /* We don't use cpp_type2name here because the parser defines
841 a few tokens of its own. */
842 static const char *const token_names[] = {
843 /* cpplib-defined token types */
844 #define OP(e, s) #e,
845 #define TK(e, s) #e,
846 TTYPE_TABLE
847 #undef OP
848 #undef TK
849 /* C++ parser token types - see "Manifest constants", above. */
850 "KEYWORD",
851 "TEMPLATE_ID",
852 "NESTED_NAME_SPECIFIER",
855 /* For some tokens, print the associated data. */
856 switch (token->type)
858 case CPP_KEYWORD:
859 /* Some keywords have a value that is not an IDENTIFIER_NODE.
860 For example, `struct' is mapped to an INTEGER_CST. */
861 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
862 break;
863 /* else fall through */
864 case CPP_NAME:
865 fputs (IDENTIFIER_POINTER (token->u.value), stream);
866 break;
868 case CPP_STRING:
869 case CPP_STRING16:
870 case CPP_STRING32:
871 case CPP_WSTRING:
872 case CPP_UTF8STRING:
873 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
874 break;
876 case CPP_NUMBER:
877 print_generic_expr (stream, token->u.value, 0);
878 break;
880 default:
881 /* If we have a name for the token, print it out. Otherwise, we
882 simply give the numeric code. */
883 if (token->type < ARRAY_SIZE(token_names))
884 fputs (token_names[token->type], stream);
885 else
886 fprintf (stream, "[%d]", token->type);
887 break;
891 /* Start emitting debugging information. */
893 static void
894 cp_lexer_start_debugging (cp_lexer* lexer)
896 lexer->debugging_p = true;
899 /* Stop emitting debugging information. */
901 static void
902 cp_lexer_stop_debugging (cp_lexer* lexer)
904 lexer->debugging_p = false;
907 #endif /* ENABLE_CHECKING */
909 /* Create a new cp_token_cache, representing a range of tokens. */
911 static cp_token_cache *
912 cp_token_cache_new (cp_token *first, cp_token *last)
914 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
915 cache->first = first;
916 cache->last = last;
917 return cache;
921 /* Decl-specifiers. */
923 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
925 static void
926 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
928 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
931 /* Declarators. */
933 /* Nothing other than the parser should be creating declarators;
934 declarators are a semi-syntactic representation of C++ entities.
935 Other parts of the front end that need to create entities (like
936 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
938 static cp_declarator *make_call_declarator
939 (cp_declarator *, tree, cp_cv_quals, tree, tree);
940 static cp_declarator *make_array_declarator
941 (cp_declarator *, tree);
942 static cp_declarator *make_pointer_declarator
943 (cp_cv_quals, cp_declarator *);
944 static cp_declarator *make_reference_declarator
945 (cp_cv_quals, cp_declarator *, bool);
946 static cp_parameter_declarator *make_parameter_declarator
947 (cp_decl_specifier_seq *, cp_declarator *, tree);
948 static cp_declarator *make_ptrmem_declarator
949 (cp_cv_quals, tree, cp_declarator *);
951 /* An erroneous declarator. */
952 static cp_declarator *cp_error_declarator;
954 /* The obstack on which declarators and related data structures are
955 allocated. */
956 static struct obstack declarator_obstack;
958 /* Alloc BYTES from the declarator memory pool. */
960 static inline void *
961 alloc_declarator (size_t bytes)
963 return obstack_alloc (&declarator_obstack, bytes);
966 /* Allocate a declarator of the indicated KIND. Clear fields that are
967 common to all declarators. */
969 static cp_declarator *
970 make_declarator (cp_declarator_kind kind)
972 cp_declarator *declarator;
974 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
975 declarator->kind = kind;
976 declarator->attributes = NULL_TREE;
977 declarator->declarator = NULL;
978 declarator->parameter_pack_p = false;
979 declarator->id_loc = UNKNOWN_LOCATION;
981 return declarator;
984 /* Make a declarator for a generalized identifier. If
985 QUALIFYING_SCOPE is non-NULL, the identifier is
986 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
987 UNQUALIFIED_NAME. SFK indicates the kind of special function this
988 is, if any. */
990 static cp_declarator *
991 make_id_declarator (tree qualifying_scope, tree unqualified_name,
992 special_function_kind sfk)
994 cp_declarator *declarator;
996 /* It is valid to write:
998 class C { void f(); };
999 typedef C D;
1000 void D::f();
1002 The standard is not clear about whether `typedef const C D' is
1003 legal; as of 2002-09-15 the committee is considering that
1004 question. EDG 3.0 allows that syntax. Therefore, we do as
1005 well. */
1006 if (qualifying_scope && TYPE_P (qualifying_scope))
1007 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1009 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1010 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1011 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1013 declarator = make_declarator (cdk_id);
1014 declarator->u.id.qualifying_scope = qualifying_scope;
1015 declarator->u.id.unqualified_name = unqualified_name;
1016 declarator->u.id.sfk = sfk;
1018 return declarator;
1021 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1022 of modifiers such as const or volatile to apply to the pointer
1023 type, represented as identifiers. */
1025 cp_declarator *
1026 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1028 cp_declarator *declarator;
1030 declarator = make_declarator (cdk_pointer);
1031 declarator->declarator = target;
1032 declarator->u.pointer.qualifiers = cv_qualifiers;
1033 declarator->u.pointer.class_type = NULL_TREE;
1034 if (target)
1036 declarator->id_loc = target->id_loc;
1037 declarator->parameter_pack_p = target->parameter_pack_p;
1038 target->parameter_pack_p = false;
1040 else
1041 declarator->parameter_pack_p = false;
1043 return declarator;
1046 /* Like make_pointer_declarator -- but for references. */
1048 cp_declarator *
1049 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1050 bool rvalue_ref)
1052 cp_declarator *declarator;
1054 declarator = make_declarator (cdk_reference);
1055 declarator->declarator = target;
1056 declarator->u.reference.qualifiers = cv_qualifiers;
1057 declarator->u.reference.rvalue_ref = rvalue_ref;
1058 if (target)
1060 declarator->id_loc = target->id_loc;
1061 declarator->parameter_pack_p = target->parameter_pack_p;
1062 target->parameter_pack_p = false;
1064 else
1065 declarator->parameter_pack_p = false;
1067 return declarator;
1070 /* Like make_pointer_declarator -- but for a pointer to a non-static
1071 member of CLASS_TYPE. */
1073 cp_declarator *
1074 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1075 cp_declarator *pointee)
1077 cp_declarator *declarator;
1079 declarator = make_declarator (cdk_ptrmem);
1080 declarator->declarator = pointee;
1081 declarator->u.pointer.qualifiers = cv_qualifiers;
1082 declarator->u.pointer.class_type = class_type;
1084 if (pointee)
1086 declarator->parameter_pack_p = pointee->parameter_pack_p;
1087 pointee->parameter_pack_p = false;
1089 else
1090 declarator->parameter_pack_p = false;
1092 return declarator;
1095 /* Make a declarator for the function given by TARGET, with the
1096 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1097 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1098 indicates what exceptions can be thrown. */
1100 cp_declarator *
1101 make_call_declarator (cp_declarator *target,
1102 tree parms,
1103 cp_cv_quals cv_qualifiers,
1104 tree exception_specification,
1105 tree late_return_type)
1107 cp_declarator *declarator;
1109 declarator = make_declarator (cdk_function);
1110 declarator->declarator = target;
1111 declarator->u.function.parameters = parms;
1112 declarator->u.function.qualifiers = cv_qualifiers;
1113 declarator->u.function.exception_specification = exception_specification;
1114 declarator->u.function.late_return_type = late_return_type;
1115 if (target)
1117 declarator->id_loc = target->id_loc;
1118 declarator->parameter_pack_p = target->parameter_pack_p;
1119 target->parameter_pack_p = false;
1121 else
1122 declarator->parameter_pack_p = false;
1124 return declarator;
1127 /* Make a declarator for an array of BOUNDS elements, each of which is
1128 defined by ELEMENT. */
1130 cp_declarator *
1131 make_array_declarator (cp_declarator *element, tree bounds)
1133 cp_declarator *declarator;
1135 declarator = make_declarator (cdk_array);
1136 declarator->declarator = element;
1137 declarator->u.array.bounds = bounds;
1138 if (element)
1140 declarator->id_loc = element->id_loc;
1141 declarator->parameter_pack_p = element->parameter_pack_p;
1142 element->parameter_pack_p = false;
1144 else
1145 declarator->parameter_pack_p = false;
1147 return declarator;
1150 /* Determine whether the declarator we've seen so far can be a
1151 parameter pack, when followed by an ellipsis. */
1152 static bool
1153 declarator_can_be_parameter_pack (cp_declarator *declarator)
1155 /* Search for a declarator name, or any other declarator that goes
1156 after the point where the ellipsis could appear in a parameter
1157 pack. If we find any of these, then this declarator can not be
1158 made into a parameter pack. */
1159 bool found = false;
1160 while (declarator && !found)
1162 switch ((int)declarator->kind)
1164 case cdk_id:
1165 case cdk_array:
1166 found = true;
1167 break;
1169 case cdk_error:
1170 return true;
1172 default:
1173 declarator = declarator->declarator;
1174 break;
1178 return !found;
1181 cp_parameter_declarator *no_parameters;
1183 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1184 DECLARATOR and DEFAULT_ARGUMENT. */
1186 cp_parameter_declarator *
1187 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1188 cp_declarator *declarator,
1189 tree default_argument)
1191 cp_parameter_declarator *parameter;
1193 parameter = ((cp_parameter_declarator *)
1194 alloc_declarator (sizeof (cp_parameter_declarator)));
1195 parameter->next = NULL;
1196 if (decl_specifiers)
1197 parameter->decl_specifiers = *decl_specifiers;
1198 else
1199 clear_decl_specs (&parameter->decl_specifiers);
1200 parameter->declarator = declarator;
1201 parameter->default_argument = default_argument;
1202 parameter->ellipsis_p = false;
1204 return parameter;
1207 /* Returns true iff DECLARATOR is a declaration for a function. */
1209 static bool
1210 function_declarator_p (const cp_declarator *declarator)
1212 while (declarator)
1214 if (declarator->kind == cdk_function
1215 && declarator->declarator->kind == cdk_id)
1216 return true;
1217 if (declarator->kind == cdk_id
1218 || declarator->kind == cdk_error)
1219 return false;
1220 declarator = declarator->declarator;
1222 return false;
1225 /* The parser. */
1227 /* Overview
1228 --------
1230 A cp_parser parses the token stream as specified by the C++
1231 grammar. Its job is purely parsing, not semantic analysis. For
1232 example, the parser breaks the token stream into declarators,
1233 expressions, statements, and other similar syntactic constructs.
1234 It does not check that the types of the expressions on either side
1235 of an assignment-statement are compatible, or that a function is
1236 not declared with a parameter of type `void'.
1238 The parser invokes routines elsewhere in the compiler to perform
1239 semantic analysis and to build up the abstract syntax tree for the
1240 code processed.
1242 The parser (and the template instantiation code, which is, in a
1243 way, a close relative of parsing) are the only parts of the
1244 compiler that should be calling push_scope and pop_scope, or
1245 related functions. The parser (and template instantiation code)
1246 keeps track of what scope is presently active; everything else
1247 should simply honor that. (The code that generates static
1248 initializers may also need to set the scope, in order to check
1249 access control correctly when emitting the initializers.)
1251 Methodology
1252 -----------
1254 The parser is of the standard recursive-descent variety. Upcoming
1255 tokens in the token stream are examined in order to determine which
1256 production to use when parsing a non-terminal. Some C++ constructs
1257 require arbitrary look ahead to disambiguate. For example, it is
1258 impossible, in the general case, to tell whether a statement is an
1259 expression or declaration without scanning the entire statement.
1260 Therefore, the parser is capable of "parsing tentatively." When the
1261 parser is not sure what construct comes next, it enters this mode.
1262 Then, while we attempt to parse the construct, the parser queues up
1263 error messages, rather than issuing them immediately, and saves the
1264 tokens it consumes. If the construct is parsed successfully, the
1265 parser "commits", i.e., it issues any queued error messages and
1266 the tokens that were being preserved are permanently discarded.
1267 If, however, the construct is not parsed successfully, the parser
1268 rolls back its state completely so that it can resume parsing using
1269 a different alternative.
1271 Future Improvements
1272 -------------------
1274 The performance of the parser could probably be improved substantially.
1275 We could often eliminate the need to parse tentatively by looking ahead
1276 a little bit. In some places, this approach might not entirely eliminate
1277 the need to parse tentatively, but it might still speed up the average
1278 case. */
1280 /* Flags that are passed to some parsing functions. These values can
1281 be bitwise-ored together. */
1283 enum
1285 /* No flags. */
1286 CP_PARSER_FLAGS_NONE = 0x0,
1287 /* The construct is optional. If it is not present, then no error
1288 should be issued. */
1289 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1290 /* When parsing a type-specifier, treat user-defined type-names
1291 as non-type identifiers. */
1292 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1293 /* When parsing a type-specifier, do not try to parse a class-specifier
1294 or enum-specifier. */
1295 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1296 /* When parsing a decl-specifier-seq, only allow type-specifier or
1297 constexpr. */
1298 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1301 /* This type is used for parameters and variables which hold
1302 combinations of the above flags. */
1303 typedef int cp_parser_flags;
1305 /* The different kinds of declarators we want to parse. */
1307 typedef enum cp_parser_declarator_kind
1309 /* We want an abstract declarator. */
1310 CP_PARSER_DECLARATOR_ABSTRACT,
1311 /* We want a named declarator. */
1312 CP_PARSER_DECLARATOR_NAMED,
1313 /* We don't mind, but the name must be an unqualified-id. */
1314 CP_PARSER_DECLARATOR_EITHER
1315 } cp_parser_declarator_kind;
1317 /* The precedence values used to parse binary expressions. The minimum value
1318 of PREC must be 1, because zero is reserved to quickly discriminate
1319 binary operators from other tokens. */
1321 enum cp_parser_prec
1323 PREC_NOT_OPERATOR,
1324 PREC_LOGICAL_OR_EXPRESSION,
1325 PREC_LOGICAL_AND_EXPRESSION,
1326 PREC_INCLUSIVE_OR_EXPRESSION,
1327 PREC_EXCLUSIVE_OR_EXPRESSION,
1328 PREC_AND_EXPRESSION,
1329 PREC_EQUALITY_EXPRESSION,
1330 PREC_RELATIONAL_EXPRESSION,
1331 PREC_SHIFT_EXPRESSION,
1332 PREC_ADDITIVE_EXPRESSION,
1333 PREC_MULTIPLICATIVE_EXPRESSION,
1334 PREC_PM_EXPRESSION,
1335 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1338 /* A mapping from a token type to a corresponding tree node type, with a
1339 precedence value. */
1341 typedef struct cp_parser_binary_operations_map_node
1343 /* The token type. */
1344 enum cpp_ttype token_type;
1345 /* The corresponding tree code. */
1346 enum tree_code tree_type;
1347 /* The precedence of this operator. */
1348 enum cp_parser_prec prec;
1349 } cp_parser_binary_operations_map_node;
1351 typedef struct cp_parser_expression_stack_entry
1353 /* Left hand side of the binary operation we are currently
1354 parsing. */
1355 tree lhs;
1356 /* Original tree code for left hand side, if it was a binary
1357 expression itself (used for -Wparentheses). */
1358 enum tree_code lhs_type;
1359 /* Tree code for the binary operation we are parsing. */
1360 enum tree_code tree_type;
1361 /* Precedence of the binary operation we are parsing. */
1362 enum cp_parser_prec prec;
1363 } cp_parser_expression_stack_entry;
1365 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1366 entries because precedence levels on the stack are monotonically
1367 increasing. */
1368 typedef struct cp_parser_expression_stack_entry
1369 cp_parser_expression_stack[NUM_PREC_VALUES];
1371 /* Prototypes. */
1373 /* Constructors and destructors. */
1375 static cp_parser_context *cp_parser_context_new
1376 (cp_parser_context *);
1378 /* Class variables. */
1380 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1382 /* The operator-precedence table used by cp_parser_binary_expression.
1383 Transformed into an associative array (binops_by_token) by
1384 cp_parser_new. */
1386 static const cp_parser_binary_operations_map_node binops[] = {
1387 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1388 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1390 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1391 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1392 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1394 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1395 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1397 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1398 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1400 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1401 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1402 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1403 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1405 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1406 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1408 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1410 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1412 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1414 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1416 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1419 /* The same as binops, but initialized by cp_parser_new so that
1420 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1421 for speed. */
1422 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1424 /* Constructors and destructors. */
1426 /* Construct a new context. The context below this one on the stack
1427 is given by NEXT. */
1429 static cp_parser_context *
1430 cp_parser_context_new (cp_parser_context* next)
1432 cp_parser_context *context;
1434 /* Allocate the storage. */
1435 if (cp_parser_context_free_list != NULL)
1437 /* Pull the first entry from the free list. */
1438 context = cp_parser_context_free_list;
1439 cp_parser_context_free_list = context->next;
1440 memset (context, 0, sizeof (*context));
1442 else
1443 context = ggc_alloc_cleared_cp_parser_context ();
1445 /* No errors have occurred yet in this context. */
1446 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1447 /* If this is not the bottommost context, copy information that we
1448 need from the previous context. */
1449 if (next)
1451 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1452 expression, then we are parsing one in this context, too. */
1453 context->object_type = next->object_type;
1454 /* Thread the stack. */
1455 context->next = next;
1458 return context;
1461 /* Managing the unparsed function queues. */
1463 #define unparsed_funs_with_default_args \
1464 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1465 #define unparsed_funs_with_definitions \
1466 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1468 static void
1469 push_unparsed_function_queues (cp_parser *parser)
1471 VEC_safe_push (cp_unparsed_functions_entry, gc,
1472 parser->unparsed_queues, NULL);
1473 unparsed_funs_with_default_args = NULL;
1474 unparsed_funs_with_definitions = make_tree_vector ();
1477 static void
1478 pop_unparsed_function_queues (cp_parser *parser)
1480 release_tree_vector (unparsed_funs_with_definitions);
1481 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1484 /* Prototypes. */
1486 /* Constructors and destructors. */
1488 static cp_parser *cp_parser_new
1489 (void);
1491 /* Routines to parse various constructs.
1493 Those that return `tree' will return the error_mark_node (rather
1494 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1495 Sometimes, they will return an ordinary node if error-recovery was
1496 attempted, even though a parse error occurred. So, to check
1497 whether or not a parse error occurred, you should always use
1498 cp_parser_error_occurred. If the construct is optional (indicated
1499 either by an `_opt' in the name of the function that does the
1500 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1501 the construct is not present. */
1503 /* Lexical conventions [gram.lex] */
1505 static tree cp_parser_identifier
1506 (cp_parser *);
1507 static tree cp_parser_string_literal
1508 (cp_parser *, bool, bool);
1510 /* Basic concepts [gram.basic] */
1512 static bool cp_parser_translation_unit
1513 (cp_parser *);
1515 /* Expressions [gram.expr] */
1517 static tree cp_parser_primary_expression
1518 (cp_parser *, bool, bool, bool, cp_id_kind *);
1519 static tree cp_parser_id_expression
1520 (cp_parser *, bool, bool, bool *, bool, bool);
1521 static tree cp_parser_unqualified_id
1522 (cp_parser *, bool, bool, bool, bool);
1523 static tree cp_parser_nested_name_specifier_opt
1524 (cp_parser *, bool, bool, bool, bool);
1525 static tree cp_parser_nested_name_specifier
1526 (cp_parser *, bool, bool, bool, bool);
1527 static tree cp_parser_qualifying_entity
1528 (cp_parser *, bool, bool, bool, bool, bool);
1529 static tree cp_parser_postfix_expression
1530 (cp_parser *, bool, bool, bool, cp_id_kind *);
1531 static tree cp_parser_postfix_open_square_expression
1532 (cp_parser *, tree, bool);
1533 static tree cp_parser_postfix_dot_deref_expression
1534 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1535 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1536 (cp_parser *, int, bool, bool, bool *);
1537 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1538 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1539 static void cp_parser_pseudo_destructor_name
1540 (cp_parser *, tree *, tree *);
1541 static tree cp_parser_unary_expression
1542 (cp_parser *, bool, bool, cp_id_kind *);
1543 static enum tree_code cp_parser_unary_operator
1544 (cp_token *);
1545 static tree cp_parser_new_expression
1546 (cp_parser *);
1547 static VEC(tree,gc) *cp_parser_new_placement
1548 (cp_parser *);
1549 static tree cp_parser_new_type_id
1550 (cp_parser *, tree *);
1551 static cp_declarator *cp_parser_new_declarator_opt
1552 (cp_parser *);
1553 static cp_declarator *cp_parser_direct_new_declarator
1554 (cp_parser *);
1555 static VEC(tree,gc) *cp_parser_new_initializer
1556 (cp_parser *);
1557 static tree cp_parser_delete_expression
1558 (cp_parser *);
1559 static tree cp_parser_cast_expression
1560 (cp_parser *, bool, bool, cp_id_kind *);
1561 static tree cp_parser_binary_expression
1562 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1563 static tree cp_parser_question_colon_clause
1564 (cp_parser *, tree);
1565 static tree cp_parser_assignment_expression
1566 (cp_parser *, bool, cp_id_kind *);
1567 static enum tree_code cp_parser_assignment_operator_opt
1568 (cp_parser *);
1569 static tree cp_parser_expression
1570 (cp_parser *, bool, cp_id_kind *);
1571 static tree cp_parser_constant_expression
1572 (cp_parser *, bool, bool *);
1573 static tree cp_parser_builtin_offsetof
1574 (cp_parser *);
1575 static tree cp_parser_lambda_expression
1576 (cp_parser *);
1577 static void cp_parser_lambda_introducer
1578 (cp_parser *, tree);
1579 static void cp_parser_lambda_declarator_opt
1580 (cp_parser *, tree);
1581 static void cp_parser_lambda_body
1582 (cp_parser *, tree);
1584 /* Statements [gram.stmt.stmt] */
1586 static void cp_parser_statement
1587 (cp_parser *, tree, bool, bool *);
1588 static void cp_parser_label_for_labeled_statement
1589 (cp_parser *);
1590 static tree cp_parser_expression_statement
1591 (cp_parser *, tree);
1592 static tree cp_parser_compound_statement
1593 (cp_parser *, tree, bool, bool);
1594 static void cp_parser_statement_seq_opt
1595 (cp_parser *, tree);
1596 static tree cp_parser_selection_statement
1597 (cp_parser *, bool *);
1598 static tree cp_parser_condition
1599 (cp_parser *);
1600 static tree cp_parser_iteration_statement
1601 (cp_parser *);
1602 static bool cp_parser_for_init_statement
1603 (cp_parser *, tree *decl);
1604 static tree cp_parser_for
1605 (cp_parser *);
1606 static tree cp_parser_c_for
1607 (cp_parser *, tree, tree);
1608 static tree cp_parser_range_for
1609 (cp_parser *, tree, tree, tree);
1610 static tree cp_parser_jump_statement
1611 (cp_parser *);
1612 static void cp_parser_declaration_statement
1613 (cp_parser *);
1615 static tree cp_parser_implicitly_scoped_statement
1616 (cp_parser *, bool *);
1617 static void cp_parser_already_scoped_statement
1618 (cp_parser *);
1620 /* Declarations [gram.dcl.dcl] */
1622 static void cp_parser_declaration_seq_opt
1623 (cp_parser *);
1624 static void cp_parser_declaration
1625 (cp_parser *);
1626 static void cp_parser_block_declaration
1627 (cp_parser *, bool);
1628 static void cp_parser_simple_declaration
1629 (cp_parser *, bool, tree *);
1630 static void cp_parser_decl_specifier_seq
1631 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1632 static tree cp_parser_storage_class_specifier_opt
1633 (cp_parser *);
1634 static tree cp_parser_function_specifier_opt
1635 (cp_parser *, cp_decl_specifier_seq *);
1636 static tree cp_parser_type_specifier
1637 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1638 int *, bool *);
1639 static tree cp_parser_simple_type_specifier
1640 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1641 static tree cp_parser_type_name
1642 (cp_parser *);
1643 static tree cp_parser_nonclass_name
1644 (cp_parser* parser);
1645 static tree cp_parser_elaborated_type_specifier
1646 (cp_parser *, bool, bool);
1647 static tree cp_parser_enum_specifier
1648 (cp_parser *);
1649 static void cp_parser_enumerator_list
1650 (cp_parser *, tree);
1651 static void cp_parser_enumerator_definition
1652 (cp_parser *, tree);
1653 static tree cp_parser_namespace_name
1654 (cp_parser *);
1655 static void cp_parser_namespace_definition
1656 (cp_parser *);
1657 static void cp_parser_namespace_body
1658 (cp_parser *);
1659 static tree cp_parser_qualified_namespace_specifier
1660 (cp_parser *);
1661 static void cp_parser_namespace_alias_definition
1662 (cp_parser *);
1663 static bool cp_parser_using_declaration
1664 (cp_parser *, bool);
1665 static void cp_parser_using_directive
1666 (cp_parser *);
1667 static void cp_parser_asm_definition
1668 (cp_parser *);
1669 static void cp_parser_linkage_specification
1670 (cp_parser *);
1671 static void cp_parser_static_assert
1672 (cp_parser *, bool);
1673 static tree cp_parser_decltype
1674 (cp_parser *);
1676 /* Declarators [gram.dcl.decl] */
1678 static tree cp_parser_init_declarator
1679 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1680 static cp_declarator *cp_parser_declarator
1681 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1682 static cp_declarator *cp_parser_direct_declarator
1683 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1684 static enum tree_code cp_parser_ptr_operator
1685 (cp_parser *, tree *, cp_cv_quals *);
1686 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1687 (cp_parser *);
1688 static tree cp_parser_late_return_type_opt
1689 (cp_parser *);
1690 static tree cp_parser_declarator_id
1691 (cp_parser *, bool);
1692 static tree cp_parser_type_id
1693 (cp_parser *);
1694 static tree cp_parser_template_type_arg
1695 (cp_parser *);
1696 static tree cp_parser_trailing_type_id (cp_parser *);
1697 static tree cp_parser_type_id_1
1698 (cp_parser *, bool, bool);
1699 static void cp_parser_type_specifier_seq
1700 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1701 static tree cp_parser_parameter_declaration_clause
1702 (cp_parser *);
1703 static tree cp_parser_parameter_declaration_list
1704 (cp_parser *, bool *);
1705 static cp_parameter_declarator *cp_parser_parameter_declaration
1706 (cp_parser *, bool, bool *);
1707 static tree cp_parser_default_argument
1708 (cp_parser *, bool);
1709 static void cp_parser_function_body
1710 (cp_parser *);
1711 static tree cp_parser_initializer
1712 (cp_parser *, bool *, bool *);
1713 static tree cp_parser_initializer_clause
1714 (cp_parser *, bool *);
1715 static tree cp_parser_braced_list
1716 (cp_parser*, bool*);
1717 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1718 (cp_parser *, bool *);
1720 static bool cp_parser_ctor_initializer_opt_and_function_body
1721 (cp_parser *);
1723 /* Classes [gram.class] */
1725 static tree cp_parser_class_name
1726 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1727 static tree cp_parser_class_specifier
1728 (cp_parser *);
1729 static tree cp_parser_class_head
1730 (cp_parser *, bool *, tree *, tree *);
1731 static enum tag_types cp_parser_class_key
1732 (cp_parser *);
1733 static void cp_parser_member_specification_opt
1734 (cp_parser *);
1735 static void cp_parser_member_declaration
1736 (cp_parser *);
1737 static tree cp_parser_pure_specifier
1738 (cp_parser *);
1739 static tree cp_parser_constant_initializer
1740 (cp_parser *);
1742 /* Derived classes [gram.class.derived] */
1744 static tree cp_parser_base_clause
1745 (cp_parser *);
1746 static tree cp_parser_base_specifier
1747 (cp_parser *);
1749 /* Special member functions [gram.special] */
1751 static tree cp_parser_conversion_function_id
1752 (cp_parser *);
1753 static tree cp_parser_conversion_type_id
1754 (cp_parser *);
1755 static cp_declarator *cp_parser_conversion_declarator_opt
1756 (cp_parser *);
1757 static bool cp_parser_ctor_initializer_opt
1758 (cp_parser *);
1759 static void cp_parser_mem_initializer_list
1760 (cp_parser *);
1761 static tree cp_parser_mem_initializer
1762 (cp_parser *);
1763 static tree cp_parser_mem_initializer_id
1764 (cp_parser *);
1766 /* Overloading [gram.over] */
1768 static tree cp_parser_operator_function_id
1769 (cp_parser *);
1770 static tree cp_parser_operator
1771 (cp_parser *);
1773 /* Templates [gram.temp] */
1775 static void cp_parser_template_declaration
1776 (cp_parser *, bool);
1777 static tree cp_parser_template_parameter_list
1778 (cp_parser *);
1779 static tree cp_parser_template_parameter
1780 (cp_parser *, bool *, bool *);
1781 static tree cp_parser_type_parameter
1782 (cp_parser *, bool *);
1783 static tree cp_parser_template_id
1784 (cp_parser *, bool, bool, bool);
1785 static tree cp_parser_template_name
1786 (cp_parser *, bool, bool, bool, bool *);
1787 static tree cp_parser_template_argument_list
1788 (cp_parser *);
1789 static tree cp_parser_template_argument
1790 (cp_parser *);
1791 static void cp_parser_explicit_instantiation
1792 (cp_parser *);
1793 static void cp_parser_explicit_specialization
1794 (cp_parser *);
1796 /* Exception handling [gram.exception] */
1798 static tree cp_parser_try_block
1799 (cp_parser *);
1800 static bool cp_parser_function_try_block
1801 (cp_parser *);
1802 static void cp_parser_handler_seq
1803 (cp_parser *);
1804 static void cp_parser_handler
1805 (cp_parser *);
1806 static tree cp_parser_exception_declaration
1807 (cp_parser *);
1808 static tree cp_parser_throw_expression
1809 (cp_parser *);
1810 static tree cp_parser_exception_specification_opt
1811 (cp_parser *);
1812 static tree cp_parser_type_id_list
1813 (cp_parser *);
1815 /* GNU Extensions */
1817 static tree cp_parser_asm_specification_opt
1818 (cp_parser *);
1819 static tree cp_parser_asm_operand_list
1820 (cp_parser *);
1821 static tree cp_parser_asm_clobber_list
1822 (cp_parser *);
1823 static tree cp_parser_asm_label_list
1824 (cp_parser *);
1825 static tree cp_parser_attributes_opt
1826 (cp_parser *);
1827 static tree cp_parser_attribute_list
1828 (cp_parser *);
1829 static bool cp_parser_extension_opt
1830 (cp_parser *, int *);
1831 static void cp_parser_label_declaration
1832 (cp_parser *);
1834 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1835 static bool cp_parser_pragma
1836 (cp_parser *, enum pragma_context);
1838 /* Objective-C++ Productions */
1840 static tree cp_parser_objc_message_receiver
1841 (cp_parser *);
1842 static tree cp_parser_objc_message_args
1843 (cp_parser *);
1844 static tree cp_parser_objc_message_expression
1845 (cp_parser *);
1846 static tree cp_parser_objc_encode_expression
1847 (cp_parser *);
1848 static tree cp_parser_objc_defs_expression
1849 (cp_parser *);
1850 static tree cp_parser_objc_protocol_expression
1851 (cp_parser *);
1852 static tree cp_parser_objc_selector_expression
1853 (cp_parser *);
1854 static tree cp_parser_objc_expression
1855 (cp_parser *);
1856 static bool cp_parser_objc_selector_p
1857 (enum cpp_ttype);
1858 static tree cp_parser_objc_selector
1859 (cp_parser *);
1860 static tree cp_parser_objc_protocol_refs_opt
1861 (cp_parser *);
1862 static void cp_parser_objc_declaration
1863 (cp_parser *, tree);
1864 static tree cp_parser_objc_statement
1865 (cp_parser *);
1866 static bool cp_parser_objc_valid_prefix_attributes
1867 (cp_parser *, tree *);
1868 static void cp_parser_objc_at_property_declaration
1869 (cp_parser *) ;
1870 static void cp_parser_objc_at_synthesize_declaration
1871 (cp_parser *) ;
1872 static void cp_parser_objc_at_dynamic_declaration
1873 (cp_parser *) ;
1874 static tree cp_parser_objc_struct_declaration
1875 (cp_parser *) ;
1877 /* Utility Routines */
1879 static tree cp_parser_lookup_name
1880 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1881 static tree cp_parser_lookup_name_simple
1882 (cp_parser *, tree, location_t);
1883 static tree cp_parser_maybe_treat_template_as_class
1884 (tree, bool);
1885 static bool cp_parser_check_declarator_template_parameters
1886 (cp_parser *, cp_declarator *, location_t);
1887 static bool cp_parser_check_template_parameters
1888 (cp_parser *, unsigned, location_t, cp_declarator *);
1889 static tree cp_parser_simple_cast_expression
1890 (cp_parser *);
1891 static tree cp_parser_global_scope_opt
1892 (cp_parser *, bool);
1893 static bool cp_parser_constructor_declarator_p
1894 (cp_parser *, bool);
1895 static tree cp_parser_function_definition_from_specifiers_and_declarator
1896 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1897 static tree cp_parser_function_definition_after_declarator
1898 (cp_parser *, bool);
1899 static void cp_parser_template_declaration_after_export
1900 (cp_parser *, bool);
1901 static void cp_parser_perform_template_parameter_access_checks
1902 (VEC (deferred_access_check,gc)*);
1903 static tree cp_parser_single_declaration
1904 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1905 static tree cp_parser_functional_cast
1906 (cp_parser *, tree);
1907 static tree cp_parser_save_member_function_body
1908 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1909 static tree cp_parser_enclosed_template_argument_list
1910 (cp_parser *);
1911 static void cp_parser_save_default_args
1912 (cp_parser *, tree);
1913 static void cp_parser_late_parsing_for_member
1914 (cp_parser *, tree);
1915 static void cp_parser_late_parsing_default_args
1916 (cp_parser *, tree);
1917 static tree cp_parser_sizeof_operand
1918 (cp_parser *, enum rid);
1919 static tree cp_parser_trait_expr
1920 (cp_parser *, enum rid);
1921 static bool cp_parser_declares_only_class_p
1922 (cp_parser *);
1923 static void cp_parser_set_storage_class
1924 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1925 static void cp_parser_set_decl_spec_type
1926 (cp_decl_specifier_seq *, tree, location_t, bool);
1927 static bool cp_parser_friend_p
1928 (const cp_decl_specifier_seq *);
1929 static void cp_parser_required_error
1930 (cp_parser *, required_token, bool);
1931 static cp_token *cp_parser_require
1932 (cp_parser *, enum cpp_ttype, required_token);
1933 static cp_token *cp_parser_require_keyword
1934 (cp_parser *, enum rid, required_token);
1935 static bool cp_parser_token_starts_function_definition_p
1936 (cp_token *);
1937 static bool cp_parser_next_token_starts_class_definition_p
1938 (cp_parser *);
1939 static bool cp_parser_next_token_ends_template_argument_p
1940 (cp_parser *);
1941 static bool cp_parser_nth_token_starts_template_argument_list_p
1942 (cp_parser *, size_t);
1943 static enum tag_types cp_parser_token_is_class_key
1944 (cp_token *);
1945 static void cp_parser_check_class_key
1946 (enum tag_types, tree type);
1947 static void cp_parser_check_access_in_redeclaration
1948 (tree type, location_t location);
1949 static bool cp_parser_optional_template_keyword
1950 (cp_parser *);
1951 static void cp_parser_pre_parsed_nested_name_specifier
1952 (cp_parser *);
1953 static bool cp_parser_cache_group
1954 (cp_parser *, enum cpp_ttype, unsigned);
1955 static void cp_parser_parse_tentatively
1956 (cp_parser *);
1957 static void cp_parser_commit_to_tentative_parse
1958 (cp_parser *);
1959 static void cp_parser_abort_tentative_parse
1960 (cp_parser *);
1961 static bool cp_parser_parse_definitely
1962 (cp_parser *);
1963 static inline bool cp_parser_parsing_tentatively
1964 (cp_parser *);
1965 static bool cp_parser_uncommitted_to_tentative_parse_p
1966 (cp_parser *);
1967 static void cp_parser_error
1968 (cp_parser *, const char *);
1969 static void cp_parser_name_lookup_error
1970 (cp_parser *, tree, tree, name_lookup_error, location_t);
1971 static bool cp_parser_simulate_error
1972 (cp_parser *);
1973 static bool cp_parser_check_type_definition
1974 (cp_parser *);
1975 static void cp_parser_check_for_definition_in_return_type
1976 (cp_declarator *, tree, location_t type_location);
1977 static void cp_parser_check_for_invalid_template_id
1978 (cp_parser *, tree, location_t location);
1979 static bool cp_parser_non_integral_constant_expression
1980 (cp_parser *, non_integral_constant);
1981 static void cp_parser_diagnose_invalid_type_name
1982 (cp_parser *, tree, tree, location_t);
1983 static bool cp_parser_parse_and_diagnose_invalid_type_name
1984 (cp_parser *);
1985 static int cp_parser_skip_to_closing_parenthesis
1986 (cp_parser *, bool, bool, bool);
1987 static void cp_parser_skip_to_end_of_statement
1988 (cp_parser *);
1989 static void cp_parser_consume_semicolon_at_end_of_statement
1990 (cp_parser *);
1991 static void cp_parser_skip_to_end_of_block_or_statement
1992 (cp_parser *);
1993 static bool cp_parser_skip_to_closing_brace
1994 (cp_parser *);
1995 static void cp_parser_skip_to_end_of_template_parameter_list
1996 (cp_parser *);
1997 static void cp_parser_skip_to_pragma_eol
1998 (cp_parser*, cp_token *);
1999 static bool cp_parser_error_occurred
2000 (cp_parser *);
2001 static bool cp_parser_allow_gnu_extensions_p
2002 (cp_parser *);
2003 static bool cp_parser_is_string_literal
2004 (cp_token *);
2005 static bool cp_parser_is_keyword
2006 (cp_token *, enum rid);
2007 static tree cp_parser_make_typename_type
2008 (cp_parser *, tree, tree, location_t location);
2009 static cp_declarator * cp_parser_make_indirect_declarator
2010 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2012 /* Returns nonzero if we are parsing tentatively. */
2014 static inline bool
2015 cp_parser_parsing_tentatively (cp_parser* parser)
2017 return parser->context->next != NULL;
2020 /* Returns nonzero if TOKEN is a string literal. */
2022 static bool
2023 cp_parser_is_string_literal (cp_token* token)
2025 return (token->type == CPP_STRING ||
2026 token->type == CPP_STRING16 ||
2027 token->type == CPP_STRING32 ||
2028 token->type == CPP_WSTRING ||
2029 token->type == CPP_UTF8STRING);
2032 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2034 static bool
2035 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2037 return token->keyword == keyword;
2040 /* If not parsing tentatively, issue a diagnostic of the form
2041 FILE:LINE: MESSAGE before TOKEN
2042 where TOKEN is the next token in the input stream. MESSAGE
2043 (specified by the caller) is usually of the form "expected
2044 OTHER-TOKEN". */
2046 static void
2047 cp_parser_error (cp_parser* parser, const char* gmsgid)
2049 if (!cp_parser_simulate_error (parser))
2051 cp_token *token = cp_lexer_peek_token (parser->lexer);
2052 /* This diagnostic makes more sense if it is tagged to the line
2053 of the token we just peeked at. */
2054 cp_lexer_set_source_position_from_token (token);
2056 if (token->type == CPP_PRAGMA)
2058 error_at (token->location,
2059 "%<#pragma%> is not allowed here");
2060 cp_parser_skip_to_pragma_eol (parser, token);
2061 return;
2064 c_parse_error (gmsgid,
2065 /* Because c_parser_error does not understand
2066 CPP_KEYWORD, keywords are treated like
2067 identifiers. */
2068 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2069 token->u.value, token->flags);
2073 /* Issue an error about name-lookup failing. NAME is the
2074 IDENTIFIER_NODE DECL is the result of
2075 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2076 the thing that we hoped to find. */
2078 static void
2079 cp_parser_name_lookup_error (cp_parser* parser,
2080 tree name,
2081 tree decl,
2082 name_lookup_error desired,
2083 location_t location)
2085 /* If name lookup completely failed, tell the user that NAME was not
2086 declared. */
2087 if (decl == error_mark_node)
2089 if (parser->scope && parser->scope != global_namespace)
2090 error_at (location, "%<%E::%E%> has not been declared",
2091 parser->scope, name);
2092 else if (parser->scope == global_namespace)
2093 error_at (location, "%<::%E%> has not been declared", name);
2094 else if (parser->object_scope
2095 && !CLASS_TYPE_P (parser->object_scope))
2096 error_at (location, "request for member %qE in non-class type %qT",
2097 name, parser->object_scope);
2098 else if (parser->object_scope)
2099 error_at (location, "%<%T::%E%> has not been declared",
2100 parser->object_scope, name);
2101 else
2102 error_at (location, "%qE has not been declared", name);
2104 else if (parser->scope && parser->scope != global_namespace)
2106 switch (desired)
2108 case NLE_TYPE:
2109 error_at (location, "%<%E::%E%> is not a type",
2110 parser->scope, name);
2111 break;
2112 case NLE_CXX98:
2113 error_at (location, "%<%E::%E%> is not a class or namespace",
2114 parser->scope, name);
2115 break;
2116 case NLE_NOT_CXX98:
2117 error_at (location,
2118 "%<%E::%E%> is not a class, namespace, or enumeration",
2119 parser->scope, name);
2120 break;
2121 default:
2122 gcc_unreachable ();
2126 else if (parser->scope == global_namespace)
2128 switch (desired)
2130 case NLE_TYPE:
2131 error_at (location, "%<::%E%> is not a type", name);
2132 break;
2133 case NLE_CXX98:
2134 error_at (location, "%<::%E%> is not a class or namespace", name);
2135 break;
2136 case NLE_NOT_CXX98:
2137 error_at (location,
2138 "%<::%E%> is not a class, namespace, or enumeration",
2139 name);
2140 break;
2141 default:
2142 gcc_unreachable ();
2145 else
2147 switch (desired)
2149 case NLE_TYPE:
2150 error_at (location, "%qE is not a type", name);
2151 break;
2152 case NLE_CXX98:
2153 error_at (location, "%qE is not a class or namespace", name);
2154 break;
2155 case NLE_NOT_CXX98:
2156 error_at (location,
2157 "%qE is not a class, namespace, or enumeration", name);
2158 break;
2159 default:
2160 gcc_unreachable ();
2165 /* If we are parsing tentatively, remember that an error has occurred
2166 during this tentative parse. Returns true if the error was
2167 simulated; false if a message should be issued by the caller. */
2169 static bool
2170 cp_parser_simulate_error (cp_parser* parser)
2172 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2174 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2175 return true;
2177 return false;
2180 /* Check for repeated decl-specifiers. */
2182 static void
2183 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2184 location_t location)
2186 int ds;
2188 for (ds = ds_first; ds != ds_last; ++ds)
2190 unsigned count = decl_specs->specs[ds];
2191 if (count < 2)
2192 continue;
2193 /* The "long" specifier is a special case because of "long long". */
2194 if (ds == ds_long)
2196 if (count > 2)
2197 error_at (location, "%<long long long%> is too long for GCC");
2198 else
2199 pedwarn_cxx98 (location, OPT_Wlong_long,
2200 "ISO C++ 1998 does not support %<long long%>");
2202 else if (count > 1)
2204 static const char *const decl_spec_names[] = {
2205 "signed",
2206 "unsigned",
2207 "short",
2208 "long",
2209 "const",
2210 "volatile",
2211 "restrict",
2212 "inline",
2213 "virtual",
2214 "explicit",
2215 "friend",
2216 "typedef",
2217 "constexpr",
2218 "__complex",
2219 "__thread"
2221 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2226 /* This function is called when a type is defined. If type
2227 definitions are forbidden at this point, an error message is
2228 issued. */
2230 static bool
2231 cp_parser_check_type_definition (cp_parser* parser)
2233 /* If types are forbidden here, issue a message. */
2234 if (parser->type_definition_forbidden_message)
2236 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2237 in the message need to be interpreted. */
2238 error (parser->type_definition_forbidden_message);
2239 return false;
2241 return true;
2244 /* This function is called when the DECLARATOR is processed. The TYPE
2245 was a type defined in the decl-specifiers. If it is invalid to
2246 define a type in the decl-specifiers for DECLARATOR, an error is
2247 issued. TYPE_LOCATION is the location of TYPE and is used
2248 for error reporting. */
2250 static void
2251 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2252 tree type, location_t type_location)
2254 /* [dcl.fct] forbids type definitions in return types.
2255 Unfortunately, it's not easy to know whether or not we are
2256 processing a return type until after the fact. */
2257 while (declarator
2258 && (declarator->kind == cdk_pointer
2259 || declarator->kind == cdk_reference
2260 || declarator->kind == cdk_ptrmem))
2261 declarator = declarator->declarator;
2262 if (declarator
2263 && declarator->kind == cdk_function)
2265 error_at (type_location,
2266 "new types may not be defined in a return type");
2267 inform (type_location,
2268 "(perhaps a semicolon is missing after the definition of %qT)",
2269 type);
2273 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2274 "<" in any valid C++ program. If the next token is indeed "<",
2275 issue a message warning the user about what appears to be an
2276 invalid attempt to form a template-id. LOCATION is the location
2277 of the type-specifier (TYPE) */
2279 static void
2280 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2281 tree type, location_t location)
2283 cp_token_position start = 0;
2285 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2287 if (TYPE_P (type))
2288 error_at (location, "%qT is not a template", type);
2289 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2290 error_at (location, "%qE is not a template", type);
2291 else
2292 error_at (location, "invalid template-id");
2293 /* Remember the location of the invalid "<". */
2294 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2295 start = cp_lexer_token_position (parser->lexer, true);
2296 /* Consume the "<". */
2297 cp_lexer_consume_token (parser->lexer);
2298 /* Parse the template arguments. */
2299 cp_parser_enclosed_template_argument_list (parser);
2300 /* Permanently remove the invalid template arguments so that
2301 this error message is not issued again. */
2302 if (start)
2303 cp_lexer_purge_tokens_after (parser->lexer, start);
2307 /* If parsing an integral constant-expression, issue an error message
2308 about the fact that THING appeared and return true. Otherwise,
2309 return false. In either case, set
2310 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2312 static bool
2313 cp_parser_non_integral_constant_expression (cp_parser *parser,
2314 non_integral_constant thing)
2316 parser->non_integral_constant_expression_p = true;
2317 if (parser->integral_constant_expression_p)
2319 if (!parser->allow_non_integral_constant_expression_p)
2321 const char *msg = NULL;
2322 switch (thing)
2324 case NIC_FLOAT:
2325 error ("floating-point literal "
2326 "cannot appear in a constant-expression");
2327 return true;
2328 case NIC_CAST:
2329 error ("a cast to a type other than an integral or "
2330 "enumeration type cannot appear in a "
2331 "constant-expression");
2332 return true;
2333 case NIC_TYPEID:
2334 error ("%<typeid%> operator "
2335 "cannot appear in a constant-expression");
2336 return true;
2337 case NIC_NCC:
2338 error ("non-constant compound literals "
2339 "cannot appear in a constant-expression");
2340 return true;
2341 case NIC_FUNC_CALL:
2342 error ("a function call "
2343 "cannot appear in a constant-expression");
2344 return true;
2345 case NIC_INC:
2346 error ("an increment "
2347 "cannot appear in a constant-expression");
2348 return true;
2349 case NIC_DEC:
2350 error ("an decrement "
2351 "cannot appear in a constant-expression");
2352 return true;
2353 case NIC_ARRAY_REF:
2354 error ("an array reference "
2355 "cannot appear in a constant-expression");
2356 return true;
2357 case NIC_ADDR_LABEL:
2358 error ("the address of a label "
2359 "cannot appear in a constant-expression");
2360 return true;
2361 case NIC_OVERLOADED:
2362 error ("calls to overloaded operators "
2363 "cannot appear in a constant-expression");
2364 return true;
2365 case NIC_ASSIGNMENT:
2366 error ("an assignment cannot appear in a constant-expression");
2367 return true;
2368 case NIC_COMMA:
2369 error ("a comma operator "
2370 "cannot appear in a constant-expression");
2371 return true;
2372 case NIC_CONSTRUCTOR:
2373 error ("a call to a constructor "
2374 "cannot appear in a constant-expression");
2375 return true;
2376 case NIC_THIS:
2377 msg = "this";
2378 break;
2379 case NIC_FUNC_NAME:
2380 msg = "__FUNCTION__";
2381 break;
2382 case NIC_PRETTY_FUNC:
2383 msg = "__PRETTY_FUNCTION__";
2384 break;
2385 case NIC_C99_FUNC:
2386 msg = "__func__";
2387 break;
2388 case NIC_VA_ARG:
2389 msg = "va_arg";
2390 break;
2391 case NIC_ARROW:
2392 msg = "->";
2393 break;
2394 case NIC_POINT:
2395 msg = ".";
2396 break;
2397 case NIC_STAR:
2398 msg = "*";
2399 break;
2400 case NIC_ADDR:
2401 msg = "&";
2402 break;
2403 case NIC_PREINCREMENT:
2404 msg = "++";
2405 break;
2406 case NIC_PREDECREMENT:
2407 msg = "--";
2408 break;
2409 case NIC_NEW:
2410 msg = "new";
2411 break;
2412 case NIC_DEL:
2413 msg = "delete";
2414 break;
2415 default:
2416 gcc_unreachable ();
2418 if (msg)
2419 error ("%qs cannot appear in a constant-expression", msg);
2420 return true;
2423 return false;
2426 /* Emit a diagnostic for an invalid type name. SCOPE is the
2427 qualifying scope (or NULL, if none) for ID. This function commits
2428 to the current active tentative parse, if any. (Otherwise, the
2429 problematic construct might be encountered again later, resulting
2430 in duplicate error messages.) LOCATION is the location of ID. */
2432 static void
2433 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2434 tree scope, tree id,
2435 location_t location)
2437 tree decl, old_scope;
2438 /* Try to lookup the identifier. */
2439 old_scope = parser->scope;
2440 parser->scope = scope;
2441 decl = cp_parser_lookup_name_simple (parser, id, location);
2442 parser->scope = old_scope;
2443 /* If the lookup found a template-name, it means that the user forgot
2444 to specify an argument list. Emit a useful error message. */
2445 if (TREE_CODE (decl) == TEMPLATE_DECL)
2446 error_at (location,
2447 "invalid use of template-name %qE without an argument list",
2448 decl);
2449 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2450 error_at (location, "invalid use of destructor %qD as a type", id);
2451 else if (TREE_CODE (decl) == TYPE_DECL)
2452 /* Something like 'unsigned A a;' */
2453 error_at (location, "invalid combination of multiple type-specifiers");
2454 else if (!parser->scope)
2456 /* Issue an error message. */
2457 error_at (location, "%qE does not name a type", id);
2458 /* If we're in a template class, it's possible that the user was
2459 referring to a type from a base class. For example:
2461 template <typename T> struct A { typedef T X; };
2462 template <typename T> struct B : public A<T> { X x; };
2464 The user should have said "typename A<T>::X". */
2465 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2466 inform (location, "C++0x %<constexpr%> only available with "
2467 "-std=c++0x or -std=gnu++0x");
2468 else if (processing_template_decl && current_class_type
2469 && TYPE_BINFO (current_class_type))
2471 tree b;
2473 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2475 b = TREE_CHAIN (b))
2477 tree base_type = BINFO_TYPE (b);
2478 if (CLASS_TYPE_P (base_type)
2479 && dependent_type_p (base_type))
2481 tree field;
2482 /* Go from a particular instantiation of the
2483 template (which will have an empty TYPE_FIELDs),
2484 to the main version. */
2485 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2486 for (field = TYPE_FIELDS (base_type);
2487 field;
2488 field = DECL_CHAIN (field))
2489 if (TREE_CODE (field) == TYPE_DECL
2490 && DECL_NAME (field) == id)
2492 inform (location,
2493 "(perhaps %<typename %T::%E%> was intended)",
2494 BINFO_TYPE (b), id);
2495 break;
2497 if (field)
2498 break;
2503 /* Here we diagnose qualified-ids where the scope is actually correct,
2504 but the identifier does not resolve to a valid type name. */
2505 else if (parser->scope != error_mark_node)
2507 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2508 error_at (location, "%qE in namespace %qE does not name a type",
2509 id, parser->scope);
2510 else if (CLASS_TYPE_P (parser->scope)
2511 && constructor_name_p (id, parser->scope))
2513 /* A<T>::A<T>() */
2514 error_at (location, "%<%T::%E%> names the constructor, not"
2515 " the type", parser->scope, id);
2516 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2517 error_at (location, "and %qT has no template constructors",
2518 parser->scope);
2520 else if (TYPE_P (parser->scope)
2521 && dependent_scope_p (parser->scope))
2522 error_at (location, "need %<typename%> before %<%T::%E%> because "
2523 "%qT is a dependent scope",
2524 parser->scope, id, parser->scope);
2525 else if (TYPE_P (parser->scope))
2526 error_at (location, "%qE in class %qT does not name a type",
2527 id, parser->scope);
2528 else
2529 gcc_unreachable ();
2531 cp_parser_commit_to_tentative_parse (parser);
2534 /* Check for a common situation where a type-name should be present,
2535 but is not, and issue a sensible error message. Returns true if an
2536 invalid type-name was detected.
2538 The situation handled by this function are variable declarations of the
2539 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2540 Usually, `ID' should name a type, but if we got here it means that it
2541 does not. We try to emit the best possible error message depending on
2542 how exactly the id-expression looks like. */
2544 static bool
2545 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2547 tree id;
2548 cp_token *token = cp_lexer_peek_token (parser->lexer);
2550 /* Avoid duplicate error about ambiguous lookup. */
2551 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2553 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2554 if (next->type == CPP_NAME && next->ambiguous_p)
2555 goto out;
2558 cp_parser_parse_tentatively (parser);
2559 id = cp_parser_id_expression (parser,
2560 /*template_keyword_p=*/false,
2561 /*check_dependency_p=*/true,
2562 /*template_p=*/NULL,
2563 /*declarator_p=*/true,
2564 /*optional_p=*/false);
2565 /* If the next token is a (, this is a function with no explicit return
2566 type, i.e. constructor, destructor or conversion op. */
2567 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2568 || TREE_CODE (id) == TYPE_DECL)
2570 cp_parser_abort_tentative_parse (parser);
2571 return false;
2573 if (!cp_parser_parse_definitely (parser))
2574 return false;
2576 /* Emit a diagnostic for the invalid type. */
2577 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2578 id, token->location);
2579 out:
2580 /* If we aren't in the middle of a declarator (i.e. in a
2581 parameter-declaration-clause), skip to the end of the declaration;
2582 there's no point in trying to process it. */
2583 if (!parser->in_declarator_p)
2584 cp_parser_skip_to_end_of_block_or_statement (parser);
2585 return true;
2588 /* Consume tokens up to, and including, the next non-nested closing `)'.
2589 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2590 are doing error recovery. Returns -1 if OR_COMMA is true and we
2591 found an unnested comma. */
2593 static int
2594 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2595 bool recovering,
2596 bool or_comma,
2597 bool consume_paren)
2599 unsigned paren_depth = 0;
2600 unsigned brace_depth = 0;
2601 unsigned square_depth = 0;
2603 if (recovering && !or_comma
2604 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2605 return 0;
2607 while (true)
2609 cp_token * token = cp_lexer_peek_token (parser->lexer);
2611 switch (token->type)
2613 case CPP_EOF:
2614 case CPP_PRAGMA_EOL:
2615 /* If we've run out of tokens, then there is no closing `)'. */
2616 return 0;
2618 /* This is good for lambda expression capture-lists. */
2619 case CPP_OPEN_SQUARE:
2620 ++square_depth;
2621 break;
2622 case CPP_CLOSE_SQUARE:
2623 if (!square_depth--)
2624 return 0;
2625 break;
2627 case CPP_SEMICOLON:
2628 /* This matches the processing in skip_to_end_of_statement. */
2629 if (!brace_depth)
2630 return 0;
2631 break;
2633 case CPP_OPEN_BRACE:
2634 ++brace_depth;
2635 break;
2636 case CPP_CLOSE_BRACE:
2637 if (!brace_depth--)
2638 return 0;
2639 break;
2641 case CPP_COMMA:
2642 if (recovering && or_comma && !brace_depth && !paren_depth
2643 && !square_depth)
2644 return -1;
2645 break;
2647 case CPP_OPEN_PAREN:
2648 if (!brace_depth)
2649 ++paren_depth;
2650 break;
2652 case CPP_CLOSE_PAREN:
2653 if (!brace_depth && !paren_depth--)
2655 if (consume_paren)
2656 cp_lexer_consume_token (parser->lexer);
2657 return 1;
2659 break;
2661 default:
2662 break;
2665 /* Consume the token. */
2666 cp_lexer_consume_token (parser->lexer);
2670 /* Consume tokens until we reach the end of the current statement.
2671 Normally, that will be just before consuming a `;'. However, if a
2672 non-nested `}' comes first, then we stop before consuming that. */
2674 static void
2675 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2677 unsigned nesting_depth = 0;
2679 while (true)
2681 cp_token *token = cp_lexer_peek_token (parser->lexer);
2683 switch (token->type)
2685 case CPP_EOF:
2686 case CPP_PRAGMA_EOL:
2687 /* If we've run out of tokens, stop. */
2688 return;
2690 case CPP_SEMICOLON:
2691 /* If the next token is a `;', we have reached the end of the
2692 statement. */
2693 if (!nesting_depth)
2694 return;
2695 break;
2697 case CPP_CLOSE_BRACE:
2698 /* If this is a non-nested '}', stop before consuming it.
2699 That way, when confronted with something like:
2701 { 3 + }
2703 we stop before consuming the closing '}', even though we
2704 have not yet reached a `;'. */
2705 if (nesting_depth == 0)
2706 return;
2708 /* If it is the closing '}' for a block that we have
2709 scanned, stop -- but only after consuming the token.
2710 That way given:
2712 void f g () { ... }
2713 typedef int I;
2715 we will stop after the body of the erroneously declared
2716 function, but before consuming the following `typedef'
2717 declaration. */
2718 if (--nesting_depth == 0)
2720 cp_lexer_consume_token (parser->lexer);
2721 return;
2724 case CPP_OPEN_BRACE:
2725 ++nesting_depth;
2726 break;
2728 default:
2729 break;
2732 /* Consume the token. */
2733 cp_lexer_consume_token (parser->lexer);
2737 /* This function is called at the end of a statement or declaration.
2738 If the next token is a semicolon, it is consumed; otherwise, error
2739 recovery is attempted. */
2741 static void
2742 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2744 /* Look for the trailing `;'. */
2745 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2747 /* If there is additional (erroneous) input, skip to the end of
2748 the statement. */
2749 cp_parser_skip_to_end_of_statement (parser);
2750 /* If the next token is now a `;', consume it. */
2751 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2752 cp_lexer_consume_token (parser->lexer);
2756 /* Skip tokens until we have consumed an entire block, or until we
2757 have consumed a non-nested `;'. */
2759 static void
2760 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2762 int nesting_depth = 0;
2764 while (nesting_depth >= 0)
2766 cp_token *token = cp_lexer_peek_token (parser->lexer);
2768 switch (token->type)
2770 case CPP_EOF:
2771 case CPP_PRAGMA_EOL:
2772 /* If we've run out of tokens, stop. */
2773 return;
2775 case CPP_SEMICOLON:
2776 /* Stop if this is an unnested ';'. */
2777 if (!nesting_depth)
2778 nesting_depth = -1;
2779 break;
2781 case CPP_CLOSE_BRACE:
2782 /* Stop if this is an unnested '}', or closes the outermost
2783 nesting level. */
2784 nesting_depth--;
2785 if (nesting_depth < 0)
2786 return;
2787 if (!nesting_depth)
2788 nesting_depth = -1;
2789 break;
2791 case CPP_OPEN_BRACE:
2792 /* Nest. */
2793 nesting_depth++;
2794 break;
2796 default:
2797 break;
2800 /* Consume the token. */
2801 cp_lexer_consume_token (parser->lexer);
2805 /* Skip tokens until a non-nested closing curly brace is the next
2806 token, or there are no more tokens. Return true in the first case,
2807 false otherwise. */
2809 static bool
2810 cp_parser_skip_to_closing_brace (cp_parser *parser)
2812 unsigned nesting_depth = 0;
2814 while (true)
2816 cp_token *token = cp_lexer_peek_token (parser->lexer);
2818 switch (token->type)
2820 case CPP_EOF:
2821 case CPP_PRAGMA_EOL:
2822 /* If we've run out of tokens, stop. */
2823 return false;
2825 case CPP_CLOSE_BRACE:
2826 /* If the next token is a non-nested `}', then we have reached
2827 the end of the current block. */
2828 if (nesting_depth-- == 0)
2829 return true;
2830 break;
2832 case CPP_OPEN_BRACE:
2833 /* If it the next token is a `{', then we are entering a new
2834 block. Consume the entire block. */
2835 ++nesting_depth;
2836 break;
2838 default:
2839 break;
2842 /* Consume the token. */
2843 cp_lexer_consume_token (parser->lexer);
2847 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2848 parameter is the PRAGMA token, allowing us to purge the entire pragma
2849 sequence. */
2851 static void
2852 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2854 cp_token *token;
2856 parser->lexer->in_pragma = false;
2859 token = cp_lexer_consume_token (parser->lexer);
2860 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2862 /* Ensure that the pragma is not parsed again. */
2863 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2866 /* Require pragma end of line, resyncing with it as necessary. The
2867 arguments are as for cp_parser_skip_to_pragma_eol. */
2869 static void
2870 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2872 parser->lexer->in_pragma = false;
2873 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2874 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2877 /* This is a simple wrapper around make_typename_type. When the id is
2878 an unresolved identifier node, we can provide a superior diagnostic
2879 using cp_parser_diagnose_invalid_type_name. */
2881 static tree
2882 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2883 tree id, location_t id_location)
2885 tree result;
2886 if (TREE_CODE (id) == IDENTIFIER_NODE)
2888 result = make_typename_type (scope, id, typename_type,
2889 /*complain=*/tf_none);
2890 if (result == error_mark_node)
2891 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2892 return result;
2894 return make_typename_type (scope, id, typename_type, tf_error);
2897 /* This is a wrapper around the
2898 make_{pointer,ptrmem,reference}_declarator functions that decides
2899 which one to call based on the CODE and CLASS_TYPE arguments. The
2900 CODE argument should be one of the values returned by
2901 cp_parser_ptr_operator. */
2902 static cp_declarator *
2903 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2904 cp_cv_quals cv_qualifiers,
2905 cp_declarator *target)
2907 if (code == ERROR_MARK)
2908 return cp_error_declarator;
2910 if (code == INDIRECT_REF)
2911 if (class_type == NULL_TREE)
2912 return make_pointer_declarator (cv_qualifiers, target);
2913 else
2914 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2915 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2916 return make_reference_declarator (cv_qualifiers, target, false);
2917 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2918 return make_reference_declarator (cv_qualifiers, target, true);
2919 gcc_unreachable ();
2922 /* Create a new C++ parser. */
2924 static cp_parser *
2925 cp_parser_new (void)
2927 cp_parser *parser;
2928 cp_lexer *lexer;
2929 unsigned i;
2931 /* cp_lexer_new_main is called before doing GC allocation because
2932 cp_lexer_new_main might load a PCH file. */
2933 lexer = cp_lexer_new_main ();
2935 /* Initialize the binops_by_token so that we can get the tree
2936 directly from the token. */
2937 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2938 binops_by_token[binops[i].token_type] = binops[i];
2940 parser = ggc_alloc_cleared_cp_parser ();
2941 parser->lexer = lexer;
2942 parser->context = cp_parser_context_new (NULL);
2944 /* For now, we always accept GNU extensions. */
2945 parser->allow_gnu_extensions_p = 1;
2947 /* The `>' token is a greater-than operator, not the end of a
2948 template-id. */
2949 parser->greater_than_is_operator_p = true;
2951 parser->default_arg_ok_p = true;
2953 /* We are not parsing a constant-expression. */
2954 parser->integral_constant_expression_p = false;
2955 parser->allow_non_integral_constant_expression_p = false;
2956 parser->non_integral_constant_expression_p = false;
2958 /* Local variable names are not forbidden. */
2959 parser->local_variables_forbidden_p = false;
2961 /* We are not processing an `extern "C"' declaration. */
2962 parser->in_unbraced_linkage_specification_p = false;
2964 /* We are not processing a declarator. */
2965 parser->in_declarator_p = false;
2967 /* We are not processing a template-argument-list. */
2968 parser->in_template_argument_list_p = false;
2970 /* We are not in an iteration statement. */
2971 parser->in_statement = 0;
2973 /* We are not in a switch statement. */
2974 parser->in_switch_statement_p = false;
2976 /* We are not parsing a type-id inside an expression. */
2977 parser->in_type_id_in_expr_p = false;
2979 /* Declarations aren't implicitly extern "C". */
2980 parser->implicit_extern_c = false;
2982 /* String literals should be translated to the execution character set. */
2983 parser->translate_strings_p = true;
2985 /* We are not parsing a function body. */
2986 parser->in_function_body = false;
2988 /* We can correct until told otherwise. */
2989 parser->colon_corrects_to_scope_p = true;
2991 /* The unparsed function queue is empty. */
2992 push_unparsed_function_queues (parser);
2994 /* There are no classes being defined. */
2995 parser->num_classes_being_defined = 0;
2997 /* No template parameters apply. */
2998 parser->num_template_parameter_lists = 0;
3000 return parser;
3003 /* Create a cp_lexer structure which will emit the tokens in CACHE
3004 and push it onto the parser's lexer stack. This is used for delayed
3005 parsing of in-class method bodies and default arguments, and should
3006 not be confused with tentative parsing. */
3007 static void
3008 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3010 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3011 lexer->next = parser->lexer;
3012 parser->lexer = lexer;
3014 /* Move the current source position to that of the first token in the
3015 new lexer. */
3016 cp_lexer_set_source_position_from_token (lexer->next_token);
3019 /* Pop the top lexer off the parser stack. This is never used for the
3020 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3021 static void
3022 cp_parser_pop_lexer (cp_parser *parser)
3024 cp_lexer *lexer = parser->lexer;
3025 parser->lexer = lexer->next;
3026 cp_lexer_destroy (lexer);
3028 /* Put the current source position back where it was before this
3029 lexer was pushed. */
3030 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3033 /* Lexical conventions [gram.lex] */
3035 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3036 identifier. */
3038 static tree
3039 cp_parser_identifier (cp_parser* parser)
3041 cp_token *token;
3043 /* Look for the identifier. */
3044 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3045 /* Return the value. */
3046 return token ? token->u.value : error_mark_node;
3049 /* Parse a sequence of adjacent string constants. Returns a
3050 TREE_STRING representing the combined, nul-terminated string
3051 constant. If TRANSLATE is true, translate the string to the
3052 execution character set. If WIDE_OK is true, a wide string is
3053 invalid here.
3055 C++98 [lex.string] says that if a narrow string literal token is
3056 adjacent to a wide string literal token, the behavior is undefined.
3057 However, C99 6.4.5p4 says that this results in a wide string literal.
3058 We follow C99 here, for consistency with the C front end.
3060 This code is largely lifted from lex_string() in c-lex.c.
3062 FUTURE: ObjC++ will need to handle @-strings here. */
3063 static tree
3064 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3066 tree value;
3067 size_t count;
3068 struct obstack str_ob;
3069 cpp_string str, istr, *strs;
3070 cp_token *tok;
3071 enum cpp_ttype type;
3073 tok = cp_lexer_peek_token (parser->lexer);
3074 if (!cp_parser_is_string_literal (tok))
3076 cp_parser_error (parser, "expected string-literal");
3077 return error_mark_node;
3080 type = tok->type;
3082 /* Try to avoid the overhead of creating and destroying an obstack
3083 for the common case of just one string. */
3084 if (!cp_parser_is_string_literal
3085 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3087 cp_lexer_consume_token (parser->lexer);
3089 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3090 str.len = TREE_STRING_LENGTH (tok->u.value);
3091 count = 1;
3093 strs = &str;
3095 else
3097 gcc_obstack_init (&str_ob);
3098 count = 0;
3102 cp_lexer_consume_token (parser->lexer);
3103 count++;
3104 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3105 str.len = TREE_STRING_LENGTH (tok->u.value);
3107 if (type != tok->type)
3109 if (type == CPP_STRING)
3110 type = tok->type;
3111 else if (tok->type != CPP_STRING)
3112 error_at (tok->location,
3113 "unsupported non-standard concatenation "
3114 "of string literals");
3117 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3119 tok = cp_lexer_peek_token (parser->lexer);
3121 while (cp_parser_is_string_literal (tok));
3123 strs = (cpp_string *) obstack_finish (&str_ob);
3126 if (type != CPP_STRING && !wide_ok)
3128 cp_parser_error (parser, "a wide string is invalid in this context");
3129 type = CPP_STRING;
3132 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3133 (parse_in, strs, count, &istr, type))
3135 value = build_string (istr.len, (const char *)istr.text);
3136 free (CONST_CAST (unsigned char *, istr.text));
3138 switch (type)
3140 default:
3141 case CPP_STRING:
3142 case CPP_UTF8STRING:
3143 TREE_TYPE (value) = char_array_type_node;
3144 break;
3145 case CPP_STRING16:
3146 TREE_TYPE (value) = char16_array_type_node;
3147 break;
3148 case CPP_STRING32:
3149 TREE_TYPE (value) = char32_array_type_node;
3150 break;
3151 case CPP_WSTRING:
3152 TREE_TYPE (value) = wchar_array_type_node;
3153 break;
3156 value = fix_string_type (value);
3158 else
3159 /* cpp_interpret_string has issued an error. */
3160 value = error_mark_node;
3162 if (count > 1)
3163 obstack_free (&str_ob, 0);
3165 return value;
3169 /* Basic concepts [gram.basic] */
3171 /* Parse a translation-unit.
3173 translation-unit:
3174 declaration-seq [opt]
3176 Returns TRUE if all went well. */
3178 static bool
3179 cp_parser_translation_unit (cp_parser* parser)
3181 /* The address of the first non-permanent object on the declarator
3182 obstack. */
3183 static void *declarator_obstack_base;
3185 bool success;
3187 /* Create the declarator obstack, if necessary. */
3188 if (!cp_error_declarator)
3190 gcc_obstack_init (&declarator_obstack);
3191 /* Create the error declarator. */
3192 cp_error_declarator = make_declarator (cdk_error);
3193 /* Create the empty parameter list. */
3194 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3195 /* Remember where the base of the declarator obstack lies. */
3196 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3199 cp_parser_declaration_seq_opt (parser);
3201 /* If there are no tokens left then all went well. */
3202 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3204 /* Get rid of the token array; we don't need it any more. */
3205 cp_lexer_destroy (parser->lexer);
3206 parser->lexer = NULL;
3208 /* This file might have been a context that's implicitly extern
3209 "C". If so, pop the lang context. (Only relevant for PCH.) */
3210 if (parser->implicit_extern_c)
3212 pop_lang_context ();
3213 parser->implicit_extern_c = false;
3216 /* Finish up. */
3217 finish_translation_unit ();
3219 success = true;
3221 else
3223 cp_parser_error (parser, "expected declaration");
3224 success = false;
3227 /* Make sure the declarator obstack was fully cleaned up. */
3228 gcc_assert (obstack_next_free (&declarator_obstack)
3229 == declarator_obstack_base);
3231 /* All went well. */
3232 return success;
3235 /* Expressions [gram.expr] */
3237 /* Parse a primary-expression.
3239 primary-expression:
3240 literal
3241 this
3242 ( expression )
3243 id-expression
3245 GNU Extensions:
3247 primary-expression:
3248 ( compound-statement )
3249 __builtin_va_arg ( assignment-expression , type-id )
3250 __builtin_offsetof ( type-id , offsetof-expression )
3252 C++ Extensions:
3253 __has_nothrow_assign ( type-id )
3254 __has_nothrow_constructor ( type-id )
3255 __has_nothrow_copy ( type-id )
3256 __has_trivial_assign ( type-id )
3257 __has_trivial_constructor ( type-id )
3258 __has_trivial_copy ( type-id )
3259 __has_trivial_destructor ( type-id )
3260 __has_virtual_destructor ( type-id )
3261 __is_abstract ( type-id )
3262 __is_base_of ( type-id , type-id )
3263 __is_class ( type-id )
3264 __is_convertible_to ( type-id , type-id )
3265 __is_empty ( type-id )
3266 __is_enum ( type-id )
3267 __is_pod ( type-id )
3268 __is_polymorphic ( type-id )
3269 __is_union ( type-id )
3271 Objective-C++ Extension:
3273 primary-expression:
3274 objc-expression
3276 literal:
3277 __null
3279 ADDRESS_P is true iff this expression was immediately preceded by
3280 "&" and therefore might denote a pointer-to-member. CAST_P is true
3281 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3282 true iff this expression is a template argument.
3284 Returns a representation of the expression. Upon return, *IDK
3285 indicates what kind of id-expression (if any) was present. */
3287 static tree
3288 cp_parser_primary_expression (cp_parser *parser,
3289 bool address_p,
3290 bool cast_p,
3291 bool template_arg_p,
3292 cp_id_kind *idk)
3294 cp_token *token = NULL;
3296 /* Assume the primary expression is not an id-expression. */
3297 *idk = CP_ID_KIND_NONE;
3299 /* Peek at the next token. */
3300 token = cp_lexer_peek_token (parser->lexer);
3301 switch (token->type)
3303 /* literal:
3304 integer-literal
3305 character-literal
3306 floating-literal
3307 string-literal
3308 boolean-literal */
3309 case CPP_CHAR:
3310 case CPP_CHAR16:
3311 case CPP_CHAR32:
3312 case CPP_WCHAR:
3313 case CPP_NUMBER:
3314 token = cp_lexer_consume_token (parser->lexer);
3315 if (TREE_CODE (token->u.value) == FIXED_CST)
3317 error_at (token->location,
3318 "fixed-point types not supported in C++");
3319 return error_mark_node;
3321 /* Floating-point literals are only allowed in an integral
3322 constant expression if they are cast to an integral or
3323 enumeration type. */
3324 if (TREE_CODE (token->u.value) == REAL_CST
3325 && parser->integral_constant_expression_p
3326 && pedantic)
3328 /* CAST_P will be set even in invalid code like "int(2.7 +
3329 ...)". Therefore, we have to check that the next token
3330 is sure to end the cast. */
3331 if (cast_p)
3333 cp_token *next_token;
3335 next_token = cp_lexer_peek_token (parser->lexer);
3336 if (/* The comma at the end of an
3337 enumerator-definition. */
3338 next_token->type != CPP_COMMA
3339 /* The curly brace at the end of an enum-specifier. */
3340 && next_token->type != CPP_CLOSE_BRACE
3341 /* The end of a statement. */
3342 && next_token->type != CPP_SEMICOLON
3343 /* The end of the cast-expression. */
3344 && next_token->type != CPP_CLOSE_PAREN
3345 /* The end of an array bound. */
3346 && next_token->type != CPP_CLOSE_SQUARE
3347 /* The closing ">" in a template-argument-list. */
3348 && (next_token->type != CPP_GREATER
3349 || parser->greater_than_is_operator_p)
3350 /* C++0x only: A ">>" treated like two ">" tokens,
3351 in a template-argument-list. */
3352 && (next_token->type != CPP_RSHIFT
3353 || (cxx_dialect == cxx98)
3354 || parser->greater_than_is_operator_p))
3355 cast_p = false;
3358 /* If we are within a cast, then the constraint that the
3359 cast is to an integral or enumeration type will be
3360 checked at that point. If we are not within a cast, then
3361 this code is invalid. */
3362 if (!cast_p)
3363 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3365 return token->u.value;
3367 case CPP_STRING:
3368 case CPP_STRING16:
3369 case CPP_STRING32:
3370 case CPP_WSTRING:
3371 case CPP_UTF8STRING:
3372 /* ??? Should wide strings be allowed when parser->translate_strings_p
3373 is false (i.e. in attributes)? If not, we can kill the third
3374 argument to cp_parser_string_literal. */
3375 return cp_parser_string_literal (parser,
3376 parser->translate_strings_p,
3377 true);
3379 case CPP_OPEN_PAREN:
3381 tree expr;
3382 bool saved_greater_than_is_operator_p;
3384 /* Consume the `('. */
3385 cp_lexer_consume_token (parser->lexer);
3386 /* Within a parenthesized expression, a `>' token is always
3387 the greater-than operator. */
3388 saved_greater_than_is_operator_p
3389 = parser->greater_than_is_operator_p;
3390 parser->greater_than_is_operator_p = true;
3391 /* If we see `( { ' then we are looking at the beginning of
3392 a GNU statement-expression. */
3393 if (cp_parser_allow_gnu_extensions_p (parser)
3394 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3396 /* Statement-expressions are not allowed by the standard. */
3397 pedwarn (token->location, OPT_pedantic,
3398 "ISO C++ forbids braced-groups within expressions");
3400 /* And they're not allowed outside of a function-body; you
3401 cannot, for example, write:
3403 int i = ({ int j = 3; j + 1; });
3405 at class or namespace scope. */
3406 if (!parser->in_function_body
3407 || parser->in_template_argument_list_p)
3409 error_at (token->location,
3410 "statement-expressions are not allowed outside "
3411 "functions nor in template-argument lists");
3412 cp_parser_skip_to_end_of_block_or_statement (parser);
3413 expr = error_mark_node;
3415 else
3417 /* Start the statement-expression. */
3418 expr = begin_stmt_expr ();
3419 /* Parse the compound-statement. */
3420 cp_parser_compound_statement (parser, expr, false, false);
3421 /* Finish up. */
3422 expr = finish_stmt_expr (expr, false);
3425 else
3427 /* Parse the parenthesized expression. */
3428 expr = cp_parser_expression (parser, cast_p, idk);
3429 /* Let the front end know that this expression was
3430 enclosed in parentheses. This matters in case, for
3431 example, the expression is of the form `A::B', since
3432 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3433 not. */
3434 finish_parenthesized_expr (expr);
3436 /* The `>' token might be the end of a template-id or
3437 template-parameter-list now. */
3438 parser->greater_than_is_operator_p
3439 = saved_greater_than_is_operator_p;
3440 /* Consume the `)'. */
3441 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3442 cp_parser_skip_to_end_of_statement (parser);
3444 return expr;
3447 case CPP_OPEN_SQUARE:
3448 if (c_dialect_objc ())
3449 /* We have an Objective-C++ message. */
3450 return cp_parser_objc_expression (parser);
3452 tree lam = cp_parser_lambda_expression (parser);
3453 /* Don't warn about a failed tentative parse. */
3454 if (cp_parser_error_occurred (parser))
3455 return error_mark_node;
3456 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3457 return lam;
3460 case CPP_OBJC_STRING:
3461 if (c_dialect_objc ())
3462 /* We have an Objective-C++ string literal. */
3463 return cp_parser_objc_expression (parser);
3464 cp_parser_error (parser, "expected primary-expression");
3465 return error_mark_node;
3467 case CPP_KEYWORD:
3468 switch (token->keyword)
3470 /* These two are the boolean literals. */
3471 case RID_TRUE:
3472 cp_lexer_consume_token (parser->lexer);
3473 return boolean_true_node;
3474 case RID_FALSE:
3475 cp_lexer_consume_token (parser->lexer);
3476 return boolean_false_node;
3478 /* The `__null' literal. */
3479 case RID_NULL:
3480 cp_lexer_consume_token (parser->lexer);
3481 return null_node;
3483 /* The `nullptr' literal. */
3484 case RID_NULLPTR:
3485 cp_lexer_consume_token (parser->lexer);
3486 return nullptr_node;
3488 /* Recognize the `this' keyword. */
3489 case RID_THIS:
3490 cp_lexer_consume_token (parser->lexer);
3491 if (parser->local_variables_forbidden_p)
3493 error_at (token->location,
3494 "%<this%> may not be used in this context");
3495 return error_mark_node;
3497 /* Pointers cannot appear in constant-expressions. */
3498 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3499 return error_mark_node;
3500 return finish_this_expr ();
3502 /* The `operator' keyword can be the beginning of an
3503 id-expression. */
3504 case RID_OPERATOR:
3505 goto id_expression;
3507 case RID_FUNCTION_NAME:
3508 case RID_PRETTY_FUNCTION_NAME:
3509 case RID_C99_FUNCTION_NAME:
3511 non_integral_constant name;
3513 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3514 __func__ are the names of variables -- but they are
3515 treated specially. Therefore, they are handled here,
3516 rather than relying on the generic id-expression logic
3517 below. Grammatically, these names are id-expressions.
3519 Consume the token. */
3520 token = cp_lexer_consume_token (parser->lexer);
3522 switch (token->keyword)
3524 case RID_FUNCTION_NAME:
3525 name = NIC_FUNC_NAME;
3526 break;
3527 case RID_PRETTY_FUNCTION_NAME:
3528 name = NIC_PRETTY_FUNC;
3529 break;
3530 case RID_C99_FUNCTION_NAME:
3531 name = NIC_C99_FUNC;
3532 break;
3533 default:
3534 gcc_unreachable ();
3537 if (cp_parser_non_integral_constant_expression (parser, name))
3538 return error_mark_node;
3540 /* Look up the name. */
3541 return finish_fname (token->u.value);
3544 case RID_VA_ARG:
3546 tree expression;
3547 tree type;
3549 /* The `__builtin_va_arg' construct is used to handle
3550 `va_arg'. Consume the `__builtin_va_arg' token. */
3551 cp_lexer_consume_token (parser->lexer);
3552 /* Look for the opening `('. */
3553 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3554 /* Now, parse the assignment-expression. */
3555 expression = cp_parser_assignment_expression (parser,
3556 /*cast_p=*/false, NULL);
3557 /* Look for the `,'. */
3558 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3559 /* Parse the type-id. */
3560 type = cp_parser_type_id (parser);
3561 /* Look for the closing `)'. */
3562 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3563 /* Using `va_arg' in a constant-expression is not
3564 allowed. */
3565 if (cp_parser_non_integral_constant_expression (parser,
3566 NIC_VA_ARG))
3567 return error_mark_node;
3568 return build_x_va_arg (expression, type);
3571 case RID_OFFSETOF:
3572 return cp_parser_builtin_offsetof (parser);
3574 case RID_HAS_NOTHROW_ASSIGN:
3575 case RID_HAS_NOTHROW_CONSTRUCTOR:
3576 case RID_HAS_NOTHROW_COPY:
3577 case RID_HAS_TRIVIAL_ASSIGN:
3578 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3579 case RID_HAS_TRIVIAL_COPY:
3580 case RID_HAS_TRIVIAL_DESTRUCTOR:
3581 case RID_HAS_VIRTUAL_DESTRUCTOR:
3582 case RID_IS_ABSTRACT:
3583 case RID_IS_BASE_OF:
3584 case RID_IS_CLASS:
3585 case RID_IS_CONVERTIBLE_TO:
3586 case RID_IS_EMPTY:
3587 case RID_IS_ENUM:
3588 case RID_IS_POD:
3589 case RID_IS_POLYMORPHIC:
3590 case RID_IS_STD_LAYOUT:
3591 case RID_IS_TRIVIAL:
3592 case RID_IS_UNION:
3593 case RID_IS_LITERAL_TYPE:
3594 return cp_parser_trait_expr (parser, token->keyword);
3596 /* Objective-C++ expressions. */
3597 case RID_AT_ENCODE:
3598 case RID_AT_PROTOCOL:
3599 case RID_AT_SELECTOR:
3600 return cp_parser_objc_expression (parser);
3602 case RID_TEMPLATE:
3603 if (parser->in_function_body
3604 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3605 == CPP_LESS))
3607 error_at (token->location,
3608 "a template declaration cannot appear at block scope");
3609 cp_parser_skip_to_end_of_block_or_statement (parser);
3610 return error_mark_node;
3612 default:
3613 cp_parser_error (parser, "expected primary-expression");
3614 return error_mark_node;
3617 /* An id-expression can start with either an identifier, a
3618 `::' as the beginning of a qualified-id, or the "operator"
3619 keyword. */
3620 case CPP_NAME:
3621 case CPP_SCOPE:
3622 case CPP_TEMPLATE_ID:
3623 case CPP_NESTED_NAME_SPECIFIER:
3625 tree id_expression;
3626 tree decl;
3627 const char *error_msg;
3628 bool template_p;
3629 bool done;
3630 cp_token *id_expr_token;
3632 id_expression:
3633 /* Parse the id-expression. */
3634 id_expression
3635 = cp_parser_id_expression (parser,
3636 /*template_keyword_p=*/false,
3637 /*check_dependency_p=*/true,
3638 &template_p,
3639 /*declarator_p=*/false,
3640 /*optional_p=*/false);
3641 if (id_expression == error_mark_node)
3642 return error_mark_node;
3643 id_expr_token = token;
3644 token = cp_lexer_peek_token (parser->lexer);
3645 done = (token->type != CPP_OPEN_SQUARE
3646 && token->type != CPP_OPEN_PAREN
3647 && token->type != CPP_DOT
3648 && token->type != CPP_DEREF
3649 && token->type != CPP_PLUS_PLUS
3650 && token->type != CPP_MINUS_MINUS);
3651 /* If we have a template-id, then no further lookup is
3652 required. If the template-id was for a template-class, we
3653 will sometimes have a TYPE_DECL at this point. */
3654 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3655 || TREE_CODE (id_expression) == TYPE_DECL)
3656 decl = id_expression;
3657 /* Look up the name. */
3658 else
3660 tree ambiguous_decls;
3662 /* If we already know that this lookup is ambiguous, then
3663 we've already issued an error message; there's no reason
3664 to check again. */
3665 if (id_expr_token->type == CPP_NAME
3666 && id_expr_token->ambiguous_p)
3668 cp_parser_simulate_error (parser);
3669 return error_mark_node;
3672 decl = cp_parser_lookup_name (parser, id_expression,
3673 none_type,
3674 template_p,
3675 /*is_namespace=*/false,
3676 /*check_dependency=*/true,
3677 &ambiguous_decls,
3678 id_expr_token->location);
3679 /* If the lookup was ambiguous, an error will already have
3680 been issued. */
3681 if (ambiguous_decls)
3682 return error_mark_node;
3684 /* In Objective-C++, we may have an Objective-C 2.0
3685 dot-syntax for classes here. */
3686 if (c_dialect_objc ()
3687 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3688 && TREE_CODE (decl) == TYPE_DECL
3689 && objc_is_class_name (decl))
3691 tree component;
3692 cp_lexer_consume_token (parser->lexer);
3693 component = cp_parser_identifier (parser);
3694 if (component == error_mark_node)
3695 return error_mark_node;
3697 return objc_build_class_component_ref (id_expression, component);
3700 /* In Objective-C++, an instance variable (ivar) may be preferred
3701 to whatever cp_parser_lookup_name() found. */
3702 decl = objc_lookup_ivar (decl, id_expression);
3704 /* If name lookup gives us a SCOPE_REF, then the
3705 qualifying scope was dependent. */
3706 if (TREE_CODE (decl) == SCOPE_REF)
3708 /* At this point, we do not know if DECL is a valid
3709 integral constant expression. We assume that it is
3710 in fact such an expression, so that code like:
3712 template <int N> struct A {
3713 int a[B<N>::i];
3716 is accepted. At template-instantiation time, we
3717 will check that B<N>::i is actually a constant. */
3718 return decl;
3720 /* Check to see if DECL is a local variable in a context
3721 where that is forbidden. */
3722 if (parser->local_variables_forbidden_p
3723 && local_variable_p (decl))
3725 /* It might be that we only found DECL because we are
3726 trying to be generous with pre-ISO scoping rules.
3727 For example, consider:
3729 int i;
3730 void g() {
3731 for (int i = 0; i < 10; ++i) {}
3732 extern void f(int j = i);
3735 Here, name look up will originally find the out
3736 of scope `i'. We need to issue a warning message,
3737 but then use the global `i'. */
3738 decl = check_for_out_of_scope_variable (decl);
3739 if (local_variable_p (decl))
3741 error_at (id_expr_token->location,
3742 "local variable %qD may not appear in this context",
3743 decl);
3744 return error_mark_node;
3749 decl = (finish_id_expression
3750 (id_expression, decl, parser->scope,
3751 idk,
3752 parser->integral_constant_expression_p,
3753 parser->allow_non_integral_constant_expression_p,
3754 &parser->non_integral_constant_expression_p,
3755 template_p, done, address_p,
3756 template_arg_p,
3757 &error_msg,
3758 id_expr_token->location));
3759 if (error_msg)
3760 cp_parser_error (parser, error_msg);
3761 return decl;
3764 /* Anything else is an error. */
3765 default:
3766 cp_parser_error (parser, "expected primary-expression");
3767 return error_mark_node;
3771 /* Parse an id-expression.
3773 id-expression:
3774 unqualified-id
3775 qualified-id
3777 qualified-id:
3778 :: [opt] nested-name-specifier template [opt] unqualified-id
3779 :: identifier
3780 :: operator-function-id
3781 :: template-id
3783 Return a representation of the unqualified portion of the
3784 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3785 a `::' or nested-name-specifier.
3787 Often, if the id-expression was a qualified-id, the caller will
3788 want to make a SCOPE_REF to represent the qualified-id. This
3789 function does not do this in order to avoid wastefully creating
3790 SCOPE_REFs when they are not required.
3792 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3793 `template' keyword.
3795 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3796 uninstantiated templates.
3798 If *TEMPLATE_P is non-NULL, it is set to true iff the
3799 `template' keyword is used to explicitly indicate that the entity
3800 named is a template.
3802 If DECLARATOR_P is true, the id-expression is appearing as part of
3803 a declarator, rather than as part of an expression. */
3805 static tree
3806 cp_parser_id_expression (cp_parser *parser,
3807 bool template_keyword_p,
3808 bool check_dependency_p,
3809 bool *template_p,
3810 bool declarator_p,
3811 bool optional_p)
3813 bool global_scope_p;
3814 bool nested_name_specifier_p;
3816 /* Assume the `template' keyword was not used. */
3817 if (template_p)
3818 *template_p = template_keyword_p;
3820 /* Look for the optional `::' operator. */
3821 global_scope_p
3822 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3823 != NULL_TREE);
3824 /* Look for the optional nested-name-specifier. */
3825 nested_name_specifier_p
3826 = (cp_parser_nested_name_specifier_opt (parser,
3827 /*typename_keyword_p=*/false,
3828 check_dependency_p,
3829 /*type_p=*/false,
3830 declarator_p)
3831 != NULL_TREE);
3832 /* If there is a nested-name-specifier, then we are looking at
3833 the first qualified-id production. */
3834 if (nested_name_specifier_p)
3836 tree saved_scope;
3837 tree saved_object_scope;
3838 tree saved_qualifying_scope;
3839 tree unqualified_id;
3840 bool is_template;
3842 /* See if the next token is the `template' keyword. */
3843 if (!template_p)
3844 template_p = &is_template;
3845 *template_p = cp_parser_optional_template_keyword (parser);
3846 /* Name lookup we do during the processing of the
3847 unqualified-id might obliterate SCOPE. */
3848 saved_scope = parser->scope;
3849 saved_object_scope = parser->object_scope;
3850 saved_qualifying_scope = parser->qualifying_scope;
3851 /* Process the final unqualified-id. */
3852 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3853 check_dependency_p,
3854 declarator_p,
3855 /*optional_p=*/false);
3856 /* Restore the SAVED_SCOPE for our caller. */
3857 parser->scope = saved_scope;
3858 parser->object_scope = saved_object_scope;
3859 parser->qualifying_scope = saved_qualifying_scope;
3861 return unqualified_id;
3863 /* Otherwise, if we are in global scope, then we are looking at one
3864 of the other qualified-id productions. */
3865 else if (global_scope_p)
3867 cp_token *token;
3868 tree id;
3870 /* Peek at the next token. */
3871 token = cp_lexer_peek_token (parser->lexer);
3873 /* If it's an identifier, and the next token is not a "<", then
3874 we can avoid the template-id case. This is an optimization
3875 for this common case. */
3876 if (token->type == CPP_NAME
3877 && !cp_parser_nth_token_starts_template_argument_list_p
3878 (parser, 2))
3879 return cp_parser_identifier (parser);
3881 cp_parser_parse_tentatively (parser);
3882 /* Try a template-id. */
3883 id = cp_parser_template_id (parser,
3884 /*template_keyword_p=*/false,
3885 /*check_dependency_p=*/true,
3886 declarator_p);
3887 /* If that worked, we're done. */
3888 if (cp_parser_parse_definitely (parser))
3889 return id;
3891 /* Peek at the next token. (Changes in the token buffer may
3892 have invalidated the pointer obtained above.) */
3893 token = cp_lexer_peek_token (parser->lexer);
3895 switch (token->type)
3897 case CPP_NAME:
3898 return cp_parser_identifier (parser);
3900 case CPP_KEYWORD:
3901 if (token->keyword == RID_OPERATOR)
3902 return cp_parser_operator_function_id (parser);
3903 /* Fall through. */
3905 default:
3906 cp_parser_error (parser, "expected id-expression");
3907 return error_mark_node;
3910 else
3911 return cp_parser_unqualified_id (parser, template_keyword_p,
3912 /*check_dependency_p=*/true,
3913 declarator_p,
3914 optional_p);
3917 /* Parse an unqualified-id.
3919 unqualified-id:
3920 identifier
3921 operator-function-id
3922 conversion-function-id
3923 ~ class-name
3924 template-id
3926 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3927 keyword, in a construct like `A::template ...'.
3929 Returns a representation of unqualified-id. For the `identifier'
3930 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3931 production a BIT_NOT_EXPR is returned; the operand of the
3932 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3933 other productions, see the documentation accompanying the
3934 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3935 names are looked up in uninstantiated templates. If DECLARATOR_P
3936 is true, the unqualified-id is appearing as part of a declarator,
3937 rather than as part of an expression. */
3939 static tree
3940 cp_parser_unqualified_id (cp_parser* parser,
3941 bool template_keyword_p,
3942 bool check_dependency_p,
3943 bool declarator_p,
3944 bool optional_p)
3946 cp_token *token;
3948 /* Peek at the next token. */
3949 token = cp_lexer_peek_token (parser->lexer);
3951 switch (token->type)
3953 case CPP_NAME:
3955 tree id;
3957 /* We don't know yet whether or not this will be a
3958 template-id. */
3959 cp_parser_parse_tentatively (parser);
3960 /* Try a template-id. */
3961 id = cp_parser_template_id (parser, template_keyword_p,
3962 check_dependency_p,
3963 declarator_p);
3964 /* If it worked, we're done. */
3965 if (cp_parser_parse_definitely (parser))
3966 return id;
3967 /* Otherwise, it's an ordinary identifier. */
3968 return cp_parser_identifier (parser);
3971 case CPP_TEMPLATE_ID:
3972 return cp_parser_template_id (parser, template_keyword_p,
3973 check_dependency_p,
3974 declarator_p);
3976 case CPP_COMPL:
3978 tree type_decl;
3979 tree qualifying_scope;
3980 tree object_scope;
3981 tree scope;
3982 bool done;
3984 /* Consume the `~' token. */
3985 cp_lexer_consume_token (parser->lexer);
3986 /* Parse the class-name. The standard, as written, seems to
3987 say that:
3989 template <typename T> struct S { ~S (); };
3990 template <typename T> S<T>::~S() {}
3992 is invalid, since `~' must be followed by a class-name, but
3993 `S<T>' is dependent, and so not known to be a class.
3994 That's not right; we need to look in uninstantiated
3995 templates. A further complication arises from:
3997 template <typename T> void f(T t) {
3998 t.T::~T();
4001 Here, it is not possible to look up `T' in the scope of `T'
4002 itself. We must look in both the current scope, and the
4003 scope of the containing complete expression.
4005 Yet another issue is:
4007 struct S {
4008 int S;
4009 ~S();
4012 S::~S() {}
4014 The standard does not seem to say that the `S' in `~S'
4015 should refer to the type `S' and not the data member
4016 `S::S'. */
4018 /* DR 244 says that we look up the name after the "~" in the
4019 same scope as we looked up the qualifying name. That idea
4020 isn't fully worked out; it's more complicated than that. */
4021 scope = parser->scope;
4022 object_scope = parser->object_scope;
4023 qualifying_scope = parser->qualifying_scope;
4025 /* Check for invalid scopes. */
4026 if (scope == error_mark_node)
4028 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4029 cp_lexer_consume_token (parser->lexer);
4030 return error_mark_node;
4032 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4034 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4035 error_at (token->location,
4036 "scope %qT before %<~%> is not a class-name",
4037 scope);
4038 cp_parser_simulate_error (parser);
4039 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4040 cp_lexer_consume_token (parser->lexer);
4041 return error_mark_node;
4043 gcc_assert (!scope || TYPE_P (scope));
4045 /* If the name is of the form "X::~X" it's OK even if X is a
4046 typedef. */
4047 token = cp_lexer_peek_token (parser->lexer);
4048 if (scope
4049 && token->type == CPP_NAME
4050 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4051 != CPP_LESS)
4052 && (token->u.value == TYPE_IDENTIFIER (scope)
4053 || constructor_name_p (token->u.value, scope)))
4055 cp_lexer_consume_token (parser->lexer);
4056 return build_nt (BIT_NOT_EXPR, scope);
4059 /* If there was an explicit qualification (S::~T), first look
4060 in the scope given by the qualification (i.e., S).
4062 Note: in the calls to cp_parser_class_name below we pass
4063 typename_type so that lookup finds the injected-class-name
4064 rather than the constructor. */
4065 done = false;
4066 type_decl = NULL_TREE;
4067 if (scope)
4069 cp_parser_parse_tentatively (parser);
4070 type_decl = cp_parser_class_name (parser,
4071 /*typename_keyword_p=*/false,
4072 /*template_keyword_p=*/false,
4073 typename_type,
4074 /*check_dependency=*/false,
4075 /*class_head_p=*/false,
4076 declarator_p);
4077 if (cp_parser_parse_definitely (parser))
4078 done = true;
4080 /* In "N::S::~S", look in "N" as well. */
4081 if (!done && scope && qualifying_scope)
4083 cp_parser_parse_tentatively (parser);
4084 parser->scope = qualifying_scope;
4085 parser->object_scope = NULL_TREE;
4086 parser->qualifying_scope = NULL_TREE;
4087 type_decl
4088 = cp_parser_class_name (parser,
4089 /*typename_keyword_p=*/false,
4090 /*template_keyword_p=*/false,
4091 typename_type,
4092 /*check_dependency=*/false,
4093 /*class_head_p=*/false,
4094 declarator_p);
4095 if (cp_parser_parse_definitely (parser))
4096 done = true;
4098 /* In "p->S::~T", look in the scope given by "*p" as well. */
4099 else if (!done && object_scope)
4101 cp_parser_parse_tentatively (parser);
4102 parser->scope = object_scope;
4103 parser->object_scope = NULL_TREE;
4104 parser->qualifying_scope = NULL_TREE;
4105 type_decl
4106 = cp_parser_class_name (parser,
4107 /*typename_keyword_p=*/false,
4108 /*template_keyword_p=*/false,
4109 typename_type,
4110 /*check_dependency=*/false,
4111 /*class_head_p=*/false,
4112 declarator_p);
4113 if (cp_parser_parse_definitely (parser))
4114 done = true;
4116 /* Look in the surrounding context. */
4117 if (!done)
4119 parser->scope = NULL_TREE;
4120 parser->object_scope = NULL_TREE;
4121 parser->qualifying_scope = NULL_TREE;
4122 if (processing_template_decl)
4123 cp_parser_parse_tentatively (parser);
4124 type_decl
4125 = cp_parser_class_name (parser,
4126 /*typename_keyword_p=*/false,
4127 /*template_keyword_p=*/false,
4128 typename_type,
4129 /*check_dependency=*/false,
4130 /*class_head_p=*/false,
4131 declarator_p);
4132 if (processing_template_decl
4133 && ! cp_parser_parse_definitely (parser))
4135 /* We couldn't find a type with this name, so just accept
4136 it and check for a match at instantiation time. */
4137 type_decl = cp_parser_identifier (parser);
4138 if (type_decl != error_mark_node)
4139 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4140 return type_decl;
4143 /* If an error occurred, assume that the name of the
4144 destructor is the same as the name of the qualifying
4145 class. That allows us to keep parsing after running
4146 into ill-formed destructor names. */
4147 if (type_decl == error_mark_node && scope)
4148 return build_nt (BIT_NOT_EXPR, scope);
4149 else if (type_decl == error_mark_node)
4150 return error_mark_node;
4152 /* Check that destructor name and scope match. */
4153 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4155 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4156 error_at (token->location,
4157 "declaration of %<~%T%> as member of %qT",
4158 type_decl, scope);
4159 cp_parser_simulate_error (parser);
4160 return error_mark_node;
4163 /* [class.dtor]
4165 A typedef-name that names a class shall not be used as the
4166 identifier in the declarator for a destructor declaration. */
4167 if (declarator_p
4168 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4169 && !DECL_SELF_REFERENCE_P (type_decl)
4170 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4171 error_at (token->location,
4172 "typedef-name %qD used as destructor declarator",
4173 type_decl);
4175 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4178 case CPP_KEYWORD:
4179 if (token->keyword == RID_OPERATOR)
4181 tree id;
4183 /* This could be a template-id, so we try that first. */
4184 cp_parser_parse_tentatively (parser);
4185 /* Try a template-id. */
4186 id = cp_parser_template_id (parser, template_keyword_p,
4187 /*check_dependency_p=*/true,
4188 declarator_p);
4189 /* If that worked, we're done. */
4190 if (cp_parser_parse_definitely (parser))
4191 return id;
4192 /* We still don't know whether we're looking at an
4193 operator-function-id or a conversion-function-id. */
4194 cp_parser_parse_tentatively (parser);
4195 /* Try an operator-function-id. */
4196 id = cp_parser_operator_function_id (parser);
4197 /* If that didn't work, try a conversion-function-id. */
4198 if (!cp_parser_parse_definitely (parser))
4199 id = cp_parser_conversion_function_id (parser);
4201 return id;
4203 /* Fall through. */
4205 default:
4206 if (optional_p)
4207 return NULL_TREE;
4208 cp_parser_error (parser, "expected unqualified-id");
4209 return error_mark_node;
4213 /* Parse an (optional) nested-name-specifier.
4215 nested-name-specifier: [C++98]
4216 class-or-namespace-name :: nested-name-specifier [opt]
4217 class-or-namespace-name :: template nested-name-specifier [opt]
4219 nested-name-specifier: [C++0x]
4220 type-name ::
4221 namespace-name ::
4222 nested-name-specifier identifier ::
4223 nested-name-specifier template [opt] simple-template-id ::
4225 PARSER->SCOPE should be set appropriately before this function is
4226 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4227 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4228 in name lookups.
4230 Sets PARSER->SCOPE to the class (TYPE) or namespace
4231 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4232 it unchanged if there is no nested-name-specifier. Returns the new
4233 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4235 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4236 part of a declaration and/or decl-specifier. */
4238 static tree
4239 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4240 bool typename_keyword_p,
4241 bool check_dependency_p,
4242 bool type_p,
4243 bool is_declaration)
4245 bool success = false;
4246 cp_token_position start = 0;
4247 cp_token *token;
4249 /* Remember where the nested-name-specifier starts. */
4250 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4252 start = cp_lexer_token_position (parser->lexer, false);
4253 push_deferring_access_checks (dk_deferred);
4256 while (true)
4258 tree new_scope;
4259 tree old_scope;
4260 tree saved_qualifying_scope;
4261 bool template_keyword_p;
4263 /* Spot cases that cannot be the beginning of a
4264 nested-name-specifier. */
4265 token = cp_lexer_peek_token (parser->lexer);
4267 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4268 the already parsed nested-name-specifier. */
4269 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4271 /* Grab the nested-name-specifier and continue the loop. */
4272 cp_parser_pre_parsed_nested_name_specifier (parser);
4273 /* If we originally encountered this nested-name-specifier
4274 with IS_DECLARATION set to false, we will not have
4275 resolved TYPENAME_TYPEs, so we must do so here. */
4276 if (is_declaration
4277 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4279 new_scope = resolve_typename_type (parser->scope,
4280 /*only_current_p=*/false);
4281 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4282 parser->scope = new_scope;
4284 success = true;
4285 continue;
4288 /* Spot cases that cannot be the beginning of a
4289 nested-name-specifier. On the second and subsequent times
4290 through the loop, we look for the `template' keyword. */
4291 if (success && token->keyword == RID_TEMPLATE)
4293 /* A template-id can start a nested-name-specifier. */
4294 else if (token->type == CPP_TEMPLATE_ID)
4296 else
4298 /* If the next token is not an identifier, then it is
4299 definitely not a type-name or namespace-name. */
4300 if (token->type != CPP_NAME)
4301 break;
4302 /* If the following token is neither a `<' (to begin a
4303 template-id), nor a `::', then we are not looking at a
4304 nested-name-specifier. */
4305 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4307 if (token->type == CPP_COLON
4308 && parser->colon_corrects_to_scope_p
4309 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4311 error_at (token->location,
4312 "found %<:%> in nested-name-specifier, expected %<::%>");
4313 token->type = CPP_SCOPE;
4316 if (token->type != CPP_SCOPE
4317 && !cp_parser_nth_token_starts_template_argument_list_p
4318 (parser, 2))
4319 break;
4322 /* The nested-name-specifier is optional, so we parse
4323 tentatively. */
4324 cp_parser_parse_tentatively (parser);
4326 /* Look for the optional `template' keyword, if this isn't the
4327 first time through the loop. */
4328 if (success)
4329 template_keyword_p = cp_parser_optional_template_keyword (parser);
4330 else
4331 template_keyword_p = false;
4333 /* Save the old scope since the name lookup we are about to do
4334 might destroy it. */
4335 old_scope = parser->scope;
4336 saved_qualifying_scope = parser->qualifying_scope;
4337 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4338 look up names in "X<T>::I" in order to determine that "Y" is
4339 a template. So, if we have a typename at this point, we make
4340 an effort to look through it. */
4341 if (is_declaration
4342 && !typename_keyword_p
4343 && parser->scope
4344 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4345 parser->scope = resolve_typename_type (parser->scope,
4346 /*only_current_p=*/false);
4347 /* Parse the qualifying entity. */
4348 new_scope
4349 = cp_parser_qualifying_entity (parser,
4350 typename_keyword_p,
4351 template_keyword_p,
4352 check_dependency_p,
4353 type_p,
4354 is_declaration);
4355 /* Look for the `::' token. */
4356 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4358 /* If we found what we wanted, we keep going; otherwise, we're
4359 done. */
4360 if (!cp_parser_parse_definitely (parser))
4362 bool error_p = false;
4364 /* Restore the OLD_SCOPE since it was valid before the
4365 failed attempt at finding the last
4366 class-or-namespace-name. */
4367 parser->scope = old_scope;
4368 parser->qualifying_scope = saved_qualifying_scope;
4369 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4370 break;
4371 /* If the next token is an identifier, and the one after
4372 that is a `::', then any valid interpretation would have
4373 found a class-or-namespace-name. */
4374 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4375 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4376 == CPP_SCOPE)
4377 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4378 != CPP_COMPL))
4380 token = cp_lexer_consume_token (parser->lexer);
4381 if (!error_p)
4383 if (!token->ambiguous_p)
4385 tree decl;
4386 tree ambiguous_decls;
4388 decl = cp_parser_lookup_name (parser, token->u.value,
4389 none_type,
4390 /*is_template=*/false,
4391 /*is_namespace=*/false,
4392 /*check_dependency=*/true,
4393 &ambiguous_decls,
4394 token->location);
4395 if (TREE_CODE (decl) == TEMPLATE_DECL)
4396 error_at (token->location,
4397 "%qD used without template parameters",
4398 decl);
4399 else if (ambiguous_decls)
4401 error_at (token->location,
4402 "reference to %qD is ambiguous",
4403 token->u.value);
4404 print_candidates (ambiguous_decls);
4405 decl = error_mark_node;
4407 else
4409 if (cxx_dialect != cxx98)
4410 cp_parser_name_lookup_error
4411 (parser, token->u.value, decl, NLE_NOT_CXX98,
4412 token->location);
4413 else
4414 cp_parser_name_lookup_error
4415 (parser, token->u.value, decl, NLE_CXX98,
4416 token->location);
4419 parser->scope = error_mark_node;
4420 error_p = true;
4421 /* Treat this as a successful nested-name-specifier
4422 due to:
4424 [basic.lookup.qual]
4426 If the name found is not a class-name (clause
4427 _class_) or namespace-name (_namespace.def_), the
4428 program is ill-formed. */
4429 success = true;
4431 cp_lexer_consume_token (parser->lexer);
4433 break;
4435 /* We've found one valid nested-name-specifier. */
4436 success = true;
4437 /* Name lookup always gives us a DECL. */
4438 if (TREE_CODE (new_scope) == TYPE_DECL)
4439 new_scope = TREE_TYPE (new_scope);
4440 /* Uses of "template" must be followed by actual templates. */
4441 if (template_keyword_p
4442 && !(CLASS_TYPE_P (new_scope)
4443 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4444 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4445 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4446 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4447 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4448 == TEMPLATE_ID_EXPR)))
4449 permerror (input_location, TYPE_P (new_scope)
4450 ? "%qT is not a template"
4451 : "%qD is not a template",
4452 new_scope);
4453 /* If it is a class scope, try to complete it; we are about to
4454 be looking up names inside the class. */
4455 if (TYPE_P (new_scope)
4456 /* Since checking types for dependency can be expensive,
4457 avoid doing it if the type is already complete. */
4458 && !COMPLETE_TYPE_P (new_scope)
4459 /* Do not try to complete dependent types. */
4460 && !dependent_type_p (new_scope))
4462 new_scope = complete_type (new_scope);
4463 /* If it is a typedef to current class, use the current
4464 class instead, as the typedef won't have any names inside
4465 it yet. */
4466 if (!COMPLETE_TYPE_P (new_scope)
4467 && currently_open_class (new_scope))
4468 new_scope = TYPE_MAIN_VARIANT (new_scope);
4470 /* Make sure we look in the right scope the next time through
4471 the loop. */
4472 parser->scope = new_scope;
4475 /* If parsing tentatively, replace the sequence of tokens that makes
4476 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4477 token. That way, should we re-parse the token stream, we will
4478 not have to repeat the effort required to do the parse, nor will
4479 we issue duplicate error messages. */
4480 if (success && start)
4482 cp_token *token;
4484 token = cp_lexer_token_at (parser->lexer, start);
4485 /* Reset the contents of the START token. */
4486 token->type = CPP_NESTED_NAME_SPECIFIER;
4487 /* Retrieve any deferred checks. Do not pop this access checks yet
4488 so the memory will not be reclaimed during token replacing below. */
4489 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4490 token->u.tree_check_value->value = parser->scope;
4491 token->u.tree_check_value->checks = get_deferred_access_checks ();
4492 token->u.tree_check_value->qualifying_scope =
4493 parser->qualifying_scope;
4494 token->keyword = RID_MAX;
4496 /* Purge all subsequent tokens. */
4497 cp_lexer_purge_tokens_after (parser->lexer, start);
4500 if (start)
4501 pop_to_parent_deferring_access_checks ();
4503 return success ? parser->scope : NULL_TREE;
4506 /* Parse a nested-name-specifier. See
4507 cp_parser_nested_name_specifier_opt for details. This function
4508 behaves identically, except that it will an issue an error if no
4509 nested-name-specifier is present. */
4511 static tree
4512 cp_parser_nested_name_specifier (cp_parser *parser,
4513 bool typename_keyword_p,
4514 bool check_dependency_p,
4515 bool type_p,
4516 bool is_declaration)
4518 tree scope;
4520 /* Look for the nested-name-specifier. */
4521 scope = cp_parser_nested_name_specifier_opt (parser,
4522 typename_keyword_p,
4523 check_dependency_p,
4524 type_p,
4525 is_declaration);
4526 /* If it was not present, issue an error message. */
4527 if (!scope)
4529 cp_parser_error (parser, "expected nested-name-specifier");
4530 parser->scope = NULL_TREE;
4533 return scope;
4536 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4537 this is either a class-name or a namespace-name (which corresponds
4538 to the class-or-namespace-name production in the grammar). For
4539 C++0x, it can also be a type-name that refers to an enumeration
4540 type.
4542 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4543 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4544 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4545 TYPE_P is TRUE iff the next name should be taken as a class-name,
4546 even the same name is declared to be another entity in the same
4547 scope.
4549 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4550 specified by the class-or-namespace-name. If neither is found the
4551 ERROR_MARK_NODE is returned. */
4553 static tree
4554 cp_parser_qualifying_entity (cp_parser *parser,
4555 bool typename_keyword_p,
4556 bool template_keyword_p,
4557 bool check_dependency_p,
4558 bool type_p,
4559 bool is_declaration)
4561 tree saved_scope;
4562 tree saved_qualifying_scope;
4563 tree saved_object_scope;
4564 tree scope;
4565 bool only_class_p;
4566 bool successful_parse_p;
4568 /* Before we try to parse the class-name, we must save away the
4569 current PARSER->SCOPE since cp_parser_class_name will destroy
4570 it. */
4571 saved_scope = parser->scope;
4572 saved_qualifying_scope = parser->qualifying_scope;
4573 saved_object_scope = parser->object_scope;
4574 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4575 there is no need to look for a namespace-name. */
4576 only_class_p = template_keyword_p
4577 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4578 if (!only_class_p)
4579 cp_parser_parse_tentatively (parser);
4580 scope = cp_parser_class_name (parser,
4581 typename_keyword_p,
4582 template_keyword_p,
4583 type_p ? class_type : none_type,
4584 check_dependency_p,
4585 /*class_head_p=*/false,
4586 is_declaration);
4587 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4588 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4589 if (!only_class_p
4590 && cxx_dialect != cxx98
4591 && !successful_parse_p)
4593 /* Restore the saved scope. */
4594 parser->scope = saved_scope;
4595 parser->qualifying_scope = saved_qualifying_scope;
4596 parser->object_scope = saved_object_scope;
4598 /* Parse tentatively. */
4599 cp_parser_parse_tentatively (parser);
4601 /* Parse a typedef-name or enum-name. */
4602 scope = cp_parser_nonclass_name (parser);
4604 /* "If the name found does not designate a namespace or a class,
4605 enumeration, or dependent type, the program is ill-formed."
4607 We cover classes and dependent types above and namespaces below,
4608 so this code is only looking for enums. */
4609 if (!scope || TREE_CODE (scope) != TYPE_DECL
4610 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4611 cp_parser_simulate_error (parser);
4613 successful_parse_p = cp_parser_parse_definitely (parser);
4615 /* If that didn't work, try for a namespace-name. */
4616 if (!only_class_p && !successful_parse_p)
4618 /* Restore the saved scope. */
4619 parser->scope = saved_scope;
4620 parser->qualifying_scope = saved_qualifying_scope;
4621 parser->object_scope = saved_object_scope;
4622 /* If we are not looking at an identifier followed by the scope
4623 resolution operator, then this is not part of a
4624 nested-name-specifier. (Note that this function is only used
4625 to parse the components of a nested-name-specifier.) */
4626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4627 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4628 return error_mark_node;
4629 scope = cp_parser_namespace_name (parser);
4632 return scope;
4635 /* Parse a postfix-expression.
4637 postfix-expression:
4638 primary-expression
4639 postfix-expression [ expression ]
4640 postfix-expression ( expression-list [opt] )
4641 simple-type-specifier ( expression-list [opt] )
4642 typename :: [opt] nested-name-specifier identifier
4643 ( expression-list [opt] )
4644 typename :: [opt] nested-name-specifier template [opt] template-id
4645 ( expression-list [opt] )
4646 postfix-expression . template [opt] id-expression
4647 postfix-expression -> template [opt] id-expression
4648 postfix-expression . pseudo-destructor-name
4649 postfix-expression -> pseudo-destructor-name
4650 postfix-expression ++
4651 postfix-expression --
4652 dynamic_cast < type-id > ( expression )
4653 static_cast < type-id > ( expression )
4654 reinterpret_cast < type-id > ( expression )
4655 const_cast < type-id > ( expression )
4656 typeid ( expression )
4657 typeid ( type-id )
4659 GNU Extension:
4661 postfix-expression:
4662 ( type-id ) { initializer-list , [opt] }
4664 This extension is a GNU version of the C99 compound-literal
4665 construct. (The C99 grammar uses `type-name' instead of `type-id',
4666 but they are essentially the same concept.)
4668 If ADDRESS_P is true, the postfix expression is the operand of the
4669 `&' operator. CAST_P is true if this expression is the target of a
4670 cast.
4672 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4673 class member access expressions [expr.ref].
4675 Returns a representation of the expression. */
4677 static tree
4678 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4679 bool member_access_only_p,
4680 cp_id_kind * pidk_return)
4682 cp_token *token;
4683 enum rid keyword;
4684 cp_id_kind idk = CP_ID_KIND_NONE;
4685 tree postfix_expression = NULL_TREE;
4686 bool is_member_access = false;
4688 /* Peek at the next token. */
4689 token = cp_lexer_peek_token (parser->lexer);
4690 /* Some of the productions are determined by keywords. */
4691 keyword = token->keyword;
4692 switch (keyword)
4694 case RID_DYNCAST:
4695 case RID_STATCAST:
4696 case RID_REINTCAST:
4697 case RID_CONSTCAST:
4699 tree type;
4700 tree expression;
4701 const char *saved_message;
4703 /* All of these can be handled in the same way from the point
4704 of view of parsing. Begin by consuming the token
4705 identifying the cast. */
4706 cp_lexer_consume_token (parser->lexer);
4708 /* New types cannot be defined in the cast. */
4709 saved_message = parser->type_definition_forbidden_message;
4710 parser->type_definition_forbidden_message
4711 = G_("types may not be defined in casts");
4713 /* Look for the opening `<'. */
4714 cp_parser_require (parser, CPP_LESS, RT_LESS);
4715 /* Parse the type to which we are casting. */
4716 type = cp_parser_type_id (parser);
4717 /* Look for the closing `>'. */
4718 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4719 /* Restore the old message. */
4720 parser->type_definition_forbidden_message = saved_message;
4722 /* And the expression which is being cast. */
4723 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4724 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4725 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4727 /* Only type conversions to integral or enumeration types
4728 can be used in constant-expressions. */
4729 if (!cast_valid_in_integral_constant_expression_p (type)
4730 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4731 return error_mark_node;
4733 switch (keyword)
4735 case RID_DYNCAST:
4736 postfix_expression
4737 = build_dynamic_cast (type, expression, tf_warning_or_error);
4738 break;
4739 case RID_STATCAST:
4740 postfix_expression
4741 = build_static_cast (type, expression, tf_warning_or_error);
4742 break;
4743 case RID_REINTCAST:
4744 postfix_expression
4745 = build_reinterpret_cast (type, expression,
4746 tf_warning_or_error);
4747 break;
4748 case RID_CONSTCAST:
4749 postfix_expression
4750 = build_const_cast (type, expression, tf_warning_or_error);
4751 break;
4752 default:
4753 gcc_unreachable ();
4756 break;
4758 case RID_TYPEID:
4760 tree type;
4761 const char *saved_message;
4762 bool saved_in_type_id_in_expr_p;
4764 /* Consume the `typeid' token. */
4765 cp_lexer_consume_token (parser->lexer);
4766 /* Look for the `(' token. */
4767 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4768 /* Types cannot be defined in a `typeid' expression. */
4769 saved_message = parser->type_definition_forbidden_message;
4770 parser->type_definition_forbidden_message
4771 = G_("types may not be defined in a %<typeid%> expression");
4772 /* We can't be sure yet whether we're looking at a type-id or an
4773 expression. */
4774 cp_parser_parse_tentatively (parser);
4775 /* Try a type-id first. */
4776 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4777 parser->in_type_id_in_expr_p = true;
4778 type = cp_parser_type_id (parser);
4779 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4780 /* Look for the `)' token. Otherwise, we can't be sure that
4781 we're not looking at an expression: consider `typeid (int
4782 (3))', for example. */
4783 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4784 /* If all went well, simply lookup the type-id. */
4785 if (cp_parser_parse_definitely (parser))
4786 postfix_expression = get_typeid (type);
4787 /* Otherwise, fall back to the expression variant. */
4788 else
4790 tree expression;
4792 /* Look for an expression. */
4793 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4794 /* Compute its typeid. */
4795 postfix_expression = build_typeid (expression);
4796 /* Look for the `)' token. */
4797 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4799 /* Restore the saved message. */
4800 parser->type_definition_forbidden_message = saved_message;
4801 /* `typeid' may not appear in an integral constant expression. */
4802 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4803 return error_mark_node;
4805 break;
4807 case RID_TYPENAME:
4809 tree type;
4810 /* The syntax permitted here is the same permitted for an
4811 elaborated-type-specifier. */
4812 type = cp_parser_elaborated_type_specifier (parser,
4813 /*is_friend=*/false,
4814 /*is_declaration=*/false);
4815 postfix_expression = cp_parser_functional_cast (parser, type);
4817 break;
4819 default:
4821 tree type;
4823 /* If the next thing is a simple-type-specifier, we may be
4824 looking at a functional cast. We could also be looking at
4825 an id-expression. So, we try the functional cast, and if
4826 that doesn't work we fall back to the primary-expression. */
4827 cp_parser_parse_tentatively (parser);
4828 /* Look for the simple-type-specifier. */
4829 type = cp_parser_simple_type_specifier (parser,
4830 /*decl_specs=*/NULL,
4831 CP_PARSER_FLAGS_NONE);
4832 /* Parse the cast itself. */
4833 if (!cp_parser_error_occurred (parser))
4834 postfix_expression
4835 = cp_parser_functional_cast (parser, type);
4836 /* If that worked, we're done. */
4837 if (cp_parser_parse_definitely (parser))
4838 break;
4840 /* If the functional-cast didn't work out, try a
4841 compound-literal. */
4842 if (cp_parser_allow_gnu_extensions_p (parser)
4843 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4845 VEC(constructor_elt,gc) *initializer_list = NULL;
4846 bool saved_in_type_id_in_expr_p;
4848 cp_parser_parse_tentatively (parser);
4849 /* Consume the `('. */
4850 cp_lexer_consume_token (parser->lexer);
4851 /* Parse the type. */
4852 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4853 parser->in_type_id_in_expr_p = true;
4854 type = cp_parser_type_id (parser);
4855 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4856 /* Look for the `)'. */
4857 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4858 /* Look for the `{'. */
4859 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4860 /* If things aren't going well, there's no need to
4861 keep going. */
4862 if (!cp_parser_error_occurred (parser))
4864 bool non_constant_p;
4865 /* Parse the initializer-list. */
4866 initializer_list
4867 = cp_parser_initializer_list (parser, &non_constant_p);
4868 /* Allow a trailing `,'. */
4869 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4870 cp_lexer_consume_token (parser->lexer);
4871 /* Look for the final `}'. */
4872 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4874 /* If that worked, we're definitely looking at a
4875 compound-literal expression. */
4876 if (cp_parser_parse_definitely (parser))
4878 /* Warn the user that a compound literal is not
4879 allowed in standard C++. */
4880 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4881 /* For simplicity, we disallow compound literals in
4882 constant-expressions. We could
4883 allow compound literals of integer type, whose
4884 initializer was a constant, in constant
4885 expressions. Permitting that usage, as a further
4886 extension, would not change the meaning of any
4887 currently accepted programs. (Of course, as
4888 compound literals are not part of ISO C++, the
4889 standard has nothing to say.) */
4890 if (cp_parser_non_integral_constant_expression (parser,
4891 NIC_NCC))
4893 postfix_expression = error_mark_node;
4894 break;
4896 /* Form the representation of the compound-literal. */
4897 postfix_expression
4898 = (finish_compound_literal
4899 (type, build_constructor (init_list_type_node,
4900 initializer_list),
4901 tf_warning_or_error));
4902 break;
4906 /* It must be a primary-expression. */
4907 postfix_expression
4908 = cp_parser_primary_expression (parser, address_p, cast_p,
4909 /*template_arg_p=*/false,
4910 &idk);
4912 break;
4915 /* Keep looping until the postfix-expression is complete. */
4916 while (true)
4918 if (idk == CP_ID_KIND_UNQUALIFIED
4919 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4920 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4921 /* It is not a Koenig lookup function call. */
4922 postfix_expression
4923 = unqualified_name_lookup_error (postfix_expression);
4925 /* Peek at the next token. */
4926 token = cp_lexer_peek_token (parser->lexer);
4928 switch (token->type)
4930 case CPP_OPEN_SQUARE:
4931 postfix_expression
4932 = cp_parser_postfix_open_square_expression (parser,
4933 postfix_expression,
4934 false);
4935 idk = CP_ID_KIND_NONE;
4936 is_member_access = false;
4937 break;
4939 case CPP_OPEN_PAREN:
4940 /* postfix-expression ( expression-list [opt] ) */
4942 bool koenig_p;
4943 bool is_builtin_constant_p;
4944 bool saved_integral_constant_expression_p = false;
4945 bool saved_non_integral_constant_expression_p = false;
4946 VEC(tree,gc) *args;
4948 is_member_access = false;
4950 is_builtin_constant_p
4951 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4952 if (is_builtin_constant_p)
4954 /* The whole point of __builtin_constant_p is to allow
4955 non-constant expressions to appear as arguments. */
4956 saved_integral_constant_expression_p
4957 = parser->integral_constant_expression_p;
4958 saved_non_integral_constant_expression_p
4959 = parser->non_integral_constant_expression_p;
4960 parser->integral_constant_expression_p = false;
4962 args = (cp_parser_parenthesized_expression_list
4963 (parser, non_attr,
4964 /*cast_p=*/false, /*allow_expansion_p=*/true,
4965 /*non_constant_p=*/NULL));
4966 if (is_builtin_constant_p)
4968 parser->integral_constant_expression_p
4969 = saved_integral_constant_expression_p;
4970 parser->non_integral_constant_expression_p
4971 = saved_non_integral_constant_expression_p;
4974 if (args == NULL)
4976 postfix_expression = error_mark_node;
4977 break;
4980 /* Function calls are not permitted in
4981 constant-expressions. */
4982 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4983 && cp_parser_non_integral_constant_expression (parser,
4984 NIC_FUNC_CALL))
4986 postfix_expression = error_mark_node;
4987 release_tree_vector (args);
4988 break;
4991 koenig_p = false;
4992 if (idk == CP_ID_KIND_UNQUALIFIED
4993 || idk == CP_ID_KIND_TEMPLATE_ID)
4995 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4997 if (!VEC_empty (tree, args))
4999 koenig_p = true;
5000 if (!any_type_dependent_arguments_p (args))
5001 postfix_expression
5002 = perform_koenig_lookup (postfix_expression, args,
5003 /*include_std=*/false);
5005 else
5006 postfix_expression
5007 = unqualified_fn_lookup_error (postfix_expression);
5009 /* We do not perform argument-dependent lookup if
5010 normal lookup finds a non-function, in accordance
5011 with the expected resolution of DR 218. */
5012 else if (!VEC_empty (tree, args)
5013 && is_overloaded_fn (postfix_expression))
5015 tree fn = get_first_fn (postfix_expression);
5016 fn = STRIP_TEMPLATE (fn);
5018 /* Do not do argument dependent lookup if regular
5019 lookup finds a member function or a block-scope
5020 function declaration. [basic.lookup.argdep]/3 */
5021 if (!DECL_FUNCTION_MEMBER_P (fn)
5022 && !DECL_LOCAL_FUNCTION_P (fn))
5024 koenig_p = true;
5025 if (!any_type_dependent_arguments_p (args))
5026 postfix_expression
5027 = perform_koenig_lookup (postfix_expression, args,
5028 /*include_std=*/false);
5033 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5035 tree instance = TREE_OPERAND (postfix_expression, 0);
5036 tree fn = TREE_OPERAND (postfix_expression, 1);
5038 if (processing_template_decl
5039 && (type_dependent_expression_p (instance)
5040 || (!BASELINK_P (fn)
5041 && TREE_CODE (fn) != FIELD_DECL)
5042 || type_dependent_expression_p (fn)
5043 || any_type_dependent_arguments_p (args)))
5045 postfix_expression
5046 = build_nt_call_vec (postfix_expression, args);
5047 release_tree_vector (args);
5048 break;
5051 if (BASELINK_P (fn))
5053 postfix_expression
5054 = (build_new_method_call
5055 (instance, fn, &args, NULL_TREE,
5056 (idk == CP_ID_KIND_QUALIFIED
5057 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
5058 /*fn_p=*/NULL,
5059 tf_warning_or_error));
5061 else
5062 postfix_expression
5063 = finish_call_expr (postfix_expression, &args,
5064 /*disallow_virtual=*/false,
5065 /*koenig_p=*/false,
5066 tf_warning_or_error);
5068 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5069 || TREE_CODE (postfix_expression) == MEMBER_REF
5070 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5071 postfix_expression = (build_offset_ref_call_from_tree
5072 (postfix_expression, &args));
5073 else if (idk == CP_ID_KIND_QUALIFIED)
5074 /* A call to a static class member, or a namespace-scope
5075 function. */
5076 postfix_expression
5077 = finish_call_expr (postfix_expression, &args,
5078 /*disallow_virtual=*/true,
5079 koenig_p,
5080 tf_warning_or_error);
5081 else
5082 /* All other function calls. */
5083 postfix_expression
5084 = finish_call_expr (postfix_expression, &args,
5085 /*disallow_virtual=*/false,
5086 koenig_p,
5087 tf_warning_or_error);
5089 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5090 idk = CP_ID_KIND_NONE;
5092 release_tree_vector (args);
5094 break;
5096 case CPP_DOT:
5097 case CPP_DEREF:
5098 /* postfix-expression . template [opt] id-expression
5099 postfix-expression . pseudo-destructor-name
5100 postfix-expression -> template [opt] id-expression
5101 postfix-expression -> pseudo-destructor-name */
5103 /* Consume the `.' or `->' operator. */
5104 cp_lexer_consume_token (parser->lexer);
5106 postfix_expression
5107 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5108 postfix_expression,
5109 false, &idk,
5110 token->location);
5112 is_member_access = true;
5113 break;
5115 case CPP_PLUS_PLUS:
5116 /* postfix-expression ++ */
5117 /* Consume the `++' token. */
5118 cp_lexer_consume_token (parser->lexer);
5119 /* Generate a representation for the complete expression. */
5120 postfix_expression
5121 = finish_increment_expr (postfix_expression,
5122 POSTINCREMENT_EXPR);
5123 /* Increments may not appear in constant-expressions. */
5124 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5125 postfix_expression = error_mark_node;
5126 idk = CP_ID_KIND_NONE;
5127 is_member_access = false;
5128 break;
5130 case CPP_MINUS_MINUS:
5131 /* postfix-expression -- */
5132 /* Consume the `--' token. */
5133 cp_lexer_consume_token (parser->lexer);
5134 /* Generate a representation for the complete expression. */
5135 postfix_expression
5136 = finish_increment_expr (postfix_expression,
5137 POSTDECREMENT_EXPR);
5138 /* Decrements may not appear in constant-expressions. */
5139 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5140 postfix_expression = error_mark_node;
5141 idk = CP_ID_KIND_NONE;
5142 is_member_access = false;
5143 break;
5145 default:
5146 if (pidk_return != NULL)
5147 * pidk_return = idk;
5148 if (member_access_only_p)
5149 return is_member_access? postfix_expression : error_mark_node;
5150 else
5151 return postfix_expression;
5155 /* We should never get here. */
5156 gcc_unreachable ();
5157 return error_mark_node;
5160 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5161 by cp_parser_builtin_offsetof. We're looking for
5163 postfix-expression [ expression ]
5165 FOR_OFFSETOF is set if we're being called in that context, which
5166 changes how we deal with integer constant expressions. */
5168 static tree
5169 cp_parser_postfix_open_square_expression (cp_parser *parser,
5170 tree postfix_expression,
5171 bool for_offsetof)
5173 tree index;
5175 /* Consume the `[' token. */
5176 cp_lexer_consume_token (parser->lexer);
5178 /* Parse the index expression. */
5179 /* ??? For offsetof, there is a question of what to allow here. If
5180 offsetof is not being used in an integral constant expression context,
5181 then we *could* get the right answer by computing the value at runtime.
5182 If we are in an integral constant expression context, then we might
5183 could accept any constant expression; hard to say without analysis.
5184 Rather than open the barn door too wide right away, allow only integer
5185 constant expressions here. */
5186 if (for_offsetof)
5187 index = cp_parser_constant_expression (parser, false, NULL);
5188 else
5189 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5191 /* Look for the closing `]'. */
5192 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5194 /* Build the ARRAY_REF. */
5195 postfix_expression = grok_array_decl (postfix_expression, index);
5197 /* When not doing offsetof, array references are not permitted in
5198 constant-expressions. */
5199 if (!for_offsetof
5200 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5201 postfix_expression = error_mark_node;
5203 return postfix_expression;
5206 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5207 by cp_parser_builtin_offsetof. We're looking for
5209 postfix-expression . template [opt] id-expression
5210 postfix-expression . pseudo-destructor-name
5211 postfix-expression -> template [opt] id-expression
5212 postfix-expression -> pseudo-destructor-name
5214 FOR_OFFSETOF is set if we're being called in that context. That sorta
5215 limits what of the above we'll actually accept, but nevermind.
5216 TOKEN_TYPE is the "." or "->" token, which will already have been
5217 removed from the stream. */
5219 static tree
5220 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5221 enum cpp_ttype token_type,
5222 tree postfix_expression,
5223 bool for_offsetof, cp_id_kind *idk,
5224 location_t location)
5226 tree name;
5227 bool dependent_p;
5228 bool pseudo_destructor_p;
5229 tree scope = NULL_TREE;
5231 /* If this is a `->' operator, dereference the pointer. */
5232 if (token_type == CPP_DEREF)
5233 postfix_expression = build_x_arrow (postfix_expression);
5234 /* Check to see whether or not the expression is type-dependent. */
5235 dependent_p = type_dependent_expression_p (postfix_expression);
5236 /* The identifier following the `->' or `.' is not qualified. */
5237 parser->scope = NULL_TREE;
5238 parser->qualifying_scope = NULL_TREE;
5239 parser->object_scope = NULL_TREE;
5240 *idk = CP_ID_KIND_NONE;
5242 /* Enter the scope corresponding to the type of the object
5243 given by the POSTFIX_EXPRESSION. */
5244 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5246 scope = TREE_TYPE (postfix_expression);
5247 /* According to the standard, no expression should ever have
5248 reference type. Unfortunately, we do not currently match
5249 the standard in this respect in that our internal representation
5250 of an expression may have reference type even when the standard
5251 says it does not. Therefore, we have to manually obtain the
5252 underlying type here. */
5253 scope = non_reference (scope);
5254 /* The type of the POSTFIX_EXPRESSION must be complete. */
5255 if (scope == unknown_type_node)
5257 error_at (location, "%qE does not have class type",
5258 postfix_expression);
5259 scope = NULL_TREE;
5261 else
5262 scope = complete_type_or_else (scope, NULL_TREE);
5263 /* Let the name lookup machinery know that we are processing a
5264 class member access expression. */
5265 parser->context->object_type = scope;
5266 /* If something went wrong, we want to be able to discern that case,
5267 as opposed to the case where there was no SCOPE due to the type
5268 of expression being dependent. */
5269 if (!scope)
5270 scope = error_mark_node;
5271 /* If the SCOPE was erroneous, make the various semantic analysis
5272 functions exit quickly -- and without issuing additional error
5273 messages. */
5274 if (scope == error_mark_node)
5275 postfix_expression = error_mark_node;
5278 /* Assume this expression is not a pseudo-destructor access. */
5279 pseudo_destructor_p = false;
5281 /* If the SCOPE is a scalar type, then, if this is a valid program,
5282 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5283 is type dependent, it can be pseudo-destructor-name or something else.
5284 Try to parse it as pseudo-destructor-name first. */
5285 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5287 tree s;
5288 tree type;
5290 cp_parser_parse_tentatively (parser);
5291 /* Parse the pseudo-destructor-name. */
5292 s = NULL_TREE;
5293 cp_parser_pseudo_destructor_name (parser, &s, &type);
5294 if (dependent_p
5295 && (cp_parser_error_occurred (parser)
5296 || TREE_CODE (type) != TYPE_DECL
5297 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5298 cp_parser_abort_tentative_parse (parser);
5299 else if (cp_parser_parse_definitely (parser))
5301 pseudo_destructor_p = true;
5302 postfix_expression
5303 = finish_pseudo_destructor_expr (postfix_expression,
5304 s, TREE_TYPE (type));
5308 if (!pseudo_destructor_p)
5310 /* If the SCOPE is not a scalar type, we are looking at an
5311 ordinary class member access expression, rather than a
5312 pseudo-destructor-name. */
5313 bool template_p;
5314 cp_token *token = cp_lexer_peek_token (parser->lexer);
5315 /* Parse the id-expression. */
5316 name = (cp_parser_id_expression
5317 (parser,
5318 cp_parser_optional_template_keyword (parser),
5319 /*check_dependency_p=*/true,
5320 &template_p,
5321 /*declarator_p=*/false,
5322 /*optional_p=*/false));
5323 /* In general, build a SCOPE_REF if the member name is qualified.
5324 However, if the name was not dependent and has already been
5325 resolved; there is no need to build the SCOPE_REF. For example;
5327 struct X { void f(); };
5328 template <typename T> void f(T* t) { t->X::f(); }
5330 Even though "t" is dependent, "X::f" is not and has been resolved
5331 to a BASELINK; there is no need to include scope information. */
5333 /* But we do need to remember that there was an explicit scope for
5334 virtual function calls. */
5335 if (parser->scope)
5336 *idk = CP_ID_KIND_QUALIFIED;
5338 /* If the name is a template-id that names a type, we will get a
5339 TYPE_DECL here. That is invalid code. */
5340 if (TREE_CODE (name) == TYPE_DECL)
5342 error_at (token->location, "invalid use of %qD", name);
5343 postfix_expression = error_mark_node;
5345 else
5347 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5349 name = build_qualified_name (/*type=*/NULL_TREE,
5350 parser->scope,
5351 name,
5352 template_p);
5353 parser->scope = NULL_TREE;
5354 parser->qualifying_scope = NULL_TREE;
5355 parser->object_scope = NULL_TREE;
5357 if (scope && name && BASELINK_P (name))
5358 adjust_result_of_qualified_name_lookup
5359 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5360 postfix_expression
5361 = finish_class_member_access_expr (postfix_expression, name,
5362 template_p,
5363 tf_warning_or_error);
5367 /* We no longer need to look up names in the scope of the object on
5368 the left-hand side of the `.' or `->' operator. */
5369 parser->context->object_type = NULL_TREE;
5371 /* Outside of offsetof, these operators may not appear in
5372 constant-expressions. */
5373 if (!for_offsetof
5374 && (cp_parser_non_integral_constant_expression
5375 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5376 postfix_expression = error_mark_node;
5378 return postfix_expression;
5381 /* Parse a parenthesized expression-list.
5383 expression-list:
5384 assignment-expression
5385 expression-list, assignment-expression
5387 attribute-list:
5388 expression-list
5389 identifier
5390 identifier, expression-list
5392 CAST_P is true if this expression is the target of a cast.
5394 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5395 argument pack.
5397 Returns a vector of trees. Each element is a representation of an
5398 assignment-expression. NULL is returned if the ( and or ) are
5399 missing. An empty, but allocated, vector is returned on no
5400 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5401 if we are parsing an attribute list for an attribute that wants a
5402 plain identifier argument, normal_attr for an attribute that wants
5403 an expression, or non_attr if we aren't parsing an attribute list. If
5404 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5405 not all of the expressions in the list were constant. */
5407 static VEC(tree,gc) *
5408 cp_parser_parenthesized_expression_list (cp_parser* parser,
5409 int is_attribute_list,
5410 bool cast_p,
5411 bool allow_expansion_p,
5412 bool *non_constant_p)
5414 VEC(tree,gc) *expression_list;
5415 bool fold_expr_p = is_attribute_list != non_attr;
5416 tree identifier = NULL_TREE;
5417 bool saved_greater_than_is_operator_p;
5419 /* Assume all the expressions will be constant. */
5420 if (non_constant_p)
5421 *non_constant_p = false;
5423 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5424 return NULL;
5426 expression_list = make_tree_vector ();
5428 /* Within a parenthesized expression, a `>' token is always
5429 the greater-than operator. */
5430 saved_greater_than_is_operator_p
5431 = parser->greater_than_is_operator_p;
5432 parser->greater_than_is_operator_p = true;
5434 /* Consume expressions until there are no more. */
5435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5436 while (true)
5438 tree expr;
5440 /* At the beginning of attribute lists, check to see if the
5441 next token is an identifier. */
5442 if (is_attribute_list == id_attr
5443 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5445 cp_token *token;
5447 /* Consume the identifier. */
5448 token = cp_lexer_consume_token (parser->lexer);
5449 /* Save the identifier. */
5450 identifier = token->u.value;
5452 else
5454 bool expr_non_constant_p;
5456 /* Parse the next assignment-expression. */
5457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5459 /* A braced-init-list. */
5460 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5461 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5462 if (non_constant_p && expr_non_constant_p)
5463 *non_constant_p = true;
5465 else if (non_constant_p)
5467 expr = (cp_parser_constant_expression
5468 (parser, /*allow_non_constant_p=*/true,
5469 &expr_non_constant_p));
5470 if (expr_non_constant_p)
5471 *non_constant_p = true;
5473 else
5474 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5476 if (fold_expr_p)
5477 expr = fold_non_dependent_expr (expr);
5479 /* If we have an ellipsis, then this is an expression
5480 expansion. */
5481 if (allow_expansion_p
5482 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5484 /* Consume the `...'. */
5485 cp_lexer_consume_token (parser->lexer);
5487 /* Build the argument pack. */
5488 expr = make_pack_expansion (expr);
5491 /* Add it to the list. We add error_mark_node
5492 expressions to the list, so that we can still tell if
5493 the correct form for a parenthesized expression-list
5494 is found. That gives better errors. */
5495 VEC_safe_push (tree, gc, expression_list, expr);
5497 if (expr == error_mark_node)
5498 goto skip_comma;
5501 /* After the first item, attribute lists look the same as
5502 expression lists. */
5503 is_attribute_list = non_attr;
5505 get_comma:;
5506 /* If the next token isn't a `,', then we are done. */
5507 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5508 break;
5510 /* Otherwise, consume the `,' and keep going. */
5511 cp_lexer_consume_token (parser->lexer);
5514 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5516 int ending;
5518 skip_comma:;
5519 /* We try and resync to an unnested comma, as that will give the
5520 user better diagnostics. */
5521 ending = cp_parser_skip_to_closing_parenthesis (parser,
5522 /*recovering=*/true,
5523 /*or_comma=*/true,
5524 /*consume_paren=*/true);
5525 if (ending < 0)
5526 goto get_comma;
5527 if (!ending)
5529 parser->greater_than_is_operator_p
5530 = saved_greater_than_is_operator_p;
5531 return NULL;
5535 parser->greater_than_is_operator_p
5536 = saved_greater_than_is_operator_p;
5538 if (identifier)
5539 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5541 return expression_list;
5544 /* Parse a pseudo-destructor-name.
5546 pseudo-destructor-name:
5547 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5548 :: [opt] nested-name-specifier template template-id :: ~ type-name
5549 :: [opt] nested-name-specifier [opt] ~ type-name
5551 If either of the first two productions is used, sets *SCOPE to the
5552 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5553 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5554 or ERROR_MARK_NODE if the parse fails. */
5556 static void
5557 cp_parser_pseudo_destructor_name (cp_parser* parser,
5558 tree* scope,
5559 tree* type)
5561 bool nested_name_specifier_p;
5563 /* Assume that things will not work out. */
5564 *type = error_mark_node;
5566 /* Look for the optional `::' operator. */
5567 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5568 /* Look for the optional nested-name-specifier. */
5569 nested_name_specifier_p
5570 = (cp_parser_nested_name_specifier_opt (parser,
5571 /*typename_keyword_p=*/false,
5572 /*check_dependency_p=*/true,
5573 /*type_p=*/false,
5574 /*is_declaration=*/false)
5575 != NULL_TREE);
5576 /* Now, if we saw a nested-name-specifier, we might be doing the
5577 second production. */
5578 if (nested_name_specifier_p
5579 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5581 /* Consume the `template' keyword. */
5582 cp_lexer_consume_token (parser->lexer);
5583 /* Parse the template-id. */
5584 cp_parser_template_id (parser,
5585 /*template_keyword_p=*/true,
5586 /*check_dependency_p=*/false,
5587 /*is_declaration=*/true);
5588 /* Look for the `::' token. */
5589 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5591 /* If the next token is not a `~', then there might be some
5592 additional qualification. */
5593 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5595 /* At this point, we're looking for "type-name :: ~". The type-name
5596 must not be a class-name, since this is a pseudo-destructor. So,
5597 it must be either an enum-name, or a typedef-name -- both of which
5598 are just identifiers. So, we peek ahead to check that the "::"
5599 and "~" tokens are present; if they are not, then we can avoid
5600 calling type_name. */
5601 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5602 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5603 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5605 cp_parser_error (parser, "non-scalar type");
5606 return;
5609 /* Look for the type-name. */
5610 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5611 if (*scope == error_mark_node)
5612 return;
5614 /* Look for the `::' token. */
5615 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5617 else
5618 *scope = NULL_TREE;
5620 /* Look for the `~'. */
5621 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5622 /* Look for the type-name again. We are not responsible for
5623 checking that it matches the first type-name. */
5624 *type = cp_parser_nonclass_name (parser);
5627 /* Parse a unary-expression.
5629 unary-expression:
5630 postfix-expression
5631 ++ cast-expression
5632 -- cast-expression
5633 unary-operator cast-expression
5634 sizeof unary-expression
5635 sizeof ( type-id )
5636 alignof ( type-id ) [C++0x]
5637 new-expression
5638 delete-expression
5640 GNU Extensions:
5642 unary-expression:
5643 __extension__ cast-expression
5644 __alignof__ unary-expression
5645 __alignof__ ( type-id )
5646 alignof unary-expression [C++0x]
5647 __real__ cast-expression
5648 __imag__ cast-expression
5649 && identifier
5651 ADDRESS_P is true iff the unary-expression is appearing as the
5652 operand of the `&' operator. CAST_P is true if this expression is
5653 the target of a cast.
5655 Returns a representation of the expression. */
5657 static tree
5658 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5659 cp_id_kind * pidk)
5661 cp_token *token;
5662 enum tree_code unary_operator;
5664 /* Peek at the next token. */
5665 token = cp_lexer_peek_token (parser->lexer);
5666 /* Some keywords give away the kind of expression. */
5667 if (token->type == CPP_KEYWORD)
5669 enum rid keyword = token->keyword;
5671 switch (keyword)
5673 case RID_ALIGNOF:
5674 case RID_SIZEOF:
5676 tree operand;
5677 enum tree_code op;
5679 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5680 /* Consume the token. */
5681 cp_lexer_consume_token (parser->lexer);
5682 /* Parse the operand. */
5683 operand = cp_parser_sizeof_operand (parser, keyword);
5685 if (TYPE_P (operand))
5686 return cxx_sizeof_or_alignof_type (operand, op, true);
5687 else
5689 /* ISO C++ defines alignof only with types, not with
5690 expressions. So pedwarn if alignof is used with a non-
5691 type expression. However, __alignof__ is ok. */
5692 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5693 pedwarn (token->location, OPT_pedantic,
5694 "ISO C++ does not allow %<alignof%> "
5695 "with a non-type");
5697 return cxx_sizeof_or_alignof_expr (operand, op, true);
5701 case RID_NEW:
5702 return cp_parser_new_expression (parser);
5704 case RID_DELETE:
5705 return cp_parser_delete_expression (parser);
5707 case RID_EXTENSION:
5709 /* The saved value of the PEDANTIC flag. */
5710 int saved_pedantic;
5711 tree expr;
5713 /* Save away the PEDANTIC flag. */
5714 cp_parser_extension_opt (parser, &saved_pedantic);
5715 /* Parse the cast-expression. */
5716 expr = cp_parser_simple_cast_expression (parser);
5717 /* Restore the PEDANTIC flag. */
5718 pedantic = saved_pedantic;
5720 return expr;
5723 case RID_REALPART:
5724 case RID_IMAGPART:
5726 tree expression;
5728 /* Consume the `__real__' or `__imag__' token. */
5729 cp_lexer_consume_token (parser->lexer);
5730 /* Parse the cast-expression. */
5731 expression = cp_parser_simple_cast_expression (parser);
5732 /* Create the complete representation. */
5733 return build_x_unary_op ((keyword == RID_REALPART
5734 ? REALPART_EXPR : IMAGPART_EXPR),
5735 expression,
5736 tf_warning_or_error);
5738 break;
5740 case RID_NOEXCEPT:
5742 tree expr;
5743 const char *saved_message;
5744 bool saved_integral_constant_expression_p;
5745 bool saved_non_integral_constant_expression_p;
5746 bool saved_greater_than_is_operator_p;
5748 cp_lexer_consume_token (parser->lexer);
5749 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5751 saved_message = parser->type_definition_forbidden_message;
5752 parser->type_definition_forbidden_message
5753 = G_("types may not be defined in %<noexcept%> expressions");
5755 saved_integral_constant_expression_p
5756 = parser->integral_constant_expression_p;
5757 saved_non_integral_constant_expression_p
5758 = parser->non_integral_constant_expression_p;
5759 parser->integral_constant_expression_p = false;
5761 saved_greater_than_is_operator_p
5762 = parser->greater_than_is_operator_p;
5763 parser->greater_than_is_operator_p = true;
5765 ++cp_unevaluated_operand;
5766 ++c_inhibit_evaluation_warnings;
5767 expr = cp_parser_expression (parser, false, NULL);
5768 --c_inhibit_evaluation_warnings;
5769 --cp_unevaluated_operand;
5771 parser->greater_than_is_operator_p
5772 = saved_greater_than_is_operator_p;
5774 parser->integral_constant_expression_p
5775 = saved_integral_constant_expression_p;
5776 parser->non_integral_constant_expression_p
5777 = saved_non_integral_constant_expression_p;
5779 parser->type_definition_forbidden_message = saved_message;
5781 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5782 return finish_noexcept_expr (expr, tf_warning_or_error);
5785 default:
5786 break;
5790 /* Look for the `:: new' and `:: delete', which also signal the
5791 beginning of a new-expression, or delete-expression,
5792 respectively. If the next token is `::', then it might be one of
5793 these. */
5794 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5796 enum rid keyword;
5798 /* See if the token after the `::' is one of the keywords in
5799 which we're interested. */
5800 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5801 /* If it's `new', we have a new-expression. */
5802 if (keyword == RID_NEW)
5803 return cp_parser_new_expression (parser);
5804 /* Similarly, for `delete'. */
5805 else if (keyword == RID_DELETE)
5806 return cp_parser_delete_expression (parser);
5809 /* Look for a unary operator. */
5810 unary_operator = cp_parser_unary_operator (token);
5811 /* The `++' and `--' operators can be handled similarly, even though
5812 they are not technically unary-operators in the grammar. */
5813 if (unary_operator == ERROR_MARK)
5815 if (token->type == CPP_PLUS_PLUS)
5816 unary_operator = PREINCREMENT_EXPR;
5817 else if (token->type == CPP_MINUS_MINUS)
5818 unary_operator = PREDECREMENT_EXPR;
5819 /* Handle the GNU address-of-label extension. */
5820 else if (cp_parser_allow_gnu_extensions_p (parser)
5821 && token->type == CPP_AND_AND)
5823 tree identifier;
5824 tree expression;
5825 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5827 /* Consume the '&&' token. */
5828 cp_lexer_consume_token (parser->lexer);
5829 /* Look for the identifier. */
5830 identifier = cp_parser_identifier (parser);
5831 /* Create an expression representing the address. */
5832 expression = finish_label_address_expr (identifier, loc);
5833 if (cp_parser_non_integral_constant_expression (parser,
5834 NIC_ADDR_LABEL))
5835 expression = error_mark_node;
5836 return expression;
5839 if (unary_operator != ERROR_MARK)
5841 tree cast_expression;
5842 tree expression = error_mark_node;
5843 non_integral_constant non_constant_p = NIC_NONE;
5845 /* Consume the operator token. */
5846 token = cp_lexer_consume_token (parser->lexer);
5847 /* Parse the cast-expression. */
5848 cast_expression
5849 = cp_parser_cast_expression (parser,
5850 unary_operator == ADDR_EXPR,
5851 /*cast_p=*/false, pidk);
5852 /* Now, build an appropriate representation. */
5853 switch (unary_operator)
5855 case INDIRECT_REF:
5856 non_constant_p = NIC_STAR;
5857 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5858 tf_warning_or_error);
5859 break;
5861 case ADDR_EXPR:
5862 non_constant_p = NIC_ADDR;
5863 /* Fall through. */
5864 case BIT_NOT_EXPR:
5865 expression = build_x_unary_op (unary_operator, cast_expression,
5866 tf_warning_or_error);
5867 break;
5869 case PREINCREMENT_EXPR:
5870 case PREDECREMENT_EXPR:
5871 non_constant_p = unary_operator == PREINCREMENT_EXPR
5872 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5873 /* Fall through. */
5874 case UNARY_PLUS_EXPR:
5875 case NEGATE_EXPR:
5876 case TRUTH_NOT_EXPR:
5877 expression = finish_unary_op_expr (unary_operator, cast_expression);
5878 break;
5880 default:
5881 gcc_unreachable ();
5884 if (non_constant_p != NIC_NONE
5885 && cp_parser_non_integral_constant_expression (parser,
5886 non_constant_p))
5887 expression = error_mark_node;
5889 return expression;
5892 return cp_parser_postfix_expression (parser, address_p, cast_p,
5893 /*member_access_only_p=*/false,
5894 pidk);
5897 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5898 unary-operator, the corresponding tree code is returned. */
5900 static enum tree_code
5901 cp_parser_unary_operator (cp_token* token)
5903 switch (token->type)
5905 case CPP_MULT:
5906 return INDIRECT_REF;
5908 case CPP_AND:
5909 return ADDR_EXPR;
5911 case CPP_PLUS:
5912 return UNARY_PLUS_EXPR;
5914 case CPP_MINUS:
5915 return NEGATE_EXPR;
5917 case CPP_NOT:
5918 return TRUTH_NOT_EXPR;
5920 case CPP_COMPL:
5921 return BIT_NOT_EXPR;
5923 default:
5924 return ERROR_MARK;
5928 /* Parse a new-expression.
5930 new-expression:
5931 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5932 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5934 Returns a representation of the expression. */
5936 static tree
5937 cp_parser_new_expression (cp_parser* parser)
5939 bool global_scope_p;
5940 VEC(tree,gc) *placement;
5941 tree type;
5942 VEC(tree,gc) *initializer;
5943 tree nelts;
5944 tree ret;
5946 /* Look for the optional `::' operator. */
5947 global_scope_p
5948 = (cp_parser_global_scope_opt (parser,
5949 /*current_scope_valid_p=*/false)
5950 != NULL_TREE);
5951 /* Look for the `new' operator. */
5952 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
5953 /* There's no easy way to tell a new-placement from the
5954 `( type-id )' construct. */
5955 cp_parser_parse_tentatively (parser);
5956 /* Look for a new-placement. */
5957 placement = cp_parser_new_placement (parser);
5958 /* If that didn't work out, there's no new-placement. */
5959 if (!cp_parser_parse_definitely (parser))
5961 if (placement != NULL)
5962 release_tree_vector (placement);
5963 placement = NULL;
5966 /* If the next token is a `(', then we have a parenthesized
5967 type-id. */
5968 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5970 cp_token *token;
5971 /* Consume the `('. */
5972 cp_lexer_consume_token (parser->lexer);
5973 /* Parse the type-id. */
5974 type = cp_parser_type_id (parser);
5975 /* Look for the closing `)'. */
5976 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5977 token = cp_lexer_peek_token (parser->lexer);
5978 /* There should not be a direct-new-declarator in this production,
5979 but GCC used to allowed this, so we check and emit a sensible error
5980 message for this case. */
5981 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5983 error_at (token->location,
5984 "array bound forbidden after parenthesized type-id");
5985 inform (token->location,
5986 "try removing the parentheses around the type-id");
5987 cp_parser_direct_new_declarator (parser);
5989 nelts = NULL_TREE;
5991 /* Otherwise, there must be a new-type-id. */
5992 else
5993 type = cp_parser_new_type_id (parser, &nelts);
5995 /* If the next token is a `(' or '{', then we have a new-initializer. */
5996 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5997 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5998 initializer = cp_parser_new_initializer (parser);
5999 else
6000 initializer = NULL;
6002 /* A new-expression may not appear in an integral constant
6003 expression. */
6004 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6005 ret = error_mark_node;
6006 else
6008 /* Create a representation of the new-expression. */
6009 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6010 tf_warning_or_error);
6013 if (placement != NULL)
6014 release_tree_vector (placement);
6015 if (initializer != NULL)
6016 release_tree_vector (initializer);
6018 return ret;
6021 /* Parse a new-placement.
6023 new-placement:
6024 ( expression-list )
6026 Returns the same representation as for an expression-list. */
6028 static VEC(tree,gc) *
6029 cp_parser_new_placement (cp_parser* parser)
6031 VEC(tree,gc) *expression_list;
6033 /* Parse the expression-list. */
6034 expression_list = (cp_parser_parenthesized_expression_list
6035 (parser, non_attr, /*cast_p=*/false,
6036 /*allow_expansion_p=*/true,
6037 /*non_constant_p=*/NULL));
6039 return expression_list;
6042 /* Parse a new-type-id.
6044 new-type-id:
6045 type-specifier-seq new-declarator [opt]
6047 Returns the TYPE allocated. If the new-type-id indicates an array
6048 type, *NELTS is set to the number of elements in the last array
6049 bound; the TYPE will not include the last array bound. */
6051 static tree
6052 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6054 cp_decl_specifier_seq type_specifier_seq;
6055 cp_declarator *new_declarator;
6056 cp_declarator *declarator;
6057 cp_declarator *outer_declarator;
6058 const char *saved_message;
6059 tree type;
6061 /* The type-specifier sequence must not contain type definitions.
6062 (It cannot contain declarations of new types either, but if they
6063 are not definitions we will catch that because they are not
6064 complete.) */
6065 saved_message = parser->type_definition_forbidden_message;
6066 parser->type_definition_forbidden_message
6067 = G_("types may not be defined in a new-type-id");
6068 /* Parse the type-specifier-seq. */
6069 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6070 /*is_trailing_return=*/false,
6071 &type_specifier_seq);
6072 /* Restore the old message. */
6073 parser->type_definition_forbidden_message = saved_message;
6074 /* Parse the new-declarator. */
6075 new_declarator = cp_parser_new_declarator_opt (parser);
6077 /* Determine the number of elements in the last array dimension, if
6078 any. */
6079 *nelts = NULL_TREE;
6080 /* Skip down to the last array dimension. */
6081 declarator = new_declarator;
6082 outer_declarator = NULL;
6083 while (declarator && (declarator->kind == cdk_pointer
6084 || declarator->kind == cdk_ptrmem))
6086 outer_declarator = declarator;
6087 declarator = declarator->declarator;
6089 while (declarator
6090 && declarator->kind == cdk_array
6091 && declarator->declarator
6092 && declarator->declarator->kind == cdk_array)
6094 outer_declarator = declarator;
6095 declarator = declarator->declarator;
6098 if (declarator && declarator->kind == cdk_array)
6100 *nelts = declarator->u.array.bounds;
6101 if (*nelts == error_mark_node)
6102 *nelts = integer_one_node;
6104 if (outer_declarator)
6105 outer_declarator->declarator = declarator->declarator;
6106 else
6107 new_declarator = NULL;
6110 type = groktypename (&type_specifier_seq, new_declarator, false);
6111 return type;
6114 /* Parse an (optional) new-declarator.
6116 new-declarator:
6117 ptr-operator new-declarator [opt]
6118 direct-new-declarator
6120 Returns the declarator. */
6122 static cp_declarator *
6123 cp_parser_new_declarator_opt (cp_parser* parser)
6125 enum tree_code code;
6126 tree type;
6127 cp_cv_quals cv_quals;
6129 /* We don't know if there's a ptr-operator next, or not. */
6130 cp_parser_parse_tentatively (parser);
6131 /* Look for a ptr-operator. */
6132 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6133 /* If that worked, look for more new-declarators. */
6134 if (cp_parser_parse_definitely (parser))
6136 cp_declarator *declarator;
6138 /* Parse another optional declarator. */
6139 declarator = cp_parser_new_declarator_opt (parser);
6141 return cp_parser_make_indirect_declarator
6142 (code, type, cv_quals, declarator);
6145 /* If the next token is a `[', there is a direct-new-declarator. */
6146 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6147 return cp_parser_direct_new_declarator (parser);
6149 return NULL;
6152 /* Parse a direct-new-declarator.
6154 direct-new-declarator:
6155 [ expression ]
6156 direct-new-declarator [constant-expression]
6160 static cp_declarator *
6161 cp_parser_direct_new_declarator (cp_parser* parser)
6163 cp_declarator *declarator = NULL;
6165 while (true)
6167 tree expression;
6169 /* Look for the opening `['. */
6170 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6171 /* The first expression is not required to be constant. */
6172 if (!declarator)
6174 cp_token *token = cp_lexer_peek_token (parser->lexer);
6175 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6176 /* The standard requires that the expression have integral
6177 type. DR 74 adds enumeration types. We believe that the
6178 real intent is that these expressions be handled like the
6179 expression in a `switch' condition, which also allows
6180 classes with a single conversion to integral or
6181 enumeration type. */
6182 if (!processing_template_decl)
6184 expression
6185 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6186 expression,
6187 /*complain=*/true);
6188 if (!expression)
6190 error_at (token->location,
6191 "expression in new-declarator must have integral "
6192 "or enumeration type");
6193 expression = error_mark_node;
6197 /* But all the other expressions must be. */
6198 else
6199 expression
6200 = cp_parser_constant_expression (parser,
6201 /*allow_non_constant=*/false,
6202 NULL);
6203 /* Look for the closing `]'. */
6204 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6206 /* Add this bound to the declarator. */
6207 declarator = make_array_declarator (declarator, expression);
6209 /* If the next token is not a `[', then there are no more
6210 bounds. */
6211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6212 break;
6215 return declarator;
6218 /* Parse a new-initializer.
6220 new-initializer:
6221 ( expression-list [opt] )
6222 braced-init-list
6224 Returns a representation of the expression-list. */
6226 static VEC(tree,gc) *
6227 cp_parser_new_initializer (cp_parser* parser)
6229 VEC(tree,gc) *expression_list;
6231 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6233 tree t;
6234 bool expr_non_constant_p;
6235 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6236 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6237 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6238 expression_list = make_tree_vector_single (t);
6240 else
6241 expression_list = (cp_parser_parenthesized_expression_list
6242 (parser, non_attr, /*cast_p=*/false,
6243 /*allow_expansion_p=*/true,
6244 /*non_constant_p=*/NULL));
6246 return expression_list;
6249 /* Parse a delete-expression.
6251 delete-expression:
6252 :: [opt] delete cast-expression
6253 :: [opt] delete [ ] cast-expression
6255 Returns a representation of the expression. */
6257 static tree
6258 cp_parser_delete_expression (cp_parser* parser)
6260 bool global_scope_p;
6261 bool array_p;
6262 tree expression;
6264 /* Look for the optional `::' operator. */
6265 global_scope_p
6266 = (cp_parser_global_scope_opt (parser,
6267 /*current_scope_valid_p=*/false)
6268 != NULL_TREE);
6269 /* Look for the `delete' keyword. */
6270 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6271 /* See if the array syntax is in use. */
6272 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6274 /* Consume the `[' token. */
6275 cp_lexer_consume_token (parser->lexer);
6276 /* Look for the `]' token. */
6277 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6278 /* Remember that this is the `[]' construct. */
6279 array_p = true;
6281 else
6282 array_p = false;
6284 /* Parse the cast-expression. */
6285 expression = cp_parser_simple_cast_expression (parser);
6287 /* A delete-expression may not appear in an integral constant
6288 expression. */
6289 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6290 return error_mark_node;
6292 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6295 /* Returns true if TOKEN may start a cast-expression and false
6296 otherwise. */
6298 static bool
6299 cp_parser_token_starts_cast_expression (cp_token *token)
6301 switch (token->type)
6303 case CPP_COMMA:
6304 case CPP_SEMICOLON:
6305 case CPP_QUERY:
6306 case CPP_COLON:
6307 case CPP_CLOSE_SQUARE:
6308 case CPP_CLOSE_PAREN:
6309 case CPP_CLOSE_BRACE:
6310 case CPP_DOT:
6311 case CPP_DOT_STAR:
6312 case CPP_DEREF:
6313 case CPP_DEREF_STAR:
6314 case CPP_DIV:
6315 case CPP_MOD:
6316 case CPP_LSHIFT:
6317 case CPP_RSHIFT:
6318 case CPP_LESS:
6319 case CPP_GREATER:
6320 case CPP_LESS_EQ:
6321 case CPP_GREATER_EQ:
6322 case CPP_EQ_EQ:
6323 case CPP_NOT_EQ:
6324 case CPP_EQ:
6325 case CPP_MULT_EQ:
6326 case CPP_DIV_EQ:
6327 case CPP_MOD_EQ:
6328 case CPP_PLUS_EQ:
6329 case CPP_MINUS_EQ:
6330 case CPP_RSHIFT_EQ:
6331 case CPP_LSHIFT_EQ:
6332 case CPP_AND_EQ:
6333 case CPP_XOR_EQ:
6334 case CPP_OR_EQ:
6335 case CPP_XOR:
6336 case CPP_OR:
6337 case CPP_OR_OR:
6338 case CPP_EOF:
6339 return false;
6341 /* '[' may start a primary-expression in obj-c++. */
6342 case CPP_OPEN_SQUARE:
6343 return c_dialect_objc ();
6345 default:
6346 return true;
6350 /* Parse a cast-expression.
6352 cast-expression:
6353 unary-expression
6354 ( type-id ) cast-expression
6356 ADDRESS_P is true iff the unary-expression is appearing as the
6357 operand of the `&' operator. CAST_P is true if this expression is
6358 the target of a cast.
6360 Returns a representation of the expression. */
6362 static tree
6363 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6364 cp_id_kind * pidk)
6366 /* If it's a `(', then we might be looking at a cast. */
6367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6369 tree type = NULL_TREE;
6370 tree expr = NULL_TREE;
6371 bool compound_literal_p;
6372 const char *saved_message;
6374 /* There's no way to know yet whether or not this is a cast.
6375 For example, `(int (3))' is a unary-expression, while `(int)
6376 3' is a cast. So, we resort to parsing tentatively. */
6377 cp_parser_parse_tentatively (parser);
6378 /* Types may not be defined in a cast. */
6379 saved_message = parser->type_definition_forbidden_message;
6380 parser->type_definition_forbidden_message
6381 = G_("types may not be defined in casts");
6382 /* Consume the `('. */
6383 cp_lexer_consume_token (parser->lexer);
6384 /* A very tricky bit is that `(struct S) { 3 }' is a
6385 compound-literal (which we permit in C++ as an extension).
6386 But, that construct is not a cast-expression -- it is a
6387 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6388 is legal; if the compound-literal were a cast-expression,
6389 you'd need an extra set of parentheses.) But, if we parse
6390 the type-id, and it happens to be a class-specifier, then we
6391 will commit to the parse at that point, because we cannot
6392 undo the action that is done when creating a new class. So,
6393 then we cannot back up and do a postfix-expression.
6395 Therefore, we scan ahead to the closing `)', and check to see
6396 if the token after the `)' is a `{'. If so, we are not
6397 looking at a cast-expression.
6399 Save tokens so that we can put them back. */
6400 cp_lexer_save_tokens (parser->lexer);
6401 /* Skip tokens until the next token is a closing parenthesis.
6402 If we find the closing `)', and the next token is a `{', then
6403 we are looking at a compound-literal. */
6404 compound_literal_p
6405 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6406 /*consume_paren=*/true)
6407 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6408 /* Roll back the tokens we skipped. */
6409 cp_lexer_rollback_tokens (parser->lexer);
6410 /* If we were looking at a compound-literal, simulate an error
6411 so that the call to cp_parser_parse_definitely below will
6412 fail. */
6413 if (compound_literal_p)
6414 cp_parser_simulate_error (parser);
6415 else
6417 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6418 parser->in_type_id_in_expr_p = true;
6419 /* Look for the type-id. */
6420 type = cp_parser_type_id (parser);
6421 /* Look for the closing `)'. */
6422 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6423 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6426 /* Restore the saved message. */
6427 parser->type_definition_forbidden_message = saved_message;
6429 /* At this point this can only be either a cast or a
6430 parenthesized ctor such as `(T ())' that looks like a cast to
6431 function returning T. */
6432 if (!cp_parser_error_occurred (parser)
6433 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6434 (parser->lexer)))
6436 cp_parser_parse_definitely (parser);
6437 expr = cp_parser_cast_expression (parser,
6438 /*address_p=*/false,
6439 /*cast_p=*/true, pidk);
6441 /* Warn about old-style casts, if so requested. */
6442 if (warn_old_style_cast
6443 && !in_system_header
6444 && !VOID_TYPE_P (type)
6445 && current_lang_name != lang_name_c)
6446 warning (OPT_Wold_style_cast, "use of old-style cast");
6448 /* Only type conversions to integral or enumeration types
6449 can be used in constant-expressions. */
6450 if (!cast_valid_in_integral_constant_expression_p (type)
6451 && cp_parser_non_integral_constant_expression (parser,
6452 NIC_CAST))
6453 return error_mark_node;
6455 /* Perform the cast. */
6456 expr = build_c_cast (input_location, type, expr);
6457 return expr;
6459 else
6460 cp_parser_abort_tentative_parse (parser);
6463 /* If we get here, then it's not a cast, so it must be a
6464 unary-expression. */
6465 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6468 /* Parse a binary expression of the general form:
6470 pm-expression:
6471 cast-expression
6472 pm-expression .* cast-expression
6473 pm-expression ->* cast-expression
6475 multiplicative-expression:
6476 pm-expression
6477 multiplicative-expression * pm-expression
6478 multiplicative-expression / pm-expression
6479 multiplicative-expression % pm-expression
6481 additive-expression:
6482 multiplicative-expression
6483 additive-expression + multiplicative-expression
6484 additive-expression - multiplicative-expression
6486 shift-expression:
6487 additive-expression
6488 shift-expression << additive-expression
6489 shift-expression >> additive-expression
6491 relational-expression:
6492 shift-expression
6493 relational-expression < shift-expression
6494 relational-expression > shift-expression
6495 relational-expression <= shift-expression
6496 relational-expression >= shift-expression
6498 GNU Extension:
6500 relational-expression:
6501 relational-expression <? shift-expression
6502 relational-expression >? shift-expression
6504 equality-expression:
6505 relational-expression
6506 equality-expression == relational-expression
6507 equality-expression != relational-expression
6509 and-expression:
6510 equality-expression
6511 and-expression & equality-expression
6513 exclusive-or-expression:
6514 and-expression
6515 exclusive-or-expression ^ and-expression
6517 inclusive-or-expression:
6518 exclusive-or-expression
6519 inclusive-or-expression | exclusive-or-expression
6521 logical-and-expression:
6522 inclusive-or-expression
6523 logical-and-expression && inclusive-or-expression
6525 logical-or-expression:
6526 logical-and-expression
6527 logical-or-expression || logical-and-expression
6529 All these are implemented with a single function like:
6531 binary-expression:
6532 simple-cast-expression
6533 binary-expression <token> binary-expression
6535 CAST_P is true if this expression is the target of a cast.
6537 The binops_by_token map is used to get the tree codes for each <token> type.
6538 binary-expressions are associated according to a precedence table. */
6540 #define TOKEN_PRECEDENCE(token) \
6541 (((token->type == CPP_GREATER \
6542 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6543 && !parser->greater_than_is_operator_p) \
6544 ? PREC_NOT_OPERATOR \
6545 : binops_by_token[token->type].prec)
6547 static tree
6548 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6549 bool no_toplevel_fold_p,
6550 enum cp_parser_prec prec,
6551 cp_id_kind * pidk)
6553 cp_parser_expression_stack stack;
6554 cp_parser_expression_stack_entry *sp = &stack[0];
6555 tree lhs, rhs;
6556 cp_token *token;
6557 enum tree_code tree_type, lhs_type, rhs_type;
6558 enum cp_parser_prec new_prec, lookahead_prec;
6559 bool overloaded_p;
6561 /* Parse the first expression. */
6562 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6563 lhs_type = ERROR_MARK;
6565 for (;;)
6567 /* Get an operator token. */
6568 token = cp_lexer_peek_token (parser->lexer);
6570 if (warn_cxx0x_compat
6571 && token->type == CPP_RSHIFT
6572 && !parser->greater_than_is_operator_p)
6574 if (warning_at (token->location, OPT_Wc__0x_compat,
6575 "%<>>%> operator will be treated as"
6576 " two right angle brackets in C++0x"))
6577 inform (token->location,
6578 "suggest parentheses around %<>>%> expression");
6581 new_prec = TOKEN_PRECEDENCE (token);
6583 /* Popping an entry off the stack means we completed a subexpression:
6584 - either we found a token which is not an operator (`>' where it is not
6585 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6586 will happen repeatedly;
6587 - or, we found an operator which has lower priority. This is the case
6588 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6589 parsing `3 * 4'. */
6590 if (new_prec <= prec)
6592 if (sp == stack)
6593 break;
6594 else
6595 goto pop;
6598 get_rhs:
6599 tree_type = binops_by_token[token->type].tree_type;
6601 /* We used the operator token. */
6602 cp_lexer_consume_token (parser->lexer);
6604 /* For "false && x" or "true || x", x will never be executed;
6605 disable warnings while evaluating it. */
6606 if (tree_type == TRUTH_ANDIF_EXPR)
6607 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6608 else if (tree_type == TRUTH_ORIF_EXPR)
6609 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6611 /* Extract another operand. It may be the RHS of this expression
6612 or the LHS of a new, higher priority expression. */
6613 rhs = cp_parser_simple_cast_expression (parser);
6614 rhs_type = ERROR_MARK;
6616 /* Get another operator token. Look up its precedence to avoid
6617 building a useless (immediately popped) stack entry for common
6618 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6619 token = cp_lexer_peek_token (parser->lexer);
6620 lookahead_prec = TOKEN_PRECEDENCE (token);
6621 if (lookahead_prec > new_prec)
6623 /* ... and prepare to parse the RHS of the new, higher priority
6624 expression. Since precedence levels on the stack are
6625 monotonically increasing, we do not have to care about
6626 stack overflows. */
6627 sp->prec = prec;
6628 sp->tree_type = tree_type;
6629 sp->lhs = lhs;
6630 sp->lhs_type = lhs_type;
6631 sp++;
6632 lhs = rhs;
6633 lhs_type = rhs_type;
6634 prec = new_prec;
6635 new_prec = lookahead_prec;
6636 goto get_rhs;
6638 pop:
6639 lookahead_prec = new_prec;
6640 /* If the stack is not empty, we have parsed into LHS the right side
6641 (`4' in the example above) of an expression we had suspended.
6642 We can use the information on the stack to recover the LHS (`3')
6643 from the stack together with the tree code (`MULT_EXPR'), and
6644 the precedence of the higher level subexpression
6645 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6646 which will be used to actually build the additive expression. */
6647 --sp;
6648 prec = sp->prec;
6649 tree_type = sp->tree_type;
6650 rhs = lhs;
6651 rhs_type = lhs_type;
6652 lhs = sp->lhs;
6653 lhs_type = sp->lhs_type;
6656 /* Undo the disabling of warnings done above. */
6657 if (tree_type == TRUTH_ANDIF_EXPR)
6658 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6659 else if (tree_type == TRUTH_ORIF_EXPR)
6660 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6662 overloaded_p = false;
6663 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6664 ERROR_MARK for everything that is not a binary expression.
6665 This makes warn_about_parentheses miss some warnings that
6666 involve unary operators. For unary expressions we should
6667 pass the correct tree_code unless the unary expression was
6668 surrounded by parentheses.
6670 if (no_toplevel_fold_p
6671 && lookahead_prec <= prec
6672 && sp == stack
6673 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6674 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6675 else
6676 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6677 &overloaded_p, tf_warning_or_error);
6678 lhs_type = tree_type;
6680 /* If the binary operator required the use of an overloaded operator,
6681 then this expression cannot be an integral constant-expression.
6682 An overloaded operator can be used even if both operands are
6683 otherwise permissible in an integral constant-expression if at
6684 least one of the operands is of enumeration type. */
6686 if (overloaded_p
6687 && cp_parser_non_integral_constant_expression (parser,
6688 NIC_OVERLOADED))
6689 return error_mark_node;
6692 return lhs;
6696 /* Parse the `? expression : assignment-expression' part of a
6697 conditional-expression. The LOGICAL_OR_EXPR is the
6698 logical-or-expression that started the conditional-expression.
6699 Returns a representation of the entire conditional-expression.
6701 This routine is used by cp_parser_assignment_expression.
6703 ? expression : assignment-expression
6705 GNU Extensions:
6707 ? : assignment-expression */
6709 static tree
6710 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6712 tree expr;
6713 tree assignment_expr;
6714 struct cp_token *token;
6716 /* Consume the `?' token. */
6717 cp_lexer_consume_token (parser->lexer);
6718 token = cp_lexer_peek_token (parser->lexer);
6719 if (cp_parser_allow_gnu_extensions_p (parser)
6720 && token->type == CPP_COLON)
6722 pedwarn (token->location, OPT_pedantic,
6723 "ISO C++ does not allow ?: with omitted middle operand");
6724 /* Implicit true clause. */
6725 expr = NULL_TREE;
6726 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6727 warn_for_omitted_condop (token->location, logical_or_expr);
6729 else
6731 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6732 parser->colon_corrects_to_scope_p = false;
6733 /* Parse the expression. */
6734 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6735 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6736 c_inhibit_evaluation_warnings +=
6737 ((logical_or_expr == truthvalue_true_node)
6738 - (logical_or_expr == truthvalue_false_node));
6739 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6742 /* The next token should be a `:'. */
6743 cp_parser_require (parser, CPP_COLON, RT_COLON);
6744 /* Parse the assignment-expression. */
6745 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6746 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6748 /* Build the conditional-expression. */
6749 return build_x_conditional_expr (logical_or_expr,
6750 expr,
6751 assignment_expr,
6752 tf_warning_or_error);
6755 /* Parse an assignment-expression.
6757 assignment-expression:
6758 conditional-expression
6759 logical-or-expression assignment-operator assignment_expression
6760 throw-expression
6762 CAST_P is true if this expression is the target of a cast.
6764 Returns a representation for the expression. */
6766 static tree
6767 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6768 cp_id_kind * pidk)
6770 tree expr;
6772 /* If the next token is the `throw' keyword, then we're looking at
6773 a throw-expression. */
6774 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6775 expr = cp_parser_throw_expression (parser);
6776 /* Otherwise, it must be that we are looking at a
6777 logical-or-expression. */
6778 else
6780 /* Parse the binary expressions (logical-or-expression). */
6781 expr = cp_parser_binary_expression (parser, cast_p, false,
6782 PREC_NOT_OPERATOR, pidk);
6783 /* If the next token is a `?' then we're actually looking at a
6784 conditional-expression. */
6785 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6786 return cp_parser_question_colon_clause (parser, expr);
6787 else
6789 enum tree_code assignment_operator;
6791 /* If it's an assignment-operator, we're using the second
6792 production. */
6793 assignment_operator
6794 = cp_parser_assignment_operator_opt (parser);
6795 if (assignment_operator != ERROR_MARK)
6797 bool non_constant_p;
6799 /* Parse the right-hand side of the assignment. */
6800 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6802 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6803 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6805 /* An assignment may not appear in a
6806 constant-expression. */
6807 if (cp_parser_non_integral_constant_expression (parser,
6808 NIC_ASSIGNMENT))
6809 return error_mark_node;
6810 /* Build the assignment expression. */
6811 expr = build_x_modify_expr (expr,
6812 assignment_operator,
6813 rhs,
6814 tf_warning_or_error);
6819 return expr;
6822 /* Parse an (optional) assignment-operator.
6824 assignment-operator: one of
6825 = *= /= %= += -= >>= <<= &= ^= |=
6827 GNU Extension:
6829 assignment-operator: one of
6830 <?= >?=
6832 If the next token is an assignment operator, the corresponding tree
6833 code is returned, and the token is consumed. For example, for
6834 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6835 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6836 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6837 operator, ERROR_MARK is returned. */
6839 static enum tree_code
6840 cp_parser_assignment_operator_opt (cp_parser* parser)
6842 enum tree_code op;
6843 cp_token *token;
6845 /* Peek at the next token. */
6846 token = cp_lexer_peek_token (parser->lexer);
6848 switch (token->type)
6850 case CPP_EQ:
6851 op = NOP_EXPR;
6852 break;
6854 case CPP_MULT_EQ:
6855 op = MULT_EXPR;
6856 break;
6858 case CPP_DIV_EQ:
6859 op = TRUNC_DIV_EXPR;
6860 break;
6862 case CPP_MOD_EQ:
6863 op = TRUNC_MOD_EXPR;
6864 break;
6866 case CPP_PLUS_EQ:
6867 op = PLUS_EXPR;
6868 break;
6870 case CPP_MINUS_EQ:
6871 op = MINUS_EXPR;
6872 break;
6874 case CPP_RSHIFT_EQ:
6875 op = RSHIFT_EXPR;
6876 break;
6878 case CPP_LSHIFT_EQ:
6879 op = LSHIFT_EXPR;
6880 break;
6882 case CPP_AND_EQ:
6883 op = BIT_AND_EXPR;
6884 break;
6886 case CPP_XOR_EQ:
6887 op = BIT_XOR_EXPR;
6888 break;
6890 case CPP_OR_EQ:
6891 op = BIT_IOR_EXPR;
6892 break;
6894 default:
6895 /* Nothing else is an assignment operator. */
6896 op = ERROR_MARK;
6899 /* If it was an assignment operator, consume it. */
6900 if (op != ERROR_MARK)
6901 cp_lexer_consume_token (parser->lexer);
6903 return op;
6906 /* Parse an expression.
6908 expression:
6909 assignment-expression
6910 expression , assignment-expression
6912 CAST_P is true if this expression is the target of a cast.
6914 Returns a representation of the expression. */
6916 static tree
6917 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6919 tree expression = NULL_TREE;
6921 while (true)
6923 tree assignment_expression;
6925 /* Parse the next assignment-expression. */
6926 assignment_expression
6927 = cp_parser_assignment_expression (parser, cast_p, pidk);
6928 /* If this is the first assignment-expression, we can just
6929 save it away. */
6930 if (!expression)
6931 expression = assignment_expression;
6932 else
6933 expression = build_x_compound_expr (expression,
6934 assignment_expression,
6935 tf_warning_or_error);
6936 /* If the next token is not a comma, then we are done with the
6937 expression. */
6938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6939 break;
6940 /* Consume the `,'. */
6941 cp_lexer_consume_token (parser->lexer);
6942 /* A comma operator cannot appear in a constant-expression. */
6943 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6944 expression = error_mark_node;
6947 return expression;
6950 /* Parse a constant-expression.
6952 constant-expression:
6953 conditional-expression
6955 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6956 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6957 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6958 is false, NON_CONSTANT_P should be NULL. */
6960 static tree
6961 cp_parser_constant_expression (cp_parser* parser,
6962 bool allow_non_constant_p,
6963 bool *non_constant_p)
6965 bool saved_integral_constant_expression_p;
6966 bool saved_allow_non_integral_constant_expression_p;
6967 bool saved_non_integral_constant_expression_p;
6968 tree expression;
6970 /* It might seem that we could simply parse the
6971 conditional-expression, and then check to see if it were
6972 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6973 one that the compiler can figure out is constant, possibly after
6974 doing some simplifications or optimizations. The standard has a
6975 precise definition of constant-expression, and we must honor
6976 that, even though it is somewhat more restrictive.
6978 For example:
6980 int i[(2, 3)];
6982 is not a legal declaration, because `(2, 3)' is not a
6983 constant-expression. The `,' operator is forbidden in a
6984 constant-expression. However, GCC's constant-folding machinery
6985 will fold this operation to an INTEGER_CST for `3'. */
6987 /* Save the old settings. */
6988 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6989 saved_allow_non_integral_constant_expression_p
6990 = parser->allow_non_integral_constant_expression_p;
6991 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6992 /* We are now parsing a constant-expression. */
6993 parser->integral_constant_expression_p = true;
6994 parser->allow_non_integral_constant_expression_p
6995 = (allow_non_constant_p || cxx_dialect >= cxx0x);
6996 parser->non_integral_constant_expression_p = false;
6997 /* Although the grammar says "conditional-expression", we parse an
6998 "assignment-expression", which also permits "throw-expression"
6999 and the use of assignment operators. In the case that
7000 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7001 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7002 actually essential that we look for an assignment-expression.
7003 For example, cp_parser_initializer_clauses uses this function to
7004 determine whether a particular assignment-expression is in fact
7005 constant. */
7006 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7007 /* Restore the old settings. */
7008 parser->integral_constant_expression_p
7009 = saved_integral_constant_expression_p;
7010 parser->allow_non_integral_constant_expression_p
7011 = saved_allow_non_integral_constant_expression_p;
7012 if (cxx_dialect >= cxx0x)
7014 /* Require an rvalue constant expression here; that's what our
7015 callers expect. Reference constant expressions are handled
7016 separately in e.g. cp_parser_template_argument. */
7017 bool is_const = potential_rvalue_constant_expression (expression);
7018 parser->non_integral_constant_expression_p = !is_const;
7019 if (!is_const && !allow_non_constant_p)
7020 require_potential_rvalue_constant_expression (expression);
7022 if (allow_non_constant_p)
7023 *non_constant_p = parser->non_integral_constant_expression_p;
7024 else if (parser->non_integral_constant_expression_p)
7025 expression = error_mark_node;
7026 parser->non_integral_constant_expression_p
7027 = saved_non_integral_constant_expression_p;
7029 return expression;
7032 /* Parse __builtin_offsetof.
7034 offsetof-expression:
7035 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7037 offsetof-member-designator:
7038 id-expression
7039 | offsetof-member-designator "." id-expression
7040 | offsetof-member-designator "[" expression "]"
7041 | offsetof-member-designator "->" id-expression */
7043 static tree
7044 cp_parser_builtin_offsetof (cp_parser *parser)
7046 int save_ice_p, save_non_ice_p;
7047 tree type, expr;
7048 cp_id_kind dummy;
7049 cp_token *token;
7051 /* We're about to accept non-integral-constant things, but will
7052 definitely yield an integral constant expression. Save and
7053 restore these values around our local parsing. */
7054 save_ice_p = parser->integral_constant_expression_p;
7055 save_non_ice_p = parser->non_integral_constant_expression_p;
7057 /* Consume the "__builtin_offsetof" token. */
7058 cp_lexer_consume_token (parser->lexer);
7059 /* Consume the opening `('. */
7060 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7061 /* Parse the type-id. */
7062 type = cp_parser_type_id (parser);
7063 /* Look for the `,'. */
7064 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7065 token = cp_lexer_peek_token (parser->lexer);
7067 /* Build the (type *)null that begins the traditional offsetof macro. */
7068 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7069 tf_warning_or_error);
7071 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7072 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7073 true, &dummy, token->location);
7074 while (true)
7076 token = cp_lexer_peek_token (parser->lexer);
7077 switch (token->type)
7079 case CPP_OPEN_SQUARE:
7080 /* offsetof-member-designator "[" expression "]" */
7081 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7082 break;
7084 case CPP_DEREF:
7085 /* offsetof-member-designator "->" identifier */
7086 expr = grok_array_decl (expr, integer_zero_node);
7087 /* FALLTHRU */
7089 case CPP_DOT:
7090 /* offsetof-member-designator "." identifier */
7091 cp_lexer_consume_token (parser->lexer);
7092 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7093 expr, true, &dummy,
7094 token->location);
7095 break;
7097 case CPP_CLOSE_PAREN:
7098 /* Consume the ")" token. */
7099 cp_lexer_consume_token (parser->lexer);
7100 goto success;
7102 default:
7103 /* Error. We know the following require will fail, but
7104 that gives the proper error message. */
7105 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7106 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7107 expr = error_mark_node;
7108 goto failure;
7112 success:
7113 /* If we're processing a template, we can't finish the semantics yet.
7114 Otherwise we can fold the entire expression now. */
7115 if (processing_template_decl)
7116 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7117 else
7118 expr = finish_offsetof (expr);
7120 failure:
7121 parser->integral_constant_expression_p = save_ice_p;
7122 parser->non_integral_constant_expression_p = save_non_ice_p;
7124 return expr;
7127 /* Parse a trait expression. */
7129 static tree
7130 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7132 cp_trait_kind kind;
7133 tree type1, type2 = NULL_TREE;
7134 bool binary = false;
7135 cp_decl_specifier_seq decl_specs;
7137 switch (keyword)
7139 case RID_HAS_NOTHROW_ASSIGN:
7140 kind = CPTK_HAS_NOTHROW_ASSIGN;
7141 break;
7142 case RID_HAS_NOTHROW_CONSTRUCTOR:
7143 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7144 break;
7145 case RID_HAS_NOTHROW_COPY:
7146 kind = CPTK_HAS_NOTHROW_COPY;
7147 break;
7148 case RID_HAS_TRIVIAL_ASSIGN:
7149 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7150 break;
7151 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7152 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7153 break;
7154 case RID_HAS_TRIVIAL_COPY:
7155 kind = CPTK_HAS_TRIVIAL_COPY;
7156 break;
7157 case RID_HAS_TRIVIAL_DESTRUCTOR:
7158 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7159 break;
7160 case RID_HAS_VIRTUAL_DESTRUCTOR:
7161 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7162 break;
7163 case RID_IS_ABSTRACT:
7164 kind = CPTK_IS_ABSTRACT;
7165 break;
7166 case RID_IS_BASE_OF:
7167 kind = CPTK_IS_BASE_OF;
7168 binary = true;
7169 break;
7170 case RID_IS_CLASS:
7171 kind = CPTK_IS_CLASS;
7172 break;
7173 case RID_IS_CONVERTIBLE_TO:
7174 kind = CPTK_IS_CONVERTIBLE_TO;
7175 binary = true;
7176 break;
7177 case RID_IS_EMPTY:
7178 kind = CPTK_IS_EMPTY;
7179 break;
7180 case RID_IS_ENUM:
7181 kind = CPTK_IS_ENUM;
7182 break;
7183 case RID_IS_POD:
7184 kind = CPTK_IS_POD;
7185 break;
7186 case RID_IS_POLYMORPHIC:
7187 kind = CPTK_IS_POLYMORPHIC;
7188 break;
7189 case RID_IS_STD_LAYOUT:
7190 kind = CPTK_IS_STD_LAYOUT;
7191 break;
7192 case RID_IS_TRIVIAL:
7193 kind = CPTK_IS_TRIVIAL;
7194 break;
7195 case RID_IS_UNION:
7196 kind = CPTK_IS_UNION;
7197 break;
7198 case RID_IS_LITERAL_TYPE:
7199 kind = CPTK_IS_LITERAL_TYPE;
7200 break;
7201 default:
7202 gcc_unreachable ();
7205 /* Consume the token. */
7206 cp_lexer_consume_token (parser->lexer);
7208 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7210 type1 = cp_parser_type_id (parser);
7212 if (type1 == error_mark_node)
7213 return error_mark_node;
7215 /* Build a trivial decl-specifier-seq. */
7216 clear_decl_specs (&decl_specs);
7217 decl_specs.type = type1;
7219 /* Call grokdeclarator to figure out what type this is. */
7220 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7221 /*initialized=*/0, /*attrlist=*/NULL);
7223 if (binary)
7225 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7227 type2 = cp_parser_type_id (parser);
7229 if (type2 == error_mark_node)
7230 return error_mark_node;
7232 /* Build a trivial decl-specifier-seq. */
7233 clear_decl_specs (&decl_specs);
7234 decl_specs.type = type2;
7236 /* Call grokdeclarator to figure out what type this is. */
7237 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7238 /*initialized=*/0, /*attrlist=*/NULL);
7241 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7243 /* Complete the trait expression, which may mean either processing
7244 the trait expr now or saving it for template instantiation. */
7245 return finish_trait_expr (kind, type1, type2);
7248 /* Lambdas that appear in variable initializer or default argument scope
7249 get that in their mangling, so we need to record it. We might as well
7250 use the count for function and namespace scopes as well. */
7251 static GTY(()) tree lambda_scope;
7252 static GTY(()) int lambda_count;
7253 typedef struct GTY(()) tree_int
7255 tree t;
7256 int i;
7257 } tree_int;
7258 DEF_VEC_O(tree_int);
7259 DEF_VEC_ALLOC_O(tree_int,gc);
7260 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7262 static void
7263 start_lambda_scope (tree decl)
7265 tree_int ti;
7266 gcc_assert (decl);
7267 /* Once we're inside a function, we ignore other scopes and just push
7268 the function again so that popping works properly. */
7269 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7270 decl = current_function_decl;
7271 ti.t = lambda_scope;
7272 ti.i = lambda_count;
7273 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7274 if (lambda_scope != decl)
7276 /* Don't reset the count if we're still in the same function. */
7277 lambda_scope = decl;
7278 lambda_count = 0;
7282 static void
7283 record_lambda_scope (tree lambda)
7285 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7286 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7289 static void
7290 finish_lambda_scope (void)
7292 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7293 if (lambda_scope != p->t)
7295 lambda_scope = p->t;
7296 lambda_count = p->i;
7298 VEC_pop (tree_int, lambda_scope_stack);
7301 /* Parse a lambda expression.
7303 lambda-expression:
7304 lambda-introducer lambda-declarator [opt] compound-statement
7306 Returns a representation of the expression. */
7308 static tree
7309 cp_parser_lambda_expression (cp_parser* parser)
7311 tree lambda_expr = build_lambda_expr ();
7312 tree type;
7314 LAMBDA_EXPR_LOCATION (lambda_expr)
7315 = cp_lexer_peek_token (parser->lexer)->location;
7317 if (cp_unevaluated_operand)
7318 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7319 "lambda-expression in unevaluated context");
7321 /* We may be in the middle of deferred access check. Disable
7322 it now. */
7323 push_deferring_access_checks (dk_no_deferred);
7325 cp_parser_lambda_introducer (parser, lambda_expr);
7327 type = begin_lambda_type (lambda_expr);
7329 record_lambda_scope (lambda_expr);
7331 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7332 determine_visibility (TYPE_NAME (type));
7334 /* Now that we've started the type, add the capture fields for any
7335 explicit captures. */
7336 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7339 /* Inside the class, surrounding template-parameter-lists do not apply. */
7340 unsigned int saved_num_template_parameter_lists
7341 = parser->num_template_parameter_lists;
7343 parser->num_template_parameter_lists = 0;
7345 /* By virtue of defining a local class, a lambda expression has access to
7346 the private variables of enclosing classes. */
7348 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7350 cp_parser_lambda_body (parser, lambda_expr);
7352 /* The capture list was built up in reverse order; fix that now. */
7354 tree newlist = NULL_TREE;
7355 tree elt, next;
7357 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7358 elt; elt = next)
7360 tree field = TREE_PURPOSE (elt);
7361 char *buf;
7363 next = TREE_CHAIN (elt);
7364 TREE_CHAIN (elt) = newlist;
7365 newlist = elt;
7367 /* Also add __ to the beginning of the field name so that code
7368 outside the lambda body can't see the captured name. We could
7369 just remove the name entirely, but this is more useful for
7370 debugging. */
7371 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7372 /* The 'this' capture already starts with __. */
7373 continue;
7375 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7376 buf[1] = buf[0] = '_';
7377 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7378 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7379 DECL_NAME (field) = get_identifier (buf);
7381 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7384 maybe_add_lambda_conv_op (type);
7386 type = finish_struct (type, /*attributes=*/NULL_TREE);
7388 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7391 pop_deferring_access_checks ();
7393 return build_lambda_object (lambda_expr);
7396 /* Parse the beginning of a lambda expression.
7398 lambda-introducer:
7399 [ lambda-capture [opt] ]
7401 LAMBDA_EXPR is the current representation of the lambda expression. */
7403 static void
7404 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7406 /* Need commas after the first capture. */
7407 bool first = true;
7409 /* Eat the leading `['. */
7410 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7412 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7413 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7414 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7415 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7416 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7417 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7419 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7421 cp_lexer_consume_token (parser->lexer);
7422 first = false;
7425 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7427 cp_token* capture_token;
7428 tree capture_id;
7429 tree capture_init_expr;
7430 cp_id_kind idk = CP_ID_KIND_NONE;
7431 bool explicit_init_p = false;
7433 enum capture_kind_type
7435 BY_COPY,
7436 BY_REFERENCE
7438 enum capture_kind_type capture_kind = BY_COPY;
7440 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7442 error ("expected end of capture-list");
7443 return;
7446 if (first)
7447 first = false;
7448 else
7449 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7451 /* Possibly capture `this'. */
7452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7454 cp_lexer_consume_token (parser->lexer);
7455 add_capture (lambda_expr,
7456 /*id=*/get_identifier ("__this"),
7457 /*initializer=*/finish_this_expr(),
7458 /*by_reference_p=*/false,
7459 explicit_init_p);
7460 continue;
7463 /* Remember whether we want to capture as a reference or not. */
7464 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7466 capture_kind = BY_REFERENCE;
7467 cp_lexer_consume_token (parser->lexer);
7470 /* Get the identifier. */
7471 capture_token = cp_lexer_peek_token (parser->lexer);
7472 capture_id = cp_parser_identifier (parser);
7474 if (capture_id == error_mark_node)
7475 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7476 delimiters, but I modified this to stop on unnested ']' as well. It
7477 was already changed to stop on unnested '}', so the
7478 "closing_parenthesis" name is no more misleading with my change. */
7480 cp_parser_skip_to_closing_parenthesis (parser,
7481 /*recovering=*/true,
7482 /*or_comma=*/true,
7483 /*consume_paren=*/true);
7484 break;
7487 /* Find the initializer for this capture. */
7488 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7490 /* An explicit expression exists. */
7491 cp_lexer_consume_token (parser->lexer);
7492 pedwarn (input_location, OPT_pedantic,
7493 "ISO C++ does not allow initializers "
7494 "in lambda expression capture lists");
7495 capture_init_expr = cp_parser_assignment_expression (parser,
7496 /*cast_p=*/true,
7497 &idk);
7498 explicit_init_p = true;
7500 else
7502 const char* error_msg;
7504 /* Turn the identifier into an id-expression. */
7505 capture_init_expr
7506 = cp_parser_lookup_name
7507 (parser,
7508 capture_id,
7509 none_type,
7510 /*is_template=*/false,
7511 /*is_namespace=*/false,
7512 /*check_dependency=*/true,
7513 /*ambiguous_decls=*/NULL,
7514 capture_token->location);
7516 capture_init_expr
7517 = finish_id_expression
7518 (capture_id,
7519 capture_init_expr,
7520 parser->scope,
7521 &idk,
7522 /*integral_constant_expression_p=*/false,
7523 /*allow_non_integral_constant_expression_p=*/false,
7524 /*non_integral_constant_expression_p=*/NULL,
7525 /*template_p=*/false,
7526 /*done=*/true,
7527 /*address_p=*/false,
7528 /*template_arg_p=*/false,
7529 &error_msg,
7530 capture_token->location);
7533 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7534 capture_init_expr
7535 = unqualified_name_lookup_error (capture_init_expr);
7537 add_capture (lambda_expr,
7538 capture_id,
7539 capture_init_expr,
7540 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7541 explicit_init_p);
7544 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7547 /* Parse the (optional) middle of a lambda expression.
7549 lambda-declarator:
7550 ( parameter-declaration-clause [opt] )
7551 attribute-specifier [opt]
7552 mutable [opt]
7553 exception-specification [opt]
7554 lambda-return-type-clause [opt]
7556 LAMBDA_EXPR is the current representation of the lambda expression. */
7558 static void
7559 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7561 /* 5.1.1.4 of the standard says:
7562 If a lambda-expression does not include a lambda-declarator, it is as if
7563 the lambda-declarator were ().
7564 This means an empty parameter list, no attributes, and no exception
7565 specification. */
7566 tree param_list = void_list_node;
7567 tree attributes = NULL_TREE;
7568 tree exception_spec = NULL_TREE;
7569 tree t;
7571 /* The lambda-declarator is optional, but must begin with an opening
7572 parenthesis if present. */
7573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7575 cp_lexer_consume_token (parser->lexer);
7577 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7579 /* Parse parameters. */
7580 param_list = cp_parser_parameter_declaration_clause (parser);
7582 /* Default arguments shall not be specified in the
7583 parameter-declaration-clause of a lambda-declarator. */
7584 for (t = param_list; t; t = TREE_CHAIN (t))
7585 if (TREE_PURPOSE (t))
7586 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7587 "default argument specified for lambda parameter");
7589 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7591 attributes = cp_parser_attributes_opt (parser);
7593 /* Parse optional `mutable' keyword. */
7594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7596 cp_lexer_consume_token (parser->lexer);
7597 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7600 /* Parse optional exception specification. */
7601 exception_spec = cp_parser_exception_specification_opt (parser);
7603 /* Parse optional trailing return type. */
7604 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7606 cp_lexer_consume_token (parser->lexer);
7607 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7610 /* The function parameters must be in scope all the way until after the
7611 trailing-return-type in case of decltype. */
7612 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7613 pop_binding (DECL_NAME (t), t);
7615 leave_scope ();
7618 /* Create the function call operator.
7620 Messing with declarators like this is no uglier than building up the
7621 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7622 other code. */
7624 cp_decl_specifier_seq return_type_specs;
7625 cp_declarator* declarator;
7626 tree fco;
7627 int quals;
7628 void *p;
7630 clear_decl_specs (&return_type_specs);
7631 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7632 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7633 else
7634 /* Maybe we will deduce the return type later, but we can use void
7635 as a placeholder return type anyways. */
7636 return_type_specs.type = void_type_node;
7638 p = obstack_alloc (&declarator_obstack, 0);
7640 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7641 sfk_none);
7643 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7644 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7645 declarator = make_call_declarator (declarator, param_list, quals,
7646 exception_spec,
7647 /*late_return_type=*/NULL_TREE);
7648 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7650 fco = grokmethod (&return_type_specs,
7651 declarator,
7652 attributes);
7653 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7654 DECL_ARTIFICIAL (fco) = 1;
7656 finish_member_declaration (fco);
7658 obstack_free (&declarator_obstack, p);
7662 /* Parse the body of a lambda expression, which is simply
7664 compound-statement
7666 but which requires special handling.
7667 LAMBDA_EXPR is the current representation of the lambda expression. */
7669 static void
7670 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7672 bool nested = (current_function_decl != NULL_TREE);
7673 if (nested)
7674 push_function_context ();
7676 /* Finish the function call operator
7677 - class_specifier
7678 + late_parsing_for_member
7679 + function_definition_after_declarator
7680 + ctor_initializer_opt_and_function_body */
7682 tree fco = lambda_function (lambda_expr);
7683 tree body;
7684 bool done = false;
7686 /* Let the front end know that we are going to be defining this
7687 function. */
7688 start_preparsed_function (fco,
7689 NULL_TREE,
7690 SF_PRE_PARSED | SF_INCLASS_INLINE);
7692 start_lambda_scope (fco);
7693 body = begin_function_body ();
7695 /* 5.1.1.4 of the standard says:
7696 If a lambda-expression does not include a trailing-return-type, it
7697 is as if the trailing-return-type denotes the following type:
7698 * if the compound-statement is of the form
7699 { return attribute-specifier [opt] expression ; }
7700 the type of the returned expression after lvalue-to-rvalue
7701 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7702 (_conv.array_ 4.2), and function-to-pointer conversion
7703 (_conv.func_ 4.3);
7704 * otherwise, void. */
7706 /* In a lambda that has neither a lambda-return-type-clause
7707 nor a deducible form, errors should be reported for return statements
7708 in the body. Since we used void as the placeholder return type, parsing
7709 the body as usual will give such desired behavior. */
7710 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7711 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7712 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7713 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7715 tree compound_stmt;
7716 tree expr = NULL_TREE;
7717 cp_id_kind idk = CP_ID_KIND_NONE;
7719 /* Parse tentatively in case there's more after the initial return
7720 statement. */
7721 cp_parser_parse_tentatively (parser);
7723 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7724 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7726 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7728 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7729 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7731 if (cp_parser_parse_definitely (parser))
7733 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7735 compound_stmt = begin_compound_stmt (0);
7736 /* Will get error here if type not deduced yet. */
7737 finish_return_stmt (expr);
7738 finish_compound_stmt (compound_stmt);
7740 done = true;
7744 if (!done)
7746 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7747 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7748 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7749 cp_parser_compound_stmt does not pass it. */
7750 cp_parser_function_body (parser);
7751 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7754 finish_function_body (body);
7755 finish_lambda_scope ();
7757 /* Finish the function and generate code for it if necessary. */
7758 expand_or_defer_fn (finish_function (/*inline*/2));
7761 if (nested)
7762 pop_function_context();
7765 /* Statements [gram.stmt.stmt] */
7767 /* Parse a statement.
7769 statement:
7770 labeled-statement
7771 expression-statement
7772 compound-statement
7773 selection-statement
7774 iteration-statement
7775 jump-statement
7776 declaration-statement
7777 try-block
7779 IN_COMPOUND is true when the statement is nested inside a
7780 cp_parser_compound_statement; this matters for certain pragmas.
7782 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7783 is a (possibly labeled) if statement which is not enclosed in braces
7784 and has an else clause. This is used to implement -Wparentheses. */
7786 static void
7787 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7788 bool in_compound, bool *if_p)
7790 tree statement;
7791 cp_token *token;
7792 location_t statement_location;
7794 restart:
7795 if (if_p != NULL)
7796 *if_p = false;
7797 /* There is no statement yet. */
7798 statement = NULL_TREE;
7799 /* Peek at the next token. */
7800 token = cp_lexer_peek_token (parser->lexer);
7801 /* Remember the location of the first token in the statement. */
7802 statement_location = token->location;
7803 /* If this is a keyword, then that will often determine what kind of
7804 statement we have. */
7805 if (token->type == CPP_KEYWORD)
7807 enum rid keyword = token->keyword;
7809 switch (keyword)
7811 case RID_CASE:
7812 case RID_DEFAULT:
7813 /* Looks like a labeled-statement with a case label.
7814 Parse the label, and then use tail recursion to parse
7815 the statement. */
7816 cp_parser_label_for_labeled_statement (parser);
7817 goto restart;
7819 case RID_IF:
7820 case RID_SWITCH:
7821 statement = cp_parser_selection_statement (parser, if_p);
7822 break;
7824 case RID_WHILE:
7825 case RID_DO:
7826 case RID_FOR:
7827 statement = cp_parser_iteration_statement (parser);
7828 break;
7830 case RID_BREAK:
7831 case RID_CONTINUE:
7832 case RID_RETURN:
7833 case RID_GOTO:
7834 statement = cp_parser_jump_statement (parser);
7835 break;
7837 /* Objective-C++ exception-handling constructs. */
7838 case RID_AT_TRY:
7839 case RID_AT_CATCH:
7840 case RID_AT_FINALLY:
7841 case RID_AT_SYNCHRONIZED:
7842 case RID_AT_THROW:
7843 statement = cp_parser_objc_statement (parser);
7844 break;
7846 case RID_TRY:
7847 statement = cp_parser_try_block (parser);
7848 break;
7850 case RID_NAMESPACE:
7851 /* This must be a namespace alias definition. */
7852 cp_parser_declaration_statement (parser);
7853 return;
7855 default:
7856 /* It might be a keyword like `int' that can start a
7857 declaration-statement. */
7858 break;
7861 else if (token->type == CPP_NAME)
7863 /* If the next token is a `:', then we are looking at a
7864 labeled-statement. */
7865 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7866 if (token->type == CPP_COLON)
7868 /* Looks like a labeled-statement with an ordinary label.
7869 Parse the label, and then use tail recursion to parse
7870 the statement. */
7871 cp_parser_label_for_labeled_statement (parser);
7872 goto restart;
7875 /* Anything that starts with a `{' must be a compound-statement. */
7876 else if (token->type == CPP_OPEN_BRACE)
7877 statement = cp_parser_compound_statement (parser, NULL, false, false);
7878 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7879 a statement all its own. */
7880 else if (token->type == CPP_PRAGMA)
7882 /* Only certain OpenMP pragmas are attached to statements, and thus
7883 are considered statements themselves. All others are not. In
7884 the context of a compound, accept the pragma as a "statement" and
7885 return so that we can check for a close brace. Otherwise we
7886 require a real statement and must go back and read one. */
7887 if (in_compound)
7888 cp_parser_pragma (parser, pragma_compound);
7889 else if (!cp_parser_pragma (parser, pragma_stmt))
7890 goto restart;
7891 return;
7893 else if (token->type == CPP_EOF)
7895 cp_parser_error (parser, "expected statement");
7896 return;
7899 /* Everything else must be a declaration-statement or an
7900 expression-statement. Try for the declaration-statement
7901 first, unless we are looking at a `;', in which case we know that
7902 we have an expression-statement. */
7903 if (!statement)
7905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7907 cp_parser_parse_tentatively (parser);
7908 /* Try to parse the declaration-statement. */
7909 cp_parser_declaration_statement (parser);
7910 /* If that worked, we're done. */
7911 if (cp_parser_parse_definitely (parser))
7912 return;
7914 /* Look for an expression-statement instead. */
7915 statement = cp_parser_expression_statement (parser, in_statement_expr);
7918 /* Set the line number for the statement. */
7919 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7920 SET_EXPR_LOCATION (statement, statement_location);
7923 /* Parse the label for a labeled-statement, i.e.
7925 identifier :
7926 case constant-expression :
7927 default :
7929 GNU Extension:
7930 case constant-expression ... constant-expression : statement
7932 When a label is parsed without errors, the label is added to the
7933 parse tree by the finish_* functions, so this function doesn't
7934 have to return the label. */
7936 static void
7937 cp_parser_label_for_labeled_statement (cp_parser* parser)
7939 cp_token *token;
7940 tree label = NULL_TREE;
7941 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7943 /* The next token should be an identifier. */
7944 token = cp_lexer_peek_token (parser->lexer);
7945 if (token->type != CPP_NAME
7946 && token->type != CPP_KEYWORD)
7948 cp_parser_error (parser, "expected labeled-statement");
7949 return;
7952 parser->colon_corrects_to_scope_p = false;
7953 switch (token->keyword)
7955 case RID_CASE:
7957 tree expr, expr_hi;
7958 cp_token *ellipsis;
7960 /* Consume the `case' token. */
7961 cp_lexer_consume_token (parser->lexer);
7962 /* Parse the constant-expression. */
7963 expr = cp_parser_constant_expression (parser,
7964 /*allow_non_constant_p=*/false,
7965 NULL);
7967 ellipsis = cp_lexer_peek_token (parser->lexer);
7968 if (ellipsis->type == CPP_ELLIPSIS)
7970 /* Consume the `...' token. */
7971 cp_lexer_consume_token (parser->lexer);
7972 expr_hi =
7973 cp_parser_constant_expression (parser,
7974 /*allow_non_constant_p=*/false,
7975 NULL);
7976 /* We don't need to emit warnings here, as the common code
7977 will do this for us. */
7979 else
7980 expr_hi = NULL_TREE;
7982 if (parser->in_switch_statement_p)
7983 finish_case_label (token->location, expr, expr_hi);
7984 else
7985 error_at (token->location,
7986 "case label %qE not within a switch statement",
7987 expr);
7989 break;
7991 case RID_DEFAULT:
7992 /* Consume the `default' token. */
7993 cp_lexer_consume_token (parser->lexer);
7995 if (parser->in_switch_statement_p)
7996 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7997 else
7998 error_at (token->location, "case label not within a switch statement");
7999 break;
8001 default:
8002 /* Anything else must be an ordinary label. */
8003 label = finish_label_stmt (cp_parser_identifier (parser));
8004 break;
8007 /* Require the `:' token. */
8008 cp_parser_require (parser, CPP_COLON, RT_COLON);
8010 /* An ordinary label may optionally be followed by attributes.
8011 However, this is only permitted if the attributes are then
8012 followed by a semicolon. This is because, for backward
8013 compatibility, when parsing
8014 lab: __attribute__ ((unused)) int i;
8015 we want the attribute to attach to "i", not "lab". */
8016 if (label != NULL_TREE
8017 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8019 tree attrs;
8021 cp_parser_parse_tentatively (parser);
8022 attrs = cp_parser_attributes_opt (parser);
8023 if (attrs == NULL_TREE
8024 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8025 cp_parser_abort_tentative_parse (parser);
8026 else if (!cp_parser_parse_definitely (parser))
8028 else
8029 cplus_decl_attributes (&label, attrs, 0);
8032 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8035 /* Parse an expression-statement.
8037 expression-statement:
8038 expression [opt] ;
8040 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8041 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8042 indicates whether this expression-statement is part of an
8043 expression statement. */
8045 static tree
8046 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8048 tree statement = NULL_TREE;
8049 cp_token *token = cp_lexer_peek_token (parser->lexer);
8051 /* If the next token is a ';', then there is no expression
8052 statement. */
8053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8054 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8056 /* Give a helpful message for "A<T>::type t;" and the like. */
8057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8058 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8060 if (TREE_CODE (statement) == SCOPE_REF)
8061 error_at (token->location, "need %<typename%> before %qE because "
8062 "%qT is a dependent scope",
8063 statement, TREE_OPERAND (statement, 0));
8064 else if (is_overloaded_fn (statement)
8065 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8067 /* A::A a; */
8068 tree fn = get_first_fn (statement);
8069 error_at (token->location,
8070 "%<%T::%D%> names the constructor, not the type",
8071 DECL_CONTEXT (fn), DECL_NAME (fn));
8075 /* Consume the final `;'. */
8076 cp_parser_consume_semicolon_at_end_of_statement (parser);
8078 if (in_statement_expr
8079 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8080 /* This is the final expression statement of a statement
8081 expression. */
8082 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8083 else if (statement)
8084 statement = finish_expr_stmt (statement);
8085 else
8086 finish_stmt ();
8088 return statement;
8091 /* Parse a compound-statement.
8093 compound-statement:
8094 { statement-seq [opt] }
8096 GNU extension:
8098 compound-statement:
8099 { label-declaration-seq [opt] statement-seq [opt] }
8101 label-declaration-seq:
8102 label-declaration
8103 label-declaration-seq label-declaration
8105 Returns a tree representing the statement. */
8107 static tree
8108 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8109 bool in_try, bool function_body)
8111 tree compound_stmt;
8113 /* Consume the `{'. */
8114 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8115 return error_mark_node;
8116 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8117 && !function_body)
8118 pedwarn (input_location, OPT_pedantic,
8119 "compound-statement in constexpr function");
8120 /* Begin the compound-statement. */
8121 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8122 /* If the next keyword is `__label__' we have a label declaration. */
8123 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8124 cp_parser_label_declaration (parser);
8125 /* Parse an (optional) statement-seq. */
8126 cp_parser_statement_seq_opt (parser, in_statement_expr);
8127 /* Finish the compound-statement. */
8128 finish_compound_stmt (compound_stmt);
8129 /* Consume the `}'. */
8130 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8132 return compound_stmt;
8135 /* Parse an (optional) statement-seq.
8137 statement-seq:
8138 statement
8139 statement-seq [opt] statement */
8141 static void
8142 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8144 /* Scan statements until there aren't any more. */
8145 while (true)
8147 cp_token *token = cp_lexer_peek_token (parser->lexer);
8149 /* If we are looking at a `}', then we have run out of
8150 statements; the same is true if we have reached the end
8151 of file, or have stumbled upon a stray '@end'. */
8152 if (token->type == CPP_CLOSE_BRACE
8153 || token->type == CPP_EOF
8154 || token->type == CPP_PRAGMA_EOL
8155 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8156 break;
8158 /* If we are in a compound statement and find 'else' then
8159 something went wrong. */
8160 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8162 if (parser->in_statement & IN_IF_STMT)
8163 break;
8164 else
8166 token = cp_lexer_consume_token (parser->lexer);
8167 error_at (token->location, "%<else%> without a previous %<if%>");
8171 /* Parse the statement. */
8172 cp_parser_statement (parser, in_statement_expr, true, NULL);
8176 /* Parse a selection-statement.
8178 selection-statement:
8179 if ( condition ) statement
8180 if ( condition ) statement else statement
8181 switch ( condition ) statement
8183 Returns the new IF_STMT or SWITCH_STMT.
8185 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8186 is a (possibly labeled) if statement which is not enclosed in
8187 braces and has an else clause. This is used to implement
8188 -Wparentheses. */
8190 static tree
8191 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8193 cp_token *token;
8194 enum rid keyword;
8196 if (if_p != NULL)
8197 *if_p = false;
8199 /* Peek at the next token. */
8200 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8202 /* See what kind of keyword it is. */
8203 keyword = token->keyword;
8204 switch (keyword)
8206 case RID_IF:
8207 case RID_SWITCH:
8209 tree statement;
8210 tree condition;
8212 /* Look for the `('. */
8213 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8215 cp_parser_skip_to_end_of_statement (parser);
8216 return error_mark_node;
8219 /* Begin the selection-statement. */
8220 if (keyword == RID_IF)
8221 statement = begin_if_stmt ();
8222 else
8223 statement = begin_switch_stmt ();
8225 /* Parse the condition. */
8226 condition = cp_parser_condition (parser);
8227 /* Look for the `)'. */
8228 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8229 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8230 /*consume_paren=*/true);
8232 if (keyword == RID_IF)
8234 bool nested_if;
8235 unsigned char in_statement;
8237 /* Add the condition. */
8238 finish_if_stmt_cond (condition, statement);
8240 /* Parse the then-clause. */
8241 in_statement = parser->in_statement;
8242 parser->in_statement |= IN_IF_STMT;
8243 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8245 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8246 add_stmt (build_empty_stmt (loc));
8247 cp_lexer_consume_token (parser->lexer);
8248 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8249 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8250 "empty body in an %<if%> statement");
8251 nested_if = false;
8253 else
8254 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8255 parser->in_statement = in_statement;
8257 finish_then_clause (statement);
8259 /* If the next token is `else', parse the else-clause. */
8260 if (cp_lexer_next_token_is_keyword (parser->lexer,
8261 RID_ELSE))
8263 /* Consume the `else' keyword. */
8264 cp_lexer_consume_token (parser->lexer);
8265 begin_else_clause (statement);
8266 /* Parse the else-clause. */
8267 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8269 location_t loc;
8270 loc = cp_lexer_peek_token (parser->lexer)->location;
8271 warning_at (loc,
8272 OPT_Wempty_body, "suggest braces around "
8273 "empty body in an %<else%> statement");
8274 add_stmt (build_empty_stmt (loc));
8275 cp_lexer_consume_token (parser->lexer);
8277 else
8278 cp_parser_implicitly_scoped_statement (parser, NULL);
8280 finish_else_clause (statement);
8282 /* If we are currently parsing a then-clause, then
8283 IF_P will not be NULL. We set it to true to
8284 indicate that this if statement has an else clause.
8285 This may trigger the Wparentheses warning below
8286 when we get back up to the parent if statement. */
8287 if (if_p != NULL)
8288 *if_p = true;
8290 else
8292 /* This if statement does not have an else clause. If
8293 NESTED_IF is true, then the then-clause is an if
8294 statement which does have an else clause. We warn
8295 about the potential ambiguity. */
8296 if (nested_if)
8297 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8298 "suggest explicit braces to avoid ambiguous"
8299 " %<else%>");
8302 /* Now we're all done with the if-statement. */
8303 finish_if_stmt (statement);
8305 else
8307 bool in_switch_statement_p;
8308 unsigned char in_statement;
8310 /* Add the condition. */
8311 finish_switch_cond (condition, statement);
8313 /* Parse the body of the switch-statement. */
8314 in_switch_statement_p = parser->in_switch_statement_p;
8315 in_statement = parser->in_statement;
8316 parser->in_switch_statement_p = true;
8317 parser->in_statement |= IN_SWITCH_STMT;
8318 cp_parser_implicitly_scoped_statement (parser, NULL);
8319 parser->in_switch_statement_p = in_switch_statement_p;
8320 parser->in_statement = in_statement;
8322 /* Now we're all done with the switch-statement. */
8323 finish_switch_stmt (statement);
8326 return statement;
8328 break;
8330 default:
8331 cp_parser_error (parser, "expected selection-statement");
8332 return error_mark_node;
8336 /* Parse a condition.
8338 condition:
8339 expression
8340 type-specifier-seq declarator = initializer-clause
8341 type-specifier-seq declarator braced-init-list
8343 GNU Extension:
8345 condition:
8346 type-specifier-seq declarator asm-specification [opt]
8347 attributes [opt] = assignment-expression
8349 Returns the expression that should be tested. */
8351 static tree
8352 cp_parser_condition (cp_parser* parser)
8354 cp_decl_specifier_seq type_specifiers;
8355 const char *saved_message;
8356 int declares_class_or_enum;
8358 /* Try the declaration first. */
8359 cp_parser_parse_tentatively (parser);
8360 /* New types are not allowed in the type-specifier-seq for a
8361 condition. */
8362 saved_message = parser->type_definition_forbidden_message;
8363 parser->type_definition_forbidden_message
8364 = G_("types may not be defined in conditions");
8365 /* Parse the type-specifier-seq. */
8366 cp_parser_decl_specifier_seq (parser,
8367 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8368 &type_specifiers,
8369 &declares_class_or_enum);
8370 /* Restore the saved message. */
8371 parser->type_definition_forbidden_message = saved_message;
8372 /* If all is well, we might be looking at a declaration. */
8373 if (!cp_parser_error_occurred (parser))
8375 tree decl;
8376 tree asm_specification;
8377 tree attributes;
8378 cp_declarator *declarator;
8379 tree initializer = NULL_TREE;
8381 /* Parse the declarator. */
8382 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8383 /*ctor_dtor_or_conv_p=*/NULL,
8384 /*parenthesized_p=*/NULL,
8385 /*member_p=*/false);
8386 /* Parse the attributes. */
8387 attributes = cp_parser_attributes_opt (parser);
8388 /* Parse the asm-specification. */
8389 asm_specification = cp_parser_asm_specification_opt (parser);
8390 /* If the next token is not an `=' or '{', then we might still be
8391 looking at an expression. For example:
8393 if (A(a).x)
8395 looks like a decl-specifier-seq and a declarator -- but then
8396 there is no `=', so this is an expression. */
8397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8398 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8399 cp_parser_simulate_error (parser);
8401 /* If we did see an `=' or '{', then we are looking at a declaration
8402 for sure. */
8403 if (cp_parser_parse_definitely (parser))
8405 tree pushed_scope;
8406 bool non_constant_p;
8407 bool flags = LOOKUP_ONLYCONVERTING;
8409 /* Create the declaration. */
8410 decl = start_decl (declarator, &type_specifiers,
8411 /*initialized_p=*/true,
8412 attributes, /*prefix_attributes=*/NULL_TREE,
8413 &pushed_scope);
8415 /* Parse the initializer. */
8416 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8418 initializer = cp_parser_braced_list (parser, &non_constant_p);
8419 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8420 flags = 0;
8422 else
8424 /* Consume the `='. */
8425 cp_parser_require (parser, CPP_EQ, RT_EQ);
8426 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8428 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8429 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8431 /* Process the initializer. */
8432 cp_finish_decl (decl,
8433 initializer, !non_constant_p,
8434 asm_specification,
8435 flags);
8437 if (pushed_scope)
8438 pop_scope (pushed_scope);
8440 return convert_from_reference (decl);
8443 /* If we didn't even get past the declarator successfully, we are
8444 definitely not looking at a declaration. */
8445 else
8446 cp_parser_abort_tentative_parse (parser);
8448 /* Otherwise, we are looking at an expression. */
8449 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8452 /* Parses a for-statement or range-for-statement until the closing ')',
8453 not included. */
8455 static tree
8456 cp_parser_for (cp_parser *parser)
8458 tree init, scope, decl;
8459 bool is_range_for;
8461 /* Begin the for-statement. */
8462 scope = begin_for_scope (&init);
8464 /* Parse the initialization. */
8465 is_range_for = cp_parser_for_init_statement (parser, &decl);
8467 if (is_range_for)
8468 return cp_parser_range_for (parser, scope, init, decl);
8469 else
8470 return cp_parser_c_for (parser, scope, init);
8473 static tree
8474 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8476 /* Normal for loop */
8477 tree condition = NULL_TREE;
8478 tree expression = NULL_TREE;
8479 tree stmt;
8481 stmt = begin_for_stmt (scope, init);
8482 /* The for-init-statement has already been parsed in
8483 cp_parser_for_init_statement, so no work is needed here. */
8484 finish_for_init_stmt (stmt);
8486 /* If there's a condition, process it. */
8487 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8488 condition = cp_parser_condition (parser);
8489 finish_for_cond (condition, stmt);
8490 /* Look for the `;'. */
8491 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8493 /* If there's an expression, process it. */
8494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8495 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8496 finish_for_expr (expression, stmt);
8498 return stmt;
8501 /* Tries to parse a range-based for-statement:
8503 range-based-for:
8504 decl-specifier-seq declarator : expression
8506 The decl-specifier-seq declarator and the `:' are already parsed by
8507 cp_parser_for_init_statement. If processing_template_decl it returns a
8508 newly created RANGE_FOR_STMT; if not, it is converted to a
8509 regular FOR_STMT. */
8511 static tree
8512 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8514 tree stmt, range_expr;
8516 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8518 bool expr_non_constant_p;
8519 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8521 else
8522 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8524 /* If in template, STMT is converted to a normal for-statement
8525 at instantiation. If not, it is done just ahead. */
8526 if (processing_template_decl)
8528 stmt = begin_range_for_stmt (scope, init);
8529 finish_range_for_decl (stmt, range_decl, range_expr);
8531 else
8533 stmt = begin_for_stmt (scope, init);
8534 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8536 return stmt;
8539 /* Converts a range-based for-statement into a normal
8540 for-statement, as per the definition.
8542 for (RANGE_DECL : RANGE_EXPR)
8543 BLOCK
8545 should be equivalent to:
8548 auto &&__range = RANGE_EXPR;
8549 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8550 __begin != __end;
8551 ++__begin)
8553 RANGE_DECL = *__begin;
8554 BLOCK
8558 If RANGE_EXPR is an array:
8559 BEGIN_EXPR = __range
8560 END_EXPR = __range + ARRAY_SIZE(__range)
8561 Else:
8562 BEGIN_EXPR = begin(__range)
8563 END_EXPR = end(__range);
8565 When calling begin()/end() we must use argument dependent
8566 lookup, but always considering 'std' as an associated namespace. */
8568 tree
8569 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8571 tree range_type, range_temp;
8572 tree begin, end;
8573 tree iter_type, begin_expr, end_expr;
8574 tree condition, expression;
8576 if (range_decl == error_mark_node || range_expr == error_mark_node)
8577 /* If an error happened previously do nothing or else a lot of
8578 unhelpful errors would be issued. */
8579 begin_expr = end_expr = iter_type = error_mark_node;
8580 else
8582 /* Find out the type deduced by the declaration
8583 * `auto &&__range = range_expr' */
8584 range_type = cp_build_reference_type (make_auto (), true);
8585 range_type = do_auto_deduction (range_type, range_expr,
8586 type_uses_auto (range_type));
8588 /* Create the __range variable */
8589 range_temp = build_decl (input_location, VAR_DECL,
8590 get_identifier ("__for_range"), range_type);
8591 TREE_USED (range_temp) = 1;
8592 DECL_ARTIFICIAL (range_temp) = 1;
8593 pushdecl (range_temp);
8594 cp_finish_decl (range_temp, range_expr,
8595 /*is_constant_init*/false, NULL_TREE,
8596 LOOKUP_ONLYCONVERTING);
8598 range_temp = convert_from_reference (range_temp);
8600 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8602 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8603 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8604 begin_expr = range_temp;
8605 end_expr
8606 = build_binary_op (input_location, PLUS_EXPR,
8607 range_temp,
8608 array_type_nelts_top (TREE_TYPE (range_temp)),
8611 else
8613 /* If it is not an array, we must call begin(__range)/end__range() */
8614 VEC(tree,gc) *vec;
8616 begin_expr = get_identifier ("begin");
8617 vec = make_tree_vector ();
8618 VEC_safe_push (tree, gc, vec, range_temp);
8619 begin_expr = perform_koenig_lookup (begin_expr, vec,
8620 /*include_std=*/true);
8621 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8622 tf_warning_or_error);
8623 release_tree_vector (vec);
8625 end_expr = get_identifier ("end");
8626 vec = make_tree_vector ();
8627 VEC_safe_push (tree, gc, vec, range_temp);
8628 end_expr = perform_koenig_lookup (end_expr, vec,
8629 /*include_std=*/true);
8630 end_expr = finish_call_expr (end_expr, &vec, false, true,
8631 tf_warning_or_error);
8632 release_tree_vector (vec);
8634 /* The unqualified type of the __begin and __end temporaries should
8635 * be the same as required by the multiple auto declaration */
8636 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8637 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8638 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8639 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8643 /* The new for initialization statement */
8644 begin = build_decl (input_location, VAR_DECL,
8645 get_identifier ("__for_begin"), iter_type);
8646 TREE_USED (begin) = 1;
8647 DECL_ARTIFICIAL (begin) = 1;
8648 pushdecl (begin);
8649 cp_finish_decl (begin, begin_expr,
8650 /*is_constant_init*/false, NULL_TREE,
8651 LOOKUP_ONLYCONVERTING);
8653 end = build_decl (input_location, VAR_DECL,
8654 get_identifier ("__for_end"), iter_type);
8655 TREE_USED (end) = 1;
8656 DECL_ARTIFICIAL (end) = 1;
8657 pushdecl (end);
8658 cp_finish_decl (end, end_expr,
8659 /*is_constant_init*/false, NULL_TREE,
8660 LOOKUP_ONLYCONVERTING);
8662 finish_for_init_stmt (statement);
8664 /* The new for condition */
8665 condition = build_x_binary_op (NE_EXPR,
8666 begin, ERROR_MARK,
8667 end, ERROR_MARK,
8668 NULL, tf_warning_or_error);
8669 finish_for_cond (condition, statement);
8671 /* The new increment expression */
8672 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8673 finish_for_expr (expression, statement);
8675 /* The declaration is initialized with *__begin inside the loop body */
8676 cp_finish_decl (range_decl,
8677 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8678 /*is_constant_init*/false, NULL_TREE,
8679 LOOKUP_ONLYCONVERTING);
8681 return statement;
8685 /* Parse an iteration-statement.
8687 iteration-statement:
8688 while ( condition ) statement
8689 do statement while ( expression ) ;
8690 for ( for-init-statement condition [opt] ; expression [opt] )
8691 statement
8693 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8695 static tree
8696 cp_parser_iteration_statement (cp_parser* parser)
8698 cp_token *token;
8699 enum rid keyword;
8700 tree statement;
8701 unsigned char in_statement;
8703 /* Peek at the next token. */
8704 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8705 if (!token)
8706 return error_mark_node;
8708 /* Remember whether or not we are already within an iteration
8709 statement. */
8710 in_statement = parser->in_statement;
8712 /* See what kind of keyword it is. */
8713 keyword = token->keyword;
8714 switch (keyword)
8716 case RID_WHILE:
8718 tree condition;
8720 /* Begin the while-statement. */
8721 statement = begin_while_stmt ();
8722 /* Look for the `('. */
8723 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8724 /* Parse the condition. */
8725 condition = cp_parser_condition (parser);
8726 finish_while_stmt_cond (condition, statement);
8727 /* Look for the `)'. */
8728 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8729 /* Parse the dependent statement. */
8730 parser->in_statement = IN_ITERATION_STMT;
8731 cp_parser_already_scoped_statement (parser);
8732 parser->in_statement = in_statement;
8733 /* We're done with the while-statement. */
8734 finish_while_stmt (statement);
8736 break;
8738 case RID_DO:
8740 tree expression;
8742 /* Begin the do-statement. */
8743 statement = begin_do_stmt ();
8744 /* Parse the body of the do-statement. */
8745 parser->in_statement = IN_ITERATION_STMT;
8746 cp_parser_implicitly_scoped_statement (parser, NULL);
8747 parser->in_statement = in_statement;
8748 finish_do_body (statement);
8749 /* Look for the `while' keyword. */
8750 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8751 /* Look for the `('. */
8752 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8753 /* Parse the expression. */
8754 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8755 /* We're done with the do-statement. */
8756 finish_do_stmt (expression, statement);
8757 /* Look for the `)'. */
8758 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8759 /* Look for the `;'. */
8760 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8762 break;
8764 case RID_FOR:
8766 /* Look for the `('. */
8767 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8769 statement = cp_parser_for (parser);
8771 /* Look for the `)'. */
8772 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8774 /* Parse the body of the for-statement. */
8775 parser->in_statement = IN_ITERATION_STMT;
8776 cp_parser_already_scoped_statement (parser);
8777 parser->in_statement = in_statement;
8779 /* We're done with the for-statement. */
8780 finish_for_stmt (statement);
8782 break;
8784 default:
8785 cp_parser_error (parser, "expected iteration-statement");
8786 statement = error_mark_node;
8787 break;
8790 return statement;
8793 /* Parse a for-init-statement or the declarator of a range-based-for.
8794 Returns true if a range-based-for declaration is seen.
8796 for-init-statement:
8797 expression-statement
8798 simple-declaration */
8800 static bool
8801 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
8803 /* If the next token is a `;', then we have an empty
8804 expression-statement. Grammatically, this is also a
8805 simple-declaration, but an invalid one, because it does not
8806 declare anything. Therefore, if we did not handle this case
8807 specially, we would issue an error message about an invalid
8808 declaration. */
8809 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8811 bool is_range_for = false;
8812 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8814 parser->colon_corrects_to_scope_p = false;
8816 /* We're going to speculatively look for a declaration, falling back
8817 to an expression, if necessary. */
8818 cp_parser_parse_tentatively (parser);
8819 /* Parse the declaration. */
8820 cp_parser_simple_declaration (parser,
8821 /*function_definition_allowed_p=*/false,
8822 decl);
8823 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8824 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8826 /* It is a range-for, consume the ':' */
8827 cp_lexer_consume_token (parser->lexer);
8828 is_range_for = true;
8829 if (cxx_dialect < cxx0x)
8831 error_at (cp_lexer_peek_token (parser->lexer)->location,
8832 "range-based-for loops are not allowed "
8833 "in C++98 mode");
8834 *decl = error_mark_node;
8837 else
8838 /* The ';' is not consumed yet because we told
8839 cp_parser_simple_declaration not to. */
8840 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8842 if (cp_parser_parse_definitely (parser))
8843 return is_range_for;
8844 /* If the tentative parse failed, then we shall need to look for an
8845 expression-statement. */
8847 /* If we are here, it is an expression-statement. */
8848 cp_parser_expression_statement (parser, NULL_TREE);
8849 return false;
8852 /* Parse a jump-statement.
8854 jump-statement:
8855 break ;
8856 continue ;
8857 return expression [opt] ;
8858 return braced-init-list ;
8859 goto identifier ;
8861 GNU extension:
8863 jump-statement:
8864 goto * expression ;
8866 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8868 static tree
8869 cp_parser_jump_statement (cp_parser* parser)
8871 tree statement = error_mark_node;
8872 cp_token *token;
8873 enum rid keyword;
8874 unsigned char in_statement;
8876 /* Peek at the next token. */
8877 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
8878 if (!token)
8879 return error_mark_node;
8881 /* See what kind of keyword it is. */
8882 keyword = token->keyword;
8883 switch (keyword)
8885 case RID_BREAK:
8886 in_statement = parser->in_statement & ~IN_IF_STMT;
8887 switch (in_statement)
8889 case 0:
8890 error_at (token->location, "break statement not within loop or switch");
8891 break;
8892 default:
8893 gcc_assert ((in_statement & IN_SWITCH_STMT)
8894 || in_statement == IN_ITERATION_STMT);
8895 statement = finish_break_stmt ();
8896 break;
8897 case IN_OMP_BLOCK:
8898 error_at (token->location, "invalid exit from OpenMP structured block");
8899 break;
8900 case IN_OMP_FOR:
8901 error_at (token->location, "break statement used with OpenMP for loop");
8902 break;
8904 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8905 break;
8907 case RID_CONTINUE:
8908 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8910 case 0:
8911 error_at (token->location, "continue statement not within a loop");
8912 break;
8913 case IN_ITERATION_STMT:
8914 case IN_OMP_FOR:
8915 statement = finish_continue_stmt ();
8916 break;
8917 case IN_OMP_BLOCK:
8918 error_at (token->location, "invalid exit from OpenMP structured block");
8919 break;
8920 default:
8921 gcc_unreachable ();
8923 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8924 break;
8926 case RID_RETURN:
8928 tree expr;
8929 bool expr_non_constant_p;
8931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8933 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8934 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8936 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8937 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8938 else
8939 /* If the next token is a `;', then there is no
8940 expression. */
8941 expr = NULL_TREE;
8942 /* Build the return-statement. */
8943 statement = finish_return_stmt (expr);
8944 /* Look for the final `;'. */
8945 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8947 break;
8949 case RID_GOTO:
8950 /* Create the goto-statement. */
8951 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8953 /* Issue a warning about this use of a GNU extension. */
8954 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8955 /* Consume the '*' token. */
8956 cp_lexer_consume_token (parser->lexer);
8957 /* Parse the dependent expression. */
8958 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8960 else
8961 finish_goto_stmt (cp_parser_identifier (parser));
8962 /* Look for the final `;'. */
8963 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8964 break;
8966 default:
8967 cp_parser_error (parser, "expected jump-statement");
8968 break;
8971 return statement;
8974 /* Parse a declaration-statement.
8976 declaration-statement:
8977 block-declaration */
8979 static void
8980 cp_parser_declaration_statement (cp_parser* parser)
8982 void *p;
8984 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8985 p = obstack_alloc (&declarator_obstack, 0);
8987 /* Parse the block-declaration. */
8988 cp_parser_block_declaration (parser, /*statement_p=*/true);
8990 /* Free any declarators allocated. */
8991 obstack_free (&declarator_obstack, p);
8993 /* Finish off the statement. */
8994 finish_stmt ();
8997 /* Some dependent statements (like `if (cond) statement'), are
8998 implicitly in their own scope. In other words, if the statement is
8999 a single statement (as opposed to a compound-statement), it is
9000 none-the-less treated as if it were enclosed in braces. Any
9001 declarations appearing in the dependent statement are out of scope
9002 after control passes that point. This function parses a statement,
9003 but ensures that is in its own scope, even if it is not a
9004 compound-statement.
9006 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9007 is a (possibly labeled) if statement which is not enclosed in
9008 braces and has an else clause. This is used to implement
9009 -Wparentheses.
9011 Returns the new statement. */
9013 static tree
9014 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9016 tree statement;
9018 if (if_p != NULL)
9019 *if_p = false;
9021 /* Mark if () ; with a special NOP_EXPR. */
9022 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9024 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9025 cp_lexer_consume_token (parser->lexer);
9026 statement = add_stmt (build_empty_stmt (loc));
9028 /* if a compound is opened, we simply parse the statement directly. */
9029 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9030 statement = cp_parser_compound_statement (parser, NULL, false, false);
9031 /* If the token is not a `{', then we must take special action. */
9032 else
9034 /* Create a compound-statement. */
9035 statement = begin_compound_stmt (0);
9036 /* Parse the dependent-statement. */
9037 cp_parser_statement (parser, NULL_TREE, false, if_p);
9038 /* Finish the dummy compound-statement. */
9039 finish_compound_stmt (statement);
9042 /* Return the statement. */
9043 return statement;
9046 /* For some dependent statements (like `while (cond) statement'), we
9047 have already created a scope. Therefore, even if the dependent
9048 statement is a compound-statement, we do not want to create another
9049 scope. */
9051 static void
9052 cp_parser_already_scoped_statement (cp_parser* parser)
9054 /* If the token is a `{', then we must take special action. */
9055 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9056 cp_parser_statement (parser, NULL_TREE, false, NULL);
9057 else
9059 /* Avoid calling cp_parser_compound_statement, so that we
9060 don't create a new scope. Do everything else by hand. */
9061 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9062 /* If the next keyword is `__label__' we have a label declaration. */
9063 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9064 cp_parser_label_declaration (parser);
9065 /* Parse an (optional) statement-seq. */
9066 cp_parser_statement_seq_opt (parser, NULL_TREE);
9067 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9071 /* Declarations [gram.dcl.dcl] */
9073 /* Parse an optional declaration-sequence.
9075 declaration-seq:
9076 declaration
9077 declaration-seq declaration */
9079 static void
9080 cp_parser_declaration_seq_opt (cp_parser* parser)
9082 while (true)
9084 cp_token *token;
9086 token = cp_lexer_peek_token (parser->lexer);
9088 if (token->type == CPP_CLOSE_BRACE
9089 || token->type == CPP_EOF
9090 || token->type == CPP_PRAGMA_EOL)
9091 break;
9093 if (token->type == CPP_SEMICOLON)
9095 /* A declaration consisting of a single semicolon is
9096 invalid. Allow it unless we're being pedantic. */
9097 cp_lexer_consume_token (parser->lexer);
9098 if (!in_system_header)
9099 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9100 continue;
9103 /* If we're entering or exiting a region that's implicitly
9104 extern "C", modify the lang context appropriately. */
9105 if (!parser->implicit_extern_c && token->implicit_extern_c)
9107 push_lang_context (lang_name_c);
9108 parser->implicit_extern_c = true;
9110 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9112 pop_lang_context ();
9113 parser->implicit_extern_c = false;
9116 if (token->type == CPP_PRAGMA)
9118 /* A top-level declaration can consist solely of a #pragma.
9119 A nested declaration cannot, so this is done here and not
9120 in cp_parser_declaration. (A #pragma at block scope is
9121 handled in cp_parser_statement.) */
9122 cp_parser_pragma (parser, pragma_external);
9123 continue;
9126 /* Parse the declaration itself. */
9127 cp_parser_declaration (parser);
9131 /* Parse a declaration.
9133 declaration:
9134 block-declaration
9135 function-definition
9136 template-declaration
9137 explicit-instantiation
9138 explicit-specialization
9139 linkage-specification
9140 namespace-definition
9142 GNU extension:
9144 declaration:
9145 __extension__ declaration */
9147 static void
9148 cp_parser_declaration (cp_parser* parser)
9150 cp_token token1;
9151 cp_token token2;
9152 int saved_pedantic;
9153 void *p;
9154 tree attributes = NULL_TREE;
9156 /* Check for the `__extension__' keyword. */
9157 if (cp_parser_extension_opt (parser, &saved_pedantic))
9159 /* Parse the qualified declaration. */
9160 cp_parser_declaration (parser);
9161 /* Restore the PEDANTIC flag. */
9162 pedantic = saved_pedantic;
9164 return;
9167 /* Try to figure out what kind of declaration is present. */
9168 token1 = *cp_lexer_peek_token (parser->lexer);
9170 if (token1.type != CPP_EOF)
9171 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9172 else
9174 token2.type = CPP_EOF;
9175 token2.keyword = RID_MAX;
9178 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9179 p = obstack_alloc (&declarator_obstack, 0);
9181 /* If the next token is `extern' and the following token is a string
9182 literal, then we have a linkage specification. */
9183 if (token1.keyword == RID_EXTERN
9184 && cp_parser_is_string_literal (&token2))
9185 cp_parser_linkage_specification (parser);
9186 /* If the next token is `template', then we have either a template
9187 declaration, an explicit instantiation, or an explicit
9188 specialization. */
9189 else if (token1.keyword == RID_TEMPLATE)
9191 /* `template <>' indicates a template specialization. */
9192 if (token2.type == CPP_LESS
9193 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9194 cp_parser_explicit_specialization (parser);
9195 /* `template <' indicates a template declaration. */
9196 else if (token2.type == CPP_LESS)
9197 cp_parser_template_declaration (parser, /*member_p=*/false);
9198 /* Anything else must be an explicit instantiation. */
9199 else
9200 cp_parser_explicit_instantiation (parser);
9202 /* If the next token is `export', then we have a template
9203 declaration. */
9204 else if (token1.keyword == RID_EXPORT)
9205 cp_parser_template_declaration (parser, /*member_p=*/false);
9206 /* If the next token is `extern', 'static' or 'inline' and the one
9207 after that is `template', we have a GNU extended explicit
9208 instantiation directive. */
9209 else if (cp_parser_allow_gnu_extensions_p (parser)
9210 && (token1.keyword == RID_EXTERN
9211 || token1.keyword == RID_STATIC
9212 || token1.keyword == RID_INLINE)
9213 && token2.keyword == RID_TEMPLATE)
9214 cp_parser_explicit_instantiation (parser);
9215 /* If the next token is `namespace', check for a named or unnamed
9216 namespace definition. */
9217 else if (token1.keyword == RID_NAMESPACE
9218 && (/* A named namespace definition. */
9219 (token2.type == CPP_NAME
9220 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9221 != CPP_EQ))
9222 /* An unnamed namespace definition. */
9223 || token2.type == CPP_OPEN_BRACE
9224 || token2.keyword == RID_ATTRIBUTE))
9225 cp_parser_namespace_definition (parser);
9226 /* An inline (associated) namespace definition. */
9227 else if (token1.keyword == RID_INLINE
9228 && token2.keyword == RID_NAMESPACE)
9229 cp_parser_namespace_definition (parser);
9230 /* Objective-C++ declaration/definition. */
9231 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9232 cp_parser_objc_declaration (parser, NULL_TREE);
9233 else if (c_dialect_objc ()
9234 && token1.keyword == RID_ATTRIBUTE
9235 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9236 cp_parser_objc_declaration (parser, attributes);
9237 /* We must have either a block declaration or a function
9238 definition. */
9239 else
9240 /* Try to parse a block-declaration, or a function-definition. */
9241 cp_parser_block_declaration (parser, /*statement_p=*/false);
9243 /* Free any declarators allocated. */
9244 obstack_free (&declarator_obstack, p);
9247 /* Parse a block-declaration.
9249 block-declaration:
9250 simple-declaration
9251 asm-definition
9252 namespace-alias-definition
9253 using-declaration
9254 using-directive
9256 GNU Extension:
9258 block-declaration:
9259 __extension__ block-declaration
9261 C++0x Extension:
9263 block-declaration:
9264 static_assert-declaration
9266 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9267 part of a declaration-statement. */
9269 static void
9270 cp_parser_block_declaration (cp_parser *parser,
9271 bool statement_p)
9273 cp_token *token1;
9274 int saved_pedantic;
9276 /* Check for the `__extension__' keyword. */
9277 if (cp_parser_extension_opt (parser, &saved_pedantic))
9279 /* Parse the qualified declaration. */
9280 cp_parser_block_declaration (parser, statement_p);
9281 /* Restore the PEDANTIC flag. */
9282 pedantic = saved_pedantic;
9284 return;
9287 /* Peek at the next token to figure out which kind of declaration is
9288 present. */
9289 token1 = cp_lexer_peek_token (parser->lexer);
9291 /* If the next keyword is `asm', we have an asm-definition. */
9292 if (token1->keyword == RID_ASM)
9294 if (statement_p)
9295 cp_parser_commit_to_tentative_parse (parser);
9296 cp_parser_asm_definition (parser);
9298 /* If the next keyword is `namespace', we have a
9299 namespace-alias-definition. */
9300 else if (token1->keyword == RID_NAMESPACE)
9301 cp_parser_namespace_alias_definition (parser);
9302 /* If the next keyword is `using', we have either a
9303 using-declaration or a using-directive. */
9304 else if (token1->keyword == RID_USING)
9306 cp_token *token2;
9308 if (statement_p)
9309 cp_parser_commit_to_tentative_parse (parser);
9310 /* If the token after `using' is `namespace', then we have a
9311 using-directive. */
9312 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9313 if (token2->keyword == RID_NAMESPACE)
9314 cp_parser_using_directive (parser);
9315 /* Otherwise, it's a using-declaration. */
9316 else
9317 cp_parser_using_declaration (parser,
9318 /*access_declaration_p=*/false);
9320 /* If the next keyword is `__label__' we have a misplaced label
9321 declaration. */
9322 else if (token1->keyword == RID_LABEL)
9324 cp_lexer_consume_token (parser->lexer);
9325 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9326 cp_parser_skip_to_end_of_statement (parser);
9327 /* If the next token is now a `;', consume it. */
9328 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9329 cp_lexer_consume_token (parser->lexer);
9331 /* If the next token is `static_assert' we have a static assertion. */
9332 else if (token1->keyword == RID_STATIC_ASSERT)
9333 cp_parser_static_assert (parser, /*member_p=*/false);
9334 /* Anything else must be a simple-declaration. */
9335 else
9336 cp_parser_simple_declaration (parser, !statement_p,
9337 /*maybe_range_for_decl*/NULL);
9340 /* Parse a simple-declaration.
9342 simple-declaration:
9343 decl-specifier-seq [opt] init-declarator-list [opt] ;
9345 init-declarator-list:
9346 init-declarator
9347 init-declarator-list , init-declarator
9349 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9350 function-definition as a simple-declaration.
9352 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9353 parsed declaration if it is an uninitialized single declarator not followed
9354 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9355 if present, will not be consumed. */
9357 static void
9358 cp_parser_simple_declaration (cp_parser* parser,
9359 bool function_definition_allowed_p,
9360 tree *maybe_range_for_decl)
9362 cp_decl_specifier_seq decl_specifiers;
9363 int declares_class_or_enum;
9364 bool saw_declarator;
9366 if (maybe_range_for_decl)
9367 *maybe_range_for_decl = NULL_TREE;
9369 /* Defer access checks until we know what is being declared; the
9370 checks for names appearing in the decl-specifier-seq should be
9371 done as if we were in the scope of the thing being declared. */
9372 push_deferring_access_checks (dk_deferred);
9374 /* Parse the decl-specifier-seq. We have to keep track of whether
9375 or not the decl-specifier-seq declares a named class or
9376 enumeration type, since that is the only case in which the
9377 init-declarator-list is allowed to be empty.
9379 [dcl.dcl]
9381 In a simple-declaration, the optional init-declarator-list can be
9382 omitted only when declaring a class or enumeration, that is when
9383 the decl-specifier-seq contains either a class-specifier, an
9384 elaborated-type-specifier, or an enum-specifier. */
9385 cp_parser_decl_specifier_seq (parser,
9386 CP_PARSER_FLAGS_OPTIONAL,
9387 &decl_specifiers,
9388 &declares_class_or_enum);
9389 /* We no longer need to defer access checks. */
9390 stop_deferring_access_checks ();
9392 /* In a block scope, a valid declaration must always have a
9393 decl-specifier-seq. By not trying to parse declarators, we can
9394 resolve the declaration/expression ambiguity more quickly. */
9395 if (!function_definition_allowed_p
9396 && !decl_specifiers.any_specifiers_p)
9398 cp_parser_error (parser, "expected declaration");
9399 goto done;
9402 /* If the next two tokens are both identifiers, the code is
9403 erroneous. The usual cause of this situation is code like:
9405 T t;
9407 where "T" should name a type -- but does not. */
9408 if (!decl_specifiers.any_type_specifiers_p
9409 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9411 /* If parsing tentatively, we should commit; we really are
9412 looking at a declaration. */
9413 cp_parser_commit_to_tentative_parse (parser);
9414 /* Give up. */
9415 goto done;
9418 /* If we have seen at least one decl-specifier, and the next token
9419 is not a parenthesis, then we must be looking at a declaration.
9420 (After "int (" we might be looking at a functional cast.) */
9421 if (decl_specifiers.any_specifiers_p
9422 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9423 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9424 && !cp_parser_error_occurred (parser))
9425 cp_parser_commit_to_tentative_parse (parser);
9427 /* Keep going until we hit the `;' at the end of the simple
9428 declaration. */
9429 saw_declarator = false;
9430 while (cp_lexer_next_token_is_not (parser->lexer,
9431 CPP_SEMICOLON))
9433 cp_token *token;
9434 bool function_definition_p;
9435 tree decl;
9437 if (saw_declarator)
9439 /* If we are processing next declarator, coma is expected */
9440 token = cp_lexer_peek_token (parser->lexer);
9441 gcc_assert (token->type == CPP_COMMA);
9442 cp_lexer_consume_token (parser->lexer);
9443 if (maybe_range_for_decl)
9444 *maybe_range_for_decl = error_mark_node;
9446 else
9447 saw_declarator = true;
9449 /* Parse the init-declarator. */
9450 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9451 /*checks=*/NULL,
9452 function_definition_allowed_p,
9453 /*member_p=*/false,
9454 declares_class_or_enum,
9455 &function_definition_p,
9456 maybe_range_for_decl);
9457 /* If an error occurred while parsing tentatively, exit quickly.
9458 (That usually happens when in the body of a function; each
9459 statement is treated as a declaration-statement until proven
9460 otherwise.) */
9461 if (cp_parser_error_occurred (parser))
9462 goto done;
9463 /* Handle function definitions specially. */
9464 if (function_definition_p)
9466 /* If the next token is a `,', then we are probably
9467 processing something like:
9469 void f() {}, *p;
9471 which is erroneous. */
9472 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9474 cp_token *token = cp_lexer_peek_token (parser->lexer);
9475 error_at (token->location,
9476 "mixing"
9477 " declarations and function-definitions is forbidden");
9479 /* Otherwise, we're done with the list of declarators. */
9480 else
9482 pop_deferring_access_checks ();
9483 return;
9486 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9487 *maybe_range_for_decl = decl;
9488 /* The next token should be either a `,' or a `;'. */
9489 token = cp_lexer_peek_token (parser->lexer);
9490 /* If it's a `,', there are more declarators to come. */
9491 if (token->type == CPP_COMMA)
9492 /* will be consumed next time around */;
9493 /* If it's a `;', we are done. */
9494 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9495 break;
9496 /* Anything else is an error. */
9497 else
9499 /* If we have already issued an error message we don't need
9500 to issue another one. */
9501 if (decl != error_mark_node
9502 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9503 cp_parser_error (parser, "expected %<,%> or %<;%>");
9504 /* Skip tokens until we reach the end of the statement. */
9505 cp_parser_skip_to_end_of_statement (parser);
9506 /* If the next token is now a `;', consume it. */
9507 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9508 cp_lexer_consume_token (parser->lexer);
9509 goto done;
9511 /* After the first time around, a function-definition is not
9512 allowed -- even if it was OK at first. For example:
9514 int i, f() {}
9516 is not valid. */
9517 function_definition_allowed_p = false;
9520 /* Issue an error message if no declarators are present, and the
9521 decl-specifier-seq does not itself declare a class or
9522 enumeration. */
9523 if (!saw_declarator)
9525 if (cp_parser_declares_only_class_p (parser))
9526 shadow_tag (&decl_specifiers);
9527 /* Perform any deferred access checks. */
9528 perform_deferred_access_checks ();
9531 /* Consume the `;'. */
9532 if (!maybe_range_for_decl)
9533 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9535 done:
9536 pop_deferring_access_checks ();
9539 /* Parse a decl-specifier-seq.
9541 decl-specifier-seq:
9542 decl-specifier-seq [opt] decl-specifier
9544 decl-specifier:
9545 storage-class-specifier
9546 type-specifier
9547 function-specifier
9548 friend
9549 typedef
9551 GNU Extension:
9553 decl-specifier:
9554 attributes
9556 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9558 The parser flags FLAGS is used to control type-specifier parsing.
9560 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9561 flags:
9563 1: one of the decl-specifiers is an elaborated-type-specifier
9564 (i.e., a type declaration)
9565 2: one of the decl-specifiers is an enum-specifier or a
9566 class-specifier (i.e., a type definition)
9570 static void
9571 cp_parser_decl_specifier_seq (cp_parser* parser,
9572 cp_parser_flags flags,
9573 cp_decl_specifier_seq *decl_specs,
9574 int* declares_class_or_enum)
9576 bool constructor_possible_p = !parser->in_declarator_p;
9577 cp_token *start_token = NULL;
9579 /* Clear DECL_SPECS. */
9580 clear_decl_specs (decl_specs);
9582 /* Assume no class or enumeration type is declared. */
9583 *declares_class_or_enum = 0;
9585 /* Keep reading specifiers until there are no more to read. */
9586 while (true)
9588 bool constructor_p;
9589 bool found_decl_spec;
9590 cp_token *token;
9592 /* Peek at the next token. */
9593 token = cp_lexer_peek_token (parser->lexer);
9595 /* Save the first token of the decl spec list for error
9596 reporting. */
9597 if (!start_token)
9598 start_token = token;
9599 /* Handle attributes. */
9600 if (token->keyword == RID_ATTRIBUTE)
9602 /* Parse the attributes. */
9603 decl_specs->attributes
9604 = chainon (decl_specs->attributes,
9605 cp_parser_attributes_opt (parser));
9606 continue;
9608 /* Assume we will find a decl-specifier keyword. */
9609 found_decl_spec = true;
9610 /* If the next token is an appropriate keyword, we can simply
9611 add it to the list. */
9612 switch (token->keyword)
9614 /* decl-specifier:
9615 friend
9616 constexpr */
9617 case RID_FRIEND:
9618 if (!at_class_scope_p ())
9620 error_at (token->location, "%<friend%> used outside of class");
9621 cp_lexer_purge_token (parser->lexer);
9623 else
9625 ++decl_specs->specs[(int) ds_friend];
9626 /* Consume the token. */
9627 cp_lexer_consume_token (parser->lexer);
9629 break;
9631 case RID_CONSTEXPR:
9632 ++decl_specs->specs[(int) ds_constexpr];
9633 cp_lexer_consume_token (parser->lexer);
9634 break;
9636 /* function-specifier:
9637 inline
9638 virtual
9639 explicit */
9640 case RID_INLINE:
9641 case RID_VIRTUAL:
9642 case RID_EXPLICIT:
9643 cp_parser_function_specifier_opt (parser, decl_specs);
9644 break;
9646 /* decl-specifier:
9647 typedef */
9648 case RID_TYPEDEF:
9649 ++decl_specs->specs[(int) ds_typedef];
9650 /* Consume the token. */
9651 cp_lexer_consume_token (parser->lexer);
9652 /* A constructor declarator cannot appear in a typedef. */
9653 constructor_possible_p = false;
9654 /* The "typedef" keyword can only occur in a declaration; we
9655 may as well commit at this point. */
9656 cp_parser_commit_to_tentative_parse (parser);
9658 if (decl_specs->storage_class != sc_none)
9659 decl_specs->conflicting_specifiers_p = true;
9660 break;
9662 /* storage-class-specifier:
9663 auto
9664 register
9665 static
9666 extern
9667 mutable
9669 GNU Extension:
9670 thread */
9671 case RID_AUTO:
9672 if (cxx_dialect == cxx98)
9674 /* Consume the token. */
9675 cp_lexer_consume_token (parser->lexer);
9677 /* Complain about `auto' as a storage specifier, if
9678 we're complaining about C++0x compatibility. */
9679 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9680 " will change meaning in C++0x; please remove it");
9682 /* Set the storage class anyway. */
9683 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9684 token->location);
9686 else
9687 /* C++0x auto type-specifier. */
9688 found_decl_spec = false;
9689 break;
9691 case RID_REGISTER:
9692 case RID_STATIC:
9693 case RID_EXTERN:
9694 case RID_MUTABLE:
9695 /* Consume the token. */
9696 cp_lexer_consume_token (parser->lexer);
9697 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9698 token->location);
9699 break;
9700 case RID_THREAD:
9701 /* Consume the token. */
9702 cp_lexer_consume_token (parser->lexer);
9703 ++decl_specs->specs[(int) ds_thread];
9704 break;
9706 default:
9707 /* We did not yet find a decl-specifier yet. */
9708 found_decl_spec = false;
9709 break;
9712 if (found_decl_spec
9713 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9714 && token->keyword != RID_CONSTEXPR)
9715 error ("decl-specifier invalid in condition");
9717 /* Constructors are a special case. The `S' in `S()' is not a
9718 decl-specifier; it is the beginning of the declarator. */
9719 constructor_p
9720 = (!found_decl_spec
9721 && constructor_possible_p
9722 && (cp_parser_constructor_declarator_p
9723 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9725 /* If we don't have a DECL_SPEC yet, then we must be looking at
9726 a type-specifier. */
9727 if (!found_decl_spec && !constructor_p)
9729 int decl_spec_declares_class_or_enum;
9730 bool is_cv_qualifier;
9731 tree type_spec;
9733 type_spec
9734 = cp_parser_type_specifier (parser, flags,
9735 decl_specs,
9736 /*is_declaration=*/true,
9737 &decl_spec_declares_class_or_enum,
9738 &is_cv_qualifier);
9739 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9741 /* If this type-specifier referenced a user-defined type
9742 (a typedef, class-name, etc.), then we can't allow any
9743 more such type-specifiers henceforth.
9745 [dcl.spec]
9747 The longest sequence of decl-specifiers that could
9748 possibly be a type name is taken as the
9749 decl-specifier-seq of a declaration. The sequence shall
9750 be self-consistent as described below.
9752 [dcl.type]
9754 As a general rule, at most one type-specifier is allowed
9755 in the complete decl-specifier-seq of a declaration. The
9756 only exceptions are the following:
9758 -- const or volatile can be combined with any other
9759 type-specifier.
9761 -- signed or unsigned can be combined with char, long,
9762 short, or int.
9764 -- ..
9766 Example:
9768 typedef char* Pc;
9769 void g (const int Pc);
9771 Here, Pc is *not* part of the decl-specifier seq; it's
9772 the declarator. Therefore, once we see a type-specifier
9773 (other than a cv-qualifier), we forbid any additional
9774 user-defined types. We *do* still allow things like `int
9775 int' to be considered a decl-specifier-seq, and issue the
9776 error message later. */
9777 if (type_spec && !is_cv_qualifier)
9778 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9779 /* A constructor declarator cannot follow a type-specifier. */
9780 if (type_spec)
9782 constructor_possible_p = false;
9783 found_decl_spec = true;
9784 if (!is_cv_qualifier)
9785 decl_specs->any_type_specifiers_p = true;
9789 /* If we still do not have a DECL_SPEC, then there are no more
9790 decl-specifiers. */
9791 if (!found_decl_spec)
9792 break;
9794 decl_specs->any_specifiers_p = true;
9795 /* After we see one decl-specifier, further decl-specifiers are
9796 always optional. */
9797 flags |= CP_PARSER_FLAGS_OPTIONAL;
9800 cp_parser_check_decl_spec (decl_specs, start_token->location);
9802 /* Don't allow a friend specifier with a class definition. */
9803 if (decl_specs->specs[(int) ds_friend] != 0
9804 && (*declares_class_or_enum & 2))
9805 error_at (start_token->location,
9806 "class definition may not be declared a friend");
9809 /* Parse an (optional) storage-class-specifier.
9811 storage-class-specifier:
9812 auto
9813 register
9814 static
9815 extern
9816 mutable
9818 GNU Extension:
9820 storage-class-specifier:
9821 thread
9823 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9825 static tree
9826 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9828 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9830 case RID_AUTO:
9831 if (cxx_dialect != cxx98)
9832 return NULL_TREE;
9833 /* Fall through for C++98. */
9835 case RID_REGISTER:
9836 case RID_STATIC:
9837 case RID_EXTERN:
9838 case RID_MUTABLE:
9839 case RID_THREAD:
9840 /* Consume the token. */
9841 return cp_lexer_consume_token (parser->lexer)->u.value;
9843 default:
9844 return NULL_TREE;
9848 /* Parse an (optional) function-specifier.
9850 function-specifier:
9851 inline
9852 virtual
9853 explicit
9855 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9856 Updates DECL_SPECS, if it is non-NULL. */
9858 static tree
9859 cp_parser_function_specifier_opt (cp_parser* parser,
9860 cp_decl_specifier_seq *decl_specs)
9862 cp_token *token = cp_lexer_peek_token (parser->lexer);
9863 switch (token->keyword)
9865 case RID_INLINE:
9866 if (decl_specs)
9867 ++decl_specs->specs[(int) ds_inline];
9868 break;
9870 case RID_VIRTUAL:
9871 /* 14.5.2.3 [temp.mem]
9873 A member function template shall not be virtual. */
9874 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9875 error_at (token->location, "templates may not be %<virtual%>");
9876 else if (decl_specs)
9877 ++decl_specs->specs[(int) ds_virtual];
9878 break;
9880 case RID_EXPLICIT:
9881 if (decl_specs)
9882 ++decl_specs->specs[(int) ds_explicit];
9883 break;
9885 default:
9886 return NULL_TREE;
9889 /* Consume the token. */
9890 return cp_lexer_consume_token (parser->lexer)->u.value;
9893 /* Parse a linkage-specification.
9895 linkage-specification:
9896 extern string-literal { declaration-seq [opt] }
9897 extern string-literal declaration */
9899 static void
9900 cp_parser_linkage_specification (cp_parser* parser)
9902 tree linkage;
9904 /* Look for the `extern' keyword. */
9905 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
9907 /* Look for the string-literal. */
9908 linkage = cp_parser_string_literal (parser, false, false);
9910 /* Transform the literal into an identifier. If the literal is a
9911 wide-character string, or contains embedded NULs, then we can't
9912 handle it as the user wants. */
9913 if (strlen (TREE_STRING_POINTER (linkage))
9914 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9916 cp_parser_error (parser, "invalid linkage-specification");
9917 /* Assume C++ linkage. */
9918 linkage = lang_name_cplusplus;
9920 else
9921 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9923 /* We're now using the new linkage. */
9924 push_lang_context (linkage);
9926 /* If the next token is a `{', then we're using the first
9927 production. */
9928 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9930 /* Consume the `{' token. */
9931 cp_lexer_consume_token (parser->lexer);
9932 /* Parse the declarations. */
9933 cp_parser_declaration_seq_opt (parser);
9934 /* Look for the closing `}'. */
9935 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9937 /* Otherwise, there's just one declaration. */
9938 else
9940 bool saved_in_unbraced_linkage_specification_p;
9942 saved_in_unbraced_linkage_specification_p
9943 = parser->in_unbraced_linkage_specification_p;
9944 parser->in_unbraced_linkage_specification_p = true;
9945 cp_parser_declaration (parser);
9946 parser->in_unbraced_linkage_specification_p
9947 = saved_in_unbraced_linkage_specification_p;
9950 /* We're done with the linkage-specification. */
9951 pop_lang_context ();
9954 /* Parse a static_assert-declaration.
9956 static_assert-declaration:
9957 static_assert ( constant-expression , string-literal ) ;
9959 If MEMBER_P, this static_assert is a class member. */
9961 static void
9962 cp_parser_static_assert(cp_parser *parser, bool member_p)
9964 tree condition;
9965 tree message;
9966 cp_token *token;
9967 location_t saved_loc;
9968 bool dummy;
9970 /* Peek at the `static_assert' token so we can keep track of exactly
9971 where the static assertion started. */
9972 token = cp_lexer_peek_token (parser->lexer);
9973 saved_loc = token->location;
9975 /* Look for the `static_assert' keyword. */
9976 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9977 RT_STATIC_ASSERT))
9978 return;
9980 /* We know we are in a static assertion; commit to any tentative
9981 parse. */
9982 if (cp_parser_parsing_tentatively (parser))
9983 cp_parser_commit_to_tentative_parse (parser);
9985 /* Parse the `(' starting the static assertion condition. */
9986 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9988 /* Parse the constant-expression. Allow a non-constant expression
9989 here in order to give better diagnostics in finish_static_assert. */
9990 condition =
9991 cp_parser_constant_expression (parser,
9992 /*allow_non_constant_p=*/true,
9993 /*non_constant_p=*/&dummy);
9995 /* Parse the separating `,'. */
9996 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9998 /* Parse the string-literal message. */
9999 message = cp_parser_string_literal (parser,
10000 /*translate=*/false,
10001 /*wide_ok=*/true);
10003 /* A `)' completes the static assertion. */
10004 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10005 cp_parser_skip_to_closing_parenthesis (parser,
10006 /*recovering=*/true,
10007 /*or_comma=*/false,
10008 /*consume_paren=*/true);
10010 /* A semicolon terminates the declaration. */
10011 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10013 /* Complete the static assertion, which may mean either processing
10014 the static assert now or saving it for template instantiation. */
10015 finish_static_assert (condition, message, saved_loc, member_p);
10018 /* Parse a `decltype' type. Returns the type.
10020 simple-type-specifier:
10021 decltype ( expression ) */
10023 static tree
10024 cp_parser_decltype (cp_parser *parser)
10026 tree expr;
10027 bool id_expression_or_member_access_p = false;
10028 const char *saved_message;
10029 bool saved_integral_constant_expression_p;
10030 bool saved_non_integral_constant_expression_p;
10031 cp_token *id_expr_start_token;
10033 /* Look for the `decltype' token. */
10034 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10035 return error_mark_node;
10037 /* Types cannot be defined in a `decltype' expression. Save away the
10038 old message. */
10039 saved_message = parser->type_definition_forbidden_message;
10041 /* And create the new one. */
10042 parser->type_definition_forbidden_message
10043 = G_("types may not be defined in %<decltype%> expressions");
10045 /* The restrictions on constant-expressions do not apply inside
10046 decltype expressions. */
10047 saved_integral_constant_expression_p
10048 = parser->integral_constant_expression_p;
10049 saved_non_integral_constant_expression_p
10050 = parser->non_integral_constant_expression_p;
10051 parser->integral_constant_expression_p = false;
10053 /* Do not actually evaluate the expression. */
10054 ++cp_unevaluated_operand;
10056 /* Do not warn about problems with the expression. */
10057 ++c_inhibit_evaluation_warnings;
10059 /* Parse the opening `('. */
10060 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10061 return error_mark_node;
10063 /* First, try parsing an id-expression. */
10064 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10065 cp_parser_parse_tentatively (parser);
10066 expr = cp_parser_id_expression (parser,
10067 /*template_keyword_p=*/false,
10068 /*check_dependency_p=*/true,
10069 /*template_p=*/NULL,
10070 /*declarator_p=*/false,
10071 /*optional_p=*/false);
10073 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10075 bool non_integral_constant_expression_p = false;
10076 tree id_expression = expr;
10077 cp_id_kind idk;
10078 const char *error_msg;
10080 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10081 /* Lookup the name we got back from the id-expression. */
10082 expr = cp_parser_lookup_name (parser, expr,
10083 none_type,
10084 /*is_template=*/false,
10085 /*is_namespace=*/false,
10086 /*check_dependency=*/true,
10087 /*ambiguous_decls=*/NULL,
10088 id_expr_start_token->location);
10090 if (expr
10091 && expr != error_mark_node
10092 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10093 && TREE_CODE (expr) != TYPE_DECL
10094 && (TREE_CODE (expr) != BIT_NOT_EXPR
10095 || !TYPE_P (TREE_OPERAND (expr, 0)))
10096 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10098 /* Complete lookup of the id-expression. */
10099 expr = (finish_id_expression
10100 (id_expression, expr, parser->scope, &idk,
10101 /*integral_constant_expression_p=*/false,
10102 /*allow_non_integral_constant_expression_p=*/true,
10103 &non_integral_constant_expression_p,
10104 /*template_p=*/false,
10105 /*done=*/true,
10106 /*address_p=*/false,
10107 /*template_arg_p=*/false,
10108 &error_msg,
10109 id_expr_start_token->location));
10111 if (expr == error_mark_node)
10112 /* We found an id-expression, but it was something that we
10113 should not have found. This is an error, not something
10114 we can recover from, so note that we found an
10115 id-expression and we'll recover as gracefully as
10116 possible. */
10117 id_expression_or_member_access_p = true;
10120 if (expr
10121 && expr != error_mark_node
10122 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10123 /* We have an id-expression. */
10124 id_expression_or_member_access_p = true;
10127 if (!id_expression_or_member_access_p)
10129 /* Abort the id-expression parse. */
10130 cp_parser_abort_tentative_parse (parser);
10132 /* Parsing tentatively, again. */
10133 cp_parser_parse_tentatively (parser);
10135 /* Parse a class member access. */
10136 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10137 /*cast_p=*/false,
10138 /*member_access_only_p=*/true, NULL);
10140 if (expr
10141 && expr != error_mark_node
10142 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10143 /* We have an id-expression. */
10144 id_expression_or_member_access_p = true;
10147 if (id_expression_or_member_access_p)
10148 /* We have parsed the complete id-expression or member access. */
10149 cp_parser_parse_definitely (parser);
10150 else
10152 bool saved_greater_than_is_operator_p;
10154 /* Abort our attempt to parse an id-expression or member access
10155 expression. */
10156 cp_parser_abort_tentative_parse (parser);
10158 /* Within a parenthesized expression, a `>' token is always
10159 the greater-than operator. */
10160 saved_greater_than_is_operator_p
10161 = parser->greater_than_is_operator_p;
10162 parser->greater_than_is_operator_p = true;
10164 /* Parse a full expression. */
10165 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10167 /* The `>' token might be the end of a template-id or
10168 template-parameter-list now. */
10169 parser->greater_than_is_operator_p
10170 = saved_greater_than_is_operator_p;
10173 /* Go back to evaluating expressions. */
10174 --cp_unevaluated_operand;
10175 --c_inhibit_evaluation_warnings;
10177 /* Restore the old message and the integral constant expression
10178 flags. */
10179 parser->type_definition_forbidden_message = saved_message;
10180 parser->integral_constant_expression_p
10181 = saved_integral_constant_expression_p;
10182 parser->non_integral_constant_expression_p
10183 = saved_non_integral_constant_expression_p;
10185 if (expr == error_mark_node)
10187 /* Skip everything up to the closing `)'. */
10188 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10189 /*consume_paren=*/true);
10190 return error_mark_node;
10193 /* Parse to the closing `)'. */
10194 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10196 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10197 /*consume_paren=*/true);
10198 return error_mark_node;
10201 return finish_decltype_type (expr, id_expression_or_member_access_p,
10202 tf_warning_or_error);
10205 /* Special member functions [gram.special] */
10207 /* Parse a conversion-function-id.
10209 conversion-function-id:
10210 operator conversion-type-id
10212 Returns an IDENTIFIER_NODE representing the operator. */
10214 static tree
10215 cp_parser_conversion_function_id (cp_parser* parser)
10217 tree type;
10218 tree saved_scope;
10219 tree saved_qualifying_scope;
10220 tree saved_object_scope;
10221 tree pushed_scope = NULL_TREE;
10223 /* Look for the `operator' token. */
10224 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10225 return error_mark_node;
10226 /* When we parse the conversion-type-id, the current scope will be
10227 reset. However, we need that information in able to look up the
10228 conversion function later, so we save it here. */
10229 saved_scope = parser->scope;
10230 saved_qualifying_scope = parser->qualifying_scope;
10231 saved_object_scope = parser->object_scope;
10232 /* We must enter the scope of the class so that the names of
10233 entities declared within the class are available in the
10234 conversion-type-id. For example, consider:
10236 struct S {
10237 typedef int I;
10238 operator I();
10241 S::operator I() { ... }
10243 In order to see that `I' is a type-name in the definition, we
10244 must be in the scope of `S'. */
10245 if (saved_scope)
10246 pushed_scope = push_scope (saved_scope);
10247 /* Parse the conversion-type-id. */
10248 type = cp_parser_conversion_type_id (parser);
10249 /* Leave the scope of the class, if any. */
10250 if (pushed_scope)
10251 pop_scope (pushed_scope);
10252 /* Restore the saved scope. */
10253 parser->scope = saved_scope;
10254 parser->qualifying_scope = saved_qualifying_scope;
10255 parser->object_scope = saved_object_scope;
10256 /* If the TYPE is invalid, indicate failure. */
10257 if (type == error_mark_node)
10258 return error_mark_node;
10259 return mangle_conv_op_name_for_type (type);
10262 /* Parse a conversion-type-id:
10264 conversion-type-id:
10265 type-specifier-seq conversion-declarator [opt]
10267 Returns the TYPE specified. */
10269 static tree
10270 cp_parser_conversion_type_id (cp_parser* parser)
10272 tree attributes;
10273 cp_decl_specifier_seq type_specifiers;
10274 cp_declarator *declarator;
10275 tree type_specified;
10277 /* Parse the attributes. */
10278 attributes = cp_parser_attributes_opt (parser);
10279 /* Parse the type-specifiers. */
10280 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10281 /*is_trailing_return=*/false,
10282 &type_specifiers);
10283 /* If that didn't work, stop. */
10284 if (type_specifiers.type == error_mark_node)
10285 return error_mark_node;
10286 /* Parse the conversion-declarator. */
10287 declarator = cp_parser_conversion_declarator_opt (parser);
10289 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10290 /*initialized=*/0, &attributes);
10291 if (attributes)
10292 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10294 /* Don't give this error when parsing tentatively. This happens to
10295 work because we always parse this definitively once. */
10296 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10297 && type_uses_auto (type_specified))
10299 error ("invalid use of %<auto%> in conversion operator");
10300 return error_mark_node;
10303 return type_specified;
10306 /* Parse an (optional) conversion-declarator.
10308 conversion-declarator:
10309 ptr-operator conversion-declarator [opt]
10313 static cp_declarator *
10314 cp_parser_conversion_declarator_opt (cp_parser* parser)
10316 enum tree_code code;
10317 tree class_type;
10318 cp_cv_quals cv_quals;
10320 /* We don't know if there's a ptr-operator next, or not. */
10321 cp_parser_parse_tentatively (parser);
10322 /* Try the ptr-operator. */
10323 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10324 /* If it worked, look for more conversion-declarators. */
10325 if (cp_parser_parse_definitely (parser))
10327 cp_declarator *declarator;
10329 /* Parse another optional declarator. */
10330 declarator = cp_parser_conversion_declarator_opt (parser);
10332 return cp_parser_make_indirect_declarator
10333 (code, class_type, cv_quals, declarator);
10336 return NULL;
10339 /* Parse an (optional) ctor-initializer.
10341 ctor-initializer:
10342 : mem-initializer-list
10344 Returns TRUE iff the ctor-initializer was actually present. */
10346 static bool
10347 cp_parser_ctor_initializer_opt (cp_parser* parser)
10349 /* If the next token is not a `:', then there is no
10350 ctor-initializer. */
10351 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10353 /* Do default initialization of any bases and members. */
10354 if (DECL_CONSTRUCTOR_P (current_function_decl))
10355 finish_mem_initializers (NULL_TREE);
10357 return false;
10360 /* Consume the `:' token. */
10361 cp_lexer_consume_token (parser->lexer);
10362 /* And the mem-initializer-list. */
10363 cp_parser_mem_initializer_list (parser);
10365 return true;
10368 /* Parse a mem-initializer-list.
10370 mem-initializer-list:
10371 mem-initializer ... [opt]
10372 mem-initializer ... [opt] , mem-initializer-list */
10374 static void
10375 cp_parser_mem_initializer_list (cp_parser* parser)
10377 tree mem_initializer_list = NULL_TREE;
10378 cp_token *token = cp_lexer_peek_token (parser->lexer);
10380 /* Let the semantic analysis code know that we are starting the
10381 mem-initializer-list. */
10382 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10383 error_at (token->location,
10384 "only constructors take member initializers");
10386 /* Loop through the list. */
10387 while (true)
10389 tree mem_initializer;
10391 token = cp_lexer_peek_token (parser->lexer);
10392 /* Parse the mem-initializer. */
10393 mem_initializer = cp_parser_mem_initializer (parser);
10394 /* If the next token is a `...', we're expanding member initializers. */
10395 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10397 /* Consume the `...'. */
10398 cp_lexer_consume_token (parser->lexer);
10400 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10401 can be expanded but members cannot. */
10402 if (mem_initializer != error_mark_node
10403 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10405 error_at (token->location,
10406 "cannot expand initializer for member %<%D%>",
10407 TREE_PURPOSE (mem_initializer));
10408 mem_initializer = error_mark_node;
10411 /* Construct the pack expansion type. */
10412 if (mem_initializer != error_mark_node)
10413 mem_initializer = make_pack_expansion (mem_initializer);
10415 /* Add it to the list, unless it was erroneous. */
10416 if (mem_initializer != error_mark_node)
10418 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10419 mem_initializer_list = mem_initializer;
10421 /* If the next token is not a `,', we're done. */
10422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10423 break;
10424 /* Consume the `,' token. */
10425 cp_lexer_consume_token (parser->lexer);
10428 /* Perform semantic analysis. */
10429 if (DECL_CONSTRUCTOR_P (current_function_decl))
10430 finish_mem_initializers (mem_initializer_list);
10433 /* Parse a mem-initializer.
10435 mem-initializer:
10436 mem-initializer-id ( expression-list [opt] )
10437 mem-initializer-id braced-init-list
10439 GNU extension:
10441 mem-initializer:
10442 ( expression-list [opt] )
10444 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10445 class) or FIELD_DECL (for a non-static data member) to initialize;
10446 the TREE_VALUE is the expression-list. An empty initialization
10447 list is represented by void_list_node. */
10449 static tree
10450 cp_parser_mem_initializer (cp_parser* parser)
10452 tree mem_initializer_id;
10453 tree expression_list;
10454 tree member;
10455 cp_token *token = cp_lexer_peek_token (parser->lexer);
10457 /* Find out what is being initialized. */
10458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10460 permerror (token->location,
10461 "anachronistic old-style base class initializer");
10462 mem_initializer_id = NULL_TREE;
10464 else
10466 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10467 if (mem_initializer_id == error_mark_node)
10468 return mem_initializer_id;
10470 member = expand_member_init (mem_initializer_id);
10471 if (member && !DECL_P (member))
10472 in_base_initializer = 1;
10474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10476 bool expr_non_constant_p;
10477 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10478 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10479 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10480 expression_list = build_tree_list (NULL_TREE, expression_list);
10482 else
10484 VEC(tree,gc)* vec;
10485 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10486 /*cast_p=*/false,
10487 /*allow_expansion_p=*/true,
10488 /*non_constant_p=*/NULL);
10489 if (vec == NULL)
10490 return error_mark_node;
10491 expression_list = build_tree_list_vec (vec);
10492 release_tree_vector (vec);
10495 if (expression_list == error_mark_node)
10496 return error_mark_node;
10497 if (!expression_list)
10498 expression_list = void_type_node;
10500 in_base_initializer = 0;
10502 return member ? build_tree_list (member, expression_list) : error_mark_node;
10505 /* Parse a mem-initializer-id.
10507 mem-initializer-id:
10508 :: [opt] nested-name-specifier [opt] class-name
10509 identifier
10511 Returns a TYPE indicating the class to be initializer for the first
10512 production. Returns an IDENTIFIER_NODE indicating the data member
10513 to be initialized for the second production. */
10515 static tree
10516 cp_parser_mem_initializer_id (cp_parser* parser)
10518 bool global_scope_p;
10519 bool nested_name_specifier_p;
10520 bool template_p = false;
10521 tree id;
10523 cp_token *token = cp_lexer_peek_token (parser->lexer);
10525 /* `typename' is not allowed in this context ([temp.res]). */
10526 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10528 error_at (token->location,
10529 "keyword %<typename%> not allowed in this context (a qualified "
10530 "member initializer is implicitly a type)");
10531 cp_lexer_consume_token (parser->lexer);
10533 /* Look for the optional `::' operator. */
10534 global_scope_p
10535 = (cp_parser_global_scope_opt (parser,
10536 /*current_scope_valid_p=*/false)
10537 != NULL_TREE);
10538 /* Look for the optional nested-name-specifier. The simplest way to
10539 implement:
10541 [temp.res]
10543 The keyword `typename' is not permitted in a base-specifier or
10544 mem-initializer; in these contexts a qualified name that
10545 depends on a template-parameter is implicitly assumed to be a
10546 type name.
10548 is to assume that we have seen the `typename' keyword at this
10549 point. */
10550 nested_name_specifier_p
10551 = (cp_parser_nested_name_specifier_opt (parser,
10552 /*typename_keyword_p=*/true,
10553 /*check_dependency_p=*/true,
10554 /*type_p=*/true,
10555 /*is_declaration=*/true)
10556 != NULL_TREE);
10557 if (nested_name_specifier_p)
10558 template_p = cp_parser_optional_template_keyword (parser);
10559 /* If there is a `::' operator or a nested-name-specifier, then we
10560 are definitely looking for a class-name. */
10561 if (global_scope_p || nested_name_specifier_p)
10562 return cp_parser_class_name (parser,
10563 /*typename_keyword_p=*/true,
10564 /*template_keyword_p=*/template_p,
10565 typename_type,
10566 /*check_dependency_p=*/true,
10567 /*class_head_p=*/false,
10568 /*is_declaration=*/true);
10569 /* Otherwise, we could also be looking for an ordinary identifier. */
10570 cp_parser_parse_tentatively (parser);
10571 /* Try a class-name. */
10572 id = cp_parser_class_name (parser,
10573 /*typename_keyword_p=*/true,
10574 /*template_keyword_p=*/false,
10575 none_type,
10576 /*check_dependency_p=*/true,
10577 /*class_head_p=*/false,
10578 /*is_declaration=*/true);
10579 /* If we found one, we're done. */
10580 if (cp_parser_parse_definitely (parser))
10581 return id;
10582 /* Otherwise, look for an ordinary identifier. */
10583 return cp_parser_identifier (parser);
10586 /* Overloading [gram.over] */
10588 /* Parse an operator-function-id.
10590 operator-function-id:
10591 operator operator
10593 Returns an IDENTIFIER_NODE for the operator which is a
10594 human-readable spelling of the identifier, e.g., `operator +'. */
10596 static tree
10597 cp_parser_operator_function_id (cp_parser* parser)
10599 /* Look for the `operator' keyword. */
10600 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10601 return error_mark_node;
10602 /* And then the name of the operator itself. */
10603 return cp_parser_operator (parser);
10606 /* Parse an operator.
10608 operator:
10609 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10610 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10611 || ++ -- , ->* -> () []
10613 GNU Extensions:
10615 operator:
10616 <? >? <?= >?=
10618 Returns an IDENTIFIER_NODE for the operator which is a
10619 human-readable spelling of the identifier, e.g., `operator +'. */
10621 static tree
10622 cp_parser_operator (cp_parser* parser)
10624 tree id = NULL_TREE;
10625 cp_token *token;
10627 /* Peek at the next token. */
10628 token = cp_lexer_peek_token (parser->lexer);
10629 /* Figure out which operator we have. */
10630 switch (token->type)
10632 case CPP_KEYWORD:
10634 enum tree_code op;
10636 /* The keyword should be either `new' or `delete'. */
10637 if (token->keyword == RID_NEW)
10638 op = NEW_EXPR;
10639 else if (token->keyword == RID_DELETE)
10640 op = DELETE_EXPR;
10641 else
10642 break;
10644 /* Consume the `new' or `delete' token. */
10645 cp_lexer_consume_token (parser->lexer);
10647 /* Peek at the next token. */
10648 token = cp_lexer_peek_token (parser->lexer);
10649 /* If it's a `[' token then this is the array variant of the
10650 operator. */
10651 if (token->type == CPP_OPEN_SQUARE)
10653 /* Consume the `[' token. */
10654 cp_lexer_consume_token (parser->lexer);
10655 /* Look for the `]' token. */
10656 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10657 id = ansi_opname (op == NEW_EXPR
10658 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10660 /* Otherwise, we have the non-array variant. */
10661 else
10662 id = ansi_opname (op);
10664 return id;
10667 case CPP_PLUS:
10668 id = ansi_opname (PLUS_EXPR);
10669 break;
10671 case CPP_MINUS:
10672 id = ansi_opname (MINUS_EXPR);
10673 break;
10675 case CPP_MULT:
10676 id = ansi_opname (MULT_EXPR);
10677 break;
10679 case CPP_DIV:
10680 id = ansi_opname (TRUNC_DIV_EXPR);
10681 break;
10683 case CPP_MOD:
10684 id = ansi_opname (TRUNC_MOD_EXPR);
10685 break;
10687 case CPP_XOR:
10688 id = ansi_opname (BIT_XOR_EXPR);
10689 break;
10691 case CPP_AND:
10692 id = ansi_opname (BIT_AND_EXPR);
10693 break;
10695 case CPP_OR:
10696 id = ansi_opname (BIT_IOR_EXPR);
10697 break;
10699 case CPP_COMPL:
10700 id = ansi_opname (BIT_NOT_EXPR);
10701 break;
10703 case CPP_NOT:
10704 id = ansi_opname (TRUTH_NOT_EXPR);
10705 break;
10707 case CPP_EQ:
10708 id = ansi_assopname (NOP_EXPR);
10709 break;
10711 case CPP_LESS:
10712 id = ansi_opname (LT_EXPR);
10713 break;
10715 case CPP_GREATER:
10716 id = ansi_opname (GT_EXPR);
10717 break;
10719 case CPP_PLUS_EQ:
10720 id = ansi_assopname (PLUS_EXPR);
10721 break;
10723 case CPP_MINUS_EQ:
10724 id = ansi_assopname (MINUS_EXPR);
10725 break;
10727 case CPP_MULT_EQ:
10728 id = ansi_assopname (MULT_EXPR);
10729 break;
10731 case CPP_DIV_EQ:
10732 id = ansi_assopname (TRUNC_DIV_EXPR);
10733 break;
10735 case CPP_MOD_EQ:
10736 id = ansi_assopname (TRUNC_MOD_EXPR);
10737 break;
10739 case CPP_XOR_EQ:
10740 id = ansi_assopname (BIT_XOR_EXPR);
10741 break;
10743 case CPP_AND_EQ:
10744 id = ansi_assopname (BIT_AND_EXPR);
10745 break;
10747 case CPP_OR_EQ:
10748 id = ansi_assopname (BIT_IOR_EXPR);
10749 break;
10751 case CPP_LSHIFT:
10752 id = ansi_opname (LSHIFT_EXPR);
10753 break;
10755 case CPP_RSHIFT:
10756 id = ansi_opname (RSHIFT_EXPR);
10757 break;
10759 case CPP_LSHIFT_EQ:
10760 id = ansi_assopname (LSHIFT_EXPR);
10761 break;
10763 case CPP_RSHIFT_EQ:
10764 id = ansi_assopname (RSHIFT_EXPR);
10765 break;
10767 case CPP_EQ_EQ:
10768 id = ansi_opname (EQ_EXPR);
10769 break;
10771 case CPP_NOT_EQ:
10772 id = ansi_opname (NE_EXPR);
10773 break;
10775 case CPP_LESS_EQ:
10776 id = ansi_opname (LE_EXPR);
10777 break;
10779 case CPP_GREATER_EQ:
10780 id = ansi_opname (GE_EXPR);
10781 break;
10783 case CPP_AND_AND:
10784 id = ansi_opname (TRUTH_ANDIF_EXPR);
10785 break;
10787 case CPP_OR_OR:
10788 id = ansi_opname (TRUTH_ORIF_EXPR);
10789 break;
10791 case CPP_PLUS_PLUS:
10792 id = ansi_opname (POSTINCREMENT_EXPR);
10793 break;
10795 case CPP_MINUS_MINUS:
10796 id = ansi_opname (PREDECREMENT_EXPR);
10797 break;
10799 case CPP_COMMA:
10800 id = ansi_opname (COMPOUND_EXPR);
10801 break;
10803 case CPP_DEREF_STAR:
10804 id = ansi_opname (MEMBER_REF);
10805 break;
10807 case CPP_DEREF:
10808 id = ansi_opname (COMPONENT_REF);
10809 break;
10811 case CPP_OPEN_PAREN:
10812 /* Consume the `('. */
10813 cp_lexer_consume_token (parser->lexer);
10814 /* Look for the matching `)'. */
10815 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10816 return ansi_opname (CALL_EXPR);
10818 case CPP_OPEN_SQUARE:
10819 /* Consume the `['. */
10820 cp_lexer_consume_token (parser->lexer);
10821 /* Look for the matching `]'. */
10822 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10823 return ansi_opname (ARRAY_REF);
10825 default:
10826 /* Anything else is an error. */
10827 break;
10830 /* If we have selected an identifier, we need to consume the
10831 operator token. */
10832 if (id)
10833 cp_lexer_consume_token (parser->lexer);
10834 /* Otherwise, no valid operator name was present. */
10835 else
10837 cp_parser_error (parser, "expected operator");
10838 id = error_mark_node;
10841 return id;
10844 /* Parse a template-declaration.
10846 template-declaration:
10847 export [opt] template < template-parameter-list > declaration
10849 If MEMBER_P is TRUE, this template-declaration occurs within a
10850 class-specifier.
10852 The grammar rule given by the standard isn't correct. What
10853 is really meant is:
10855 template-declaration:
10856 export [opt] template-parameter-list-seq
10857 decl-specifier-seq [opt] init-declarator [opt] ;
10858 export [opt] template-parameter-list-seq
10859 function-definition
10861 template-parameter-list-seq:
10862 template-parameter-list-seq [opt]
10863 template < template-parameter-list > */
10865 static void
10866 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10868 /* Check for `export'. */
10869 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10871 /* Consume the `export' token. */
10872 cp_lexer_consume_token (parser->lexer);
10873 /* Warn that we do not support `export'. */
10874 warning (0, "keyword %<export%> not implemented, and will be ignored");
10877 cp_parser_template_declaration_after_export (parser, member_p);
10880 /* Parse a template-parameter-list.
10882 template-parameter-list:
10883 template-parameter
10884 template-parameter-list , template-parameter
10886 Returns a TREE_LIST. Each node represents a template parameter.
10887 The nodes are connected via their TREE_CHAINs. */
10889 static tree
10890 cp_parser_template_parameter_list (cp_parser* parser)
10892 tree parameter_list = NULL_TREE;
10894 begin_template_parm_list ();
10896 /* The loop below parses the template parms. We first need to know
10897 the total number of template parms to be able to compute proper
10898 canonical types of each dependent type. So after the loop, when
10899 we know the total number of template parms,
10900 end_template_parm_list computes the proper canonical types and
10901 fixes up the dependent types accordingly. */
10902 while (true)
10904 tree parameter;
10905 bool is_non_type;
10906 bool is_parameter_pack;
10907 location_t parm_loc;
10909 /* Parse the template-parameter. */
10910 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10911 parameter = cp_parser_template_parameter (parser,
10912 &is_non_type,
10913 &is_parameter_pack);
10914 /* Add it to the list. */
10915 if (parameter != error_mark_node)
10916 parameter_list = process_template_parm (parameter_list,
10917 parm_loc,
10918 parameter,
10919 is_non_type,
10920 is_parameter_pack,
10922 else
10924 tree err_parm = build_tree_list (parameter, parameter);
10925 parameter_list = chainon (parameter_list, err_parm);
10928 /* If the next token is not a `,', we're done. */
10929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10930 break;
10931 /* Otherwise, consume the `,' token. */
10932 cp_lexer_consume_token (parser->lexer);
10935 return end_template_parm_list (parameter_list);
10938 /* Parse a template-parameter.
10940 template-parameter:
10941 type-parameter
10942 parameter-declaration
10944 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10945 the parameter. The TREE_PURPOSE is the default value, if any.
10946 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10947 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10948 set to true iff this parameter is a parameter pack. */
10950 static tree
10951 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10952 bool *is_parameter_pack)
10954 cp_token *token;
10955 cp_parameter_declarator *parameter_declarator;
10956 cp_declarator *id_declarator;
10957 tree parm;
10959 /* Assume it is a type parameter or a template parameter. */
10960 *is_non_type = false;
10961 /* Assume it not a parameter pack. */
10962 *is_parameter_pack = false;
10963 /* Peek at the next token. */
10964 token = cp_lexer_peek_token (parser->lexer);
10965 /* If it is `class' or `template', we have a type-parameter. */
10966 if (token->keyword == RID_TEMPLATE)
10967 return cp_parser_type_parameter (parser, is_parameter_pack);
10968 /* If it is `class' or `typename' we do not know yet whether it is a
10969 type parameter or a non-type parameter. Consider:
10971 template <typename T, typename T::X X> ...
10975 template <class C, class D*> ...
10977 Here, the first parameter is a type parameter, and the second is
10978 a non-type parameter. We can tell by looking at the token after
10979 the identifier -- if it is a `,', `=', or `>' then we have a type
10980 parameter. */
10981 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10983 /* Peek at the token after `class' or `typename'. */
10984 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10985 /* If it's an ellipsis, we have a template type parameter
10986 pack. */
10987 if (token->type == CPP_ELLIPSIS)
10988 return cp_parser_type_parameter (parser, is_parameter_pack);
10989 /* If it's an identifier, skip it. */
10990 if (token->type == CPP_NAME)
10991 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10992 /* Now, see if the token looks like the end of a template
10993 parameter. */
10994 if (token->type == CPP_COMMA
10995 || token->type == CPP_EQ
10996 || token->type == CPP_GREATER)
10997 return cp_parser_type_parameter (parser, is_parameter_pack);
11000 /* Otherwise, it is a non-type parameter.
11002 [temp.param]
11004 When parsing a default template-argument for a non-type
11005 template-parameter, the first non-nested `>' is taken as the end
11006 of the template parameter-list rather than a greater-than
11007 operator. */
11008 *is_non_type = true;
11009 parameter_declarator
11010 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11011 /*parenthesized_p=*/NULL);
11013 /* If the parameter declaration is marked as a parameter pack, set
11014 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11015 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11016 grokdeclarator. */
11017 if (parameter_declarator
11018 && parameter_declarator->declarator
11019 && parameter_declarator->declarator->parameter_pack_p)
11021 *is_parameter_pack = true;
11022 parameter_declarator->declarator->parameter_pack_p = false;
11025 /* If the next token is an ellipsis, and we don't already have it
11026 marked as a parameter pack, then we have a parameter pack (that
11027 has no declarator). */
11028 if (!*is_parameter_pack
11029 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11030 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11032 /* Consume the `...'. */
11033 cp_lexer_consume_token (parser->lexer);
11034 maybe_warn_variadic_templates ();
11036 *is_parameter_pack = true;
11038 /* We might end up with a pack expansion as the type of the non-type
11039 template parameter, in which case this is a non-type template
11040 parameter pack. */
11041 else if (parameter_declarator
11042 && parameter_declarator->decl_specifiers.type
11043 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11045 *is_parameter_pack = true;
11046 parameter_declarator->decl_specifiers.type =
11047 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11050 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11052 /* Parameter packs cannot have default arguments. However, a
11053 user may try to do so, so we'll parse them and give an
11054 appropriate diagnostic here. */
11056 /* Consume the `='. */
11057 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11058 cp_lexer_consume_token (parser->lexer);
11060 /* Find the name of the parameter pack. */
11061 id_declarator = parameter_declarator->declarator;
11062 while (id_declarator && id_declarator->kind != cdk_id)
11063 id_declarator = id_declarator->declarator;
11065 if (id_declarator && id_declarator->kind == cdk_id)
11066 error_at (start_token->location,
11067 "template parameter pack %qD cannot have a default argument",
11068 id_declarator->u.id.unqualified_name);
11069 else
11070 error_at (start_token->location,
11071 "template parameter pack cannot have a default argument");
11073 /* Parse the default argument, but throw away the result. */
11074 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11077 parm = grokdeclarator (parameter_declarator->declarator,
11078 &parameter_declarator->decl_specifiers,
11079 TPARM, /*initialized=*/0,
11080 /*attrlist=*/NULL);
11081 if (parm == error_mark_node)
11082 return error_mark_node;
11084 return build_tree_list (parameter_declarator->default_argument, parm);
11087 /* Parse a type-parameter.
11089 type-parameter:
11090 class identifier [opt]
11091 class identifier [opt] = type-id
11092 typename identifier [opt]
11093 typename identifier [opt] = type-id
11094 template < template-parameter-list > class identifier [opt]
11095 template < template-parameter-list > class identifier [opt]
11096 = id-expression
11098 GNU Extension (variadic templates):
11100 type-parameter:
11101 class ... identifier [opt]
11102 typename ... identifier [opt]
11104 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11105 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11106 the declaration of the parameter.
11108 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11110 static tree
11111 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11113 cp_token *token;
11114 tree parameter;
11116 /* Look for a keyword to tell us what kind of parameter this is. */
11117 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11118 if (!token)
11119 return error_mark_node;
11121 switch (token->keyword)
11123 case RID_CLASS:
11124 case RID_TYPENAME:
11126 tree identifier;
11127 tree default_argument;
11129 /* If the next token is an ellipsis, we have a template
11130 argument pack. */
11131 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11133 /* Consume the `...' token. */
11134 cp_lexer_consume_token (parser->lexer);
11135 maybe_warn_variadic_templates ();
11137 *is_parameter_pack = true;
11140 /* If the next token is an identifier, then it names the
11141 parameter. */
11142 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11143 identifier = cp_parser_identifier (parser);
11144 else
11145 identifier = NULL_TREE;
11147 /* Create the parameter. */
11148 parameter = finish_template_type_parm (class_type_node, identifier);
11150 /* If the next token is an `=', we have a default argument. */
11151 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11153 /* Consume the `=' token. */
11154 cp_lexer_consume_token (parser->lexer);
11155 /* Parse the default-argument. */
11156 push_deferring_access_checks (dk_no_deferred);
11157 default_argument = cp_parser_type_id (parser);
11159 /* Template parameter packs cannot have default
11160 arguments. */
11161 if (*is_parameter_pack)
11163 if (identifier)
11164 error_at (token->location,
11165 "template parameter pack %qD cannot have a "
11166 "default argument", identifier);
11167 else
11168 error_at (token->location,
11169 "template parameter packs cannot have "
11170 "default arguments");
11171 default_argument = NULL_TREE;
11173 pop_deferring_access_checks ();
11175 else
11176 default_argument = NULL_TREE;
11178 /* Create the combined representation of the parameter and the
11179 default argument. */
11180 parameter = build_tree_list (default_argument, parameter);
11182 break;
11184 case RID_TEMPLATE:
11186 tree identifier;
11187 tree default_argument;
11189 /* Look for the `<'. */
11190 cp_parser_require (parser, CPP_LESS, RT_LESS);
11191 /* Parse the template-parameter-list. */
11192 cp_parser_template_parameter_list (parser);
11193 /* Look for the `>'. */
11194 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11195 /* Look for the `class' keyword. */
11196 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11197 /* If the next token is an ellipsis, we have a template
11198 argument pack. */
11199 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11201 /* Consume the `...' token. */
11202 cp_lexer_consume_token (parser->lexer);
11203 maybe_warn_variadic_templates ();
11205 *is_parameter_pack = true;
11207 /* If the next token is an `=', then there is a
11208 default-argument. If the next token is a `>', we are at
11209 the end of the parameter-list. If the next token is a `,',
11210 then we are at the end of this parameter. */
11211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11212 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11213 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11215 identifier = cp_parser_identifier (parser);
11216 /* Treat invalid names as if the parameter were nameless. */
11217 if (identifier == error_mark_node)
11218 identifier = NULL_TREE;
11220 else
11221 identifier = NULL_TREE;
11223 /* Create the template parameter. */
11224 parameter = finish_template_template_parm (class_type_node,
11225 identifier);
11227 /* If the next token is an `=', then there is a
11228 default-argument. */
11229 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11231 bool is_template;
11233 /* Consume the `='. */
11234 cp_lexer_consume_token (parser->lexer);
11235 /* Parse the id-expression. */
11236 push_deferring_access_checks (dk_no_deferred);
11237 /* save token before parsing the id-expression, for error
11238 reporting */
11239 token = cp_lexer_peek_token (parser->lexer);
11240 default_argument
11241 = cp_parser_id_expression (parser,
11242 /*template_keyword_p=*/false,
11243 /*check_dependency_p=*/true,
11244 /*template_p=*/&is_template,
11245 /*declarator_p=*/false,
11246 /*optional_p=*/false);
11247 if (TREE_CODE (default_argument) == TYPE_DECL)
11248 /* If the id-expression was a template-id that refers to
11249 a template-class, we already have the declaration here,
11250 so no further lookup is needed. */
11252 else
11253 /* Look up the name. */
11254 default_argument
11255 = cp_parser_lookup_name (parser, default_argument,
11256 none_type,
11257 /*is_template=*/is_template,
11258 /*is_namespace=*/false,
11259 /*check_dependency=*/true,
11260 /*ambiguous_decls=*/NULL,
11261 token->location);
11262 /* See if the default argument is valid. */
11263 default_argument
11264 = check_template_template_default_arg (default_argument);
11266 /* Template parameter packs cannot have default
11267 arguments. */
11268 if (*is_parameter_pack)
11270 if (identifier)
11271 error_at (token->location,
11272 "template parameter pack %qD cannot "
11273 "have a default argument",
11274 identifier);
11275 else
11276 error_at (token->location, "template parameter packs cannot "
11277 "have default arguments");
11278 default_argument = NULL_TREE;
11280 pop_deferring_access_checks ();
11282 else
11283 default_argument = NULL_TREE;
11285 /* Create the combined representation of the parameter and the
11286 default argument. */
11287 parameter = build_tree_list (default_argument, parameter);
11289 break;
11291 default:
11292 gcc_unreachable ();
11293 break;
11296 return parameter;
11299 /* Parse a template-id.
11301 template-id:
11302 template-name < template-argument-list [opt] >
11304 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11305 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11306 returned. Otherwise, if the template-name names a function, or set
11307 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11308 names a class, returns a TYPE_DECL for the specialization.
11310 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11311 uninstantiated templates. */
11313 static tree
11314 cp_parser_template_id (cp_parser *parser,
11315 bool template_keyword_p,
11316 bool check_dependency_p,
11317 bool is_declaration)
11319 int i;
11320 tree templ;
11321 tree arguments;
11322 tree template_id;
11323 cp_token_position start_of_id = 0;
11324 deferred_access_check *chk;
11325 VEC (deferred_access_check,gc) *access_check;
11326 cp_token *next_token = NULL, *next_token_2 = NULL;
11327 bool is_identifier;
11329 /* If the next token corresponds to a template-id, there is no need
11330 to reparse it. */
11331 next_token = cp_lexer_peek_token (parser->lexer);
11332 if (next_token->type == CPP_TEMPLATE_ID)
11334 struct tree_check *check_value;
11336 /* Get the stored value. */
11337 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11338 /* Perform any access checks that were deferred. */
11339 access_check = check_value->checks;
11340 if (access_check)
11342 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11343 perform_or_defer_access_check (chk->binfo,
11344 chk->decl,
11345 chk->diag_decl);
11347 /* Return the stored value. */
11348 return check_value->value;
11351 /* Avoid performing name lookup if there is no possibility of
11352 finding a template-id. */
11353 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11354 || (next_token->type == CPP_NAME
11355 && !cp_parser_nth_token_starts_template_argument_list_p
11356 (parser, 2)))
11358 cp_parser_error (parser, "expected template-id");
11359 return error_mark_node;
11362 /* Remember where the template-id starts. */
11363 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11364 start_of_id = cp_lexer_token_position (parser->lexer, false);
11366 push_deferring_access_checks (dk_deferred);
11368 /* Parse the template-name. */
11369 is_identifier = false;
11370 templ = cp_parser_template_name (parser, template_keyword_p,
11371 check_dependency_p,
11372 is_declaration,
11373 &is_identifier);
11374 if (templ == error_mark_node || is_identifier)
11376 pop_deferring_access_checks ();
11377 return templ;
11380 /* If we find the sequence `[:' after a template-name, it's probably
11381 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11382 parse correctly the argument list. */
11383 next_token = cp_lexer_peek_token (parser->lexer);
11384 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11385 if (next_token->type == CPP_OPEN_SQUARE
11386 && next_token->flags & DIGRAPH
11387 && next_token_2->type == CPP_COLON
11388 && !(next_token_2->flags & PREV_WHITE))
11390 cp_parser_parse_tentatively (parser);
11391 /* Change `:' into `::'. */
11392 next_token_2->type = CPP_SCOPE;
11393 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11394 CPP_LESS. */
11395 cp_lexer_consume_token (parser->lexer);
11397 /* Parse the arguments. */
11398 arguments = cp_parser_enclosed_template_argument_list (parser);
11399 if (!cp_parser_parse_definitely (parser))
11401 /* If we couldn't parse an argument list, then we revert our changes
11402 and return simply an error. Maybe this is not a template-id
11403 after all. */
11404 next_token_2->type = CPP_COLON;
11405 cp_parser_error (parser, "expected %<<%>");
11406 pop_deferring_access_checks ();
11407 return error_mark_node;
11409 /* Otherwise, emit an error about the invalid digraph, but continue
11410 parsing because we got our argument list. */
11411 if (permerror (next_token->location,
11412 "%<<::%> cannot begin a template-argument list"))
11414 static bool hint = false;
11415 inform (next_token->location,
11416 "%<<:%> is an alternate spelling for %<[%>."
11417 " Insert whitespace between %<<%> and %<::%>");
11418 if (!hint && !flag_permissive)
11420 inform (next_token->location, "(if you use %<-fpermissive%>"
11421 " G++ will accept your code)");
11422 hint = true;
11426 else
11428 /* Look for the `<' that starts the template-argument-list. */
11429 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11431 pop_deferring_access_checks ();
11432 return error_mark_node;
11434 /* Parse the arguments. */
11435 arguments = cp_parser_enclosed_template_argument_list (parser);
11438 /* Build a representation of the specialization. */
11439 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11440 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11441 else if (DECL_CLASS_TEMPLATE_P (templ)
11442 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11444 bool entering_scope;
11445 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11446 template (rather than some instantiation thereof) only if
11447 is not nested within some other construct. For example, in
11448 "template <typename T> void f(T) { A<T>::", A<T> is just an
11449 instantiation of A. */
11450 entering_scope = (template_parm_scope_p ()
11451 && cp_lexer_next_token_is (parser->lexer,
11452 CPP_SCOPE));
11453 template_id
11454 = finish_template_type (templ, arguments, entering_scope);
11456 else
11458 /* If it's not a class-template or a template-template, it should be
11459 a function-template. */
11460 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11461 || TREE_CODE (templ) == OVERLOAD
11462 || BASELINK_P (templ)));
11464 template_id = lookup_template_function (templ, arguments);
11467 /* If parsing tentatively, replace the sequence of tokens that makes
11468 up the template-id with a CPP_TEMPLATE_ID token. That way,
11469 should we re-parse the token stream, we will not have to repeat
11470 the effort required to do the parse, nor will we issue duplicate
11471 error messages about problems during instantiation of the
11472 template. */
11473 if (start_of_id)
11475 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11477 /* Reset the contents of the START_OF_ID token. */
11478 token->type = CPP_TEMPLATE_ID;
11479 /* Retrieve any deferred checks. Do not pop this access checks yet
11480 so the memory will not be reclaimed during token replacing below. */
11481 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11482 token->u.tree_check_value->value = template_id;
11483 token->u.tree_check_value->checks = get_deferred_access_checks ();
11484 token->keyword = RID_MAX;
11486 /* Purge all subsequent tokens. */
11487 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11489 /* ??? Can we actually assume that, if template_id ==
11490 error_mark_node, we will have issued a diagnostic to the
11491 user, as opposed to simply marking the tentative parse as
11492 failed? */
11493 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11494 error_at (token->location, "parse error in template argument list");
11497 pop_deferring_access_checks ();
11498 return template_id;
11501 /* Parse a template-name.
11503 template-name:
11504 identifier
11506 The standard should actually say:
11508 template-name:
11509 identifier
11510 operator-function-id
11512 A defect report has been filed about this issue.
11514 A conversion-function-id cannot be a template name because they cannot
11515 be part of a template-id. In fact, looking at this code:
11517 a.operator K<int>()
11519 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11520 It is impossible to call a templated conversion-function-id with an
11521 explicit argument list, since the only allowed template parameter is
11522 the type to which it is converting.
11524 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11525 `template' keyword, in a construction like:
11527 T::template f<3>()
11529 In that case `f' is taken to be a template-name, even though there
11530 is no way of knowing for sure.
11532 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11533 name refers to a set of overloaded functions, at least one of which
11534 is a template, or an IDENTIFIER_NODE with the name of the template,
11535 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11536 names are looked up inside uninstantiated templates. */
11538 static tree
11539 cp_parser_template_name (cp_parser* parser,
11540 bool template_keyword_p,
11541 bool check_dependency_p,
11542 bool is_declaration,
11543 bool *is_identifier)
11545 tree identifier;
11546 tree decl;
11547 tree fns;
11548 cp_token *token = cp_lexer_peek_token (parser->lexer);
11550 /* If the next token is `operator', then we have either an
11551 operator-function-id or a conversion-function-id. */
11552 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11554 /* We don't know whether we're looking at an
11555 operator-function-id or a conversion-function-id. */
11556 cp_parser_parse_tentatively (parser);
11557 /* Try an operator-function-id. */
11558 identifier = cp_parser_operator_function_id (parser);
11559 /* If that didn't work, try a conversion-function-id. */
11560 if (!cp_parser_parse_definitely (parser))
11562 cp_parser_error (parser, "expected template-name");
11563 return error_mark_node;
11566 /* Look for the identifier. */
11567 else
11568 identifier = cp_parser_identifier (parser);
11570 /* If we didn't find an identifier, we don't have a template-id. */
11571 if (identifier == error_mark_node)
11572 return error_mark_node;
11574 /* If the name immediately followed the `template' keyword, then it
11575 is a template-name. However, if the next token is not `<', then
11576 we do not treat it as a template-name, since it is not being used
11577 as part of a template-id. This enables us to handle constructs
11578 like:
11580 template <typename T> struct S { S(); };
11581 template <typename T> S<T>::S();
11583 correctly. We would treat `S' as a template -- if it were `S<T>'
11584 -- but we do not if there is no `<'. */
11586 if (processing_template_decl
11587 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11589 /* In a declaration, in a dependent context, we pretend that the
11590 "template" keyword was present in order to improve error
11591 recovery. For example, given:
11593 template <typename T> void f(T::X<int>);
11595 we want to treat "X<int>" as a template-id. */
11596 if (is_declaration
11597 && !template_keyword_p
11598 && parser->scope && TYPE_P (parser->scope)
11599 && check_dependency_p
11600 && dependent_scope_p (parser->scope)
11601 /* Do not do this for dtors (or ctors), since they never
11602 need the template keyword before their name. */
11603 && !constructor_name_p (identifier, parser->scope))
11605 cp_token_position start = 0;
11607 /* Explain what went wrong. */
11608 error_at (token->location, "non-template %qD used as template",
11609 identifier);
11610 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11611 parser->scope, identifier);
11612 /* If parsing tentatively, find the location of the "<" token. */
11613 if (cp_parser_simulate_error (parser))
11614 start = cp_lexer_token_position (parser->lexer, true);
11615 /* Parse the template arguments so that we can issue error
11616 messages about them. */
11617 cp_lexer_consume_token (parser->lexer);
11618 cp_parser_enclosed_template_argument_list (parser);
11619 /* Skip tokens until we find a good place from which to
11620 continue parsing. */
11621 cp_parser_skip_to_closing_parenthesis (parser,
11622 /*recovering=*/true,
11623 /*or_comma=*/true,
11624 /*consume_paren=*/false);
11625 /* If parsing tentatively, permanently remove the
11626 template argument list. That will prevent duplicate
11627 error messages from being issued about the missing
11628 "template" keyword. */
11629 if (start)
11630 cp_lexer_purge_tokens_after (parser->lexer, start);
11631 if (is_identifier)
11632 *is_identifier = true;
11633 return identifier;
11636 /* If the "template" keyword is present, then there is generally
11637 no point in doing name-lookup, so we just return IDENTIFIER.
11638 But, if the qualifying scope is non-dependent then we can
11639 (and must) do name-lookup normally. */
11640 if (template_keyword_p
11641 && (!parser->scope
11642 || (TYPE_P (parser->scope)
11643 && dependent_type_p (parser->scope))))
11644 return identifier;
11647 /* Look up the name. */
11648 decl = cp_parser_lookup_name (parser, identifier,
11649 none_type,
11650 /*is_template=*/true,
11651 /*is_namespace=*/false,
11652 check_dependency_p,
11653 /*ambiguous_decls=*/NULL,
11654 token->location);
11656 /* If DECL is a template, then the name was a template-name. */
11657 if (TREE_CODE (decl) == TEMPLATE_DECL)
11659 else
11661 tree fn = NULL_TREE;
11663 /* The standard does not explicitly indicate whether a name that
11664 names a set of overloaded declarations, some of which are
11665 templates, is a template-name. However, such a name should
11666 be a template-name; otherwise, there is no way to form a
11667 template-id for the overloaded templates. */
11668 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11669 if (TREE_CODE (fns) == OVERLOAD)
11670 for (fn = fns; fn; fn = OVL_NEXT (fn))
11671 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11672 break;
11674 if (!fn)
11676 /* The name does not name a template. */
11677 cp_parser_error (parser, "expected template-name");
11678 return error_mark_node;
11682 /* If DECL is dependent, and refers to a function, then just return
11683 its name; we will look it up again during template instantiation. */
11684 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11686 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11687 if (TYPE_P (scope) && dependent_type_p (scope))
11688 return identifier;
11691 return decl;
11694 /* Parse a template-argument-list.
11696 template-argument-list:
11697 template-argument ... [opt]
11698 template-argument-list , template-argument ... [opt]
11700 Returns a TREE_VEC containing the arguments. */
11702 static tree
11703 cp_parser_template_argument_list (cp_parser* parser)
11705 tree fixed_args[10];
11706 unsigned n_args = 0;
11707 unsigned alloced = 10;
11708 tree *arg_ary = fixed_args;
11709 tree vec;
11710 bool saved_in_template_argument_list_p;
11711 bool saved_ice_p;
11712 bool saved_non_ice_p;
11714 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11715 parser->in_template_argument_list_p = true;
11716 /* Even if the template-id appears in an integral
11717 constant-expression, the contents of the argument list do
11718 not. */
11719 saved_ice_p = parser->integral_constant_expression_p;
11720 parser->integral_constant_expression_p = false;
11721 saved_non_ice_p = parser->non_integral_constant_expression_p;
11722 parser->non_integral_constant_expression_p = false;
11723 /* Parse the arguments. */
11726 tree argument;
11728 if (n_args)
11729 /* Consume the comma. */
11730 cp_lexer_consume_token (parser->lexer);
11732 /* Parse the template-argument. */
11733 argument = cp_parser_template_argument (parser);
11735 /* If the next token is an ellipsis, we're expanding a template
11736 argument pack. */
11737 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11739 if (argument == error_mark_node)
11741 cp_token *token = cp_lexer_peek_token (parser->lexer);
11742 error_at (token->location,
11743 "expected parameter pack before %<...%>");
11745 /* Consume the `...' token. */
11746 cp_lexer_consume_token (parser->lexer);
11748 /* Make the argument into a TYPE_PACK_EXPANSION or
11749 EXPR_PACK_EXPANSION. */
11750 argument = make_pack_expansion (argument);
11753 if (n_args == alloced)
11755 alloced *= 2;
11757 if (arg_ary == fixed_args)
11759 arg_ary = XNEWVEC (tree, alloced);
11760 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11762 else
11763 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11765 arg_ary[n_args++] = argument;
11767 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11769 vec = make_tree_vec (n_args);
11771 while (n_args--)
11772 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11774 if (arg_ary != fixed_args)
11775 free (arg_ary);
11776 parser->non_integral_constant_expression_p = saved_non_ice_p;
11777 parser->integral_constant_expression_p = saved_ice_p;
11778 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11779 #ifdef ENABLE_CHECKING
11780 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11781 #endif
11782 return vec;
11785 /* Parse a template-argument.
11787 template-argument:
11788 assignment-expression
11789 type-id
11790 id-expression
11792 The representation is that of an assignment-expression, type-id, or
11793 id-expression -- except that the qualified id-expression is
11794 evaluated, so that the value returned is either a DECL or an
11795 OVERLOAD.
11797 Although the standard says "assignment-expression", it forbids
11798 throw-expressions or assignments in the template argument.
11799 Therefore, we use "conditional-expression" instead. */
11801 static tree
11802 cp_parser_template_argument (cp_parser* parser)
11804 tree argument;
11805 bool template_p;
11806 bool address_p;
11807 bool maybe_type_id = false;
11808 cp_token *token = NULL, *argument_start_token = NULL;
11809 cp_id_kind idk;
11811 /* There's really no way to know what we're looking at, so we just
11812 try each alternative in order.
11814 [temp.arg]
11816 In a template-argument, an ambiguity between a type-id and an
11817 expression is resolved to a type-id, regardless of the form of
11818 the corresponding template-parameter.
11820 Therefore, we try a type-id first. */
11821 cp_parser_parse_tentatively (parser);
11822 argument = cp_parser_template_type_arg (parser);
11823 /* If there was no error parsing the type-id but the next token is a
11824 '>>', our behavior depends on which dialect of C++ we're
11825 parsing. In C++98, we probably found a typo for '> >'. But there
11826 are type-id which are also valid expressions. For instance:
11828 struct X { int operator >> (int); };
11829 template <int V> struct Foo {};
11830 Foo<X () >> 5> r;
11832 Here 'X()' is a valid type-id of a function type, but the user just
11833 wanted to write the expression "X() >> 5". Thus, we remember that we
11834 found a valid type-id, but we still try to parse the argument as an
11835 expression to see what happens.
11837 In C++0x, the '>>' will be considered two separate '>'
11838 tokens. */
11839 if (!cp_parser_error_occurred (parser)
11840 && cxx_dialect == cxx98
11841 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11843 maybe_type_id = true;
11844 cp_parser_abort_tentative_parse (parser);
11846 else
11848 /* If the next token isn't a `,' or a `>', then this argument wasn't
11849 really finished. This means that the argument is not a valid
11850 type-id. */
11851 if (!cp_parser_next_token_ends_template_argument_p (parser))
11852 cp_parser_error (parser, "expected template-argument");
11853 /* If that worked, we're done. */
11854 if (cp_parser_parse_definitely (parser))
11855 return argument;
11857 /* We're still not sure what the argument will be. */
11858 cp_parser_parse_tentatively (parser);
11859 /* Try a template. */
11860 argument_start_token = cp_lexer_peek_token (parser->lexer);
11861 argument = cp_parser_id_expression (parser,
11862 /*template_keyword_p=*/false,
11863 /*check_dependency_p=*/true,
11864 &template_p,
11865 /*declarator_p=*/false,
11866 /*optional_p=*/false);
11867 /* If the next token isn't a `,' or a `>', then this argument wasn't
11868 really finished. */
11869 if (!cp_parser_next_token_ends_template_argument_p (parser))
11870 cp_parser_error (parser, "expected template-argument");
11871 if (!cp_parser_error_occurred (parser))
11873 /* Figure out what is being referred to. If the id-expression
11874 was for a class template specialization, then we will have a
11875 TYPE_DECL at this point. There is no need to do name lookup
11876 at this point in that case. */
11877 if (TREE_CODE (argument) != TYPE_DECL)
11878 argument = cp_parser_lookup_name (parser, argument,
11879 none_type,
11880 /*is_template=*/template_p,
11881 /*is_namespace=*/false,
11882 /*check_dependency=*/true,
11883 /*ambiguous_decls=*/NULL,
11884 argument_start_token->location);
11885 if (TREE_CODE (argument) != TEMPLATE_DECL
11886 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11887 cp_parser_error (parser, "expected template-name");
11889 if (cp_parser_parse_definitely (parser))
11890 return argument;
11891 /* It must be a non-type argument. There permitted cases are given
11892 in [temp.arg.nontype]:
11894 -- an integral constant-expression of integral or enumeration
11895 type; or
11897 -- the name of a non-type template-parameter; or
11899 -- the name of an object or function with external linkage...
11901 -- the address of an object or function with external linkage...
11903 -- a pointer to member... */
11904 /* Look for a non-type template parameter. */
11905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11907 cp_parser_parse_tentatively (parser);
11908 argument = cp_parser_primary_expression (parser,
11909 /*address_p=*/false,
11910 /*cast_p=*/false,
11911 /*template_arg_p=*/true,
11912 &idk);
11913 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11914 || !cp_parser_next_token_ends_template_argument_p (parser))
11915 cp_parser_simulate_error (parser);
11916 if (cp_parser_parse_definitely (parser))
11917 return argument;
11920 /* If the next token is "&", the argument must be the address of an
11921 object or function with external linkage. */
11922 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11923 if (address_p)
11924 cp_lexer_consume_token (parser->lexer);
11925 /* See if we might have an id-expression. */
11926 token = cp_lexer_peek_token (parser->lexer);
11927 if (token->type == CPP_NAME
11928 || token->keyword == RID_OPERATOR
11929 || token->type == CPP_SCOPE
11930 || token->type == CPP_TEMPLATE_ID
11931 || token->type == CPP_NESTED_NAME_SPECIFIER)
11933 cp_parser_parse_tentatively (parser);
11934 argument = cp_parser_primary_expression (parser,
11935 address_p,
11936 /*cast_p=*/false,
11937 /*template_arg_p=*/true,
11938 &idk);
11939 if (cp_parser_error_occurred (parser)
11940 || !cp_parser_next_token_ends_template_argument_p (parser))
11941 cp_parser_abort_tentative_parse (parser);
11942 else
11944 tree probe;
11946 if (TREE_CODE (argument) == INDIRECT_REF)
11948 gcc_assert (REFERENCE_REF_P (argument));
11949 argument = TREE_OPERAND (argument, 0);
11952 /* If we're in a template, we represent a qualified-id referring
11953 to a static data member as a SCOPE_REF even if the scope isn't
11954 dependent so that we can check access control later. */
11955 probe = argument;
11956 if (TREE_CODE (probe) == SCOPE_REF)
11957 probe = TREE_OPERAND (probe, 1);
11958 if (TREE_CODE (probe) == VAR_DECL)
11960 /* A variable without external linkage might still be a
11961 valid constant-expression, so no error is issued here
11962 if the external-linkage check fails. */
11963 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11964 cp_parser_simulate_error (parser);
11966 else if (is_overloaded_fn (argument))
11967 /* All overloaded functions are allowed; if the external
11968 linkage test does not pass, an error will be issued
11969 later. */
11971 else if (address_p
11972 && (TREE_CODE (argument) == OFFSET_REF
11973 || TREE_CODE (argument) == SCOPE_REF))
11974 /* A pointer-to-member. */
11976 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11978 else
11979 cp_parser_simulate_error (parser);
11981 if (cp_parser_parse_definitely (parser))
11983 if (address_p)
11984 argument = build_x_unary_op (ADDR_EXPR, argument,
11985 tf_warning_or_error);
11986 return argument;
11990 /* If the argument started with "&", there are no other valid
11991 alternatives at this point. */
11992 if (address_p)
11994 cp_parser_error (parser, "invalid non-type template argument");
11995 return error_mark_node;
11998 /* If the argument wasn't successfully parsed as a type-id followed
11999 by '>>', the argument can only be a constant expression now.
12000 Otherwise, we try parsing the constant-expression tentatively,
12001 because the argument could really be a type-id. */
12002 if (maybe_type_id)
12003 cp_parser_parse_tentatively (parser);
12004 argument = cp_parser_constant_expression (parser,
12005 /*allow_non_constant_p=*/false,
12006 /*non_constant_p=*/NULL);
12007 argument = fold_non_dependent_expr (argument);
12008 if (!maybe_type_id)
12009 return argument;
12010 if (!cp_parser_next_token_ends_template_argument_p (parser))
12011 cp_parser_error (parser, "expected template-argument");
12012 if (cp_parser_parse_definitely (parser))
12013 return argument;
12014 /* We did our best to parse the argument as a non type-id, but that
12015 was the only alternative that matched (albeit with a '>' after
12016 it). We can assume it's just a typo from the user, and a
12017 diagnostic will then be issued. */
12018 return cp_parser_template_type_arg (parser);
12021 /* Parse an explicit-instantiation.
12023 explicit-instantiation:
12024 template declaration
12026 Although the standard says `declaration', what it really means is:
12028 explicit-instantiation:
12029 template decl-specifier-seq [opt] declarator [opt] ;
12031 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12032 supposed to be allowed. A defect report has been filed about this
12033 issue.
12035 GNU Extension:
12037 explicit-instantiation:
12038 storage-class-specifier template
12039 decl-specifier-seq [opt] declarator [opt] ;
12040 function-specifier template
12041 decl-specifier-seq [opt] declarator [opt] ; */
12043 static void
12044 cp_parser_explicit_instantiation (cp_parser* parser)
12046 int declares_class_or_enum;
12047 cp_decl_specifier_seq decl_specifiers;
12048 tree extension_specifier = NULL_TREE;
12050 /* Look for an (optional) storage-class-specifier or
12051 function-specifier. */
12052 if (cp_parser_allow_gnu_extensions_p (parser))
12054 extension_specifier
12055 = cp_parser_storage_class_specifier_opt (parser);
12056 if (!extension_specifier)
12057 extension_specifier
12058 = cp_parser_function_specifier_opt (parser,
12059 /*decl_specs=*/NULL);
12062 /* Look for the `template' keyword. */
12063 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12064 /* Let the front end know that we are processing an explicit
12065 instantiation. */
12066 begin_explicit_instantiation ();
12067 /* [temp.explicit] says that we are supposed to ignore access
12068 control while processing explicit instantiation directives. */
12069 push_deferring_access_checks (dk_no_check);
12070 /* Parse a decl-specifier-seq. */
12071 cp_parser_decl_specifier_seq (parser,
12072 CP_PARSER_FLAGS_OPTIONAL,
12073 &decl_specifiers,
12074 &declares_class_or_enum);
12075 /* If there was exactly one decl-specifier, and it declared a class,
12076 and there's no declarator, then we have an explicit type
12077 instantiation. */
12078 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12080 tree type;
12082 type = check_tag_decl (&decl_specifiers);
12083 /* Turn access control back on for names used during
12084 template instantiation. */
12085 pop_deferring_access_checks ();
12086 if (type)
12087 do_type_instantiation (type, extension_specifier,
12088 /*complain=*/tf_error);
12090 else
12092 cp_declarator *declarator;
12093 tree decl;
12095 /* Parse the declarator. */
12096 declarator
12097 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12098 /*ctor_dtor_or_conv_p=*/NULL,
12099 /*parenthesized_p=*/NULL,
12100 /*member_p=*/false);
12101 if (declares_class_or_enum & 2)
12102 cp_parser_check_for_definition_in_return_type (declarator,
12103 decl_specifiers.type,
12104 decl_specifiers.type_location);
12105 if (declarator != cp_error_declarator)
12107 if (decl_specifiers.specs[(int)ds_inline])
12108 permerror (input_location, "explicit instantiation shall not use"
12109 " %<inline%> specifier");
12110 if (decl_specifiers.specs[(int)ds_constexpr])
12111 permerror (input_location, "explicit instantiation shall not use"
12112 " %<constexpr%> specifier");
12114 decl = grokdeclarator (declarator, &decl_specifiers,
12115 NORMAL, 0, &decl_specifiers.attributes);
12116 /* Turn access control back on for names used during
12117 template instantiation. */
12118 pop_deferring_access_checks ();
12119 /* Do the explicit instantiation. */
12120 do_decl_instantiation (decl, extension_specifier);
12122 else
12124 pop_deferring_access_checks ();
12125 /* Skip the body of the explicit instantiation. */
12126 cp_parser_skip_to_end_of_statement (parser);
12129 /* We're done with the instantiation. */
12130 end_explicit_instantiation ();
12132 cp_parser_consume_semicolon_at_end_of_statement (parser);
12135 /* Parse an explicit-specialization.
12137 explicit-specialization:
12138 template < > declaration
12140 Although the standard says `declaration', what it really means is:
12142 explicit-specialization:
12143 template <> decl-specifier [opt] init-declarator [opt] ;
12144 template <> function-definition
12145 template <> explicit-specialization
12146 template <> template-declaration */
12148 static void
12149 cp_parser_explicit_specialization (cp_parser* parser)
12151 bool need_lang_pop;
12152 cp_token *token = cp_lexer_peek_token (parser->lexer);
12154 /* Look for the `template' keyword. */
12155 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12156 /* Look for the `<'. */
12157 cp_parser_require (parser, CPP_LESS, RT_LESS);
12158 /* Look for the `>'. */
12159 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12160 /* We have processed another parameter list. */
12161 ++parser->num_template_parameter_lists;
12162 /* [temp]
12164 A template ... explicit specialization ... shall not have C
12165 linkage. */
12166 if (current_lang_name == lang_name_c)
12168 error_at (token->location, "template specialization with C linkage");
12169 /* Give it C++ linkage to avoid confusing other parts of the
12170 front end. */
12171 push_lang_context (lang_name_cplusplus);
12172 need_lang_pop = true;
12174 else
12175 need_lang_pop = false;
12176 /* Let the front end know that we are beginning a specialization. */
12177 if (!begin_specialization ())
12179 end_specialization ();
12180 return;
12183 /* If the next keyword is `template', we need to figure out whether
12184 or not we're looking a template-declaration. */
12185 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12187 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12188 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12189 cp_parser_template_declaration_after_export (parser,
12190 /*member_p=*/false);
12191 else
12192 cp_parser_explicit_specialization (parser);
12194 else
12195 /* Parse the dependent declaration. */
12196 cp_parser_single_declaration (parser,
12197 /*checks=*/NULL,
12198 /*member_p=*/false,
12199 /*explicit_specialization_p=*/true,
12200 /*friend_p=*/NULL);
12201 /* We're done with the specialization. */
12202 end_specialization ();
12203 /* For the erroneous case of a template with C linkage, we pushed an
12204 implicit C++ linkage scope; exit that scope now. */
12205 if (need_lang_pop)
12206 pop_lang_context ();
12207 /* We're done with this parameter list. */
12208 --parser->num_template_parameter_lists;
12211 /* Parse a type-specifier.
12213 type-specifier:
12214 simple-type-specifier
12215 class-specifier
12216 enum-specifier
12217 elaborated-type-specifier
12218 cv-qualifier
12220 GNU Extension:
12222 type-specifier:
12223 __complex__
12225 Returns a representation of the type-specifier. For a
12226 class-specifier, enum-specifier, or elaborated-type-specifier, a
12227 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12229 The parser flags FLAGS is used to control type-specifier parsing.
12231 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12232 in a decl-specifier-seq.
12234 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12235 class-specifier, enum-specifier, or elaborated-type-specifier, then
12236 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12237 if a type is declared; 2 if it is defined. Otherwise, it is set to
12238 zero.
12240 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12241 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12242 is set to FALSE. */
12244 static tree
12245 cp_parser_type_specifier (cp_parser* parser,
12246 cp_parser_flags flags,
12247 cp_decl_specifier_seq *decl_specs,
12248 bool is_declaration,
12249 int* declares_class_or_enum,
12250 bool* is_cv_qualifier)
12252 tree type_spec = NULL_TREE;
12253 cp_token *token;
12254 enum rid keyword;
12255 cp_decl_spec ds = ds_last;
12257 /* Assume this type-specifier does not declare a new type. */
12258 if (declares_class_or_enum)
12259 *declares_class_or_enum = 0;
12260 /* And that it does not specify a cv-qualifier. */
12261 if (is_cv_qualifier)
12262 *is_cv_qualifier = false;
12263 /* Peek at the next token. */
12264 token = cp_lexer_peek_token (parser->lexer);
12266 /* If we're looking at a keyword, we can use that to guide the
12267 production we choose. */
12268 keyword = token->keyword;
12269 switch (keyword)
12271 case RID_ENUM:
12272 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12273 goto elaborated_type_specifier;
12275 /* Look for the enum-specifier. */
12276 type_spec = cp_parser_enum_specifier (parser);
12277 /* If that worked, we're done. */
12278 if (type_spec)
12280 if (declares_class_or_enum)
12281 *declares_class_or_enum = 2;
12282 if (decl_specs)
12283 cp_parser_set_decl_spec_type (decl_specs,
12284 type_spec,
12285 token->location,
12286 /*user_defined_p=*/true);
12287 return type_spec;
12289 else
12290 goto elaborated_type_specifier;
12292 /* Any of these indicate either a class-specifier, or an
12293 elaborated-type-specifier. */
12294 case RID_CLASS:
12295 case RID_STRUCT:
12296 case RID_UNION:
12297 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12298 goto elaborated_type_specifier;
12300 /* Parse tentatively so that we can back up if we don't find a
12301 class-specifier. */
12302 cp_parser_parse_tentatively (parser);
12303 /* Look for the class-specifier. */
12304 type_spec = cp_parser_class_specifier (parser);
12305 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12306 /* If that worked, we're done. */
12307 if (cp_parser_parse_definitely (parser))
12309 if (declares_class_or_enum)
12310 *declares_class_or_enum = 2;
12311 if (decl_specs)
12312 cp_parser_set_decl_spec_type (decl_specs,
12313 type_spec,
12314 token->location,
12315 /*user_defined_p=*/true);
12316 return type_spec;
12319 /* Fall through. */
12320 elaborated_type_specifier:
12321 /* We're declaring (not defining) a class or enum. */
12322 if (declares_class_or_enum)
12323 *declares_class_or_enum = 1;
12325 /* Fall through. */
12326 case RID_TYPENAME:
12327 /* Look for an elaborated-type-specifier. */
12328 type_spec
12329 = (cp_parser_elaborated_type_specifier
12330 (parser,
12331 decl_specs && decl_specs->specs[(int) ds_friend],
12332 is_declaration));
12333 if (decl_specs)
12334 cp_parser_set_decl_spec_type (decl_specs,
12335 type_spec,
12336 token->location,
12337 /*user_defined_p=*/true);
12338 return type_spec;
12340 case RID_CONST:
12341 ds = ds_const;
12342 if (is_cv_qualifier)
12343 *is_cv_qualifier = true;
12344 break;
12346 case RID_VOLATILE:
12347 ds = ds_volatile;
12348 if (is_cv_qualifier)
12349 *is_cv_qualifier = true;
12350 break;
12352 case RID_RESTRICT:
12353 ds = ds_restrict;
12354 if (is_cv_qualifier)
12355 *is_cv_qualifier = true;
12356 break;
12358 case RID_COMPLEX:
12359 /* The `__complex__' keyword is a GNU extension. */
12360 ds = ds_complex;
12361 break;
12363 default:
12364 break;
12367 /* Handle simple keywords. */
12368 if (ds != ds_last)
12370 if (decl_specs)
12372 ++decl_specs->specs[(int)ds];
12373 decl_specs->any_specifiers_p = true;
12375 return cp_lexer_consume_token (parser->lexer)->u.value;
12378 /* If we do not already have a type-specifier, assume we are looking
12379 at a simple-type-specifier. */
12380 type_spec = cp_parser_simple_type_specifier (parser,
12381 decl_specs,
12382 flags);
12384 /* If we didn't find a type-specifier, and a type-specifier was not
12385 optional in this context, issue an error message. */
12386 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12388 cp_parser_error (parser, "expected type specifier");
12389 return error_mark_node;
12392 return type_spec;
12395 /* Parse a simple-type-specifier.
12397 simple-type-specifier:
12398 :: [opt] nested-name-specifier [opt] type-name
12399 :: [opt] nested-name-specifier template template-id
12400 char
12401 wchar_t
12402 bool
12403 short
12405 long
12406 signed
12407 unsigned
12408 float
12409 double
12410 void
12412 C++0x Extension:
12414 simple-type-specifier:
12415 auto
12416 decltype ( expression )
12417 char16_t
12418 char32_t
12420 GNU Extension:
12422 simple-type-specifier:
12423 __int128
12424 __typeof__ unary-expression
12425 __typeof__ ( type-id )
12427 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12428 appropriately updated. */
12430 static tree
12431 cp_parser_simple_type_specifier (cp_parser* parser,
12432 cp_decl_specifier_seq *decl_specs,
12433 cp_parser_flags flags)
12435 tree type = NULL_TREE;
12436 cp_token *token;
12438 /* Peek at the next token. */
12439 token = cp_lexer_peek_token (parser->lexer);
12441 /* If we're looking at a keyword, things are easy. */
12442 switch (token->keyword)
12444 case RID_CHAR:
12445 if (decl_specs)
12446 decl_specs->explicit_char_p = true;
12447 type = char_type_node;
12448 break;
12449 case RID_CHAR16:
12450 type = char16_type_node;
12451 break;
12452 case RID_CHAR32:
12453 type = char32_type_node;
12454 break;
12455 case RID_WCHAR:
12456 type = wchar_type_node;
12457 break;
12458 case RID_BOOL:
12459 type = boolean_type_node;
12460 break;
12461 case RID_SHORT:
12462 if (decl_specs)
12463 ++decl_specs->specs[(int) ds_short];
12464 type = short_integer_type_node;
12465 break;
12466 case RID_INT:
12467 if (decl_specs)
12468 decl_specs->explicit_int_p = true;
12469 type = integer_type_node;
12470 break;
12471 case RID_INT128:
12472 if (!int128_integer_type_node)
12473 break;
12474 if (decl_specs)
12475 decl_specs->explicit_int128_p = true;
12476 type = int128_integer_type_node;
12477 break;
12478 case RID_LONG:
12479 if (decl_specs)
12480 ++decl_specs->specs[(int) ds_long];
12481 type = long_integer_type_node;
12482 break;
12483 case RID_SIGNED:
12484 if (decl_specs)
12485 ++decl_specs->specs[(int) ds_signed];
12486 type = integer_type_node;
12487 break;
12488 case RID_UNSIGNED:
12489 if (decl_specs)
12490 ++decl_specs->specs[(int) ds_unsigned];
12491 type = unsigned_type_node;
12492 break;
12493 case RID_FLOAT:
12494 type = float_type_node;
12495 break;
12496 case RID_DOUBLE:
12497 type = double_type_node;
12498 break;
12499 case RID_VOID:
12500 type = void_type_node;
12501 break;
12503 case RID_AUTO:
12504 maybe_warn_cpp0x (CPP0X_AUTO);
12505 type = make_auto ();
12506 break;
12508 case RID_DECLTYPE:
12509 /* Parse the `decltype' type. */
12510 type = cp_parser_decltype (parser);
12512 if (decl_specs)
12513 cp_parser_set_decl_spec_type (decl_specs, type,
12514 token->location,
12515 /*user_defined_p=*/true);
12517 return type;
12519 case RID_TYPEOF:
12520 /* Consume the `typeof' token. */
12521 cp_lexer_consume_token (parser->lexer);
12522 /* Parse the operand to `typeof'. */
12523 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12524 /* If it is not already a TYPE, take its type. */
12525 if (!TYPE_P (type))
12526 type = finish_typeof (type);
12528 if (decl_specs)
12529 cp_parser_set_decl_spec_type (decl_specs, type,
12530 token->location,
12531 /*user_defined_p=*/true);
12533 return type;
12535 default:
12536 break;
12539 /* If the type-specifier was for a built-in type, we're done. */
12540 if (type)
12542 /* Record the type. */
12543 if (decl_specs
12544 && (token->keyword != RID_SIGNED
12545 && token->keyword != RID_UNSIGNED
12546 && token->keyword != RID_SHORT
12547 && token->keyword != RID_LONG))
12548 cp_parser_set_decl_spec_type (decl_specs,
12549 type,
12550 token->location,
12551 /*user_defined=*/false);
12552 if (decl_specs)
12553 decl_specs->any_specifiers_p = true;
12555 /* Consume the token. */
12556 cp_lexer_consume_token (parser->lexer);
12558 /* There is no valid C++ program where a non-template type is
12559 followed by a "<". That usually indicates that the user thought
12560 that the type was a template. */
12561 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12563 return TYPE_NAME (type);
12566 /* The type-specifier must be a user-defined type. */
12567 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12569 bool qualified_p;
12570 bool global_p;
12572 /* Don't gobble tokens or issue error messages if this is an
12573 optional type-specifier. */
12574 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12575 cp_parser_parse_tentatively (parser);
12577 /* Look for the optional `::' operator. */
12578 global_p
12579 = (cp_parser_global_scope_opt (parser,
12580 /*current_scope_valid_p=*/false)
12581 != NULL_TREE);
12582 /* Look for the nested-name specifier. */
12583 qualified_p
12584 = (cp_parser_nested_name_specifier_opt (parser,
12585 /*typename_keyword_p=*/false,
12586 /*check_dependency_p=*/true,
12587 /*type_p=*/false,
12588 /*is_declaration=*/false)
12589 != NULL_TREE);
12590 token = cp_lexer_peek_token (parser->lexer);
12591 /* If we have seen a nested-name-specifier, and the next token
12592 is `template', then we are using the template-id production. */
12593 if (parser->scope
12594 && cp_parser_optional_template_keyword (parser))
12596 /* Look for the template-id. */
12597 type = cp_parser_template_id (parser,
12598 /*template_keyword_p=*/true,
12599 /*check_dependency_p=*/true,
12600 /*is_declaration=*/false);
12601 /* If the template-id did not name a type, we are out of
12602 luck. */
12603 if (TREE_CODE (type) != TYPE_DECL)
12605 cp_parser_error (parser, "expected template-id for type");
12606 type = NULL_TREE;
12609 /* Otherwise, look for a type-name. */
12610 else
12611 type = cp_parser_type_name (parser);
12612 /* Keep track of all name-lookups performed in class scopes. */
12613 if (type
12614 && !global_p
12615 && !qualified_p
12616 && TREE_CODE (type) == TYPE_DECL
12617 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12618 maybe_note_name_used_in_class (DECL_NAME (type), type);
12619 /* If it didn't work out, we don't have a TYPE. */
12620 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12621 && !cp_parser_parse_definitely (parser))
12622 type = NULL_TREE;
12623 if (type && decl_specs)
12624 cp_parser_set_decl_spec_type (decl_specs, type,
12625 token->location,
12626 /*user_defined=*/true);
12629 /* If we didn't get a type-name, issue an error message. */
12630 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12632 cp_parser_error (parser, "expected type-name");
12633 return error_mark_node;
12636 if (type && type != error_mark_node)
12638 /* See if TYPE is an Objective-C type, and if so, parse and
12639 accept any protocol references following it. Do this before
12640 the cp_parser_check_for_invalid_template_id() call, because
12641 Objective-C types can be followed by '<...>' which would
12642 enclose protocol names rather than template arguments, and so
12643 everything is fine. */
12644 if (c_dialect_objc () && !parser->scope
12645 && (objc_is_id (type) || objc_is_class_name (type)))
12647 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12648 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12650 /* Clobber the "unqualified" type previously entered into
12651 DECL_SPECS with the new, improved protocol-qualified version. */
12652 if (decl_specs)
12653 decl_specs->type = qual_type;
12655 return qual_type;
12658 /* There is no valid C++ program where a non-template type is
12659 followed by a "<". That usually indicates that the user
12660 thought that the type was a template. */
12661 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12662 token->location);
12665 return type;
12668 /* Parse a type-name.
12670 type-name:
12671 class-name
12672 enum-name
12673 typedef-name
12675 enum-name:
12676 identifier
12678 typedef-name:
12679 identifier
12681 Returns a TYPE_DECL for the type. */
12683 static tree
12684 cp_parser_type_name (cp_parser* parser)
12686 tree type_decl;
12688 /* We can't know yet whether it is a class-name or not. */
12689 cp_parser_parse_tentatively (parser);
12690 /* Try a class-name. */
12691 type_decl = cp_parser_class_name (parser,
12692 /*typename_keyword_p=*/false,
12693 /*template_keyword_p=*/false,
12694 none_type,
12695 /*check_dependency_p=*/true,
12696 /*class_head_p=*/false,
12697 /*is_declaration=*/false);
12698 /* If it's not a class-name, keep looking. */
12699 if (!cp_parser_parse_definitely (parser))
12701 /* It must be a typedef-name or an enum-name. */
12702 return cp_parser_nonclass_name (parser);
12705 return type_decl;
12708 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12710 enum-name:
12711 identifier
12713 typedef-name:
12714 identifier
12716 Returns a TYPE_DECL for the type. */
12718 static tree
12719 cp_parser_nonclass_name (cp_parser* parser)
12721 tree type_decl;
12722 tree identifier;
12724 cp_token *token = cp_lexer_peek_token (parser->lexer);
12725 identifier = cp_parser_identifier (parser);
12726 if (identifier == error_mark_node)
12727 return error_mark_node;
12729 /* Look up the type-name. */
12730 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12732 if (TREE_CODE (type_decl) != TYPE_DECL
12733 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12735 /* See if this is an Objective-C type. */
12736 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12737 tree type = objc_get_protocol_qualified_type (identifier, protos);
12738 if (type)
12739 type_decl = TYPE_NAME (type);
12742 /* Issue an error if we did not find a type-name. */
12743 if (TREE_CODE (type_decl) != TYPE_DECL
12744 /* In Objective-C, we have the complication that class names are
12745 normally type names and start declarations (eg, the
12746 "NSObject" in "NSObject *object;"), but can be used in an
12747 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12748 is an expression. So, a classname followed by a dot is not a
12749 valid type-name. */
12750 || (objc_is_class_name (TREE_TYPE (type_decl))
12751 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12753 if (!cp_parser_simulate_error (parser))
12754 cp_parser_name_lookup_error (parser, identifier, type_decl,
12755 NLE_TYPE, token->location);
12756 return error_mark_node;
12758 /* Remember that the name was used in the definition of the
12759 current class so that we can check later to see if the
12760 meaning would have been different after the class was
12761 entirely defined. */
12762 else if (type_decl != error_mark_node
12763 && !parser->scope)
12764 maybe_note_name_used_in_class (identifier, type_decl);
12766 return type_decl;
12769 /* Parse an elaborated-type-specifier. Note that the grammar given
12770 here incorporates the resolution to DR68.
12772 elaborated-type-specifier:
12773 class-key :: [opt] nested-name-specifier [opt] identifier
12774 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12775 enum-key :: [opt] nested-name-specifier [opt] identifier
12776 typename :: [opt] nested-name-specifier identifier
12777 typename :: [opt] nested-name-specifier template [opt]
12778 template-id
12780 GNU extension:
12782 elaborated-type-specifier:
12783 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12784 class-key attributes :: [opt] nested-name-specifier [opt]
12785 template [opt] template-id
12786 enum attributes :: [opt] nested-name-specifier [opt] identifier
12788 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12789 declared `friend'. If IS_DECLARATION is TRUE, then this
12790 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12791 something is being declared.
12793 Returns the TYPE specified. */
12795 static tree
12796 cp_parser_elaborated_type_specifier (cp_parser* parser,
12797 bool is_friend,
12798 bool is_declaration)
12800 enum tag_types tag_type;
12801 tree identifier;
12802 tree type = NULL_TREE;
12803 tree attributes = NULL_TREE;
12804 tree globalscope;
12805 cp_token *token = NULL;
12807 /* See if we're looking at the `enum' keyword. */
12808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12810 /* Consume the `enum' token. */
12811 cp_lexer_consume_token (parser->lexer);
12812 /* Remember that it's an enumeration type. */
12813 tag_type = enum_type;
12814 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12815 enums) is used here. */
12816 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12817 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12819 pedwarn (input_location, 0, "elaborated-type-specifier "
12820 "for a scoped enum must not use the %<%D%> keyword",
12821 cp_lexer_peek_token (parser->lexer)->u.value);
12822 /* Consume the `struct' or `class' and parse it anyway. */
12823 cp_lexer_consume_token (parser->lexer);
12825 /* Parse the attributes. */
12826 attributes = cp_parser_attributes_opt (parser);
12828 /* Or, it might be `typename'. */
12829 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12830 RID_TYPENAME))
12832 /* Consume the `typename' token. */
12833 cp_lexer_consume_token (parser->lexer);
12834 /* Remember that it's a `typename' type. */
12835 tag_type = typename_type;
12837 /* Otherwise it must be a class-key. */
12838 else
12840 tag_type = cp_parser_class_key (parser);
12841 if (tag_type == none_type)
12842 return error_mark_node;
12843 /* Parse the attributes. */
12844 attributes = cp_parser_attributes_opt (parser);
12847 /* Look for the `::' operator. */
12848 globalscope = cp_parser_global_scope_opt (parser,
12849 /*current_scope_valid_p=*/false);
12850 /* Look for the nested-name-specifier. */
12851 if (tag_type == typename_type && !globalscope)
12853 if (!cp_parser_nested_name_specifier (parser,
12854 /*typename_keyword_p=*/true,
12855 /*check_dependency_p=*/true,
12856 /*type_p=*/true,
12857 is_declaration))
12858 return error_mark_node;
12860 else
12861 /* Even though `typename' is not present, the proposed resolution
12862 to Core Issue 180 says that in `class A<T>::B', `B' should be
12863 considered a type-name, even if `A<T>' is dependent. */
12864 cp_parser_nested_name_specifier_opt (parser,
12865 /*typename_keyword_p=*/true,
12866 /*check_dependency_p=*/true,
12867 /*type_p=*/true,
12868 is_declaration);
12869 /* For everything but enumeration types, consider a template-id.
12870 For an enumeration type, consider only a plain identifier. */
12871 if (tag_type != enum_type)
12873 bool template_p = false;
12874 tree decl;
12876 /* Allow the `template' keyword. */
12877 template_p = cp_parser_optional_template_keyword (parser);
12878 /* If we didn't see `template', we don't know if there's a
12879 template-id or not. */
12880 if (!template_p)
12881 cp_parser_parse_tentatively (parser);
12882 /* Parse the template-id. */
12883 token = cp_lexer_peek_token (parser->lexer);
12884 decl = cp_parser_template_id (parser, template_p,
12885 /*check_dependency_p=*/true,
12886 is_declaration);
12887 /* If we didn't find a template-id, look for an ordinary
12888 identifier. */
12889 if (!template_p && !cp_parser_parse_definitely (parser))
12891 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12892 in effect, then we must assume that, upon instantiation, the
12893 template will correspond to a class. */
12894 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12895 && tag_type == typename_type)
12896 type = make_typename_type (parser->scope, decl,
12897 typename_type,
12898 /*complain=*/tf_error);
12899 /* If the `typename' keyword is in effect and DECL is not a type
12900 decl. Then type is non existant. */
12901 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12902 type = NULL_TREE;
12903 else
12904 type = TREE_TYPE (decl);
12907 if (!type)
12909 token = cp_lexer_peek_token (parser->lexer);
12910 identifier = cp_parser_identifier (parser);
12912 if (identifier == error_mark_node)
12914 parser->scope = NULL_TREE;
12915 return error_mark_node;
12918 /* For a `typename', we needn't call xref_tag. */
12919 if (tag_type == typename_type
12920 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12921 return cp_parser_make_typename_type (parser, parser->scope,
12922 identifier,
12923 token->location);
12924 /* Look up a qualified name in the usual way. */
12925 if (parser->scope)
12927 tree decl;
12928 tree ambiguous_decls;
12930 decl = cp_parser_lookup_name (parser, identifier,
12931 tag_type,
12932 /*is_template=*/false,
12933 /*is_namespace=*/false,
12934 /*check_dependency=*/true,
12935 &ambiguous_decls,
12936 token->location);
12938 /* If the lookup was ambiguous, an error will already have been
12939 issued. */
12940 if (ambiguous_decls)
12941 return error_mark_node;
12943 /* If we are parsing friend declaration, DECL may be a
12944 TEMPLATE_DECL tree node here. However, we need to check
12945 whether this TEMPLATE_DECL results in valid code. Consider
12946 the following example:
12948 namespace N {
12949 template <class T> class C {};
12951 class X {
12952 template <class T> friend class N::C; // #1, valid code
12954 template <class T> class Y {
12955 friend class N::C; // #2, invalid code
12958 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12959 name lookup of `N::C'. We see that friend declaration must
12960 be template for the code to be valid. Note that
12961 processing_template_decl does not work here since it is
12962 always 1 for the above two cases. */
12964 decl = (cp_parser_maybe_treat_template_as_class
12965 (decl, /*tag_name_p=*/is_friend
12966 && parser->num_template_parameter_lists));
12968 if (TREE_CODE (decl) != TYPE_DECL)
12970 cp_parser_diagnose_invalid_type_name (parser,
12971 parser->scope,
12972 identifier,
12973 token->location);
12974 return error_mark_node;
12977 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12979 bool allow_template = (parser->num_template_parameter_lists
12980 || DECL_SELF_REFERENCE_P (decl));
12981 type = check_elaborated_type_specifier (tag_type, decl,
12982 allow_template);
12984 if (type == error_mark_node)
12985 return error_mark_node;
12988 /* Forward declarations of nested types, such as
12990 class C1::C2;
12991 class C1::C2::C3;
12993 are invalid unless all components preceding the final '::'
12994 are complete. If all enclosing types are complete, these
12995 declarations become merely pointless.
12997 Invalid forward declarations of nested types are errors
12998 caught elsewhere in parsing. Those that are pointless arrive
12999 here. */
13001 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13002 && !is_friend && !processing_explicit_instantiation)
13003 warning (0, "declaration %qD does not declare anything", decl);
13005 type = TREE_TYPE (decl);
13007 else
13009 /* An elaborated-type-specifier sometimes introduces a new type and
13010 sometimes names an existing type. Normally, the rule is that it
13011 introduces a new type only if there is not an existing type of
13012 the same name already in scope. For example, given:
13014 struct S {};
13015 void f() { struct S s; }
13017 the `struct S' in the body of `f' is the same `struct S' as in
13018 the global scope; the existing definition is used. However, if
13019 there were no global declaration, this would introduce a new
13020 local class named `S'.
13022 An exception to this rule applies to the following code:
13024 namespace N { struct S; }
13026 Here, the elaborated-type-specifier names a new type
13027 unconditionally; even if there is already an `S' in the
13028 containing scope this declaration names a new type.
13029 This exception only applies if the elaborated-type-specifier
13030 forms the complete declaration:
13032 [class.name]
13034 A declaration consisting solely of `class-key identifier ;' is
13035 either a redeclaration of the name in the current scope or a
13036 forward declaration of the identifier as a class name. It
13037 introduces the name into the current scope.
13039 We are in this situation precisely when the next token is a `;'.
13041 An exception to the exception is that a `friend' declaration does
13042 *not* name a new type; i.e., given:
13044 struct S { friend struct T; };
13046 `T' is not a new type in the scope of `S'.
13048 Also, `new struct S' or `sizeof (struct S)' never results in the
13049 definition of a new type; a new type can only be declared in a
13050 declaration context. */
13052 tag_scope ts;
13053 bool template_p;
13055 if (is_friend)
13056 /* Friends have special name lookup rules. */
13057 ts = ts_within_enclosing_non_class;
13058 else if (is_declaration
13059 && cp_lexer_next_token_is (parser->lexer,
13060 CPP_SEMICOLON))
13061 /* This is a `class-key identifier ;' */
13062 ts = ts_current;
13063 else
13064 ts = ts_global;
13066 template_p =
13067 (parser->num_template_parameter_lists
13068 && (cp_parser_next_token_starts_class_definition_p (parser)
13069 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13070 /* An unqualified name was used to reference this type, so
13071 there were no qualifying templates. */
13072 if (!cp_parser_check_template_parameters (parser,
13073 /*num_templates=*/0,
13074 token->location,
13075 /*declarator=*/NULL))
13076 return error_mark_node;
13077 type = xref_tag (tag_type, identifier, ts, template_p);
13081 if (type == error_mark_node)
13082 return error_mark_node;
13084 /* Allow attributes on forward declarations of classes. */
13085 if (attributes)
13087 if (TREE_CODE (type) == TYPENAME_TYPE)
13088 warning (OPT_Wattributes,
13089 "attributes ignored on uninstantiated type");
13090 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13091 && ! processing_explicit_instantiation)
13092 warning (OPT_Wattributes,
13093 "attributes ignored on template instantiation");
13094 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13095 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13096 else
13097 warning (OPT_Wattributes,
13098 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13101 if (tag_type != enum_type)
13102 cp_parser_check_class_key (tag_type, type);
13104 /* A "<" cannot follow an elaborated type specifier. If that
13105 happens, the user was probably trying to form a template-id. */
13106 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13108 return type;
13111 /* Parse an enum-specifier.
13113 enum-specifier:
13114 enum-head { enumerator-list [opt] }
13116 enum-head:
13117 enum-key identifier [opt] enum-base [opt]
13118 enum-key nested-name-specifier identifier enum-base [opt]
13120 enum-key:
13121 enum
13122 enum class [C++0x]
13123 enum struct [C++0x]
13125 enum-base: [C++0x]
13126 : type-specifier-seq
13128 opaque-enum-specifier:
13129 enum-key identifier enum-base [opt] ;
13131 GNU Extensions:
13132 enum-key attributes[opt] identifier [opt] enum-base [opt]
13133 { enumerator-list [opt] }attributes[opt]
13135 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13136 if the token stream isn't an enum-specifier after all. */
13138 static tree
13139 cp_parser_enum_specifier (cp_parser* parser)
13141 tree identifier;
13142 tree type = NULL_TREE;
13143 tree prev_scope;
13144 tree nested_name_specifier = NULL_TREE;
13145 tree attributes;
13146 bool scoped_enum_p = false;
13147 bool has_underlying_type = false;
13148 bool nested_being_defined = false;
13149 bool new_value_list = false;
13150 bool is_new_type = false;
13151 bool is_anonymous = false;
13152 tree underlying_type = NULL_TREE;
13153 cp_token *type_start_token = NULL;
13154 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13156 parser->colon_corrects_to_scope_p = false;
13158 /* Parse tentatively so that we can back up if we don't find a
13159 enum-specifier. */
13160 cp_parser_parse_tentatively (parser);
13162 /* Caller guarantees that the current token is 'enum', an identifier
13163 possibly follows, and the token after that is an opening brace.
13164 If we don't have an identifier, fabricate an anonymous name for
13165 the enumeration being defined. */
13166 cp_lexer_consume_token (parser->lexer);
13168 /* Parse the "class" or "struct", which indicates a scoped
13169 enumeration type in C++0x. */
13170 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13171 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13173 if (cxx_dialect < cxx0x)
13174 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13176 /* Consume the `struct' or `class' token. */
13177 cp_lexer_consume_token (parser->lexer);
13179 scoped_enum_p = true;
13182 attributes = cp_parser_attributes_opt (parser);
13184 /* Clear the qualification. */
13185 parser->scope = NULL_TREE;
13186 parser->qualifying_scope = NULL_TREE;
13187 parser->object_scope = NULL_TREE;
13189 /* Figure out in what scope the declaration is being placed. */
13190 prev_scope = current_scope ();
13192 type_start_token = cp_lexer_peek_token (parser->lexer);
13194 push_deferring_access_checks (dk_no_check);
13195 nested_name_specifier
13196 = cp_parser_nested_name_specifier_opt (parser,
13197 /*typename_keyword_p=*/true,
13198 /*check_dependency_p=*/false,
13199 /*type_p=*/false,
13200 /*is_declaration=*/false);
13202 if (nested_name_specifier)
13204 tree name;
13206 identifier = cp_parser_identifier (parser);
13207 name = cp_parser_lookup_name (parser, identifier,
13208 enum_type,
13209 /*is_template=*/false,
13210 /*is_namespace=*/false,
13211 /*check_dependency=*/true,
13212 /*ambiguous_decls=*/NULL,
13213 input_location);
13214 if (name)
13216 type = TREE_TYPE (name);
13217 if (TREE_CODE (type) == TYPENAME_TYPE)
13219 /* Are template enums allowed in ISO? */
13220 if (template_parm_scope_p ())
13221 pedwarn (type_start_token->location, OPT_pedantic,
13222 "%qD is an enumeration template", name);
13223 /* ignore a typename reference, for it will be solved by name
13224 in start_enum. */
13225 type = NULL_TREE;
13228 else
13229 error_at (type_start_token->location,
13230 "%qD is not an enumerator-name", identifier);
13232 else
13234 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13235 identifier = cp_parser_identifier (parser);
13236 else
13238 identifier = make_anon_name ();
13239 is_anonymous = true;
13242 pop_deferring_access_checks ();
13244 /* Check for the `:' that denotes a specified underlying type in C++0x.
13245 Note that a ':' could also indicate a bitfield width, however. */
13246 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13248 cp_decl_specifier_seq type_specifiers;
13250 /* Consume the `:'. */
13251 cp_lexer_consume_token (parser->lexer);
13253 /* Parse the type-specifier-seq. */
13254 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13255 /*is_trailing_return=*/false,
13256 &type_specifiers);
13258 /* At this point this is surely not elaborated type specifier. */
13259 if (!cp_parser_parse_definitely (parser))
13260 return NULL_TREE;
13262 if (cxx_dialect < cxx0x)
13263 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13265 has_underlying_type = true;
13267 /* If that didn't work, stop. */
13268 if (type_specifiers.type != error_mark_node)
13270 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13271 /*initialized=*/0, NULL);
13272 if (underlying_type == error_mark_node)
13273 underlying_type = NULL_TREE;
13277 /* Look for the `{' but don't consume it yet. */
13278 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13280 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13282 cp_parser_error (parser, "expected %<{%>");
13283 if (has_underlying_type)
13285 type = NULL_TREE;
13286 goto out;
13289 /* An opaque-enum-specifier must have a ';' here. */
13290 if ((scoped_enum_p || underlying_type)
13291 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13293 cp_parser_error (parser, "expected %<;%> or %<{%>");
13294 if (has_underlying_type)
13296 type = NULL_TREE;
13297 goto out;
13302 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13303 return NULL_TREE;
13305 if (nested_name_specifier)
13307 if (CLASS_TYPE_P (nested_name_specifier))
13309 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13310 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13311 push_scope (nested_name_specifier);
13313 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13315 push_nested_namespace (nested_name_specifier);
13319 /* Issue an error message if type-definitions are forbidden here. */
13320 if (!cp_parser_check_type_definition (parser))
13321 type = error_mark_node;
13322 else
13323 /* Create the new type. We do this before consuming the opening
13324 brace so the enum will be recorded as being on the line of its
13325 tag (or the 'enum' keyword, if there is no tag). */
13326 type = start_enum (identifier, type, underlying_type,
13327 scoped_enum_p, &is_new_type);
13329 /* If the next token is not '{' it is an opaque-enum-specifier or an
13330 elaborated-type-specifier. */
13331 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13333 if (nested_name_specifier)
13335 /* The following catches invalid code such as:
13336 enum class S<int>::E { A, B, C }; */
13337 if (!processing_specialization
13338 && CLASS_TYPE_P (nested_name_specifier)
13339 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13340 error_at (type_start_token->location, "cannot add an enumerator "
13341 "list to a template instantiation");
13343 /* If that scope does not contain the scope in which the
13344 class was originally declared, the program is invalid. */
13345 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13347 if (at_namespace_scope_p ())
13348 error_at (type_start_token->location,
13349 "declaration of %qD in namespace %qD which does not "
13350 "enclose %qD",
13351 type, prev_scope, nested_name_specifier);
13352 else
13353 error_at (type_start_token->location,
13354 "declaration of %qD in %qD which does not enclose %qD",
13355 type, prev_scope, nested_name_specifier);
13356 type = error_mark_node;
13360 if (scoped_enum_p)
13361 begin_scope (sk_scoped_enum, type);
13363 /* Consume the opening brace. */
13364 cp_lexer_consume_token (parser->lexer);
13366 if (type == error_mark_node)
13367 ; /* Nothing to add */
13368 else if (OPAQUE_ENUM_P (type)
13369 || (cxx_dialect > cxx98 && processing_specialization))
13371 new_value_list = true;
13372 SET_OPAQUE_ENUM_P (type, false);
13373 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13375 else
13377 error_at (type_start_token->location, "multiple definition of %q#T", type);
13378 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13379 "previous definition here");
13380 type = error_mark_node;
13383 if (type == error_mark_node)
13384 cp_parser_skip_to_end_of_block_or_statement (parser);
13385 /* If the next token is not '}', then there are some enumerators. */
13386 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13387 cp_parser_enumerator_list (parser, type);
13389 /* Consume the final '}'. */
13390 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13392 if (scoped_enum_p)
13393 finish_scope ();
13395 else
13397 /* If a ';' follows, then it is an opaque-enum-specifier
13398 and additional restrictions apply. */
13399 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13401 if (is_anonymous)
13402 error_at (type_start_token->location,
13403 "opaque-enum-specifier without name");
13404 else if (nested_name_specifier)
13405 error_at (type_start_token->location,
13406 "opaque-enum-specifier must use a simple identifier");
13410 /* Look for trailing attributes to apply to this enumeration, and
13411 apply them if appropriate. */
13412 if (cp_parser_allow_gnu_extensions_p (parser))
13414 tree trailing_attr = cp_parser_attributes_opt (parser);
13415 trailing_attr = chainon (trailing_attr, attributes);
13416 cplus_decl_attributes (&type,
13417 trailing_attr,
13418 (int) ATTR_FLAG_TYPE_IN_PLACE);
13421 /* Finish up the enumeration. */
13422 if (type != error_mark_node)
13424 if (new_value_list)
13425 finish_enum_value_list (type);
13426 if (is_new_type)
13427 finish_enum (type);
13430 if (nested_name_specifier)
13432 if (CLASS_TYPE_P (nested_name_specifier))
13434 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13435 pop_scope (nested_name_specifier);
13437 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13439 pop_nested_namespace (nested_name_specifier);
13442 out:
13443 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13444 return type;
13447 /* Parse an enumerator-list. The enumerators all have the indicated
13448 TYPE.
13450 enumerator-list:
13451 enumerator-definition
13452 enumerator-list , enumerator-definition */
13454 static void
13455 cp_parser_enumerator_list (cp_parser* parser, tree type)
13457 while (true)
13459 /* Parse an enumerator-definition. */
13460 cp_parser_enumerator_definition (parser, type);
13462 /* If the next token is not a ',', we've reached the end of
13463 the list. */
13464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13465 break;
13466 /* Otherwise, consume the `,' and keep going. */
13467 cp_lexer_consume_token (parser->lexer);
13468 /* If the next token is a `}', there is a trailing comma. */
13469 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13471 if (!in_system_header)
13472 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13473 break;
13478 /* Parse an enumerator-definition. The enumerator has the indicated
13479 TYPE.
13481 enumerator-definition:
13482 enumerator
13483 enumerator = constant-expression
13485 enumerator:
13486 identifier */
13488 static void
13489 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13491 tree identifier;
13492 tree value;
13493 location_t loc;
13495 /* Save the input location because we are interested in the location
13496 of the identifier and not the location of the explicit value. */
13497 loc = cp_lexer_peek_token (parser->lexer)->location;
13499 /* Look for the identifier. */
13500 identifier = cp_parser_identifier (parser);
13501 if (identifier == error_mark_node)
13502 return;
13504 /* If the next token is an '=', then there is an explicit value. */
13505 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13507 /* Consume the `=' token. */
13508 cp_lexer_consume_token (parser->lexer);
13509 /* Parse the value. */
13510 value = cp_parser_constant_expression (parser,
13511 /*allow_non_constant_p=*/false,
13512 NULL);
13514 else
13515 value = NULL_TREE;
13517 /* If we are processing a template, make sure the initializer of the
13518 enumerator doesn't contain any bare template parameter pack. */
13519 if (check_for_bare_parameter_packs (value))
13520 value = error_mark_node;
13522 /* integral_constant_value will pull out this expression, so make sure
13523 it's folded as appropriate. */
13524 value = fold_non_dependent_expr (value);
13526 /* Create the enumerator. */
13527 build_enumerator (identifier, value, type, loc);
13530 /* Parse a namespace-name.
13532 namespace-name:
13533 original-namespace-name
13534 namespace-alias
13536 Returns the NAMESPACE_DECL for the namespace. */
13538 static tree
13539 cp_parser_namespace_name (cp_parser* parser)
13541 tree identifier;
13542 tree namespace_decl;
13544 cp_token *token = cp_lexer_peek_token (parser->lexer);
13546 /* Get the name of the namespace. */
13547 identifier = cp_parser_identifier (parser);
13548 if (identifier == error_mark_node)
13549 return error_mark_node;
13551 /* Look up the identifier in the currently active scope. Look only
13552 for namespaces, due to:
13554 [basic.lookup.udir]
13556 When looking up a namespace-name in a using-directive or alias
13557 definition, only namespace names are considered.
13559 And:
13561 [basic.lookup.qual]
13563 During the lookup of a name preceding the :: scope resolution
13564 operator, object, function, and enumerator names are ignored.
13566 (Note that cp_parser_qualifying_entity only calls this
13567 function if the token after the name is the scope resolution
13568 operator.) */
13569 namespace_decl = cp_parser_lookup_name (parser, identifier,
13570 none_type,
13571 /*is_template=*/false,
13572 /*is_namespace=*/true,
13573 /*check_dependency=*/true,
13574 /*ambiguous_decls=*/NULL,
13575 token->location);
13576 /* If it's not a namespace, issue an error. */
13577 if (namespace_decl == error_mark_node
13578 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13580 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13581 error_at (token->location, "%qD is not a namespace-name", identifier);
13582 cp_parser_error (parser, "expected namespace-name");
13583 namespace_decl = error_mark_node;
13586 return namespace_decl;
13589 /* Parse a namespace-definition.
13591 namespace-definition:
13592 named-namespace-definition
13593 unnamed-namespace-definition
13595 named-namespace-definition:
13596 original-namespace-definition
13597 extension-namespace-definition
13599 original-namespace-definition:
13600 namespace identifier { namespace-body }
13602 extension-namespace-definition:
13603 namespace original-namespace-name { namespace-body }
13605 unnamed-namespace-definition:
13606 namespace { namespace-body } */
13608 static void
13609 cp_parser_namespace_definition (cp_parser* parser)
13611 tree identifier, attribs;
13612 bool has_visibility;
13613 bool is_inline;
13615 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13617 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13618 is_inline = true;
13619 cp_lexer_consume_token (parser->lexer);
13621 else
13622 is_inline = false;
13624 /* Look for the `namespace' keyword. */
13625 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13627 /* Get the name of the namespace. We do not attempt to distinguish
13628 between an original-namespace-definition and an
13629 extension-namespace-definition at this point. The semantic
13630 analysis routines are responsible for that. */
13631 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13632 identifier = cp_parser_identifier (parser);
13633 else
13634 identifier = NULL_TREE;
13636 /* Parse any specified attributes. */
13637 attribs = cp_parser_attributes_opt (parser);
13639 /* Look for the `{' to start the namespace. */
13640 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13641 /* Start the namespace. */
13642 push_namespace (identifier);
13644 /* "inline namespace" is equivalent to a stub namespace definition
13645 followed by a strong using directive. */
13646 if (is_inline)
13648 tree name_space = current_namespace;
13649 /* Set up namespace association. */
13650 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13651 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13652 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13653 /* Import the contents of the inline namespace. */
13654 pop_namespace ();
13655 do_using_directive (name_space);
13656 push_namespace (identifier);
13659 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13661 /* Parse the body of the namespace. */
13662 cp_parser_namespace_body (parser);
13664 if (has_visibility)
13665 pop_visibility (1);
13667 /* Finish the namespace. */
13668 pop_namespace ();
13669 /* Look for the final `}'. */
13670 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13673 /* Parse a namespace-body.
13675 namespace-body:
13676 declaration-seq [opt] */
13678 static void
13679 cp_parser_namespace_body (cp_parser* parser)
13681 cp_parser_declaration_seq_opt (parser);
13684 /* Parse a namespace-alias-definition.
13686 namespace-alias-definition:
13687 namespace identifier = qualified-namespace-specifier ; */
13689 static void
13690 cp_parser_namespace_alias_definition (cp_parser* parser)
13692 tree identifier;
13693 tree namespace_specifier;
13695 cp_token *token = cp_lexer_peek_token (parser->lexer);
13697 /* Look for the `namespace' keyword. */
13698 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13699 /* Look for the identifier. */
13700 identifier = cp_parser_identifier (parser);
13701 if (identifier == error_mark_node)
13702 return;
13703 /* Look for the `=' token. */
13704 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13705 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13707 error_at (token->location, "%<namespace%> definition is not allowed here");
13708 /* Skip the definition. */
13709 cp_lexer_consume_token (parser->lexer);
13710 if (cp_parser_skip_to_closing_brace (parser))
13711 cp_lexer_consume_token (parser->lexer);
13712 return;
13714 cp_parser_require (parser, CPP_EQ, RT_EQ);
13715 /* Look for the qualified-namespace-specifier. */
13716 namespace_specifier
13717 = cp_parser_qualified_namespace_specifier (parser);
13718 /* Look for the `;' token. */
13719 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13721 /* Register the alias in the symbol table. */
13722 do_namespace_alias (identifier, namespace_specifier);
13725 /* Parse a qualified-namespace-specifier.
13727 qualified-namespace-specifier:
13728 :: [opt] nested-name-specifier [opt] namespace-name
13730 Returns a NAMESPACE_DECL corresponding to the specified
13731 namespace. */
13733 static tree
13734 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13736 /* Look for the optional `::'. */
13737 cp_parser_global_scope_opt (parser,
13738 /*current_scope_valid_p=*/false);
13740 /* Look for the optional nested-name-specifier. */
13741 cp_parser_nested_name_specifier_opt (parser,
13742 /*typename_keyword_p=*/false,
13743 /*check_dependency_p=*/true,
13744 /*type_p=*/false,
13745 /*is_declaration=*/true);
13747 return cp_parser_namespace_name (parser);
13750 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13751 access declaration.
13753 using-declaration:
13754 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13755 using :: unqualified-id ;
13757 access-declaration:
13758 qualified-id ;
13762 static bool
13763 cp_parser_using_declaration (cp_parser* parser,
13764 bool access_declaration_p)
13766 cp_token *token;
13767 bool typename_p = false;
13768 bool global_scope_p;
13769 tree decl;
13770 tree identifier;
13771 tree qscope;
13773 if (access_declaration_p)
13774 cp_parser_parse_tentatively (parser);
13775 else
13777 /* Look for the `using' keyword. */
13778 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13780 /* Peek at the next token. */
13781 token = cp_lexer_peek_token (parser->lexer);
13782 /* See if it's `typename'. */
13783 if (token->keyword == RID_TYPENAME)
13785 /* Remember that we've seen it. */
13786 typename_p = true;
13787 /* Consume the `typename' token. */
13788 cp_lexer_consume_token (parser->lexer);
13792 /* Look for the optional global scope qualification. */
13793 global_scope_p
13794 = (cp_parser_global_scope_opt (parser,
13795 /*current_scope_valid_p=*/false)
13796 != NULL_TREE);
13798 /* If we saw `typename', or didn't see `::', then there must be a
13799 nested-name-specifier present. */
13800 if (typename_p || !global_scope_p)
13801 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13802 /*check_dependency_p=*/true,
13803 /*type_p=*/false,
13804 /*is_declaration=*/true);
13805 /* Otherwise, we could be in either of the two productions. In that
13806 case, treat the nested-name-specifier as optional. */
13807 else
13808 qscope = cp_parser_nested_name_specifier_opt (parser,
13809 /*typename_keyword_p=*/false,
13810 /*check_dependency_p=*/true,
13811 /*type_p=*/false,
13812 /*is_declaration=*/true);
13813 if (!qscope)
13814 qscope = global_namespace;
13816 if (access_declaration_p && cp_parser_error_occurred (parser))
13817 /* Something has already gone wrong; there's no need to parse
13818 further. Since an error has occurred, the return value of
13819 cp_parser_parse_definitely will be false, as required. */
13820 return cp_parser_parse_definitely (parser);
13822 token = cp_lexer_peek_token (parser->lexer);
13823 /* Parse the unqualified-id. */
13824 identifier = cp_parser_unqualified_id (parser,
13825 /*template_keyword_p=*/false,
13826 /*check_dependency_p=*/true,
13827 /*declarator_p=*/true,
13828 /*optional_p=*/false);
13830 if (access_declaration_p)
13832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13833 cp_parser_simulate_error (parser);
13834 if (!cp_parser_parse_definitely (parser))
13835 return false;
13838 /* The function we call to handle a using-declaration is different
13839 depending on what scope we are in. */
13840 if (qscope == error_mark_node || identifier == error_mark_node)
13842 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13843 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13844 /* [namespace.udecl]
13846 A using declaration shall not name a template-id. */
13847 error_at (token->location,
13848 "a template-id may not appear in a using-declaration");
13849 else
13851 if (at_class_scope_p ())
13853 /* Create the USING_DECL. */
13854 decl = do_class_using_decl (parser->scope, identifier);
13856 if (check_for_bare_parameter_packs (decl))
13857 return false;
13858 else
13859 /* Add it to the list of members in this class. */
13860 finish_member_declaration (decl);
13862 else
13864 decl = cp_parser_lookup_name_simple (parser,
13865 identifier,
13866 token->location);
13867 if (decl == error_mark_node)
13868 cp_parser_name_lookup_error (parser, identifier,
13869 decl, NLE_NULL,
13870 token->location);
13871 else if (check_for_bare_parameter_packs (decl))
13872 return false;
13873 else if (!at_namespace_scope_p ())
13874 do_local_using_decl (decl, qscope, identifier);
13875 else
13876 do_toplevel_using_decl (decl, qscope, identifier);
13880 /* Look for the final `;'. */
13881 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13883 return true;
13886 /* Parse a using-directive.
13888 using-directive:
13889 using namespace :: [opt] nested-name-specifier [opt]
13890 namespace-name ; */
13892 static void
13893 cp_parser_using_directive (cp_parser* parser)
13895 tree namespace_decl;
13896 tree attribs;
13898 /* Look for the `using' keyword. */
13899 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13900 /* And the `namespace' keyword. */
13901 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13902 /* Look for the optional `::' operator. */
13903 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13904 /* And the optional nested-name-specifier. */
13905 cp_parser_nested_name_specifier_opt (parser,
13906 /*typename_keyword_p=*/false,
13907 /*check_dependency_p=*/true,
13908 /*type_p=*/false,
13909 /*is_declaration=*/true);
13910 /* Get the namespace being used. */
13911 namespace_decl = cp_parser_namespace_name (parser);
13912 /* And any specified attributes. */
13913 attribs = cp_parser_attributes_opt (parser);
13914 /* Update the symbol table. */
13915 parse_using_directive (namespace_decl, attribs);
13916 /* Look for the final `;'. */
13917 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13920 /* Parse an asm-definition.
13922 asm-definition:
13923 asm ( string-literal ) ;
13925 GNU Extension:
13927 asm-definition:
13928 asm volatile [opt] ( string-literal ) ;
13929 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13930 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13931 : asm-operand-list [opt] ) ;
13932 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13933 : asm-operand-list [opt]
13934 : asm-clobber-list [opt] ) ;
13935 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13936 : asm-clobber-list [opt]
13937 : asm-goto-list ) ; */
13939 static void
13940 cp_parser_asm_definition (cp_parser* parser)
13942 tree string;
13943 tree outputs = NULL_TREE;
13944 tree inputs = NULL_TREE;
13945 tree clobbers = NULL_TREE;
13946 tree labels = NULL_TREE;
13947 tree asm_stmt;
13948 bool volatile_p = false;
13949 bool extended_p = false;
13950 bool invalid_inputs_p = false;
13951 bool invalid_outputs_p = false;
13952 bool goto_p = false;
13953 required_token missing = RT_NONE;
13955 /* Look for the `asm' keyword. */
13956 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
13957 /* See if the next token is `volatile'. */
13958 if (cp_parser_allow_gnu_extensions_p (parser)
13959 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13961 /* Remember that we saw the `volatile' keyword. */
13962 volatile_p = true;
13963 /* Consume the token. */
13964 cp_lexer_consume_token (parser->lexer);
13966 if (cp_parser_allow_gnu_extensions_p (parser)
13967 && parser->in_function_body
13968 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13970 /* Remember that we saw the `goto' keyword. */
13971 goto_p = true;
13972 /* Consume the token. */
13973 cp_lexer_consume_token (parser->lexer);
13975 /* Look for the opening `('. */
13976 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13977 return;
13978 /* Look for the string. */
13979 string = cp_parser_string_literal (parser, false, false);
13980 if (string == error_mark_node)
13982 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13983 /*consume_paren=*/true);
13984 return;
13987 /* If we're allowing GNU extensions, check for the extended assembly
13988 syntax. Unfortunately, the `:' tokens need not be separated by
13989 a space in C, and so, for compatibility, we tolerate that here
13990 too. Doing that means that we have to treat the `::' operator as
13991 two `:' tokens. */
13992 if (cp_parser_allow_gnu_extensions_p (parser)
13993 && parser->in_function_body
13994 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13995 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13997 bool inputs_p = false;
13998 bool clobbers_p = false;
13999 bool labels_p = false;
14001 /* The extended syntax was used. */
14002 extended_p = true;
14004 /* Look for outputs. */
14005 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14007 /* Consume the `:'. */
14008 cp_lexer_consume_token (parser->lexer);
14009 /* Parse the output-operands. */
14010 if (cp_lexer_next_token_is_not (parser->lexer,
14011 CPP_COLON)
14012 && cp_lexer_next_token_is_not (parser->lexer,
14013 CPP_SCOPE)
14014 && cp_lexer_next_token_is_not (parser->lexer,
14015 CPP_CLOSE_PAREN)
14016 && !goto_p)
14017 outputs = cp_parser_asm_operand_list (parser);
14019 if (outputs == error_mark_node)
14020 invalid_outputs_p = true;
14022 /* If the next token is `::', there are no outputs, and the
14023 next token is the beginning of the inputs. */
14024 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14025 /* The inputs are coming next. */
14026 inputs_p = true;
14028 /* Look for inputs. */
14029 if (inputs_p
14030 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14032 /* Consume the `:' or `::'. */
14033 cp_lexer_consume_token (parser->lexer);
14034 /* Parse the output-operands. */
14035 if (cp_lexer_next_token_is_not (parser->lexer,
14036 CPP_COLON)
14037 && cp_lexer_next_token_is_not (parser->lexer,
14038 CPP_SCOPE)
14039 && cp_lexer_next_token_is_not (parser->lexer,
14040 CPP_CLOSE_PAREN))
14041 inputs = cp_parser_asm_operand_list (parser);
14043 if (inputs == error_mark_node)
14044 invalid_inputs_p = true;
14046 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14047 /* The clobbers are coming next. */
14048 clobbers_p = true;
14050 /* Look for clobbers. */
14051 if (clobbers_p
14052 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14054 clobbers_p = true;
14055 /* Consume the `:' or `::'. */
14056 cp_lexer_consume_token (parser->lexer);
14057 /* Parse the clobbers. */
14058 if (cp_lexer_next_token_is_not (parser->lexer,
14059 CPP_COLON)
14060 && cp_lexer_next_token_is_not (parser->lexer,
14061 CPP_CLOSE_PAREN))
14062 clobbers = cp_parser_asm_clobber_list (parser);
14064 else if (goto_p
14065 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14066 /* The labels are coming next. */
14067 labels_p = true;
14069 /* Look for labels. */
14070 if (labels_p
14071 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14073 labels_p = true;
14074 /* Consume the `:' or `::'. */
14075 cp_lexer_consume_token (parser->lexer);
14076 /* Parse the labels. */
14077 labels = cp_parser_asm_label_list (parser);
14080 if (goto_p && !labels_p)
14081 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14083 else if (goto_p)
14084 missing = RT_COLON_SCOPE;
14086 /* Look for the closing `)'. */
14087 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14088 missing ? missing : RT_CLOSE_PAREN))
14089 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14090 /*consume_paren=*/true);
14091 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14093 if (!invalid_inputs_p && !invalid_outputs_p)
14095 /* Create the ASM_EXPR. */
14096 if (parser->in_function_body)
14098 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14099 inputs, clobbers, labels);
14100 /* If the extended syntax was not used, mark the ASM_EXPR. */
14101 if (!extended_p)
14103 tree temp = asm_stmt;
14104 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14105 temp = TREE_OPERAND (temp, 0);
14107 ASM_INPUT_P (temp) = 1;
14110 else
14111 cgraph_add_asm_node (string);
14115 /* Declarators [gram.dcl.decl] */
14117 /* Parse an init-declarator.
14119 init-declarator:
14120 declarator initializer [opt]
14122 GNU Extension:
14124 init-declarator:
14125 declarator asm-specification [opt] attributes [opt] initializer [opt]
14127 function-definition:
14128 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14129 function-body
14130 decl-specifier-seq [opt] declarator function-try-block
14132 GNU Extension:
14134 function-definition:
14135 __extension__ function-definition
14137 The DECL_SPECIFIERS apply to this declarator. Returns a
14138 representation of the entity declared. If MEMBER_P is TRUE, then
14139 this declarator appears in a class scope. The new DECL created by
14140 this declarator is returned.
14142 The CHECKS are access checks that should be performed once we know
14143 what entity is being declared (and, therefore, what classes have
14144 befriended it).
14146 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14147 for a function-definition here as well. If the declarator is a
14148 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14149 be TRUE upon return. By that point, the function-definition will
14150 have been completely parsed.
14152 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14153 is FALSE.
14155 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14156 parsed declaration if it is an uninitialized single declarator not followed
14157 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14158 if present, will not be consumed. If returned, this declarator will be
14159 created with SD_INITIALIZED but will not call cp_finish_decl. */
14161 static tree
14162 cp_parser_init_declarator (cp_parser* parser,
14163 cp_decl_specifier_seq *decl_specifiers,
14164 VEC (deferred_access_check,gc)* checks,
14165 bool function_definition_allowed_p,
14166 bool member_p,
14167 int declares_class_or_enum,
14168 bool* function_definition_p,
14169 tree* maybe_range_for_decl)
14171 cp_token *token = NULL, *asm_spec_start_token = NULL,
14172 *attributes_start_token = NULL;
14173 cp_declarator *declarator;
14174 tree prefix_attributes;
14175 tree attributes;
14176 tree asm_specification;
14177 tree initializer;
14178 tree decl = NULL_TREE;
14179 tree scope;
14180 int is_initialized;
14181 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14182 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14183 "(...)". */
14184 enum cpp_ttype initialization_kind;
14185 bool is_direct_init = false;
14186 bool is_non_constant_init;
14187 int ctor_dtor_or_conv_p;
14188 bool friend_p;
14189 tree pushed_scope = NULL;
14190 bool range_for_decl_p = false;
14192 /* Gather the attributes that were provided with the
14193 decl-specifiers. */
14194 prefix_attributes = decl_specifiers->attributes;
14196 /* Assume that this is not the declarator for a function
14197 definition. */
14198 if (function_definition_p)
14199 *function_definition_p = false;
14201 /* Defer access checks while parsing the declarator; we cannot know
14202 what names are accessible until we know what is being
14203 declared. */
14204 resume_deferring_access_checks ();
14206 /* Parse the declarator. */
14207 token = cp_lexer_peek_token (parser->lexer);
14208 declarator
14209 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14210 &ctor_dtor_or_conv_p,
14211 /*parenthesized_p=*/NULL,
14212 /*member_p=*/false);
14213 /* Gather up the deferred checks. */
14214 stop_deferring_access_checks ();
14216 /* If the DECLARATOR was erroneous, there's no need to go
14217 further. */
14218 if (declarator == cp_error_declarator)
14219 return error_mark_node;
14221 /* Check that the number of template-parameter-lists is OK. */
14222 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14223 token->location))
14224 return error_mark_node;
14226 if (declares_class_or_enum & 2)
14227 cp_parser_check_for_definition_in_return_type (declarator,
14228 decl_specifiers->type,
14229 decl_specifiers->type_location);
14231 /* Figure out what scope the entity declared by the DECLARATOR is
14232 located in. `grokdeclarator' sometimes changes the scope, so
14233 we compute it now. */
14234 scope = get_scope_of_declarator (declarator);
14236 /* Perform any lookups in the declared type which were thought to be
14237 dependent, but are not in the scope of the declarator. */
14238 decl_specifiers->type
14239 = maybe_update_decl_type (decl_specifiers->type, scope);
14241 /* If we're allowing GNU extensions, look for an asm-specification
14242 and attributes. */
14243 if (cp_parser_allow_gnu_extensions_p (parser))
14245 /* Look for an asm-specification. */
14246 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14247 asm_specification = cp_parser_asm_specification_opt (parser);
14248 /* And attributes. */
14249 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14250 attributes = cp_parser_attributes_opt (parser);
14252 else
14254 asm_specification = NULL_TREE;
14255 attributes = NULL_TREE;
14258 /* Peek at the next token. */
14259 token = cp_lexer_peek_token (parser->lexer);
14260 /* Check to see if the token indicates the start of a
14261 function-definition. */
14262 if (function_declarator_p (declarator)
14263 && cp_parser_token_starts_function_definition_p (token))
14265 if (!function_definition_allowed_p)
14267 /* If a function-definition should not appear here, issue an
14268 error message. */
14269 cp_parser_error (parser,
14270 "a function-definition is not allowed here");
14271 return error_mark_node;
14273 else
14275 location_t func_brace_location
14276 = cp_lexer_peek_token (parser->lexer)->location;
14278 /* Neither attributes nor an asm-specification are allowed
14279 on a function-definition. */
14280 if (asm_specification)
14281 error_at (asm_spec_start_token->location,
14282 "an asm-specification is not allowed "
14283 "on a function-definition");
14284 if (attributes)
14285 error_at (attributes_start_token->location,
14286 "attributes are not allowed on a function-definition");
14287 /* This is a function-definition. */
14288 *function_definition_p = true;
14290 /* Parse the function definition. */
14291 if (member_p)
14292 decl = cp_parser_save_member_function_body (parser,
14293 decl_specifiers,
14294 declarator,
14295 prefix_attributes);
14296 else
14297 decl
14298 = (cp_parser_function_definition_from_specifiers_and_declarator
14299 (parser, decl_specifiers, prefix_attributes, declarator));
14301 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14303 /* This is where the prologue starts... */
14304 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14305 = func_brace_location;
14308 return decl;
14312 /* [dcl.dcl]
14314 Only in function declarations for constructors, destructors, and
14315 type conversions can the decl-specifier-seq be omitted.
14317 We explicitly postpone this check past the point where we handle
14318 function-definitions because we tolerate function-definitions
14319 that are missing their return types in some modes. */
14320 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14322 cp_parser_error (parser,
14323 "expected constructor, destructor, or type conversion");
14324 return error_mark_node;
14327 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14328 if (token->type == CPP_EQ
14329 || token->type == CPP_OPEN_PAREN
14330 || token->type == CPP_OPEN_BRACE)
14332 is_initialized = SD_INITIALIZED;
14333 initialization_kind = token->type;
14334 if (maybe_range_for_decl)
14335 *maybe_range_for_decl = error_mark_node;
14337 if (token->type == CPP_EQ
14338 && function_declarator_p (declarator))
14340 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14341 if (t2->keyword == RID_DEFAULT)
14342 is_initialized = SD_DEFAULTED;
14343 else if (t2->keyword == RID_DELETE)
14344 is_initialized = SD_DELETED;
14347 else
14349 /* If the init-declarator isn't initialized and isn't followed by a
14350 `,' or `;', it's not a valid init-declarator. */
14351 if (token->type != CPP_COMMA
14352 && token->type != CPP_SEMICOLON)
14354 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14355 range_for_decl_p = true;
14356 else
14358 cp_parser_error (parser, "expected initializer");
14359 return error_mark_node;
14362 is_initialized = SD_UNINITIALIZED;
14363 initialization_kind = CPP_EOF;
14366 /* Because start_decl has side-effects, we should only call it if we
14367 know we're going ahead. By this point, we know that we cannot
14368 possibly be looking at any other construct. */
14369 cp_parser_commit_to_tentative_parse (parser);
14371 /* If the decl specifiers were bad, issue an error now that we're
14372 sure this was intended to be a declarator. Then continue
14373 declaring the variable(s), as int, to try to cut down on further
14374 errors. */
14375 if (decl_specifiers->any_specifiers_p
14376 && decl_specifiers->type == error_mark_node)
14378 cp_parser_error (parser, "invalid type in declaration");
14379 decl_specifiers->type = integer_type_node;
14382 /* Check to see whether or not this declaration is a friend. */
14383 friend_p = cp_parser_friend_p (decl_specifiers);
14385 /* Enter the newly declared entry in the symbol table. If we're
14386 processing a declaration in a class-specifier, we wait until
14387 after processing the initializer. */
14388 if (!member_p)
14390 if (parser->in_unbraced_linkage_specification_p)
14391 decl_specifiers->storage_class = sc_extern;
14392 decl = start_decl (declarator, decl_specifiers,
14393 range_for_decl_p? SD_INITIALIZED : is_initialized,
14394 attributes, prefix_attributes,
14395 &pushed_scope);
14396 /* Adjust location of decl if declarator->id_loc is more appropriate:
14397 set, and decl wasn't merged with another decl, in which case its
14398 location would be different from input_location, and more accurate. */
14399 if (DECL_P (decl)
14400 && declarator->id_loc != UNKNOWN_LOCATION
14401 && DECL_SOURCE_LOCATION (decl) == input_location)
14402 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14404 else if (scope)
14405 /* Enter the SCOPE. That way unqualified names appearing in the
14406 initializer will be looked up in SCOPE. */
14407 pushed_scope = push_scope (scope);
14409 /* Perform deferred access control checks, now that we know in which
14410 SCOPE the declared entity resides. */
14411 if (!member_p && decl)
14413 tree saved_current_function_decl = NULL_TREE;
14415 /* If the entity being declared is a function, pretend that we
14416 are in its scope. If it is a `friend', it may have access to
14417 things that would not otherwise be accessible. */
14418 if (TREE_CODE (decl) == FUNCTION_DECL)
14420 saved_current_function_decl = current_function_decl;
14421 current_function_decl = decl;
14424 /* Perform access checks for template parameters. */
14425 cp_parser_perform_template_parameter_access_checks (checks);
14427 /* Perform the access control checks for the declarator and the
14428 decl-specifiers. */
14429 perform_deferred_access_checks ();
14431 /* Restore the saved value. */
14432 if (TREE_CODE (decl) == FUNCTION_DECL)
14433 current_function_decl = saved_current_function_decl;
14436 /* Parse the initializer. */
14437 initializer = NULL_TREE;
14438 is_direct_init = false;
14439 is_non_constant_init = true;
14440 if (is_initialized)
14442 if (function_declarator_p (declarator))
14444 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14445 if (initialization_kind == CPP_EQ)
14446 initializer = cp_parser_pure_specifier (parser);
14447 else
14449 /* If the declaration was erroneous, we don't really
14450 know what the user intended, so just silently
14451 consume the initializer. */
14452 if (decl != error_mark_node)
14453 error_at (initializer_start_token->location,
14454 "initializer provided for function");
14455 cp_parser_skip_to_closing_parenthesis (parser,
14456 /*recovering=*/true,
14457 /*or_comma=*/false,
14458 /*consume_paren=*/true);
14461 else
14463 /* We want to record the extra mangling scope for in-class
14464 initializers of class members and initializers of static data
14465 member templates. The former is a C++0x feature which isn't
14466 implemented yet, and I expect it will involve deferring
14467 parsing of the initializer until end of class as with default
14468 arguments. So right here we only handle the latter. */
14469 if (!member_p && processing_template_decl)
14470 start_lambda_scope (decl);
14471 initializer = cp_parser_initializer (parser,
14472 &is_direct_init,
14473 &is_non_constant_init);
14474 if (!member_p && processing_template_decl)
14475 finish_lambda_scope ();
14479 /* The old parser allows attributes to appear after a parenthesized
14480 initializer. Mark Mitchell proposed removing this functionality
14481 on the GCC mailing lists on 2002-08-13. This parser accepts the
14482 attributes -- but ignores them. */
14483 if (cp_parser_allow_gnu_extensions_p (parser)
14484 && initialization_kind == CPP_OPEN_PAREN)
14485 if (cp_parser_attributes_opt (parser))
14486 warning (OPT_Wattributes,
14487 "attributes after parenthesized initializer ignored");
14489 /* For an in-class declaration, use `grokfield' to create the
14490 declaration. */
14491 if (member_p)
14493 if (pushed_scope)
14495 pop_scope (pushed_scope);
14496 pushed_scope = false;
14498 decl = grokfield (declarator, decl_specifiers,
14499 initializer, !is_non_constant_init,
14500 /*asmspec=*/NULL_TREE,
14501 prefix_attributes);
14502 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14503 cp_parser_save_default_args (parser, decl);
14506 /* Finish processing the declaration. But, skip member
14507 declarations. */
14508 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14510 cp_finish_decl (decl,
14511 initializer, !is_non_constant_init,
14512 asm_specification,
14513 /* If the initializer is in parentheses, then this is
14514 a direct-initialization, which means that an
14515 `explicit' constructor is OK. Otherwise, an
14516 `explicit' constructor cannot be used. */
14517 ((is_direct_init || !is_initialized)
14518 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14520 else if ((cxx_dialect != cxx98) && friend_p
14521 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14522 /* Core issue #226 (C++0x only): A default template-argument
14523 shall not be specified in a friend class template
14524 declaration. */
14525 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14526 /*is_partial=*/0, /*is_friend_decl=*/1);
14528 if (!friend_p && pushed_scope)
14529 pop_scope (pushed_scope);
14531 return decl;
14534 /* Parse a declarator.
14536 declarator:
14537 direct-declarator
14538 ptr-operator declarator
14540 abstract-declarator:
14541 ptr-operator abstract-declarator [opt]
14542 direct-abstract-declarator
14544 GNU Extensions:
14546 declarator:
14547 attributes [opt] direct-declarator
14548 attributes [opt] ptr-operator declarator
14550 abstract-declarator:
14551 attributes [opt] ptr-operator abstract-declarator [opt]
14552 attributes [opt] direct-abstract-declarator
14554 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14555 detect constructor, destructor or conversion operators. It is set
14556 to -1 if the declarator is a name, and +1 if it is a
14557 function. Otherwise it is set to zero. Usually you just want to
14558 test for >0, but internally the negative value is used.
14560 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14561 a decl-specifier-seq unless it declares a constructor, destructor,
14562 or conversion. It might seem that we could check this condition in
14563 semantic analysis, rather than parsing, but that makes it difficult
14564 to handle something like `f()'. We want to notice that there are
14565 no decl-specifiers, and therefore realize that this is an
14566 expression, not a declaration.)
14568 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14569 the declarator is a direct-declarator of the form "(...)".
14571 MEMBER_P is true iff this declarator is a member-declarator. */
14573 static cp_declarator *
14574 cp_parser_declarator (cp_parser* parser,
14575 cp_parser_declarator_kind dcl_kind,
14576 int* ctor_dtor_or_conv_p,
14577 bool* parenthesized_p,
14578 bool member_p)
14580 cp_declarator *declarator;
14581 enum tree_code code;
14582 cp_cv_quals cv_quals;
14583 tree class_type;
14584 tree attributes = NULL_TREE;
14586 /* Assume this is not a constructor, destructor, or type-conversion
14587 operator. */
14588 if (ctor_dtor_or_conv_p)
14589 *ctor_dtor_or_conv_p = 0;
14591 if (cp_parser_allow_gnu_extensions_p (parser))
14592 attributes = cp_parser_attributes_opt (parser);
14594 /* Check for the ptr-operator production. */
14595 cp_parser_parse_tentatively (parser);
14596 /* Parse the ptr-operator. */
14597 code = cp_parser_ptr_operator (parser,
14598 &class_type,
14599 &cv_quals);
14600 /* If that worked, then we have a ptr-operator. */
14601 if (cp_parser_parse_definitely (parser))
14603 /* If a ptr-operator was found, then this declarator was not
14604 parenthesized. */
14605 if (parenthesized_p)
14606 *parenthesized_p = true;
14607 /* The dependent declarator is optional if we are parsing an
14608 abstract-declarator. */
14609 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14610 cp_parser_parse_tentatively (parser);
14612 /* Parse the dependent declarator. */
14613 declarator = cp_parser_declarator (parser, dcl_kind,
14614 /*ctor_dtor_or_conv_p=*/NULL,
14615 /*parenthesized_p=*/NULL,
14616 /*member_p=*/false);
14618 /* If we are parsing an abstract-declarator, we must handle the
14619 case where the dependent declarator is absent. */
14620 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14621 && !cp_parser_parse_definitely (parser))
14622 declarator = NULL;
14624 declarator = cp_parser_make_indirect_declarator
14625 (code, class_type, cv_quals, declarator);
14627 /* Everything else is a direct-declarator. */
14628 else
14630 if (parenthesized_p)
14631 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14632 CPP_OPEN_PAREN);
14633 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14634 ctor_dtor_or_conv_p,
14635 member_p);
14638 if (attributes && declarator && declarator != cp_error_declarator)
14639 declarator->attributes = attributes;
14641 return declarator;
14644 /* Parse a direct-declarator or direct-abstract-declarator.
14646 direct-declarator:
14647 declarator-id
14648 direct-declarator ( parameter-declaration-clause )
14649 cv-qualifier-seq [opt]
14650 exception-specification [opt]
14651 direct-declarator [ constant-expression [opt] ]
14652 ( declarator )
14654 direct-abstract-declarator:
14655 direct-abstract-declarator [opt]
14656 ( parameter-declaration-clause )
14657 cv-qualifier-seq [opt]
14658 exception-specification [opt]
14659 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14660 ( abstract-declarator )
14662 Returns a representation of the declarator. DCL_KIND is
14663 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14664 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14665 we are parsing a direct-declarator. It is
14666 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14667 of ambiguity we prefer an abstract declarator, as per
14668 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14669 cp_parser_declarator. */
14671 static cp_declarator *
14672 cp_parser_direct_declarator (cp_parser* parser,
14673 cp_parser_declarator_kind dcl_kind,
14674 int* ctor_dtor_or_conv_p,
14675 bool member_p)
14677 cp_token *token;
14678 cp_declarator *declarator = NULL;
14679 tree scope = NULL_TREE;
14680 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14681 bool saved_in_declarator_p = parser->in_declarator_p;
14682 bool first = true;
14683 tree pushed_scope = NULL_TREE;
14685 while (true)
14687 /* Peek at the next token. */
14688 token = cp_lexer_peek_token (parser->lexer);
14689 if (token->type == CPP_OPEN_PAREN)
14691 /* This is either a parameter-declaration-clause, or a
14692 parenthesized declarator. When we know we are parsing a
14693 named declarator, it must be a parenthesized declarator
14694 if FIRST is true. For instance, `(int)' is a
14695 parameter-declaration-clause, with an omitted
14696 direct-abstract-declarator. But `((*))', is a
14697 parenthesized abstract declarator. Finally, when T is a
14698 template parameter `(T)' is a
14699 parameter-declaration-clause, and not a parenthesized
14700 named declarator.
14702 We first try and parse a parameter-declaration-clause,
14703 and then try a nested declarator (if FIRST is true).
14705 It is not an error for it not to be a
14706 parameter-declaration-clause, even when FIRST is
14707 false. Consider,
14709 int i (int);
14710 int i (3);
14712 The first is the declaration of a function while the
14713 second is the definition of a variable, including its
14714 initializer.
14716 Having seen only the parenthesis, we cannot know which of
14717 these two alternatives should be selected. Even more
14718 complex are examples like:
14720 int i (int (a));
14721 int i (int (3));
14723 The former is a function-declaration; the latter is a
14724 variable initialization.
14726 Thus again, we try a parameter-declaration-clause, and if
14727 that fails, we back out and return. */
14729 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14731 tree params;
14732 unsigned saved_num_template_parameter_lists;
14733 bool is_declarator = false;
14734 tree t;
14736 /* In a member-declarator, the only valid interpretation
14737 of a parenthesis is the start of a
14738 parameter-declaration-clause. (It is invalid to
14739 initialize a static data member with a parenthesized
14740 initializer; only the "=" form of initialization is
14741 permitted.) */
14742 if (!member_p)
14743 cp_parser_parse_tentatively (parser);
14745 /* Consume the `('. */
14746 cp_lexer_consume_token (parser->lexer);
14747 if (first)
14749 /* If this is going to be an abstract declarator, we're
14750 in a declarator and we can't have default args. */
14751 parser->default_arg_ok_p = false;
14752 parser->in_declarator_p = true;
14755 /* Inside the function parameter list, surrounding
14756 template-parameter-lists do not apply. */
14757 saved_num_template_parameter_lists
14758 = parser->num_template_parameter_lists;
14759 parser->num_template_parameter_lists = 0;
14761 begin_scope (sk_function_parms, NULL_TREE);
14763 /* Parse the parameter-declaration-clause. */
14764 params = cp_parser_parameter_declaration_clause (parser);
14766 parser->num_template_parameter_lists
14767 = saved_num_template_parameter_lists;
14769 /* If all went well, parse the cv-qualifier-seq and the
14770 exception-specification. */
14771 if (member_p || cp_parser_parse_definitely (parser))
14773 cp_cv_quals cv_quals;
14774 tree exception_specification;
14775 tree late_return;
14777 is_declarator = true;
14779 if (ctor_dtor_or_conv_p)
14780 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14781 first = false;
14782 /* Consume the `)'. */
14783 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14785 /* Parse the cv-qualifier-seq. */
14786 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14787 /* And the exception-specification. */
14788 exception_specification
14789 = cp_parser_exception_specification_opt (parser);
14791 late_return
14792 = cp_parser_late_return_type_opt (parser);
14794 /* Create the function-declarator. */
14795 declarator = make_call_declarator (declarator,
14796 params,
14797 cv_quals,
14798 exception_specification,
14799 late_return);
14800 /* Any subsequent parameter lists are to do with
14801 return type, so are not those of the declared
14802 function. */
14803 parser->default_arg_ok_p = false;
14806 /* Remove the function parms from scope. */
14807 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14808 pop_binding (DECL_NAME (t), t);
14809 leave_scope();
14811 if (is_declarator)
14812 /* Repeat the main loop. */
14813 continue;
14816 /* If this is the first, we can try a parenthesized
14817 declarator. */
14818 if (first)
14820 bool saved_in_type_id_in_expr_p;
14822 parser->default_arg_ok_p = saved_default_arg_ok_p;
14823 parser->in_declarator_p = saved_in_declarator_p;
14825 /* Consume the `('. */
14826 cp_lexer_consume_token (parser->lexer);
14827 /* Parse the nested declarator. */
14828 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14829 parser->in_type_id_in_expr_p = true;
14830 declarator
14831 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14832 /*parenthesized_p=*/NULL,
14833 member_p);
14834 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14835 first = false;
14836 /* Expect a `)'. */
14837 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14838 declarator = cp_error_declarator;
14839 if (declarator == cp_error_declarator)
14840 break;
14842 goto handle_declarator;
14844 /* Otherwise, we must be done. */
14845 else
14846 break;
14848 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14849 && token->type == CPP_OPEN_SQUARE)
14851 /* Parse an array-declarator. */
14852 tree bounds;
14854 if (ctor_dtor_or_conv_p)
14855 *ctor_dtor_or_conv_p = 0;
14857 first = false;
14858 parser->default_arg_ok_p = false;
14859 parser->in_declarator_p = true;
14860 /* Consume the `['. */
14861 cp_lexer_consume_token (parser->lexer);
14862 /* Peek at the next token. */
14863 token = cp_lexer_peek_token (parser->lexer);
14864 /* If the next token is `]', then there is no
14865 constant-expression. */
14866 if (token->type != CPP_CLOSE_SQUARE)
14868 bool non_constant_p;
14870 bounds
14871 = cp_parser_constant_expression (parser,
14872 /*allow_non_constant=*/true,
14873 &non_constant_p);
14874 if (!non_constant_p)
14875 /* OK */;
14876 /* Normally, the array bound must be an integral constant
14877 expression. However, as an extension, we allow VLAs
14878 in function scopes as long as they aren't part of a
14879 parameter declaration. */
14880 else if (!parser->in_function_body
14881 || current_binding_level->kind == sk_function_parms)
14883 cp_parser_error (parser,
14884 "array bound is not an integer constant");
14885 bounds = error_mark_node;
14887 else if (processing_template_decl && !error_operand_p (bounds))
14889 /* Remember this wasn't a constant-expression. */
14890 bounds = build_nop (TREE_TYPE (bounds), bounds);
14891 TREE_SIDE_EFFECTS (bounds) = 1;
14894 else
14895 bounds = NULL_TREE;
14896 /* Look for the closing `]'. */
14897 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14899 declarator = cp_error_declarator;
14900 break;
14903 declarator = make_array_declarator (declarator, bounds);
14905 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14908 tree qualifying_scope;
14909 tree unqualified_name;
14910 special_function_kind sfk;
14911 bool abstract_ok;
14912 bool pack_expansion_p = false;
14913 cp_token *declarator_id_start_token;
14915 /* Parse a declarator-id */
14916 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14917 if (abstract_ok)
14919 cp_parser_parse_tentatively (parser);
14921 /* If we see an ellipsis, we should be looking at a
14922 parameter pack. */
14923 if (token->type == CPP_ELLIPSIS)
14925 /* Consume the `...' */
14926 cp_lexer_consume_token (parser->lexer);
14928 pack_expansion_p = true;
14932 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14933 unqualified_name
14934 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14935 qualifying_scope = parser->scope;
14936 if (abstract_ok)
14938 bool okay = false;
14940 if (!unqualified_name && pack_expansion_p)
14942 /* Check whether an error occurred. */
14943 okay = !cp_parser_error_occurred (parser);
14945 /* We already consumed the ellipsis to mark a
14946 parameter pack, but we have no way to report it,
14947 so abort the tentative parse. We will be exiting
14948 immediately anyway. */
14949 cp_parser_abort_tentative_parse (parser);
14951 else
14952 okay = cp_parser_parse_definitely (parser);
14954 if (!okay)
14955 unqualified_name = error_mark_node;
14956 else if (unqualified_name
14957 && (qualifying_scope
14958 || (TREE_CODE (unqualified_name)
14959 != IDENTIFIER_NODE)))
14961 cp_parser_error (parser, "expected unqualified-id");
14962 unqualified_name = error_mark_node;
14966 if (!unqualified_name)
14967 return NULL;
14968 if (unqualified_name == error_mark_node)
14970 declarator = cp_error_declarator;
14971 pack_expansion_p = false;
14972 declarator->parameter_pack_p = false;
14973 break;
14976 if (qualifying_scope && at_namespace_scope_p ()
14977 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14979 /* In the declaration of a member of a template class
14980 outside of the class itself, the SCOPE will sometimes
14981 be a TYPENAME_TYPE. For example, given:
14983 template <typename T>
14984 int S<T>::R::i = 3;
14986 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14987 this context, we must resolve S<T>::R to an ordinary
14988 type, rather than a typename type.
14990 The reason we normally avoid resolving TYPENAME_TYPEs
14991 is that a specialization of `S' might render
14992 `S<T>::R' not a type. However, if `S' is
14993 specialized, then this `i' will not be used, so there
14994 is no harm in resolving the types here. */
14995 tree type;
14997 /* Resolve the TYPENAME_TYPE. */
14998 type = resolve_typename_type (qualifying_scope,
14999 /*only_current_p=*/false);
15000 /* If that failed, the declarator is invalid. */
15001 if (TREE_CODE (type) == TYPENAME_TYPE)
15003 if (typedef_variant_p (type))
15004 error_at (declarator_id_start_token->location,
15005 "cannot define member of dependent typedef "
15006 "%qT", type);
15007 else
15008 error_at (declarator_id_start_token->location,
15009 "%<%T::%E%> is not a type",
15010 TYPE_CONTEXT (qualifying_scope),
15011 TYPE_IDENTIFIER (qualifying_scope));
15013 qualifying_scope = type;
15016 sfk = sfk_none;
15018 if (unqualified_name)
15020 tree class_type;
15022 if (qualifying_scope
15023 && CLASS_TYPE_P (qualifying_scope))
15024 class_type = qualifying_scope;
15025 else
15026 class_type = current_class_type;
15028 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15030 tree name_type = TREE_TYPE (unqualified_name);
15031 if (class_type && same_type_p (name_type, class_type))
15033 if (qualifying_scope
15034 && CLASSTYPE_USE_TEMPLATE (name_type))
15036 error_at (declarator_id_start_token->location,
15037 "invalid use of constructor as a template");
15038 inform (declarator_id_start_token->location,
15039 "use %<%T::%D%> instead of %<%T::%D%> to "
15040 "name the constructor in a qualified name",
15041 class_type,
15042 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15043 class_type, name_type);
15044 declarator = cp_error_declarator;
15045 break;
15047 else
15048 unqualified_name = constructor_name (class_type);
15050 else
15052 /* We do not attempt to print the declarator
15053 here because we do not have enough
15054 information about its original syntactic
15055 form. */
15056 cp_parser_error (parser, "invalid declarator");
15057 declarator = cp_error_declarator;
15058 break;
15062 if (class_type)
15064 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15065 sfk = sfk_destructor;
15066 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15067 sfk = sfk_conversion;
15068 else if (/* There's no way to declare a constructor
15069 for an anonymous type, even if the type
15070 got a name for linkage purposes. */
15071 !TYPE_WAS_ANONYMOUS (class_type)
15072 && constructor_name_p (unqualified_name,
15073 class_type))
15075 unqualified_name = constructor_name (class_type);
15076 sfk = sfk_constructor;
15078 else if (is_overloaded_fn (unqualified_name)
15079 && DECL_CONSTRUCTOR_P (get_first_fn
15080 (unqualified_name)))
15081 sfk = sfk_constructor;
15083 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15084 *ctor_dtor_or_conv_p = -1;
15087 declarator = make_id_declarator (qualifying_scope,
15088 unqualified_name,
15089 sfk);
15090 declarator->id_loc = token->location;
15091 declarator->parameter_pack_p = pack_expansion_p;
15093 if (pack_expansion_p)
15094 maybe_warn_variadic_templates ();
15097 handle_declarator:;
15098 scope = get_scope_of_declarator (declarator);
15099 if (scope)
15100 /* Any names that appear after the declarator-id for a
15101 member are looked up in the containing scope. */
15102 pushed_scope = push_scope (scope);
15103 parser->in_declarator_p = true;
15104 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15105 || (declarator && declarator->kind == cdk_id))
15106 /* Default args are only allowed on function
15107 declarations. */
15108 parser->default_arg_ok_p = saved_default_arg_ok_p;
15109 else
15110 parser->default_arg_ok_p = false;
15112 first = false;
15114 /* We're done. */
15115 else
15116 break;
15119 /* For an abstract declarator, we might wind up with nothing at this
15120 point. That's an error; the declarator is not optional. */
15121 if (!declarator)
15122 cp_parser_error (parser, "expected declarator");
15124 /* If we entered a scope, we must exit it now. */
15125 if (pushed_scope)
15126 pop_scope (pushed_scope);
15128 parser->default_arg_ok_p = saved_default_arg_ok_p;
15129 parser->in_declarator_p = saved_in_declarator_p;
15131 return declarator;
15134 /* Parse a ptr-operator.
15136 ptr-operator:
15137 * cv-qualifier-seq [opt]
15139 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15141 GNU Extension:
15143 ptr-operator:
15144 & cv-qualifier-seq [opt]
15146 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15147 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15148 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15149 filled in with the TYPE containing the member. *CV_QUALS is
15150 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15151 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15152 Note that the tree codes returned by this function have nothing
15153 to do with the types of trees that will be eventually be created
15154 to represent the pointer or reference type being parsed. They are
15155 just constants with suggestive names. */
15156 static enum tree_code
15157 cp_parser_ptr_operator (cp_parser* parser,
15158 tree* type,
15159 cp_cv_quals *cv_quals)
15161 enum tree_code code = ERROR_MARK;
15162 cp_token *token;
15164 /* Assume that it's not a pointer-to-member. */
15165 *type = NULL_TREE;
15166 /* And that there are no cv-qualifiers. */
15167 *cv_quals = TYPE_UNQUALIFIED;
15169 /* Peek at the next token. */
15170 token = cp_lexer_peek_token (parser->lexer);
15172 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15173 if (token->type == CPP_MULT)
15174 code = INDIRECT_REF;
15175 else if (token->type == CPP_AND)
15176 code = ADDR_EXPR;
15177 else if ((cxx_dialect != cxx98) &&
15178 token->type == CPP_AND_AND) /* C++0x only */
15179 code = NON_LVALUE_EXPR;
15181 if (code != ERROR_MARK)
15183 /* Consume the `*', `&' or `&&'. */
15184 cp_lexer_consume_token (parser->lexer);
15186 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15187 `&', if we are allowing GNU extensions. (The only qualifier
15188 that can legally appear after `&' is `restrict', but that is
15189 enforced during semantic analysis. */
15190 if (code == INDIRECT_REF
15191 || cp_parser_allow_gnu_extensions_p (parser))
15192 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15194 else
15196 /* Try the pointer-to-member case. */
15197 cp_parser_parse_tentatively (parser);
15198 /* Look for the optional `::' operator. */
15199 cp_parser_global_scope_opt (parser,
15200 /*current_scope_valid_p=*/false);
15201 /* Look for the nested-name specifier. */
15202 token = cp_lexer_peek_token (parser->lexer);
15203 cp_parser_nested_name_specifier (parser,
15204 /*typename_keyword_p=*/false,
15205 /*check_dependency_p=*/true,
15206 /*type_p=*/false,
15207 /*is_declaration=*/false);
15208 /* If we found it, and the next token is a `*', then we are
15209 indeed looking at a pointer-to-member operator. */
15210 if (!cp_parser_error_occurred (parser)
15211 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15213 /* Indicate that the `*' operator was used. */
15214 code = INDIRECT_REF;
15216 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15217 error_at (token->location, "%qD is a namespace", parser->scope);
15218 else
15220 /* The type of which the member is a member is given by the
15221 current SCOPE. */
15222 *type = parser->scope;
15223 /* The next name will not be qualified. */
15224 parser->scope = NULL_TREE;
15225 parser->qualifying_scope = NULL_TREE;
15226 parser->object_scope = NULL_TREE;
15227 /* Look for the optional cv-qualifier-seq. */
15228 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15231 /* If that didn't work we don't have a ptr-operator. */
15232 if (!cp_parser_parse_definitely (parser))
15233 cp_parser_error (parser, "expected ptr-operator");
15236 return code;
15239 /* Parse an (optional) cv-qualifier-seq.
15241 cv-qualifier-seq:
15242 cv-qualifier cv-qualifier-seq [opt]
15244 cv-qualifier:
15245 const
15246 volatile
15248 GNU Extension:
15250 cv-qualifier:
15251 __restrict__
15253 Returns a bitmask representing the cv-qualifiers. */
15255 static cp_cv_quals
15256 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15258 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15260 while (true)
15262 cp_token *token;
15263 cp_cv_quals cv_qualifier;
15265 /* Peek at the next token. */
15266 token = cp_lexer_peek_token (parser->lexer);
15267 /* See if it's a cv-qualifier. */
15268 switch (token->keyword)
15270 case RID_CONST:
15271 cv_qualifier = TYPE_QUAL_CONST;
15272 break;
15274 case RID_VOLATILE:
15275 cv_qualifier = TYPE_QUAL_VOLATILE;
15276 break;
15278 case RID_RESTRICT:
15279 cv_qualifier = TYPE_QUAL_RESTRICT;
15280 break;
15282 default:
15283 cv_qualifier = TYPE_UNQUALIFIED;
15284 break;
15287 if (!cv_qualifier)
15288 break;
15290 if (cv_quals & cv_qualifier)
15292 error_at (token->location, "duplicate cv-qualifier");
15293 cp_lexer_purge_token (parser->lexer);
15295 else
15297 cp_lexer_consume_token (parser->lexer);
15298 cv_quals |= cv_qualifier;
15302 return cv_quals;
15305 /* Parse a late-specified return type, if any. This is not a separate
15306 non-terminal, but part of a function declarator, which looks like
15308 -> trailing-type-specifier-seq abstract-declarator(opt)
15310 Returns the type indicated by the type-id. */
15312 static tree
15313 cp_parser_late_return_type_opt (cp_parser* parser)
15315 cp_token *token;
15317 /* Peek at the next token. */
15318 token = cp_lexer_peek_token (parser->lexer);
15319 /* A late-specified return type is indicated by an initial '->'. */
15320 if (token->type != CPP_DEREF)
15321 return NULL_TREE;
15323 /* Consume the ->. */
15324 cp_lexer_consume_token (parser->lexer);
15326 return cp_parser_trailing_type_id (parser);
15329 /* Parse a declarator-id.
15331 declarator-id:
15332 id-expression
15333 :: [opt] nested-name-specifier [opt] type-name
15335 In the `id-expression' case, the value returned is as for
15336 cp_parser_id_expression if the id-expression was an unqualified-id.
15337 If the id-expression was a qualified-id, then a SCOPE_REF is
15338 returned. The first operand is the scope (either a NAMESPACE_DECL
15339 or TREE_TYPE), but the second is still just a representation of an
15340 unqualified-id. */
15342 static tree
15343 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15345 tree id;
15346 /* The expression must be an id-expression. Assume that qualified
15347 names are the names of types so that:
15349 template <class T>
15350 int S<T>::R::i = 3;
15352 will work; we must treat `S<T>::R' as the name of a type.
15353 Similarly, assume that qualified names are templates, where
15354 required, so that:
15356 template <class T>
15357 int S<T>::R<T>::i = 3;
15359 will work, too. */
15360 id = cp_parser_id_expression (parser,
15361 /*template_keyword_p=*/false,
15362 /*check_dependency_p=*/false,
15363 /*template_p=*/NULL,
15364 /*declarator_p=*/true,
15365 optional_p);
15366 if (id && BASELINK_P (id))
15367 id = BASELINK_FUNCTIONS (id);
15368 return id;
15371 /* Parse a type-id.
15373 type-id:
15374 type-specifier-seq abstract-declarator [opt]
15376 Returns the TYPE specified. */
15378 static tree
15379 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15380 bool is_trailing_return)
15382 cp_decl_specifier_seq type_specifier_seq;
15383 cp_declarator *abstract_declarator;
15385 /* Parse the type-specifier-seq. */
15386 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15387 is_trailing_return,
15388 &type_specifier_seq);
15389 if (type_specifier_seq.type == error_mark_node)
15390 return error_mark_node;
15392 /* There might or might not be an abstract declarator. */
15393 cp_parser_parse_tentatively (parser);
15394 /* Look for the declarator. */
15395 abstract_declarator
15396 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15397 /*parenthesized_p=*/NULL,
15398 /*member_p=*/false);
15399 /* Check to see if there really was a declarator. */
15400 if (!cp_parser_parse_definitely (parser))
15401 abstract_declarator = NULL;
15403 if (type_specifier_seq.type
15404 && type_uses_auto (type_specifier_seq.type))
15406 /* A type-id with type 'auto' is only ok if the abstract declarator
15407 is a function declarator with a late-specified return type. */
15408 if (abstract_declarator
15409 && abstract_declarator->kind == cdk_function
15410 && abstract_declarator->u.function.late_return_type)
15411 /* OK */;
15412 else
15414 error ("invalid use of %<auto%>");
15415 return error_mark_node;
15419 return groktypename (&type_specifier_seq, abstract_declarator,
15420 is_template_arg);
15423 static tree cp_parser_type_id (cp_parser *parser)
15425 return cp_parser_type_id_1 (parser, false, false);
15428 static tree cp_parser_template_type_arg (cp_parser *parser)
15430 tree r;
15431 const char *saved_message = parser->type_definition_forbidden_message;
15432 parser->type_definition_forbidden_message
15433 = G_("types may not be defined in template arguments");
15434 r = cp_parser_type_id_1 (parser, true, false);
15435 parser->type_definition_forbidden_message = saved_message;
15436 return r;
15439 static tree cp_parser_trailing_type_id (cp_parser *parser)
15441 return cp_parser_type_id_1 (parser, false, true);
15444 /* Parse a type-specifier-seq.
15446 type-specifier-seq:
15447 type-specifier type-specifier-seq [opt]
15449 GNU extension:
15451 type-specifier-seq:
15452 attributes type-specifier-seq [opt]
15454 If IS_DECLARATION is true, we are at the start of a "condition" or
15455 exception-declaration, so we might be followed by a declarator-id.
15457 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15458 i.e. we've just seen "->".
15460 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15462 static void
15463 cp_parser_type_specifier_seq (cp_parser* parser,
15464 bool is_declaration,
15465 bool is_trailing_return,
15466 cp_decl_specifier_seq *type_specifier_seq)
15468 bool seen_type_specifier = false;
15469 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15470 cp_token *start_token = NULL;
15472 /* Clear the TYPE_SPECIFIER_SEQ. */
15473 clear_decl_specs (type_specifier_seq);
15475 /* In the context of a trailing return type, enum E { } is an
15476 elaborated-type-specifier followed by a function-body, not an
15477 enum-specifier. */
15478 if (is_trailing_return)
15479 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15481 /* Parse the type-specifiers and attributes. */
15482 while (true)
15484 tree type_specifier;
15485 bool is_cv_qualifier;
15487 /* Check for attributes first. */
15488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15490 type_specifier_seq->attributes =
15491 chainon (type_specifier_seq->attributes,
15492 cp_parser_attributes_opt (parser));
15493 continue;
15496 /* record the token of the beginning of the type specifier seq,
15497 for error reporting purposes*/
15498 if (!start_token)
15499 start_token = cp_lexer_peek_token (parser->lexer);
15501 /* Look for the type-specifier. */
15502 type_specifier = cp_parser_type_specifier (parser,
15503 flags,
15504 type_specifier_seq,
15505 /*is_declaration=*/false,
15506 NULL,
15507 &is_cv_qualifier);
15508 if (!type_specifier)
15510 /* If the first type-specifier could not be found, this is not a
15511 type-specifier-seq at all. */
15512 if (!seen_type_specifier)
15514 cp_parser_error (parser, "expected type-specifier");
15515 type_specifier_seq->type = error_mark_node;
15516 return;
15518 /* If subsequent type-specifiers could not be found, the
15519 type-specifier-seq is complete. */
15520 break;
15523 seen_type_specifier = true;
15524 /* The standard says that a condition can be:
15526 type-specifier-seq declarator = assignment-expression
15528 However, given:
15530 struct S {};
15531 if (int S = ...)
15533 we should treat the "S" as a declarator, not as a
15534 type-specifier. The standard doesn't say that explicitly for
15535 type-specifier-seq, but it does say that for
15536 decl-specifier-seq in an ordinary declaration. Perhaps it
15537 would be clearer just to allow a decl-specifier-seq here, and
15538 then add a semantic restriction that if any decl-specifiers
15539 that are not type-specifiers appear, the program is invalid. */
15540 if (is_declaration && !is_cv_qualifier)
15541 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15544 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15547 /* Parse a parameter-declaration-clause.
15549 parameter-declaration-clause:
15550 parameter-declaration-list [opt] ... [opt]
15551 parameter-declaration-list , ...
15553 Returns a representation for the parameter declarations. A return
15554 value of NULL indicates a parameter-declaration-clause consisting
15555 only of an ellipsis. */
15557 static tree
15558 cp_parser_parameter_declaration_clause (cp_parser* parser)
15560 tree parameters;
15561 cp_token *token;
15562 bool ellipsis_p;
15563 bool is_error;
15565 /* Peek at the next token. */
15566 token = cp_lexer_peek_token (parser->lexer);
15567 /* Check for trivial parameter-declaration-clauses. */
15568 if (token->type == CPP_ELLIPSIS)
15570 /* Consume the `...' token. */
15571 cp_lexer_consume_token (parser->lexer);
15572 return NULL_TREE;
15574 else if (token->type == CPP_CLOSE_PAREN)
15575 /* There are no parameters. */
15577 #ifndef NO_IMPLICIT_EXTERN_C
15578 if (in_system_header && current_class_type == NULL
15579 && current_lang_name == lang_name_c)
15580 return NULL_TREE;
15581 else
15582 #endif
15583 return void_list_node;
15585 /* Check for `(void)', too, which is a special case. */
15586 else if (token->keyword == RID_VOID
15587 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15588 == CPP_CLOSE_PAREN))
15590 /* Consume the `void' token. */
15591 cp_lexer_consume_token (parser->lexer);
15592 /* There are no parameters. */
15593 return void_list_node;
15596 /* Parse the parameter-declaration-list. */
15597 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15598 /* If a parse error occurred while parsing the
15599 parameter-declaration-list, then the entire
15600 parameter-declaration-clause is erroneous. */
15601 if (is_error)
15602 return NULL;
15604 /* Peek at the next token. */
15605 token = cp_lexer_peek_token (parser->lexer);
15606 /* If it's a `,', the clause should terminate with an ellipsis. */
15607 if (token->type == CPP_COMMA)
15609 /* Consume the `,'. */
15610 cp_lexer_consume_token (parser->lexer);
15611 /* Expect an ellipsis. */
15612 ellipsis_p
15613 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15615 /* It might also be `...' if the optional trailing `,' was
15616 omitted. */
15617 else if (token->type == CPP_ELLIPSIS)
15619 /* Consume the `...' token. */
15620 cp_lexer_consume_token (parser->lexer);
15621 /* And remember that we saw it. */
15622 ellipsis_p = true;
15624 else
15625 ellipsis_p = false;
15627 /* Finish the parameter list. */
15628 if (!ellipsis_p)
15629 parameters = chainon (parameters, void_list_node);
15631 return parameters;
15634 /* Parse a parameter-declaration-list.
15636 parameter-declaration-list:
15637 parameter-declaration
15638 parameter-declaration-list , parameter-declaration
15640 Returns a representation of the parameter-declaration-list, as for
15641 cp_parser_parameter_declaration_clause. However, the
15642 `void_list_node' is never appended to the list. Upon return,
15643 *IS_ERROR will be true iff an error occurred. */
15645 static tree
15646 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15648 tree parameters = NULL_TREE;
15649 tree *tail = &parameters;
15650 bool saved_in_unbraced_linkage_specification_p;
15651 int index = 0;
15653 /* Assume all will go well. */
15654 *is_error = false;
15655 /* The special considerations that apply to a function within an
15656 unbraced linkage specifications do not apply to the parameters
15657 to the function. */
15658 saved_in_unbraced_linkage_specification_p
15659 = parser->in_unbraced_linkage_specification_p;
15660 parser->in_unbraced_linkage_specification_p = false;
15662 /* Look for more parameters. */
15663 while (true)
15665 cp_parameter_declarator *parameter;
15666 tree decl = error_mark_node;
15667 bool parenthesized_p;
15668 /* Parse the parameter. */
15669 parameter
15670 = cp_parser_parameter_declaration (parser,
15671 /*template_parm_p=*/false,
15672 &parenthesized_p);
15674 /* We don't know yet if the enclosing context is deprecated, so wait
15675 and warn in grokparms if appropriate. */
15676 deprecated_state = DEPRECATED_SUPPRESS;
15678 if (parameter)
15679 decl = grokdeclarator (parameter->declarator,
15680 &parameter->decl_specifiers,
15681 PARM,
15682 parameter->default_argument != NULL_TREE,
15683 &parameter->decl_specifiers.attributes);
15685 deprecated_state = DEPRECATED_NORMAL;
15687 /* If a parse error occurred parsing the parameter declaration,
15688 then the entire parameter-declaration-list is erroneous. */
15689 if (decl == error_mark_node)
15691 *is_error = true;
15692 parameters = error_mark_node;
15693 break;
15696 if (parameter->decl_specifiers.attributes)
15697 cplus_decl_attributes (&decl,
15698 parameter->decl_specifiers.attributes,
15700 if (DECL_NAME (decl))
15701 decl = pushdecl (decl);
15703 if (decl != error_mark_node)
15705 retrofit_lang_decl (decl);
15706 DECL_PARM_INDEX (decl) = ++index;
15707 DECL_PARM_LEVEL (decl) = function_parm_depth ();
15710 /* Add the new parameter to the list. */
15711 *tail = build_tree_list (parameter->default_argument, decl);
15712 tail = &TREE_CHAIN (*tail);
15714 /* Peek at the next token. */
15715 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15716 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15717 /* These are for Objective-C++ */
15718 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15719 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15720 /* The parameter-declaration-list is complete. */
15721 break;
15722 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15724 cp_token *token;
15726 /* Peek at the next token. */
15727 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15728 /* If it's an ellipsis, then the list is complete. */
15729 if (token->type == CPP_ELLIPSIS)
15730 break;
15731 /* Otherwise, there must be more parameters. Consume the
15732 `,'. */
15733 cp_lexer_consume_token (parser->lexer);
15734 /* When parsing something like:
15736 int i(float f, double d)
15738 we can tell after seeing the declaration for "f" that we
15739 are not looking at an initialization of a variable "i",
15740 but rather at the declaration of a function "i".
15742 Due to the fact that the parsing of template arguments
15743 (as specified to a template-id) requires backtracking we
15744 cannot use this technique when inside a template argument
15745 list. */
15746 if (!parser->in_template_argument_list_p
15747 && !parser->in_type_id_in_expr_p
15748 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15749 /* However, a parameter-declaration of the form
15750 "foat(f)" (which is a valid declaration of a
15751 parameter "f") can also be interpreted as an
15752 expression (the conversion of "f" to "float"). */
15753 && !parenthesized_p)
15754 cp_parser_commit_to_tentative_parse (parser);
15756 else
15758 cp_parser_error (parser, "expected %<,%> or %<...%>");
15759 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15760 cp_parser_skip_to_closing_parenthesis (parser,
15761 /*recovering=*/true,
15762 /*or_comma=*/false,
15763 /*consume_paren=*/false);
15764 break;
15768 parser->in_unbraced_linkage_specification_p
15769 = saved_in_unbraced_linkage_specification_p;
15771 return parameters;
15774 /* Parse a parameter declaration.
15776 parameter-declaration:
15777 decl-specifier-seq ... [opt] declarator
15778 decl-specifier-seq declarator = assignment-expression
15779 decl-specifier-seq ... [opt] abstract-declarator [opt]
15780 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15782 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15783 declares a template parameter. (In that case, a non-nested `>'
15784 token encountered during the parsing of the assignment-expression
15785 is not interpreted as a greater-than operator.)
15787 Returns a representation of the parameter, or NULL if an error
15788 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15789 true iff the declarator is of the form "(p)". */
15791 static cp_parameter_declarator *
15792 cp_parser_parameter_declaration (cp_parser *parser,
15793 bool template_parm_p,
15794 bool *parenthesized_p)
15796 int declares_class_or_enum;
15797 cp_decl_specifier_seq decl_specifiers;
15798 cp_declarator *declarator;
15799 tree default_argument;
15800 cp_token *token = NULL, *declarator_token_start = NULL;
15801 const char *saved_message;
15803 /* In a template parameter, `>' is not an operator.
15805 [temp.param]
15807 When parsing a default template-argument for a non-type
15808 template-parameter, the first non-nested `>' is taken as the end
15809 of the template parameter-list rather than a greater-than
15810 operator. */
15812 /* Type definitions may not appear in parameter types. */
15813 saved_message = parser->type_definition_forbidden_message;
15814 parser->type_definition_forbidden_message
15815 = G_("types may not be defined in parameter types");
15817 /* Parse the declaration-specifiers. */
15818 cp_parser_decl_specifier_seq (parser,
15819 CP_PARSER_FLAGS_NONE,
15820 &decl_specifiers,
15821 &declares_class_or_enum);
15823 /* Complain about missing 'typename' or other invalid type names. */
15824 if (!decl_specifiers.any_type_specifiers_p)
15825 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15827 /* If an error occurred, there's no reason to attempt to parse the
15828 rest of the declaration. */
15829 if (cp_parser_error_occurred (parser))
15831 parser->type_definition_forbidden_message = saved_message;
15832 return NULL;
15835 /* Peek at the next token. */
15836 token = cp_lexer_peek_token (parser->lexer);
15838 /* If the next token is a `)', `,', `=', `>', or `...', then there
15839 is no declarator. However, when variadic templates are enabled,
15840 there may be a declarator following `...'. */
15841 if (token->type == CPP_CLOSE_PAREN
15842 || token->type == CPP_COMMA
15843 || token->type == CPP_EQ
15844 || token->type == CPP_GREATER)
15846 declarator = NULL;
15847 if (parenthesized_p)
15848 *parenthesized_p = false;
15850 /* Otherwise, there should be a declarator. */
15851 else
15853 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15854 parser->default_arg_ok_p = false;
15856 /* After seeing a decl-specifier-seq, if the next token is not a
15857 "(", there is no possibility that the code is a valid
15858 expression. Therefore, if parsing tentatively, we commit at
15859 this point. */
15860 if (!parser->in_template_argument_list_p
15861 /* In an expression context, having seen:
15863 (int((char ...
15865 we cannot be sure whether we are looking at a
15866 function-type (taking a "char" as a parameter) or a cast
15867 of some object of type "char" to "int". */
15868 && !parser->in_type_id_in_expr_p
15869 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15870 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15871 cp_parser_commit_to_tentative_parse (parser);
15872 /* Parse the declarator. */
15873 declarator_token_start = token;
15874 declarator = cp_parser_declarator (parser,
15875 CP_PARSER_DECLARATOR_EITHER,
15876 /*ctor_dtor_or_conv_p=*/NULL,
15877 parenthesized_p,
15878 /*member_p=*/false);
15879 parser->default_arg_ok_p = saved_default_arg_ok_p;
15880 /* After the declarator, allow more attributes. */
15881 decl_specifiers.attributes
15882 = chainon (decl_specifiers.attributes,
15883 cp_parser_attributes_opt (parser));
15886 /* If the next token is an ellipsis, and we have not seen a
15887 declarator name, and the type of the declarator contains parameter
15888 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15889 a parameter pack expansion expression. Otherwise, leave the
15890 ellipsis for a C-style variadic function. */
15891 token = cp_lexer_peek_token (parser->lexer);
15892 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15894 tree type = decl_specifiers.type;
15896 if (type && DECL_P (type))
15897 type = TREE_TYPE (type);
15899 if (type
15900 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15901 && declarator_can_be_parameter_pack (declarator)
15902 && (!declarator || !declarator->parameter_pack_p)
15903 && uses_parameter_packs (type))
15905 /* Consume the `...'. */
15906 cp_lexer_consume_token (parser->lexer);
15907 maybe_warn_variadic_templates ();
15909 /* Build a pack expansion type */
15910 if (declarator)
15911 declarator->parameter_pack_p = true;
15912 else
15913 decl_specifiers.type = make_pack_expansion (type);
15917 /* The restriction on defining new types applies only to the type
15918 of the parameter, not to the default argument. */
15919 parser->type_definition_forbidden_message = saved_message;
15921 /* If the next token is `=', then process a default argument. */
15922 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15924 /* Consume the `='. */
15925 cp_lexer_consume_token (parser->lexer);
15927 /* If we are defining a class, then the tokens that make up the
15928 default argument must be saved and processed later. */
15929 if (!template_parm_p && at_class_scope_p ()
15930 && TYPE_BEING_DEFINED (current_class_type)
15931 && !LAMBDA_TYPE_P (current_class_type))
15933 unsigned depth = 0;
15934 int maybe_template_id = 0;
15935 cp_token *first_token;
15936 cp_token *token;
15938 /* Add tokens until we have processed the entire default
15939 argument. We add the range [first_token, token). */
15940 first_token = cp_lexer_peek_token (parser->lexer);
15941 while (true)
15943 bool done = false;
15945 /* Peek at the next token. */
15946 token = cp_lexer_peek_token (parser->lexer);
15947 /* What we do depends on what token we have. */
15948 switch (token->type)
15950 /* In valid code, a default argument must be
15951 immediately followed by a `,' `)', or `...'. */
15952 case CPP_COMMA:
15953 if (depth == 0 && maybe_template_id)
15955 /* If we've seen a '<', we might be in a
15956 template-argument-list. Until Core issue 325 is
15957 resolved, we don't know how this situation ought
15958 to be handled, so try to DTRT. We check whether
15959 what comes after the comma is a valid parameter
15960 declaration list. If it is, then the comma ends
15961 the default argument; otherwise the default
15962 argument continues. */
15963 bool error = false;
15964 tree t;
15966 /* Set ITALP so cp_parser_parameter_declaration_list
15967 doesn't decide to commit to this parse. */
15968 bool saved_italp = parser->in_template_argument_list_p;
15969 parser->in_template_argument_list_p = true;
15971 cp_parser_parse_tentatively (parser);
15972 cp_lexer_consume_token (parser->lexer);
15973 begin_scope (sk_function_parms, NULL_TREE);
15974 cp_parser_parameter_declaration_list (parser, &error);
15975 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15976 pop_binding (DECL_NAME (t), t);
15977 leave_scope ();
15978 if (!cp_parser_error_occurred (parser) && !error)
15979 done = true;
15980 cp_parser_abort_tentative_parse (parser);
15982 parser->in_template_argument_list_p = saved_italp;
15983 break;
15985 case CPP_CLOSE_PAREN:
15986 case CPP_ELLIPSIS:
15987 /* If we run into a non-nested `;', `}', or `]',
15988 then the code is invalid -- but the default
15989 argument is certainly over. */
15990 case CPP_SEMICOLON:
15991 case CPP_CLOSE_BRACE:
15992 case CPP_CLOSE_SQUARE:
15993 if (depth == 0)
15994 done = true;
15995 /* Update DEPTH, if necessary. */
15996 else if (token->type == CPP_CLOSE_PAREN
15997 || token->type == CPP_CLOSE_BRACE
15998 || token->type == CPP_CLOSE_SQUARE)
15999 --depth;
16000 break;
16002 case CPP_OPEN_PAREN:
16003 case CPP_OPEN_SQUARE:
16004 case CPP_OPEN_BRACE:
16005 ++depth;
16006 break;
16008 case CPP_LESS:
16009 if (depth == 0)
16010 /* This might be the comparison operator, or it might
16011 start a template argument list. */
16012 ++maybe_template_id;
16013 break;
16015 case CPP_RSHIFT:
16016 if (cxx_dialect == cxx98)
16017 break;
16018 /* Fall through for C++0x, which treats the `>>'
16019 operator like two `>' tokens in certain
16020 cases. */
16022 case CPP_GREATER:
16023 if (depth == 0)
16025 /* This might be an operator, or it might close a
16026 template argument list. But if a previous '<'
16027 started a template argument list, this will have
16028 closed it, so we can't be in one anymore. */
16029 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16030 if (maybe_template_id < 0)
16031 maybe_template_id = 0;
16033 break;
16035 /* If we run out of tokens, issue an error message. */
16036 case CPP_EOF:
16037 case CPP_PRAGMA_EOL:
16038 error_at (token->location, "file ends in default argument");
16039 done = true;
16040 break;
16042 case CPP_NAME:
16043 case CPP_SCOPE:
16044 /* In these cases, we should look for template-ids.
16045 For example, if the default argument is
16046 `X<int, double>()', we need to do name lookup to
16047 figure out whether or not `X' is a template; if
16048 so, the `,' does not end the default argument.
16050 That is not yet done. */
16051 break;
16053 default:
16054 break;
16057 /* If we've reached the end, stop. */
16058 if (done)
16059 break;
16061 /* Add the token to the token block. */
16062 token = cp_lexer_consume_token (parser->lexer);
16065 /* Create a DEFAULT_ARG to represent the unparsed default
16066 argument. */
16067 default_argument = make_node (DEFAULT_ARG);
16068 DEFARG_TOKENS (default_argument)
16069 = cp_token_cache_new (first_token, token);
16070 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16072 /* Outside of a class definition, we can just parse the
16073 assignment-expression. */
16074 else
16076 token = cp_lexer_peek_token (parser->lexer);
16077 default_argument
16078 = cp_parser_default_argument (parser, template_parm_p);
16081 if (!parser->default_arg_ok_p)
16083 if (flag_permissive)
16084 warning (0, "deprecated use of default argument for parameter of non-function");
16085 else
16087 error_at (token->location,
16088 "default arguments are only "
16089 "permitted for function parameters");
16090 default_argument = NULL_TREE;
16093 else if ((declarator && declarator->parameter_pack_p)
16094 || (decl_specifiers.type
16095 && PACK_EXPANSION_P (decl_specifiers.type)))
16097 /* Find the name of the parameter pack. */
16098 cp_declarator *id_declarator = declarator;
16099 while (id_declarator && id_declarator->kind != cdk_id)
16100 id_declarator = id_declarator->declarator;
16102 if (id_declarator && id_declarator->kind == cdk_id)
16103 error_at (declarator_token_start->location,
16104 template_parm_p
16105 ? "template parameter pack %qD"
16106 " cannot have a default argument"
16107 : "parameter pack %qD cannot have a default argument",
16108 id_declarator->u.id.unqualified_name);
16109 else
16110 error_at (declarator_token_start->location,
16111 template_parm_p
16112 ? "template parameter pack cannot have a default argument"
16113 : "parameter pack cannot have a default argument");
16115 default_argument = NULL_TREE;
16118 else
16119 default_argument = NULL_TREE;
16121 return make_parameter_declarator (&decl_specifiers,
16122 declarator,
16123 default_argument);
16126 /* Parse a default argument and return it.
16128 TEMPLATE_PARM_P is true if this is a default argument for a
16129 non-type template parameter. */
16130 static tree
16131 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16133 tree default_argument = NULL_TREE;
16134 bool saved_greater_than_is_operator_p;
16135 bool saved_local_variables_forbidden_p;
16137 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16138 set correctly. */
16139 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16140 parser->greater_than_is_operator_p = !template_parm_p;
16141 /* Local variable names (and the `this' keyword) may not
16142 appear in a default argument. */
16143 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16144 parser->local_variables_forbidden_p = true;
16145 /* Parse the assignment-expression. */
16146 if (template_parm_p)
16147 push_deferring_access_checks (dk_no_deferred);
16148 default_argument
16149 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16150 if (template_parm_p)
16151 pop_deferring_access_checks ();
16152 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16153 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16155 return default_argument;
16158 /* Parse a function-body.
16160 function-body:
16161 compound_statement */
16163 static void
16164 cp_parser_function_body (cp_parser *parser)
16166 cp_parser_compound_statement (parser, NULL, false, true);
16169 /* Parse a ctor-initializer-opt followed by a function-body. Return
16170 true if a ctor-initializer was present. */
16172 static bool
16173 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16175 tree body, list;
16176 bool ctor_initializer_p;
16177 const bool check_body_p =
16178 DECL_CONSTRUCTOR_P (current_function_decl)
16179 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16180 tree last = NULL;
16182 /* Begin the function body. */
16183 body = begin_function_body ();
16184 /* Parse the optional ctor-initializer. */
16185 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16187 /* If we're parsing a constexpr constructor definition, we need
16188 to check that the constructor body is indeed empty. However,
16189 before we get to cp_parser_function_body lot of junk has been
16190 generated, so we can't just check that we have an empty block.
16191 Rather we take a snapshot of the outermost block, and check whether
16192 cp_parser_function_body changed its state. */
16193 if (check_body_p)
16195 list = body;
16196 if (TREE_CODE (list) == BIND_EXPR)
16197 list = BIND_EXPR_BODY (list);
16198 if (TREE_CODE (list) == STATEMENT_LIST
16199 && STATEMENT_LIST_TAIL (list) != NULL)
16200 last = STATEMENT_LIST_TAIL (list)->stmt;
16202 /* Parse the function-body. */
16203 cp_parser_function_body (parser);
16204 if (check_body_p)
16205 check_constexpr_ctor_body (last, list);
16206 /* Finish the function body. */
16207 finish_function_body (body);
16209 return ctor_initializer_p;
16212 /* Parse an initializer.
16214 initializer:
16215 = initializer-clause
16216 ( expression-list )
16218 Returns an expression representing the initializer. If no
16219 initializer is present, NULL_TREE is returned.
16221 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16222 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16223 set to TRUE if there is no initializer present. If there is an
16224 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16225 is set to true; otherwise it is set to false. */
16227 static tree
16228 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16229 bool* non_constant_p)
16231 cp_token *token;
16232 tree init;
16234 /* Peek at the next token. */
16235 token = cp_lexer_peek_token (parser->lexer);
16237 /* Let our caller know whether or not this initializer was
16238 parenthesized. */
16239 *is_direct_init = (token->type != CPP_EQ);
16240 /* Assume that the initializer is constant. */
16241 *non_constant_p = false;
16243 if (token->type == CPP_EQ)
16245 /* Consume the `='. */
16246 cp_lexer_consume_token (parser->lexer);
16247 /* Parse the initializer-clause. */
16248 init = cp_parser_initializer_clause (parser, non_constant_p);
16250 else if (token->type == CPP_OPEN_PAREN)
16252 VEC(tree,gc) *vec;
16253 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16254 /*cast_p=*/false,
16255 /*allow_expansion_p=*/true,
16256 non_constant_p);
16257 if (vec == NULL)
16258 return error_mark_node;
16259 init = build_tree_list_vec (vec);
16260 release_tree_vector (vec);
16262 else if (token->type == CPP_OPEN_BRACE)
16264 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16265 init = cp_parser_braced_list (parser, non_constant_p);
16266 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16268 else
16270 /* Anything else is an error. */
16271 cp_parser_error (parser, "expected initializer");
16272 init = error_mark_node;
16275 return init;
16278 /* Parse an initializer-clause.
16280 initializer-clause:
16281 assignment-expression
16282 braced-init-list
16284 Returns an expression representing the initializer.
16286 If the `assignment-expression' production is used the value
16287 returned is simply a representation for the expression.
16289 Otherwise, calls cp_parser_braced_list. */
16291 static tree
16292 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16294 tree initializer;
16296 /* Assume the expression is constant. */
16297 *non_constant_p = false;
16299 /* If it is not a `{', then we are looking at an
16300 assignment-expression. */
16301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16303 initializer
16304 = cp_parser_constant_expression (parser,
16305 /*allow_non_constant_p=*/true,
16306 non_constant_p);
16307 if (!*non_constant_p)
16309 /* We only want to fold if this is really a constant
16310 expression. FIXME Actually, we don't want to fold here, but in
16311 cp_finish_decl. */
16312 tree folded = fold_non_dependent_expr (initializer);
16313 folded = maybe_constant_value (folded);
16314 if (TREE_CONSTANT (folded))
16315 initializer = folded;
16318 else
16319 initializer = cp_parser_braced_list (parser, non_constant_p);
16321 return initializer;
16324 /* Parse a brace-enclosed initializer list.
16326 braced-init-list:
16327 { initializer-list , [opt] }
16330 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16331 the elements of the initializer-list (or NULL, if the last
16332 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16333 NULL_TREE. There is no way to detect whether or not the optional
16334 trailing `,' was provided. NON_CONSTANT_P is as for
16335 cp_parser_initializer. */
16337 static tree
16338 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16340 tree initializer;
16342 /* Consume the `{' token. */
16343 cp_lexer_consume_token (parser->lexer);
16344 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16345 initializer = make_node (CONSTRUCTOR);
16346 /* If it's not a `}', then there is a non-trivial initializer. */
16347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16349 /* Parse the initializer list. */
16350 CONSTRUCTOR_ELTS (initializer)
16351 = cp_parser_initializer_list (parser, non_constant_p);
16352 /* A trailing `,' token is allowed. */
16353 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16354 cp_lexer_consume_token (parser->lexer);
16356 /* Now, there should be a trailing `}'. */
16357 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16358 TREE_TYPE (initializer) = init_list_type_node;
16359 return initializer;
16362 /* Parse an initializer-list.
16364 initializer-list:
16365 initializer-clause ... [opt]
16366 initializer-list , initializer-clause ... [opt]
16368 GNU Extension:
16370 initializer-list:
16371 identifier : initializer-clause
16372 initializer-list, identifier : initializer-clause
16374 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16375 for the initializer. If the INDEX of the elt is non-NULL, it is the
16376 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16377 as for cp_parser_initializer. */
16379 static VEC(constructor_elt,gc) *
16380 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16382 VEC(constructor_elt,gc) *v = NULL;
16384 /* Assume all of the expressions are constant. */
16385 *non_constant_p = false;
16387 /* Parse the rest of the list. */
16388 while (true)
16390 cp_token *token;
16391 tree identifier;
16392 tree initializer;
16393 bool clause_non_constant_p;
16395 /* If the next token is an identifier and the following one is a
16396 colon, we are looking at the GNU designated-initializer
16397 syntax. */
16398 if (cp_parser_allow_gnu_extensions_p (parser)
16399 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16400 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16402 /* Warn the user that they are using an extension. */
16403 pedwarn (input_location, OPT_pedantic,
16404 "ISO C++ does not allow designated initializers");
16405 /* Consume the identifier. */
16406 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16407 /* Consume the `:'. */
16408 cp_lexer_consume_token (parser->lexer);
16410 else
16411 identifier = NULL_TREE;
16413 /* Parse the initializer. */
16414 initializer = cp_parser_initializer_clause (parser,
16415 &clause_non_constant_p);
16416 /* If any clause is non-constant, so is the entire initializer. */
16417 if (clause_non_constant_p)
16418 *non_constant_p = true;
16420 /* If we have an ellipsis, this is an initializer pack
16421 expansion. */
16422 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16424 /* Consume the `...'. */
16425 cp_lexer_consume_token (parser->lexer);
16427 /* Turn the initializer into an initializer expansion. */
16428 initializer = make_pack_expansion (initializer);
16431 /* Add it to the vector. */
16432 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16434 /* If the next token is not a comma, we have reached the end of
16435 the list. */
16436 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16437 break;
16439 /* Peek at the next token. */
16440 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16441 /* If the next token is a `}', then we're still done. An
16442 initializer-clause can have a trailing `,' after the
16443 initializer-list and before the closing `}'. */
16444 if (token->type == CPP_CLOSE_BRACE)
16445 break;
16447 /* Consume the `,' token. */
16448 cp_lexer_consume_token (parser->lexer);
16451 return v;
16454 /* Classes [gram.class] */
16456 /* Parse a class-name.
16458 class-name:
16459 identifier
16460 template-id
16462 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16463 to indicate that names looked up in dependent types should be
16464 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16465 keyword has been used to indicate that the name that appears next
16466 is a template. TAG_TYPE indicates the explicit tag given before
16467 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16468 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16469 is the class being defined in a class-head.
16471 Returns the TYPE_DECL representing the class. */
16473 static tree
16474 cp_parser_class_name (cp_parser *parser,
16475 bool typename_keyword_p,
16476 bool template_keyword_p,
16477 enum tag_types tag_type,
16478 bool check_dependency_p,
16479 bool class_head_p,
16480 bool is_declaration)
16482 tree decl;
16483 tree scope;
16484 bool typename_p;
16485 cp_token *token;
16486 tree identifier = NULL_TREE;
16488 /* All class-names start with an identifier. */
16489 token = cp_lexer_peek_token (parser->lexer);
16490 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16492 cp_parser_error (parser, "expected class-name");
16493 return error_mark_node;
16496 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16497 to a template-id, so we save it here. */
16498 scope = parser->scope;
16499 if (scope == error_mark_node)
16500 return error_mark_node;
16502 /* Any name names a type if we're following the `typename' keyword
16503 in a qualified name where the enclosing scope is type-dependent. */
16504 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16505 && dependent_type_p (scope));
16506 /* Handle the common case (an identifier, but not a template-id)
16507 efficiently. */
16508 if (token->type == CPP_NAME
16509 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16511 cp_token *identifier_token;
16512 bool ambiguous_p;
16514 /* Look for the identifier. */
16515 identifier_token = cp_lexer_peek_token (parser->lexer);
16516 ambiguous_p = identifier_token->ambiguous_p;
16517 identifier = cp_parser_identifier (parser);
16518 /* If the next token isn't an identifier, we are certainly not
16519 looking at a class-name. */
16520 if (identifier == error_mark_node)
16521 decl = error_mark_node;
16522 /* If we know this is a type-name, there's no need to look it
16523 up. */
16524 else if (typename_p)
16525 decl = identifier;
16526 else
16528 tree ambiguous_decls;
16529 /* If we already know that this lookup is ambiguous, then
16530 we've already issued an error message; there's no reason
16531 to check again. */
16532 if (ambiguous_p)
16534 cp_parser_simulate_error (parser);
16535 return error_mark_node;
16537 /* If the next token is a `::', then the name must be a type
16538 name.
16540 [basic.lookup.qual]
16542 During the lookup for a name preceding the :: scope
16543 resolution operator, object, function, and enumerator
16544 names are ignored. */
16545 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16546 tag_type = typename_type;
16547 /* Look up the name. */
16548 decl = cp_parser_lookup_name (parser, identifier,
16549 tag_type,
16550 /*is_template=*/false,
16551 /*is_namespace=*/false,
16552 check_dependency_p,
16553 &ambiguous_decls,
16554 identifier_token->location);
16555 if (ambiguous_decls)
16557 if (cp_parser_parsing_tentatively (parser))
16558 cp_parser_simulate_error (parser);
16559 return error_mark_node;
16563 else
16565 /* Try a template-id. */
16566 decl = cp_parser_template_id (parser, template_keyword_p,
16567 check_dependency_p,
16568 is_declaration);
16569 if (decl == error_mark_node)
16570 return error_mark_node;
16573 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16575 /* If this is a typename, create a TYPENAME_TYPE. */
16576 if (typename_p && decl != error_mark_node)
16578 decl = make_typename_type (scope, decl, typename_type,
16579 /*complain=*/tf_error);
16580 if (decl != error_mark_node)
16581 decl = TYPE_NAME (decl);
16584 /* Check to see that it is really the name of a class. */
16585 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16586 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16587 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16588 /* Situations like this:
16590 template <typename T> struct A {
16591 typename T::template X<int>::I i;
16594 are problematic. Is `T::template X<int>' a class-name? The
16595 standard does not seem to be definitive, but there is no other
16596 valid interpretation of the following `::'. Therefore, those
16597 names are considered class-names. */
16599 decl = make_typename_type (scope, decl, tag_type, tf_error);
16600 if (decl != error_mark_node)
16601 decl = TYPE_NAME (decl);
16603 else if (TREE_CODE (decl) != TYPE_DECL
16604 || TREE_TYPE (decl) == error_mark_node
16605 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16606 /* In Objective-C 2.0, a classname followed by '.' starts a
16607 dot-syntax expression, and it's not a type-name. */
16608 || (c_dialect_objc ()
16609 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
16610 && objc_is_class_name (decl)))
16611 decl = error_mark_node;
16613 if (decl == error_mark_node)
16614 cp_parser_error (parser, "expected class-name");
16615 else if (identifier && !parser->scope)
16616 maybe_note_name_used_in_class (identifier, decl);
16618 return decl;
16621 /* Parse a class-specifier.
16623 class-specifier:
16624 class-head { member-specification [opt] }
16626 Returns the TREE_TYPE representing the class. */
16628 static tree
16629 cp_parser_class_specifier (cp_parser* parser)
16631 tree type;
16632 tree attributes = NULL_TREE;
16633 bool nested_name_specifier_p;
16634 unsigned saved_num_template_parameter_lists;
16635 bool saved_in_function_body;
16636 bool saved_in_unbraced_linkage_specification_p;
16637 tree old_scope = NULL_TREE;
16638 tree scope = NULL_TREE;
16639 tree bases;
16640 cp_token *closing_brace;
16642 push_deferring_access_checks (dk_no_deferred);
16644 /* Parse the class-head. */
16645 type = cp_parser_class_head (parser,
16646 &nested_name_specifier_p,
16647 &attributes,
16648 &bases);
16649 /* If the class-head was a semantic disaster, skip the entire body
16650 of the class. */
16651 if (!type)
16653 cp_parser_skip_to_end_of_block_or_statement (parser);
16654 pop_deferring_access_checks ();
16655 return error_mark_node;
16658 /* Look for the `{'. */
16659 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16661 pop_deferring_access_checks ();
16662 return error_mark_node;
16665 /* Process the base classes. If they're invalid, skip the
16666 entire class body. */
16667 if (!xref_basetypes (type, bases))
16669 /* Consuming the closing brace yields better error messages
16670 later on. */
16671 if (cp_parser_skip_to_closing_brace (parser))
16672 cp_lexer_consume_token (parser->lexer);
16673 pop_deferring_access_checks ();
16674 return error_mark_node;
16677 /* Issue an error message if type-definitions are forbidden here. */
16678 cp_parser_check_type_definition (parser);
16679 /* Remember that we are defining one more class. */
16680 ++parser->num_classes_being_defined;
16681 /* Inside the class, surrounding template-parameter-lists do not
16682 apply. */
16683 saved_num_template_parameter_lists
16684 = parser->num_template_parameter_lists;
16685 parser->num_template_parameter_lists = 0;
16686 /* We are not in a function body. */
16687 saved_in_function_body = parser->in_function_body;
16688 parser->in_function_body = false;
16689 /* We are not immediately inside an extern "lang" block. */
16690 saved_in_unbraced_linkage_specification_p
16691 = parser->in_unbraced_linkage_specification_p;
16692 parser->in_unbraced_linkage_specification_p = false;
16694 /* Start the class. */
16695 if (nested_name_specifier_p)
16697 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16698 old_scope = push_inner_scope (scope);
16700 type = begin_class_definition (type, attributes);
16702 if (type == error_mark_node)
16703 /* If the type is erroneous, skip the entire body of the class. */
16704 cp_parser_skip_to_closing_brace (parser);
16705 else
16706 /* Parse the member-specification. */
16707 cp_parser_member_specification_opt (parser);
16709 /* Look for the trailing `}'. */
16710 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16711 /* Look for trailing attributes to apply to this class. */
16712 if (cp_parser_allow_gnu_extensions_p (parser))
16713 attributes = cp_parser_attributes_opt (parser);
16714 if (type != error_mark_node)
16715 type = finish_struct (type, attributes);
16716 if (nested_name_specifier_p)
16717 pop_inner_scope (old_scope, scope);
16719 /* We've finished a type definition. Check for the common syntax
16720 error of forgetting a semicolon after the definition. We need to
16721 be careful, as we can't just check for not-a-semicolon and be done
16722 with it; the user might have typed:
16724 class X { } c = ...;
16725 class X { } *p = ...;
16727 and so forth. Instead, enumerate all the possible tokens that
16728 might follow this production; if we don't see one of them, then
16729 complain and silently insert the semicolon. */
16731 cp_token *token = cp_lexer_peek_token (parser->lexer);
16732 bool want_semicolon = true;
16734 switch (token->type)
16736 case CPP_NAME:
16737 case CPP_SEMICOLON:
16738 case CPP_MULT:
16739 case CPP_AND:
16740 case CPP_OPEN_PAREN:
16741 case CPP_CLOSE_PAREN:
16742 case CPP_COMMA:
16743 want_semicolon = false;
16744 break;
16746 /* While it's legal for type qualifiers and storage class
16747 specifiers to follow type definitions in the grammar, only
16748 compiler testsuites contain code like that. Assume that if
16749 we see such code, then what we're really seeing is a case
16750 like:
16752 class X { }
16753 const <type> var = ...;
16757 class Y { }
16758 static <type> func (...) ...
16760 i.e. the qualifier or specifier applies to the next
16761 declaration. To do so, however, we need to look ahead one
16762 more token to see if *that* token is a type specifier.
16764 This code could be improved to handle:
16766 class Z { }
16767 static const <type> var = ...; */
16768 case CPP_KEYWORD:
16769 if (keyword_is_decl_specifier (token->keyword))
16771 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16773 /* Handling user-defined types here would be nice, but very
16774 tricky. */
16775 want_semicolon
16776 = (lookahead->type == CPP_KEYWORD
16777 && keyword_begins_type_specifier (lookahead->keyword));
16779 break;
16780 default:
16781 break;
16784 /* If we don't have a type, then something is very wrong and we
16785 shouldn't try to do anything clever. Likewise for not seeing the
16786 closing brace. */
16787 if (closing_brace && TYPE_P (type) && want_semicolon)
16789 cp_token_position prev
16790 = cp_lexer_previous_token_position (parser->lexer);
16791 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16792 location_t loc = prev_token->location;
16794 if (CLASSTYPE_DECLARED_CLASS (type))
16795 error_at (loc, "expected %<;%> after class definition");
16796 else if (TREE_CODE (type) == RECORD_TYPE)
16797 error_at (loc, "expected %<;%> after struct definition");
16798 else if (TREE_CODE (type) == UNION_TYPE)
16799 error_at (loc, "expected %<;%> after union definition");
16800 else
16801 gcc_unreachable ();
16803 /* Unget one token and smash it to look as though we encountered
16804 a semicolon in the input stream. */
16805 cp_lexer_set_token_position (parser->lexer, prev);
16806 token = cp_lexer_peek_token (parser->lexer);
16807 token->type = CPP_SEMICOLON;
16808 token->keyword = RID_MAX;
16812 /* If this class is not itself within the scope of another class,
16813 then we need to parse the bodies of all of the queued function
16814 definitions. Note that the queued functions defined in a class
16815 are not always processed immediately following the
16816 class-specifier for that class. Consider:
16818 struct A {
16819 struct B { void f() { sizeof (A); } };
16822 If `f' were processed before the processing of `A' were
16823 completed, there would be no way to compute the size of `A'.
16824 Note that the nesting we are interested in here is lexical --
16825 not the semantic nesting given by TYPE_CONTEXT. In particular,
16826 for:
16828 struct A { struct B; };
16829 struct A::B { void f() { } };
16831 there is no need to delay the parsing of `A::B::f'. */
16832 if (--parser->num_classes_being_defined == 0)
16834 tree fn;
16835 tree class_type = NULL_TREE;
16836 tree pushed_scope = NULL_TREE;
16837 unsigned ix;
16838 cp_default_arg_entry *e;
16840 /* In a first pass, parse default arguments to the functions.
16841 Then, in a second pass, parse the bodies of the functions.
16842 This two-phased approach handles cases like:
16844 struct S {
16845 void f() { g(); }
16846 void g(int i = 3);
16850 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16851 ix, e)
16853 fn = e->decl;
16854 /* If there are default arguments that have not yet been processed,
16855 take care of them now. */
16856 if (class_type != e->class_type)
16858 if (pushed_scope)
16859 pop_scope (pushed_scope);
16860 class_type = e->class_type;
16861 pushed_scope = push_scope (class_type);
16863 /* Make sure that any template parameters are in scope. */
16864 maybe_begin_member_template_processing (fn);
16865 /* Parse the default argument expressions. */
16866 cp_parser_late_parsing_default_args (parser, fn);
16867 /* Remove any template parameters from the symbol table. */
16868 maybe_end_member_template_processing ();
16870 if (pushed_scope)
16871 pop_scope (pushed_scope);
16872 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16873 /* Now parse the body of the functions. */
16874 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16875 cp_parser_late_parsing_for_member (parser, fn);
16876 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
16879 /* Put back any saved access checks. */
16880 pop_deferring_access_checks ();
16882 /* Restore saved state. */
16883 parser->in_function_body = saved_in_function_body;
16884 parser->num_template_parameter_lists
16885 = saved_num_template_parameter_lists;
16886 parser->in_unbraced_linkage_specification_p
16887 = saved_in_unbraced_linkage_specification_p;
16889 return type;
16892 /* Parse a class-head.
16894 class-head:
16895 class-key identifier [opt] base-clause [opt]
16896 class-key nested-name-specifier identifier base-clause [opt]
16897 class-key nested-name-specifier [opt] template-id
16898 base-clause [opt]
16900 GNU Extensions:
16901 class-key attributes identifier [opt] base-clause [opt]
16902 class-key attributes nested-name-specifier identifier base-clause [opt]
16903 class-key attributes nested-name-specifier [opt] template-id
16904 base-clause [opt]
16906 Upon return BASES is initialized to the list of base classes (or
16907 NULL, if there are none) in the same form returned by
16908 cp_parser_base_clause.
16910 Returns the TYPE of the indicated class. Sets
16911 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16912 involving a nested-name-specifier was used, and FALSE otherwise.
16914 Returns error_mark_node if this is not a class-head.
16916 Returns NULL_TREE if the class-head is syntactically valid, but
16917 semantically invalid in a way that means we should skip the entire
16918 body of the class. */
16920 static tree
16921 cp_parser_class_head (cp_parser* parser,
16922 bool* nested_name_specifier_p,
16923 tree *attributes_p,
16924 tree *bases)
16926 tree nested_name_specifier;
16927 enum tag_types class_key;
16928 tree id = NULL_TREE;
16929 tree type = NULL_TREE;
16930 tree attributes;
16931 bool template_id_p = false;
16932 bool qualified_p = false;
16933 bool invalid_nested_name_p = false;
16934 bool invalid_explicit_specialization_p = false;
16935 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
16936 tree pushed_scope = NULL_TREE;
16937 unsigned num_templates;
16938 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16939 /* Assume no nested-name-specifier will be present. */
16940 *nested_name_specifier_p = false;
16941 /* Assume no template parameter lists will be used in defining the
16942 type. */
16943 num_templates = 0;
16944 parser->colon_corrects_to_scope_p = false;
16946 *bases = NULL_TREE;
16948 /* Look for the class-key. */
16949 class_key = cp_parser_class_key (parser);
16950 if (class_key == none_type)
16951 return error_mark_node;
16953 /* Parse the attributes. */
16954 attributes = cp_parser_attributes_opt (parser);
16956 /* If the next token is `::', that is invalid -- but sometimes
16957 people do try to write:
16959 struct ::S {};
16961 Handle this gracefully by accepting the extra qualifier, and then
16962 issuing an error about it later if this really is a
16963 class-head. If it turns out just to be an elaborated type
16964 specifier, remain silent. */
16965 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16966 qualified_p = true;
16968 push_deferring_access_checks (dk_no_check);
16970 /* Determine the name of the class. Begin by looking for an
16971 optional nested-name-specifier. */
16972 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16973 nested_name_specifier
16974 = cp_parser_nested_name_specifier_opt (parser,
16975 /*typename_keyword_p=*/false,
16976 /*check_dependency_p=*/false,
16977 /*type_p=*/false,
16978 /*is_declaration=*/false);
16979 /* If there was a nested-name-specifier, then there *must* be an
16980 identifier. */
16981 if (nested_name_specifier)
16983 type_start_token = cp_lexer_peek_token (parser->lexer);
16984 /* Although the grammar says `identifier', it really means
16985 `class-name' or `template-name'. You are only allowed to
16986 define a class that has already been declared with this
16987 syntax.
16989 The proposed resolution for Core Issue 180 says that wherever
16990 you see `class T::X' you should treat `X' as a type-name.
16992 It is OK to define an inaccessible class; for example:
16994 class A { class B; };
16995 class A::B {};
16997 We do not know if we will see a class-name, or a
16998 template-name. We look for a class-name first, in case the
16999 class-name is a template-id; if we looked for the
17000 template-name first we would stop after the template-name. */
17001 cp_parser_parse_tentatively (parser);
17002 type = cp_parser_class_name (parser,
17003 /*typename_keyword_p=*/false,
17004 /*template_keyword_p=*/false,
17005 class_type,
17006 /*check_dependency_p=*/false,
17007 /*class_head_p=*/true,
17008 /*is_declaration=*/false);
17009 /* If that didn't work, ignore the nested-name-specifier. */
17010 if (!cp_parser_parse_definitely (parser))
17012 invalid_nested_name_p = true;
17013 type_start_token = cp_lexer_peek_token (parser->lexer);
17014 id = cp_parser_identifier (parser);
17015 if (id == error_mark_node)
17016 id = NULL_TREE;
17018 /* If we could not find a corresponding TYPE, treat this
17019 declaration like an unqualified declaration. */
17020 if (type == error_mark_node)
17021 nested_name_specifier = NULL_TREE;
17022 /* Otherwise, count the number of templates used in TYPE and its
17023 containing scopes. */
17024 else
17026 tree scope;
17028 for (scope = TREE_TYPE (type);
17029 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17030 scope = (TYPE_P (scope)
17031 ? TYPE_CONTEXT (scope)
17032 : DECL_CONTEXT (scope)))
17033 if (TYPE_P (scope)
17034 && CLASS_TYPE_P (scope)
17035 && CLASSTYPE_TEMPLATE_INFO (scope)
17036 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17037 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17038 ++num_templates;
17041 /* Otherwise, the identifier is optional. */
17042 else
17044 /* We don't know whether what comes next is a template-id,
17045 an identifier, or nothing at all. */
17046 cp_parser_parse_tentatively (parser);
17047 /* Check for a template-id. */
17048 type_start_token = cp_lexer_peek_token (parser->lexer);
17049 id = cp_parser_template_id (parser,
17050 /*template_keyword_p=*/false,
17051 /*check_dependency_p=*/true,
17052 /*is_declaration=*/true);
17053 /* If that didn't work, it could still be an identifier. */
17054 if (!cp_parser_parse_definitely (parser))
17056 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17058 type_start_token = cp_lexer_peek_token (parser->lexer);
17059 id = cp_parser_identifier (parser);
17061 else
17062 id = NULL_TREE;
17064 else
17066 template_id_p = true;
17067 ++num_templates;
17071 pop_deferring_access_checks ();
17073 if (id)
17074 cp_parser_check_for_invalid_template_id (parser, id,
17075 type_start_token->location);
17077 /* If it's not a `:' or a `{' then we can't really be looking at a
17078 class-head, since a class-head only appears as part of a
17079 class-specifier. We have to detect this situation before calling
17080 xref_tag, since that has irreversible side-effects. */
17081 if (!cp_parser_next_token_starts_class_definition_p (parser))
17083 cp_parser_error (parser, "expected %<{%> or %<:%>");
17084 type = error_mark_node;
17085 goto out;
17088 /* At this point, we're going ahead with the class-specifier, even
17089 if some other problem occurs. */
17090 cp_parser_commit_to_tentative_parse (parser);
17091 /* Issue the error about the overly-qualified name now. */
17092 if (qualified_p)
17094 cp_parser_error (parser,
17095 "global qualification of class name is invalid");
17096 type = error_mark_node;
17097 goto out;
17099 else if (invalid_nested_name_p)
17101 cp_parser_error (parser,
17102 "qualified name does not name a class");
17103 type = error_mark_node;
17104 goto out;
17106 else if (nested_name_specifier)
17108 tree scope;
17110 /* Reject typedef-names in class heads. */
17111 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17113 error_at (type_start_token->location,
17114 "invalid class name in declaration of %qD",
17115 type);
17116 type = NULL_TREE;
17117 goto done;
17120 /* Figure out in what scope the declaration is being placed. */
17121 scope = current_scope ();
17122 /* If that scope does not contain the scope in which the
17123 class was originally declared, the program is invalid. */
17124 if (scope && !is_ancestor (scope, nested_name_specifier))
17126 if (at_namespace_scope_p ())
17127 error_at (type_start_token->location,
17128 "declaration of %qD in namespace %qD which does not "
17129 "enclose %qD",
17130 type, scope, nested_name_specifier);
17131 else
17132 error_at (type_start_token->location,
17133 "declaration of %qD in %qD which does not enclose %qD",
17134 type, scope, nested_name_specifier);
17135 type = NULL_TREE;
17136 goto done;
17138 /* [dcl.meaning]
17140 A declarator-id shall not be qualified except for the
17141 definition of a ... nested class outside of its class
17142 ... [or] the definition or explicit instantiation of a
17143 class member of a namespace outside of its namespace. */
17144 if (scope == nested_name_specifier)
17146 permerror (nested_name_specifier_token_start->location,
17147 "extra qualification not allowed");
17148 nested_name_specifier = NULL_TREE;
17149 num_templates = 0;
17152 /* An explicit-specialization must be preceded by "template <>". If
17153 it is not, try to recover gracefully. */
17154 if (at_namespace_scope_p ()
17155 && parser->num_template_parameter_lists == 0
17156 && template_id_p)
17158 error_at (type_start_token->location,
17159 "an explicit specialization must be preceded by %<template <>%>");
17160 invalid_explicit_specialization_p = true;
17161 /* Take the same action that would have been taken by
17162 cp_parser_explicit_specialization. */
17163 ++parser->num_template_parameter_lists;
17164 begin_specialization ();
17166 /* There must be no "return" statements between this point and the
17167 end of this function; set "type "to the correct return value and
17168 use "goto done;" to return. */
17169 /* Make sure that the right number of template parameters were
17170 present. */
17171 if (!cp_parser_check_template_parameters (parser, num_templates,
17172 type_start_token->location,
17173 /*declarator=*/NULL))
17175 /* If something went wrong, there is no point in even trying to
17176 process the class-definition. */
17177 type = NULL_TREE;
17178 goto done;
17181 /* Look up the type. */
17182 if (template_id_p)
17184 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17185 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17186 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17188 error_at (type_start_token->location,
17189 "function template %qD redeclared as a class template", id);
17190 type = error_mark_node;
17192 else
17194 type = TREE_TYPE (id);
17195 type = maybe_process_partial_specialization (type);
17197 if (nested_name_specifier)
17198 pushed_scope = push_scope (nested_name_specifier);
17200 else if (nested_name_specifier)
17202 tree class_type;
17204 /* Given:
17206 template <typename T> struct S { struct T };
17207 template <typename T> struct S<T>::T { };
17209 we will get a TYPENAME_TYPE when processing the definition of
17210 `S::T'. We need to resolve it to the actual type before we
17211 try to define it. */
17212 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17214 class_type = resolve_typename_type (TREE_TYPE (type),
17215 /*only_current_p=*/false);
17216 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17217 type = TYPE_NAME (class_type);
17218 else
17220 cp_parser_error (parser, "could not resolve typename type");
17221 type = error_mark_node;
17225 if (maybe_process_partial_specialization (TREE_TYPE (type))
17226 == error_mark_node)
17228 type = NULL_TREE;
17229 goto done;
17232 class_type = current_class_type;
17233 /* Enter the scope indicated by the nested-name-specifier. */
17234 pushed_scope = push_scope (nested_name_specifier);
17235 /* Get the canonical version of this type. */
17236 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17237 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17238 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17240 type = push_template_decl (type);
17241 if (type == error_mark_node)
17243 type = NULL_TREE;
17244 goto done;
17248 type = TREE_TYPE (type);
17249 *nested_name_specifier_p = true;
17251 else /* The name is not a nested name. */
17253 /* If the class was unnamed, create a dummy name. */
17254 if (!id)
17255 id = make_anon_name ();
17256 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17257 parser->num_template_parameter_lists);
17260 /* Indicate whether this class was declared as a `class' or as a
17261 `struct'. */
17262 if (TREE_CODE (type) == RECORD_TYPE)
17263 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17264 cp_parser_check_class_key (class_key, type);
17266 /* If this type was already complete, and we see another definition,
17267 that's an error. */
17268 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17270 error_at (type_start_token->location, "redefinition of %q#T",
17271 type);
17272 error_at (type_start_token->location, "previous definition of %q+#T",
17273 type);
17274 type = NULL_TREE;
17275 goto done;
17277 else if (type == error_mark_node)
17278 type = NULL_TREE;
17280 /* We will have entered the scope containing the class; the names of
17281 base classes should be looked up in that context. For example:
17283 struct A { struct B {}; struct C; };
17284 struct A::C : B {};
17286 is valid. */
17288 /* Get the list of base-classes, if there is one. */
17289 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17290 *bases = cp_parser_base_clause (parser);
17292 done:
17293 /* Leave the scope given by the nested-name-specifier. We will
17294 enter the class scope itself while processing the members. */
17295 if (pushed_scope)
17296 pop_scope (pushed_scope);
17298 if (invalid_explicit_specialization_p)
17300 end_specialization ();
17301 --parser->num_template_parameter_lists;
17304 if (type)
17305 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17306 *attributes_p = attributes;
17307 out:
17308 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17309 return type;
17312 /* Parse a class-key.
17314 class-key:
17315 class
17316 struct
17317 union
17319 Returns the kind of class-key specified, or none_type to indicate
17320 error. */
17322 static enum tag_types
17323 cp_parser_class_key (cp_parser* parser)
17325 cp_token *token;
17326 enum tag_types tag_type;
17328 /* Look for the class-key. */
17329 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17330 if (!token)
17331 return none_type;
17333 /* Check to see if the TOKEN is a class-key. */
17334 tag_type = cp_parser_token_is_class_key (token);
17335 if (!tag_type)
17336 cp_parser_error (parser, "expected class-key");
17337 return tag_type;
17340 /* Parse an (optional) member-specification.
17342 member-specification:
17343 member-declaration member-specification [opt]
17344 access-specifier : member-specification [opt] */
17346 static void
17347 cp_parser_member_specification_opt (cp_parser* parser)
17349 while (true)
17351 cp_token *token;
17352 enum rid keyword;
17354 /* Peek at the next token. */
17355 token = cp_lexer_peek_token (parser->lexer);
17356 /* If it's a `}', or EOF then we've seen all the members. */
17357 if (token->type == CPP_CLOSE_BRACE
17358 || token->type == CPP_EOF
17359 || token->type == CPP_PRAGMA_EOL)
17360 break;
17362 /* See if this token is a keyword. */
17363 keyword = token->keyword;
17364 switch (keyword)
17366 case RID_PUBLIC:
17367 case RID_PROTECTED:
17368 case RID_PRIVATE:
17369 /* Consume the access-specifier. */
17370 cp_lexer_consume_token (parser->lexer);
17371 /* Remember which access-specifier is active. */
17372 current_access_specifier = token->u.value;
17373 /* Look for the `:'. */
17374 cp_parser_require (parser, CPP_COLON, RT_COLON);
17375 break;
17377 default:
17378 /* Accept #pragmas at class scope. */
17379 if (token->type == CPP_PRAGMA)
17381 cp_parser_pragma (parser, pragma_external);
17382 break;
17385 /* Otherwise, the next construction must be a
17386 member-declaration. */
17387 cp_parser_member_declaration (parser);
17392 /* Parse a member-declaration.
17394 member-declaration:
17395 decl-specifier-seq [opt] member-declarator-list [opt] ;
17396 function-definition ; [opt]
17397 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17398 using-declaration
17399 template-declaration
17401 member-declarator-list:
17402 member-declarator
17403 member-declarator-list , member-declarator
17405 member-declarator:
17406 declarator pure-specifier [opt]
17407 declarator constant-initializer [opt]
17408 identifier [opt] : constant-expression
17410 GNU Extensions:
17412 member-declaration:
17413 __extension__ member-declaration
17415 member-declarator:
17416 declarator attributes [opt] pure-specifier [opt]
17417 declarator attributes [opt] constant-initializer [opt]
17418 identifier [opt] attributes [opt] : constant-expression
17420 C++0x Extensions:
17422 member-declaration:
17423 static_assert-declaration */
17425 static void
17426 cp_parser_member_declaration (cp_parser* parser)
17428 cp_decl_specifier_seq decl_specifiers;
17429 tree prefix_attributes;
17430 tree decl;
17431 int declares_class_or_enum;
17432 bool friend_p;
17433 cp_token *token = NULL;
17434 cp_token *decl_spec_token_start = NULL;
17435 cp_token *initializer_token_start = NULL;
17436 int saved_pedantic;
17437 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17439 /* Check for the `__extension__' keyword. */
17440 if (cp_parser_extension_opt (parser, &saved_pedantic))
17442 /* Recurse. */
17443 cp_parser_member_declaration (parser);
17444 /* Restore the old value of the PEDANTIC flag. */
17445 pedantic = saved_pedantic;
17447 return;
17450 /* Check for a template-declaration. */
17451 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17453 /* An explicit specialization here is an error condition, and we
17454 expect the specialization handler to detect and report this. */
17455 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17456 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17457 cp_parser_explicit_specialization (parser);
17458 else
17459 cp_parser_template_declaration (parser, /*member_p=*/true);
17461 return;
17464 /* Check for a using-declaration. */
17465 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17467 /* Parse the using-declaration. */
17468 cp_parser_using_declaration (parser,
17469 /*access_declaration_p=*/false);
17470 return;
17473 /* Check for @defs. */
17474 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17476 tree ivar, member;
17477 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17478 ivar = ivar_chains;
17479 while (ivar)
17481 member = ivar;
17482 ivar = TREE_CHAIN (member);
17483 TREE_CHAIN (member) = NULL_TREE;
17484 finish_member_declaration (member);
17486 return;
17489 /* If the next token is `static_assert' we have a static assertion. */
17490 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17492 cp_parser_static_assert (parser, /*member_p=*/true);
17493 return;
17496 parser->colon_corrects_to_scope_p = false;
17498 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17499 goto out;
17501 /* Parse the decl-specifier-seq. */
17502 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17503 cp_parser_decl_specifier_seq (parser,
17504 CP_PARSER_FLAGS_OPTIONAL,
17505 &decl_specifiers,
17506 &declares_class_or_enum);
17507 prefix_attributes = decl_specifiers.attributes;
17508 decl_specifiers.attributes = NULL_TREE;
17509 /* Check for an invalid type-name. */
17510 if (!decl_specifiers.any_type_specifiers_p
17511 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17512 goto out;
17513 /* If there is no declarator, then the decl-specifier-seq should
17514 specify a type. */
17515 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17517 /* If there was no decl-specifier-seq, and the next token is a
17518 `;', then we have something like:
17520 struct S { ; };
17522 [class.mem]
17524 Each member-declaration shall declare at least one member
17525 name of the class. */
17526 if (!decl_specifiers.any_specifiers_p)
17528 cp_token *token = cp_lexer_peek_token (parser->lexer);
17529 if (!in_system_header_at (token->location))
17530 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17532 else
17534 tree type;
17536 /* See if this declaration is a friend. */
17537 friend_p = cp_parser_friend_p (&decl_specifiers);
17538 /* If there were decl-specifiers, check to see if there was
17539 a class-declaration. */
17540 type = check_tag_decl (&decl_specifiers);
17541 /* Nested classes have already been added to the class, but
17542 a `friend' needs to be explicitly registered. */
17543 if (friend_p)
17545 /* If the `friend' keyword was present, the friend must
17546 be introduced with a class-key. */
17547 if (!declares_class_or_enum)
17548 error_at (decl_spec_token_start->location,
17549 "a class-key must be used when declaring a friend");
17550 /* In this case:
17552 template <typename T> struct A {
17553 friend struct A<T>::B;
17556 A<T>::B will be represented by a TYPENAME_TYPE, and
17557 therefore not recognized by check_tag_decl. */
17558 if (!type
17559 && decl_specifiers.type
17560 && TYPE_P (decl_specifiers.type))
17561 type = decl_specifiers.type;
17562 if (!type || !TYPE_P (type))
17563 error_at (decl_spec_token_start->location,
17564 "friend declaration does not name a class or "
17565 "function");
17566 else
17567 make_friend_class (current_class_type, type,
17568 /*complain=*/true);
17570 /* If there is no TYPE, an error message will already have
17571 been issued. */
17572 else if (!type || type == error_mark_node)
17574 /* An anonymous aggregate has to be handled specially; such
17575 a declaration really declares a data member (with a
17576 particular type), as opposed to a nested class. */
17577 else if (ANON_AGGR_TYPE_P (type))
17579 /* Remove constructors and such from TYPE, now that we
17580 know it is an anonymous aggregate. */
17581 fixup_anonymous_aggr (type);
17582 /* And make the corresponding data member. */
17583 decl = build_decl (decl_spec_token_start->location,
17584 FIELD_DECL, NULL_TREE, type);
17585 /* Add it to the class. */
17586 finish_member_declaration (decl);
17588 else
17589 cp_parser_check_access_in_redeclaration
17590 (TYPE_NAME (type),
17591 decl_spec_token_start->location);
17594 else
17596 bool assume_semicolon = false;
17598 /* See if these declarations will be friends. */
17599 friend_p = cp_parser_friend_p (&decl_specifiers);
17601 /* Keep going until we hit the `;' at the end of the
17602 declaration. */
17603 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17605 tree attributes = NULL_TREE;
17606 tree first_attribute;
17608 /* Peek at the next token. */
17609 token = cp_lexer_peek_token (parser->lexer);
17611 /* Check for a bitfield declaration. */
17612 if (token->type == CPP_COLON
17613 || (token->type == CPP_NAME
17614 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17615 == CPP_COLON))
17617 tree identifier;
17618 tree width;
17620 /* Get the name of the bitfield. Note that we cannot just
17621 check TOKEN here because it may have been invalidated by
17622 the call to cp_lexer_peek_nth_token above. */
17623 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17624 identifier = cp_parser_identifier (parser);
17625 else
17626 identifier = NULL_TREE;
17628 /* Consume the `:' token. */
17629 cp_lexer_consume_token (parser->lexer);
17630 /* Get the width of the bitfield. */
17631 width
17632 = cp_parser_constant_expression (parser,
17633 /*allow_non_constant=*/false,
17634 NULL);
17636 /* Look for attributes that apply to the bitfield. */
17637 attributes = cp_parser_attributes_opt (parser);
17638 /* Remember which attributes are prefix attributes and
17639 which are not. */
17640 first_attribute = attributes;
17641 /* Combine the attributes. */
17642 attributes = chainon (prefix_attributes, attributes);
17644 /* Create the bitfield declaration. */
17645 decl = grokbitfield (identifier
17646 ? make_id_declarator (NULL_TREE,
17647 identifier,
17648 sfk_none)
17649 : NULL,
17650 &decl_specifiers,
17651 width,
17652 attributes);
17654 else
17656 cp_declarator *declarator;
17657 tree initializer;
17658 tree asm_specification;
17659 int ctor_dtor_or_conv_p;
17661 /* Parse the declarator. */
17662 declarator
17663 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17664 &ctor_dtor_or_conv_p,
17665 /*parenthesized_p=*/NULL,
17666 /*member_p=*/true);
17668 /* If something went wrong parsing the declarator, make sure
17669 that we at least consume some tokens. */
17670 if (declarator == cp_error_declarator)
17672 /* Skip to the end of the statement. */
17673 cp_parser_skip_to_end_of_statement (parser);
17674 /* If the next token is not a semicolon, that is
17675 probably because we just skipped over the body of
17676 a function. So, we consume a semicolon if
17677 present, but do not issue an error message if it
17678 is not present. */
17679 if (cp_lexer_next_token_is (parser->lexer,
17680 CPP_SEMICOLON))
17681 cp_lexer_consume_token (parser->lexer);
17682 goto out;
17685 if (declares_class_or_enum & 2)
17686 cp_parser_check_for_definition_in_return_type
17687 (declarator, decl_specifiers.type,
17688 decl_specifiers.type_location);
17690 /* Look for an asm-specification. */
17691 asm_specification = cp_parser_asm_specification_opt (parser);
17692 /* Look for attributes that apply to the declaration. */
17693 attributes = cp_parser_attributes_opt (parser);
17694 /* Remember which attributes are prefix attributes and
17695 which are not. */
17696 first_attribute = attributes;
17697 /* Combine the attributes. */
17698 attributes = chainon (prefix_attributes, attributes);
17700 /* If it's an `=', then we have a constant-initializer or a
17701 pure-specifier. It is not correct to parse the
17702 initializer before registering the member declaration
17703 since the member declaration should be in scope while
17704 its initializer is processed. However, the rest of the
17705 front end does not yet provide an interface that allows
17706 us to handle this correctly. */
17707 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17709 /* In [class.mem]:
17711 A pure-specifier shall be used only in the declaration of
17712 a virtual function.
17714 A member-declarator can contain a constant-initializer
17715 only if it declares a static member of integral or
17716 enumeration type.
17718 Therefore, if the DECLARATOR is for a function, we look
17719 for a pure-specifier; otherwise, we look for a
17720 constant-initializer. When we call `grokfield', it will
17721 perform more stringent semantics checks. */
17722 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17723 if (function_declarator_p (declarator))
17724 initializer = cp_parser_pure_specifier (parser);
17725 else
17726 /* Parse the initializer. */
17727 initializer = cp_parser_constant_initializer (parser);
17729 /* Otherwise, there is no initializer. */
17730 else
17731 initializer = NULL_TREE;
17733 /* See if we are probably looking at a function
17734 definition. We are certainly not looking at a
17735 member-declarator. Calling `grokfield' has
17736 side-effects, so we must not do it unless we are sure
17737 that we are looking at a member-declarator. */
17738 if (cp_parser_token_starts_function_definition_p
17739 (cp_lexer_peek_token (parser->lexer)))
17741 /* The grammar does not allow a pure-specifier to be
17742 used when a member function is defined. (It is
17743 possible that this fact is an oversight in the
17744 standard, since a pure function may be defined
17745 outside of the class-specifier. */
17746 if (initializer)
17747 error_at (initializer_token_start->location,
17748 "pure-specifier on function-definition");
17749 decl = cp_parser_save_member_function_body (parser,
17750 &decl_specifiers,
17751 declarator,
17752 attributes);
17753 /* If the member was not a friend, declare it here. */
17754 if (!friend_p)
17755 finish_member_declaration (decl);
17756 /* Peek at the next token. */
17757 token = cp_lexer_peek_token (parser->lexer);
17758 /* If the next token is a semicolon, consume it. */
17759 if (token->type == CPP_SEMICOLON)
17760 cp_lexer_consume_token (parser->lexer);
17761 goto out;
17763 else
17764 if (declarator->kind == cdk_function)
17765 declarator->id_loc = token->location;
17766 /* Create the declaration. */
17767 decl = grokfield (declarator, &decl_specifiers,
17768 initializer, /*init_const_expr_p=*/true,
17769 asm_specification,
17770 attributes);
17773 /* Reset PREFIX_ATTRIBUTES. */
17774 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17775 attributes = TREE_CHAIN (attributes);
17776 if (attributes)
17777 TREE_CHAIN (attributes) = NULL_TREE;
17779 /* If there is any qualification still in effect, clear it
17780 now; we will be starting fresh with the next declarator. */
17781 parser->scope = NULL_TREE;
17782 parser->qualifying_scope = NULL_TREE;
17783 parser->object_scope = NULL_TREE;
17784 /* If it's a `,', then there are more declarators. */
17785 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17786 cp_lexer_consume_token (parser->lexer);
17787 /* If the next token isn't a `;', then we have a parse error. */
17788 else if (cp_lexer_next_token_is_not (parser->lexer,
17789 CPP_SEMICOLON))
17791 /* The next token might be a ways away from where the
17792 actual semicolon is missing. Find the previous token
17793 and use that for our error position. */
17794 cp_token *token = cp_lexer_previous_token (parser->lexer);
17795 error_at (token->location,
17796 "expected %<;%> at end of member declaration");
17798 /* Assume that the user meant to provide a semicolon. If
17799 we were to cp_parser_skip_to_end_of_statement, we might
17800 skip to a semicolon inside a member function definition
17801 and issue nonsensical error messages. */
17802 assume_semicolon = true;
17805 if (decl)
17807 /* Add DECL to the list of members. */
17808 if (!friend_p)
17809 finish_member_declaration (decl);
17811 if (TREE_CODE (decl) == FUNCTION_DECL)
17812 cp_parser_save_default_args (parser, decl);
17815 if (assume_semicolon)
17816 goto out;
17820 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17821 out:
17822 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17825 /* Parse a pure-specifier.
17827 pure-specifier:
17830 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17831 Otherwise, ERROR_MARK_NODE is returned. */
17833 static tree
17834 cp_parser_pure_specifier (cp_parser* parser)
17836 cp_token *token;
17838 /* Look for the `=' token. */
17839 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17840 return error_mark_node;
17841 /* Look for the `0' token. */
17842 token = cp_lexer_peek_token (parser->lexer);
17844 if (token->type == CPP_EOF
17845 || token->type == CPP_PRAGMA_EOL)
17846 return error_mark_node;
17848 cp_lexer_consume_token (parser->lexer);
17850 /* Accept = default or = delete in c++0x mode. */
17851 if (token->keyword == RID_DEFAULT
17852 || token->keyword == RID_DELETE)
17854 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17855 return token->u.value;
17858 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17859 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17861 cp_parser_error (parser,
17862 "invalid pure specifier (only %<= 0%> is allowed)");
17863 cp_parser_skip_to_end_of_statement (parser);
17864 return error_mark_node;
17866 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
17868 error_at (token->location, "templates may not be %<virtual%>");
17869 return error_mark_node;
17872 return integer_zero_node;
17875 /* Parse a constant-initializer.
17877 constant-initializer:
17878 = constant-expression
17880 Returns a representation of the constant-expression. */
17882 static tree
17883 cp_parser_constant_initializer (cp_parser* parser)
17885 /* Look for the `=' token. */
17886 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17887 return error_mark_node;
17889 /* It is invalid to write:
17891 struct S { static const int i = { 7 }; };
17894 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17896 cp_parser_error (parser,
17897 "a brace-enclosed initializer is not allowed here");
17898 /* Consume the opening brace. */
17899 cp_lexer_consume_token (parser->lexer);
17900 /* Skip the initializer. */
17901 cp_parser_skip_to_closing_brace (parser);
17902 /* Look for the trailing `}'. */
17903 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17905 return error_mark_node;
17908 return cp_parser_constant_expression (parser,
17909 /*allow_non_constant=*/false,
17910 NULL);
17913 /* Derived classes [gram.class.derived] */
17915 /* Parse a base-clause.
17917 base-clause:
17918 : base-specifier-list
17920 base-specifier-list:
17921 base-specifier ... [opt]
17922 base-specifier-list , base-specifier ... [opt]
17924 Returns a TREE_LIST representing the base-classes, in the order in
17925 which they were declared. The representation of each node is as
17926 described by cp_parser_base_specifier.
17928 In the case that no bases are specified, this function will return
17929 NULL_TREE, not ERROR_MARK_NODE. */
17931 static tree
17932 cp_parser_base_clause (cp_parser* parser)
17934 tree bases = NULL_TREE;
17936 /* Look for the `:' that begins the list. */
17937 cp_parser_require (parser, CPP_COLON, RT_COLON);
17939 /* Scan the base-specifier-list. */
17940 while (true)
17942 cp_token *token;
17943 tree base;
17944 bool pack_expansion_p = false;
17946 /* Look for the base-specifier. */
17947 base = cp_parser_base_specifier (parser);
17948 /* Look for the (optional) ellipsis. */
17949 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17951 /* Consume the `...'. */
17952 cp_lexer_consume_token (parser->lexer);
17954 pack_expansion_p = true;
17957 /* Add BASE to the front of the list. */
17958 if (base != error_mark_node)
17960 if (pack_expansion_p)
17961 /* Make this a pack expansion type. */
17962 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17965 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17967 TREE_CHAIN (base) = bases;
17968 bases = base;
17971 /* Peek at the next token. */
17972 token = cp_lexer_peek_token (parser->lexer);
17973 /* If it's not a comma, then the list is complete. */
17974 if (token->type != CPP_COMMA)
17975 break;
17976 /* Consume the `,'. */
17977 cp_lexer_consume_token (parser->lexer);
17980 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17981 base class had a qualified name. However, the next name that
17982 appears is certainly not qualified. */
17983 parser->scope = NULL_TREE;
17984 parser->qualifying_scope = NULL_TREE;
17985 parser->object_scope = NULL_TREE;
17987 return nreverse (bases);
17990 /* Parse a base-specifier.
17992 base-specifier:
17993 :: [opt] nested-name-specifier [opt] class-name
17994 virtual access-specifier [opt] :: [opt] nested-name-specifier
17995 [opt] class-name
17996 access-specifier virtual [opt] :: [opt] nested-name-specifier
17997 [opt] class-name
17999 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18000 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18001 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18002 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18004 static tree
18005 cp_parser_base_specifier (cp_parser* parser)
18007 cp_token *token;
18008 bool done = false;
18009 bool virtual_p = false;
18010 bool duplicate_virtual_error_issued_p = false;
18011 bool duplicate_access_error_issued_p = false;
18012 bool class_scope_p, template_p;
18013 tree access = access_default_node;
18014 tree type;
18016 /* Process the optional `virtual' and `access-specifier'. */
18017 while (!done)
18019 /* Peek at the next token. */
18020 token = cp_lexer_peek_token (parser->lexer);
18021 /* Process `virtual'. */
18022 switch (token->keyword)
18024 case RID_VIRTUAL:
18025 /* If `virtual' appears more than once, issue an error. */
18026 if (virtual_p && !duplicate_virtual_error_issued_p)
18028 cp_parser_error (parser,
18029 "%<virtual%> specified more than once in base-specified");
18030 duplicate_virtual_error_issued_p = true;
18033 virtual_p = true;
18035 /* Consume the `virtual' token. */
18036 cp_lexer_consume_token (parser->lexer);
18038 break;
18040 case RID_PUBLIC:
18041 case RID_PROTECTED:
18042 case RID_PRIVATE:
18043 /* If more than one access specifier appears, issue an
18044 error. */
18045 if (access != access_default_node
18046 && !duplicate_access_error_issued_p)
18048 cp_parser_error (parser,
18049 "more than one access specifier in base-specified");
18050 duplicate_access_error_issued_p = true;
18053 access = ridpointers[(int) token->keyword];
18055 /* Consume the access-specifier. */
18056 cp_lexer_consume_token (parser->lexer);
18058 break;
18060 default:
18061 done = true;
18062 break;
18065 /* It is not uncommon to see programs mechanically, erroneously, use
18066 the 'typename' keyword to denote (dependent) qualified types
18067 as base classes. */
18068 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18070 token = cp_lexer_peek_token (parser->lexer);
18071 if (!processing_template_decl)
18072 error_at (token->location,
18073 "keyword %<typename%> not allowed outside of templates");
18074 else
18075 error_at (token->location,
18076 "keyword %<typename%> not allowed in this context "
18077 "(the base class is implicitly a type)");
18078 cp_lexer_consume_token (parser->lexer);
18081 /* Look for the optional `::' operator. */
18082 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18083 /* Look for the nested-name-specifier. The simplest way to
18084 implement:
18086 [temp.res]
18088 The keyword `typename' is not permitted in a base-specifier or
18089 mem-initializer; in these contexts a qualified name that
18090 depends on a template-parameter is implicitly assumed to be a
18091 type name.
18093 is to pretend that we have seen the `typename' keyword at this
18094 point. */
18095 cp_parser_nested_name_specifier_opt (parser,
18096 /*typename_keyword_p=*/true,
18097 /*check_dependency_p=*/true,
18098 typename_type,
18099 /*is_declaration=*/true);
18100 /* If the base class is given by a qualified name, assume that names
18101 we see are type names or templates, as appropriate. */
18102 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18103 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18105 /* Finally, look for the class-name. */
18106 type = cp_parser_class_name (parser,
18107 class_scope_p,
18108 template_p,
18109 typename_type,
18110 /*check_dependency_p=*/true,
18111 /*class_head_p=*/false,
18112 /*is_declaration=*/true);
18114 if (type == error_mark_node)
18115 return error_mark_node;
18117 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18120 /* Exception handling [gram.exception] */
18122 /* Parse an (optional) exception-specification.
18124 exception-specification:
18125 throw ( type-id-list [opt] )
18127 Returns a TREE_LIST representing the exception-specification. The
18128 TREE_VALUE of each node is a type. */
18130 static tree
18131 cp_parser_exception_specification_opt (cp_parser* parser)
18133 cp_token *token;
18134 tree type_id_list;
18135 const char *saved_message;
18137 /* Peek at the next token. */
18138 token = cp_lexer_peek_token (parser->lexer);
18140 /* Is it a noexcept-specification? */
18141 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18143 tree expr;
18144 cp_lexer_consume_token (parser->lexer);
18146 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18148 cp_lexer_consume_token (parser->lexer);
18150 /* Types may not be defined in an exception-specification. */
18151 saved_message = parser->type_definition_forbidden_message;
18152 parser->type_definition_forbidden_message
18153 = G_("types may not be defined in an exception-specification");
18155 expr = cp_parser_constant_expression (parser, false, NULL);
18157 /* Restore the saved message. */
18158 parser->type_definition_forbidden_message = saved_message;
18160 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18162 else
18163 expr = boolean_true_node;
18165 return build_noexcept_spec (expr, tf_warning_or_error);
18168 /* If it's not `throw', then there's no exception-specification. */
18169 if (!cp_parser_is_keyword (token, RID_THROW))
18170 return NULL_TREE;
18172 #if 0
18173 /* Enable this once a lot of code has transitioned to noexcept? */
18174 if (cxx_dialect == cxx0x && !in_system_header)
18175 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18176 "deprecated in C++0x; use %<noexcept%> instead");
18177 #endif
18179 /* Consume the `throw'. */
18180 cp_lexer_consume_token (parser->lexer);
18182 /* Look for the `('. */
18183 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18185 /* Peek at the next token. */
18186 token = cp_lexer_peek_token (parser->lexer);
18187 /* If it's not a `)', then there is a type-id-list. */
18188 if (token->type != CPP_CLOSE_PAREN)
18190 /* Types may not be defined in an exception-specification. */
18191 saved_message = parser->type_definition_forbidden_message;
18192 parser->type_definition_forbidden_message
18193 = G_("types may not be defined in an exception-specification");
18194 /* Parse the type-id-list. */
18195 type_id_list = cp_parser_type_id_list (parser);
18196 /* Restore the saved message. */
18197 parser->type_definition_forbidden_message = saved_message;
18199 else
18200 type_id_list = empty_except_spec;
18202 /* Look for the `)'. */
18203 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18205 return type_id_list;
18208 /* Parse an (optional) type-id-list.
18210 type-id-list:
18211 type-id ... [opt]
18212 type-id-list , type-id ... [opt]
18214 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18215 in the order that the types were presented. */
18217 static tree
18218 cp_parser_type_id_list (cp_parser* parser)
18220 tree types = NULL_TREE;
18222 while (true)
18224 cp_token *token;
18225 tree type;
18227 /* Get the next type-id. */
18228 type = cp_parser_type_id (parser);
18229 /* Parse the optional ellipsis. */
18230 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18232 /* Consume the `...'. */
18233 cp_lexer_consume_token (parser->lexer);
18235 /* Turn the type into a pack expansion expression. */
18236 type = make_pack_expansion (type);
18238 /* Add it to the list. */
18239 types = add_exception_specifier (types, type, /*complain=*/1);
18240 /* Peek at the next token. */
18241 token = cp_lexer_peek_token (parser->lexer);
18242 /* If it is not a `,', we are done. */
18243 if (token->type != CPP_COMMA)
18244 break;
18245 /* Consume the `,'. */
18246 cp_lexer_consume_token (parser->lexer);
18249 return nreverse (types);
18252 /* Parse a try-block.
18254 try-block:
18255 try compound-statement handler-seq */
18257 static tree
18258 cp_parser_try_block (cp_parser* parser)
18260 tree try_block;
18262 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18263 try_block = begin_try_block ();
18264 cp_parser_compound_statement (parser, NULL, true, false);
18265 finish_try_block (try_block);
18266 cp_parser_handler_seq (parser);
18267 finish_handler_sequence (try_block);
18269 return try_block;
18272 /* Parse a function-try-block.
18274 function-try-block:
18275 try ctor-initializer [opt] function-body handler-seq */
18277 static bool
18278 cp_parser_function_try_block (cp_parser* parser)
18280 tree compound_stmt;
18281 tree try_block;
18282 bool ctor_initializer_p;
18284 /* Look for the `try' keyword. */
18285 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18286 return false;
18287 /* Let the rest of the front end know where we are. */
18288 try_block = begin_function_try_block (&compound_stmt);
18289 /* Parse the function-body. */
18290 ctor_initializer_p
18291 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18292 /* We're done with the `try' part. */
18293 finish_function_try_block (try_block);
18294 /* Parse the handlers. */
18295 cp_parser_handler_seq (parser);
18296 /* We're done with the handlers. */
18297 finish_function_handler_sequence (try_block, compound_stmt);
18299 return ctor_initializer_p;
18302 /* Parse a handler-seq.
18304 handler-seq:
18305 handler handler-seq [opt] */
18307 static void
18308 cp_parser_handler_seq (cp_parser* parser)
18310 while (true)
18312 cp_token *token;
18314 /* Parse the handler. */
18315 cp_parser_handler (parser);
18316 /* Peek at the next token. */
18317 token = cp_lexer_peek_token (parser->lexer);
18318 /* If it's not `catch' then there are no more handlers. */
18319 if (!cp_parser_is_keyword (token, RID_CATCH))
18320 break;
18324 /* Parse a handler.
18326 handler:
18327 catch ( exception-declaration ) compound-statement */
18329 static void
18330 cp_parser_handler (cp_parser* parser)
18332 tree handler;
18333 tree declaration;
18335 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18336 handler = begin_handler ();
18337 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18338 declaration = cp_parser_exception_declaration (parser);
18339 finish_handler_parms (declaration, handler);
18340 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18341 cp_parser_compound_statement (parser, NULL, false, false);
18342 finish_handler (handler);
18345 /* Parse an exception-declaration.
18347 exception-declaration:
18348 type-specifier-seq declarator
18349 type-specifier-seq abstract-declarator
18350 type-specifier-seq
18353 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18354 ellipsis variant is used. */
18356 static tree
18357 cp_parser_exception_declaration (cp_parser* parser)
18359 cp_decl_specifier_seq type_specifiers;
18360 cp_declarator *declarator;
18361 const char *saved_message;
18363 /* If it's an ellipsis, it's easy to handle. */
18364 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18366 /* Consume the `...' token. */
18367 cp_lexer_consume_token (parser->lexer);
18368 return NULL_TREE;
18371 /* Types may not be defined in exception-declarations. */
18372 saved_message = parser->type_definition_forbidden_message;
18373 parser->type_definition_forbidden_message
18374 = G_("types may not be defined in exception-declarations");
18376 /* Parse the type-specifier-seq. */
18377 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18378 /*is_trailing_return=*/false,
18379 &type_specifiers);
18380 /* If it's a `)', then there is no declarator. */
18381 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18382 declarator = NULL;
18383 else
18384 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18385 /*ctor_dtor_or_conv_p=*/NULL,
18386 /*parenthesized_p=*/NULL,
18387 /*member_p=*/false);
18389 /* Restore the saved message. */
18390 parser->type_definition_forbidden_message = saved_message;
18392 if (!type_specifiers.any_specifiers_p)
18393 return error_mark_node;
18395 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18398 /* Parse a throw-expression.
18400 throw-expression:
18401 throw assignment-expression [opt]
18403 Returns a THROW_EXPR representing the throw-expression. */
18405 static tree
18406 cp_parser_throw_expression (cp_parser* parser)
18408 tree expression;
18409 cp_token* token;
18411 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18412 token = cp_lexer_peek_token (parser->lexer);
18413 /* Figure out whether or not there is an assignment-expression
18414 following the "throw" keyword. */
18415 if (token->type == CPP_COMMA
18416 || token->type == CPP_SEMICOLON
18417 || token->type == CPP_CLOSE_PAREN
18418 || token->type == CPP_CLOSE_SQUARE
18419 || token->type == CPP_CLOSE_BRACE
18420 || token->type == CPP_COLON)
18421 expression = NULL_TREE;
18422 else
18423 expression = cp_parser_assignment_expression (parser,
18424 /*cast_p=*/false, NULL);
18426 return build_throw (expression);
18429 /* GNU Extensions */
18431 /* Parse an (optional) asm-specification.
18433 asm-specification:
18434 asm ( string-literal )
18436 If the asm-specification is present, returns a STRING_CST
18437 corresponding to the string-literal. Otherwise, returns
18438 NULL_TREE. */
18440 static tree
18441 cp_parser_asm_specification_opt (cp_parser* parser)
18443 cp_token *token;
18444 tree asm_specification;
18446 /* Peek at the next token. */
18447 token = cp_lexer_peek_token (parser->lexer);
18448 /* If the next token isn't the `asm' keyword, then there's no
18449 asm-specification. */
18450 if (!cp_parser_is_keyword (token, RID_ASM))
18451 return NULL_TREE;
18453 /* Consume the `asm' token. */
18454 cp_lexer_consume_token (parser->lexer);
18455 /* Look for the `('. */
18456 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18458 /* Look for the string-literal. */
18459 asm_specification = cp_parser_string_literal (parser, false, false);
18461 /* Look for the `)'. */
18462 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18464 return asm_specification;
18467 /* Parse an asm-operand-list.
18469 asm-operand-list:
18470 asm-operand
18471 asm-operand-list , asm-operand
18473 asm-operand:
18474 string-literal ( expression )
18475 [ string-literal ] string-literal ( expression )
18477 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18478 each node is the expression. The TREE_PURPOSE is itself a
18479 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18480 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18481 is a STRING_CST for the string literal before the parenthesis. Returns
18482 ERROR_MARK_NODE if any of the operands are invalid. */
18484 static tree
18485 cp_parser_asm_operand_list (cp_parser* parser)
18487 tree asm_operands = NULL_TREE;
18488 bool invalid_operands = false;
18490 while (true)
18492 tree string_literal;
18493 tree expression;
18494 tree name;
18496 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18498 /* Consume the `[' token. */
18499 cp_lexer_consume_token (parser->lexer);
18500 /* Read the operand name. */
18501 name = cp_parser_identifier (parser);
18502 if (name != error_mark_node)
18503 name = build_string (IDENTIFIER_LENGTH (name),
18504 IDENTIFIER_POINTER (name));
18505 /* Look for the closing `]'. */
18506 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18508 else
18509 name = NULL_TREE;
18510 /* Look for the string-literal. */
18511 string_literal = cp_parser_string_literal (parser, false, false);
18513 /* Look for the `('. */
18514 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18515 /* Parse the expression. */
18516 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18517 /* Look for the `)'. */
18518 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18520 if (name == error_mark_node
18521 || string_literal == error_mark_node
18522 || expression == error_mark_node)
18523 invalid_operands = true;
18525 /* Add this operand to the list. */
18526 asm_operands = tree_cons (build_tree_list (name, string_literal),
18527 expression,
18528 asm_operands);
18529 /* If the next token is not a `,', there are no more
18530 operands. */
18531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18532 break;
18533 /* Consume the `,'. */
18534 cp_lexer_consume_token (parser->lexer);
18537 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18540 /* Parse an asm-clobber-list.
18542 asm-clobber-list:
18543 string-literal
18544 asm-clobber-list , string-literal
18546 Returns a TREE_LIST, indicating the clobbers in the order that they
18547 appeared. The TREE_VALUE of each node is a STRING_CST. */
18549 static tree
18550 cp_parser_asm_clobber_list (cp_parser* parser)
18552 tree clobbers = NULL_TREE;
18554 while (true)
18556 tree string_literal;
18558 /* Look for the string literal. */
18559 string_literal = cp_parser_string_literal (parser, false, false);
18560 /* Add it to the list. */
18561 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18562 /* If the next token is not a `,', then the list is
18563 complete. */
18564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18565 break;
18566 /* Consume the `,' token. */
18567 cp_lexer_consume_token (parser->lexer);
18570 return clobbers;
18573 /* Parse an asm-label-list.
18575 asm-label-list:
18576 identifier
18577 asm-label-list , identifier
18579 Returns a TREE_LIST, indicating the labels in the order that they
18580 appeared. The TREE_VALUE of each node is a label. */
18582 static tree
18583 cp_parser_asm_label_list (cp_parser* parser)
18585 tree labels = NULL_TREE;
18587 while (true)
18589 tree identifier, label, name;
18591 /* Look for the identifier. */
18592 identifier = cp_parser_identifier (parser);
18593 if (!error_operand_p (identifier))
18595 label = lookup_label (identifier);
18596 if (TREE_CODE (label) == LABEL_DECL)
18598 TREE_USED (label) = 1;
18599 check_goto (label);
18600 name = build_string (IDENTIFIER_LENGTH (identifier),
18601 IDENTIFIER_POINTER (identifier));
18602 labels = tree_cons (name, label, labels);
18605 /* If the next token is not a `,', then the list is
18606 complete. */
18607 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18608 break;
18609 /* Consume the `,' token. */
18610 cp_lexer_consume_token (parser->lexer);
18613 return nreverse (labels);
18616 /* Parse an (optional) series of attributes.
18618 attributes:
18619 attributes attribute
18621 attribute:
18622 __attribute__ (( attribute-list [opt] ))
18624 The return value is as for cp_parser_attribute_list. */
18626 static tree
18627 cp_parser_attributes_opt (cp_parser* parser)
18629 tree attributes = NULL_TREE;
18631 while (true)
18633 cp_token *token;
18634 tree attribute_list;
18636 /* Peek at the next token. */
18637 token = cp_lexer_peek_token (parser->lexer);
18638 /* If it's not `__attribute__', then we're done. */
18639 if (token->keyword != RID_ATTRIBUTE)
18640 break;
18642 /* Consume the `__attribute__' keyword. */
18643 cp_lexer_consume_token (parser->lexer);
18644 /* Look for the two `(' tokens. */
18645 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18646 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18648 /* Peek at the next token. */
18649 token = cp_lexer_peek_token (parser->lexer);
18650 if (token->type != CPP_CLOSE_PAREN)
18651 /* Parse the attribute-list. */
18652 attribute_list = cp_parser_attribute_list (parser);
18653 else
18654 /* If the next token is a `)', then there is no attribute
18655 list. */
18656 attribute_list = NULL;
18658 /* Look for the two `)' tokens. */
18659 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18660 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18662 /* Add these new attributes to the list. */
18663 attributes = chainon (attributes, attribute_list);
18666 return attributes;
18669 /* Parse an attribute-list.
18671 attribute-list:
18672 attribute
18673 attribute-list , attribute
18675 attribute:
18676 identifier
18677 identifier ( identifier )
18678 identifier ( identifier , expression-list )
18679 identifier ( expression-list )
18681 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18682 to an attribute. The TREE_PURPOSE of each node is the identifier
18683 indicating which attribute is in use. The TREE_VALUE represents
18684 the arguments, if any. */
18686 static tree
18687 cp_parser_attribute_list (cp_parser* parser)
18689 tree attribute_list = NULL_TREE;
18690 bool save_translate_strings_p = parser->translate_strings_p;
18692 parser->translate_strings_p = false;
18693 while (true)
18695 cp_token *token;
18696 tree identifier;
18697 tree attribute;
18699 /* Look for the identifier. We also allow keywords here; for
18700 example `__attribute__ ((const))' is legal. */
18701 token = cp_lexer_peek_token (parser->lexer);
18702 if (token->type == CPP_NAME
18703 || token->type == CPP_KEYWORD)
18705 tree arguments = NULL_TREE;
18707 /* Consume the token. */
18708 token = cp_lexer_consume_token (parser->lexer);
18710 /* Save away the identifier that indicates which attribute
18711 this is. */
18712 identifier = (token->type == CPP_KEYWORD)
18713 /* For keywords, use the canonical spelling, not the
18714 parsed identifier. */
18715 ? ridpointers[(int) token->keyword]
18716 : token->u.value;
18718 attribute = build_tree_list (identifier, NULL_TREE);
18720 /* Peek at the next token. */
18721 token = cp_lexer_peek_token (parser->lexer);
18722 /* If it's an `(', then parse the attribute arguments. */
18723 if (token->type == CPP_OPEN_PAREN)
18725 VEC(tree,gc) *vec;
18726 int attr_flag = (attribute_takes_identifier_p (identifier)
18727 ? id_attr : normal_attr);
18728 vec = cp_parser_parenthesized_expression_list
18729 (parser, attr_flag, /*cast_p=*/false,
18730 /*allow_expansion_p=*/false,
18731 /*non_constant_p=*/NULL);
18732 if (vec == NULL)
18733 arguments = error_mark_node;
18734 else
18736 arguments = build_tree_list_vec (vec);
18737 release_tree_vector (vec);
18739 /* Save the arguments away. */
18740 TREE_VALUE (attribute) = arguments;
18743 if (arguments != error_mark_node)
18745 /* Add this attribute to the list. */
18746 TREE_CHAIN (attribute) = attribute_list;
18747 attribute_list = attribute;
18750 token = cp_lexer_peek_token (parser->lexer);
18752 /* Now, look for more attributes. If the next token isn't a
18753 `,', we're done. */
18754 if (token->type != CPP_COMMA)
18755 break;
18757 /* Consume the comma and keep going. */
18758 cp_lexer_consume_token (parser->lexer);
18760 parser->translate_strings_p = save_translate_strings_p;
18762 /* We built up the list in reverse order. */
18763 return nreverse (attribute_list);
18766 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18767 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18768 current value of the PEDANTIC flag, regardless of whether or not
18769 the `__extension__' keyword is present. The caller is responsible
18770 for restoring the value of the PEDANTIC flag. */
18772 static bool
18773 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18775 /* Save the old value of the PEDANTIC flag. */
18776 *saved_pedantic = pedantic;
18778 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18780 /* Consume the `__extension__' token. */
18781 cp_lexer_consume_token (parser->lexer);
18782 /* We're not being pedantic while the `__extension__' keyword is
18783 in effect. */
18784 pedantic = 0;
18786 return true;
18789 return false;
18792 /* Parse a label declaration.
18794 label-declaration:
18795 __label__ label-declarator-seq ;
18797 label-declarator-seq:
18798 identifier , label-declarator-seq
18799 identifier */
18801 static void
18802 cp_parser_label_declaration (cp_parser* parser)
18804 /* Look for the `__label__' keyword. */
18805 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18807 while (true)
18809 tree identifier;
18811 /* Look for an identifier. */
18812 identifier = cp_parser_identifier (parser);
18813 /* If we failed, stop. */
18814 if (identifier == error_mark_node)
18815 break;
18816 /* Declare it as a label. */
18817 finish_label_decl (identifier);
18818 /* If the next token is a `;', stop. */
18819 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18820 break;
18821 /* Look for the `,' separating the label declarations. */
18822 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18825 /* Look for the final `;'. */
18826 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18829 /* Support Functions */
18831 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18832 NAME should have one of the representations used for an
18833 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18834 is returned. If PARSER->SCOPE is a dependent type, then a
18835 SCOPE_REF is returned.
18837 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18838 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18839 was formed. Abstractly, such entities should not be passed to this
18840 function, because they do not need to be looked up, but it is
18841 simpler to check for this special case here, rather than at the
18842 call-sites.
18844 In cases not explicitly covered above, this function returns a
18845 DECL, OVERLOAD, or baselink representing the result of the lookup.
18846 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18847 is returned.
18849 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18850 (e.g., "struct") that was used. In that case bindings that do not
18851 refer to types are ignored.
18853 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18854 ignored.
18856 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18857 are ignored.
18859 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18860 types.
18862 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18863 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18864 NULL_TREE otherwise. */
18866 static tree
18867 cp_parser_lookup_name (cp_parser *parser, tree name,
18868 enum tag_types tag_type,
18869 bool is_template,
18870 bool is_namespace,
18871 bool check_dependency,
18872 tree *ambiguous_decls,
18873 location_t name_location)
18875 int flags = 0;
18876 tree decl;
18877 tree object_type = parser->context->object_type;
18879 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18880 flags |= LOOKUP_COMPLAIN;
18882 /* Assume that the lookup will be unambiguous. */
18883 if (ambiguous_decls)
18884 *ambiguous_decls = NULL_TREE;
18886 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
18887 no longer valid. Note that if we are parsing tentatively, and
18888 the parse fails, OBJECT_TYPE will be automatically restored. */
18889 parser->context->object_type = NULL_TREE;
18891 if (name == error_mark_node)
18892 return error_mark_node;
18894 /* A template-id has already been resolved; there is no lookup to
18895 do. */
18896 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
18897 return name;
18898 if (BASELINK_P (name))
18900 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
18901 == TEMPLATE_ID_EXPR);
18902 return name;
18905 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
18906 it should already have been checked to make sure that the name
18907 used matches the type being destroyed. */
18908 if (TREE_CODE (name) == BIT_NOT_EXPR)
18910 tree type;
18912 /* Figure out to which type this destructor applies. */
18913 if (parser->scope)
18914 type = parser->scope;
18915 else if (object_type)
18916 type = object_type;
18917 else
18918 type = current_class_type;
18919 /* If that's not a class type, there is no destructor. */
18920 if (!type || !CLASS_TYPE_P (type))
18921 return error_mark_node;
18922 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
18923 lazily_declare_fn (sfk_destructor, type);
18924 if (!CLASSTYPE_DESTRUCTORS (type))
18925 return error_mark_node;
18926 /* If it was a class type, return the destructor. */
18927 return CLASSTYPE_DESTRUCTORS (type);
18930 /* By this point, the NAME should be an ordinary identifier. If
18931 the id-expression was a qualified name, the qualifying scope is
18932 stored in PARSER->SCOPE at this point. */
18933 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
18935 /* Perform the lookup. */
18936 if (parser->scope)
18938 bool dependent_p;
18940 if (parser->scope == error_mark_node)
18941 return error_mark_node;
18943 /* If the SCOPE is dependent, the lookup must be deferred until
18944 the template is instantiated -- unless we are explicitly
18945 looking up names in uninstantiated templates. Even then, we
18946 cannot look up the name if the scope is not a class type; it
18947 might, for example, be a template type parameter. */
18948 dependent_p = (TYPE_P (parser->scope)
18949 && dependent_scope_p (parser->scope));
18950 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
18951 && dependent_p)
18952 /* Defer lookup. */
18953 decl = error_mark_node;
18954 else
18956 tree pushed_scope = NULL_TREE;
18958 /* If PARSER->SCOPE is a dependent type, then it must be a
18959 class type, and we must not be checking dependencies;
18960 otherwise, we would have processed this lookup above. So
18961 that PARSER->SCOPE is not considered a dependent base by
18962 lookup_member, we must enter the scope here. */
18963 if (dependent_p)
18964 pushed_scope = push_scope (parser->scope);
18966 /* If the PARSER->SCOPE is a template specialization, it
18967 may be instantiated during name lookup. In that case,
18968 errors may be issued. Even if we rollback the current
18969 tentative parse, those errors are valid. */
18970 decl = lookup_qualified_name (parser->scope, name,
18971 tag_type != none_type,
18972 /*complain=*/true);
18974 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18975 lookup result and the nested-name-specifier nominates a class C:
18976 * if the name specified after the nested-name-specifier, when
18977 looked up in C, is the injected-class-name of C (Clause 9), or
18978 * if the name specified after the nested-name-specifier is the
18979 same as the identifier or the simple-template-id's template-
18980 name in the last component of the nested-name-specifier,
18981 the name is instead considered to name the constructor of
18982 class C. [ Note: for example, the constructor is not an
18983 acceptable lookup result in an elaborated-type-specifier so
18984 the constructor would not be used in place of the
18985 injected-class-name. --end note ] Such a constructor name
18986 shall be used only in the declarator-id of a declaration that
18987 names a constructor or in a using-declaration. */
18988 if (tag_type == none_type
18989 && DECL_SELF_REFERENCE_P (decl)
18990 && same_type_p (DECL_CONTEXT (decl), parser->scope))
18991 decl = lookup_qualified_name (parser->scope, ctor_identifier,
18992 tag_type != none_type,
18993 /*complain=*/true);
18995 /* If we have a single function from a using decl, pull it out. */
18996 if (TREE_CODE (decl) == OVERLOAD
18997 && !really_overloaded_fn (decl))
18998 decl = OVL_FUNCTION (decl);
19000 if (pushed_scope)
19001 pop_scope (pushed_scope);
19004 /* If the scope is a dependent type and either we deferred lookup or
19005 we did lookup but didn't find the name, rememeber the name. */
19006 if (decl == error_mark_node && TYPE_P (parser->scope)
19007 && dependent_type_p (parser->scope))
19009 if (tag_type)
19011 tree type;
19013 /* The resolution to Core Issue 180 says that `struct
19014 A::B' should be considered a type-name, even if `A'
19015 is dependent. */
19016 type = make_typename_type (parser->scope, name, tag_type,
19017 /*complain=*/tf_error);
19018 decl = TYPE_NAME (type);
19020 else if (is_template
19021 && (cp_parser_next_token_ends_template_argument_p (parser)
19022 || cp_lexer_next_token_is (parser->lexer,
19023 CPP_CLOSE_PAREN)))
19024 decl = make_unbound_class_template (parser->scope,
19025 name, NULL_TREE,
19026 /*complain=*/tf_error);
19027 else
19028 decl = build_qualified_name (/*type=*/NULL_TREE,
19029 parser->scope, name,
19030 is_template);
19032 parser->qualifying_scope = parser->scope;
19033 parser->object_scope = NULL_TREE;
19035 else if (object_type)
19037 tree object_decl = NULL_TREE;
19038 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19039 OBJECT_TYPE is not a class. */
19040 if (CLASS_TYPE_P (object_type))
19041 /* If the OBJECT_TYPE is a template specialization, it may
19042 be instantiated during name lookup. In that case, errors
19043 may be issued. Even if we rollback the current tentative
19044 parse, those errors are valid. */
19045 object_decl = lookup_member (object_type,
19046 name,
19047 /*protect=*/0,
19048 tag_type != none_type);
19049 /* Look it up in the enclosing context, too. */
19050 decl = lookup_name_real (name, tag_type != none_type,
19051 /*nonclass=*/0,
19052 /*block_p=*/true, is_namespace, flags);
19053 parser->object_scope = object_type;
19054 parser->qualifying_scope = NULL_TREE;
19055 if (object_decl)
19056 decl = object_decl;
19058 else
19060 decl = lookup_name_real (name, tag_type != none_type,
19061 /*nonclass=*/0,
19062 /*block_p=*/true, is_namespace, flags);
19063 parser->qualifying_scope = NULL_TREE;
19064 parser->object_scope = NULL_TREE;
19067 /* If the lookup failed, let our caller know. */
19068 if (!decl || decl == error_mark_node)
19069 return error_mark_node;
19071 /* Pull out the template from an injected-class-name (or multiple). */
19072 if (is_template)
19073 decl = maybe_get_template_decl_from_type_decl (decl);
19075 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19076 if (TREE_CODE (decl) == TREE_LIST)
19078 if (ambiguous_decls)
19079 *ambiguous_decls = decl;
19080 /* The error message we have to print is too complicated for
19081 cp_parser_error, so we incorporate its actions directly. */
19082 if (!cp_parser_simulate_error (parser))
19084 error_at (name_location, "reference to %qD is ambiguous",
19085 name);
19086 print_candidates (decl);
19088 return error_mark_node;
19091 gcc_assert (DECL_P (decl)
19092 || TREE_CODE (decl) == OVERLOAD
19093 || TREE_CODE (decl) == SCOPE_REF
19094 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19095 || BASELINK_P (decl));
19097 /* If we have resolved the name of a member declaration, check to
19098 see if the declaration is accessible. When the name resolves to
19099 set of overloaded functions, accessibility is checked when
19100 overload resolution is done.
19102 During an explicit instantiation, access is not checked at all,
19103 as per [temp.explicit]. */
19104 if (DECL_P (decl))
19105 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19107 return decl;
19110 /* Like cp_parser_lookup_name, but for use in the typical case where
19111 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19112 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19114 static tree
19115 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19117 return cp_parser_lookup_name (parser, name,
19118 none_type,
19119 /*is_template=*/false,
19120 /*is_namespace=*/false,
19121 /*check_dependency=*/true,
19122 /*ambiguous_decls=*/NULL,
19123 location);
19126 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19127 the current context, return the TYPE_DECL. If TAG_NAME_P is
19128 true, the DECL indicates the class being defined in a class-head,
19129 or declared in an elaborated-type-specifier.
19131 Otherwise, return DECL. */
19133 static tree
19134 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19136 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19137 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19139 struct A {
19140 template <typename T> struct B;
19143 template <typename T> struct A::B {};
19145 Similarly, in an elaborated-type-specifier:
19147 namespace N { struct X{}; }
19149 struct A {
19150 template <typename T> friend struct N::X;
19153 However, if the DECL refers to a class type, and we are in
19154 the scope of the class, then the name lookup automatically
19155 finds the TYPE_DECL created by build_self_reference rather
19156 than a TEMPLATE_DECL. For example, in:
19158 template <class T> struct S {
19159 S s;
19162 there is no need to handle such case. */
19164 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19165 return DECL_TEMPLATE_RESULT (decl);
19167 return decl;
19170 /* If too many, or too few, template-parameter lists apply to the
19171 declarator, issue an error message. Returns TRUE if all went well,
19172 and FALSE otherwise. */
19174 static bool
19175 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19176 cp_declarator *declarator,
19177 location_t declarator_location)
19179 unsigned num_templates;
19181 /* We haven't seen any classes that involve template parameters yet. */
19182 num_templates = 0;
19184 switch (declarator->kind)
19186 case cdk_id:
19187 if (declarator->u.id.qualifying_scope)
19189 tree scope;
19191 scope = declarator->u.id.qualifying_scope;
19193 while (scope && CLASS_TYPE_P (scope))
19195 /* You're supposed to have one `template <...>'
19196 for every template class, but you don't need one
19197 for a full specialization. For example:
19199 template <class T> struct S{};
19200 template <> struct S<int> { void f(); };
19201 void S<int>::f () {}
19203 is correct; there shouldn't be a `template <>' for
19204 the definition of `S<int>::f'. */
19205 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19206 /* If SCOPE does not have template information of any
19207 kind, then it is not a template, nor is it nested
19208 within a template. */
19209 break;
19210 if (explicit_class_specialization_p (scope))
19211 break;
19212 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19213 ++num_templates;
19215 scope = TYPE_CONTEXT (scope);
19218 else if (TREE_CODE (declarator->u.id.unqualified_name)
19219 == TEMPLATE_ID_EXPR)
19220 /* If the DECLARATOR has the form `X<y>' then it uses one
19221 additional level of template parameters. */
19222 ++num_templates;
19224 return cp_parser_check_template_parameters
19225 (parser, num_templates, declarator_location, declarator);
19228 case cdk_function:
19229 case cdk_array:
19230 case cdk_pointer:
19231 case cdk_reference:
19232 case cdk_ptrmem:
19233 return (cp_parser_check_declarator_template_parameters
19234 (parser, declarator->declarator, declarator_location));
19236 case cdk_error:
19237 return true;
19239 default:
19240 gcc_unreachable ();
19242 return false;
19245 /* NUM_TEMPLATES were used in the current declaration. If that is
19246 invalid, return FALSE and issue an error messages. Otherwise,
19247 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19248 declarator and we can print more accurate diagnostics. */
19250 static bool
19251 cp_parser_check_template_parameters (cp_parser* parser,
19252 unsigned num_templates,
19253 location_t location,
19254 cp_declarator *declarator)
19256 /* If there are the same number of template classes and parameter
19257 lists, that's OK. */
19258 if (parser->num_template_parameter_lists == num_templates)
19259 return true;
19260 /* If there are more, but only one more, then we are referring to a
19261 member template. That's OK too. */
19262 if (parser->num_template_parameter_lists == num_templates + 1)
19263 return true;
19264 /* If there are more template classes than parameter lists, we have
19265 something like:
19267 template <class T> void S<T>::R<T>::f (); */
19268 if (parser->num_template_parameter_lists < num_templates)
19270 if (declarator && !current_function_decl)
19271 error_at (location, "specializing member %<%T::%E%> "
19272 "requires %<template<>%> syntax",
19273 declarator->u.id.qualifying_scope,
19274 declarator->u.id.unqualified_name);
19275 else if (declarator)
19276 error_at (location, "invalid declaration of %<%T::%E%>",
19277 declarator->u.id.qualifying_scope,
19278 declarator->u.id.unqualified_name);
19279 else
19280 error_at (location, "too few template-parameter-lists");
19281 return false;
19283 /* Otherwise, there are too many template parameter lists. We have
19284 something like:
19286 template <class T> template <class U> void S::f(); */
19287 error_at (location, "too many template-parameter-lists");
19288 return false;
19291 /* Parse an optional `::' token indicating that the following name is
19292 from the global namespace. If so, PARSER->SCOPE is set to the
19293 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19294 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19295 Returns the new value of PARSER->SCOPE, if the `::' token is
19296 present, and NULL_TREE otherwise. */
19298 static tree
19299 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19301 cp_token *token;
19303 /* Peek at the next token. */
19304 token = cp_lexer_peek_token (parser->lexer);
19305 /* If we're looking at a `::' token then we're starting from the
19306 global namespace, not our current location. */
19307 if (token->type == CPP_SCOPE)
19309 /* Consume the `::' token. */
19310 cp_lexer_consume_token (parser->lexer);
19311 /* Set the SCOPE so that we know where to start the lookup. */
19312 parser->scope = global_namespace;
19313 parser->qualifying_scope = global_namespace;
19314 parser->object_scope = NULL_TREE;
19316 return parser->scope;
19318 else if (!current_scope_valid_p)
19320 parser->scope = NULL_TREE;
19321 parser->qualifying_scope = NULL_TREE;
19322 parser->object_scope = NULL_TREE;
19325 return NULL_TREE;
19328 /* Returns TRUE if the upcoming token sequence is the start of a
19329 constructor declarator. If FRIEND_P is true, the declarator is
19330 preceded by the `friend' specifier. */
19332 static bool
19333 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19335 bool constructor_p;
19336 tree nested_name_specifier;
19337 cp_token *next_token;
19339 /* The common case is that this is not a constructor declarator, so
19340 try to avoid doing lots of work if at all possible. It's not
19341 valid declare a constructor at function scope. */
19342 if (parser->in_function_body)
19343 return false;
19344 /* And only certain tokens can begin a constructor declarator. */
19345 next_token = cp_lexer_peek_token (parser->lexer);
19346 if (next_token->type != CPP_NAME
19347 && next_token->type != CPP_SCOPE
19348 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19349 && next_token->type != CPP_TEMPLATE_ID)
19350 return false;
19352 /* Parse tentatively; we are going to roll back all of the tokens
19353 consumed here. */
19354 cp_parser_parse_tentatively (parser);
19355 /* Assume that we are looking at a constructor declarator. */
19356 constructor_p = true;
19358 /* Look for the optional `::' operator. */
19359 cp_parser_global_scope_opt (parser,
19360 /*current_scope_valid_p=*/false);
19361 /* Look for the nested-name-specifier. */
19362 nested_name_specifier
19363 = (cp_parser_nested_name_specifier_opt (parser,
19364 /*typename_keyword_p=*/false,
19365 /*check_dependency_p=*/false,
19366 /*type_p=*/false,
19367 /*is_declaration=*/false));
19368 /* Outside of a class-specifier, there must be a
19369 nested-name-specifier. */
19370 if (!nested_name_specifier &&
19371 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19372 || friend_p))
19373 constructor_p = false;
19374 else if (nested_name_specifier == error_mark_node)
19375 constructor_p = false;
19377 /* If we have a class scope, this is easy; DR 147 says that S::S always
19378 names the constructor, and no other qualified name could. */
19379 if (constructor_p && nested_name_specifier
19380 && TYPE_P (nested_name_specifier))
19382 tree id = cp_parser_unqualified_id (parser,
19383 /*template_keyword_p=*/false,
19384 /*check_dependency_p=*/false,
19385 /*declarator_p=*/true,
19386 /*optional_p=*/false);
19387 if (is_overloaded_fn (id))
19388 id = DECL_NAME (get_first_fn (id));
19389 if (!constructor_name_p (id, nested_name_specifier))
19390 constructor_p = false;
19392 /* If we still think that this might be a constructor-declarator,
19393 look for a class-name. */
19394 else if (constructor_p)
19396 /* If we have:
19398 template <typename T> struct S {
19399 S();
19402 we must recognize that the nested `S' names a class. */
19403 tree type_decl;
19404 type_decl = cp_parser_class_name (parser,
19405 /*typename_keyword_p=*/false,
19406 /*template_keyword_p=*/false,
19407 none_type,
19408 /*check_dependency_p=*/false,
19409 /*class_head_p=*/false,
19410 /*is_declaration=*/false);
19411 /* If there was no class-name, then this is not a constructor. */
19412 constructor_p = !cp_parser_error_occurred (parser);
19414 /* If we're still considering a constructor, we have to see a `(',
19415 to begin the parameter-declaration-clause, followed by either a
19416 `)', an `...', or a decl-specifier. We need to check for a
19417 type-specifier to avoid being fooled into thinking that:
19419 S (f) (int);
19421 is a constructor. (It is actually a function named `f' that
19422 takes one parameter (of type `int') and returns a value of type
19423 `S'. */
19424 if (constructor_p
19425 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19426 constructor_p = false;
19428 if (constructor_p
19429 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19430 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19431 /* A parameter declaration begins with a decl-specifier,
19432 which is either the "attribute" keyword, a storage class
19433 specifier, or (usually) a type-specifier. */
19434 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19436 tree type;
19437 tree pushed_scope = NULL_TREE;
19438 unsigned saved_num_template_parameter_lists;
19440 /* Names appearing in the type-specifier should be looked up
19441 in the scope of the class. */
19442 if (current_class_type)
19443 type = NULL_TREE;
19444 else
19446 type = TREE_TYPE (type_decl);
19447 if (TREE_CODE (type) == TYPENAME_TYPE)
19449 type = resolve_typename_type (type,
19450 /*only_current_p=*/false);
19451 if (TREE_CODE (type) == TYPENAME_TYPE)
19453 cp_parser_abort_tentative_parse (parser);
19454 return false;
19457 pushed_scope = push_scope (type);
19460 /* Inside the constructor parameter list, surrounding
19461 template-parameter-lists do not apply. */
19462 saved_num_template_parameter_lists
19463 = parser->num_template_parameter_lists;
19464 parser->num_template_parameter_lists = 0;
19466 /* Look for the type-specifier. */
19467 cp_parser_type_specifier (parser,
19468 CP_PARSER_FLAGS_NONE,
19469 /*decl_specs=*/NULL,
19470 /*is_declarator=*/true,
19471 /*declares_class_or_enum=*/NULL,
19472 /*is_cv_qualifier=*/NULL);
19474 parser->num_template_parameter_lists
19475 = saved_num_template_parameter_lists;
19477 /* Leave the scope of the class. */
19478 if (pushed_scope)
19479 pop_scope (pushed_scope);
19481 constructor_p = !cp_parser_error_occurred (parser);
19485 /* We did not really want to consume any tokens. */
19486 cp_parser_abort_tentative_parse (parser);
19488 return constructor_p;
19491 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19492 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19493 they must be performed once we are in the scope of the function.
19495 Returns the function defined. */
19497 static tree
19498 cp_parser_function_definition_from_specifiers_and_declarator
19499 (cp_parser* parser,
19500 cp_decl_specifier_seq *decl_specifiers,
19501 tree attributes,
19502 const cp_declarator *declarator)
19504 tree fn;
19505 bool success_p;
19507 /* Begin the function-definition. */
19508 success_p = start_function (decl_specifiers, declarator, attributes);
19510 /* The things we're about to see are not directly qualified by any
19511 template headers we've seen thus far. */
19512 reset_specialization ();
19514 /* If there were names looked up in the decl-specifier-seq that we
19515 did not check, check them now. We must wait until we are in the
19516 scope of the function to perform the checks, since the function
19517 might be a friend. */
19518 perform_deferred_access_checks ();
19520 if (!success_p)
19522 /* Skip the entire function. */
19523 cp_parser_skip_to_end_of_block_or_statement (parser);
19524 fn = error_mark_node;
19526 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19528 /* Seen already, skip it. An error message has already been output. */
19529 cp_parser_skip_to_end_of_block_or_statement (parser);
19530 fn = current_function_decl;
19531 current_function_decl = NULL_TREE;
19532 /* If this is a function from a class, pop the nested class. */
19533 if (current_class_name)
19534 pop_nested_class ();
19536 else
19537 fn = cp_parser_function_definition_after_declarator (parser,
19538 /*inline_p=*/false);
19540 return fn;
19543 /* Parse the part of a function-definition that follows the
19544 declarator. INLINE_P is TRUE iff this function is an inline
19545 function defined within a class-specifier.
19547 Returns the function defined. */
19549 static tree
19550 cp_parser_function_definition_after_declarator (cp_parser* parser,
19551 bool inline_p)
19553 tree fn;
19554 bool ctor_initializer_p = false;
19555 bool saved_in_unbraced_linkage_specification_p;
19556 bool saved_in_function_body;
19557 unsigned saved_num_template_parameter_lists;
19558 cp_token *token;
19560 saved_in_function_body = parser->in_function_body;
19561 parser->in_function_body = true;
19562 /* If the next token is `return', then the code may be trying to
19563 make use of the "named return value" extension that G++ used to
19564 support. */
19565 token = cp_lexer_peek_token (parser->lexer);
19566 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19568 /* Consume the `return' keyword. */
19569 cp_lexer_consume_token (parser->lexer);
19570 /* Look for the identifier that indicates what value is to be
19571 returned. */
19572 cp_parser_identifier (parser);
19573 /* Issue an error message. */
19574 error_at (token->location,
19575 "named return values are no longer supported");
19576 /* Skip tokens until we reach the start of the function body. */
19577 while (true)
19579 cp_token *token = cp_lexer_peek_token (parser->lexer);
19580 if (token->type == CPP_OPEN_BRACE
19581 || token->type == CPP_EOF
19582 || token->type == CPP_PRAGMA_EOL)
19583 break;
19584 cp_lexer_consume_token (parser->lexer);
19587 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19588 anything declared inside `f'. */
19589 saved_in_unbraced_linkage_specification_p
19590 = parser->in_unbraced_linkage_specification_p;
19591 parser->in_unbraced_linkage_specification_p = false;
19592 /* Inside the function, surrounding template-parameter-lists do not
19593 apply. */
19594 saved_num_template_parameter_lists
19595 = parser->num_template_parameter_lists;
19596 parser->num_template_parameter_lists = 0;
19598 start_lambda_scope (current_function_decl);
19600 /* If the next token is `try', then we are looking at a
19601 function-try-block. */
19602 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19603 ctor_initializer_p = cp_parser_function_try_block (parser);
19604 /* A function-try-block includes the function-body, so we only do
19605 this next part if we're not processing a function-try-block. */
19606 else
19607 ctor_initializer_p
19608 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19610 finish_lambda_scope ();
19612 /* Finish the function. */
19613 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19614 (inline_p ? 2 : 0));
19615 /* Generate code for it, if necessary. */
19616 expand_or_defer_fn (fn);
19617 /* Restore the saved values. */
19618 parser->in_unbraced_linkage_specification_p
19619 = saved_in_unbraced_linkage_specification_p;
19620 parser->num_template_parameter_lists
19621 = saved_num_template_parameter_lists;
19622 parser->in_function_body = saved_in_function_body;
19624 return fn;
19627 /* Parse a template-declaration, assuming that the `export' (and
19628 `extern') keywords, if present, has already been scanned. MEMBER_P
19629 is as for cp_parser_template_declaration. */
19631 static void
19632 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19634 tree decl = NULL_TREE;
19635 VEC (deferred_access_check,gc) *checks;
19636 tree parameter_list;
19637 bool friend_p = false;
19638 bool need_lang_pop;
19639 cp_token *token;
19641 /* Look for the `template' keyword. */
19642 token = cp_lexer_peek_token (parser->lexer);
19643 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19644 return;
19646 /* And the `<'. */
19647 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19648 return;
19649 if (at_class_scope_p () && current_function_decl)
19651 /* 14.5.2.2 [temp.mem]
19653 A local class shall not have member templates. */
19654 error_at (token->location,
19655 "invalid declaration of member template in local class");
19656 cp_parser_skip_to_end_of_block_or_statement (parser);
19657 return;
19659 /* [temp]
19661 A template ... shall not have C linkage. */
19662 if (current_lang_name == lang_name_c)
19664 error_at (token->location, "template with C linkage");
19665 /* Give it C++ linkage to avoid confusing other parts of the
19666 front end. */
19667 push_lang_context (lang_name_cplusplus);
19668 need_lang_pop = true;
19670 else
19671 need_lang_pop = false;
19673 /* We cannot perform access checks on the template parameter
19674 declarations until we know what is being declared, just as we
19675 cannot check the decl-specifier list. */
19676 push_deferring_access_checks (dk_deferred);
19678 /* If the next token is `>', then we have an invalid
19679 specialization. Rather than complain about an invalid template
19680 parameter, issue an error message here. */
19681 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19683 cp_parser_error (parser, "invalid explicit specialization");
19684 begin_specialization ();
19685 parameter_list = NULL_TREE;
19687 else
19689 /* Parse the template parameters. */
19690 parameter_list = cp_parser_template_parameter_list (parser);
19691 fixup_template_parms ();
19694 /* Get the deferred access checks from the parameter list. These
19695 will be checked once we know what is being declared, as for a
19696 member template the checks must be performed in the scope of the
19697 class containing the member. */
19698 checks = get_deferred_access_checks ();
19700 /* Look for the `>'. */
19701 cp_parser_skip_to_end_of_template_parameter_list (parser);
19702 /* We just processed one more parameter list. */
19703 ++parser->num_template_parameter_lists;
19704 /* If the next token is `template', there are more template
19705 parameters. */
19706 if (cp_lexer_next_token_is_keyword (parser->lexer,
19707 RID_TEMPLATE))
19708 cp_parser_template_declaration_after_export (parser, member_p);
19709 else
19711 /* There are no access checks when parsing a template, as we do not
19712 know if a specialization will be a friend. */
19713 push_deferring_access_checks (dk_no_check);
19714 token = cp_lexer_peek_token (parser->lexer);
19715 decl = cp_parser_single_declaration (parser,
19716 checks,
19717 member_p,
19718 /*explicit_specialization_p=*/false,
19719 &friend_p);
19720 pop_deferring_access_checks ();
19722 /* If this is a member template declaration, let the front
19723 end know. */
19724 if (member_p && !friend_p && decl)
19726 if (TREE_CODE (decl) == TYPE_DECL)
19727 cp_parser_check_access_in_redeclaration (decl, token->location);
19729 decl = finish_member_template_decl (decl);
19731 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19732 make_friend_class (current_class_type, TREE_TYPE (decl),
19733 /*complain=*/true);
19735 /* We are done with the current parameter list. */
19736 --parser->num_template_parameter_lists;
19738 pop_deferring_access_checks ();
19740 /* Finish up. */
19741 finish_template_decl (parameter_list);
19743 /* Register member declarations. */
19744 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19745 finish_member_declaration (decl);
19746 /* For the erroneous case of a template with C linkage, we pushed an
19747 implicit C++ linkage scope; exit that scope now. */
19748 if (need_lang_pop)
19749 pop_lang_context ();
19750 /* If DECL is a function template, we must return to parse it later.
19751 (Even though there is no definition, there might be default
19752 arguments that need handling.) */
19753 if (member_p && decl
19754 && (TREE_CODE (decl) == FUNCTION_DECL
19755 || DECL_FUNCTION_TEMPLATE_P (decl)))
19756 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19759 /* Perform the deferred access checks from a template-parameter-list.
19760 CHECKS is a TREE_LIST of access checks, as returned by
19761 get_deferred_access_checks. */
19763 static void
19764 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19766 ++processing_template_parmlist;
19767 perform_access_checks (checks);
19768 --processing_template_parmlist;
19771 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19772 `function-definition' sequence. MEMBER_P is true, this declaration
19773 appears in a class scope.
19775 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19776 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19778 static tree
19779 cp_parser_single_declaration (cp_parser* parser,
19780 VEC (deferred_access_check,gc)* checks,
19781 bool member_p,
19782 bool explicit_specialization_p,
19783 bool* friend_p)
19785 int declares_class_or_enum;
19786 tree decl = NULL_TREE;
19787 cp_decl_specifier_seq decl_specifiers;
19788 bool function_definition_p = false;
19789 cp_token *decl_spec_token_start;
19791 /* This function is only used when processing a template
19792 declaration. */
19793 gcc_assert (innermost_scope_kind () == sk_template_parms
19794 || innermost_scope_kind () == sk_template_spec);
19796 /* Defer access checks until we know what is being declared. */
19797 push_deferring_access_checks (dk_deferred);
19799 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19800 alternative. */
19801 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19802 cp_parser_decl_specifier_seq (parser,
19803 CP_PARSER_FLAGS_OPTIONAL,
19804 &decl_specifiers,
19805 &declares_class_or_enum);
19806 if (friend_p)
19807 *friend_p = cp_parser_friend_p (&decl_specifiers);
19809 /* There are no template typedefs. */
19810 if (decl_specifiers.specs[(int) ds_typedef])
19812 error_at (decl_spec_token_start->location,
19813 "template declaration of %<typedef%>");
19814 decl = error_mark_node;
19817 /* Gather up the access checks that occurred the
19818 decl-specifier-seq. */
19819 stop_deferring_access_checks ();
19821 /* Check for the declaration of a template class. */
19822 if (declares_class_or_enum)
19824 if (cp_parser_declares_only_class_p (parser))
19826 decl = shadow_tag (&decl_specifiers);
19828 /* In this case:
19830 struct C {
19831 friend template <typename T> struct A<T>::B;
19834 A<T>::B will be represented by a TYPENAME_TYPE, and
19835 therefore not recognized by shadow_tag. */
19836 if (friend_p && *friend_p
19837 && !decl
19838 && decl_specifiers.type
19839 && TYPE_P (decl_specifiers.type))
19840 decl = decl_specifiers.type;
19842 if (decl && decl != error_mark_node)
19843 decl = TYPE_NAME (decl);
19844 else
19845 decl = error_mark_node;
19847 /* Perform access checks for template parameters. */
19848 cp_parser_perform_template_parameter_access_checks (checks);
19852 /* Complain about missing 'typename' or other invalid type names. */
19853 if (!decl_specifiers.any_type_specifiers_p
19854 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19856 /* cp_parser_parse_and_diagnose_invalid_type_name calls
19857 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
19858 the rest of this declaration. */
19859 decl = error_mark_node;
19860 goto out;
19863 /* If it's not a template class, try for a template function. If
19864 the next token is a `;', then this declaration does not declare
19865 anything. But, if there were errors in the decl-specifiers, then
19866 the error might well have come from an attempted class-specifier.
19867 In that case, there's no need to warn about a missing declarator. */
19868 if (!decl
19869 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
19870 || decl_specifiers.type != error_mark_node))
19872 decl = cp_parser_init_declarator (parser,
19873 &decl_specifiers,
19874 checks,
19875 /*function_definition_allowed_p=*/true,
19876 member_p,
19877 declares_class_or_enum,
19878 &function_definition_p,
19879 NULL);
19881 /* 7.1.1-1 [dcl.stc]
19883 A storage-class-specifier shall not be specified in an explicit
19884 specialization... */
19885 if (decl
19886 && explicit_specialization_p
19887 && decl_specifiers.storage_class != sc_none)
19889 error_at (decl_spec_token_start->location,
19890 "explicit template specialization cannot have a storage class");
19891 decl = error_mark_node;
19895 /* Look for a trailing `;' after the declaration. */
19896 if (!function_definition_p
19897 && (decl == error_mark_node
19898 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
19899 cp_parser_skip_to_end_of_block_or_statement (parser);
19901 out:
19902 pop_deferring_access_checks ();
19904 /* Clear any current qualification; whatever comes next is the start
19905 of something new. */
19906 parser->scope = NULL_TREE;
19907 parser->qualifying_scope = NULL_TREE;
19908 parser->object_scope = NULL_TREE;
19910 return decl;
19913 /* Parse a cast-expression that is not the operand of a unary "&". */
19915 static tree
19916 cp_parser_simple_cast_expression (cp_parser *parser)
19918 return cp_parser_cast_expression (parser, /*address_p=*/false,
19919 /*cast_p=*/false, NULL);
19922 /* Parse a functional cast to TYPE. Returns an expression
19923 representing the cast. */
19925 static tree
19926 cp_parser_functional_cast (cp_parser* parser, tree type)
19928 VEC(tree,gc) *vec;
19929 tree expression_list;
19930 tree cast;
19931 bool nonconst_p;
19933 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19935 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19936 expression_list = cp_parser_braced_list (parser, &nonconst_p);
19937 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
19938 if (TREE_CODE (type) == TYPE_DECL)
19939 type = TREE_TYPE (type);
19940 return finish_compound_literal (type, expression_list,
19941 tf_warning_or_error);
19945 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19946 /*cast_p=*/true,
19947 /*allow_expansion_p=*/true,
19948 /*non_constant_p=*/NULL);
19949 if (vec == NULL)
19950 expression_list = error_mark_node;
19951 else
19953 expression_list = build_tree_list_vec (vec);
19954 release_tree_vector (vec);
19957 cast = build_functional_cast (type, expression_list,
19958 tf_warning_or_error);
19959 /* [expr.const]/1: In an integral constant expression "only type
19960 conversions to integral or enumeration type can be used". */
19961 if (TREE_CODE (type) == TYPE_DECL)
19962 type = TREE_TYPE (type);
19963 if (cast != error_mark_node
19964 && !cast_valid_in_integral_constant_expression_p (type)
19965 && cp_parser_non_integral_constant_expression (parser,
19966 NIC_CONSTRUCTOR))
19967 return error_mark_node;
19968 return cast;
19971 /* Save the tokens that make up the body of a member function defined
19972 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
19973 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
19974 specifiers applied to the declaration. Returns the FUNCTION_DECL
19975 for the member function. */
19977 static tree
19978 cp_parser_save_member_function_body (cp_parser* parser,
19979 cp_decl_specifier_seq *decl_specifiers,
19980 cp_declarator *declarator,
19981 tree attributes)
19983 cp_token *first;
19984 cp_token *last;
19985 tree fn;
19987 /* Create the FUNCTION_DECL. */
19988 fn = grokmethod (decl_specifiers, declarator, attributes);
19989 /* If something went badly wrong, bail out now. */
19990 if (fn == error_mark_node)
19992 /* If there's a function-body, skip it. */
19993 if (cp_parser_token_starts_function_definition_p
19994 (cp_lexer_peek_token (parser->lexer)))
19995 cp_parser_skip_to_end_of_block_or_statement (parser);
19996 return error_mark_node;
19999 /* Remember it, if there default args to post process. */
20000 cp_parser_save_default_args (parser, fn);
20002 /* Save away the tokens that make up the body of the
20003 function. */
20004 first = parser->lexer->next_token;
20005 /* We can have braced-init-list mem-initializers before the fn body. */
20006 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20008 cp_lexer_consume_token (parser->lexer);
20009 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20010 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20012 /* cache_group will stop after an un-nested { } pair, too. */
20013 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20014 break;
20016 /* variadic mem-inits have ... after the ')'. */
20017 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20018 cp_lexer_consume_token (parser->lexer);
20021 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20022 /* Handle function try blocks. */
20023 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20024 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20025 last = parser->lexer->next_token;
20027 /* Save away the inline definition; we will process it when the
20028 class is complete. */
20029 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20030 DECL_PENDING_INLINE_P (fn) = 1;
20032 /* We need to know that this was defined in the class, so that
20033 friend templates are handled correctly. */
20034 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20036 /* Add FN to the queue of functions to be parsed later. */
20037 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20039 return fn;
20042 /* Parse a template-argument-list, as well as the trailing ">" (but
20043 not the opening ">"). See cp_parser_template_argument_list for the
20044 return value. */
20046 static tree
20047 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20049 tree arguments;
20050 tree saved_scope;
20051 tree saved_qualifying_scope;
20052 tree saved_object_scope;
20053 bool saved_greater_than_is_operator_p;
20054 int saved_unevaluated_operand;
20055 int saved_inhibit_evaluation_warnings;
20057 /* [temp.names]
20059 When parsing a template-id, the first non-nested `>' is taken as
20060 the end of the template-argument-list rather than a greater-than
20061 operator. */
20062 saved_greater_than_is_operator_p
20063 = parser->greater_than_is_operator_p;
20064 parser->greater_than_is_operator_p = false;
20065 /* Parsing the argument list may modify SCOPE, so we save it
20066 here. */
20067 saved_scope = parser->scope;
20068 saved_qualifying_scope = parser->qualifying_scope;
20069 saved_object_scope = parser->object_scope;
20070 /* We need to evaluate the template arguments, even though this
20071 template-id may be nested within a "sizeof". */
20072 saved_unevaluated_operand = cp_unevaluated_operand;
20073 cp_unevaluated_operand = 0;
20074 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20075 c_inhibit_evaluation_warnings = 0;
20076 /* Parse the template-argument-list itself. */
20077 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20078 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20079 arguments = NULL_TREE;
20080 else
20081 arguments = cp_parser_template_argument_list (parser);
20082 /* Look for the `>' that ends the template-argument-list. If we find
20083 a '>>' instead, it's probably just a typo. */
20084 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20086 if (cxx_dialect != cxx98)
20088 /* In C++0x, a `>>' in a template argument list or cast
20089 expression is considered to be two separate `>'
20090 tokens. So, change the current token to a `>', but don't
20091 consume it: it will be consumed later when the outer
20092 template argument list (or cast expression) is parsed.
20093 Note that this replacement of `>' for `>>' is necessary
20094 even if we are parsing tentatively: in the tentative
20095 case, after calling
20096 cp_parser_enclosed_template_argument_list we will always
20097 throw away all of the template arguments and the first
20098 closing `>', either because the template argument list
20099 was erroneous or because we are replacing those tokens
20100 with a CPP_TEMPLATE_ID token. The second `>' (which will
20101 not have been thrown away) is needed either to close an
20102 outer template argument list or to complete a new-style
20103 cast. */
20104 cp_token *token = cp_lexer_peek_token (parser->lexer);
20105 token->type = CPP_GREATER;
20107 else if (!saved_greater_than_is_operator_p)
20109 /* If we're in a nested template argument list, the '>>' has
20110 to be a typo for '> >'. We emit the error message, but we
20111 continue parsing and we push a '>' as next token, so that
20112 the argument list will be parsed correctly. Note that the
20113 global source location is still on the token before the
20114 '>>', so we need to say explicitly where we want it. */
20115 cp_token *token = cp_lexer_peek_token (parser->lexer);
20116 error_at (token->location, "%<>>%> should be %<> >%> "
20117 "within a nested template argument list");
20119 token->type = CPP_GREATER;
20121 else
20123 /* If this is not a nested template argument list, the '>>'
20124 is a typo for '>'. Emit an error message and continue.
20125 Same deal about the token location, but here we can get it
20126 right by consuming the '>>' before issuing the diagnostic. */
20127 cp_token *token = cp_lexer_consume_token (parser->lexer);
20128 error_at (token->location,
20129 "spurious %<>>%>, use %<>%> to terminate "
20130 "a template argument list");
20133 else
20134 cp_parser_skip_to_end_of_template_parameter_list (parser);
20135 /* The `>' token might be a greater-than operator again now. */
20136 parser->greater_than_is_operator_p
20137 = saved_greater_than_is_operator_p;
20138 /* Restore the SAVED_SCOPE. */
20139 parser->scope = saved_scope;
20140 parser->qualifying_scope = saved_qualifying_scope;
20141 parser->object_scope = saved_object_scope;
20142 cp_unevaluated_operand = saved_unevaluated_operand;
20143 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20145 return arguments;
20148 /* MEMBER_FUNCTION is a member function, or a friend. If default
20149 arguments, or the body of the function have not yet been parsed,
20150 parse them now. */
20152 static void
20153 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20155 /* If this member is a template, get the underlying
20156 FUNCTION_DECL. */
20157 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20158 member_function = DECL_TEMPLATE_RESULT (member_function);
20160 /* There should not be any class definitions in progress at this
20161 point; the bodies of members are only parsed outside of all class
20162 definitions. */
20163 gcc_assert (parser->num_classes_being_defined == 0);
20164 /* While we're parsing the member functions we might encounter more
20165 classes. We want to handle them right away, but we don't want
20166 them getting mixed up with functions that are currently in the
20167 queue. */
20168 push_unparsed_function_queues (parser);
20170 /* Make sure that any template parameters are in scope. */
20171 maybe_begin_member_template_processing (member_function);
20173 /* If the body of the function has not yet been parsed, parse it
20174 now. */
20175 if (DECL_PENDING_INLINE_P (member_function))
20177 tree function_scope;
20178 cp_token_cache *tokens;
20180 /* The function is no longer pending; we are processing it. */
20181 tokens = DECL_PENDING_INLINE_INFO (member_function);
20182 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20183 DECL_PENDING_INLINE_P (member_function) = 0;
20185 /* If this is a local class, enter the scope of the containing
20186 function. */
20187 function_scope = current_function_decl;
20188 if (function_scope)
20189 push_function_context ();
20191 /* Push the body of the function onto the lexer stack. */
20192 cp_parser_push_lexer_for_tokens (parser, tokens);
20194 /* Let the front end know that we going to be defining this
20195 function. */
20196 start_preparsed_function (member_function, NULL_TREE,
20197 SF_PRE_PARSED | SF_INCLASS_INLINE);
20199 /* Don't do access checking if it is a templated function. */
20200 if (processing_template_decl)
20201 push_deferring_access_checks (dk_no_check);
20203 /* Now, parse the body of the function. */
20204 cp_parser_function_definition_after_declarator (parser,
20205 /*inline_p=*/true);
20207 if (processing_template_decl)
20208 pop_deferring_access_checks ();
20210 /* Leave the scope of the containing function. */
20211 if (function_scope)
20212 pop_function_context ();
20213 cp_parser_pop_lexer (parser);
20216 /* Remove any template parameters from the symbol table. */
20217 maybe_end_member_template_processing ();
20219 /* Restore the queue. */
20220 pop_unparsed_function_queues (parser);
20223 /* If DECL contains any default args, remember it on the unparsed
20224 functions queue. */
20226 static void
20227 cp_parser_save_default_args (cp_parser* parser, tree decl)
20229 tree probe;
20231 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20232 probe;
20233 probe = TREE_CHAIN (probe))
20234 if (TREE_PURPOSE (probe))
20236 cp_default_arg_entry *entry
20237 = VEC_safe_push (cp_default_arg_entry, gc,
20238 unparsed_funs_with_default_args, NULL);
20239 entry->class_type = current_class_type;
20240 entry->decl = decl;
20241 break;
20245 /* FN is a FUNCTION_DECL which may contains a parameter with an
20246 unparsed DEFAULT_ARG. Parse the default args now. This function
20247 assumes that the current scope is the scope in which the default
20248 argument should be processed. */
20250 static void
20251 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20253 bool saved_local_variables_forbidden_p;
20254 tree parm, parmdecl;
20256 /* While we're parsing the default args, we might (due to the
20257 statement expression extension) encounter more classes. We want
20258 to handle them right away, but we don't want them getting mixed
20259 up with default args that are currently in the queue. */
20260 push_unparsed_function_queues (parser);
20262 /* Local variable names (and the `this' keyword) may not appear
20263 in a default argument. */
20264 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20265 parser->local_variables_forbidden_p = true;
20267 push_defarg_context (fn);
20269 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20270 parmdecl = DECL_ARGUMENTS (fn);
20271 parm && parm != void_list_node;
20272 parm = TREE_CHAIN (parm),
20273 parmdecl = DECL_CHAIN (parmdecl))
20275 cp_token_cache *tokens;
20276 tree default_arg = TREE_PURPOSE (parm);
20277 tree parsed_arg;
20278 VEC(tree,gc) *insts;
20279 tree copy;
20280 unsigned ix;
20282 if (!default_arg)
20283 continue;
20285 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20286 /* This can happen for a friend declaration for a function
20287 already declared with default arguments. */
20288 continue;
20290 /* Push the saved tokens for the default argument onto the parser's
20291 lexer stack. */
20292 tokens = DEFARG_TOKENS (default_arg);
20293 cp_parser_push_lexer_for_tokens (parser, tokens);
20295 start_lambda_scope (parmdecl);
20297 /* Parse the assignment-expression. */
20298 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20299 if (parsed_arg == error_mark_node)
20301 cp_parser_pop_lexer (parser);
20302 continue;
20305 if (!processing_template_decl)
20306 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20308 TREE_PURPOSE (parm) = parsed_arg;
20310 /* Update any instantiations we've already created. */
20311 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20312 VEC_iterate (tree, insts, ix, copy); ix++)
20313 TREE_PURPOSE (copy) = parsed_arg;
20315 finish_lambda_scope ();
20317 /* If the token stream has not been completely used up, then
20318 there was extra junk after the end of the default
20319 argument. */
20320 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20321 cp_parser_error (parser, "expected %<,%>");
20323 /* Revert to the main lexer. */
20324 cp_parser_pop_lexer (parser);
20327 pop_defarg_context ();
20329 /* Make sure no default arg is missing. */
20330 check_default_args (fn);
20332 /* Restore the state of local_variables_forbidden_p. */
20333 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20335 /* Restore the queue. */
20336 pop_unparsed_function_queues (parser);
20339 /* Parse the operand of `sizeof' (or a similar operator). Returns
20340 either a TYPE or an expression, depending on the form of the
20341 input. The KEYWORD indicates which kind of expression we have
20342 encountered. */
20344 static tree
20345 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20347 tree expr = NULL_TREE;
20348 const char *saved_message;
20349 char *tmp;
20350 bool saved_integral_constant_expression_p;
20351 bool saved_non_integral_constant_expression_p;
20352 bool pack_expansion_p = false;
20354 /* Types cannot be defined in a `sizeof' expression. Save away the
20355 old message. */
20356 saved_message = parser->type_definition_forbidden_message;
20357 /* And create the new one. */
20358 tmp = concat ("types may not be defined in %<",
20359 IDENTIFIER_POINTER (ridpointers[keyword]),
20360 "%> expressions", NULL);
20361 parser->type_definition_forbidden_message = tmp;
20363 /* The restrictions on constant-expressions do not apply inside
20364 sizeof expressions. */
20365 saved_integral_constant_expression_p
20366 = parser->integral_constant_expression_p;
20367 saved_non_integral_constant_expression_p
20368 = parser->non_integral_constant_expression_p;
20369 parser->integral_constant_expression_p = false;
20371 /* If it's a `...', then we are computing the length of a parameter
20372 pack. */
20373 if (keyword == RID_SIZEOF
20374 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20376 /* Consume the `...'. */
20377 cp_lexer_consume_token (parser->lexer);
20378 maybe_warn_variadic_templates ();
20380 /* Note that this is an expansion. */
20381 pack_expansion_p = true;
20384 /* Do not actually evaluate the expression. */
20385 ++cp_unevaluated_operand;
20386 ++c_inhibit_evaluation_warnings;
20387 /* If it's a `(', then we might be looking at the type-id
20388 construction. */
20389 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20391 tree type;
20392 bool saved_in_type_id_in_expr_p;
20394 /* We can't be sure yet whether we're looking at a type-id or an
20395 expression. */
20396 cp_parser_parse_tentatively (parser);
20397 /* Consume the `('. */
20398 cp_lexer_consume_token (parser->lexer);
20399 /* Parse the type-id. */
20400 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20401 parser->in_type_id_in_expr_p = true;
20402 type = cp_parser_type_id (parser);
20403 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20404 /* Now, look for the trailing `)'. */
20405 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20406 /* If all went well, then we're done. */
20407 if (cp_parser_parse_definitely (parser))
20409 cp_decl_specifier_seq decl_specs;
20411 /* Build a trivial decl-specifier-seq. */
20412 clear_decl_specs (&decl_specs);
20413 decl_specs.type = type;
20415 /* Call grokdeclarator to figure out what type this is. */
20416 expr = grokdeclarator (NULL,
20417 &decl_specs,
20418 TYPENAME,
20419 /*initialized=*/0,
20420 /*attrlist=*/NULL);
20424 /* If the type-id production did not work out, then we must be
20425 looking at the unary-expression production. */
20426 if (!expr)
20427 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20428 /*cast_p=*/false, NULL);
20430 if (pack_expansion_p)
20431 /* Build a pack expansion. */
20432 expr = make_pack_expansion (expr);
20434 /* Go back to evaluating expressions. */
20435 --cp_unevaluated_operand;
20436 --c_inhibit_evaluation_warnings;
20438 /* Free the message we created. */
20439 free (tmp);
20440 /* And restore the old one. */
20441 parser->type_definition_forbidden_message = saved_message;
20442 parser->integral_constant_expression_p
20443 = saved_integral_constant_expression_p;
20444 parser->non_integral_constant_expression_p
20445 = saved_non_integral_constant_expression_p;
20447 return expr;
20450 /* If the current declaration has no declarator, return true. */
20452 static bool
20453 cp_parser_declares_only_class_p (cp_parser *parser)
20455 /* If the next token is a `;' or a `,' then there is no
20456 declarator. */
20457 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20458 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20461 /* Update the DECL_SPECS to reflect the storage class indicated by
20462 KEYWORD. */
20464 static void
20465 cp_parser_set_storage_class (cp_parser *parser,
20466 cp_decl_specifier_seq *decl_specs,
20467 enum rid keyword,
20468 location_t location)
20470 cp_storage_class storage_class;
20472 if (parser->in_unbraced_linkage_specification_p)
20474 error_at (location, "invalid use of %qD in linkage specification",
20475 ridpointers[keyword]);
20476 return;
20478 else if (decl_specs->storage_class != sc_none)
20480 decl_specs->conflicting_specifiers_p = true;
20481 return;
20484 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20485 && decl_specs->specs[(int) ds_thread])
20487 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20488 decl_specs->specs[(int) ds_thread] = 0;
20491 switch (keyword)
20493 case RID_AUTO:
20494 storage_class = sc_auto;
20495 break;
20496 case RID_REGISTER:
20497 storage_class = sc_register;
20498 break;
20499 case RID_STATIC:
20500 storage_class = sc_static;
20501 break;
20502 case RID_EXTERN:
20503 storage_class = sc_extern;
20504 break;
20505 case RID_MUTABLE:
20506 storage_class = sc_mutable;
20507 break;
20508 default:
20509 gcc_unreachable ();
20511 decl_specs->storage_class = storage_class;
20513 /* A storage class specifier cannot be applied alongside a typedef
20514 specifier. If there is a typedef specifier present then set
20515 conflicting_specifiers_p which will trigger an error later
20516 on in grokdeclarator. */
20517 if (decl_specs->specs[(int)ds_typedef])
20518 decl_specs->conflicting_specifiers_p = true;
20521 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20522 is true, the type is a user-defined type; otherwise it is a
20523 built-in type specified by a keyword. */
20525 static void
20526 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20527 tree type_spec,
20528 location_t location,
20529 bool user_defined_p)
20531 decl_specs->any_specifiers_p = true;
20533 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20534 (with, for example, in "typedef int wchar_t;") we remember that
20535 this is what happened. In system headers, we ignore these
20536 declarations so that G++ can work with system headers that are not
20537 C++-safe. */
20538 if (decl_specs->specs[(int) ds_typedef]
20539 && !user_defined_p
20540 && (type_spec == boolean_type_node
20541 || type_spec == char16_type_node
20542 || type_spec == char32_type_node
20543 || type_spec == wchar_type_node)
20544 && (decl_specs->type
20545 || decl_specs->specs[(int) ds_long]
20546 || decl_specs->specs[(int) ds_short]
20547 || decl_specs->specs[(int) ds_unsigned]
20548 || decl_specs->specs[(int) ds_signed]))
20550 decl_specs->redefined_builtin_type = type_spec;
20551 if (!decl_specs->type)
20553 decl_specs->type = type_spec;
20554 decl_specs->user_defined_type_p = false;
20555 decl_specs->type_location = location;
20558 else if (decl_specs->type)
20559 decl_specs->multiple_types_p = true;
20560 else
20562 decl_specs->type = type_spec;
20563 decl_specs->user_defined_type_p = user_defined_p;
20564 decl_specs->redefined_builtin_type = NULL_TREE;
20565 decl_specs->type_location = location;
20569 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20570 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20572 static bool
20573 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20575 return decl_specifiers->specs[(int) ds_friend] != 0;
20578 /* Issue an error message indicating that TOKEN_DESC was expected.
20579 If KEYWORD is true, it indicated this function is called by
20580 cp_parser_require_keword and the required token can only be
20581 a indicated keyword. */
20583 static void
20584 cp_parser_required_error (cp_parser *parser,
20585 required_token token_desc,
20586 bool keyword)
20588 switch (token_desc)
20590 case RT_NEW:
20591 cp_parser_error (parser, "expected %<new%>");
20592 return;
20593 case RT_DELETE:
20594 cp_parser_error (parser, "expected %<delete%>");
20595 return;
20596 case RT_RETURN:
20597 cp_parser_error (parser, "expected %<return%>");
20598 return;
20599 case RT_WHILE:
20600 cp_parser_error (parser, "expected %<while%>");
20601 return;
20602 case RT_EXTERN:
20603 cp_parser_error (parser, "expected %<extern%>");
20604 return;
20605 case RT_STATIC_ASSERT:
20606 cp_parser_error (parser, "expected %<static_assert%>");
20607 return;
20608 case RT_DECLTYPE:
20609 cp_parser_error (parser, "expected %<decltype%>");
20610 return;
20611 case RT_OPERATOR:
20612 cp_parser_error (parser, "expected %<operator%>");
20613 return;
20614 case RT_CLASS:
20615 cp_parser_error (parser, "expected %<class%>");
20616 return;
20617 case RT_TEMPLATE:
20618 cp_parser_error (parser, "expected %<template%>");
20619 return;
20620 case RT_NAMESPACE:
20621 cp_parser_error (parser, "expected %<namespace%>");
20622 return;
20623 case RT_USING:
20624 cp_parser_error (parser, "expected %<using%>");
20625 return;
20626 case RT_ASM:
20627 cp_parser_error (parser, "expected %<asm%>");
20628 return;
20629 case RT_TRY:
20630 cp_parser_error (parser, "expected %<try%>");
20631 return;
20632 case RT_CATCH:
20633 cp_parser_error (parser, "expected %<catch%>");
20634 return;
20635 case RT_THROW:
20636 cp_parser_error (parser, "expected %<throw%>");
20637 return;
20638 case RT_LABEL:
20639 cp_parser_error (parser, "expected %<__label__%>");
20640 return;
20641 case RT_AT_TRY:
20642 cp_parser_error (parser, "expected %<@try%>");
20643 return;
20644 case RT_AT_SYNCHRONIZED:
20645 cp_parser_error (parser, "expected %<@synchronized%>");
20646 return;
20647 case RT_AT_THROW:
20648 cp_parser_error (parser, "expected %<@throw%>");
20649 return;
20650 default:
20651 break;
20653 if (!keyword)
20655 switch (token_desc)
20657 case RT_SEMICOLON:
20658 cp_parser_error (parser, "expected %<;%>");
20659 return;
20660 case RT_OPEN_PAREN:
20661 cp_parser_error (parser, "expected %<(%>");
20662 return;
20663 case RT_CLOSE_BRACE:
20664 cp_parser_error (parser, "expected %<}%>");
20665 return;
20666 case RT_OPEN_BRACE:
20667 cp_parser_error (parser, "expected %<{%>");
20668 return;
20669 case RT_CLOSE_SQUARE:
20670 cp_parser_error (parser, "expected %<]%>");
20671 return;
20672 case RT_OPEN_SQUARE:
20673 cp_parser_error (parser, "expected %<[%>");
20674 return;
20675 case RT_COMMA:
20676 cp_parser_error (parser, "expected %<,%>");
20677 return;
20678 case RT_SCOPE:
20679 cp_parser_error (parser, "expected %<::%>");
20680 return;
20681 case RT_LESS:
20682 cp_parser_error (parser, "expected %<<%>");
20683 return;
20684 case RT_GREATER:
20685 cp_parser_error (parser, "expected %<>%>");
20686 return;
20687 case RT_EQ:
20688 cp_parser_error (parser, "expected %<=%>");
20689 return;
20690 case RT_ELLIPSIS:
20691 cp_parser_error (parser, "expected %<...%>");
20692 return;
20693 case RT_MULT:
20694 cp_parser_error (parser, "expected %<*%>");
20695 return;
20696 case RT_COMPL:
20697 cp_parser_error (parser, "expected %<~%>");
20698 return;
20699 case RT_COLON:
20700 cp_parser_error (parser, "expected %<:%>");
20701 return;
20702 case RT_COLON_SCOPE:
20703 cp_parser_error (parser, "expected %<:%> or %<::%>");
20704 return;
20705 case RT_CLOSE_PAREN:
20706 cp_parser_error (parser, "expected %<)%>");
20707 return;
20708 case RT_COMMA_CLOSE_PAREN:
20709 cp_parser_error (parser, "expected %<,%> or %<)%>");
20710 return;
20711 case RT_PRAGMA_EOL:
20712 cp_parser_error (parser, "expected end of line");
20713 return;
20714 case RT_NAME:
20715 cp_parser_error (parser, "expected identifier");
20716 return;
20717 case RT_SELECT:
20718 cp_parser_error (parser, "expected selection-statement");
20719 return;
20720 case RT_INTERATION:
20721 cp_parser_error (parser, "expected iteration-statement");
20722 return;
20723 case RT_JUMP:
20724 cp_parser_error (parser, "expected jump-statement");
20725 return;
20726 case RT_CLASS_KEY:
20727 cp_parser_error (parser, "expected class-key");
20728 return;
20729 case RT_CLASS_TYPENAME_TEMPLATE:
20730 cp_parser_error (parser,
20731 "expected %<class%>, %<typename%>, or %<template%>");
20732 return;
20733 default:
20734 gcc_unreachable ();
20737 else
20738 gcc_unreachable ();
20743 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20744 issue an error message indicating that TOKEN_DESC was expected.
20746 Returns the token consumed, if the token had the appropriate type.
20747 Otherwise, returns NULL. */
20749 static cp_token *
20750 cp_parser_require (cp_parser* parser,
20751 enum cpp_ttype type,
20752 required_token token_desc)
20754 if (cp_lexer_next_token_is (parser->lexer, type))
20755 return cp_lexer_consume_token (parser->lexer);
20756 else
20758 /* Output the MESSAGE -- unless we're parsing tentatively. */
20759 if (!cp_parser_simulate_error (parser))
20760 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20761 return NULL;
20765 /* An error message is produced if the next token is not '>'.
20766 All further tokens are skipped until the desired token is
20767 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20769 static void
20770 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20772 /* Current level of '< ... >'. */
20773 unsigned level = 0;
20774 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20775 unsigned nesting_depth = 0;
20777 /* Are we ready, yet? If not, issue error message. */
20778 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20779 return;
20781 /* Skip tokens until the desired token is found. */
20782 while (true)
20784 /* Peek at the next token. */
20785 switch (cp_lexer_peek_token (parser->lexer)->type)
20787 case CPP_LESS:
20788 if (!nesting_depth)
20789 ++level;
20790 break;
20792 case CPP_RSHIFT:
20793 if (cxx_dialect == cxx98)
20794 /* C++0x views the `>>' operator as two `>' tokens, but
20795 C++98 does not. */
20796 break;
20797 else if (!nesting_depth && level-- == 0)
20799 /* We've hit a `>>' where the first `>' closes the
20800 template argument list, and the second `>' is
20801 spurious. Just consume the `>>' and stop; we've
20802 already produced at least one error. */
20803 cp_lexer_consume_token (parser->lexer);
20804 return;
20806 /* Fall through for C++0x, so we handle the second `>' in
20807 the `>>'. */
20809 case CPP_GREATER:
20810 if (!nesting_depth && level-- == 0)
20812 /* We've reached the token we want, consume it and stop. */
20813 cp_lexer_consume_token (parser->lexer);
20814 return;
20816 break;
20818 case CPP_OPEN_PAREN:
20819 case CPP_OPEN_SQUARE:
20820 ++nesting_depth;
20821 break;
20823 case CPP_CLOSE_PAREN:
20824 case CPP_CLOSE_SQUARE:
20825 if (nesting_depth-- == 0)
20826 return;
20827 break;
20829 case CPP_EOF:
20830 case CPP_PRAGMA_EOL:
20831 case CPP_SEMICOLON:
20832 case CPP_OPEN_BRACE:
20833 case CPP_CLOSE_BRACE:
20834 /* The '>' was probably forgotten, don't look further. */
20835 return;
20837 default:
20838 break;
20841 /* Consume this token. */
20842 cp_lexer_consume_token (parser->lexer);
20846 /* If the next token is the indicated keyword, consume it. Otherwise,
20847 issue an error message indicating that TOKEN_DESC was expected.
20849 Returns the token consumed, if the token had the appropriate type.
20850 Otherwise, returns NULL. */
20852 static cp_token *
20853 cp_parser_require_keyword (cp_parser* parser,
20854 enum rid keyword,
20855 required_token token_desc)
20857 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20859 if (token && token->keyword != keyword)
20861 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
20862 return NULL;
20865 return token;
20868 /* Returns TRUE iff TOKEN is a token that can begin the body of a
20869 function-definition. */
20871 static bool
20872 cp_parser_token_starts_function_definition_p (cp_token* token)
20874 return (/* An ordinary function-body begins with an `{'. */
20875 token->type == CPP_OPEN_BRACE
20876 /* A ctor-initializer begins with a `:'. */
20877 || token->type == CPP_COLON
20878 /* A function-try-block begins with `try'. */
20879 || token->keyword == RID_TRY
20880 /* The named return value extension begins with `return'. */
20881 || token->keyword == RID_RETURN);
20884 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
20885 definition. */
20887 static bool
20888 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
20890 cp_token *token;
20892 token = cp_lexer_peek_token (parser->lexer);
20893 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
20896 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
20897 C++0x) ending a template-argument. */
20899 static bool
20900 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
20902 cp_token *token;
20904 token = cp_lexer_peek_token (parser->lexer);
20905 return (token->type == CPP_COMMA
20906 || token->type == CPP_GREATER
20907 || token->type == CPP_ELLIPSIS
20908 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
20911 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
20912 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
20914 static bool
20915 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
20916 size_t n)
20918 cp_token *token;
20920 token = cp_lexer_peek_nth_token (parser->lexer, n);
20921 if (token->type == CPP_LESS)
20922 return true;
20923 /* Check for the sequence `<::' in the original code. It would be lexed as
20924 `[:', where `[' is a digraph, and there is no whitespace before
20925 `:'. */
20926 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
20928 cp_token *token2;
20929 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
20930 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
20931 return true;
20933 return false;
20936 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
20937 or none_type otherwise. */
20939 static enum tag_types
20940 cp_parser_token_is_class_key (cp_token* token)
20942 switch (token->keyword)
20944 case RID_CLASS:
20945 return class_type;
20946 case RID_STRUCT:
20947 return record_type;
20948 case RID_UNION:
20949 return union_type;
20951 default:
20952 return none_type;
20956 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
20958 static void
20959 cp_parser_check_class_key (enum tag_types class_key, tree type)
20961 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
20962 permerror (input_location, "%qs tag used in naming %q#T",
20963 class_key == union_type ? "union"
20964 : class_key == record_type ? "struct" : "class",
20965 type);
20968 /* Issue an error message if DECL is redeclared with different
20969 access than its original declaration [class.access.spec/3].
20970 This applies to nested classes and nested class templates.
20971 [class.mem/1]. */
20973 static void
20974 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
20976 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
20977 return;
20979 if ((TREE_PRIVATE (decl)
20980 != (current_access_specifier == access_private_node))
20981 || (TREE_PROTECTED (decl)
20982 != (current_access_specifier == access_protected_node)))
20983 error_at (location, "%qD redeclared with different access", decl);
20986 /* Look for the `template' keyword, as a syntactic disambiguator.
20987 Return TRUE iff it is present, in which case it will be
20988 consumed. */
20990 static bool
20991 cp_parser_optional_template_keyword (cp_parser *parser)
20993 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20995 /* The `template' keyword can only be used within templates;
20996 outside templates the parser can always figure out what is a
20997 template and what is not. */
20998 if (!processing_template_decl)
21000 cp_token *token = cp_lexer_peek_token (parser->lexer);
21001 error_at (token->location,
21002 "%<template%> (as a disambiguator) is only allowed "
21003 "within templates");
21004 /* If this part of the token stream is rescanned, the same
21005 error message would be generated. So, we purge the token
21006 from the stream. */
21007 cp_lexer_purge_token (parser->lexer);
21008 return false;
21010 else
21012 /* Consume the `template' keyword. */
21013 cp_lexer_consume_token (parser->lexer);
21014 return true;
21018 return false;
21021 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21022 set PARSER->SCOPE, and perform other related actions. */
21024 static void
21025 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21027 int i;
21028 struct tree_check *check_value;
21029 deferred_access_check *chk;
21030 VEC (deferred_access_check,gc) *checks;
21032 /* Get the stored value. */
21033 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21034 /* Perform any access checks that were deferred. */
21035 checks = check_value->checks;
21036 if (checks)
21038 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21039 perform_or_defer_access_check (chk->binfo,
21040 chk->decl,
21041 chk->diag_decl);
21043 /* Set the scope from the stored value. */
21044 parser->scope = check_value->value;
21045 parser->qualifying_scope = check_value->qualifying_scope;
21046 parser->object_scope = NULL_TREE;
21049 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21050 encounter the end of a block before what we were looking for. */
21052 static bool
21053 cp_parser_cache_group (cp_parser *parser,
21054 enum cpp_ttype end,
21055 unsigned depth)
21057 while (true)
21059 cp_token *token = cp_lexer_peek_token (parser->lexer);
21061 /* Abort a parenthesized expression if we encounter a semicolon. */
21062 if ((end == CPP_CLOSE_PAREN || depth == 0)
21063 && token->type == CPP_SEMICOLON)
21064 return true;
21065 /* If we've reached the end of the file, stop. */
21066 if (token->type == CPP_EOF
21067 || (end != CPP_PRAGMA_EOL
21068 && token->type == CPP_PRAGMA_EOL))
21069 return true;
21070 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21071 /* We've hit the end of an enclosing block, so there's been some
21072 kind of syntax error. */
21073 return true;
21075 /* Consume the token. */
21076 cp_lexer_consume_token (parser->lexer);
21077 /* See if it starts a new group. */
21078 if (token->type == CPP_OPEN_BRACE)
21080 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21081 /* In theory this should probably check end == '}', but
21082 cp_parser_save_member_function_body needs it to exit
21083 after either '}' or ')' when called with ')'. */
21084 if (depth == 0)
21085 return false;
21087 else if (token->type == CPP_OPEN_PAREN)
21089 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21090 if (depth == 0 && end == CPP_CLOSE_PAREN)
21091 return false;
21093 else if (token->type == CPP_PRAGMA)
21094 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21095 else if (token->type == end)
21096 return false;
21100 /* Begin parsing tentatively. We always save tokens while parsing
21101 tentatively so that if the tentative parsing fails we can restore the
21102 tokens. */
21104 static void
21105 cp_parser_parse_tentatively (cp_parser* parser)
21107 /* Enter a new parsing context. */
21108 parser->context = cp_parser_context_new (parser->context);
21109 /* Begin saving tokens. */
21110 cp_lexer_save_tokens (parser->lexer);
21111 /* In order to avoid repetitive access control error messages,
21112 access checks are queued up until we are no longer parsing
21113 tentatively. */
21114 push_deferring_access_checks (dk_deferred);
21117 /* Commit to the currently active tentative parse. */
21119 static void
21120 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21122 cp_parser_context *context;
21123 cp_lexer *lexer;
21125 /* Mark all of the levels as committed. */
21126 lexer = parser->lexer;
21127 for (context = parser->context; context->next; context = context->next)
21129 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21130 break;
21131 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21132 while (!cp_lexer_saving_tokens (lexer))
21133 lexer = lexer->next;
21134 cp_lexer_commit_tokens (lexer);
21138 /* Abort the currently active tentative parse. All consumed tokens
21139 will be rolled back, and no diagnostics will be issued. */
21141 static void
21142 cp_parser_abort_tentative_parse (cp_parser* parser)
21144 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21145 || errorcount > 0);
21146 cp_parser_simulate_error (parser);
21147 /* Now, pretend that we want to see if the construct was
21148 successfully parsed. */
21149 cp_parser_parse_definitely (parser);
21152 /* Stop parsing tentatively. If a parse error has occurred, restore the
21153 token stream. Otherwise, commit to the tokens we have consumed.
21154 Returns true if no error occurred; false otherwise. */
21156 static bool
21157 cp_parser_parse_definitely (cp_parser* parser)
21159 bool error_occurred;
21160 cp_parser_context *context;
21162 /* Remember whether or not an error occurred, since we are about to
21163 destroy that information. */
21164 error_occurred = cp_parser_error_occurred (parser);
21165 /* Remove the topmost context from the stack. */
21166 context = parser->context;
21167 parser->context = context->next;
21168 /* If no parse errors occurred, commit to the tentative parse. */
21169 if (!error_occurred)
21171 /* Commit to the tokens read tentatively, unless that was
21172 already done. */
21173 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21174 cp_lexer_commit_tokens (parser->lexer);
21176 pop_to_parent_deferring_access_checks ();
21178 /* Otherwise, if errors occurred, roll back our state so that things
21179 are just as they were before we began the tentative parse. */
21180 else
21182 cp_lexer_rollback_tokens (parser->lexer);
21183 pop_deferring_access_checks ();
21185 /* Add the context to the front of the free list. */
21186 context->next = cp_parser_context_free_list;
21187 cp_parser_context_free_list = context;
21189 return !error_occurred;
21192 /* Returns true if we are parsing tentatively and are not committed to
21193 this tentative parse. */
21195 static bool
21196 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21198 return (cp_parser_parsing_tentatively (parser)
21199 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21202 /* Returns nonzero iff an error has occurred during the most recent
21203 tentative parse. */
21205 static bool
21206 cp_parser_error_occurred (cp_parser* parser)
21208 return (cp_parser_parsing_tentatively (parser)
21209 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21212 /* Returns nonzero if GNU extensions are allowed. */
21214 static bool
21215 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21217 return parser->allow_gnu_extensions_p;
21220 /* Objective-C++ Productions */
21223 /* Parse an Objective-C expression, which feeds into a primary-expression
21224 above.
21226 objc-expression:
21227 objc-message-expression
21228 objc-string-literal
21229 objc-encode-expression
21230 objc-protocol-expression
21231 objc-selector-expression
21233 Returns a tree representation of the expression. */
21235 static tree
21236 cp_parser_objc_expression (cp_parser* parser)
21238 /* Try to figure out what kind of declaration is present. */
21239 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21241 switch (kwd->type)
21243 case CPP_OPEN_SQUARE:
21244 return cp_parser_objc_message_expression (parser);
21246 case CPP_OBJC_STRING:
21247 kwd = cp_lexer_consume_token (parser->lexer);
21248 return objc_build_string_object (kwd->u.value);
21250 case CPP_KEYWORD:
21251 switch (kwd->keyword)
21253 case RID_AT_ENCODE:
21254 return cp_parser_objc_encode_expression (parser);
21256 case RID_AT_PROTOCOL:
21257 return cp_parser_objc_protocol_expression (parser);
21259 case RID_AT_SELECTOR:
21260 return cp_parser_objc_selector_expression (parser);
21262 default:
21263 break;
21265 default:
21266 error_at (kwd->location,
21267 "misplaced %<@%D%> Objective-C++ construct",
21268 kwd->u.value);
21269 cp_parser_skip_to_end_of_block_or_statement (parser);
21272 return error_mark_node;
21275 /* Parse an Objective-C message expression.
21277 objc-message-expression:
21278 [ objc-message-receiver objc-message-args ]
21280 Returns a representation of an Objective-C message. */
21282 static tree
21283 cp_parser_objc_message_expression (cp_parser* parser)
21285 tree receiver, messageargs;
21287 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21288 receiver = cp_parser_objc_message_receiver (parser);
21289 messageargs = cp_parser_objc_message_args (parser);
21290 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21292 return objc_build_message_expr (receiver, messageargs);
21295 /* Parse an objc-message-receiver.
21297 objc-message-receiver:
21298 expression
21299 simple-type-specifier
21301 Returns a representation of the type or expression. */
21303 static tree
21304 cp_parser_objc_message_receiver (cp_parser* parser)
21306 tree rcv;
21308 /* An Objective-C message receiver may be either (1) a type
21309 or (2) an expression. */
21310 cp_parser_parse_tentatively (parser);
21311 rcv = cp_parser_expression (parser, false, NULL);
21313 if (cp_parser_parse_definitely (parser))
21314 return rcv;
21316 rcv = cp_parser_simple_type_specifier (parser,
21317 /*decl_specs=*/NULL,
21318 CP_PARSER_FLAGS_NONE);
21320 return objc_get_class_reference (rcv);
21323 /* Parse the arguments and selectors comprising an Objective-C message.
21325 objc-message-args:
21326 objc-selector
21327 objc-selector-args
21328 objc-selector-args , objc-comma-args
21330 objc-selector-args:
21331 objc-selector [opt] : assignment-expression
21332 objc-selector-args objc-selector [opt] : assignment-expression
21334 objc-comma-args:
21335 assignment-expression
21336 objc-comma-args , assignment-expression
21338 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21339 selector arguments and TREE_VALUE containing a list of comma
21340 arguments. */
21342 static tree
21343 cp_parser_objc_message_args (cp_parser* parser)
21345 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21346 bool maybe_unary_selector_p = true;
21347 cp_token *token = cp_lexer_peek_token (parser->lexer);
21349 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21351 tree selector = NULL_TREE, arg;
21353 if (token->type != CPP_COLON)
21354 selector = cp_parser_objc_selector (parser);
21356 /* Detect if we have a unary selector. */
21357 if (maybe_unary_selector_p
21358 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21359 return build_tree_list (selector, NULL_TREE);
21361 maybe_unary_selector_p = false;
21362 cp_parser_require (parser, CPP_COLON, RT_COLON);
21363 arg = cp_parser_assignment_expression (parser, false, NULL);
21365 sel_args
21366 = chainon (sel_args,
21367 build_tree_list (selector, arg));
21369 token = cp_lexer_peek_token (parser->lexer);
21372 /* Handle non-selector arguments, if any. */
21373 while (token->type == CPP_COMMA)
21375 tree arg;
21377 cp_lexer_consume_token (parser->lexer);
21378 arg = cp_parser_assignment_expression (parser, false, NULL);
21380 addl_args
21381 = chainon (addl_args,
21382 build_tree_list (NULL_TREE, arg));
21384 token = cp_lexer_peek_token (parser->lexer);
21387 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21389 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21390 return build_tree_list (error_mark_node, error_mark_node);
21393 return build_tree_list (sel_args, addl_args);
21396 /* Parse an Objective-C encode expression.
21398 objc-encode-expression:
21399 @encode objc-typename
21401 Returns an encoded representation of the type argument. */
21403 static tree
21404 cp_parser_objc_encode_expression (cp_parser* parser)
21406 tree type;
21407 cp_token *token;
21409 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21410 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21411 token = cp_lexer_peek_token (parser->lexer);
21412 type = complete_type (cp_parser_type_id (parser));
21413 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21415 if (!type)
21417 error_at (token->location,
21418 "%<@encode%> must specify a type as an argument");
21419 return error_mark_node;
21422 /* This happens if we find @encode(T) (where T is a template
21423 typename or something dependent on a template typename) when
21424 parsing a template. In that case, we can't compile it
21425 immediately, but we rather create an AT_ENCODE_EXPR which will
21426 need to be instantiated when the template is used.
21428 if (dependent_type_p (type))
21430 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21431 TREE_READONLY (value) = 1;
21432 return value;
21435 return objc_build_encode_expr (type);
21438 /* Parse an Objective-C @defs expression. */
21440 static tree
21441 cp_parser_objc_defs_expression (cp_parser *parser)
21443 tree name;
21445 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21446 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21447 name = cp_parser_identifier (parser);
21448 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21450 return objc_get_class_ivars (name);
21453 /* Parse an Objective-C protocol expression.
21455 objc-protocol-expression:
21456 @protocol ( identifier )
21458 Returns a representation of the protocol expression. */
21460 static tree
21461 cp_parser_objc_protocol_expression (cp_parser* parser)
21463 tree proto;
21465 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21466 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21467 proto = cp_parser_identifier (parser);
21468 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21470 return objc_build_protocol_expr (proto);
21473 /* Parse an Objective-C selector expression.
21475 objc-selector-expression:
21476 @selector ( objc-method-signature )
21478 objc-method-signature:
21479 objc-selector
21480 objc-selector-seq
21482 objc-selector-seq:
21483 objc-selector :
21484 objc-selector-seq objc-selector :
21486 Returns a representation of the method selector. */
21488 static tree
21489 cp_parser_objc_selector_expression (cp_parser* parser)
21491 tree sel_seq = NULL_TREE;
21492 bool maybe_unary_selector_p = true;
21493 cp_token *token;
21494 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21496 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21497 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21498 token = cp_lexer_peek_token (parser->lexer);
21500 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21501 || token->type == CPP_SCOPE)
21503 tree selector = NULL_TREE;
21505 if (token->type != CPP_COLON
21506 || token->type == CPP_SCOPE)
21507 selector = cp_parser_objc_selector (parser);
21509 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21510 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21512 /* Detect if we have a unary selector. */
21513 if (maybe_unary_selector_p)
21515 sel_seq = selector;
21516 goto finish_selector;
21518 else
21520 cp_parser_error (parser, "expected %<:%>");
21523 maybe_unary_selector_p = false;
21524 token = cp_lexer_consume_token (parser->lexer);
21526 if (token->type == CPP_SCOPE)
21528 sel_seq
21529 = chainon (sel_seq,
21530 build_tree_list (selector, NULL_TREE));
21531 sel_seq
21532 = chainon (sel_seq,
21533 build_tree_list (NULL_TREE, NULL_TREE));
21535 else
21536 sel_seq
21537 = chainon (sel_seq,
21538 build_tree_list (selector, NULL_TREE));
21540 token = cp_lexer_peek_token (parser->lexer);
21543 finish_selector:
21544 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21546 return objc_build_selector_expr (loc, sel_seq);
21549 /* Parse a list of identifiers.
21551 objc-identifier-list:
21552 identifier
21553 objc-identifier-list , identifier
21555 Returns a TREE_LIST of identifier nodes. */
21557 static tree
21558 cp_parser_objc_identifier_list (cp_parser* parser)
21560 tree identifier;
21561 tree list;
21562 cp_token *sep;
21564 identifier = cp_parser_identifier (parser);
21565 if (identifier == error_mark_node)
21566 return error_mark_node;
21568 list = build_tree_list (NULL_TREE, identifier);
21569 sep = cp_lexer_peek_token (parser->lexer);
21571 while (sep->type == CPP_COMMA)
21573 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21574 identifier = cp_parser_identifier (parser);
21575 if (identifier == error_mark_node)
21576 return list;
21578 list = chainon (list, build_tree_list (NULL_TREE,
21579 identifier));
21580 sep = cp_lexer_peek_token (parser->lexer);
21583 return list;
21586 /* Parse an Objective-C alias declaration.
21588 objc-alias-declaration:
21589 @compatibility_alias identifier identifier ;
21591 This function registers the alias mapping with the Objective-C front end.
21592 It returns nothing. */
21594 static void
21595 cp_parser_objc_alias_declaration (cp_parser* parser)
21597 tree alias, orig;
21599 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21600 alias = cp_parser_identifier (parser);
21601 orig = cp_parser_identifier (parser);
21602 objc_declare_alias (alias, orig);
21603 cp_parser_consume_semicolon_at_end_of_statement (parser);
21606 /* Parse an Objective-C class forward-declaration.
21608 objc-class-declaration:
21609 @class objc-identifier-list ;
21611 The function registers the forward declarations with the Objective-C
21612 front end. It returns nothing. */
21614 static void
21615 cp_parser_objc_class_declaration (cp_parser* parser)
21617 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21618 while (true)
21620 tree id;
21622 id = cp_parser_identifier (parser);
21623 if (id == error_mark_node)
21624 break;
21626 objc_declare_class (id);
21628 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21629 cp_lexer_consume_token (parser->lexer);
21630 else
21631 break;
21633 cp_parser_consume_semicolon_at_end_of_statement (parser);
21636 /* Parse a list of Objective-C protocol references.
21638 objc-protocol-refs-opt:
21639 objc-protocol-refs [opt]
21641 objc-protocol-refs:
21642 < objc-identifier-list >
21644 Returns a TREE_LIST of identifiers, if any. */
21646 static tree
21647 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21649 tree protorefs = NULL_TREE;
21651 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21653 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21654 protorefs = cp_parser_objc_identifier_list (parser);
21655 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21658 return protorefs;
21661 /* Parse a Objective-C visibility specification. */
21663 static void
21664 cp_parser_objc_visibility_spec (cp_parser* parser)
21666 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21668 switch (vis->keyword)
21670 case RID_AT_PRIVATE:
21671 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21672 break;
21673 case RID_AT_PROTECTED:
21674 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21675 break;
21676 case RID_AT_PUBLIC:
21677 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21678 break;
21679 case RID_AT_PACKAGE:
21680 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21681 break;
21682 default:
21683 return;
21686 /* Eat '@private'/'@protected'/'@public'. */
21687 cp_lexer_consume_token (parser->lexer);
21690 /* Parse an Objective-C method type. Return 'true' if it is a class
21691 (+) method, and 'false' if it is an instance (-) method. */
21693 static inline bool
21694 cp_parser_objc_method_type (cp_parser* parser)
21696 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21697 return true;
21698 else
21699 return false;
21702 /* Parse an Objective-C protocol qualifier. */
21704 static tree
21705 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21707 tree quals = NULL_TREE, node;
21708 cp_token *token = cp_lexer_peek_token (parser->lexer);
21710 node = token->u.value;
21712 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21713 && (node == ridpointers [(int) RID_IN]
21714 || node == ridpointers [(int) RID_OUT]
21715 || node == ridpointers [(int) RID_INOUT]
21716 || node == ridpointers [(int) RID_BYCOPY]
21717 || node == ridpointers [(int) RID_BYREF]
21718 || node == ridpointers [(int) RID_ONEWAY]))
21720 quals = tree_cons (NULL_TREE, node, quals);
21721 cp_lexer_consume_token (parser->lexer);
21722 token = cp_lexer_peek_token (parser->lexer);
21723 node = token->u.value;
21726 return quals;
21729 /* Parse an Objective-C typename. */
21731 static tree
21732 cp_parser_objc_typename (cp_parser* parser)
21734 tree type_name = NULL_TREE;
21736 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21738 tree proto_quals, cp_type = NULL_TREE;
21740 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21741 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21743 /* An ObjC type name may consist of just protocol qualifiers, in which
21744 case the type shall default to 'id'. */
21745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21747 cp_type = cp_parser_type_id (parser);
21749 /* If the type could not be parsed, an error has already
21750 been produced. For error recovery, behave as if it had
21751 not been specified, which will use the default type
21752 'id'. */
21753 if (cp_type == error_mark_node)
21755 cp_type = NULL_TREE;
21756 /* We need to skip to the closing parenthesis as
21757 cp_parser_type_id() does not seem to do it for
21758 us. */
21759 cp_parser_skip_to_closing_parenthesis (parser,
21760 /*recovering=*/true,
21761 /*or_comma=*/false,
21762 /*consume_paren=*/false);
21766 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21767 type_name = build_tree_list (proto_quals, cp_type);
21770 return type_name;
21773 /* Check to see if TYPE refers to an Objective-C selector name. */
21775 static bool
21776 cp_parser_objc_selector_p (enum cpp_ttype type)
21778 return (type == CPP_NAME || type == CPP_KEYWORD
21779 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21780 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21781 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21782 || type == CPP_XOR || type == CPP_XOR_EQ);
21785 /* Parse an Objective-C selector. */
21787 static tree
21788 cp_parser_objc_selector (cp_parser* parser)
21790 cp_token *token = cp_lexer_consume_token (parser->lexer);
21792 if (!cp_parser_objc_selector_p (token->type))
21794 error_at (token->location, "invalid Objective-C++ selector name");
21795 return error_mark_node;
21798 /* C++ operator names are allowed to appear in ObjC selectors. */
21799 switch (token->type)
21801 case CPP_AND_AND: return get_identifier ("and");
21802 case CPP_AND_EQ: return get_identifier ("and_eq");
21803 case CPP_AND: return get_identifier ("bitand");
21804 case CPP_OR: return get_identifier ("bitor");
21805 case CPP_COMPL: return get_identifier ("compl");
21806 case CPP_NOT: return get_identifier ("not");
21807 case CPP_NOT_EQ: return get_identifier ("not_eq");
21808 case CPP_OR_OR: return get_identifier ("or");
21809 case CPP_OR_EQ: return get_identifier ("or_eq");
21810 case CPP_XOR: return get_identifier ("xor");
21811 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21812 default: return token->u.value;
21816 /* Parse an Objective-C params list. */
21818 static tree
21819 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21821 tree params = NULL_TREE;
21822 bool maybe_unary_selector_p = true;
21823 cp_token *token = cp_lexer_peek_token (parser->lexer);
21825 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21827 tree selector = NULL_TREE, type_name, identifier;
21828 tree parm_attr = NULL_TREE;
21830 if (token->keyword == RID_ATTRIBUTE)
21831 break;
21833 if (token->type != CPP_COLON)
21834 selector = cp_parser_objc_selector (parser);
21836 /* Detect if we have a unary selector. */
21837 if (maybe_unary_selector_p
21838 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21840 params = selector; /* Might be followed by attributes. */
21841 break;
21844 maybe_unary_selector_p = false;
21845 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21847 /* Something went quite wrong. There should be a colon
21848 here, but there is not. Stop parsing parameters. */
21849 break;
21851 type_name = cp_parser_objc_typename (parser);
21852 /* New ObjC allows attributes on parameters too. */
21853 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21854 parm_attr = cp_parser_attributes_opt (parser);
21855 identifier = cp_parser_identifier (parser);
21857 params
21858 = chainon (params,
21859 objc_build_keyword_decl (selector,
21860 type_name,
21861 identifier,
21862 parm_attr));
21864 token = cp_lexer_peek_token (parser->lexer);
21867 if (params == NULL_TREE)
21869 cp_parser_error (parser, "objective-c++ method declaration is expected");
21870 return error_mark_node;
21873 /* We allow tail attributes for the method. */
21874 if (token->keyword == RID_ATTRIBUTE)
21876 *attributes = cp_parser_attributes_opt (parser);
21877 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21878 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21879 return params;
21880 cp_parser_error (parser,
21881 "method attributes must be specified at the end");
21882 return error_mark_node;
21885 if (params == NULL_TREE)
21887 cp_parser_error (parser, "objective-c++ method declaration is expected");
21888 return error_mark_node;
21890 return params;
21893 /* Parse the non-keyword Objective-C params. */
21895 static tree
21896 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
21897 tree* attributes)
21899 tree params = make_node (TREE_LIST);
21900 cp_token *token = cp_lexer_peek_token (parser->lexer);
21901 *ellipsisp = false; /* Initially, assume no ellipsis. */
21903 while (token->type == CPP_COMMA)
21905 cp_parameter_declarator *parmdecl;
21906 tree parm;
21908 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21909 token = cp_lexer_peek_token (parser->lexer);
21911 if (token->type == CPP_ELLIPSIS)
21913 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
21914 *ellipsisp = true;
21915 token = cp_lexer_peek_token (parser->lexer);
21916 break;
21919 /* TODO: parse attributes for tail parameters. */
21920 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21921 parm = grokdeclarator (parmdecl->declarator,
21922 &parmdecl->decl_specifiers,
21923 PARM, /*initialized=*/0,
21924 /*attrlist=*/NULL);
21926 chainon (params, build_tree_list (NULL_TREE, parm));
21927 token = cp_lexer_peek_token (parser->lexer);
21930 /* We allow tail attributes for the method. */
21931 if (token->keyword == RID_ATTRIBUTE)
21933 if (*attributes == NULL_TREE)
21935 *attributes = cp_parser_attributes_opt (parser);
21936 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21937 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21938 return params;
21940 else
21941 /* We have an error, but parse the attributes, so that we can
21942 carry on. */
21943 *attributes = cp_parser_attributes_opt (parser);
21945 cp_parser_error (parser,
21946 "method attributes must be specified at the end");
21947 return error_mark_node;
21950 return params;
21953 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
21955 static void
21956 cp_parser_objc_interstitial_code (cp_parser* parser)
21958 cp_token *token = cp_lexer_peek_token (parser->lexer);
21960 /* If the next token is `extern' and the following token is a string
21961 literal, then we have a linkage specification. */
21962 if (token->keyword == RID_EXTERN
21963 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
21964 cp_parser_linkage_specification (parser);
21965 /* Handle #pragma, if any. */
21966 else if (token->type == CPP_PRAGMA)
21967 cp_parser_pragma (parser, pragma_external);
21968 /* Allow stray semicolons. */
21969 else if (token->type == CPP_SEMICOLON)
21970 cp_lexer_consume_token (parser->lexer);
21971 /* Mark methods as optional or required, when building protocols. */
21972 else if (token->keyword == RID_AT_OPTIONAL)
21974 cp_lexer_consume_token (parser->lexer);
21975 objc_set_method_opt (true);
21977 else if (token->keyword == RID_AT_REQUIRED)
21979 cp_lexer_consume_token (parser->lexer);
21980 objc_set_method_opt (false);
21982 else if (token->keyword == RID_NAMESPACE)
21983 cp_parser_namespace_definition (parser);
21984 /* Other stray characters must generate errors. */
21985 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
21987 cp_lexer_consume_token (parser->lexer);
21988 error ("stray %qs between Objective-C++ methods",
21989 token->type == CPP_OPEN_BRACE ? "{" : "}");
21991 /* Finally, try to parse a block-declaration, or a function-definition. */
21992 else
21993 cp_parser_block_declaration (parser, /*statement_p=*/false);
21996 /* Parse a method signature. */
21998 static tree
21999 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22001 tree rettype, kwdparms, optparms;
22002 bool ellipsis = false;
22003 bool is_class_method;
22005 is_class_method = cp_parser_objc_method_type (parser);
22006 rettype = cp_parser_objc_typename (parser);
22007 *attributes = NULL_TREE;
22008 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22009 if (kwdparms == error_mark_node)
22010 return error_mark_node;
22011 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22012 if (optparms == error_mark_node)
22013 return error_mark_node;
22015 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22018 static bool
22019 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22021 tree tattr;
22022 cp_lexer_save_tokens (parser->lexer);
22023 tattr = cp_parser_attributes_opt (parser);
22024 gcc_assert (tattr) ;
22026 /* If the attributes are followed by a method introducer, this is not allowed.
22027 Dump the attributes and flag the situation. */
22028 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22029 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22030 return true;
22032 /* Otherwise, the attributes introduce some interstitial code, possibly so
22033 rewind to allow that check. */
22034 cp_lexer_rollback_tokens (parser->lexer);
22035 return false;
22038 /* Parse an Objective-C method prototype list. */
22040 static void
22041 cp_parser_objc_method_prototype_list (cp_parser* parser)
22043 cp_token *token = cp_lexer_peek_token (parser->lexer);
22045 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22047 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22049 tree attributes, sig;
22050 bool is_class_method;
22051 if (token->type == CPP_PLUS)
22052 is_class_method = true;
22053 else
22054 is_class_method = false;
22055 sig = cp_parser_objc_method_signature (parser, &attributes);
22056 if (sig == error_mark_node)
22058 cp_parser_skip_to_end_of_block_or_statement (parser);
22059 token = cp_lexer_peek_token (parser->lexer);
22060 continue;
22062 objc_add_method_declaration (is_class_method, sig, attributes);
22063 cp_parser_consume_semicolon_at_end_of_statement (parser);
22065 else if (token->keyword == RID_AT_PROPERTY)
22066 cp_parser_objc_at_property_declaration (parser);
22067 else if (token->keyword == RID_ATTRIBUTE
22068 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22069 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22070 OPT_Wattributes,
22071 "prefix attributes are ignored for methods");
22072 else
22073 /* Allow for interspersed non-ObjC++ code. */
22074 cp_parser_objc_interstitial_code (parser);
22076 token = cp_lexer_peek_token (parser->lexer);
22079 if (token->type != CPP_EOF)
22080 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22081 else
22082 cp_parser_error (parser, "expected %<@end%>");
22084 objc_finish_interface ();
22087 /* Parse an Objective-C method definition list. */
22089 static void
22090 cp_parser_objc_method_definition_list (cp_parser* parser)
22092 cp_token *token = cp_lexer_peek_token (parser->lexer);
22094 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22096 tree meth;
22098 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22100 cp_token *ptk;
22101 tree sig, attribute;
22102 bool is_class_method;
22103 if (token->type == CPP_PLUS)
22104 is_class_method = true;
22105 else
22106 is_class_method = false;
22107 push_deferring_access_checks (dk_deferred);
22108 sig = cp_parser_objc_method_signature (parser, &attribute);
22109 if (sig == error_mark_node)
22111 cp_parser_skip_to_end_of_block_or_statement (parser);
22112 token = cp_lexer_peek_token (parser->lexer);
22113 continue;
22115 objc_start_method_definition (is_class_method, sig, attribute);
22117 /* For historical reasons, we accept an optional semicolon. */
22118 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22119 cp_lexer_consume_token (parser->lexer);
22121 ptk = cp_lexer_peek_token (parser->lexer);
22122 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22123 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22125 perform_deferred_access_checks ();
22126 stop_deferring_access_checks ();
22127 meth = cp_parser_function_definition_after_declarator (parser,
22128 false);
22129 pop_deferring_access_checks ();
22130 objc_finish_method_definition (meth);
22133 /* The following case will be removed once @synthesize is
22134 completely implemented. */
22135 else if (token->keyword == RID_AT_PROPERTY)
22136 cp_parser_objc_at_property_declaration (parser);
22137 else if (token->keyword == RID_AT_SYNTHESIZE)
22138 cp_parser_objc_at_synthesize_declaration (parser);
22139 else if (token->keyword == RID_AT_DYNAMIC)
22140 cp_parser_objc_at_dynamic_declaration (parser);
22141 else if (token->keyword == RID_ATTRIBUTE
22142 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22143 warning_at (token->location, OPT_Wattributes,
22144 "prefix attributes are ignored for methods");
22145 else
22146 /* Allow for interspersed non-ObjC++ code. */
22147 cp_parser_objc_interstitial_code (parser);
22149 token = cp_lexer_peek_token (parser->lexer);
22152 if (token->type != CPP_EOF)
22153 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22154 else
22155 cp_parser_error (parser, "expected %<@end%>");
22157 objc_finish_implementation ();
22160 /* Parse Objective-C ivars. */
22162 static void
22163 cp_parser_objc_class_ivars (cp_parser* parser)
22165 cp_token *token = cp_lexer_peek_token (parser->lexer);
22167 if (token->type != CPP_OPEN_BRACE)
22168 return; /* No ivars specified. */
22170 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22171 token = cp_lexer_peek_token (parser->lexer);
22173 while (token->type != CPP_CLOSE_BRACE
22174 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22176 cp_decl_specifier_seq declspecs;
22177 int decl_class_or_enum_p;
22178 tree prefix_attributes;
22180 cp_parser_objc_visibility_spec (parser);
22182 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22183 break;
22185 cp_parser_decl_specifier_seq (parser,
22186 CP_PARSER_FLAGS_OPTIONAL,
22187 &declspecs,
22188 &decl_class_or_enum_p);
22190 /* auto, register, static, extern, mutable. */
22191 if (declspecs.storage_class != sc_none)
22193 cp_parser_error (parser, "invalid type for instance variable");
22194 declspecs.storage_class = sc_none;
22197 /* __thread. */
22198 if (declspecs.specs[(int) ds_thread])
22200 cp_parser_error (parser, "invalid type for instance variable");
22201 declspecs.specs[(int) ds_thread] = 0;
22204 /* typedef. */
22205 if (declspecs.specs[(int) ds_typedef])
22207 cp_parser_error (parser, "invalid type for instance variable");
22208 declspecs.specs[(int) ds_typedef] = 0;
22211 prefix_attributes = declspecs.attributes;
22212 declspecs.attributes = NULL_TREE;
22214 /* Keep going until we hit the `;' at the end of the
22215 declaration. */
22216 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22218 tree width = NULL_TREE, attributes, first_attribute, decl;
22219 cp_declarator *declarator = NULL;
22220 int ctor_dtor_or_conv_p;
22222 /* Check for a (possibly unnamed) bitfield declaration. */
22223 token = cp_lexer_peek_token (parser->lexer);
22224 if (token->type == CPP_COLON)
22225 goto eat_colon;
22227 if (token->type == CPP_NAME
22228 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22229 == CPP_COLON))
22231 /* Get the name of the bitfield. */
22232 declarator = make_id_declarator (NULL_TREE,
22233 cp_parser_identifier (parser),
22234 sfk_none);
22236 eat_colon:
22237 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22238 /* Get the width of the bitfield. */
22239 width
22240 = cp_parser_constant_expression (parser,
22241 /*allow_non_constant=*/false,
22242 NULL);
22244 else
22246 /* Parse the declarator. */
22247 declarator
22248 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22249 &ctor_dtor_or_conv_p,
22250 /*parenthesized_p=*/NULL,
22251 /*member_p=*/false);
22254 /* Look for attributes that apply to the ivar. */
22255 attributes = cp_parser_attributes_opt (parser);
22256 /* Remember which attributes are prefix attributes and
22257 which are not. */
22258 first_attribute = attributes;
22259 /* Combine the attributes. */
22260 attributes = chainon (prefix_attributes, attributes);
22262 if (width)
22263 /* Create the bitfield declaration. */
22264 decl = grokbitfield (declarator, &declspecs,
22265 width,
22266 attributes);
22267 else
22268 decl = grokfield (declarator, &declspecs,
22269 NULL_TREE, /*init_const_expr_p=*/false,
22270 NULL_TREE, attributes);
22272 /* Add the instance variable. */
22273 objc_add_instance_variable (decl);
22275 /* Reset PREFIX_ATTRIBUTES. */
22276 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22277 attributes = TREE_CHAIN (attributes);
22278 if (attributes)
22279 TREE_CHAIN (attributes) = NULL_TREE;
22281 token = cp_lexer_peek_token (parser->lexer);
22283 if (token->type == CPP_COMMA)
22285 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22286 continue;
22288 break;
22291 cp_parser_consume_semicolon_at_end_of_statement (parser);
22292 token = cp_lexer_peek_token (parser->lexer);
22295 if (token->keyword == RID_AT_END)
22296 cp_parser_error (parser, "expected %<}%>");
22298 /* Do not consume the RID_AT_END, so it will be read again as terminating
22299 the @interface of @implementation. */
22300 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22301 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22303 /* For historical reasons, we accept an optional semicolon. */
22304 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22305 cp_lexer_consume_token (parser->lexer);
22308 /* Parse an Objective-C protocol declaration. */
22310 static void
22311 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22313 tree proto, protorefs;
22314 cp_token *tok;
22316 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22317 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22319 tok = cp_lexer_peek_token (parser->lexer);
22320 error_at (tok->location, "identifier expected after %<@protocol%>");
22321 goto finish;
22324 /* See if we have a forward declaration or a definition. */
22325 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22327 /* Try a forward declaration first. */
22328 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22330 objc_declare_protocols (cp_parser_objc_identifier_list (parser),
22331 attributes);
22332 finish:
22333 cp_parser_consume_semicolon_at_end_of_statement (parser);
22336 /* Ok, we got a full-fledged definition (or at least should). */
22337 else
22339 proto = cp_parser_identifier (parser);
22340 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22341 objc_start_protocol (proto, protorefs, attributes);
22342 cp_parser_objc_method_prototype_list (parser);
22346 /* Parse an Objective-C superclass or category. */
22348 static void
22349 cp_parser_objc_superclass_or_category (cp_parser *parser,
22350 bool iface_p,
22351 tree *super,
22352 tree *categ, bool *is_class_extension)
22354 cp_token *next = cp_lexer_peek_token (parser->lexer);
22356 *super = *categ = NULL_TREE;
22357 *is_class_extension = false;
22358 if (next->type == CPP_COLON)
22360 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22361 *super = cp_parser_identifier (parser);
22363 else if (next->type == CPP_OPEN_PAREN)
22365 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22367 /* If there is no category name, and this is an @interface, we
22368 have a class extension. */
22369 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22371 *categ = NULL_TREE;
22372 *is_class_extension = true;
22374 else
22375 *categ = cp_parser_identifier (parser);
22377 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22381 /* Parse an Objective-C class interface. */
22383 static void
22384 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22386 tree name, super, categ, protos;
22387 bool is_class_extension;
22389 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22390 name = cp_parser_identifier (parser);
22391 if (name == error_mark_node)
22393 /* It's hard to recover because even if valid @interface stuff
22394 is to follow, we can't compile it (or validate it) if we
22395 don't even know which class it refers to. Let's assume this
22396 was a stray '@interface' token in the stream and skip it.
22398 return;
22400 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22401 &is_class_extension);
22402 protos = cp_parser_objc_protocol_refs_opt (parser);
22404 /* We have either a class or a category on our hands. */
22405 if (categ || is_class_extension)
22406 objc_start_category_interface (name, categ, protos, attributes);
22407 else
22409 objc_start_class_interface (name, super, protos, attributes);
22410 /* Handle instance variable declarations, if any. */
22411 cp_parser_objc_class_ivars (parser);
22412 objc_continue_interface ();
22415 cp_parser_objc_method_prototype_list (parser);
22418 /* Parse an Objective-C class implementation. */
22420 static void
22421 cp_parser_objc_class_implementation (cp_parser* parser)
22423 tree name, super, categ;
22424 bool is_class_extension;
22426 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22427 name = cp_parser_identifier (parser);
22428 if (name == error_mark_node)
22430 /* It's hard to recover because even if valid @implementation
22431 stuff is to follow, we can't compile it (or validate it) if
22432 we don't even know which class it refers to. Let's assume
22433 this was a stray '@implementation' token in the stream and
22434 skip it.
22436 return;
22438 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22439 &is_class_extension);
22441 /* We have either a class or a category on our hands. */
22442 if (categ)
22443 objc_start_category_implementation (name, categ);
22444 else
22446 objc_start_class_implementation (name, super);
22447 /* Handle instance variable declarations, if any. */
22448 cp_parser_objc_class_ivars (parser);
22449 objc_continue_implementation ();
22452 cp_parser_objc_method_definition_list (parser);
22455 /* Consume the @end token and finish off the implementation. */
22457 static void
22458 cp_parser_objc_end_implementation (cp_parser* parser)
22460 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22461 objc_finish_implementation ();
22464 /* Parse an Objective-C declaration. */
22466 static void
22467 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22469 /* Try to figure out what kind of declaration is present. */
22470 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22472 if (attributes)
22473 switch (kwd->keyword)
22475 case RID_AT_ALIAS:
22476 case RID_AT_CLASS:
22477 case RID_AT_END:
22478 error_at (kwd->location, "attributes may not be specified before"
22479 " the %<@%D%> Objective-C++ keyword",
22480 kwd->u.value);
22481 attributes = NULL;
22482 break;
22483 case RID_AT_IMPLEMENTATION:
22484 warning_at (kwd->location, OPT_Wattributes,
22485 "prefix attributes are ignored before %<@%D%>",
22486 kwd->u.value);
22487 attributes = NULL;
22488 default:
22489 break;
22492 switch (kwd->keyword)
22494 case RID_AT_ALIAS:
22495 cp_parser_objc_alias_declaration (parser);
22496 break;
22497 case RID_AT_CLASS:
22498 cp_parser_objc_class_declaration (parser);
22499 break;
22500 case RID_AT_PROTOCOL:
22501 cp_parser_objc_protocol_declaration (parser, attributes);
22502 break;
22503 case RID_AT_INTERFACE:
22504 cp_parser_objc_class_interface (parser, attributes);
22505 break;
22506 case RID_AT_IMPLEMENTATION:
22507 cp_parser_objc_class_implementation (parser);
22508 break;
22509 case RID_AT_END:
22510 cp_parser_objc_end_implementation (parser);
22511 break;
22512 default:
22513 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22514 kwd->u.value);
22515 cp_parser_skip_to_end_of_block_or_statement (parser);
22519 /* Parse an Objective-C try-catch-finally statement.
22521 objc-try-catch-finally-stmt:
22522 @try compound-statement objc-catch-clause-seq [opt]
22523 objc-finally-clause [opt]
22525 objc-catch-clause-seq:
22526 objc-catch-clause objc-catch-clause-seq [opt]
22528 objc-catch-clause:
22529 @catch ( objc-exception-declaration ) compound-statement
22531 objc-finally-clause:
22532 @finally compound-statement
22534 objc-exception-declaration:
22535 parameter-declaration
22536 '...'
22538 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22540 Returns NULL_TREE.
22542 PS: This function is identical to c_parser_objc_try_catch_finally_statement
22543 for C. Keep them in sync. */
22545 static tree
22546 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22548 location_t location;
22549 tree stmt;
22551 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22552 location = cp_lexer_peek_token (parser->lexer)->location;
22553 objc_maybe_warn_exceptions (location);
22554 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22555 node, lest it get absorbed into the surrounding block. */
22556 stmt = push_stmt_list ();
22557 cp_parser_compound_statement (parser, NULL, false, false);
22558 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22560 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22562 cp_parameter_declarator *parm;
22563 tree parameter_declaration = error_mark_node;
22564 bool seen_open_paren = false;
22566 cp_lexer_consume_token (parser->lexer);
22567 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22568 seen_open_paren = true;
22569 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22571 /* We have "@catch (...)" (where the '...' are literally
22572 what is in the code). Skip the '...'.
22573 parameter_declaration is set to NULL_TREE, and
22574 objc_being_catch_clauses() knows that that means
22575 '...'. */
22576 cp_lexer_consume_token (parser->lexer);
22577 parameter_declaration = NULL_TREE;
22579 else
22581 /* We have "@catch (NSException *exception)" or something
22582 like that. Parse the parameter declaration. */
22583 parm = cp_parser_parameter_declaration (parser, false, NULL);
22584 if (parm == NULL)
22585 parameter_declaration = error_mark_node;
22586 else
22587 parameter_declaration = grokdeclarator (parm->declarator,
22588 &parm->decl_specifiers,
22589 PARM, /*initialized=*/0,
22590 /*attrlist=*/NULL);
22592 if (seen_open_paren)
22593 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22594 else
22596 /* If there was no open parenthesis, we are recovering from
22597 an error, and we are trying to figure out what mistake
22598 the user has made. */
22600 /* If there is an immediate closing parenthesis, the user
22601 probably forgot the opening one (ie, they typed "@catch
22602 NSException *e)". Parse the closing parenthesis and keep
22603 going. */
22604 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22605 cp_lexer_consume_token (parser->lexer);
22607 /* If these is no immediate closing parenthesis, the user
22608 probably doesn't know that parenthesis are required at
22609 all (ie, they typed "@catch NSException *e"). So, just
22610 forget about the closing parenthesis and keep going. */
22612 objc_begin_catch_clause (parameter_declaration);
22613 cp_parser_compound_statement (parser, NULL, false, false);
22614 objc_finish_catch_clause ();
22616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22618 cp_lexer_consume_token (parser->lexer);
22619 location = cp_lexer_peek_token (parser->lexer)->location;
22620 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22621 node, lest it get absorbed into the surrounding block. */
22622 stmt = push_stmt_list ();
22623 cp_parser_compound_statement (parser, NULL, false, false);
22624 objc_build_finally_clause (location, pop_stmt_list (stmt));
22627 return objc_finish_try_stmt ();
22630 /* Parse an Objective-C synchronized statement.
22632 objc-synchronized-stmt:
22633 @synchronized ( expression ) compound-statement
22635 Returns NULL_TREE. */
22637 static tree
22638 cp_parser_objc_synchronized_statement (cp_parser *parser)
22640 location_t location;
22641 tree lock, stmt;
22643 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22645 location = cp_lexer_peek_token (parser->lexer)->location;
22646 objc_maybe_warn_exceptions (location);
22647 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22648 lock = cp_parser_expression (parser, false, NULL);
22649 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22651 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22652 node, lest it get absorbed into the surrounding block. */
22653 stmt = push_stmt_list ();
22654 cp_parser_compound_statement (parser, NULL, false, false);
22656 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22659 /* Parse an Objective-C throw statement.
22661 objc-throw-stmt:
22662 @throw assignment-expression [opt] ;
22664 Returns a constructed '@throw' statement. */
22666 static tree
22667 cp_parser_objc_throw_statement (cp_parser *parser)
22669 tree expr = NULL_TREE;
22670 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22672 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22675 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22677 cp_parser_consume_semicolon_at_end_of_statement (parser);
22679 return objc_build_throw_stmt (loc, expr);
22682 /* Parse an Objective-C statement. */
22684 static tree
22685 cp_parser_objc_statement (cp_parser * parser)
22687 /* Try to figure out what kind of declaration is present. */
22688 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22690 switch (kwd->keyword)
22692 case RID_AT_TRY:
22693 return cp_parser_objc_try_catch_finally_statement (parser);
22694 case RID_AT_SYNCHRONIZED:
22695 return cp_parser_objc_synchronized_statement (parser);
22696 case RID_AT_THROW:
22697 return cp_parser_objc_throw_statement (parser);
22698 default:
22699 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22700 kwd->u.value);
22701 cp_parser_skip_to_end_of_block_or_statement (parser);
22704 return error_mark_node;
22707 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22708 look ahead to see if an objc keyword follows the attributes. This
22709 is to detect the use of prefix attributes on ObjC @interface and
22710 @protocol. */
22712 static bool
22713 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22715 cp_lexer_save_tokens (parser->lexer);
22716 *attrib = cp_parser_attributes_opt (parser);
22717 gcc_assert (*attrib);
22718 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22720 cp_lexer_commit_tokens (parser->lexer);
22721 return true;
22723 cp_lexer_rollback_tokens (parser->lexer);
22724 return false;
22727 /* This routine is a minimal replacement for
22728 c_parser_struct_declaration () used when parsing the list of
22729 types/names or ObjC++ properties. For example, when parsing the
22730 code
22732 @property (readonly) int a, b, c;
22734 this function is responsible for parsing "int a, int b, int c" and
22735 returning the declarations as CHAIN of DECLs.
22737 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22738 similar parsing. */
22739 static tree
22740 cp_parser_objc_struct_declaration (cp_parser *parser)
22742 tree decls = NULL_TREE;
22743 cp_decl_specifier_seq declspecs;
22744 int decl_class_or_enum_p;
22745 tree prefix_attributes;
22747 cp_parser_decl_specifier_seq (parser,
22748 CP_PARSER_FLAGS_NONE,
22749 &declspecs,
22750 &decl_class_or_enum_p);
22752 if (declspecs.type == error_mark_node)
22753 return error_mark_node;
22755 /* auto, register, static, extern, mutable. */
22756 if (declspecs.storage_class != sc_none)
22758 cp_parser_error (parser, "invalid type for property");
22759 declspecs.storage_class = sc_none;
22762 /* __thread. */
22763 if (declspecs.specs[(int) ds_thread])
22765 cp_parser_error (parser, "invalid type for property");
22766 declspecs.specs[(int) ds_thread] = 0;
22769 /* typedef. */
22770 if (declspecs.specs[(int) ds_typedef])
22772 cp_parser_error (parser, "invalid type for property");
22773 declspecs.specs[(int) ds_typedef] = 0;
22776 prefix_attributes = declspecs.attributes;
22777 declspecs.attributes = NULL_TREE;
22779 /* Keep going until we hit the `;' at the end of the declaration. */
22780 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22782 tree attributes, first_attribute, decl;
22783 cp_declarator *declarator;
22784 cp_token *token;
22786 /* Parse the declarator. */
22787 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22788 NULL, NULL, false);
22790 /* Look for attributes that apply to the ivar. */
22791 attributes = cp_parser_attributes_opt (parser);
22792 /* Remember which attributes are prefix attributes and
22793 which are not. */
22794 first_attribute = attributes;
22795 /* Combine the attributes. */
22796 attributes = chainon (prefix_attributes, attributes);
22798 decl = grokfield (declarator, &declspecs,
22799 NULL_TREE, /*init_const_expr_p=*/false,
22800 NULL_TREE, attributes);
22802 if (decl == error_mark_node || decl == NULL_TREE)
22803 return error_mark_node;
22805 /* Reset PREFIX_ATTRIBUTES. */
22806 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22807 attributes = TREE_CHAIN (attributes);
22808 if (attributes)
22809 TREE_CHAIN (attributes) = NULL_TREE;
22811 DECL_CHAIN (decl) = decls;
22812 decls = decl;
22814 token = cp_lexer_peek_token (parser->lexer);
22815 if (token->type == CPP_COMMA)
22817 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22818 continue;
22820 else
22821 break;
22823 return decls;
22826 /* Parse an Objective-C @property declaration. The syntax is:
22828 objc-property-declaration:
22829 '@property' objc-property-attributes[opt] struct-declaration ;
22831 objc-property-attributes:
22832 '(' objc-property-attribute-list ')'
22834 objc-property-attribute-list:
22835 objc-property-attribute
22836 objc-property-attribute-list, objc-property-attribute
22838 objc-property-attribute
22839 'getter' = identifier
22840 'setter' = identifier
22841 'readonly'
22842 'readwrite'
22843 'assign'
22844 'retain'
22845 'copy'
22846 'nonatomic'
22848 For example:
22849 @property NSString *name;
22850 @property (readonly) id object;
22851 @property (retain, nonatomic, getter=getTheName) id name;
22852 @property int a, b, c;
22854 PS: This function is identical to
22855 c_parser_objc_at_property_declaration for C. Keep them in sync. */
22856 static void
22857 cp_parser_objc_at_property_declaration (cp_parser *parser)
22859 /* The following variables hold the attributes of the properties as
22860 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
22861 seen. When we see an attribute, we set them to 'true' (if they
22862 are boolean properties) or to the identifier (if they have an
22863 argument, ie, for getter and setter). Note that here we only
22864 parse the list of attributes, check the syntax and accumulate the
22865 attributes that we find. objc_add_property_declaration() will
22866 then process the information. */
22867 bool property_assign = false;
22868 bool property_copy = false;
22869 tree property_getter_ident = NULL_TREE;
22870 bool property_nonatomic = false;
22871 bool property_readonly = false;
22872 bool property_readwrite = false;
22873 bool property_retain = false;
22874 tree property_setter_ident = NULL_TREE;
22876 /* 'properties' is the list of properties that we read. Usually a
22877 single one, but maybe more (eg, in "@property int a, b, c;" there
22878 are three). */
22879 tree properties;
22880 location_t loc;
22882 loc = cp_lexer_peek_token (parser->lexer)->location;
22884 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
22886 /* Parse the optional attribute list... */
22887 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22889 /* Eat the '('. */
22890 cp_lexer_consume_token (parser->lexer);
22892 while (true)
22894 bool syntax_error = false;
22895 cp_token *token = cp_lexer_peek_token (parser->lexer);
22896 enum rid keyword;
22898 if (token->type != CPP_NAME)
22900 cp_parser_error (parser, "expected identifier");
22901 break;
22903 keyword = C_RID_CODE (token->u.value);
22904 cp_lexer_consume_token (parser->lexer);
22905 switch (keyword)
22907 case RID_ASSIGN: property_assign = true; break;
22908 case RID_COPY: property_copy = true; break;
22909 case RID_NONATOMIC: property_nonatomic = true; break;
22910 case RID_READONLY: property_readonly = true; break;
22911 case RID_READWRITE: property_readwrite = true; break;
22912 case RID_RETAIN: property_retain = true; break;
22914 case RID_GETTER:
22915 case RID_SETTER:
22916 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22918 if (keyword == RID_GETTER)
22919 cp_parser_error (parser,
22920 "missing %<=%> (after %<getter%> attribute)");
22921 else
22922 cp_parser_error (parser,
22923 "missing %<=%> (after %<setter%> attribute)");
22924 syntax_error = true;
22925 break;
22927 cp_lexer_consume_token (parser->lexer); /* eat the = */
22928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22930 cp_parser_error (parser, "expected identifier");
22931 syntax_error = true;
22932 break;
22934 if (keyword == RID_SETTER)
22936 if (property_setter_ident != NULL_TREE)
22937 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
22938 else
22939 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22940 cp_lexer_consume_token (parser->lexer);
22941 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22942 cp_parser_error (parser, "setter name must terminate with %<:%>");
22943 else
22944 cp_lexer_consume_token (parser->lexer);
22946 else
22948 if (property_getter_ident != NULL_TREE)
22949 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
22950 else
22951 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
22952 cp_lexer_consume_token (parser->lexer);
22954 break;
22955 default:
22956 cp_parser_error (parser, "unknown property attribute");
22957 syntax_error = true;
22958 break;
22961 if (syntax_error)
22962 break;
22964 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22965 cp_lexer_consume_token (parser->lexer);
22966 else
22967 break;
22970 /* FIXME: "@property (setter, assign);" will generate a spurious
22971 "error: expected ‘)’ before ‘,’ token". This is because
22972 cp_parser_require, unlike the C counterpart, will produce an
22973 error even if we are in error recovery. */
22974 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22976 cp_parser_skip_to_closing_parenthesis (parser,
22977 /*recovering=*/true,
22978 /*or_comma=*/false,
22979 /*consume_paren=*/true);
22983 /* ... and the property declaration(s). */
22984 properties = cp_parser_objc_struct_declaration (parser);
22986 if (properties == error_mark_node)
22988 cp_parser_skip_to_end_of_statement (parser);
22989 /* If the next token is now a `;', consume it. */
22990 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22991 cp_lexer_consume_token (parser->lexer);
22992 return;
22995 if (properties == NULL_TREE)
22996 cp_parser_error (parser, "expected identifier");
22997 else
22999 /* Comma-separated properties are chained together in
23000 reverse order; add them one by one. */
23001 properties = nreverse (properties);
23003 for (; properties; properties = TREE_CHAIN (properties))
23004 objc_add_property_declaration (loc, copy_node (properties),
23005 property_readonly, property_readwrite,
23006 property_assign, property_retain,
23007 property_copy, property_nonatomic,
23008 property_getter_ident, property_setter_ident);
23011 cp_parser_consume_semicolon_at_end_of_statement (parser);
23014 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
23016 objc-synthesize-declaration:
23017 @synthesize objc-synthesize-identifier-list ;
23019 objc-synthesize-identifier-list:
23020 objc-synthesize-identifier
23021 objc-synthesize-identifier-list, objc-synthesize-identifier
23023 objc-synthesize-identifier
23024 identifier
23025 identifier = identifier
23027 For example:
23028 @synthesize MyProperty;
23029 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23031 PS: This function is identical to c_parser_objc_at_synthesize_declaration
23032 for C. Keep them in sync.
23034 static void
23035 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23037 tree list = NULL_TREE;
23038 location_t loc;
23039 loc = cp_lexer_peek_token (parser->lexer)->location;
23041 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
23042 while (true)
23044 tree property, ivar;
23045 property = cp_parser_identifier (parser);
23046 if (property == error_mark_node)
23048 cp_parser_consume_semicolon_at_end_of_statement (parser);
23049 return;
23051 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23053 cp_lexer_consume_token (parser->lexer);
23054 ivar = cp_parser_identifier (parser);
23055 if (ivar == error_mark_node)
23057 cp_parser_consume_semicolon_at_end_of_statement (parser);
23058 return;
23061 else
23062 ivar = NULL_TREE;
23063 list = chainon (list, build_tree_list (ivar, property));
23064 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23065 cp_lexer_consume_token (parser->lexer);
23066 else
23067 break;
23069 cp_parser_consume_semicolon_at_end_of_statement (parser);
23070 objc_add_synthesize_declaration (loc, list);
23073 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
23075 objc-dynamic-declaration:
23076 @dynamic identifier-list ;
23078 For example:
23079 @dynamic MyProperty;
23080 @dynamic MyProperty, AnotherProperty;
23082 PS: This function is identical to c_parser_objc_at_dynamic_declaration
23083 for C. Keep them in sync.
23085 static void
23086 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23088 tree list = NULL_TREE;
23089 location_t loc;
23090 loc = cp_lexer_peek_token (parser->lexer)->location;
23092 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23093 while (true)
23095 tree property;
23096 property = cp_parser_identifier (parser);
23097 if (property == error_mark_node)
23099 cp_parser_consume_semicolon_at_end_of_statement (parser);
23100 return;
23102 list = chainon (list, build_tree_list (NULL, property));
23103 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23104 cp_lexer_consume_token (parser->lexer);
23105 else
23106 break;
23108 cp_parser_consume_semicolon_at_end_of_statement (parser);
23109 objc_add_dynamic_declaration (loc, list);
23113 /* OpenMP 2.5 parsing routines. */
23115 /* Returns name of the next clause.
23116 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23117 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23118 returned and the token is consumed. */
23120 static pragma_omp_clause
23121 cp_parser_omp_clause_name (cp_parser *parser)
23123 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23125 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23126 result = PRAGMA_OMP_CLAUSE_IF;
23127 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23128 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23129 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23130 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23131 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23133 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23134 const char *p = IDENTIFIER_POINTER (id);
23136 switch (p[0])
23138 case 'c':
23139 if (!strcmp ("collapse", p))
23140 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23141 else if (!strcmp ("copyin", p))
23142 result = PRAGMA_OMP_CLAUSE_COPYIN;
23143 else if (!strcmp ("copyprivate", p))
23144 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23145 break;
23146 case 'f':
23147 if (!strcmp ("firstprivate", p))
23148 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23149 break;
23150 case 'l':
23151 if (!strcmp ("lastprivate", p))
23152 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23153 break;
23154 case 'n':
23155 if (!strcmp ("nowait", p))
23156 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23157 else if (!strcmp ("num_threads", p))
23158 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23159 break;
23160 case 'o':
23161 if (!strcmp ("ordered", p))
23162 result = PRAGMA_OMP_CLAUSE_ORDERED;
23163 break;
23164 case 'r':
23165 if (!strcmp ("reduction", p))
23166 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23167 break;
23168 case 's':
23169 if (!strcmp ("schedule", p))
23170 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23171 else if (!strcmp ("shared", p))
23172 result = PRAGMA_OMP_CLAUSE_SHARED;
23173 break;
23174 case 'u':
23175 if (!strcmp ("untied", p))
23176 result = PRAGMA_OMP_CLAUSE_UNTIED;
23177 break;
23181 if (result != PRAGMA_OMP_CLAUSE_NONE)
23182 cp_lexer_consume_token (parser->lexer);
23184 return result;
23187 /* Validate that a clause of the given type does not already exist. */
23189 static void
23190 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23191 const char *name, location_t location)
23193 tree c;
23195 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23196 if (OMP_CLAUSE_CODE (c) == code)
23198 error_at (location, "too many %qs clauses", name);
23199 break;
23203 /* OpenMP 2.5:
23204 variable-list:
23205 identifier
23206 variable-list , identifier
23208 In addition, we match a closing parenthesis. An opening parenthesis
23209 will have been consumed by the caller.
23211 If KIND is nonzero, create the appropriate node and install the decl
23212 in OMP_CLAUSE_DECL and add the node to the head of the list.
23214 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23215 return the list created. */
23217 static tree
23218 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23219 tree list)
23221 cp_token *token;
23222 while (1)
23224 tree name, decl;
23226 token = cp_lexer_peek_token (parser->lexer);
23227 name = cp_parser_id_expression (parser, /*template_p=*/false,
23228 /*check_dependency_p=*/true,
23229 /*template_p=*/NULL,
23230 /*declarator_p=*/false,
23231 /*optional_p=*/false);
23232 if (name == error_mark_node)
23233 goto skip_comma;
23235 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23236 if (decl == error_mark_node)
23237 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23238 token->location);
23239 else if (kind != 0)
23241 tree u = build_omp_clause (token->location, kind);
23242 OMP_CLAUSE_DECL (u) = decl;
23243 OMP_CLAUSE_CHAIN (u) = list;
23244 list = u;
23246 else
23247 list = tree_cons (decl, NULL_TREE, list);
23249 get_comma:
23250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23251 break;
23252 cp_lexer_consume_token (parser->lexer);
23255 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23257 int ending;
23259 /* Try to resync to an unnested comma. Copied from
23260 cp_parser_parenthesized_expression_list. */
23261 skip_comma:
23262 ending = cp_parser_skip_to_closing_parenthesis (parser,
23263 /*recovering=*/true,
23264 /*or_comma=*/true,
23265 /*consume_paren=*/true);
23266 if (ending < 0)
23267 goto get_comma;
23270 return list;
23273 /* Similarly, but expect leading and trailing parenthesis. This is a very
23274 common case for omp clauses. */
23276 static tree
23277 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23279 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23280 return cp_parser_omp_var_list_no_open (parser, kind, list);
23281 return list;
23284 /* OpenMP 3.0:
23285 collapse ( constant-expression ) */
23287 static tree
23288 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23290 tree c, num;
23291 location_t loc;
23292 HOST_WIDE_INT n;
23294 loc = cp_lexer_peek_token (parser->lexer)->location;
23295 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23296 return list;
23298 num = cp_parser_constant_expression (parser, false, NULL);
23300 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23301 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23302 /*or_comma=*/false,
23303 /*consume_paren=*/true);
23305 if (num == error_mark_node)
23306 return list;
23307 num = fold_non_dependent_expr (num);
23308 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23309 || !host_integerp (num, 0)
23310 || (n = tree_low_cst (num, 0)) <= 0
23311 || (int) n != n)
23313 error_at (loc, "collapse argument needs positive constant integer expression");
23314 return list;
23317 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23318 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23319 OMP_CLAUSE_CHAIN (c) = list;
23320 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23322 return c;
23325 /* OpenMP 2.5:
23326 default ( shared | none ) */
23328 static tree
23329 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23331 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23332 tree c;
23334 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23335 return list;
23336 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23338 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23339 const char *p = IDENTIFIER_POINTER (id);
23341 switch (p[0])
23343 case 'n':
23344 if (strcmp ("none", p) != 0)
23345 goto invalid_kind;
23346 kind = OMP_CLAUSE_DEFAULT_NONE;
23347 break;
23349 case 's':
23350 if (strcmp ("shared", p) != 0)
23351 goto invalid_kind;
23352 kind = OMP_CLAUSE_DEFAULT_SHARED;
23353 break;
23355 default:
23356 goto invalid_kind;
23359 cp_lexer_consume_token (parser->lexer);
23361 else
23363 invalid_kind:
23364 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23367 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23368 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23369 /*or_comma=*/false,
23370 /*consume_paren=*/true);
23372 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23373 return list;
23375 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23376 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23377 OMP_CLAUSE_CHAIN (c) = list;
23378 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23380 return c;
23383 /* OpenMP 2.5:
23384 if ( expression ) */
23386 static tree
23387 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23389 tree t, c;
23391 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23392 return list;
23394 t = cp_parser_condition (parser);
23396 if (t == error_mark_node
23397 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23398 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23399 /*or_comma=*/false,
23400 /*consume_paren=*/true);
23402 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23404 c = build_omp_clause (location, OMP_CLAUSE_IF);
23405 OMP_CLAUSE_IF_EXPR (c) = t;
23406 OMP_CLAUSE_CHAIN (c) = list;
23408 return c;
23411 /* OpenMP 2.5:
23412 nowait */
23414 static tree
23415 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23416 tree list, location_t location)
23418 tree c;
23420 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23422 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23423 OMP_CLAUSE_CHAIN (c) = list;
23424 return c;
23427 /* OpenMP 2.5:
23428 num_threads ( expression ) */
23430 static tree
23431 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23432 location_t location)
23434 tree t, c;
23436 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23437 return list;
23439 t = cp_parser_expression (parser, false, NULL);
23441 if (t == error_mark_node
23442 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23443 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23444 /*or_comma=*/false,
23445 /*consume_paren=*/true);
23447 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23448 "num_threads", location);
23450 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23451 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23452 OMP_CLAUSE_CHAIN (c) = list;
23454 return c;
23457 /* OpenMP 2.5:
23458 ordered */
23460 static tree
23461 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23462 tree list, location_t location)
23464 tree c;
23466 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23467 "ordered", location);
23469 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23470 OMP_CLAUSE_CHAIN (c) = list;
23471 return c;
23474 /* OpenMP 2.5:
23475 reduction ( reduction-operator : variable-list )
23477 reduction-operator:
23478 One of: + * - & ^ | && || */
23480 static tree
23481 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23483 enum tree_code code;
23484 tree nlist, c;
23486 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23487 return list;
23489 switch (cp_lexer_peek_token (parser->lexer)->type)
23491 case CPP_PLUS:
23492 code = PLUS_EXPR;
23493 break;
23494 case CPP_MULT:
23495 code = MULT_EXPR;
23496 break;
23497 case CPP_MINUS:
23498 code = MINUS_EXPR;
23499 break;
23500 case CPP_AND:
23501 code = BIT_AND_EXPR;
23502 break;
23503 case CPP_XOR:
23504 code = BIT_XOR_EXPR;
23505 break;
23506 case CPP_OR:
23507 code = BIT_IOR_EXPR;
23508 break;
23509 case CPP_AND_AND:
23510 code = TRUTH_ANDIF_EXPR;
23511 break;
23512 case CPP_OR_OR:
23513 code = TRUTH_ORIF_EXPR;
23514 break;
23515 default:
23516 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23517 "%<|%>, %<&&%>, or %<||%>");
23518 resync_fail:
23519 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23520 /*or_comma=*/false,
23521 /*consume_paren=*/true);
23522 return list;
23524 cp_lexer_consume_token (parser->lexer);
23526 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23527 goto resync_fail;
23529 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23530 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23531 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23533 return nlist;
23536 /* OpenMP 2.5:
23537 schedule ( schedule-kind )
23538 schedule ( schedule-kind , expression )
23540 schedule-kind:
23541 static | dynamic | guided | runtime | auto */
23543 static tree
23544 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23546 tree c, t;
23548 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23549 return list;
23551 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23553 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23555 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23556 const char *p = IDENTIFIER_POINTER (id);
23558 switch (p[0])
23560 case 'd':
23561 if (strcmp ("dynamic", p) != 0)
23562 goto invalid_kind;
23563 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23564 break;
23566 case 'g':
23567 if (strcmp ("guided", p) != 0)
23568 goto invalid_kind;
23569 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23570 break;
23572 case 'r':
23573 if (strcmp ("runtime", p) != 0)
23574 goto invalid_kind;
23575 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23576 break;
23578 default:
23579 goto invalid_kind;
23582 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23583 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23584 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23585 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23586 else
23587 goto invalid_kind;
23588 cp_lexer_consume_token (parser->lexer);
23590 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23592 cp_token *token;
23593 cp_lexer_consume_token (parser->lexer);
23595 token = cp_lexer_peek_token (parser->lexer);
23596 t = cp_parser_assignment_expression (parser, false, NULL);
23598 if (t == error_mark_node)
23599 goto resync_fail;
23600 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23601 error_at (token->location, "schedule %<runtime%> does not take "
23602 "a %<chunk_size%> parameter");
23603 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23604 error_at (token->location, "schedule %<auto%> does not take "
23605 "a %<chunk_size%> parameter");
23606 else
23607 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23609 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23610 goto resync_fail;
23612 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23613 goto resync_fail;
23615 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23616 OMP_CLAUSE_CHAIN (c) = list;
23617 return c;
23619 invalid_kind:
23620 cp_parser_error (parser, "invalid schedule kind");
23621 resync_fail:
23622 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23623 /*or_comma=*/false,
23624 /*consume_paren=*/true);
23625 return list;
23628 /* OpenMP 3.0:
23629 untied */
23631 static tree
23632 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23633 tree list, location_t location)
23635 tree c;
23637 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23639 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23640 OMP_CLAUSE_CHAIN (c) = list;
23641 return c;
23644 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23645 is a bitmask in MASK. Return the list of clauses found; the result
23646 of clause default goes in *pdefault. */
23648 static tree
23649 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23650 const char *where, cp_token *pragma_tok)
23652 tree clauses = NULL;
23653 bool first = true;
23654 cp_token *token = NULL;
23656 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23658 pragma_omp_clause c_kind;
23659 const char *c_name;
23660 tree prev = clauses;
23662 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23663 cp_lexer_consume_token (parser->lexer);
23665 token = cp_lexer_peek_token (parser->lexer);
23666 c_kind = cp_parser_omp_clause_name (parser);
23667 first = false;
23669 switch (c_kind)
23671 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23672 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23673 token->location);
23674 c_name = "collapse";
23675 break;
23676 case PRAGMA_OMP_CLAUSE_COPYIN:
23677 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23678 c_name = "copyin";
23679 break;
23680 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23681 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23682 clauses);
23683 c_name = "copyprivate";
23684 break;
23685 case PRAGMA_OMP_CLAUSE_DEFAULT:
23686 clauses = cp_parser_omp_clause_default (parser, clauses,
23687 token->location);
23688 c_name = "default";
23689 break;
23690 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23691 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23692 clauses);
23693 c_name = "firstprivate";
23694 break;
23695 case PRAGMA_OMP_CLAUSE_IF:
23696 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23697 c_name = "if";
23698 break;
23699 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23700 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23701 clauses);
23702 c_name = "lastprivate";
23703 break;
23704 case PRAGMA_OMP_CLAUSE_NOWAIT:
23705 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23706 c_name = "nowait";
23707 break;
23708 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23709 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23710 token->location);
23711 c_name = "num_threads";
23712 break;
23713 case PRAGMA_OMP_CLAUSE_ORDERED:
23714 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23715 token->location);
23716 c_name = "ordered";
23717 break;
23718 case PRAGMA_OMP_CLAUSE_PRIVATE:
23719 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23720 clauses);
23721 c_name = "private";
23722 break;
23723 case PRAGMA_OMP_CLAUSE_REDUCTION:
23724 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23725 c_name = "reduction";
23726 break;
23727 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23728 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23729 token->location);
23730 c_name = "schedule";
23731 break;
23732 case PRAGMA_OMP_CLAUSE_SHARED:
23733 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23734 clauses);
23735 c_name = "shared";
23736 break;
23737 case PRAGMA_OMP_CLAUSE_UNTIED:
23738 clauses = cp_parser_omp_clause_untied (parser, clauses,
23739 token->location);
23740 c_name = "nowait";
23741 break;
23742 default:
23743 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23744 goto saw_error;
23747 if (((mask >> c_kind) & 1) == 0)
23749 /* Remove the invalid clause(s) from the list to avoid
23750 confusing the rest of the compiler. */
23751 clauses = prev;
23752 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23755 saw_error:
23756 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23757 return finish_omp_clauses (clauses);
23760 /* OpenMP 2.5:
23761 structured-block:
23762 statement
23764 In practice, we're also interested in adding the statement to an
23765 outer node. So it is convenient if we work around the fact that
23766 cp_parser_statement calls add_stmt. */
23768 static unsigned
23769 cp_parser_begin_omp_structured_block (cp_parser *parser)
23771 unsigned save = parser->in_statement;
23773 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23774 This preserves the "not within loop or switch" style error messages
23775 for nonsense cases like
23776 void foo() {
23777 #pragma omp single
23778 break;
23781 if (parser->in_statement)
23782 parser->in_statement = IN_OMP_BLOCK;
23784 return save;
23787 static void
23788 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23790 parser->in_statement = save;
23793 static tree
23794 cp_parser_omp_structured_block (cp_parser *parser)
23796 tree stmt = begin_omp_structured_block ();
23797 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23799 cp_parser_statement (parser, NULL_TREE, false, NULL);
23801 cp_parser_end_omp_structured_block (parser, save);
23802 return finish_omp_structured_block (stmt);
23805 /* OpenMP 2.5:
23806 # pragma omp atomic new-line
23807 expression-stmt
23809 expression-stmt:
23810 x binop= expr | x++ | ++x | x-- | --x
23811 binop:
23812 +, *, -, /, &, ^, |, <<, >>
23814 where x is an lvalue expression with scalar type. */
23816 static void
23817 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23819 tree lhs, rhs;
23820 enum tree_code code;
23822 cp_parser_require_pragma_eol (parser, pragma_tok);
23824 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23825 /*cast_p=*/false, NULL);
23826 switch (TREE_CODE (lhs))
23828 case ERROR_MARK:
23829 goto saw_error;
23831 case PREINCREMENT_EXPR:
23832 case POSTINCREMENT_EXPR:
23833 lhs = TREE_OPERAND (lhs, 0);
23834 code = PLUS_EXPR;
23835 rhs = integer_one_node;
23836 break;
23838 case PREDECREMENT_EXPR:
23839 case POSTDECREMENT_EXPR:
23840 lhs = TREE_OPERAND (lhs, 0);
23841 code = MINUS_EXPR;
23842 rhs = integer_one_node;
23843 break;
23845 case COMPOUND_EXPR:
23846 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23847 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23848 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23849 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23850 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23851 (TREE_OPERAND (lhs, 1), 0), 0)))
23852 == BOOLEAN_TYPE)
23853 /* Undo effects of boolean_increment for post {in,de}crement. */
23854 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23855 /* FALLTHRU */
23856 case MODIFY_EXPR:
23857 if (TREE_CODE (lhs) == MODIFY_EXPR
23858 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23860 /* Undo effects of boolean_increment. */
23861 if (integer_onep (TREE_OPERAND (lhs, 1)))
23863 /* This is pre or post increment. */
23864 rhs = TREE_OPERAND (lhs, 1);
23865 lhs = TREE_OPERAND (lhs, 0);
23866 code = NOP_EXPR;
23867 break;
23870 /* FALLTHRU */
23871 default:
23872 switch (cp_lexer_peek_token (parser->lexer)->type)
23874 case CPP_MULT_EQ:
23875 code = MULT_EXPR;
23876 break;
23877 case CPP_DIV_EQ:
23878 code = TRUNC_DIV_EXPR;
23879 break;
23880 case CPP_PLUS_EQ:
23881 code = PLUS_EXPR;
23882 break;
23883 case CPP_MINUS_EQ:
23884 code = MINUS_EXPR;
23885 break;
23886 case CPP_LSHIFT_EQ:
23887 code = LSHIFT_EXPR;
23888 break;
23889 case CPP_RSHIFT_EQ:
23890 code = RSHIFT_EXPR;
23891 break;
23892 case CPP_AND_EQ:
23893 code = BIT_AND_EXPR;
23894 break;
23895 case CPP_OR_EQ:
23896 code = BIT_IOR_EXPR;
23897 break;
23898 case CPP_XOR_EQ:
23899 code = BIT_XOR_EXPR;
23900 break;
23901 default:
23902 cp_parser_error (parser,
23903 "invalid operator for %<#pragma omp atomic%>");
23904 goto saw_error;
23906 cp_lexer_consume_token (parser->lexer);
23908 rhs = cp_parser_expression (parser, false, NULL);
23909 if (rhs == error_mark_node)
23910 goto saw_error;
23911 break;
23913 finish_omp_atomic (code, lhs, rhs);
23914 cp_parser_consume_semicolon_at_end_of_statement (parser);
23915 return;
23917 saw_error:
23918 cp_parser_skip_to_end_of_block_or_statement (parser);
23922 /* OpenMP 2.5:
23923 # pragma omp barrier new-line */
23925 static void
23926 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23928 cp_parser_require_pragma_eol (parser, pragma_tok);
23929 finish_omp_barrier ();
23932 /* OpenMP 2.5:
23933 # pragma omp critical [(name)] new-line
23934 structured-block */
23936 static tree
23937 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23939 tree stmt, name = NULL;
23941 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23943 cp_lexer_consume_token (parser->lexer);
23945 name = cp_parser_identifier (parser);
23947 if (name == error_mark_node
23948 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23949 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23950 /*or_comma=*/false,
23951 /*consume_paren=*/true);
23952 if (name == error_mark_node)
23953 name = NULL;
23955 cp_parser_require_pragma_eol (parser, pragma_tok);
23957 stmt = cp_parser_omp_structured_block (parser);
23958 return c_finish_omp_critical (input_location, stmt, name);
23961 /* OpenMP 2.5:
23962 # pragma omp flush flush-vars[opt] new-line
23964 flush-vars:
23965 ( variable-list ) */
23967 static void
23968 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
23970 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23971 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
23972 cp_parser_require_pragma_eol (parser, pragma_tok);
23974 finish_omp_flush ();
23977 /* Helper function, to parse omp for increment expression. */
23979 static tree
23980 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
23982 tree cond = cp_parser_binary_expression (parser, false, true,
23983 PREC_NOT_OPERATOR, NULL);
23984 bool overloaded_p;
23986 if (cond == error_mark_node
23987 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23989 cp_parser_skip_to_end_of_statement (parser);
23990 return error_mark_node;
23993 switch (TREE_CODE (cond))
23995 case GT_EXPR:
23996 case GE_EXPR:
23997 case LT_EXPR:
23998 case LE_EXPR:
23999 break;
24000 default:
24001 return error_mark_node;
24004 /* If decl is an iterator, preserve LHS and RHS of the relational
24005 expr until finish_omp_for. */
24006 if (decl
24007 && (type_dependent_expression_p (decl)
24008 || CLASS_TYPE_P (TREE_TYPE (decl))))
24009 return cond;
24011 return build_x_binary_op (TREE_CODE (cond),
24012 TREE_OPERAND (cond, 0), ERROR_MARK,
24013 TREE_OPERAND (cond, 1), ERROR_MARK,
24014 &overloaded_p, tf_warning_or_error);
24017 /* Helper function, to parse omp for increment expression. */
24019 static tree
24020 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24022 cp_token *token = cp_lexer_peek_token (parser->lexer);
24023 enum tree_code op;
24024 tree lhs, rhs;
24025 cp_id_kind idk;
24026 bool decl_first;
24028 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24030 op = (token->type == CPP_PLUS_PLUS
24031 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24032 cp_lexer_consume_token (parser->lexer);
24033 lhs = cp_parser_cast_expression (parser, false, false, NULL);
24034 if (lhs != decl)
24035 return error_mark_node;
24036 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24039 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24040 if (lhs != decl)
24041 return error_mark_node;
24043 token = cp_lexer_peek_token (parser->lexer);
24044 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24046 op = (token->type == CPP_PLUS_PLUS
24047 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24048 cp_lexer_consume_token (parser->lexer);
24049 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24052 op = cp_parser_assignment_operator_opt (parser);
24053 if (op == ERROR_MARK)
24054 return error_mark_node;
24056 if (op != NOP_EXPR)
24058 rhs = cp_parser_assignment_expression (parser, false, NULL);
24059 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24060 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24063 lhs = cp_parser_binary_expression (parser, false, false,
24064 PREC_ADDITIVE_EXPRESSION, NULL);
24065 token = cp_lexer_peek_token (parser->lexer);
24066 decl_first = lhs == decl;
24067 if (decl_first)
24068 lhs = NULL_TREE;
24069 if (token->type != CPP_PLUS
24070 && token->type != CPP_MINUS)
24071 return error_mark_node;
24075 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24076 cp_lexer_consume_token (parser->lexer);
24077 rhs = cp_parser_binary_expression (parser, false, false,
24078 PREC_ADDITIVE_EXPRESSION, NULL);
24079 token = cp_lexer_peek_token (parser->lexer);
24080 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24082 if (lhs == NULL_TREE)
24084 if (op == PLUS_EXPR)
24085 lhs = rhs;
24086 else
24087 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24089 else
24090 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24091 NULL, tf_warning_or_error);
24094 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24096 if (!decl_first)
24098 if (rhs != decl || op == MINUS_EXPR)
24099 return error_mark_node;
24100 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24102 else
24103 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24105 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24108 /* Parse the restricted form of the for statement allowed by OpenMP. */
24110 static tree
24111 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24113 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24114 tree real_decl, initv, condv, incrv, declv;
24115 tree this_pre_body, cl;
24116 location_t loc_first;
24117 bool collapse_err = false;
24118 int i, collapse = 1, nbraces = 0;
24119 VEC(tree,gc) *for_block = make_tree_vector ();
24121 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24122 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24123 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24125 gcc_assert (collapse >= 1);
24127 declv = make_tree_vec (collapse);
24128 initv = make_tree_vec (collapse);
24129 condv = make_tree_vec (collapse);
24130 incrv = make_tree_vec (collapse);
24132 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24134 for (i = 0; i < collapse; i++)
24136 int bracecount = 0;
24137 bool add_private_clause = false;
24138 location_t loc;
24140 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24142 cp_parser_error (parser, "for statement expected");
24143 return NULL;
24145 loc = cp_lexer_consume_token (parser->lexer)->location;
24147 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24148 return NULL;
24150 init = decl = real_decl = NULL;
24151 this_pre_body = push_stmt_list ();
24152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24154 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24156 init-expr:
24157 var = lb
24158 integer-type var = lb
24159 random-access-iterator-type var = lb
24160 pointer-type var = lb
24162 cp_decl_specifier_seq type_specifiers;
24164 /* First, try to parse as an initialized declaration. See
24165 cp_parser_condition, from whence the bulk of this is copied. */
24167 cp_parser_parse_tentatively (parser);
24168 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24169 /*is_trailing_return=*/false,
24170 &type_specifiers);
24171 if (cp_parser_parse_definitely (parser))
24173 /* If parsing a type specifier seq succeeded, then this
24174 MUST be a initialized declaration. */
24175 tree asm_specification, attributes;
24176 cp_declarator *declarator;
24178 declarator = cp_parser_declarator (parser,
24179 CP_PARSER_DECLARATOR_NAMED,
24180 /*ctor_dtor_or_conv_p=*/NULL,
24181 /*parenthesized_p=*/NULL,
24182 /*member_p=*/false);
24183 attributes = cp_parser_attributes_opt (parser);
24184 asm_specification = cp_parser_asm_specification_opt (parser);
24186 if (declarator == cp_error_declarator)
24187 cp_parser_skip_to_end_of_statement (parser);
24189 else
24191 tree pushed_scope, auto_node;
24193 decl = start_decl (declarator, &type_specifiers,
24194 SD_INITIALIZED, attributes,
24195 /*prefix_attributes=*/NULL_TREE,
24196 &pushed_scope);
24198 auto_node = type_uses_auto (TREE_TYPE (decl));
24199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24201 if (cp_lexer_next_token_is (parser->lexer,
24202 CPP_OPEN_PAREN))
24203 error ("parenthesized initialization is not allowed in "
24204 "OpenMP %<for%> loop");
24205 else
24206 /* Trigger an error. */
24207 cp_parser_require (parser, CPP_EQ, RT_EQ);
24209 init = error_mark_node;
24210 cp_parser_skip_to_end_of_statement (parser);
24212 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24213 || type_dependent_expression_p (decl)
24214 || auto_node)
24216 bool is_direct_init, is_non_constant_init;
24218 init = cp_parser_initializer (parser,
24219 &is_direct_init,
24220 &is_non_constant_init);
24222 if (auto_node && describable_type (init))
24224 TREE_TYPE (decl)
24225 = do_auto_deduction (TREE_TYPE (decl), init,
24226 auto_node);
24228 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24229 && !type_dependent_expression_p (decl))
24230 goto non_class;
24233 cp_finish_decl (decl, init, !is_non_constant_init,
24234 asm_specification,
24235 LOOKUP_ONLYCONVERTING);
24236 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24238 VEC_safe_push (tree, gc, for_block, this_pre_body);
24239 init = NULL_TREE;
24241 else
24242 init = pop_stmt_list (this_pre_body);
24243 this_pre_body = NULL_TREE;
24245 else
24247 /* Consume '='. */
24248 cp_lexer_consume_token (parser->lexer);
24249 init = cp_parser_assignment_expression (parser, false, NULL);
24251 non_class:
24252 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24253 init = error_mark_node;
24254 else
24255 cp_finish_decl (decl, NULL_TREE,
24256 /*init_const_expr_p=*/false,
24257 asm_specification,
24258 LOOKUP_ONLYCONVERTING);
24261 if (pushed_scope)
24262 pop_scope (pushed_scope);
24265 else
24267 cp_id_kind idk;
24268 /* If parsing a type specifier sequence failed, then
24269 this MUST be a simple expression. */
24270 cp_parser_parse_tentatively (parser);
24271 decl = cp_parser_primary_expression (parser, false, false,
24272 false, &idk);
24273 if (!cp_parser_error_occurred (parser)
24274 && decl
24275 && DECL_P (decl)
24276 && CLASS_TYPE_P (TREE_TYPE (decl)))
24278 tree rhs;
24280 cp_parser_parse_definitely (parser);
24281 cp_parser_require (parser, CPP_EQ, RT_EQ);
24282 rhs = cp_parser_assignment_expression (parser, false, NULL);
24283 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24284 rhs,
24285 tf_warning_or_error));
24286 add_private_clause = true;
24288 else
24290 decl = NULL;
24291 cp_parser_abort_tentative_parse (parser);
24292 init = cp_parser_expression (parser, false, NULL);
24293 if (init)
24295 if (TREE_CODE (init) == MODIFY_EXPR
24296 || TREE_CODE (init) == MODOP_EXPR)
24297 real_decl = TREE_OPERAND (init, 0);
24302 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24303 if (this_pre_body)
24305 this_pre_body = pop_stmt_list (this_pre_body);
24306 if (pre_body)
24308 tree t = pre_body;
24309 pre_body = push_stmt_list ();
24310 add_stmt (t);
24311 add_stmt (this_pre_body);
24312 pre_body = pop_stmt_list (pre_body);
24314 else
24315 pre_body = this_pre_body;
24318 if (decl)
24319 real_decl = decl;
24320 if (par_clauses != NULL && real_decl != NULL_TREE)
24322 tree *c;
24323 for (c = par_clauses; *c ; )
24324 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24325 && OMP_CLAUSE_DECL (*c) == real_decl)
24327 error_at (loc, "iteration variable %qD"
24328 " should not be firstprivate", real_decl);
24329 *c = OMP_CLAUSE_CHAIN (*c);
24331 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24332 && OMP_CLAUSE_DECL (*c) == real_decl)
24334 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24335 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24336 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24337 OMP_CLAUSE_DECL (l) = real_decl;
24338 OMP_CLAUSE_CHAIN (l) = clauses;
24339 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24340 clauses = l;
24341 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24342 CP_OMP_CLAUSE_INFO (*c) = NULL;
24343 add_private_clause = false;
24345 else
24347 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24348 && OMP_CLAUSE_DECL (*c) == real_decl)
24349 add_private_clause = false;
24350 c = &OMP_CLAUSE_CHAIN (*c);
24354 if (add_private_clause)
24356 tree c;
24357 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24359 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24360 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24361 && OMP_CLAUSE_DECL (c) == decl)
24362 break;
24363 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24364 && OMP_CLAUSE_DECL (c) == decl)
24365 error_at (loc, "iteration variable %qD "
24366 "should not be firstprivate",
24367 decl);
24368 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24369 && OMP_CLAUSE_DECL (c) == decl)
24370 error_at (loc, "iteration variable %qD should not be reduction",
24371 decl);
24373 if (c == NULL)
24375 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24376 OMP_CLAUSE_DECL (c) = decl;
24377 c = finish_omp_clauses (c);
24378 if (c)
24380 OMP_CLAUSE_CHAIN (c) = clauses;
24381 clauses = c;
24386 cond = NULL;
24387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24388 cond = cp_parser_omp_for_cond (parser, decl);
24389 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24391 incr = NULL;
24392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24394 /* If decl is an iterator, preserve the operator on decl
24395 until finish_omp_for. */
24396 if (decl
24397 && (type_dependent_expression_p (decl)
24398 || CLASS_TYPE_P (TREE_TYPE (decl))))
24399 incr = cp_parser_omp_for_incr (parser, decl);
24400 else
24401 incr = cp_parser_expression (parser, false, NULL);
24404 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24405 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24406 /*or_comma=*/false,
24407 /*consume_paren=*/true);
24409 TREE_VEC_ELT (declv, i) = decl;
24410 TREE_VEC_ELT (initv, i) = init;
24411 TREE_VEC_ELT (condv, i) = cond;
24412 TREE_VEC_ELT (incrv, i) = incr;
24414 if (i == collapse - 1)
24415 break;
24417 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24418 in between the collapsed for loops to be still considered perfectly
24419 nested. Hopefully the final version clarifies this.
24420 For now handle (multiple) {'s and empty statements. */
24421 cp_parser_parse_tentatively (parser);
24424 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24425 break;
24426 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24428 cp_lexer_consume_token (parser->lexer);
24429 bracecount++;
24431 else if (bracecount
24432 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24433 cp_lexer_consume_token (parser->lexer);
24434 else
24436 loc = cp_lexer_peek_token (parser->lexer)->location;
24437 error_at (loc, "not enough collapsed for loops");
24438 collapse_err = true;
24439 cp_parser_abort_tentative_parse (parser);
24440 declv = NULL_TREE;
24441 break;
24444 while (1);
24446 if (declv)
24448 cp_parser_parse_definitely (parser);
24449 nbraces += bracecount;
24453 /* Note that we saved the original contents of this flag when we entered
24454 the structured block, and so we don't need to re-save it here. */
24455 parser->in_statement = IN_OMP_FOR;
24457 /* Note that the grammar doesn't call for a structured block here,
24458 though the loop as a whole is a structured block. */
24459 body = push_stmt_list ();
24460 cp_parser_statement (parser, NULL_TREE, false, NULL);
24461 body = pop_stmt_list (body);
24463 if (declv == NULL_TREE)
24464 ret = NULL_TREE;
24465 else
24466 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24467 pre_body, clauses);
24469 while (nbraces)
24471 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24473 cp_lexer_consume_token (parser->lexer);
24474 nbraces--;
24476 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24477 cp_lexer_consume_token (parser->lexer);
24478 else
24480 if (!collapse_err)
24482 error_at (cp_lexer_peek_token (parser->lexer)->location,
24483 "collapsed loops not perfectly nested");
24485 collapse_err = true;
24486 cp_parser_statement_seq_opt (parser, NULL);
24487 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24488 break;
24492 while (!VEC_empty (tree, for_block))
24493 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24494 release_tree_vector (for_block);
24496 return ret;
24499 /* OpenMP 2.5:
24500 #pragma omp for for-clause[optseq] new-line
24501 for-loop */
24503 #define OMP_FOR_CLAUSE_MASK \
24504 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24505 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24506 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24507 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24508 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24509 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24510 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24511 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24513 static tree
24514 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24516 tree clauses, sb, ret;
24517 unsigned int save;
24519 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24520 "#pragma omp for", pragma_tok);
24522 sb = begin_omp_structured_block ();
24523 save = cp_parser_begin_omp_structured_block (parser);
24525 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24527 cp_parser_end_omp_structured_block (parser, save);
24528 add_stmt (finish_omp_structured_block (sb));
24530 return ret;
24533 /* OpenMP 2.5:
24534 # pragma omp master new-line
24535 structured-block */
24537 static tree
24538 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24540 cp_parser_require_pragma_eol (parser, pragma_tok);
24541 return c_finish_omp_master (input_location,
24542 cp_parser_omp_structured_block (parser));
24545 /* OpenMP 2.5:
24546 # pragma omp ordered new-line
24547 structured-block */
24549 static tree
24550 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24552 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24553 cp_parser_require_pragma_eol (parser, pragma_tok);
24554 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24557 /* OpenMP 2.5:
24559 section-scope:
24560 { section-sequence }
24562 section-sequence:
24563 section-directive[opt] structured-block
24564 section-sequence section-directive structured-block */
24566 static tree
24567 cp_parser_omp_sections_scope (cp_parser *parser)
24569 tree stmt, substmt;
24570 bool error_suppress = false;
24571 cp_token *tok;
24573 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24574 return NULL_TREE;
24576 stmt = push_stmt_list ();
24578 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24580 unsigned save;
24582 substmt = begin_omp_structured_block ();
24583 save = cp_parser_begin_omp_structured_block (parser);
24585 while (1)
24587 cp_parser_statement (parser, NULL_TREE, false, NULL);
24589 tok = cp_lexer_peek_token (parser->lexer);
24590 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24591 break;
24592 if (tok->type == CPP_CLOSE_BRACE)
24593 break;
24594 if (tok->type == CPP_EOF)
24595 break;
24598 cp_parser_end_omp_structured_block (parser, save);
24599 substmt = finish_omp_structured_block (substmt);
24600 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24601 add_stmt (substmt);
24604 while (1)
24606 tok = cp_lexer_peek_token (parser->lexer);
24607 if (tok->type == CPP_CLOSE_BRACE)
24608 break;
24609 if (tok->type == CPP_EOF)
24610 break;
24612 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24614 cp_lexer_consume_token (parser->lexer);
24615 cp_parser_require_pragma_eol (parser, tok);
24616 error_suppress = false;
24618 else if (!error_suppress)
24620 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24621 error_suppress = true;
24624 substmt = cp_parser_omp_structured_block (parser);
24625 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24626 add_stmt (substmt);
24628 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24630 substmt = pop_stmt_list (stmt);
24632 stmt = make_node (OMP_SECTIONS);
24633 TREE_TYPE (stmt) = void_type_node;
24634 OMP_SECTIONS_BODY (stmt) = substmt;
24636 add_stmt (stmt);
24637 return stmt;
24640 /* OpenMP 2.5:
24641 # pragma omp sections sections-clause[optseq] newline
24642 sections-scope */
24644 #define OMP_SECTIONS_CLAUSE_MASK \
24645 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24646 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24647 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24648 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24649 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24651 static tree
24652 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24654 tree clauses, ret;
24656 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24657 "#pragma omp sections", pragma_tok);
24659 ret = cp_parser_omp_sections_scope (parser);
24660 if (ret)
24661 OMP_SECTIONS_CLAUSES (ret) = clauses;
24663 return ret;
24666 /* OpenMP 2.5:
24667 # pragma parallel parallel-clause new-line
24668 # pragma parallel for parallel-for-clause new-line
24669 # pragma parallel sections parallel-sections-clause new-line */
24671 #define OMP_PARALLEL_CLAUSE_MASK \
24672 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24673 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24674 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24675 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24676 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24677 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24678 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24679 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24681 static tree
24682 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24684 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24685 const char *p_name = "#pragma omp parallel";
24686 tree stmt, clauses, par_clause, ws_clause, block;
24687 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24688 unsigned int save;
24689 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24691 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24693 cp_lexer_consume_token (parser->lexer);
24694 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24695 p_name = "#pragma omp parallel for";
24696 mask |= OMP_FOR_CLAUSE_MASK;
24697 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24699 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24701 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24702 const char *p = IDENTIFIER_POINTER (id);
24703 if (strcmp (p, "sections") == 0)
24705 cp_lexer_consume_token (parser->lexer);
24706 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24707 p_name = "#pragma omp parallel sections";
24708 mask |= OMP_SECTIONS_CLAUSE_MASK;
24709 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24713 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24714 block = begin_omp_parallel ();
24715 save = cp_parser_begin_omp_structured_block (parser);
24717 switch (p_kind)
24719 case PRAGMA_OMP_PARALLEL:
24720 cp_parser_statement (parser, NULL_TREE, false, NULL);
24721 par_clause = clauses;
24722 break;
24724 case PRAGMA_OMP_PARALLEL_FOR:
24725 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24726 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24727 break;
24729 case PRAGMA_OMP_PARALLEL_SECTIONS:
24730 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24731 stmt = cp_parser_omp_sections_scope (parser);
24732 if (stmt)
24733 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24734 break;
24736 default:
24737 gcc_unreachable ();
24740 cp_parser_end_omp_structured_block (parser, save);
24741 stmt = finish_omp_parallel (par_clause, block);
24742 if (p_kind != PRAGMA_OMP_PARALLEL)
24743 OMP_PARALLEL_COMBINED (stmt) = 1;
24744 return stmt;
24747 /* OpenMP 2.5:
24748 # pragma omp single single-clause[optseq] new-line
24749 structured-block */
24751 #define OMP_SINGLE_CLAUSE_MASK \
24752 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24753 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24754 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24755 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24757 static tree
24758 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24760 tree stmt = make_node (OMP_SINGLE);
24761 TREE_TYPE (stmt) = void_type_node;
24763 OMP_SINGLE_CLAUSES (stmt)
24764 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24765 "#pragma omp single", pragma_tok);
24766 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24768 return add_stmt (stmt);
24771 /* OpenMP 3.0:
24772 # pragma omp task task-clause[optseq] new-line
24773 structured-block */
24775 #define OMP_TASK_CLAUSE_MASK \
24776 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24777 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24778 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24779 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24780 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24781 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24783 static tree
24784 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24786 tree clauses, block;
24787 unsigned int save;
24789 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24790 "#pragma omp task", pragma_tok);
24791 block = begin_omp_task ();
24792 save = cp_parser_begin_omp_structured_block (parser);
24793 cp_parser_statement (parser, NULL_TREE, false, NULL);
24794 cp_parser_end_omp_structured_block (parser, save);
24795 return finish_omp_task (clauses, block);
24798 /* OpenMP 3.0:
24799 # pragma omp taskwait new-line */
24801 static void
24802 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24804 cp_parser_require_pragma_eol (parser, pragma_tok);
24805 finish_omp_taskwait ();
24808 /* OpenMP 2.5:
24809 # pragma omp threadprivate (variable-list) */
24811 static void
24812 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24814 tree vars;
24816 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24817 cp_parser_require_pragma_eol (parser, pragma_tok);
24819 finish_omp_threadprivate (vars);
24822 /* Main entry point to OpenMP statement pragmas. */
24824 static void
24825 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24827 tree stmt;
24829 switch (pragma_tok->pragma_kind)
24831 case PRAGMA_OMP_ATOMIC:
24832 cp_parser_omp_atomic (parser, pragma_tok);
24833 return;
24834 case PRAGMA_OMP_CRITICAL:
24835 stmt = cp_parser_omp_critical (parser, pragma_tok);
24836 break;
24837 case PRAGMA_OMP_FOR:
24838 stmt = cp_parser_omp_for (parser, pragma_tok);
24839 break;
24840 case PRAGMA_OMP_MASTER:
24841 stmt = cp_parser_omp_master (parser, pragma_tok);
24842 break;
24843 case PRAGMA_OMP_ORDERED:
24844 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24845 break;
24846 case PRAGMA_OMP_PARALLEL:
24847 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24848 break;
24849 case PRAGMA_OMP_SECTIONS:
24850 stmt = cp_parser_omp_sections (parser, pragma_tok);
24851 break;
24852 case PRAGMA_OMP_SINGLE:
24853 stmt = cp_parser_omp_single (parser, pragma_tok);
24854 break;
24855 case PRAGMA_OMP_TASK:
24856 stmt = cp_parser_omp_task (parser, pragma_tok);
24857 break;
24858 default:
24859 gcc_unreachable ();
24862 if (stmt)
24863 SET_EXPR_LOCATION (stmt, pragma_tok->location);
24866 /* The parser. */
24868 static GTY (()) cp_parser *the_parser;
24871 /* Special handling for the first token or line in the file. The first
24872 thing in the file might be #pragma GCC pch_preprocess, which loads a
24873 PCH file, which is a GC collection point. So we need to handle this
24874 first pragma without benefit of an existing lexer structure.
24876 Always returns one token to the caller in *FIRST_TOKEN. This is
24877 either the true first token of the file, or the first token after
24878 the initial pragma. */
24880 static void
24881 cp_parser_initial_pragma (cp_token *first_token)
24883 tree name = NULL;
24885 cp_lexer_get_preprocessor_token (NULL, first_token);
24886 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24887 return;
24889 cp_lexer_get_preprocessor_token (NULL, first_token);
24890 if (first_token->type == CPP_STRING)
24892 name = first_token->u.value;
24894 cp_lexer_get_preprocessor_token (NULL, first_token);
24895 if (first_token->type != CPP_PRAGMA_EOL)
24896 error_at (first_token->location,
24897 "junk at end of %<#pragma GCC pch_preprocess%>");
24899 else
24900 error_at (first_token->location, "expected string literal");
24902 /* Skip to the end of the pragma. */
24903 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24904 cp_lexer_get_preprocessor_token (NULL, first_token);
24906 /* Now actually load the PCH file. */
24907 if (name)
24908 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
24910 /* Read one more token to return to our caller. We have to do this
24911 after reading the PCH file in, since its pointers have to be
24912 live. */
24913 cp_lexer_get_preprocessor_token (NULL, first_token);
24916 /* Normal parsing of a pragma token. Here we can (and must) use the
24917 regular lexer. */
24919 static bool
24920 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24922 cp_token *pragma_tok;
24923 unsigned int id;
24925 pragma_tok = cp_lexer_consume_token (parser->lexer);
24926 gcc_assert (pragma_tok->type == CPP_PRAGMA);
24927 parser->lexer->in_pragma = true;
24929 id = pragma_tok->pragma_kind;
24930 switch (id)
24932 case PRAGMA_GCC_PCH_PREPROCESS:
24933 error_at (pragma_tok->location,
24934 "%<#pragma GCC pch_preprocess%> must be first");
24935 break;
24937 case PRAGMA_OMP_BARRIER:
24938 switch (context)
24940 case pragma_compound:
24941 cp_parser_omp_barrier (parser, pragma_tok);
24942 return false;
24943 case pragma_stmt:
24944 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
24945 "used in compound statements");
24946 break;
24947 default:
24948 goto bad_stmt;
24950 break;
24952 case PRAGMA_OMP_FLUSH:
24953 switch (context)
24955 case pragma_compound:
24956 cp_parser_omp_flush (parser, pragma_tok);
24957 return false;
24958 case pragma_stmt:
24959 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
24960 "used in compound statements");
24961 break;
24962 default:
24963 goto bad_stmt;
24965 break;
24967 case PRAGMA_OMP_TASKWAIT:
24968 switch (context)
24970 case pragma_compound:
24971 cp_parser_omp_taskwait (parser, pragma_tok);
24972 return false;
24973 case pragma_stmt:
24974 error_at (pragma_tok->location,
24975 "%<#pragma omp taskwait%> may only be "
24976 "used in compound statements");
24977 break;
24978 default:
24979 goto bad_stmt;
24981 break;
24983 case PRAGMA_OMP_THREADPRIVATE:
24984 cp_parser_omp_threadprivate (parser, pragma_tok);
24985 return false;
24987 case PRAGMA_OMP_ATOMIC:
24988 case PRAGMA_OMP_CRITICAL:
24989 case PRAGMA_OMP_FOR:
24990 case PRAGMA_OMP_MASTER:
24991 case PRAGMA_OMP_ORDERED:
24992 case PRAGMA_OMP_PARALLEL:
24993 case PRAGMA_OMP_SECTIONS:
24994 case PRAGMA_OMP_SINGLE:
24995 case PRAGMA_OMP_TASK:
24996 if (context == pragma_external)
24997 goto bad_stmt;
24998 cp_parser_omp_construct (parser, pragma_tok);
24999 return true;
25001 case PRAGMA_OMP_SECTION:
25002 error_at (pragma_tok->location,
25003 "%<#pragma omp section%> may only be used in "
25004 "%<#pragma omp sections%> construct");
25005 break;
25007 default:
25008 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25009 c_invoke_pragma_handler (id);
25010 break;
25012 bad_stmt:
25013 cp_parser_error (parser, "expected declaration specifiers");
25014 break;
25017 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25018 return false;
25021 /* The interface the pragma parsers have to the lexer. */
25023 enum cpp_ttype
25024 pragma_lex (tree *value)
25026 cp_token *tok;
25027 enum cpp_ttype ret;
25029 tok = cp_lexer_peek_token (the_parser->lexer);
25031 ret = tok->type;
25032 *value = tok->u.value;
25034 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25035 ret = CPP_EOF;
25036 else if (ret == CPP_STRING)
25037 *value = cp_parser_string_literal (the_parser, false, false);
25038 else
25040 cp_lexer_consume_token (the_parser->lexer);
25041 if (ret == CPP_KEYWORD)
25042 ret = CPP_NAME;
25045 return ret;
25049 /* External interface. */
25051 /* Parse one entire translation unit. */
25053 void
25054 c_parse_file (void)
25056 static bool already_called = false;
25058 if (already_called)
25060 sorry ("inter-module optimizations not implemented for C++");
25061 return;
25063 already_called = true;
25065 the_parser = cp_parser_new ();
25066 push_deferring_access_checks (flag_access_control
25067 ? dk_no_deferred : dk_no_check);
25068 cp_parser_translation_unit (the_parser);
25069 the_parser = NULL;
25072 #include "gt-cp-parser.h"