Cilk Plus #pragma simd iteration with Jason.
[official-gcc.git] / gcc / cp / parser.c
blob224fea184bde14758f6099101bc942831096755c
1 /* C++ Parser.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.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 "target.h"
35 #include "cgraph.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "plugin.h"
39 #include "tree-pretty-print.h"
40 #include "parser.h"
43 /* The lexer. */
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 static cp_token eof_token =
50 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 /* The various kinds of non integral constant we encounter. */
54 typedef enum non_integral_constant {
55 NIC_NONE,
56 /* floating-point literal */
57 NIC_FLOAT,
58 /* %<this%> */
59 NIC_THIS,
60 /* %<__FUNCTION__%> */
61 NIC_FUNC_NAME,
62 /* %<__PRETTY_FUNCTION__%> */
63 NIC_PRETTY_FUNC,
64 /* %<__func__%> */
65 NIC_C99_FUNC,
66 /* "%<va_arg%> */
67 NIC_VA_ARG,
68 /* a cast */
69 NIC_CAST,
70 /* %<typeid%> operator */
71 NIC_TYPEID,
72 /* non-constant compound literals */
73 NIC_NCC,
74 /* a function call */
75 NIC_FUNC_CALL,
76 /* an increment */
77 NIC_INC,
78 /* an decrement */
79 NIC_DEC,
80 /* an array reference */
81 NIC_ARRAY_REF,
82 /* %<->%> */
83 NIC_ARROW,
84 /* %<.%> */
85 NIC_POINT,
86 /* the address of a label */
87 NIC_ADDR_LABEL,
88 /* %<*%> */
89 NIC_STAR,
90 /* %<&%> */
91 NIC_ADDR,
92 /* %<++%> */
93 NIC_PREINCREMENT,
94 /* %<--%> */
95 NIC_PREDECREMENT,
96 /* %<new%> */
97 NIC_NEW,
98 /* %<delete%> */
99 NIC_DEL,
100 /* calls to overloaded operators */
101 NIC_OVERLOADED,
102 /* an assignment */
103 NIC_ASSIGNMENT,
104 /* a comma operator */
105 NIC_COMMA,
106 /* a call to a constructor */
107 NIC_CONSTRUCTOR,
108 /* a transaction expression */
109 NIC_TRANSACTION
110 } non_integral_constant;
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114 /* NULL */
115 NLE_NULL,
116 /* is not a type */
117 NLE_TYPE,
118 /* is not a class or namespace */
119 NLE_CXX98,
120 /* is not a class, namespace, or enumeration */
121 NLE_NOT_CXX98
122 } name_lookup_error;
124 /* The various kinds of required token */
125 typedef enum required_token {
126 RT_NONE,
127 RT_SEMICOLON, /* ';' */
128 RT_OPEN_PAREN, /* '(' */
129 RT_CLOSE_BRACE, /* '}' */
130 RT_OPEN_BRACE, /* '{' */
131 RT_CLOSE_SQUARE, /* ']' */
132 RT_OPEN_SQUARE, /* '[' */
133 RT_COMMA, /* ',' */
134 RT_SCOPE, /* '::' */
135 RT_LESS, /* '<' */
136 RT_GREATER, /* '>' */
137 RT_EQ, /* '=' */
138 RT_ELLIPSIS, /* '...' */
139 RT_MULT, /* '*' */
140 RT_COMPL, /* '~' */
141 RT_COLON, /* ':' */
142 RT_COLON_SCOPE, /* ':' or '::' */
143 RT_CLOSE_PAREN, /* ')' */
144 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145 RT_PRAGMA_EOL, /* end of line */
146 RT_NAME, /* identifier */
148 /* The type is CPP_KEYWORD */
149 RT_NEW, /* new */
150 RT_DELETE, /* delete */
151 RT_RETURN, /* return */
152 RT_WHILE, /* while */
153 RT_EXTERN, /* extern */
154 RT_STATIC_ASSERT, /* static_assert */
155 RT_DECLTYPE, /* decltype */
156 RT_OPERATOR, /* operator */
157 RT_CLASS, /* class */
158 RT_TEMPLATE, /* template */
159 RT_NAMESPACE, /* namespace */
160 RT_USING, /* using */
161 RT_ASM, /* asm */
162 RT_TRY, /* try */
163 RT_CATCH, /* catch */
164 RT_THROW, /* throw */
165 RT_LABEL, /* __label__ */
166 RT_AT_TRY, /* @try */
167 RT_AT_SYNCHRONIZED, /* @synchronized */
168 RT_AT_THROW, /* @throw */
170 RT_SELECT, /* selection-statement */
171 RT_INTERATION, /* iteration-statement */
172 RT_JUMP, /* jump-statement */
173 RT_CLASS_KEY, /* class-key */
174 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
175 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
176 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
177 RT_TRANSACTION_CANCEL /* __transaction_cancel */
178 } required_token;
180 /* Prototypes. */
182 static cp_lexer *cp_lexer_new_main
183 (void);
184 static cp_lexer *cp_lexer_new_from_tokens
185 (cp_token_cache *tokens);
186 static void cp_lexer_destroy
187 (cp_lexer *);
188 static int cp_lexer_saving_tokens
189 (const cp_lexer *);
190 static cp_token *cp_lexer_token_at
191 (cp_lexer *, cp_token_position);
192 static void cp_lexer_get_preprocessor_token
193 (cp_lexer *, cp_token *);
194 static inline cp_token *cp_lexer_peek_token
195 (cp_lexer *);
196 static cp_token *cp_lexer_peek_nth_token
197 (cp_lexer *, size_t);
198 static inline bool cp_lexer_next_token_is
199 (cp_lexer *, enum cpp_ttype);
200 static bool cp_lexer_next_token_is_not
201 (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_keyword
203 (cp_lexer *, enum rid);
204 static cp_token *cp_lexer_consume_token
205 (cp_lexer *);
206 static void cp_lexer_purge_token
207 (cp_lexer *);
208 static void cp_lexer_purge_tokens_after
209 (cp_lexer *, cp_token_position);
210 static void cp_lexer_save_tokens
211 (cp_lexer *);
212 static void cp_lexer_commit_tokens
213 (cp_lexer *);
214 static void cp_lexer_rollback_tokens
215 (cp_lexer *);
216 static void cp_lexer_print_token
217 (FILE *, cp_token *);
218 static inline bool cp_lexer_debugging_p
219 (cp_lexer *);
220 static void cp_lexer_start_debugging
221 (cp_lexer *) ATTRIBUTE_UNUSED;
222 static void cp_lexer_stop_debugging
223 (cp_lexer *) ATTRIBUTE_UNUSED;
225 static cp_token_cache *cp_token_cache_new
226 (cp_token *, cp_token *);
228 static void cp_parser_initial_pragma
229 (cp_token *);
231 static tree cp_literal_operator_id
232 (const char *);
234 static void cp_parser_cilk_simd_construct
235 (cp_parser *, cp_token *);
236 static tree cp_parser_cilk_for
237 (cp_parser *, enum rid, tree);
239 /* Manifest constants. */
240 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
241 #define CP_SAVED_TOKEN_STACK 5
243 /* Variables. */
245 /* The stream to which debugging output should be written. */
246 static FILE *cp_lexer_debug_stream;
248 /* Nonzero if we are parsing an unevaluated operand: an operand to
249 sizeof, typeof, or alignof. */
250 int cp_unevaluated_operand;
252 /* Dump up to NUM tokens in BUFFER to FILE starting with token
253 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
254 first token in BUFFER. If NUM is 0, dump all the tokens. If
255 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
256 highlighted by surrounding it in [[ ]]. */
258 static void
259 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
260 cp_token *start_token, unsigned num,
261 cp_token *curr_token)
263 unsigned i, nprinted;
264 cp_token *token;
265 bool do_print;
267 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
269 if (buffer == NULL)
270 return;
272 if (num == 0)
273 num = buffer->length ();
275 if (start_token == NULL)
276 start_token = buffer->address ();
278 if (start_token > buffer->address ())
280 cp_lexer_print_token (file, &(*buffer)[0]);
281 fprintf (file, " ... ");
284 do_print = false;
285 nprinted = 0;
286 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
288 if (token == start_token)
289 do_print = true;
291 if (!do_print)
292 continue;
294 nprinted++;
295 if (token == curr_token)
296 fprintf (file, "[[");
298 cp_lexer_print_token (file, token);
300 if (token == curr_token)
301 fprintf (file, "]]");
303 switch (token->type)
305 case CPP_SEMICOLON:
306 case CPP_OPEN_BRACE:
307 case CPP_CLOSE_BRACE:
308 case CPP_EOF:
309 fputc ('\n', file);
310 break;
312 default:
313 fputc (' ', file);
317 if (i == num && i < buffer->length ())
319 fprintf (file, " ... ");
320 cp_lexer_print_token (file, &buffer->last ());
323 fprintf (file, "\n");
327 /* Dump all tokens in BUFFER to stderr. */
329 void
330 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
332 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
335 DEBUG_FUNCTION void
336 debug (vec<cp_token, va_gc> &ref)
338 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
341 DEBUG_FUNCTION void
342 debug (vec<cp_token, va_gc> *ptr)
344 if (ptr)
345 debug (*ptr);
346 else
347 fprintf (stderr, "<nil>\n");
351 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
352 description for T. */
354 static void
355 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
357 if (t)
359 fprintf (file, "%s: ", desc);
360 print_node_brief (file, "", t, 0);
365 /* Dump parser context C to FILE. */
367 static void
368 cp_debug_print_context (FILE *file, cp_parser_context *c)
370 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
371 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
372 print_node_brief (file, "", c->object_type, 0);
373 fprintf (file, "}\n");
377 /* Print the stack of parsing contexts to FILE starting with FIRST. */
379 static void
380 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
382 unsigned i;
383 cp_parser_context *c;
385 fprintf (file, "Parsing context stack:\n");
386 for (i = 0, c = first; c; c = c->next, i++)
388 fprintf (file, "\t#%u: ", i);
389 cp_debug_print_context (file, c);
394 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
396 static void
397 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
399 if (flag)
400 fprintf (file, "%s: true\n", desc);
404 /* Print an unparsed function entry UF to FILE. */
406 static void
407 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
409 unsigned i;
410 cp_default_arg_entry *default_arg_fn;
411 tree fn;
413 fprintf (file, "\tFunctions with default args:\n");
414 for (i = 0;
415 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
416 i++)
418 fprintf (file, "\t\tClass type: ");
419 print_node_brief (file, "", default_arg_fn->class_type, 0);
420 fprintf (file, "\t\tDeclaration: ");
421 print_node_brief (file, "", default_arg_fn->decl, 0);
422 fprintf (file, "\n");
425 fprintf (file, "\n\tFunctions with definitions that require "
426 "post-processing\n\t\t");
427 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
429 print_node_brief (file, "", fn, 0);
430 fprintf (file, " ");
432 fprintf (file, "\n");
434 fprintf (file, "\n\tNon-static data members with initializers that require "
435 "post-processing\n\t\t");
436 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
438 print_node_brief (file, "", fn, 0);
439 fprintf (file, " ");
441 fprintf (file, "\n");
445 /* Print the stack of unparsed member functions S to FILE. */
447 static void
448 cp_debug_print_unparsed_queues (FILE *file,
449 vec<cp_unparsed_functions_entry, va_gc> *s)
451 unsigned i;
452 cp_unparsed_functions_entry *uf;
454 fprintf (file, "Unparsed functions\n");
455 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
457 fprintf (file, "#%u:\n", i);
458 cp_debug_print_unparsed_function (file, uf);
463 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
464 the given PARSER. If FILE is NULL, the output is printed on stderr. */
466 static void
467 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
469 cp_token *next_token, *first_token, *start_token;
471 if (file == NULL)
472 file = stderr;
474 next_token = parser->lexer->next_token;
475 first_token = parser->lexer->buffer->address ();
476 start_token = (next_token > first_token + window_size / 2)
477 ? next_token - window_size / 2
478 : first_token;
479 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
480 next_token);
484 /* Dump debugging information for the given PARSER. If FILE is NULL,
485 the output is printed on stderr. */
487 void
488 cp_debug_parser (FILE *file, cp_parser *parser)
490 const size_t window_size = 20;
491 cp_token *token;
492 expanded_location eloc;
494 if (file == NULL)
495 file = stderr;
497 fprintf (file, "Parser state\n\n");
498 fprintf (file, "Number of tokens: %u\n",
499 vec_safe_length (parser->lexer->buffer));
500 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
501 cp_debug_print_tree_if_set (file, "Object scope",
502 parser->object_scope);
503 cp_debug_print_tree_if_set (file, "Qualifying scope",
504 parser->qualifying_scope);
505 cp_debug_print_context_stack (file, parser->context);
506 cp_debug_print_flag (file, "Allow GNU extensions",
507 parser->allow_gnu_extensions_p);
508 cp_debug_print_flag (file, "'>' token is greater-than",
509 parser->greater_than_is_operator_p);
510 cp_debug_print_flag (file, "Default args allowed in current "
511 "parameter list", parser->default_arg_ok_p);
512 cp_debug_print_flag (file, "Parsing integral constant-expression",
513 parser->integral_constant_expression_p);
514 cp_debug_print_flag (file, "Allow non-constant expression in current "
515 "constant-expression",
516 parser->allow_non_integral_constant_expression_p);
517 cp_debug_print_flag (file, "Seen non-constant expression",
518 parser->non_integral_constant_expression_p);
519 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
520 "current context",
521 parser->local_variables_forbidden_p);
522 cp_debug_print_flag (file, "In unbraced linkage specification",
523 parser->in_unbraced_linkage_specification_p);
524 cp_debug_print_flag (file, "Parsing a declarator",
525 parser->in_declarator_p);
526 cp_debug_print_flag (file, "In template argument list",
527 parser->in_template_argument_list_p);
528 cp_debug_print_flag (file, "Parsing an iteration statement",
529 parser->in_statement & IN_ITERATION_STMT);
530 cp_debug_print_flag (file, "Parsing a switch statement",
531 parser->in_statement & IN_SWITCH_STMT);
532 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
533 parser->in_statement & IN_OMP_BLOCK);
534 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
535 parser->in_statement & IN_OMP_FOR);
536 cp_debug_print_flag (file, "Parsing an if statement",
537 parser->in_statement & IN_IF_STMT);
538 cp_debug_print_flag (file, "Parsing a type-id in an expression "
539 "context", parser->in_type_id_in_expr_p);
540 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
541 parser->implicit_extern_c);
542 cp_debug_print_flag (file, "String expressions should be translated "
543 "to execution character set",
544 parser->translate_strings_p);
545 cp_debug_print_flag (file, "Parsing function body outside of a "
546 "local class", parser->in_function_body);
547 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
548 parser->colon_corrects_to_scope_p);
549 if (parser->type_definition_forbidden_message)
550 fprintf (file, "Error message for forbidden type definitions: %s\n",
551 parser->type_definition_forbidden_message);
552 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
553 fprintf (file, "Number of class definitions in progress: %u\n",
554 parser->num_classes_being_defined);
555 fprintf (file, "Number of template parameter lists for the current "
556 "declaration: %u\n", parser->num_template_parameter_lists);
557 cp_debug_parser_tokens (file, parser, window_size);
558 token = parser->lexer->next_token;
559 fprintf (file, "Next token to parse:\n");
560 fprintf (file, "\tToken: ");
561 cp_lexer_print_token (file, token);
562 eloc = expand_location (token->location);
563 fprintf (file, "\n\tFile: %s\n", eloc.file);
564 fprintf (file, "\tLine: %d\n", eloc.line);
565 fprintf (file, "\tColumn: %d\n", eloc.column);
568 DEBUG_FUNCTION void
569 debug (cp_parser &ref)
571 cp_debug_parser (stderr, &ref);
574 DEBUG_FUNCTION void
575 debug (cp_parser *ptr)
577 if (ptr)
578 debug (*ptr);
579 else
580 fprintf (stderr, "<nil>\n");
583 /* Allocate memory for a new lexer object and return it. */
585 static cp_lexer *
586 cp_lexer_alloc (void)
588 cp_lexer *lexer;
590 c_common_no_more_pch ();
592 /* Allocate the memory. */
593 lexer = ggc_alloc_cleared_cp_lexer ();
595 /* Initially we are not debugging. */
596 lexer->debugging_p = false;
598 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
600 /* Create the buffer. */
601 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
603 return lexer;
607 /* Create a new main C++ lexer, the lexer that gets tokens from the
608 preprocessor. */
610 static cp_lexer *
611 cp_lexer_new_main (void)
613 cp_lexer *lexer;
614 cp_token token;
616 /* It's possible that parsing the first pragma will load a PCH file,
617 which is a GC collection point. So we have to do that before
618 allocating any memory. */
619 cp_parser_initial_pragma (&token);
621 lexer = cp_lexer_alloc ();
623 /* Put the first token in the buffer. */
624 lexer->buffer->quick_push (token);
626 /* Get the remaining tokens from the preprocessor. */
627 while (token.type != CPP_EOF)
629 cp_lexer_get_preprocessor_token (lexer, &token);
630 vec_safe_push (lexer->buffer, token);
633 lexer->last_token = lexer->buffer->address ()
634 + lexer->buffer->length ()
635 - 1;
636 lexer->next_token = lexer->buffer->length ()
637 ? lexer->buffer->address ()
638 : &eof_token;
640 /* Subsequent preprocessor diagnostics should use compiler
641 diagnostic functions to get the compiler source location. */
642 done_lexing = true;
644 gcc_assert (!lexer->next_token->purged_p);
645 return lexer;
648 /* Create a new lexer whose token stream is primed with the tokens in
649 CACHE. When these tokens are exhausted, no new tokens will be read. */
651 static cp_lexer *
652 cp_lexer_new_from_tokens (cp_token_cache *cache)
654 cp_token *first = cache->first;
655 cp_token *last = cache->last;
656 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
658 /* We do not own the buffer. */
659 lexer->buffer = NULL;
660 lexer->next_token = first == last ? &eof_token : first;
661 lexer->last_token = last;
663 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
665 /* Initially we are not debugging. */
666 lexer->debugging_p = false;
668 gcc_assert (!lexer->next_token->purged_p);
669 return lexer;
672 /* Frees all resources associated with LEXER. */
674 static void
675 cp_lexer_destroy (cp_lexer *lexer)
677 vec_free (lexer->buffer);
678 lexer->saved_tokens.release ();
679 ggc_free (lexer);
682 /* Returns nonzero if debugging information should be output. */
684 static inline bool
685 cp_lexer_debugging_p (cp_lexer *lexer)
687 return lexer->debugging_p;
691 static inline cp_token_position
692 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
694 gcc_assert (!previous_p || lexer->next_token != &eof_token);
696 return lexer->next_token - previous_p;
699 static inline cp_token *
700 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
702 return pos;
705 static inline void
706 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
708 lexer->next_token = cp_lexer_token_at (lexer, pos);
711 static inline cp_token_position
712 cp_lexer_previous_token_position (cp_lexer *lexer)
714 if (lexer->next_token == &eof_token)
715 return lexer->last_token - 1;
716 else
717 return cp_lexer_token_position (lexer, true);
720 static inline cp_token *
721 cp_lexer_previous_token (cp_lexer *lexer)
723 cp_token_position tp = cp_lexer_previous_token_position (lexer);
725 return cp_lexer_token_at (lexer, tp);
728 /* nonzero if we are presently saving tokens. */
730 static inline int
731 cp_lexer_saving_tokens (const cp_lexer* lexer)
733 return lexer->saved_tokens.length () != 0;
736 /* Store the next token from the preprocessor in *TOKEN. Return true
737 if we reach EOF. If LEXER is NULL, assume we are handling an
738 initial #pragma pch_preprocess, and thus want the lexer to return
739 processed strings. */
741 static void
742 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
744 static int is_extern_c = 0;
746 /* Get a new token from the preprocessor. */
747 token->type
748 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
749 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
750 token->keyword = RID_MAX;
751 token->pragma_kind = PRAGMA_NONE;
752 token->purged_p = false;
754 /* On some systems, some header files are surrounded by an
755 implicit extern "C" block. Set a flag in the token if it
756 comes from such a header. */
757 is_extern_c += pending_lang_change;
758 pending_lang_change = 0;
759 token->implicit_extern_c = is_extern_c > 0;
761 /* Check to see if this token is a keyword. */
762 if (token->type == CPP_NAME)
764 if (C_IS_RESERVED_WORD (token->u.value))
766 /* Mark this token as a keyword. */
767 token->type = CPP_KEYWORD;
768 /* Record which keyword. */
769 token->keyword = C_RID_CODE (token->u.value);
771 else
773 if (warn_cxx0x_compat
774 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
775 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
777 /* Warn about the C++0x keyword (but still treat it as
778 an identifier). */
779 warning (OPT_Wc__0x_compat,
780 "identifier %qE is a keyword in C++11",
781 token->u.value);
783 /* Clear out the C_RID_CODE so we don't warn about this
784 particular identifier-turned-keyword again. */
785 C_SET_RID_CODE (token->u.value, RID_MAX);
788 token->ambiguous_p = false;
789 token->keyword = RID_MAX;
792 else if (token->type == CPP_AT_NAME)
794 /* This only happens in Objective-C++; it must be a keyword. */
795 token->type = CPP_KEYWORD;
796 switch (C_RID_CODE (token->u.value))
798 /* Replace 'class' with '@class', 'private' with '@private',
799 etc. This prevents confusion with the C++ keyword
800 'class', and makes the tokens consistent with other
801 Objective-C 'AT' keywords. For example '@class' is
802 reported as RID_AT_CLASS which is consistent with
803 '@synchronized', which is reported as
804 RID_AT_SYNCHRONIZED.
806 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
807 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
808 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
809 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
810 case RID_THROW: token->keyword = RID_AT_THROW; break;
811 case RID_TRY: token->keyword = RID_AT_TRY; break;
812 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
813 default: token->keyword = C_RID_CODE (token->u.value);
816 else if (token->type == CPP_PRAGMA)
818 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
819 token->pragma_kind = ((enum pragma_kind)
820 TREE_INT_CST_LOW (token->u.value));
821 token->u.value = NULL_TREE;
825 /* Update the globals input_location and the input file stack from TOKEN. */
826 static inline void
827 cp_lexer_set_source_position_from_token (cp_token *token)
829 if (token->type != CPP_EOF)
831 input_location = token->location;
835 /* Return a pointer to the next token in the token stream, but do not
836 consume it. */
838 static inline cp_token *
839 cp_lexer_peek_token (cp_lexer *lexer)
841 if (cp_lexer_debugging_p (lexer))
843 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
844 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
845 putc ('\n', cp_lexer_debug_stream);
847 return lexer->next_token;
850 /* Return true if the next token has the indicated TYPE. */
852 static inline bool
853 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
855 return cp_lexer_peek_token (lexer)->type == type;
858 /* Return true if the next token does not have the indicated TYPE. */
860 static inline bool
861 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
863 return !cp_lexer_next_token_is (lexer, type);
866 /* Return true if the next token is the indicated KEYWORD. */
868 static inline bool
869 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
871 return cp_lexer_peek_token (lexer)->keyword == keyword;
874 static inline bool
875 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
877 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
880 /* Return true if the next token is not the indicated KEYWORD. */
882 static inline bool
883 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
885 return cp_lexer_peek_token (lexer)->keyword != keyword;
888 /* Return true if the next token is a keyword for a decl-specifier. */
890 static bool
891 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
893 cp_token *token;
895 token = cp_lexer_peek_token (lexer);
896 switch (token->keyword)
898 /* auto specifier: storage-class-specifier in C++,
899 simple-type-specifier in C++0x. */
900 case RID_AUTO:
901 /* Storage classes. */
902 case RID_REGISTER:
903 case RID_STATIC:
904 case RID_EXTERN:
905 case RID_MUTABLE:
906 case RID_THREAD:
907 /* Elaborated type specifiers. */
908 case RID_ENUM:
909 case RID_CLASS:
910 case RID_STRUCT:
911 case RID_UNION:
912 case RID_TYPENAME:
913 /* Simple type specifiers. */
914 case RID_CHAR:
915 case RID_CHAR16:
916 case RID_CHAR32:
917 case RID_WCHAR:
918 case RID_BOOL:
919 case RID_SHORT:
920 case RID_INT:
921 case RID_LONG:
922 case RID_INT128:
923 case RID_SIGNED:
924 case RID_UNSIGNED:
925 case RID_FLOAT:
926 case RID_DOUBLE:
927 case RID_VOID:
928 /* GNU extensions. */
929 case RID_ATTRIBUTE:
930 case RID_TYPEOF:
931 /* C++0x extensions. */
932 case RID_DECLTYPE:
933 case RID_UNDERLYING_TYPE:
934 return true;
936 default:
937 return false;
941 /* Returns TRUE iff the token T begins a decltype type. */
943 static bool
944 token_is_decltype (cp_token *t)
946 return (t->keyword == RID_DECLTYPE
947 || t->type == CPP_DECLTYPE);
950 /* Returns TRUE iff the next token begins a decltype type. */
952 static bool
953 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
955 cp_token *t = cp_lexer_peek_token (lexer);
956 return token_is_decltype (t);
959 /* Return a pointer to the Nth token in the token stream. If N is 1,
960 then this is precisely equivalent to cp_lexer_peek_token (except
961 that it is not inline). One would like to disallow that case, but
962 there is one case (cp_parser_nth_token_starts_template_id) where
963 the caller passes a variable for N and it might be 1. */
965 static cp_token *
966 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
968 cp_token *token;
970 /* N is 1-based, not zero-based. */
971 gcc_assert (n > 0);
973 if (cp_lexer_debugging_p (lexer))
974 fprintf (cp_lexer_debug_stream,
975 "cp_lexer: peeking ahead %ld at token: ", (long)n);
977 --n;
978 token = lexer->next_token;
979 gcc_assert (!n || token != &eof_token);
980 while (n != 0)
982 ++token;
983 if (token == lexer->last_token)
985 token = &eof_token;
986 break;
989 if (!token->purged_p)
990 --n;
993 if (cp_lexer_debugging_p (lexer))
995 cp_lexer_print_token (cp_lexer_debug_stream, token);
996 putc ('\n', cp_lexer_debug_stream);
999 return token;
1002 /* Return the next token, and advance the lexer's next_token pointer
1003 to point to the next non-purged token. */
1005 static cp_token *
1006 cp_lexer_consume_token (cp_lexer* lexer)
1008 cp_token *token = lexer->next_token;
1010 gcc_assert (token != &eof_token);
1011 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1015 lexer->next_token++;
1016 if (lexer->next_token == lexer->last_token)
1018 lexer->next_token = &eof_token;
1019 break;
1023 while (lexer->next_token->purged_p);
1025 cp_lexer_set_source_position_from_token (token);
1027 /* Provide debugging output. */
1028 if (cp_lexer_debugging_p (lexer))
1030 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1031 cp_lexer_print_token (cp_lexer_debug_stream, token);
1032 putc ('\n', cp_lexer_debug_stream);
1035 return token;
1038 /* Permanently remove the next token from the token stream, and
1039 advance the next_token pointer to refer to the next non-purged
1040 token. */
1042 static void
1043 cp_lexer_purge_token (cp_lexer *lexer)
1045 cp_token *tok = lexer->next_token;
1047 gcc_assert (tok != &eof_token);
1048 tok->purged_p = true;
1049 tok->location = UNKNOWN_LOCATION;
1050 tok->u.value = NULL_TREE;
1051 tok->keyword = RID_MAX;
1055 tok++;
1056 if (tok == lexer->last_token)
1058 tok = &eof_token;
1059 break;
1062 while (tok->purged_p);
1063 lexer->next_token = tok;
1066 /* Permanently remove all tokens after TOK, up to, but not
1067 including, the token that will be returned next by
1068 cp_lexer_peek_token. */
1070 static void
1071 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1073 cp_token *peek = lexer->next_token;
1075 if (peek == &eof_token)
1076 peek = lexer->last_token;
1078 gcc_assert (tok < peek);
1080 for ( tok += 1; tok != peek; tok += 1)
1082 tok->purged_p = true;
1083 tok->location = UNKNOWN_LOCATION;
1084 tok->u.value = NULL_TREE;
1085 tok->keyword = RID_MAX;
1089 /* Begin saving tokens. All tokens consumed after this point will be
1090 preserved. */
1092 static void
1093 cp_lexer_save_tokens (cp_lexer* lexer)
1095 /* Provide debugging output. */
1096 if (cp_lexer_debugging_p (lexer))
1097 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1099 lexer->saved_tokens.safe_push (lexer->next_token);
1102 /* Commit to the portion of the token stream most recently saved. */
1104 static void
1105 cp_lexer_commit_tokens (cp_lexer* lexer)
1107 /* Provide debugging output. */
1108 if (cp_lexer_debugging_p (lexer))
1109 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1111 lexer->saved_tokens.pop ();
1114 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1115 to the token stream. Stop saving tokens. */
1117 static void
1118 cp_lexer_rollback_tokens (cp_lexer* lexer)
1120 /* Provide debugging output. */
1121 if (cp_lexer_debugging_p (lexer))
1122 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1124 lexer->next_token = lexer->saved_tokens.pop ();
1127 /* Print a representation of the TOKEN on the STREAM. */
1129 static void
1130 cp_lexer_print_token (FILE * stream, cp_token *token)
1132 /* We don't use cpp_type2name here because the parser defines
1133 a few tokens of its own. */
1134 static const char *const token_names[] = {
1135 /* cpplib-defined token types */
1136 #define OP(e, s) #e,
1137 #define TK(e, s) #e,
1138 TTYPE_TABLE
1139 #undef OP
1140 #undef TK
1141 /* C++ parser token types - see "Manifest constants", above. */
1142 "KEYWORD",
1143 "TEMPLATE_ID",
1144 "NESTED_NAME_SPECIFIER",
1147 /* For some tokens, print the associated data. */
1148 switch (token->type)
1150 case CPP_KEYWORD:
1151 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1152 For example, `struct' is mapped to an INTEGER_CST. */
1153 if (!identifier_p (token->u.value))
1154 break;
1155 /* else fall through */
1156 case CPP_NAME:
1157 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1158 break;
1160 case CPP_STRING:
1161 case CPP_STRING16:
1162 case CPP_STRING32:
1163 case CPP_WSTRING:
1164 case CPP_UTF8STRING:
1165 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1166 break;
1168 case CPP_NUMBER:
1169 print_generic_expr (stream, token->u.value, 0);
1170 break;
1172 default:
1173 /* If we have a name for the token, print it out. Otherwise, we
1174 simply give the numeric code. */
1175 if (token->type < ARRAY_SIZE(token_names))
1176 fputs (token_names[token->type], stream);
1177 else
1178 fprintf (stream, "[%d]", token->type);
1179 break;
1183 DEBUG_FUNCTION void
1184 debug (cp_token &ref)
1186 cp_lexer_print_token (stderr, &ref);
1187 fprintf (stderr, "\n");
1190 DEBUG_FUNCTION void
1191 debug (cp_token *ptr)
1193 if (ptr)
1194 debug (*ptr);
1195 else
1196 fprintf (stderr, "<nil>\n");
1200 /* Start emitting debugging information. */
1202 static void
1203 cp_lexer_start_debugging (cp_lexer* lexer)
1205 lexer->debugging_p = true;
1206 cp_lexer_debug_stream = stderr;
1209 /* Stop emitting debugging information. */
1211 static void
1212 cp_lexer_stop_debugging (cp_lexer* lexer)
1214 lexer->debugging_p = false;
1215 cp_lexer_debug_stream = NULL;
1218 /* Create a new cp_token_cache, representing a range of tokens. */
1220 static cp_token_cache *
1221 cp_token_cache_new (cp_token *first, cp_token *last)
1223 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1224 cache->first = first;
1225 cache->last = last;
1226 return cache;
1230 /* Decl-specifiers. */
1232 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1234 static void
1235 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1237 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1240 /* Declarators. */
1242 /* Nothing other than the parser should be creating declarators;
1243 declarators are a semi-syntactic representation of C++ entities.
1244 Other parts of the front end that need to create entities (like
1245 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1247 static cp_declarator *make_call_declarator
1248 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1249 static cp_declarator *make_array_declarator
1250 (cp_declarator *, tree);
1251 static cp_declarator *make_pointer_declarator
1252 (cp_cv_quals, cp_declarator *, tree);
1253 static cp_declarator *make_reference_declarator
1254 (cp_cv_quals, cp_declarator *, bool, tree);
1255 static cp_parameter_declarator *make_parameter_declarator
1256 (cp_decl_specifier_seq *, cp_declarator *, tree);
1257 static cp_declarator *make_ptrmem_declarator
1258 (cp_cv_quals, tree, cp_declarator *, tree);
1260 /* An erroneous declarator. */
1261 static cp_declarator *cp_error_declarator;
1263 /* The obstack on which declarators and related data structures are
1264 allocated. */
1265 static struct obstack declarator_obstack;
1267 /* Alloc BYTES from the declarator memory pool. */
1269 static inline void *
1270 alloc_declarator (size_t bytes)
1272 return obstack_alloc (&declarator_obstack, bytes);
1275 /* Allocate a declarator of the indicated KIND. Clear fields that are
1276 common to all declarators. */
1278 static cp_declarator *
1279 make_declarator (cp_declarator_kind kind)
1281 cp_declarator *declarator;
1283 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1284 declarator->kind = kind;
1285 declarator->attributes = NULL_TREE;
1286 declarator->std_attributes = NULL_TREE;
1287 declarator->declarator = NULL;
1288 declarator->parameter_pack_p = false;
1289 declarator->id_loc = UNKNOWN_LOCATION;
1291 return declarator;
1294 /* Make a declarator for a generalized identifier. If
1295 QUALIFYING_SCOPE is non-NULL, the identifier is
1296 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1297 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1298 is, if any. */
1300 static cp_declarator *
1301 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1302 special_function_kind sfk)
1304 cp_declarator *declarator;
1306 /* It is valid to write:
1308 class C { void f(); };
1309 typedef C D;
1310 void D::f();
1312 The standard is not clear about whether `typedef const C D' is
1313 legal; as of 2002-09-15 the committee is considering that
1314 question. EDG 3.0 allows that syntax. Therefore, we do as
1315 well. */
1316 if (qualifying_scope && TYPE_P (qualifying_scope))
1317 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1319 gcc_assert (identifier_p (unqualified_name)
1320 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1321 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1323 declarator = make_declarator (cdk_id);
1324 declarator->u.id.qualifying_scope = qualifying_scope;
1325 declarator->u.id.unqualified_name = unqualified_name;
1326 declarator->u.id.sfk = sfk;
1328 return declarator;
1331 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1332 of modifiers such as const or volatile to apply to the pointer
1333 type, represented as identifiers. ATTRIBUTES represent the attributes that
1334 appertain to the pointer or reference. */
1336 cp_declarator *
1337 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1338 tree attributes)
1340 cp_declarator *declarator;
1342 declarator = make_declarator (cdk_pointer);
1343 declarator->declarator = target;
1344 declarator->u.pointer.qualifiers = cv_qualifiers;
1345 declarator->u.pointer.class_type = NULL_TREE;
1346 if (target)
1348 declarator->id_loc = target->id_loc;
1349 declarator->parameter_pack_p = target->parameter_pack_p;
1350 target->parameter_pack_p = false;
1352 else
1353 declarator->parameter_pack_p = false;
1355 declarator->std_attributes = attributes;
1357 return declarator;
1360 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1361 represent the attributes that appertain to the pointer or
1362 reference. */
1364 cp_declarator *
1365 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1366 bool rvalue_ref, tree attributes)
1368 cp_declarator *declarator;
1370 declarator = make_declarator (cdk_reference);
1371 declarator->declarator = target;
1372 declarator->u.reference.qualifiers = cv_qualifiers;
1373 declarator->u.reference.rvalue_ref = rvalue_ref;
1374 if (target)
1376 declarator->id_loc = target->id_loc;
1377 declarator->parameter_pack_p = target->parameter_pack_p;
1378 target->parameter_pack_p = false;
1380 else
1381 declarator->parameter_pack_p = false;
1383 declarator->std_attributes = attributes;
1385 return declarator;
1388 /* Like make_pointer_declarator -- but for a pointer to a non-static
1389 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1390 appertain to the pointer or reference. */
1392 cp_declarator *
1393 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1394 cp_declarator *pointee,
1395 tree attributes)
1397 cp_declarator *declarator;
1399 declarator = make_declarator (cdk_ptrmem);
1400 declarator->declarator = pointee;
1401 declarator->u.pointer.qualifiers = cv_qualifiers;
1402 declarator->u.pointer.class_type = class_type;
1404 if (pointee)
1406 declarator->parameter_pack_p = pointee->parameter_pack_p;
1407 pointee->parameter_pack_p = false;
1409 else
1410 declarator->parameter_pack_p = false;
1412 declarator->std_attributes = attributes;
1414 return declarator;
1417 /* Make a declarator for the function given by TARGET, with the
1418 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1419 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1420 indicates what exceptions can be thrown. */
1422 cp_declarator *
1423 make_call_declarator (cp_declarator *target,
1424 tree parms,
1425 cp_cv_quals cv_qualifiers,
1426 cp_virt_specifiers virt_specifiers,
1427 cp_ref_qualifier ref_qualifier,
1428 tree exception_specification,
1429 tree late_return_type)
1431 cp_declarator *declarator;
1433 declarator = make_declarator (cdk_function);
1434 declarator->declarator = target;
1435 declarator->u.function.parameters = parms;
1436 declarator->u.function.qualifiers = cv_qualifiers;
1437 declarator->u.function.virt_specifiers = virt_specifiers;
1438 declarator->u.function.ref_qualifier = ref_qualifier;
1439 declarator->u.function.exception_specification = exception_specification;
1440 declarator->u.function.late_return_type = late_return_type;
1441 if (target)
1443 declarator->id_loc = target->id_loc;
1444 declarator->parameter_pack_p = target->parameter_pack_p;
1445 target->parameter_pack_p = false;
1447 else
1448 declarator->parameter_pack_p = false;
1450 return declarator;
1453 /* Make a declarator for an array of BOUNDS elements, each of which is
1454 defined by ELEMENT. */
1456 cp_declarator *
1457 make_array_declarator (cp_declarator *element, tree bounds)
1459 cp_declarator *declarator;
1461 declarator = make_declarator (cdk_array);
1462 declarator->declarator = element;
1463 declarator->u.array.bounds = bounds;
1464 if (element)
1466 declarator->id_loc = element->id_loc;
1467 declarator->parameter_pack_p = element->parameter_pack_p;
1468 element->parameter_pack_p = false;
1470 else
1471 declarator->parameter_pack_p = false;
1473 return declarator;
1476 /* Determine whether the declarator we've seen so far can be a
1477 parameter pack, when followed by an ellipsis. */
1478 static bool
1479 declarator_can_be_parameter_pack (cp_declarator *declarator)
1481 /* Search for a declarator name, or any other declarator that goes
1482 after the point where the ellipsis could appear in a parameter
1483 pack. If we find any of these, then this declarator can not be
1484 made into a parameter pack. */
1485 bool found = false;
1486 while (declarator && !found)
1488 switch ((int)declarator->kind)
1490 case cdk_id:
1491 case cdk_array:
1492 found = true;
1493 break;
1495 case cdk_error:
1496 return true;
1498 default:
1499 declarator = declarator->declarator;
1500 break;
1504 return !found;
1507 cp_parameter_declarator *no_parameters;
1509 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1510 DECLARATOR and DEFAULT_ARGUMENT. */
1512 cp_parameter_declarator *
1513 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1514 cp_declarator *declarator,
1515 tree default_argument)
1517 cp_parameter_declarator *parameter;
1519 parameter = ((cp_parameter_declarator *)
1520 alloc_declarator (sizeof (cp_parameter_declarator)));
1521 parameter->next = NULL;
1522 if (decl_specifiers)
1523 parameter->decl_specifiers = *decl_specifiers;
1524 else
1525 clear_decl_specs (&parameter->decl_specifiers);
1526 parameter->declarator = declarator;
1527 parameter->default_argument = default_argument;
1528 parameter->ellipsis_p = false;
1530 return parameter;
1533 /* Returns true iff DECLARATOR is a declaration for a function. */
1535 static bool
1536 function_declarator_p (const cp_declarator *declarator)
1538 while (declarator)
1540 if (declarator->kind == cdk_function
1541 && declarator->declarator->kind == cdk_id)
1542 return true;
1543 if (declarator->kind == cdk_id
1544 || declarator->kind == cdk_error)
1545 return false;
1546 declarator = declarator->declarator;
1548 return false;
1551 /* The parser. */
1553 /* Overview
1554 --------
1556 A cp_parser parses the token stream as specified by the C++
1557 grammar. Its job is purely parsing, not semantic analysis. For
1558 example, the parser breaks the token stream into declarators,
1559 expressions, statements, and other similar syntactic constructs.
1560 It does not check that the types of the expressions on either side
1561 of an assignment-statement are compatible, or that a function is
1562 not declared with a parameter of type `void'.
1564 The parser invokes routines elsewhere in the compiler to perform
1565 semantic analysis and to build up the abstract syntax tree for the
1566 code processed.
1568 The parser (and the template instantiation code, which is, in a
1569 way, a close relative of parsing) are the only parts of the
1570 compiler that should be calling push_scope and pop_scope, or
1571 related functions. The parser (and template instantiation code)
1572 keeps track of what scope is presently active; everything else
1573 should simply honor that. (The code that generates static
1574 initializers may also need to set the scope, in order to check
1575 access control correctly when emitting the initializers.)
1577 Methodology
1578 -----------
1580 The parser is of the standard recursive-descent variety. Upcoming
1581 tokens in the token stream are examined in order to determine which
1582 production to use when parsing a non-terminal. Some C++ constructs
1583 require arbitrary look ahead to disambiguate. For example, it is
1584 impossible, in the general case, to tell whether a statement is an
1585 expression or declaration without scanning the entire statement.
1586 Therefore, the parser is capable of "parsing tentatively." When the
1587 parser is not sure what construct comes next, it enters this mode.
1588 Then, while we attempt to parse the construct, the parser queues up
1589 error messages, rather than issuing them immediately, and saves the
1590 tokens it consumes. If the construct is parsed successfully, the
1591 parser "commits", i.e., it issues any queued error messages and
1592 the tokens that were being preserved are permanently discarded.
1593 If, however, the construct is not parsed successfully, the parser
1594 rolls back its state completely so that it can resume parsing using
1595 a different alternative.
1597 Future Improvements
1598 -------------------
1600 The performance of the parser could probably be improved substantially.
1601 We could often eliminate the need to parse tentatively by looking ahead
1602 a little bit. In some places, this approach might not entirely eliminate
1603 the need to parse tentatively, but it might still speed up the average
1604 case. */
1606 /* Flags that are passed to some parsing functions. These values can
1607 be bitwise-ored together. */
1609 enum
1611 /* No flags. */
1612 CP_PARSER_FLAGS_NONE = 0x0,
1613 /* The construct is optional. If it is not present, then no error
1614 should be issued. */
1615 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1616 /* When parsing a type-specifier, treat user-defined type-names
1617 as non-type identifiers. */
1618 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1619 /* When parsing a type-specifier, do not try to parse a class-specifier
1620 or enum-specifier. */
1621 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1622 /* When parsing a decl-specifier-seq, only allow type-specifier or
1623 constexpr. */
1624 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1627 /* This type is used for parameters and variables which hold
1628 combinations of the above flags. */
1629 typedef int cp_parser_flags;
1631 /* The different kinds of declarators we want to parse. */
1633 typedef enum cp_parser_declarator_kind
1635 /* We want an abstract declarator. */
1636 CP_PARSER_DECLARATOR_ABSTRACT,
1637 /* We want a named declarator. */
1638 CP_PARSER_DECLARATOR_NAMED,
1639 /* We don't mind, but the name must be an unqualified-id. */
1640 CP_PARSER_DECLARATOR_EITHER
1641 } cp_parser_declarator_kind;
1643 /* The precedence values used to parse binary expressions. The minimum value
1644 of PREC must be 1, because zero is reserved to quickly discriminate
1645 binary operators from other tokens. */
1647 enum cp_parser_prec
1649 PREC_NOT_OPERATOR,
1650 PREC_LOGICAL_OR_EXPRESSION,
1651 PREC_LOGICAL_AND_EXPRESSION,
1652 PREC_INCLUSIVE_OR_EXPRESSION,
1653 PREC_EXCLUSIVE_OR_EXPRESSION,
1654 PREC_AND_EXPRESSION,
1655 PREC_EQUALITY_EXPRESSION,
1656 PREC_RELATIONAL_EXPRESSION,
1657 PREC_SHIFT_EXPRESSION,
1658 PREC_ADDITIVE_EXPRESSION,
1659 PREC_MULTIPLICATIVE_EXPRESSION,
1660 PREC_PM_EXPRESSION,
1661 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1664 /* A mapping from a token type to a corresponding tree node type, with a
1665 precedence value. */
1667 typedef struct cp_parser_binary_operations_map_node
1669 /* The token type. */
1670 enum cpp_ttype token_type;
1671 /* The corresponding tree code. */
1672 enum tree_code tree_type;
1673 /* The precedence of this operator. */
1674 enum cp_parser_prec prec;
1675 } cp_parser_binary_operations_map_node;
1677 typedef struct cp_parser_expression_stack_entry
1679 /* Left hand side of the binary operation we are currently
1680 parsing. */
1681 tree lhs;
1682 /* Original tree code for left hand side, if it was a binary
1683 expression itself (used for -Wparentheses). */
1684 enum tree_code lhs_type;
1685 /* Tree code for the binary operation we are parsing. */
1686 enum tree_code tree_type;
1687 /* Precedence of the binary operation we are parsing. */
1688 enum cp_parser_prec prec;
1689 /* Location of the binary operation we are parsing. */
1690 location_t loc;
1691 } cp_parser_expression_stack_entry;
1693 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1694 entries because precedence levels on the stack are monotonically
1695 increasing. */
1696 typedef struct cp_parser_expression_stack_entry
1697 cp_parser_expression_stack[NUM_PREC_VALUES];
1699 /* Prototypes. */
1701 /* Constructors and destructors. */
1703 static cp_parser_context *cp_parser_context_new
1704 (cp_parser_context *);
1706 /* Class variables. */
1708 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1710 /* The operator-precedence table used by cp_parser_binary_expression.
1711 Transformed into an associative array (binops_by_token) by
1712 cp_parser_new. */
1714 static const cp_parser_binary_operations_map_node binops[] = {
1715 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1716 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1718 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1719 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1720 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1722 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1723 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1725 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1726 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1728 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1729 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1730 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1731 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1733 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1734 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1736 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1738 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1740 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1742 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1744 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1747 /* The same as binops, but initialized by cp_parser_new so that
1748 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1749 for speed. */
1750 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1752 /* Constructors and destructors. */
1754 /* Construct a new context. The context below this one on the stack
1755 is given by NEXT. */
1757 static cp_parser_context *
1758 cp_parser_context_new (cp_parser_context* next)
1760 cp_parser_context *context;
1762 /* Allocate the storage. */
1763 if (cp_parser_context_free_list != NULL)
1765 /* Pull the first entry from the free list. */
1766 context = cp_parser_context_free_list;
1767 cp_parser_context_free_list = context->next;
1768 memset (context, 0, sizeof (*context));
1770 else
1771 context = ggc_alloc_cleared_cp_parser_context ();
1773 /* No errors have occurred yet in this context. */
1774 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1775 /* If this is not the bottommost context, copy information that we
1776 need from the previous context. */
1777 if (next)
1779 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1780 expression, then we are parsing one in this context, too. */
1781 context->object_type = next->object_type;
1782 /* Thread the stack. */
1783 context->next = next;
1786 return context;
1789 /* Managing the unparsed function queues. */
1791 #define unparsed_funs_with_default_args \
1792 parser->unparsed_queues->last ().funs_with_default_args
1793 #define unparsed_funs_with_definitions \
1794 parser->unparsed_queues->last ().funs_with_definitions
1795 #define unparsed_nsdmis \
1796 parser->unparsed_queues->last ().nsdmis
1798 static void
1799 push_unparsed_function_queues (cp_parser *parser)
1801 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1802 vec_safe_push (parser->unparsed_queues, e);
1805 static void
1806 pop_unparsed_function_queues (cp_parser *parser)
1808 release_tree_vector (unparsed_funs_with_definitions);
1809 parser->unparsed_queues->pop ();
1812 /* Prototypes. */
1814 /* Constructors and destructors. */
1816 static cp_parser *cp_parser_new
1817 (void);
1819 /* Routines to parse various constructs.
1821 Those that return `tree' will return the error_mark_node (rather
1822 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1823 Sometimes, they will return an ordinary node if error-recovery was
1824 attempted, even though a parse error occurred. So, to check
1825 whether or not a parse error occurred, you should always use
1826 cp_parser_error_occurred. If the construct is optional (indicated
1827 either by an `_opt' in the name of the function that does the
1828 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1829 the construct is not present. */
1831 /* Lexical conventions [gram.lex] */
1833 static tree cp_parser_identifier
1834 (cp_parser *);
1835 static tree cp_parser_string_literal
1836 (cp_parser *, bool, bool);
1837 static tree cp_parser_userdef_char_literal
1838 (cp_parser *);
1839 static tree cp_parser_userdef_string_literal
1840 (cp_token *);
1841 static tree cp_parser_userdef_numeric_literal
1842 (cp_parser *);
1844 /* Basic concepts [gram.basic] */
1846 static bool cp_parser_translation_unit
1847 (cp_parser *);
1849 /* Expressions [gram.expr] */
1851 static tree cp_parser_primary_expression
1852 (cp_parser *, bool, bool, bool, cp_id_kind *);
1853 static tree cp_parser_id_expression
1854 (cp_parser *, bool, bool, bool *, bool, bool);
1855 static tree cp_parser_unqualified_id
1856 (cp_parser *, bool, bool, bool, bool);
1857 static tree cp_parser_nested_name_specifier_opt
1858 (cp_parser *, bool, bool, bool, bool);
1859 static tree cp_parser_nested_name_specifier
1860 (cp_parser *, bool, bool, bool, bool);
1861 static tree cp_parser_qualifying_entity
1862 (cp_parser *, bool, bool, bool, bool, bool);
1863 static tree cp_parser_postfix_expression
1864 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1865 static tree cp_parser_postfix_open_square_expression
1866 (cp_parser *, tree, bool, bool);
1867 static tree cp_parser_postfix_dot_deref_expression
1868 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1869 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1870 (cp_parser *, int, bool, bool, bool *);
1871 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1872 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1873 static void cp_parser_pseudo_destructor_name
1874 (cp_parser *, tree, tree *, tree *);
1875 static tree cp_parser_unary_expression
1876 (cp_parser *, bool, bool, cp_id_kind *);
1877 static enum tree_code cp_parser_unary_operator
1878 (cp_token *);
1879 static tree cp_parser_new_expression
1880 (cp_parser *);
1881 static vec<tree, va_gc> *cp_parser_new_placement
1882 (cp_parser *);
1883 static tree cp_parser_new_type_id
1884 (cp_parser *, tree *);
1885 static cp_declarator *cp_parser_new_declarator_opt
1886 (cp_parser *);
1887 static cp_declarator *cp_parser_direct_new_declarator
1888 (cp_parser *);
1889 static vec<tree, va_gc> *cp_parser_new_initializer
1890 (cp_parser *);
1891 static tree cp_parser_delete_expression
1892 (cp_parser *);
1893 static tree cp_parser_cast_expression
1894 (cp_parser *, bool, bool, bool, cp_id_kind *);
1895 static tree cp_parser_binary_expression
1896 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1897 static tree cp_parser_question_colon_clause
1898 (cp_parser *, tree);
1899 static tree cp_parser_assignment_expression
1900 (cp_parser *, bool, cp_id_kind *);
1901 static enum tree_code cp_parser_assignment_operator_opt
1902 (cp_parser *);
1903 static tree cp_parser_expression
1904 (cp_parser *, bool, cp_id_kind *);
1905 static tree cp_parser_expression
1906 (cp_parser *, bool, bool, cp_id_kind *);
1907 static tree cp_parser_constant_expression
1908 (cp_parser *, bool, bool *);
1909 static tree cp_parser_builtin_offsetof
1910 (cp_parser *);
1911 static tree cp_parser_lambda_expression
1912 (cp_parser *);
1913 static void cp_parser_lambda_introducer
1914 (cp_parser *, tree);
1915 static bool cp_parser_lambda_declarator_opt
1916 (cp_parser *, tree);
1917 static void cp_parser_lambda_body
1918 (cp_parser *, tree);
1920 /* Statements [gram.stmt.stmt] */
1922 static void cp_parser_statement
1923 (cp_parser *, tree, bool, bool *);
1924 static void cp_parser_label_for_labeled_statement
1925 (cp_parser *, tree);
1926 static tree cp_parser_expression_statement
1927 (cp_parser *, tree);
1928 static tree cp_parser_compound_statement
1929 (cp_parser *, tree, bool, bool);
1930 static void cp_parser_statement_seq_opt
1931 (cp_parser *, tree);
1932 static tree cp_parser_selection_statement
1933 (cp_parser *, bool *);
1934 static tree cp_parser_condition
1935 (cp_parser *);
1936 static tree cp_parser_iteration_statement
1937 (cp_parser *);
1938 static bool cp_parser_for_init_statement
1939 (cp_parser *, tree *decl);
1940 static tree cp_parser_for
1941 (cp_parser *);
1942 static tree cp_parser_c_for
1943 (cp_parser *, tree, tree);
1944 static tree cp_parser_range_for
1945 (cp_parser *, tree, tree, tree);
1946 static void do_range_for_auto_deduction
1947 (tree, tree);
1948 static tree cp_parser_perform_range_for_lookup
1949 (tree, tree *, tree *);
1950 static tree cp_parser_range_for_member_function
1951 (tree, tree);
1952 static tree cp_parser_jump_statement
1953 (cp_parser *);
1954 static void cp_parser_declaration_statement
1955 (cp_parser *);
1957 static tree cp_parser_implicitly_scoped_statement
1958 (cp_parser *, bool *);
1959 static void cp_parser_already_scoped_statement
1960 (cp_parser *);
1962 /* Declarations [gram.dcl.dcl] */
1964 static void cp_parser_declaration_seq_opt
1965 (cp_parser *);
1966 static void cp_parser_declaration
1967 (cp_parser *);
1968 static void cp_parser_block_declaration
1969 (cp_parser *, bool);
1970 static void cp_parser_simple_declaration
1971 (cp_parser *, bool, tree *);
1972 static void cp_parser_decl_specifier_seq
1973 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1974 static tree cp_parser_storage_class_specifier_opt
1975 (cp_parser *);
1976 static tree cp_parser_function_specifier_opt
1977 (cp_parser *, cp_decl_specifier_seq *);
1978 static tree cp_parser_type_specifier
1979 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1980 int *, bool *);
1981 static tree cp_parser_simple_type_specifier
1982 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1983 static tree cp_parser_type_name
1984 (cp_parser *);
1985 static tree cp_parser_nonclass_name
1986 (cp_parser* parser);
1987 static tree cp_parser_elaborated_type_specifier
1988 (cp_parser *, bool, bool);
1989 static tree cp_parser_enum_specifier
1990 (cp_parser *);
1991 static void cp_parser_enumerator_list
1992 (cp_parser *, tree);
1993 static void cp_parser_enumerator_definition
1994 (cp_parser *, tree);
1995 static tree cp_parser_namespace_name
1996 (cp_parser *);
1997 static void cp_parser_namespace_definition
1998 (cp_parser *);
1999 static void cp_parser_namespace_body
2000 (cp_parser *);
2001 static tree cp_parser_qualified_namespace_specifier
2002 (cp_parser *);
2003 static void cp_parser_namespace_alias_definition
2004 (cp_parser *);
2005 static bool cp_parser_using_declaration
2006 (cp_parser *, bool);
2007 static void cp_parser_using_directive
2008 (cp_parser *);
2009 static tree cp_parser_alias_declaration
2010 (cp_parser *);
2011 static void cp_parser_asm_definition
2012 (cp_parser *);
2013 static void cp_parser_linkage_specification
2014 (cp_parser *);
2015 static void cp_parser_static_assert
2016 (cp_parser *, bool);
2017 static tree cp_parser_decltype
2018 (cp_parser *);
2020 /* Declarators [gram.dcl.decl] */
2022 static tree cp_parser_init_declarator
2023 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2024 static cp_declarator *cp_parser_declarator
2025 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2026 static cp_declarator *cp_parser_direct_declarator
2027 (cp_parser *, cp_parser_declarator_kind, int *, bool);
2028 static enum tree_code cp_parser_ptr_operator
2029 (cp_parser *, tree *, cp_cv_quals *, tree *);
2030 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2031 (cp_parser *);
2032 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2033 (cp_parser *);
2034 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2035 (cp_parser *);
2036 static tree cp_parser_late_return_type_opt
2037 (cp_parser *, cp_cv_quals);
2038 static tree cp_parser_declarator_id
2039 (cp_parser *, bool);
2040 static tree cp_parser_type_id
2041 (cp_parser *);
2042 static tree cp_parser_template_type_arg
2043 (cp_parser *);
2044 static tree cp_parser_trailing_type_id (cp_parser *);
2045 static tree cp_parser_type_id_1
2046 (cp_parser *, bool, bool);
2047 static void cp_parser_type_specifier_seq
2048 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2049 static tree cp_parser_parameter_declaration_clause
2050 (cp_parser *);
2051 static tree cp_parser_parameter_declaration_list
2052 (cp_parser *, bool *);
2053 static cp_parameter_declarator *cp_parser_parameter_declaration
2054 (cp_parser *, bool, bool *);
2055 static tree cp_parser_default_argument
2056 (cp_parser *, bool);
2057 static void cp_parser_function_body
2058 (cp_parser *, bool);
2059 static tree cp_parser_initializer
2060 (cp_parser *, bool *, bool *);
2061 static tree cp_parser_initializer_clause
2062 (cp_parser *, bool *);
2063 static tree cp_parser_braced_list
2064 (cp_parser*, bool*);
2065 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2066 (cp_parser *, bool *);
2068 static bool cp_parser_ctor_initializer_opt_and_function_body
2069 (cp_parser *, bool);
2071 /* Classes [gram.class] */
2073 static tree cp_parser_class_name
2074 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2075 static tree cp_parser_class_specifier
2076 (cp_parser *);
2077 static tree cp_parser_class_head
2078 (cp_parser *, bool *);
2079 static enum tag_types cp_parser_class_key
2080 (cp_parser *);
2081 static void cp_parser_member_specification_opt
2082 (cp_parser *);
2083 static void cp_parser_member_declaration
2084 (cp_parser *);
2085 static tree cp_parser_pure_specifier
2086 (cp_parser *);
2087 static tree cp_parser_constant_initializer
2088 (cp_parser *);
2090 /* Derived classes [gram.class.derived] */
2092 static tree cp_parser_base_clause
2093 (cp_parser *);
2094 static tree cp_parser_base_specifier
2095 (cp_parser *);
2097 /* Special member functions [gram.special] */
2099 static tree cp_parser_conversion_function_id
2100 (cp_parser *);
2101 static tree cp_parser_conversion_type_id
2102 (cp_parser *);
2103 static cp_declarator *cp_parser_conversion_declarator_opt
2104 (cp_parser *);
2105 static bool cp_parser_ctor_initializer_opt
2106 (cp_parser *);
2107 static void cp_parser_mem_initializer_list
2108 (cp_parser *);
2109 static tree cp_parser_mem_initializer
2110 (cp_parser *);
2111 static tree cp_parser_mem_initializer_id
2112 (cp_parser *);
2114 /* Overloading [gram.over] */
2116 static tree cp_parser_operator_function_id
2117 (cp_parser *);
2118 static tree cp_parser_operator
2119 (cp_parser *);
2121 /* Templates [gram.temp] */
2123 static void cp_parser_template_declaration
2124 (cp_parser *, bool);
2125 static tree cp_parser_template_parameter_list
2126 (cp_parser *);
2127 static tree cp_parser_template_parameter
2128 (cp_parser *, bool *, bool *);
2129 static tree cp_parser_type_parameter
2130 (cp_parser *, bool *);
2131 static tree cp_parser_template_id
2132 (cp_parser *, bool, bool, enum tag_types, bool);
2133 static tree cp_parser_template_name
2134 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2135 static tree cp_parser_template_argument_list
2136 (cp_parser *);
2137 static tree cp_parser_template_argument
2138 (cp_parser *);
2139 static void cp_parser_explicit_instantiation
2140 (cp_parser *);
2141 static void cp_parser_explicit_specialization
2142 (cp_parser *);
2144 /* Exception handling [gram.exception] */
2146 static tree cp_parser_try_block
2147 (cp_parser *);
2148 static bool cp_parser_function_try_block
2149 (cp_parser *);
2150 static void cp_parser_handler_seq
2151 (cp_parser *);
2152 static void cp_parser_handler
2153 (cp_parser *);
2154 static tree cp_parser_exception_declaration
2155 (cp_parser *);
2156 static tree cp_parser_throw_expression
2157 (cp_parser *);
2158 static tree cp_parser_exception_specification_opt
2159 (cp_parser *);
2160 static tree cp_parser_type_id_list
2161 (cp_parser *);
2163 /* GNU Extensions */
2165 static tree cp_parser_asm_specification_opt
2166 (cp_parser *);
2167 static tree cp_parser_asm_operand_list
2168 (cp_parser *);
2169 static tree cp_parser_asm_clobber_list
2170 (cp_parser *);
2171 static tree cp_parser_asm_label_list
2172 (cp_parser *);
2173 static bool cp_next_tokens_can_be_attribute_p
2174 (cp_parser *);
2175 static bool cp_next_tokens_can_be_gnu_attribute_p
2176 (cp_parser *);
2177 static bool cp_next_tokens_can_be_std_attribute_p
2178 (cp_parser *);
2179 static bool cp_nth_tokens_can_be_std_attribute_p
2180 (cp_parser *, size_t);
2181 static bool cp_nth_tokens_can_be_gnu_attribute_p
2182 (cp_parser *, size_t);
2183 static bool cp_nth_tokens_can_be_attribute_p
2184 (cp_parser *, size_t);
2185 static tree cp_parser_attributes_opt
2186 (cp_parser *);
2187 static tree cp_parser_gnu_attributes_opt
2188 (cp_parser *);
2189 static tree cp_parser_gnu_attribute_list
2190 (cp_parser *);
2191 static tree cp_parser_std_attribute
2192 (cp_parser *);
2193 static tree cp_parser_std_attribute_spec
2194 (cp_parser *);
2195 static tree cp_parser_std_attribute_spec_seq
2196 (cp_parser *);
2197 static bool cp_parser_extension_opt
2198 (cp_parser *, int *);
2199 static void cp_parser_label_declaration
2200 (cp_parser *);
2202 /* Transactional Memory Extensions */
2204 static tree cp_parser_transaction
2205 (cp_parser *, enum rid);
2206 static tree cp_parser_transaction_expression
2207 (cp_parser *, enum rid);
2208 static bool cp_parser_function_transaction
2209 (cp_parser *, enum rid);
2210 static tree cp_parser_transaction_cancel
2211 (cp_parser *);
2213 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2214 static bool cp_parser_pragma
2215 (cp_parser *, enum pragma_context);
2217 /* Objective-C++ Productions */
2219 static tree cp_parser_objc_message_receiver
2220 (cp_parser *);
2221 static tree cp_parser_objc_message_args
2222 (cp_parser *);
2223 static tree cp_parser_objc_message_expression
2224 (cp_parser *);
2225 static tree cp_parser_objc_encode_expression
2226 (cp_parser *);
2227 static tree cp_parser_objc_defs_expression
2228 (cp_parser *);
2229 static tree cp_parser_objc_protocol_expression
2230 (cp_parser *);
2231 static tree cp_parser_objc_selector_expression
2232 (cp_parser *);
2233 static tree cp_parser_objc_expression
2234 (cp_parser *);
2235 static bool cp_parser_objc_selector_p
2236 (enum cpp_ttype);
2237 static tree cp_parser_objc_selector
2238 (cp_parser *);
2239 static tree cp_parser_objc_protocol_refs_opt
2240 (cp_parser *);
2241 static void cp_parser_objc_declaration
2242 (cp_parser *, tree);
2243 static tree cp_parser_objc_statement
2244 (cp_parser *);
2245 static bool cp_parser_objc_valid_prefix_attributes
2246 (cp_parser *, tree *);
2247 static void cp_parser_objc_at_property_declaration
2248 (cp_parser *) ;
2249 static void cp_parser_objc_at_synthesize_declaration
2250 (cp_parser *) ;
2251 static void cp_parser_objc_at_dynamic_declaration
2252 (cp_parser *) ;
2253 static tree cp_parser_objc_struct_declaration
2254 (cp_parser *) ;
2256 /* Utility Routines */
2258 static tree cp_parser_lookup_name
2259 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2260 static tree cp_parser_lookup_name_simple
2261 (cp_parser *, tree, location_t);
2262 static tree cp_parser_maybe_treat_template_as_class
2263 (tree, bool);
2264 static bool cp_parser_check_declarator_template_parameters
2265 (cp_parser *, cp_declarator *, location_t);
2266 static bool cp_parser_check_template_parameters
2267 (cp_parser *, unsigned, location_t, cp_declarator *);
2268 static tree cp_parser_simple_cast_expression
2269 (cp_parser *);
2270 static tree cp_parser_global_scope_opt
2271 (cp_parser *, bool);
2272 static bool cp_parser_constructor_declarator_p
2273 (cp_parser *, bool);
2274 static tree cp_parser_function_definition_from_specifiers_and_declarator
2275 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2276 static tree cp_parser_function_definition_after_declarator
2277 (cp_parser *, bool);
2278 static void cp_parser_template_declaration_after_export
2279 (cp_parser *, bool);
2280 static void cp_parser_perform_template_parameter_access_checks
2281 (vec<deferred_access_check, va_gc> *);
2282 static tree cp_parser_single_declaration
2283 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2284 static tree cp_parser_functional_cast
2285 (cp_parser *, tree);
2286 static tree cp_parser_save_member_function_body
2287 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2288 static tree cp_parser_save_nsdmi
2289 (cp_parser *);
2290 static tree cp_parser_enclosed_template_argument_list
2291 (cp_parser *);
2292 static void cp_parser_save_default_args
2293 (cp_parser *, tree);
2294 static void cp_parser_late_parsing_for_member
2295 (cp_parser *, tree);
2296 static tree cp_parser_late_parse_one_default_arg
2297 (cp_parser *, tree, tree, tree);
2298 static void cp_parser_late_parsing_nsdmi
2299 (cp_parser *, tree);
2300 static void cp_parser_late_parsing_default_args
2301 (cp_parser *, tree);
2302 static tree cp_parser_sizeof_operand
2303 (cp_parser *, enum rid);
2304 static tree cp_parser_trait_expr
2305 (cp_parser *, enum rid);
2306 static bool cp_parser_declares_only_class_p
2307 (cp_parser *);
2308 static void cp_parser_set_storage_class
2309 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2310 static void cp_parser_set_decl_spec_type
2311 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2312 static void set_and_check_decl_spec_loc
2313 (cp_decl_specifier_seq *decl_specs,
2314 cp_decl_spec ds, cp_token *);
2315 static bool cp_parser_friend_p
2316 (const cp_decl_specifier_seq *);
2317 static void cp_parser_required_error
2318 (cp_parser *, required_token, bool);
2319 static cp_token *cp_parser_require
2320 (cp_parser *, enum cpp_ttype, required_token);
2321 static cp_token *cp_parser_require_keyword
2322 (cp_parser *, enum rid, required_token);
2323 static bool cp_parser_token_starts_function_definition_p
2324 (cp_token *);
2325 static bool cp_parser_next_token_starts_class_definition_p
2326 (cp_parser *);
2327 static bool cp_parser_next_token_ends_template_argument_p
2328 (cp_parser *);
2329 static bool cp_parser_nth_token_starts_template_argument_list_p
2330 (cp_parser *, size_t);
2331 static enum tag_types cp_parser_token_is_class_key
2332 (cp_token *);
2333 static void cp_parser_check_class_key
2334 (enum tag_types, tree type);
2335 static void cp_parser_check_access_in_redeclaration
2336 (tree type, location_t location);
2337 static bool cp_parser_optional_template_keyword
2338 (cp_parser *);
2339 static void cp_parser_pre_parsed_nested_name_specifier
2340 (cp_parser *);
2341 static bool cp_parser_cache_group
2342 (cp_parser *, enum cpp_ttype, unsigned);
2343 static tree cp_parser_cache_defarg
2344 (cp_parser *parser, bool nsdmi);
2345 static void cp_parser_parse_tentatively
2346 (cp_parser *);
2347 static void cp_parser_commit_to_tentative_parse
2348 (cp_parser *);
2349 static void cp_parser_abort_tentative_parse
2350 (cp_parser *);
2351 static bool cp_parser_parse_definitely
2352 (cp_parser *);
2353 static inline bool cp_parser_parsing_tentatively
2354 (cp_parser *);
2355 static bool cp_parser_uncommitted_to_tentative_parse_p
2356 (cp_parser *);
2357 static void cp_parser_error
2358 (cp_parser *, const char *);
2359 static void cp_parser_name_lookup_error
2360 (cp_parser *, tree, tree, name_lookup_error, location_t);
2361 static bool cp_parser_simulate_error
2362 (cp_parser *);
2363 static bool cp_parser_check_type_definition
2364 (cp_parser *);
2365 static void cp_parser_check_for_definition_in_return_type
2366 (cp_declarator *, tree, location_t type_location);
2367 static void cp_parser_check_for_invalid_template_id
2368 (cp_parser *, tree, enum tag_types, location_t location);
2369 static bool cp_parser_non_integral_constant_expression
2370 (cp_parser *, non_integral_constant);
2371 static void cp_parser_diagnose_invalid_type_name
2372 (cp_parser *, tree, tree, location_t);
2373 static bool cp_parser_parse_and_diagnose_invalid_type_name
2374 (cp_parser *);
2375 static int cp_parser_skip_to_closing_parenthesis
2376 (cp_parser *, bool, bool, bool);
2377 static void cp_parser_skip_to_end_of_statement
2378 (cp_parser *);
2379 static void cp_parser_consume_semicolon_at_end_of_statement
2380 (cp_parser *);
2381 static void cp_parser_skip_to_end_of_block_or_statement
2382 (cp_parser *);
2383 static bool cp_parser_skip_to_closing_brace
2384 (cp_parser *);
2385 static void cp_parser_skip_to_end_of_template_parameter_list
2386 (cp_parser *);
2387 static void cp_parser_skip_to_pragma_eol
2388 (cp_parser*, cp_token *);
2389 static bool cp_parser_error_occurred
2390 (cp_parser *);
2391 static bool cp_parser_allow_gnu_extensions_p
2392 (cp_parser *);
2393 static bool cp_parser_is_pure_string_literal
2394 (cp_token *);
2395 static bool cp_parser_is_string_literal
2396 (cp_token *);
2397 static bool cp_parser_is_keyword
2398 (cp_token *, enum rid);
2399 static tree cp_parser_make_typename_type
2400 (cp_parser *, tree, tree, location_t location);
2401 static cp_declarator * cp_parser_make_indirect_declarator
2402 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2404 /* Returns nonzero if we are parsing tentatively. */
2406 static inline bool
2407 cp_parser_parsing_tentatively (cp_parser* parser)
2409 return parser->context->next != NULL;
2412 /* Returns nonzero if TOKEN is a string literal. */
2414 static bool
2415 cp_parser_is_pure_string_literal (cp_token* token)
2417 return (token->type == CPP_STRING ||
2418 token->type == CPP_STRING16 ||
2419 token->type == CPP_STRING32 ||
2420 token->type == CPP_WSTRING ||
2421 token->type == CPP_UTF8STRING);
2424 /* Returns nonzero if TOKEN is a string literal
2425 of a user-defined string literal. */
2427 static bool
2428 cp_parser_is_string_literal (cp_token* token)
2430 return (cp_parser_is_pure_string_literal (token) ||
2431 token->type == CPP_STRING_USERDEF ||
2432 token->type == CPP_STRING16_USERDEF ||
2433 token->type == CPP_STRING32_USERDEF ||
2434 token->type == CPP_WSTRING_USERDEF ||
2435 token->type == CPP_UTF8STRING_USERDEF);
2438 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2440 static bool
2441 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2443 return token->keyword == keyword;
2446 /* If not parsing tentatively, issue a diagnostic of the form
2447 FILE:LINE: MESSAGE before TOKEN
2448 where TOKEN is the next token in the input stream. MESSAGE
2449 (specified by the caller) is usually of the form "expected
2450 OTHER-TOKEN". */
2452 static void
2453 cp_parser_error (cp_parser* parser, const char* gmsgid)
2455 if (!cp_parser_simulate_error (parser))
2457 cp_token *token = cp_lexer_peek_token (parser->lexer);
2458 /* This diagnostic makes more sense if it is tagged to the line
2459 of the token we just peeked at. */
2460 cp_lexer_set_source_position_from_token (token);
2462 if (token->type == CPP_PRAGMA)
2464 error_at (token->location,
2465 "%<#pragma%> is not allowed here");
2466 cp_parser_skip_to_pragma_eol (parser, token);
2467 return;
2470 c_parse_error (gmsgid,
2471 /* Because c_parser_error does not understand
2472 CPP_KEYWORD, keywords are treated like
2473 identifiers. */
2474 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2475 token->u.value, token->flags);
2479 /* Issue an error about name-lookup failing. NAME is the
2480 IDENTIFIER_NODE DECL is the result of
2481 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2482 the thing that we hoped to find. */
2484 static void
2485 cp_parser_name_lookup_error (cp_parser* parser,
2486 tree name,
2487 tree decl,
2488 name_lookup_error desired,
2489 location_t location)
2491 /* If name lookup completely failed, tell the user that NAME was not
2492 declared. */
2493 if (decl == error_mark_node)
2495 if (parser->scope && parser->scope != global_namespace)
2496 error_at (location, "%<%E::%E%> has not been declared",
2497 parser->scope, name);
2498 else if (parser->scope == global_namespace)
2499 error_at (location, "%<::%E%> has not been declared", name);
2500 else if (parser->object_scope
2501 && !CLASS_TYPE_P (parser->object_scope))
2502 error_at (location, "request for member %qE in non-class type %qT",
2503 name, parser->object_scope);
2504 else if (parser->object_scope)
2505 error_at (location, "%<%T::%E%> has not been declared",
2506 parser->object_scope, name);
2507 else
2508 error_at (location, "%qE has not been declared", name);
2510 else if (parser->scope && parser->scope != global_namespace)
2512 switch (desired)
2514 case NLE_TYPE:
2515 error_at (location, "%<%E::%E%> is not a type",
2516 parser->scope, name);
2517 break;
2518 case NLE_CXX98:
2519 error_at (location, "%<%E::%E%> is not a class or namespace",
2520 parser->scope, name);
2521 break;
2522 case NLE_NOT_CXX98:
2523 error_at (location,
2524 "%<%E::%E%> is not a class, namespace, or enumeration",
2525 parser->scope, name);
2526 break;
2527 default:
2528 gcc_unreachable ();
2532 else if (parser->scope == global_namespace)
2534 switch (desired)
2536 case NLE_TYPE:
2537 error_at (location, "%<::%E%> is not a type", name);
2538 break;
2539 case NLE_CXX98:
2540 error_at (location, "%<::%E%> is not a class or namespace", name);
2541 break;
2542 case NLE_NOT_CXX98:
2543 error_at (location,
2544 "%<::%E%> is not a class, namespace, or enumeration",
2545 name);
2546 break;
2547 default:
2548 gcc_unreachable ();
2551 else
2553 switch (desired)
2555 case NLE_TYPE:
2556 error_at (location, "%qE is not a type", name);
2557 break;
2558 case NLE_CXX98:
2559 error_at (location, "%qE is not a class or namespace", name);
2560 break;
2561 case NLE_NOT_CXX98:
2562 error_at (location,
2563 "%qE is not a class, namespace, or enumeration", name);
2564 break;
2565 default:
2566 gcc_unreachable ();
2571 /* If we are parsing tentatively, remember that an error has occurred
2572 during this tentative parse. Returns true if the error was
2573 simulated; false if a message should be issued by the caller. */
2575 static bool
2576 cp_parser_simulate_error (cp_parser* parser)
2578 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2580 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2581 return true;
2583 return false;
2586 /* This function is called when a type is defined. If type
2587 definitions are forbidden at this point, an error message is
2588 issued. */
2590 static bool
2591 cp_parser_check_type_definition (cp_parser* parser)
2593 /* If types are forbidden here, issue a message. */
2594 if (parser->type_definition_forbidden_message)
2596 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2597 in the message need to be interpreted. */
2598 error (parser->type_definition_forbidden_message);
2599 return false;
2601 return true;
2604 /* This function is called when the DECLARATOR is processed. The TYPE
2605 was a type defined in the decl-specifiers. If it is invalid to
2606 define a type in the decl-specifiers for DECLARATOR, an error is
2607 issued. TYPE_LOCATION is the location of TYPE and is used
2608 for error reporting. */
2610 static void
2611 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2612 tree type, location_t type_location)
2614 /* [dcl.fct] forbids type definitions in return types.
2615 Unfortunately, it's not easy to know whether or not we are
2616 processing a return type until after the fact. */
2617 while (declarator
2618 && (declarator->kind == cdk_pointer
2619 || declarator->kind == cdk_reference
2620 || declarator->kind == cdk_ptrmem))
2621 declarator = declarator->declarator;
2622 if (declarator
2623 && declarator->kind == cdk_function)
2625 error_at (type_location,
2626 "new types may not be defined in a return type");
2627 inform (type_location,
2628 "(perhaps a semicolon is missing after the definition of %qT)",
2629 type);
2633 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2634 "<" in any valid C++ program. If the next token is indeed "<",
2635 issue a message warning the user about what appears to be an
2636 invalid attempt to form a template-id. LOCATION is the location
2637 of the type-specifier (TYPE) */
2639 static void
2640 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2641 tree type,
2642 enum tag_types tag_type,
2643 location_t location)
2645 cp_token_position start = 0;
2647 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2649 if (TYPE_P (type))
2650 error_at (location, "%qT is not a template", type);
2651 else if (identifier_p (type))
2653 if (tag_type != none_type)
2654 error_at (location, "%qE is not a class template", type);
2655 else
2656 error_at (location, "%qE is not a template", type);
2658 else
2659 error_at (location, "invalid template-id");
2660 /* Remember the location of the invalid "<". */
2661 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2662 start = cp_lexer_token_position (parser->lexer, true);
2663 /* Consume the "<". */
2664 cp_lexer_consume_token (parser->lexer);
2665 /* Parse the template arguments. */
2666 cp_parser_enclosed_template_argument_list (parser);
2667 /* Permanently remove the invalid template arguments so that
2668 this error message is not issued again. */
2669 if (start)
2670 cp_lexer_purge_tokens_after (parser->lexer, start);
2674 /* If parsing an integral constant-expression, issue an error message
2675 about the fact that THING appeared and return true. Otherwise,
2676 return false. In either case, set
2677 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2679 static bool
2680 cp_parser_non_integral_constant_expression (cp_parser *parser,
2681 non_integral_constant thing)
2683 parser->non_integral_constant_expression_p = true;
2684 if (parser->integral_constant_expression_p)
2686 if (!parser->allow_non_integral_constant_expression_p)
2688 const char *msg = NULL;
2689 switch (thing)
2691 case NIC_FLOAT:
2692 error ("floating-point literal "
2693 "cannot appear in a constant-expression");
2694 return true;
2695 case NIC_CAST:
2696 error ("a cast to a type other than an integral or "
2697 "enumeration type cannot appear in a "
2698 "constant-expression");
2699 return true;
2700 case NIC_TYPEID:
2701 error ("%<typeid%> operator "
2702 "cannot appear in a constant-expression");
2703 return true;
2704 case NIC_NCC:
2705 error ("non-constant compound literals "
2706 "cannot appear in a constant-expression");
2707 return true;
2708 case NIC_FUNC_CALL:
2709 error ("a function call "
2710 "cannot appear in a constant-expression");
2711 return true;
2712 case NIC_INC:
2713 error ("an increment "
2714 "cannot appear in a constant-expression");
2715 return true;
2716 case NIC_DEC:
2717 error ("an decrement "
2718 "cannot appear in a constant-expression");
2719 return true;
2720 case NIC_ARRAY_REF:
2721 error ("an array reference "
2722 "cannot appear in a constant-expression");
2723 return true;
2724 case NIC_ADDR_LABEL:
2725 error ("the address of a label "
2726 "cannot appear in a constant-expression");
2727 return true;
2728 case NIC_OVERLOADED:
2729 error ("calls to overloaded operators "
2730 "cannot appear in a constant-expression");
2731 return true;
2732 case NIC_ASSIGNMENT:
2733 error ("an assignment cannot appear in a constant-expression");
2734 return true;
2735 case NIC_COMMA:
2736 error ("a comma operator "
2737 "cannot appear in a constant-expression");
2738 return true;
2739 case NIC_CONSTRUCTOR:
2740 error ("a call to a constructor "
2741 "cannot appear in a constant-expression");
2742 return true;
2743 case NIC_TRANSACTION:
2744 error ("a transaction expression "
2745 "cannot appear in a constant-expression");
2746 return true;
2747 case NIC_THIS:
2748 msg = "this";
2749 break;
2750 case NIC_FUNC_NAME:
2751 msg = "__FUNCTION__";
2752 break;
2753 case NIC_PRETTY_FUNC:
2754 msg = "__PRETTY_FUNCTION__";
2755 break;
2756 case NIC_C99_FUNC:
2757 msg = "__func__";
2758 break;
2759 case NIC_VA_ARG:
2760 msg = "va_arg";
2761 break;
2762 case NIC_ARROW:
2763 msg = "->";
2764 break;
2765 case NIC_POINT:
2766 msg = ".";
2767 break;
2768 case NIC_STAR:
2769 msg = "*";
2770 break;
2771 case NIC_ADDR:
2772 msg = "&";
2773 break;
2774 case NIC_PREINCREMENT:
2775 msg = "++";
2776 break;
2777 case NIC_PREDECREMENT:
2778 msg = "--";
2779 break;
2780 case NIC_NEW:
2781 msg = "new";
2782 break;
2783 case NIC_DEL:
2784 msg = "delete";
2785 break;
2786 default:
2787 gcc_unreachable ();
2789 if (msg)
2790 error ("%qs cannot appear in a constant-expression", msg);
2791 return true;
2794 return false;
2797 /* Emit a diagnostic for an invalid type name. SCOPE is the
2798 qualifying scope (or NULL, if none) for ID. This function commits
2799 to the current active tentative parse, if any. (Otherwise, the
2800 problematic construct might be encountered again later, resulting
2801 in duplicate error messages.) LOCATION is the location of ID. */
2803 static void
2804 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2805 tree scope, tree id,
2806 location_t location)
2808 tree decl, old_scope;
2809 cp_parser_commit_to_tentative_parse (parser);
2810 /* Try to lookup the identifier. */
2811 old_scope = parser->scope;
2812 parser->scope = scope;
2813 decl = cp_parser_lookup_name_simple (parser, id, location);
2814 parser->scope = old_scope;
2815 /* If the lookup found a template-name, it means that the user forgot
2816 to specify an argument list. Emit a useful error message. */
2817 if (TREE_CODE (decl) == TEMPLATE_DECL)
2818 error_at (location,
2819 "invalid use of template-name %qE without an argument list",
2820 decl);
2821 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2822 error_at (location, "invalid use of destructor %qD as a type", id);
2823 else if (TREE_CODE (decl) == TYPE_DECL)
2824 /* Something like 'unsigned A a;' */
2825 error_at (location, "invalid combination of multiple type-specifiers");
2826 else if (!parser->scope)
2828 /* Issue an error message. */
2829 error_at (location, "%qE does not name a type", id);
2830 /* If we're in a template class, it's possible that the user was
2831 referring to a type from a base class. For example:
2833 template <typename T> struct A { typedef T X; };
2834 template <typename T> struct B : public A<T> { X x; };
2836 The user should have said "typename A<T>::X". */
2837 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2838 inform (location, "C++11 %<constexpr%> only available with "
2839 "-std=c++11 or -std=gnu++11");
2840 else if (processing_template_decl && current_class_type
2841 && TYPE_BINFO (current_class_type))
2843 tree b;
2845 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2847 b = TREE_CHAIN (b))
2849 tree base_type = BINFO_TYPE (b);
2850 if (CLASS_TYPE_P (base_type)
2851 && dependent_type_p (base_type))
2853 tree field;
2854 /* Go from a particular instantiation of the
2855 template (which will have an empty TYPE_FIELDs),
2856 to the main version. */
2857 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2858 for (field = TYPE_FIELDS (base_type);
2859 field;
2860 field = DECL_CHAIN (field))
2861 if (TREE_CODE (field) == TYPE_DECL
2862 && DECL_NAME (field) == id)
2864 inform (location,
2865 "(perhaps %<typename %T::%E%> was intended)",
2866 BINFO_TYPE (b), id);
2867 break;
2869 if (field)
2870 break;
2875 /* Here we diagnose qualified-ids where the scope is actually correct,
2876 but the identifier does not resolve to a valid type name. */
2877 else if (parser->scope != error_mark_node)
2879 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2881 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2882 error_at (location_of (id),
2883 "%qE in namespace %qE does not name a template type",
2884 id, parser->scope);
2885 else
2886 error_at (location_of (id),
2887 "%qE in namespace %qE does not name a type",
2888 id, parser->scope);
2890 else if (CLASS_TYPE_P (parser->scope)
2891 && constructor_name_p (id, parser->scope))
2893 /* A<T>::A<T>() */
2894 error_at (location, "%<%T::%E%> names the constructor, not"
2895 " the type", parser->scope, id);
2896 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2897 error_at (location, "and %qT has no template constructors",
2898 parser->scope);
2900 else if (TYPE_P (parser->scope)
2901 && dependent_scope_p (parser->scope))
2902 error_at (location, "need %<typename%> before %<%T::%E%> because "
2903 "%qT is a dependent scope",
2904 parser->scope, id, parser->scope);
2905 else if (TYPE_P (parser->scope))
2907 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2908 error_at (location_of (id),
2909 "%qE in %q#T does not name a template type",
2910 id, parser->scope);
2911 else
2912 error_at (location_of (id),
2913 "%qE in %q#T does not name a type",
2914 id, parser->scope);
2916 else
2917 gcc_unreachable ();
2921 /* Check for a common situation where a type-name should be present,
2922 but is not, and issue a sensible error message. Returns true if an
2923 invalid type-name was detected.
2925 The situation handled by this function are variable declarations of the
2926 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2927 Usually, `ID' should name a type, but if we got here it means that it
2928 does not. We try to emit the best possible error message depending on
2929 how exactly the id-expression looks like. */
2931 static bool
2932 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2934 tree id;
2935 cp_token *token = cp_lexer_peek_token (parser->lexer);
2937 /* Avoid duplicate error about ambiguous lookup. */
2938 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2940 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2941 if (next->type == CPP_NAME && next->ambiguous_p)
2942 goto out;
2945 cp_parser_parse_tentatively (parser);
2946 id = cp_parser_id_expression (parser,
2947 /*template_keyword_p=*/false,
2948 /*check_dependency_p=*/true,
2949 /*template_p=*/NULL,
2950 /*declarator_p=*/true,
2951 /*optional_p=*/false);
2952 /* If the next token is a (, this is a function with no explicit return
2953 type, i.e. constructor, destructor or conversion op. */
2954 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2955 || TREE_CODE (id) == TYPE_DECL)
2957 cp_parser_abort_tentative_parse (parser);
2958 return false;
2960 if (!cp_parser_parse_definitely (parser))
2961 return false;
2963 /* Emit a diagnostic for the invalid type. */
2964 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2965 id, token->location);
2966 out:
2967 /* If we aren't in the middle of a declarator (i.e. in a
2968 parameter-declaration-clause), skip to the end of the declaration;
2969 there's no point in trying to process it. */
2970 if (!parser->in_declarator_p)
2971 cp_parser_skip_to_end_of_block_or_statement (parser);
2972 return true;
2975 /* Consume tokens up to, and including, the next non-nested closing `)'.
2976 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2977 are doing error recovery. Returns -1 if OR_COMMA is true and we
2978 found an unnested comma. */
2980 static int
2981 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2982 bool recovering,
2983 bool or_comma,
2984 bool consume_paren)
2986 unsigned paren_depth = 0;
2987 unsigned brace_depth = 0;
2988 unsigned square_depth = 0;
2990 if (recovering && !or_comma
2991 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2992 return 0;
2994 while (true)
2996 cp_token * token = cp_lexer_peek_token (parser->lexer);
2998 switch (token->type)
3000 case CPP_EOF:
3001 case CPP_PRAGMA_EOL:
3002 /* If we've run out of tokens, then there is no closing `)'. */
3003 return 0;
3005 /* This is good for lambda expression capture-lists. */
3006 case CPP_OPEN_SQUARE:
3007 ++square_depth;
3008 break;
3009 case CPP_CLOSE_SQUARE:
3010 if (!square_depth--)
3011 return 0;
3012 break;
3014 case CPP_SEMICOLON:
3015 /* This matches the processing in skip_to_end_of_statement. */
3016 if (!brace_depth)
3017 return 0;
3018 break;
3020 case CPP_OPEN_BRACE:
3021 ++brace_depth;
3022 break;
3023 case CPP_CLOSE_BRACE:
3024 if (!brace_depth--)
3025 return 0;
3026 break;
3028 case CPP_COMMA:
3029 if (recovering && or_comma && !brace_depth && !paren_depth
3030 && !square_depth)
3031 return -1;
3032 break;
3034 case CPP_OPEN_PAREN:
3035 if (!brace_depth)
3036 ++paren_depth;
3037 break;
3039 case CPP_CLOSE_PAREN:
3040 if (!brace_depth && !paren_depth--)
3042 if (consume_paren)
3043 cp_lexer_consume_token (parser->lexer);
3044 return 1;
3046 break;
3048 default:
3049 break;
3052 /* Consume the token. */
3053 cp_lexer_consume_token (parser->lexer);
3057 /* Consume tokens until we reach the end of the current statement.
3058 Normally, that will be just before consuming a `;'. However, if a
3059 non-nested `}' comes first, then we stop before consuming that. */
3061 static void
3062 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3064 unsigned nesting_depth = 0;
3066 while (true)
3068 cp_token *token = cp_lexer_peek_token (parser->lexer);
3070 switch (token->type)
3072 case CPP_EOF:
3073 case CPP_PRAGMA_EOL:
3074 /* If we've run out of tokens, stop. */
3075 return;
3077 case CPP_SEMICOLON:
3078 /* If the next token is a `;', we have reached the end of the
3079 statement. */
3080 if (!nesting_depth)
3081 return;
3082 break;
3084 case CPP_CLOSE_BRACE:
3085 /* If this is a non-nested '}', stop before consuming it.
3086 That way, when confronted with something like:
3088 { 3 + }
3090 we stop before consuming the closing '}', even though we
3091 have not yet reached a `;'. */
3092 if (nesting_depth == 0)
3093 return;
3095 /* If it is the closing '}' for a block that we have
3096 scanned, stop -- but only after consuming the token.
3097 That way given:
3099 void f g () { ... }
3100 typedef int I;
3102 we will stop after the body of the erroneously declared
3103 function, but before consuming the following `typedef'
3104 declaration. */
3105 if (--nesting_depth == 0)
3107 cp_lexer_consume_token (parser->lexer);
3108 return;
3111 case CPP_OPEN_BRACE:
3112 ++nesting_depth;
3113 break;
3115 default:
3116 break;
3119 /* Consume the token. */
3120 cp_lexer_consume_token (parser->lexer);
3124 /* This function is called at the end of a statement or declaration.
3125 If the next token is a semicolon, it is consumed; otherwise, error
3126 recovery is attempted. */
3128 static void
3129 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3131 /* Look for the trailing `;'. */
3132 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3134 /* If there is additional (erroneous) input, skip to the end of
3135 the statement. */
3136 cp_parser_skip_to_end_of_statement (parser);
3137 /* If the next token is now a `;', consume it. */
3138 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3139 cp_lexer_consume_token (parser->lexer);
3143 /* Skip tokens until we have consumed an entire block, or until we
3144 have consumed a non-nested `;'. */
3146 static void
3147 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3149 int nesting_depth = 0;
3151 while (nesting_depth >= 0)
3153 cp_token *token = cp_lexer_peek_token (parser->lexer);
3155 switch (token->type)
3157 case CPP_EOF:
3158 case CPP_PRAGMA_EOL:
3159 /* If we've run out of tokens, stop. */
3160 return;
3162 case CPP_SEMICOLON:
3163 /* Stop if this is an unnested ';'. */
3164 if (!nesting_depth)
3165 nesting_depth = -1;
3166 break;
3168 case CPP_CLOSE_BRACE:
3169 /* Stop if this is an unnested '}', or closes the outermost
3170 nesting level. */
3171 nesting_depth--;
3172 if (nesting_depth < 0)
3173 return;
3174 if (!nesting_depth)
3175 nesting_depth = -1;
3176 break;
3178 case CPP_OPEN_BRACE:
3179 /* Nest. */
3180 nesting_depth++;
3181 break;
3183 default:
3184 break;
3187 /* Consume the token. */
3188 cp_lexer_consume_token (parser->lexer);
3192 /* Skip tokens until a non-nested closing curly brace is the next
3193 token, or there are no more tokens. Return true in the first case,
3194 false otherwise. */
3196 static bool
3197 cp_parser_skip_to_closing_brace (cp_parser *parser)
3199 unsigned nesting_depth = 0;
3201 while (true)
3203 cp_token *token = cp_lexer_peek_token (parser->lexer);
3205 switch (token->type)
3207 case CPP_EOF:
3208 case CPP_PRAGMA_EOL:
3209 /* If we've run out of tokens, stop. */
3210 return false;
3212 case CPP_CLOSE_BRACE:
3213 /* If the next token is a non-nested `}', then we have reached
3214 the end of the current block. */
3215 if (nesting_depth-- == 0)
3216 return true;
3217 break;
3219 case CPP_OPEN_BRACE:
3220 /* If it the next token is a `{', then we are entering a new
3221 block. Consume the entire block. */
3222 ++nesting_depth;
3223 break;
3225 default:
3226 break;
3229 /* Consume the token. */
3230 cp_lexer_consume_token (parser->lexer);
3234 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3235 parameter is the PRAGMA token, allowing us to purge the entire pragma
3236 sequence. */
3238 static void
3239 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3241 cp_token *token;
3243 parser->lexer->in_pragma = false;
3246 token = cp_lexer_consume_token (parser->lexer);
3247 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3249 /* Ensure that the pragma is not parsed again. */
3250 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3253 /* Require pragma end of line, resyncing with it as necessary. The
3254 arguments are as for cp_parser_skip_to_pragma_eol. */
3256 static void
3257 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3259 parser->lexer->in_pragma = false;
3260 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3261 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3264 /* This is a simple wrapper around make_typename_type. When the id is
3265 an unresolved identifier node, we can provide a superior diagnostic
3266 using cp_parser_diagnose_invalid_type_name. */
3268 static tree
3269 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3270 tree id, location_t id_location)
3272 tree result;
3273 if (identifier_p (id))
3275 result = make_typename_type (scope, id, typename_type,
3276 /*complain=*/tf_none);
3277 if (result == error_mark_node)
3278 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3279 return result;
3281 return make_typename_type (scope, id, typename_type, tf_error);
3284 /* This is a wrapper around the
3285 make_{pointer,ptrmem,reference}_declarator functions that decides
3286 which one to call based on the CODE and CLASS_TYPE arguments. The
3287 CODE argument should be one of the values returned by
3288 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3289 appertain to the pointer or reference. */
3291 static cp_declarator *
3292 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3293 cp_cv_quals cv_qualifiers,
3294 cp_declarator *target,
3295 tree attributes)
3297 if (code == ERROR_MARK)
3298 return cp_error_declarator;
3300 if (code == INDIRECT_REF)
3301 if (class_type == NULL_TREE)
3302 return make_pointer_declarator (cv_qualifiers, target, attributes);
3303 else
3304 return make_ptrmem_declarator (cv_qualifiers, class_type,
3305 target, attributes);
3306 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3307 return make_reference_declarator (cv_qualifiers, target,
3308 false, attributes);
3309 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3310 return make_reference_declarator (cv_qualifiers, target,
3311 true, attributes);
3312 gcc_unreachable ();
3315 /* Create a new C++ parser. */
3317 static cp_parser *
3318 cp_parser_new (void)
3320 cp_parser *parser;
3321 cp_lexer *lexer;
3322 unsigned i;
3324 /* cp_lexer_new_main is called before doing GC allocation because
3325 cp_lexer_new_main might load a PCH file. */
3326 lexer = cp_lexer_new_main ();
3328 /* Initialize the binops_by_token so that we can get the tree
3329 directly from the token. */
3330 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3331 binops_by_token[binops[i].token_type] = binops[i];
3333 parser = ggc_alloc_cleared_cp_parser ();
3334 parser->lexer = lexer;
3335 parser->context = cp_parser_context_new (NULL);
3337 /* For now, we always accept GNU extensions. */
3338 parser->allow_gnu_extensions_p = 1;
3340 /* The `>' token is a greater-than operator, not the end of a
3341 template-id. */
3342 parser->greater_than_is_operator_p = true;
3344 parser->default_arg_ok_p = true;
3346 /* We are not parsing a constant-expression. */
3347 parser->integral_constant_expression_p = false;
3348 parser->allow_non_integral_constant_expression_p = false;
3349 parser->non_integral_constant_expression_p = false;
3351 /* Local variable names are not forbidden. */
3352 parser->local_variables_forbidden_p = false;
3354 /* We are not processing an `extern "C"' declaration. */
3355 parser->in_unbraced_linkage_specification_p = false;
3357 /* We are not processing a declarator. */
3358 parser->in_declarator_p = false;
3360 /* We are not processing a template-argument-list. */
3361 parser->in_template_argument_list_p = false;
3363 /* We are not in an iteration statement. */
3364 parser->in_statement = 0;
3366 /* We are not in a switch statement. */
3367 parser->in_switch_statement_p = false;
3369 /* We are not parsing a type-id inside an expression. */
3370 parser->in_type_id_in_expr_p = false;
3372 /* Declarations aren't implicitly extern "C". */
3373 parser->implicit_extern_c = false;
3375 /* String literals should be translated to the execution character set. */
3376 parser->translate_strings_p = true;
3378 /* We are not parsing a function body. */
3379 parser->in_function_body = false;
3381 /* We can correct until told otherwise. */
3382 parser->colon_corrects_to_scope_p = true;
3384 /* The unparsed function queue is empty. */
3385 push_unparsed_function_queues (parser);
3387 /* There are no classes being defined. */
3388 parser->num_classes_being_defined = 0;
3390 /* No template parameters apply. */
3391 parser->num_template_parameter_lists = 0;
3393 return parser;
3396 /* Create a cp_lexer structure which will emit the tokens in CACHE
3397 and push it onto the parser's lexer stack. This is used for delayed
3398 parsing of in-class method bodies and default arguments, and should
3399 not be confused with tentative parsing. */
3400 static void
3401 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3403 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3404 lexer->next = parser->lexer;
3405 parser->lexer = lexer;
3407 /* Move the current source position to that of the first token in the
3408 new lexer. */
3409 cp_lexer_set_source_position_from_token (lexer->next_token);
3412 /* Pop the top lexer off the parser stack. This is never used for the
3413 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3414 static void
3415 cp_parser_pop_lexer (cp_parser *parser)
3417 cp_lexer *lexer = parser->lexer;
3418 parser->lexer = lexer->next;
3419 cp_lexer_destroy (lexer);
3421 /* Put the current source position back where it was before this
3422 lexer was pushed. */
3423 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3426 /* Lexical conventions [gram.lex] */
3428 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3429 identifier. */
3431 static tree
3432 cp_parser_identifier (cp_parser* parser)
3434 cp_token *token;
3436 /* Look for the identifier. */
3437 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3438 /* Return the value. */
3439 return token ? token->u.value : error_mark_node;
3442 /* Parse a sequence of adjacent string constants. Returns a
3443 TREE_STRING representing the combined, nul-terminated string
3444 constant. If TRANSLATE is true, translate the string to the
3445 execution character set. If WIDE_OK is true, a wide string is
3446 invalid here.
3448 C++98 [lex.string] says that if a narrow string literal token is
3449 adjacent to a wide string literal token, the behavior is undefined.
3450 However, C99 6.4.5p4 says that this results in a wide string literal.
3451 We follow C99 here, for consistency with the C front end.
3453 This code is largely lifted from lex_string() in c-lex.c.
3455 FUTURE: ObjC++ will need to handle @-strings here. */
3456 static tree
3457 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3459 tree value;
3460 size_t count;
3461 struct obstack str_ob;
3462 cpp_string str, istr, *strs;
3463 cp_token *tok;
3464 enum cpp_ttype type, curr_type;
3465 int have_suffix_p = 0;
3466 tree string_tree;
3467 tree suffix_id = NULL_TREE;
3468 bool curr_tok_is_userdef_p = false;
3470 tok = cp_lexer_peek_token (parser->lexer);
3471 if (!cp_parser_is_string_literal (tok))
3473 cp_parser_error (parser, "expected string-literal");
3474 return error_mark_node;
3477 if (cpp_userdef_string_p (tok->type))
3479 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3480 curr_type = cpp_userdef_string_remove_type (tok->type);
3481 curr_tok_is_userdef_p = true;
3483 else
3485 string_tree = tok->u.value;
3486 curr_type = tok->type;
3488 type = curr_type;
3490 /* Try to avoid the overhead of creating and destroying an obstack
3491 for the common case of just one string. */
3492 if (!cp_parser_is_string_literal
3493 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3495 cp_lexer_consume_token (parser->lexer);
3497 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3498 str.len = TREE_STRING_LENGTH (string_tree);
3499 count = 1;
3501 if (curr_tok_is_userdef_p)
3503 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3504 have_suffix_p = 1;
3505 curr_type = cpp_userdef_string_remove_type (tok->type);
3507 else
3508 curr_type = tok->type;
3510 strs = &str;
3512 else
3514 gcc_obstack_init (&str_ob);
3515 count = 0;
3519 cp_lexer_consume_token (parser->lexer);
3520 count++;
3521 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3522 str.len = TREE_STRING_LENGTH (string_tree);
3524 if (curr_tok_is_userdef_p)
3526 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3527 if (have_suffix_p == 0)
3529 suffix_id = curr_suffix_id;
3530 have_suffix_p = 1;
3532 else if (have_suffix_p == 1
3533 && curr_suffix_id != suffix_id)
3535 error ("inconsistent user-defined literal suffixes"
3536 " %qD and %qD in string literal",
3537 suffix_id, curr_suffix_id);
3538 have_suffix_p = -1;
3540 curr_type = cpp_userdef_string_remove_type (tok->type);
3542 else
3543 curr_type = tok->type;
3545 if (type != curr_type)
3547 if (type == CPP_STRING)
3548 type = curr_type;
3549 else if (curr_type != CPP_STRING)
3550 error_at (tok->location,
3551 "unsupported non-standard concatenation "
3552 "of string literals");
3555 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3557 tok = cp_lexer_peek_token (parser->lexer);
3558 if (cpp_userdef_string_p (tok->type))
3560 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3561 curr_type = cpp_userdef_string_remove_type (tok->type);
3562 curr_tok_is_userdef_p = true;
3564 else
3566 string_tree = tok->u.value;
3567 curr_type = tok->type;
3568 curr_tok_is_userdef_p = false;
3571 while (cp_parser_is_string_literal (tok));
3573 strs = (cpp_string *) obstack_finish (&str_ob);
3576 if (type != CPP_STRING && !wide_ok)
3578 cp_parser_error (parser, "a wide string is invalid in this context");
3579 type = CPP_STRING;
3582 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3583 (parse_in, strs, count, &istr, type))
3585 value = build_string (istr.len, (const char *)istr.text);
3586 free (CONST_CAST (unsigned char *, istr.text));
3588 switch (type)
3590 default:
3591 case CPP_STRING:
3592 case CPP_UTF8STRING:
3593 TREE_TYPE (value) = char_array_type_node;
3594 break;
3595 case CPP_STRING16:
3596 TREE_TYPE (value) = char16_array_type_node;
3597 break;
3598 case CPP_STRING32:
3599 TREE_TYPE (value) = char32_array_type_node;
3600 break;
3601 case CPP_WSTRING:
3602 TREE_TYPE (value) = wchar_array_type_node;
3603 break;
3606 value = fix_string_type (value);
3608 if (have_suffix_p)
3610 tree literal = build_userdef_literal (suffix_id, value,
3611 OT_NONE, NULL_TREE);
3612 tok->u.value = literal;
3613 return cp_parser_userdef_string_literal (tok);
3616 else
3617 /* cpp_interpret_string has issued an error. */
3618 value = error_mark_node;
3620 if (count > 1)
3621 obstack_free (&str_ob, 0);
3623 return value;
3626 /* Look up a literal operator with the name and the exact arguments. */
3628 static tree
3629 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3631 tree decl, fns;
3632 decl = lookup_name (name);
3633 if (!decl || !is_overloaded_fn (decl))
3634 return error_mark_node;
3636 for (fns = decl; fns; fns = OVL_NEXT (fns))
3638 unsigned int ix;
3639 bool found = true;
3640 tree fn = OVL_CURRENT (fns);
3641 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3642 if (parmtypes != NULL_TREE)
3644 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3645 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3647 tree tparm = TREE_VALUE (parmtypes);
3648 tree targ = TREE_TYPE ((*args)[ix]);
3649 bool ptr = TYPE_PTR_P (tparm);
3650 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3651 if ((ptr || arr || !same_type_p (tparm, targ))
3652 && (!ptr || !arr
3653 || !same_type_p (TREE_TYPE (tparm),
3654 TREE_TYPE (targ))))
3655 found = false;
3657 if (found
3658 && ix == vec_safe_length (args)
3659 /* May be this should be sufficient_parms_p instead,
3660 depending on how exactly should user-defined literals
3661 work in presence of default arguments on the literal
3662 operator parameters. */
3663 && parmtypes == void_list_node)
3664 return fn;
3668 return error_mark_node;
3671 /* Parse a user-defined char constant. Returns a call to a user-defined
3672 literal operator taking the character as an argument. */
3674 static tree
3675 cp_parser_userdef_char_literal (cp_parser *parser)
3677 cp_token *token = cp_lexer_consume_token (parser->lexer);
3678 tree literal = token->u.value;
3679 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3680 tree value = USERDEF_LITERAL_VALUE (literal);
3681 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3682 tree decl, result;
3684 /* Build up a call to the user-defined operator */
3685 /* Lookup the name we got back from the id-expression. */
3686 vec<tree, va_gc> *args = make_tree_vector ();
3687 vec_safe_push (args, value);
3688 decl = lookup_literal_operator (name, args);
3689 if (!decl || decl == error_mark_node)
3691 error ("unable to find character literal operator %qD with %qT argument",
3692 name, TREE_TYPE (value));
3693 release_tree_vector (args);
3694 return error_mark_node;
3696 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3697 release_tree_vector (args);
3698 if (result != error_mark_node)
3699 return result;
3701 error ("unable to find character literal operator %qD with %qT argument",
3702 name, TREE_TYPE (value));
3703 return error_mark_node;
3706 /* A subroutine of cp_parser_userdef_numeric_literal to
3707 create a char... template parameter pack from a string node. */
3709 static tree
3710 make_char_string_pack (tree value)
3712 tree charvec;
3713 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3714 const char *str = TREE_STRING_POINTER (value);
3715 int i, len = TREE_STRING_LENGTH (value) - 1;
3716 tree argvec = make_tree_vec (1);
3718 /* Fill in CHARVEC with all of the parameters. */
3719 charvec = make_tree_vec (len);
3720 for (i = 0; i < len; ++i)
3721 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3723 /* Build the argument packs. */
3724 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3725 TREE_TYPE (argpack) = char_type_node;
3727 TREE_VEC_ELT (argvec, 0) = argpack;
3729 return argvec;
3732 /* A subroutine of cp_parser_userdef_numeric_literal to
3733 create a char... template parameter pack from a string node. */
3735 static tree
3736 make_string_pack (tree value)
3738 tree charvec;
3739 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3740 const char *str = TREE_STRING_POINTER (value);
3741 int i, len = TREE_STRING_LENGTH (value) - 1;
3742 tree argvec = make_tree_vec (2);
3744 tree string_char_type_node = TREE_TYPE (TREE_TYPE (value));
3746 /* First template parm is character type. */
3747 TREE_VEC_ELT (argvec, 0) = string_char_type_node;
3749 /* Fill in CHARVEC with all of the parameters. */
3750 charvec = make_tree_vec (len);
3751 for (i = 0; i < len; ++i)
3752 TREE_VEC_ELT (charvec, i) = build_int_cst (string_char_type_node, str[i]);
3754 /* Build the argument packs. */
3755 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3756 TREE_TYPE (argpack) = string_char_type_node;
3758 TREE_VEC_ELT (argvec, 1) = argpack;
3760 return argvec;
3763 /* Parse a user-defined numeric constant. returns a call to a user-defined
3764 literal operator. */
3766 static tree
3767 cp_parser_userdef_numeric_literal (cp_parser *parser)
3769 cp_token *token = cp_lexer_consume_token (parser->lexer);
3770 tree literal = token->u.value;
3771 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3772 tree value = USERDEF_LITERAL_VALUE (literal);
3773 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3774 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3775 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3776 tree decl, result;
3777 vec<tree, va_gc> *args;
3779 /* Look for a literal operator taking the exact type of numeric argument
3780 as the literal value. */
3781 args = make_tree_vector ();
3782 vec_safe_push (args, value);
3783 decl = lookup_literal_operator (name, args);
3784 if (decl && decl != error_mark_node)
3786 result = finish_call_expr (decl, &args, false, true, tf_none);
3787 if (result != error_mark_node)
3789 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3790 warning_at (token->location, OPT_Woverflow,
3791 "integer literal exceeds range of %qT type",
3792 long_long_unsigned_type_node);
3793 else
3795 if (overflow > 0)
3796 warning_at (token->location, OPT_Woverflow,
3797 "floating literal exceeds range of %qT type",
3798 long_double_type_node);
3799 else if (overflow < 0)
3800 warning_at (token->location, OPT_Woverflow,
3801 "floating literal truncated to zero");
3803 release_tree_vector (args);
3804 return result;
3807 release_tree_vector (args);
3809 /* If the numeric argument didn't work, look for a raw literal
3810 operator taking a const char* argument consisting of the number
3811 in string format. */
3812 args = make_tree_vector ();
3813 vec_safe_push (args, num_string);
3814 decl = lookup_literal_operator (name, args);
3815 if (decl && decl != error_mark_node)
3817 result = finish_call_expr (decl, &args, false, true, tf_none);
3818 if (result != error_mark_node)
3820 release_tree_vector (args);
3821 return result;
3824 release_tree_vector (args);
3826 /* If the raw literal didn't work, look for a non-type template
3827 function with parameter pack char.... Call the function with
3828 template parameter characters representing the number. */
3829 args = make_tree_vector ();
3830 decl = lookup_literal_operator (name, args);
3831 if (decl && decl != error_mark_node)
3833 tree tmpl_args = make_char_string_pack (num_string);
3834 decl = lookup_template_function (decl, tmpl_args);
3835 result = finish_call_expr (decl, &args, false, true, tf_none);
3836 if (result != error_mark_node)
3838 release_tree_vector (args);
3839 return result;
3842 release_tree_vector (args);
3844 error ("unable to find numeric literal operator %qD", name);
3845 return error_mark_node;
3848 /* Parse a user-defined string constant. Returns a call to a user-defined
3849 literal operator taking a character pointer and the length of the string
3850 as arguments. */
3852 static tree
3853 cp_parser_userdef_string_literal (cp_token *token)
3855 tree literal = token->u.value;
3856 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3857 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3858 tree value = USERDEF_LITERAL_VALUE (literal);
3859 int len = TREE_STRING_LENGTH (value)
3860 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3861 tree decl, result;
3862 vec<tree, va_gc> *args;
3864 /* Look for a template function with typename parameter CharT
3865 and parameter pack CharT... Call the function with
3866 template parameter characters representing the string. */
3867 args = make_tree_vector ();
3868 decl = lookup_literal_operator (name, args);
3869 if (decl && decl != error_mark_node)
3871 tree tmpl_args = make_string_pack (value);
3872 decl = lookup_template_function (decl, tmpl_args);
3873 result = finish_call_expr (decl, &args, false, true, tf_none);
3874 if (result != error_mark_node)
3876 release_tree_vector (args);
3877 return result;
3880 release_tree_vector (args);
3882 /* Build up a call to the user-defined operator */
3883 /* Lookup the name we got back from the id-expression. */
3884 args = make_tree_vector ();
3885 vec_safe_push (args, value);
3886 vec_safe_push (args, build_int_cst (size_type_node, len));
3887 decl = lookup_name (name);
3888 if (!decl || decl == error_mark_node)
3890 error ("unable to find string literal operator %qD", name);
3891 release_tree_vector (args);
3892 return error_mark_node;
3894 result = finish_call_expr (decl, &args, false, true, tf_none);
3895 release_tree_vector (args);
3896 if (result != error_mark_node)
3897 return result;
3899 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3900 name, TREE_TYPE (value), size_type_node);
3901 return error_mark_node;
3905 /* Basic concepts [gram.basic] */
3907 /* Parse a translation-unit.
3909 translation-unit:
3910 declaration-seq [opt]
3912 Returns TRUE if all went well. */
3914 static bool
3915 cp_parser_translation_unit (cp_parser* parser)
3917 /* The address of the first non-permanent object on the declarator
3918 obstack. */
3919 static void *declarator_obstack_base;
3921 bool success;
3923 /* Create the declarator obstack, if necessary. */
3924 if (!cp_error_declarator)
3926 gcc_obstack_init (&declarator_obstack);
3927 /* Create the error declarator. */
3928 cp_error_declarator = make_declarator (cdk_error);
3929 /* Create the empty parameter list. */
3930 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3931 /* Remember where the base of the declarator obstack lies. */
3932 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3935 cp_parser_declaration_seq_opt (parser);
3937 /* If there are no tokens left then all went well. */
3938 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3940 /* Get rid of the token array; we don't need it any more. */
3941 cp_lexer_destroy (parser->lexer);
3942 parser->lexer = NULL;
3944 /* This file might have been a context that's implicitly extern
3945 "C". If so, pop the lang context. (Only relevant for PCH.) */
3946 if (parser->implicit_extern_c)
3948 pop_lang_context ();
3949 parser->implicit_extern_c = false;
3952 /* Finish up. */
3953 finish_translation_unit ();
3955 success = true;
3957 else
3959 cp_parser_error (parser, "expected declaration");
3960 success = false;
3963 /* Make sure the declarator obstack was fully cleaned up. */
3964 gcc_assert (obstack_next_free (&declarator_obstack)
3965 == declarator_obstack_base);
3967 /* All went well. */
3968 return success;
3971 /* Return the appropriate tsubst flags for parsing, possibly in N3276
3972 decltype context. */
3974 static inline tsubst_flags_t
3975 complain_flags (bool decltype_p)
3977 tsubst_flags_t complain = tf_warning_or_error;
3978 if (decltype_p)
3979 complain |= tf_decltype;
3980 return complain;
3983 /* Expressions [gram.expr] */
3985 /* Parse a primary-expression.
3987 primary-expression:
3988 literal
3989 this
3990 ( expression )
3991 id-expression
3993 GNU Extensions:
3995 primary-expression:
3996 ( compound-statement )
3997 __builtin_va_arg ( assignment-expression , type-id )
3998 __builtin_offsetof ( type-id , offsetof-expression )
4000 C++ Extensions:
4001 __has_nothrow_assign ( type-id )
4002 __has_nothrow_constructor ( type-id )
4003 __has_nothrow_copy ( type-id )
4004 __has_trivial_assign ( type-id )
4005 __has_trivial_constructor ( type-id )
4006 __has_trivial_copy ( type-id )
4007 __has_trivial_destructor ( type-id )
4008 __has_virtual_destructor ( type-id )
4009 __is_abstract ( type-id )
4010 __is_base_of ( type-id , type-id )
4011 __is_class ( type-id )
4012 __is_convertible_to ( type-id , type-id )
4013 __is_empty ( type-id )
4014 __is_enum ( type-id )
4015 __is_final ( type-id )
4016 __is_literal_type ( type-id )
4017 __is_pod ( type-id )
4018 __is_polymorphic ( type-id )
4019 __is_std_layout ( type-id )
4020 __is_trivial ( type-id )
4021 __is_union ( type-id )
4023 Objective-C++ Extension:
4025 primary-expression:
4026 objc-expression
4028 literal:
4029 __null
4031 ADDRESS_P is true iff this expression was immediately preceded by
4032 "&" and therefore might denote a pointer-to-member. CAST_P is true
4033 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4034 true iff this expression is a template argument.
4036 Returns a representation of the expression. Upon return, *IDK
4037 indicates what kind of id-expression (if any) was present. */
4039 static tree
4040 cp_parser_primary_expression (cp_parser *parser,
4041 bool address_p,
4042 bool cast_p,
4043 bool template_arg_p,
4044 bool decltype_p,
4045 cp_id_kind *idk)
4047 cp_token *token = NULL;
4049 /* Assume the primary expression is not an id-expression. */
4050 *idk = CP_ID_KIND_NONE;
4052 /* Peek at the next token. */
4053 token = cp_lexer_peek_token (parser->lexer);
4054 switch (token->type)
4056 /* literal:
4057 integer-literal
4058 character-literal
4059 floating-literal
4060 string-literal
4061 boolean-literal
4062 pointer-literal
4063 user-defined-literal */
4064 case CPP_CHAR:
4065 case CPP_CHAR16:
4066 case CPP_CHAR32:
4067 case CPP_WCHAR:
4068 case CPP_NUMBER:
4069 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4070 return cp_parser_userdef_numeric_literal (parser);
4071 token = cp_lexer_consume_token (parser->lexer);
4072 if (TREE_CODE (token->u.value) == FIXED_CST)
4074 error_at (token->location,
4075 "fixed-point types not supported in C++");
4076 return error_mark_node;
4078 /* Floating-point literals are only allowed in an integral
4079 constant expression if they are cast to an integral or
4080 enumeration type. */
4081 if (TREE_CODE (token->u.value) == REAL_CST
4082 && parser->integral_constant_expression_p
4083 && pedantic)
4085 /* CAST_P will be set even in invalid code like "int(2.7 +
4086 ...)". Therefore, we have to check that the next token
4087 is sure to end the cast. */
4088 if (cast_p)
4090 cp_token *next_token;
4092 next_token = cp_lexer_peek_token (parser->lexer);
4093 if (/* The comma at the end of an
4094 enumerator-definition. */
4095 next_token->type != CPP_COMMA
4096 /* The curly brace at the end of an enum-specifier. */
4097 && next_token->type != CPP_CLOSE_BRACE
4098 /* The end of a statement. */
4099 && next_token->type != CPP_SEMICOLON
4100 /* The end of the cast-expression. */
4101 && next_token->type != CPP_CLOSE_PAREN
4102 /* The end of an array bound. */
4103 && next_token->type != CPP_CLOSE_SQUARE
4104 /* The closing ">" in a template-argument-list. */
4105 && (next_token->type != CPP_GREATER
4106 || parser->greater_than_is_operator_p)
4107 /* C++0x only: A ">>" treated like two ">" tokens,
4108 in a template-argument-list. */
4109 && (next_token->type != CPP_RSHIFT
4110 || (cxx_dialect == cxx98)
4111 || parser->greater_than_is_operator_p))
4112 cast_p = false;
4115 /* If we are within a cast, then the constraint that the
4116 cast is to an integral or enumeration type will be
4117 checked at that point. If we are not within a cast, then
4118 this code is invalid. */
4119 if (!cast_p)
4120 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4122 return token->u.value;
4124 case CPP_CHAR_USERDEF:
4125 case CPP_CHAR16_USERDEF:
4126 case CPP_CHAR32_USERDEF:
4127 case CPP_WCHAR_USERDEF:
4128 return cp_parser_userdef_char_literal (parser);
4130 case CPP_STRING:
4131 case CPP_STRING16:
4132 case CPP_STRING32:
4133 case CPP_WSTRING:
4134 case CPP_UTF8STRING:
4135 case CPP_STRING_USERDEF:
4136 case CPP_STRING16_USERDEF:
4137 case CPP_STRING32_USERDEF:
4138 case CPP_WSTRING_USERDEF:
4139 case CPP_UTF8STRING_USERDEF:
4140 /* ??? Should wide strings be allowed when parser->translate_strings_p
4141 is false (i.e. in attributes)? If not, we can kill the third
4142 argument to cp_parser_string_literal. */
4143 return cp_parser_string_literal (parser,
4144 parser->translate_strings_p,
4145 true);
4147 case CPP_OPEN_PAREN:
4149 tree expr;
4150 bool saved_greater_than_is_operator_p;
4152 /* Consume the `('. */
4153 cp_lexer_consume_token (parser->lexer);
4154 /* Within a parenthesized expression, a `>' token is always
4155 the greater-than operator. */
4156 saved_greater_than_is_operator_p
4157 = parser->greater_than_is_operator_p;
4158 parser->greater_than_is_operator_p = true;
4159 /* If we see `( { ' then we are looking at the beginning of
4160 a GNU statement-expression. */
4161 if (cp_parser_allow_gnu_extensions_p (parser)
4162 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4164 /* Statement-expressions are not allowed by the standard. */
4165 pedwarn (token->location, OPT_Wpedantic,
4166 "ISO C++ forbids braced-groups within expressions");
4168 /* And they're not allowed outside of a function-body; you
4169 cannot, for example, write:
4171 int i = ({ int j = 3; j + 1; });
4173 at class or namespace scope. */
4174 if (!parser->in_function_body
4175 || parser->in_template_argument_list_p)
4177 error_at (token->location,
4178 "statement-expressions are not allowed outside "
4179 "functions nor in template-argument lists");
4180 cp_parser_skip_to_end_of_block_or_statement (parser);
4181 expr = error_mark_node;
4183 else
4185 /* Start the statement-expression. */
4186 expr = begin_stmt_expr ();
4187 /* Parse the compound-statement. */
4188 cp_parser_compound_statement (parser, expr, false, false);
4189 /* Finish up. */
4190 expr = finish_stmt_expr (expr, false);
4193 else
4195 /* Parse the parenthesized expression. */
4196 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4197 /* Let the front end know that this expression was
4198 enclosed in parentheses. This matters in case, for
4199 example, the expression is of the form `A::B', since
4200 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4201 not. */
4202 expr = finish_parenthesized_expr (expr);
4203 /* DR 705: Wrapping an unqualified name in parentheses
4204 suppresses arg-dependent lookup. We want to pass back
4205 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4206 (c++/37862), but none of the others. */
4207 if (*idk != CP_ID_KIND_QUALIFIED)
4208 *idk = CP_ID_KIND_NONE;
4210 /* The `>' token might be the end of a template-id or
4211 template-parameter-list now. */
4212 parser->greater_than_is_operator_p
4213 = saved_greater_than_is_operator_p;
4214 /* Consume the `)'. */
4215 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4216 cp_parser_skip_to_end_of_statement (parser);
4218 return expr;
4221 case CPP_OPEN_SQUARE:
4222 if (c_dialect_objc ())
4223 /* We have an Objective-C++ message. */
4224 return cp_parser_objc_expression (parser);
4226 tree lam = cp_parser_lambda_expression (parser);
4227 /* Don't warn about a failed tentative parse. */
4228 if (cp_parser_error_occurred (parser))
4229 return error_mark_node;
4230 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4231 return lam;
4234 case CPP_OBJC_STRING:
4235 if (c_dialect_objc ())
4236 /* We have an Objective-C++ string literal. */
4237 return cp_parser_objc_expression (parser);
4238 cp_parser_error (parser, "expected primary-expression");
4239 return error_mark_node;
4241 case CPP_KEYWORD:
4242 switch (token->keyword)
4244 /* These two are the boolean literals. */
4245 case RID_TRUE:
4246 cp_lexer_consume_token (parser->lexer);
4247 return boolean_true_node;
4248 case RID_FALSE:
4249 cp_lexer_consume_token (parser->lexer);
4250 return boolean_false_node;
4252 /* The `__null' literal. */
4253 case RID_NULL:
4254 cp_lexer_consume_token (parser->lexer);
4255 return null_node;
4257 /* The `nullptr' literal. */
4258 case RID_NULLPTR:
4259 cp_lexer_consume_token (parser->lexer);
4260 return nullptr_node;
4262 /* Recognize the `this' keyword. */
4263 case RID_THIS:
4264 cp_lexer_consume_token (parser->lexer);
4265 if (parser->local_variables_forbidden_p)
4267 error_at (token->location,
4268 "%<this%> may not be used in this context");
4269 return error_mark_node;
4271 /* Pointers cannot appear in constant-expressions. */
4272 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4273 return error_mark_node;
4274 return finish_this_expr ();
4276 /* The `operator' keyword can be the beginning of an
4277 id-expression. */
4278 case RID_OPERATOR:
4279 goto id_expression;
4281 case RID_FUNCTION_NAME:
4282 case RID_PRETTY_FUNCTION_NAME:
4283 case RID_C99_FUNCTION_NAME:
4285 non_integral_constant name;
4287 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4288 __func__ are the names of variables -- but they are
4289 treated specially. Therefore, they are handled here,
4290 rather than relying on the generic id-expression logic
4291 below. Grammatically, these names are id-expressions.
4293 Consume the token. */
4294 token = cp_lexer_consume_token (parser->lexer);
4296 switch (token->keyword)
4298 case RID_FUNCTION_NAME:
4299 name = NIC_FUNC_NAME;
4300 break;
4301 case RID_PRETTY_FUNCTION_NAME:
4302 name = NIC_PRETTY_FUNC;
4303 break;
4304 case RID_C99_FUNCTION_NAME:
4305 name = NIC_C99_FUNC;
4306 break;
4307 default:
4308 gcc_unreachable ();
4311 if (cp_parser_non_integral_constant_expression (parser, name))
4312 return error_mark_node;
4314 /* Look up the name. */
4315 return finish_fname (token->u.value);
4318 case RID_VA_ARG:
4320 tree expression;
4321 tree type;
4322 source_location type_location;
4324 /* The `__builtin_va_arg' construct is used to handle
4325 `va_arg'. Consume the `__builtin_va_arg' token. */
4326 cp_lexer_consume_token (parser->lexer);
4327 /* Look for the opening `('. */
4328 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4329 /* Now, parse the assignment-expression. */
4330 expression = cp_parser_assignment_expression (parser,
4331 /*cast_p=*/false, NULL);
4332 /* Look for the `,'. */
4333 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4334 type_location = cp_lexer_peek_token (parser->lexer)->location;
4335 /* Parse the type-id. */
4336 type = cp_parser_type_id (parser);
4337 /* Look for the closing `)'. */
4338 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4339 /* Using `va_arg' in a constant-expression is not
4340 allowed. */
4341 if (cp_parser_non_integral_constant_expression (parser,
4342 NIC_VA_ARG))
4343 return error_mark_node;
4344 return build_x_va_arg (type_location, expression, type);
4347 case RID_OFFSETOF:
4348 return cp_parser_builtin_offsetof (parser);
4350 case RID_HAS_NOTHROW_ASSIGN:
4351 case RID_HAS_NOTHROW_CONSTRUCTOR:
4352 case RID_HAS_NOTHROW_COPY:
4353 case RID_HAS_TRIVIAL_ASSIGN:
4354 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4355 case RID_HAS_TRIVIAL_COPY:
4356 case RID_HAS_TRIVIAL_DESTRUCTOR:
4357 case RID_HAS_VIRTUAL_DESTRUCTOR:
4358 case RID_IS_ABSTRACT:
4359 case RID_IS_BASE_OF:
4360 case RID_IS_CLASS:
4361 case RID_IS_CONVERTIBLE_TO:
4362 case RID_IS_EMPTY:
4363 case RID_IS_ENUM:
4364 case RID_IS_FINAL:
4365 case RID_IS_LITERAL_TYPE:
4366 case RID_IS_POD:
4367 case RID_IS_POLYMORPHIC:
4368 case RID_IS_STD_LAYOUT:
4369 case RID_IS_TRIVIAL:
4370 case RID_IS_UNION:
4371 return cp_parser_trait_expr (parser, token->keyword);
4373 /* Objective-C++ expressions. */
4374 case RID_AT_ENCODE:
4375 case RID_AT_PROTOCOL:
4376 case RID_AT_SELECTOR:
4377 return cp_parser_objc_expression (parser);
4379 case RID_TEMPLATE:
4380 if (parser->in_function_body
4381 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4382 == CPP_LESS))
4384 error_at (token->location,
4385 "a template declaration cannot appear at block scope");
4386 cp_parser_skip_to_end_of_block_or_statement (parser);
4387 return error_mark_node;
4389 default:
4390 cp_parser_error (parser, "expected primary-expression");
4391 return error_mark_node;
4394 /* An id-expression can start with either an identifier, a
4395 `::' as the beginning of a qualified-id, or the "operator"
4396 keyword. */
4397 case CPP_NAME:
4398 case CPP_SCOPE:
4399 case CPP_TEMPLATE_ID:
4400 case CPP_NESTED_NAME_SPECIFIER:
4402 tree id_expression;
4403 tree decl;
4404 const char *error_msg;
4405 bool template_p;
4406 bool done;
4407 cp_token *id_expr_token;
4409 id_expression:
4410 /* Parse the id-expression. */
4411 id_expression
4412 = cp_parser_id_expression (parser,
4413 /*template_keyword_p=*/false,
4414 /*check_dependency_p=*/true,
4415 &template_p,
4416 /*declarator_p=*/false,
4417 /*optional_p=*/false);
4418 if (id_expression == error_mark_node)
4419 return error_mark_node;
4420 id_expr_token = token;
4421 token = cp_lexer_peek_token (parser->lexer);
4422 done = (token->type != CPP_OPEN_SQUARE
4423 && token->type != CPP_OPEN_PAREN
4424 && token->type != CPP_DOT
4425 && token->type != CPP_DEREF
4426 && token->type != CPP_PLUS_PLUS
4427 && token->type != CPP_MINUS_MINUS);
4428 /* If we have a template-id, then no further lookup is
4429 required. If the template-id was for a template-class, we
4430 will sometimes have a TYPE_DECL at this point. */
4431 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4432 || TREE_CODE (id_expression) == TYPE_DECL)
4433 decl = id_expression;
4434 /* Look up the name. */
4435 else
4437 tree ambiguous_decls;
4439 /* If we already know that this lookup is ambiguous, then
4440 we've already issued an error message; there's no reason
4441 to check again. */
4442 if (id_expr_token->type == CPP_NAME
4443 && id_expr_token->ambiguous_p)
4445 cp_parser_simulate_error (parser);
4446 return error_mark_node;
4449 decl = cp_parser_lookup_name (parser, id_expression,
4450 none_type,
4451 template_p,
4452 /*is_namespace=*/false,
4453 /*check_dependency=*/true,
4454 &ambiguous_decls,
4455 id_expr_token->location);
4456 /* If the lookup was ambiguous, an error will already have
4457 been issued. */
4458 if (ambiguous_decls)
4459 return error_mark_node;
4461 /* In Objective-C++, we may have an Objective-C 2.0
4462 dot-syntax for classes here. */
4463 if (c_dialect_objc ()
4464 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4465 && TREE_CODE (decl) == TYPE_DECL
4466 && objc_is_class_name (decl))
4468 tree component;
4469 cp_lexer_consume_token (parser->lexer);
4470 component = cp_parser_identifier (parser);
4471 if (component == error_mark_node)
4472 return error_mark_node;
4474 return objc_build_class_component_ref (id_expression, component);
4477 /* In Objective-C++, an instance variable (ivar) may be preferred
4478 to whatever cp_parser_lookup_name() found. */
4479 decl = objc_lookup_ivar (decl, id_expression);
4481 /* If name lookup gives us a SCOPE_REF, then the
4482 qualifying scope was dependent. */
4483 if (TREE_CODE (decl) == SCOPE_REF)
4485 /* At this point, we do not know if DECL is a valid
4486 integral constant expression. We assume that it is
4487 in fact such an expression, so that code like:
4489 template <int N> struct A {
4490 int a[B<N>::i];
4493 is accepted. At template-instantiation time, we
4494 will check that B<N>::i is actually a constant. */
4495 return decl;
4497 /* Check to see if DECL is a local variable in a context
4498 where that is forbidden. */
4499 if (parser->local_variables_forbidden_p
4500 && local_variable_p (decl))
4502 /* It might be that we only found DECL because we are
4503 trying to be generous with pre-ISO scoping rules.
4504 For example, consider:
4506 int i;
4507 void g() {
4508 for (int i = 0; i < 10; ++i) {}
4509 extern void f(int j = i);
4512 Here, name look up will originally find the out
4513 of scope `i'. We need to issue a warning message,
4514 but then use the global `i'. */
4515 decl = check_for_out_of_scope_variable (decl);
4516 if (local_variable_p (decl))
4518 error_at (id_expr_token->location,
4519 "local variable %qD may not appear in this context",
4520 decl);
4521 return error_mark_node;
4526 decl = (finish_id_expression
4527 (id_expression, decl, parser->scope,
4528 idk,
4529 parser->integral_constant_expression_p,
4530 parser->allow_non_integral_constant_expression_p,
4531 &parser->non_integral_constant_expression_p,
4532 template_p, done, address_p,
4533 template_arg_p,
4534 &error_msg,
4535 id_expr_token->location));
4536 if (error_msg)
4537 cp_parser_error (parser, error_msg);
4538 return decl;
4541 /* Anything else is an error. */
4542 default:
4543 cp_parser_error (parser, "expected primary-expression");
4544 return error_mark_node;
4548 static inline tree
4549 cp_parser_primary_expression (cp_parser *parser,
4550 bool address_p,
4551 bool cast_p,
4552 bool template_arg_p,
4553 cp_id_kind *idk)
4555 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4556 /*decltype*/false, idk);
4559 /* Parse an id-expression.
4561 id-expression:
4562 unqualified-id
4563 qualified-id
4565 qualified-id:
4566 :: [opt] nested-name-specifier template [opt] unqualified-id
4567 :: identifier
4568 :: operator-function-id
4569 :: template-id
4571 Return a representation of the unqualified portion of the
4572 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4573 a `::' or nested-name-specifier.
4575 Often, if the id-expression was a qualified-id, the caller will
4576 want to make a SCOPE_REF to represent the qualified-id. This
4577 function does not do this in order to avoid wastefully creating
4578 SCOPE_REFs when they are not required.
4580 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4581 `template' keyword.
4583 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4584 uninstantiated templates.
4586 If *TEMPLATE_P is non-NULL, it is set to true iff the
4587 `template' keyword is used to explicitly indicate that the entity
4588 named is a template.
4590 If DECLARATOR_P is true, the id-expression is appearing as part of
4591 a declarator, rather than as part of an expression. */
4593 static tree
4594 cp_parser_id_expression (cp_parser *parser,
4595 bool template_keyword_p,
4596 bool check_dependency_p,
4597 bool *template_p,
4598 bool declarator_p,
4599 bool optional_p)
4601 bool global_scope_p;
4602 bool nested_name_specifier_p;
4604 /* Assume the `template' keyword was not used. */
4605 if (template_p)
4606 *template_p = template_keyword_p;
4608 /* Look for the optional `::' operator. */
4609 global_scope_p
4610 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4611 != NULL_TREE);
4612 /* Look for the optional nested-name-specifier. */
4613 nested_name_specifier_p
4614 = (cp_parser_nested_name_specifier_opt (parser,
4615 /*typename_keyword_p=*/false,
4616 check_dependency_p,
4617 /*type_p=*/false,
4618 declarator_p)
4619 != NULL_TREE);
4620 /* If there is a nested-name-specifier, then we are looking at
4621 the first qualified-id production. */
4622 if (nested_name_specifier_p)
4624 tree saved_scope;
4625 tree saved_object_scope;
4626 tree saved_qualifying_scope;
4627 tree unqualified_id;
4628 bool is_template;
4630 /* See if the next token is the `template' keyword. */
4631 if (!template_p)
4632 template_p = &is_template;
4633 *template_p = cp_parser_optional_template_keyword (parser);
4634 /* Name lookup we do during the processing of the
4635 unqualified-id might obliterate SCOPE. */
4636 saved_scope = parser->scope;
4637 saved_object_scope = parser->object_scope;
4638 saved_qualifying_scope = parser->qualifying_scope;
4639 /* Process the final unqualified-id. */
4640 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4641 check_dependency_p,
4642 declarator_p,
4643 /*optional_p=*/false);
4644 /* Restore the SAVED_SCOPE for our caller. */
4645 parser->scope = saved_scope;
4646 parser->object_scope = saved_object_scope;
4647 parser->qualifying_scope = saved_qualifying_scope;
4649 return unqualified_id;
4651 /* Otherwise, if we are in global scope, then we are looking at one
4652 of the other qualified-id productions. */
4653 else if (global_scope_p)
4655 cp_token *token;
4656 tree id;
4658 /* Peek at the next token. */
4659 token = cp_lexer_peek_token (parser->lexer);
4661 /* If it's an identifier, and the next token is not a "<", then
4662 we can avoid the template-id case. This is an optimization
4663 for this common case. */
4664 if (token->type == CPP_NAME
4665 && !cp_parser_nth_token_starts_template_argument_list_p
4666 (parser, 2))
4667 return cp_parser_identifier (parser);
4669 cp_parser_parse_tentatively (parser);
4670 /* Try a template-id. */
4671 id = cp_parser_template_id (parser,
4672 /*template_keyword_p=*/false,
4673 /*check_dependency_p=*/true,
4674 none_type,
4675 declarator_p);
4676 /* If that worked, we're done. */
4677 if (cp_parser_parse_definitely (parser))
4678 return id;
4680 /* Peek at the next token. (Changes in the token buffer may
4681 have invalidated the pointer obtained above.) */
4682 token = cp_lexer_peek_token (parser->lexer);
4684 switch (token->type)
4686 case CPP_NAME:
4687 return cp_parser_identifier (parser);
4689 case CPP_KEYWORD:
4690 if (token->keyword == RID_OPERATOR)
4691 return cp_parser_operator_function_id (parser);
4692 /* Fall through. */
4694 default:
4695 cp_parser_error (parser, "expected id-expression");
4696 return error_mark_node;
4699 else
4700 return cp_parser_unqualified_id (parser, template_keyword_p,
4701 /*check_dependency_p=*/true,
4702 declarator_p,
4703 optional_p);
4706 /* Parse an unqualified-id.
4708 unqualified-id:
4709 identifier
4710 operator-function-id
4711 conversion-function-id
4712 ~ class-name
4713 template-id
4715 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4716 keyword, in a construct like `A::template ...'.
4718 Returns a representation of unqualified-id. For the `identifier'
4719 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4720 production a BIT_NOT_EXPR is returned; the operand of the
4721 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4722 other productions, see the documentation accompanying the
4723 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4724 names are looked up in uninstantiated templates. If DECLARATOR_P
4725 is true, the unqualified-id is appearing as part of a declarator,
4726 rather than as part of an expression. */
4728 static tree
4729 cp_parser_unqualified_id (cp_parser* parser,
4730 bool template_keyword_p,
4731 bool check_dependency_p,
4732 bool declarator_p,
4733 bool optional_p)
4735 cp_token *token;
4737 /* Peek at the next token. */
4738 token = cp_lexer_peek_token (parser->lexer);
4740 switch (token->type)
4742 case CPP_NAME:
4744 tree id;
4746 /* We don't know yet whether or not this will be a
4747 template-id. */
4748 cp_parser_parse_tentatively (parser);
4749 /* Try a template-id. */
4750 id = cp_parser_template_id (parser, template_keyword_p,
4751 check_dependency_p,
4752 none_type,
4753 declarator_p);
4754 /* If it worked, we're done. */
4755 if (cp_parser_parse_definitely (parser))
4756 return id;
4757 /* Otherwise, it's an ordinary identifier. */
4758 return cp_parser_identifier (parser);
4761 case CPP_TEMPLATE_ID:
4762 return cp_parser_template_id (parser, template_keyword_p,
4763 check_dependency_p,
4764 none_type,
4765 declarator_p);
4767 case CPP_COMPL:
4769 tree type_decl;
4770 tree qualifying_scope;
4771 tree object_scope;
4772 tree scope;
4773 bool done;
4775 /* Consume the `~' token. */
4776 cp_lexer_consume_token (parser->lexer);
4777 /* Parse the class-name. The standard, as written, seems to
4778 say that:
4780 template <typename T> struct S { ~S (); };
4781 template <typename T> S<T>::~S() {}
4783 is invalid, since `~' must be followed by a class-name, but
4784 `S<T>' is dependent, and so not known to be a class.
4785 That's not right; we need to look in uninstantiated
4786 templates. A further complication arises from:
4788 template <typename T> void f(T t) {
4789 t.T::~T();
4792 Here, it is not possible to look up `T' in the scope of `T'
4793 itself. We must look in both the current scope, and the
4794 scope of the containing complete expression.
4796 Yet another issue is:
4798 struct S {
4799 int S;
4800 ~S();
4803 S::~S() {}
4805 The standard does not seem to say that the `S' in `~S'
4806 should refer to the type `S' and not the data member
4807 `S::S'. */
4809 /* DR 244 says that we look up the name after the "~" in the
4810 same scope as we looked up the qualifying name. That idea
4811 isn't fully worked out; it's more complicated than that. */
4812 scope = parser->scope;
4813 object_scope = parser->object_scope;
4814 qualifying_scope = parser->qualifying_scope;
4816 /* Check for invalid scopes. */
4817 if (scope == error_mark_node)
4819 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4820 cp_lexer_consume_token (parser->lexer);
4821 return error_mark_node;
4823 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4825 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4826 error_at (token->location,
4827 "scope %qT before %<~%> is not a class-name",
4828 scope);
4829 cp_parser_simulate_error (parser);
4830 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4831 cp_lexer_consume_token (parser->lexer);
4832 return error_mark_node;
4834 gcc_assert (!scope || TYPE_P (scope));
4836 /* If the name is of the form "X::~X" it's OK even if X is a
4837 typedef. */
4838 token = cp_lexer_peek_token (parser->lexer);
4839 if (scope
4840 && token->type == CPP_NAME
4841 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4842 != CPP_LESS)
4843 && (token->u.value == TYPE_IDENTIFIER (scope)
4844 || (CLASS_TYPE_P (scope)
4845 && constructor_name_p (token->u.value, scope))))
4847 cp_lexer_consume_token (parser->lexer);
4848 return build_nt (BIT_NOT_EXPR, scope);
4851 /* ~auto means the destructor of whatever the object is. */
4852 if (cp_parser_is_keyword (token, RID_AUTO))
4854 if (cxx_dialect < cxx1y)
4855 pedwarn (input_location, 0,
4856 "%<~auto%> only available with "
4857 "-std=c++1y or -std=gnu++1y");
4858 cp_lexer_consume_token (parser->lexer);
4859 return build_nt (BIT_NOT_EXPR, make_auto ());
4862 /* If there was an explicit qualification (S::~T), first look
4863 in the scope given by the qualification (i.e., S).
4865 Note: in the calls to cp_parser_class_name below we pass
4866 typename_type so that lookup finds the injected-class-name
4867 rather than the constructor. */
4868 done = false;
4869 type_decl = NULL_TREE;
4870 if (scope)
4872 cp_parser_parse_tentatively (parser);
4873 type_decl = cp_parser_class_name (parser,
4874 /*typename_keyword_p=*/false,
4875 /*template_keyword_p=*/false,
4876 typename_type,
4877 /*check_dependency=*/false,
4878 /*class_head_p=*/false,
4879 declarator_p);
4880 if (cp_parser_parse_definitely (parser))
4881 done = true;
4883 /* In "N::S::~S", look in "N" as well. */
4884 if (!done && scope && qualifying_scope)
4886 cp_parser_parse_tentatively (parser);
4887 parser->scope = qualifying_scope;
4888 parser->object_scope = NULL_TREE;
4889 parser->qualifying_scope = NULL_TREE;
4890 type_decl
4891 = cp_parser_class_name (parser,
4892 /*typename_keyword_p=*/false,
4893 /*template_keyword_p=*/false,
4894 typename_type,
4895 /*check_dependency=*/false,
4896 /*class_head_p=*/false,
4897 declarator_p);
4898 if (cp_parser_parse_definitely (parser))
4899 done = true;
4901 /* In "p->S::~T", look in the scope given by "*p" as well. */
4902 else if (!done && object_scope)
4904 cp_parser_parse_tentatively (parser);
4905 parser->scope = object_scope;
4906 parser->object_scope = NULL_TREE;
4907 parser->qualifying_scope = NULL_TREE;
4908 type_decl
4909 = cp_parser_class_name (parser,
4910 /*typename_keyword_p=*/false,
4911 /*template_keyword_p=*/false,
4912 typename_type,
4913 /*check_dependency=*/false,
4914 /*class_head_p=*/false,
4915 declarator_p);
4916 if (cp_parser_parse_definitely (parser))
4917 done = true;
4919 /* Look in the surrounding context. */
4920 if (!done)
4922 parser->scope = NULL_TREE;
4923 parser->object_scope = NULL_TREE;
4924 parser->qualifying_scope = NULL_TREE;
4925 if (processing_template_decl)
4926 cp_parser_parse_tentatively (parser);
4927 type_decl
4928 = cp_parser_class_name (parser,
4929 /*typename_keyword_p=*/false,
4930 /*template_keyword_p=*/false,
4931 typename_type,
4932 /*check_dependency=*/false,
4933 /*class_head_p=*/false,
4934 declarator_p);
4935 if (processing_template_decl
4936 && ! cp_parser_parse_definitely (parser))
4938 /* We couldn't find a type with this name, so just accept
4939 it and check for a match at instantiation time. */
4940 type_decl = cp_parser_identifier (parser);
4941 if (type_decl != error_mark_node)
4942 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4943 return type_decl;
4946 /* If an error occurred, assume that the name of the
4947 destructor is the same as the name of the qualifying
4948 class. That allows us to keep parsing after running
4949 into ill-formed destructor names. */
4950 if (type_decl == error_mark_node && scope)
4951 return build_nt (BIT_NOT_EXPR, scope);
4952 else if (type_decl == error_mark_node)
4953 return error_mark_node;
4955 /* Check that destructor name and scope match. */
4956 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4958 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4959 error_at (token->location,
4960 "declaration of %<~%T%> as member of %qT",
4961 type_decl, scope);
4962 cp_parser_simulate_error (parser);
4963 return error_mark_node;
4966 /* [class.dtor]
4968 A typedef-name that names a class shall not be used as the
4969 identifier in the declarator for a destructor declaration. */
4970 if (declarator_p
4971 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4972 && !DECL_SELF_REFERENCE_P (type_decl)
4973 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4974 error_at (token->location,
4975 "typedef-name %qD used as destructor declarator",
4976 type_decl);
4978 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4981 case CPP_KEYWORD:
4982 if (token->keyword == RID_OPERATOR)
4984 tree id;
4986 /* This could be a template-id, so we try that first. */
4987 cp_parser_parse_tentatively (parser);
4988 /* Try a template-id. */
4989 id = cp_parser_template_id (parser, template_keyword_p,
4990 /*check_dependency_p=*/true,
4991 none_type,
4992 declarator_p);
4993 /* If that worked, we're done. */
4994 if (cp_parser_parse_definitely (parser))
4995 return id;
4996 /* We still don't know whether we're looking at an
4997 operator-function-id or a conversion-function-id. */
4998 cp_parser_parse_tentatively (parser);
4999 /* Try an operator-function-id. */
5000 id = cp_parser_operator_function_id (parser);
5001 /* If that didn't work, try a conversion-function-id. */
5002 if (!cp_parser_parse_definitely (parser))
5003 id = cp_parser_conversion_function_id (parser);
5004 else if (UDLIT_OPER_P (id))
5006 /* 17.6.3.3.5 */
5007 const char *name = UDLIT_OP_SUFFIX (id);
5008 if (name[0] != '_' && !in_system_header && declarator_p)
5009 warning (0, "literal operator suffixes not preceded by %<_%>"
5010 " are reserved for future standardization");
5013 return id;
5015 /* Fall through. */
5017 default:
5018 if (optional_p)
5019 return NULL_TREE;
5020 cp_parser_error (parser, "expected unqualified-id");
5021 return error_mark_node;
5025 /* Parse an (optional) nested-name-specifier.
5027 nested-name-specifier: [C++98]
5028 class-or-namespace-name :: nested-name-specifier [opt]
5029 class-or-namespace-name :: template nested-name-specifier [opt]
5031 nested-name-specifier: [C++0x]
5032 type-name ::
5033 namespace-name ::
5034 nested-name-specifier identifier ::
5035 nested-name-specifier template [opt] simple-template-id ::
5037 PARSER->SCOPE should be set appropriately before this function is
5038 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5039 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5040 in name lookups.
5042 Sets PARSER->SCOPE to the class (TYPE) or namespace
5043 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5044 it unchanged if there is no nested-name-specifier. Returns the new
5045 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5047 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5048 part of a declaration and/or decl-specifier. */
5050 static tree
5051 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5052 bool typename_keyword_p,
5053 bool check_dependency_p,
5054 bool type_p,
5055 bool is_declaration)
5057 bool success = false;
5058 cp_token_position start = 0;
5059 cp_token *token;
5061 /* Remember where the nested-name-specifier starts. */
5062 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5064 start = cp_lexer_token_position (parser->lexer, false);
5065 push_deferring_access_checks (dk_deferred);
5068 while (true)
5070 tree new_scope;
5071 tree old_scope;
5072 tree saved_qualifying_scope;
5073 bool template_keyword_p;
5075 /* Spot cases that cannot be the beginning of a
5076 nested-name-specifier. */
5077 token = cp_lexer_peek_token (parser->lexer);
5079 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5080 the already parsed nested-name-specifier. */
5081 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5083 /* Grab the nested-name-specifier and continue the loop. */
5084 cp_parser_pre_parsed_nested_name_specifier (parser);
5085 /* If we originally encountered this nested-name-specifier
5086 with IS_DECLARATION set to false, we will not have
5087 resolved TYPENAME_TYPEs, so we must do so here. */
5088 if (is_declaration
5089 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5091 new_scope = resolve_typename_type (parser->scope,
5092 /*only_current_p=*/false);
5093 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5094 parser->scope = new_scope;
5096 success = true;
5097 continue;
5100 /* Spot cases that cannot be the beginning of a
5101 nested-name-specifier. On the second and subsequent times
5102 through the loop, we look for the `template' keyword. */
5103 if (success && token->keyword == RID_TEMPLATE)
5105 /* A template-id can start a nested-name-specifier. */
5106 else if (token->type == CPP_TEMPLATE_ID)
5108 /* DR 743: decltype can be used in a nested-name-specifier. */
5109 else if (token_is_decltype (token))
5111 else
5113 /* If the next token is not an identifier, then it is
5114 definitely not a type-name or namespace-name. */
5115 if (token->type != CPP_NAME)
5116 break;
5117 /* If the following token is neither a `<' (to begin a
5118 template-id), nor a `::', then we are not looking at a
5119 nested-name-specifier. */
5120 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5122 if (token->type == CPP_COLON
5123 && parser->colon_corrects_to_scope_p
5124 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5126 error_at (token->location,
5127 "found %<:%> in nested-name-specifier, expected %<::%>");
5128 token->type = CPP_SCOPE;
5131 if (token->type != CPP_SCOPE
5132 && !cp_parser_nth_token_starts_template_argument_list_p
5133 (parser, 2))
5134 break;
5137 /* The nested-name-specifier is optional, so we parse
5138 tentatively. */
5139 cp_parser_parse_tentatively (parser);
5141 /* Look for the optional `template' keyword, if this isn't the
5142 first time through the loop. */
5143 if (success)
5144 template_keyword_p = cp_parser_optional_template_keyword (parser);
5145 else
5146 template_keyword_p = false;
5148 /* Save the old scope since the name lookup we are about to do
5149 might destroy it. */
5150 old_scope = parser->scope;
5151 saved_qualifying_scope = parser->qualifying_scope;
5152 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5153 look up names in "X<T>::I" in order to determine that "Y" is
5154 a template. So, if we have a typename at this point, we make
5155 an effort to look through it. */
5156 if (is_declaration
5157 && !typename_keyword_p
5158 && parser->scope
5159 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5160 parser->scope = resolve_typename_type (parser->scope,
5161 /*only_current_p=*/false);
5162 /* Parse the qualifying entity. */
5163 new_scope
5164 = cp_parser_qualifying_entity (parser,
5165 typename_keyword_p,
5166 template_keyword_p,
5167 check_dependency_p,
5168 type_p,
5169 is_declaration);
5170 /* Look for the `::' token. */
5171 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5173 /* If we found what we wanted, we keep going; otherwise, we're
5174 done. */
5175 if (!cp_parser_parse_definitely (parser))
5177 bool error_p = false;
5179 /* Restore the OLD_SCOPE since it was valid before the
5180 failed attempt at finding the last
5181 class-or-namespace-name. */
5182 parser->scope = old_scope;
5183 parser->qualifying_scope = saved_qualifying_scope;
5185 /* If the next token is a decltype, and the one after that is a
5186 `::', then the decltype has failed to resolve to a class or
5187 enumeration type. Give this error even when parsing
5188 tentatively since it can't possibly be valid--and we're going
5189 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5190 won't get another chance.*/
5191 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5192 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5193 == CPP_SCOPE))
5195 token = cp_lexer_consume_token (parser->lexer);
5196 error_at (token->location, "decltype evaluates to %qT, "
5197 "which is not a class or enumeration type",
5198 token->u.value);
5199 parser->scope = error_mark_node;
5200 error_p = true;
5201 /* As below. */
5202 success = true;
5203 cp_lexer_consume_token (parser->lexer);
5206 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5207 break;
5208 /* If the next token is an identifier, and the one after
5209 that is a `::', then any valid interpretation would have
5210 found a class-or-namespace-name. */
5211 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5212 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5213 == CPP_SCOPE)
5214 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5215 != CPP_COMPL))
5217 token = cp_lexer_consume_token (parser->lexer);
5218 if (!error_p)
5220 if (!token->ambiguous_p)
5222 tree decl;
5223 tree ambiguous_decls;
5225 decl = cp_parser_lookup_name (parser, token->u.value,
5226 none_type,
5227 /*is_template=*/false,
5228 /*is_namespace=*/false,
5229 /*check_dependency=*/true,
5230 &ambiguous_decls,
5231 token->location);
5232 if (TREE_CODE (decl) == TEMPLATE_DECL)
5233 error_at (token->location,
5234 "%qD used without template parameters",
5235 decl);
5236 else if (ambiguous_decls)
5238 // cp_parser_lookup_name has the same diagnostic,
5239 // thus make sure to emit it at most once.
5240 if (cp_parser_uncommitted_to_tentative_parse_p
5241 (parser))
5243 error_at (token->location,
5244 "reference to %qD is ambiguous",
5245 token->u.value);
5246 print_candidates (ambiguous_decls);
5248 decl = error_mark_node;
5250 else
5252 if (cxx_dialect != cxx98)
5253 cp_parser_name_lookup_error
5254 (parser, token->u.value, decl, NLE_NOT_CXX98,
5255 token->location);
5256 else
5257 cp_parser_name_lookup_error
5258 (parser, token->u.value, decl, NLE_CXX98,
5259 token->location);
5262 parser->scope = error_mark_node;
5263 error_p = true;
5264 /* Treat this as a successful nested-name-specifier
5265 due to:
5267 [basic.lookup.qual]
5269 If the name found is not a class-name (clause
5270 _class_) or namespace-name (_namespace.def_), the
5271 program is ill-formed. */
5272 success = true;
5274 cp_lexer_consume_token (parser->lexer);
5276 break;
5278 /* We've found one valid nested-name-specifier. */
5279 success = true;
5280 /* Name lookup always gives us a DECL. */
5281 if (TREE_CODE (new_scope) == TYPE_DECL)
5282 new_scope = TREE_TYPE (new_scope);
5283 /* Uses of "template" must be followed by actual templates. */
5284 if (template_keyword_p
5285 && !(CLASS_TYPE_P (new_scope)
5286 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5287 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5288 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5289 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5290 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5291 == TEMPLATE_ID_EXPR)))
5292 permerror (input_location, TYPE_P (new_scope)
5293 ? G_("%qT is not a template")
5294 : G_("%qD is not a template"),
5295 new_scope);
5296 /* If it is a class scope, try to complete it; we are about to
5297 be looking up names inside the class. */
5298 if (TYPE_P (new_scope)
5299 /* Since checking types for dependency can be expensive,
5300 avoid doing it if the type is already complete. */
5301 && !COMPLETE_TYPE_P (new_scope)
5302 /* Do not try to complete dependent types. */
5303 && !dependent_type_p (new_scope))
5305 new_scope = complete_type (new_scope);
5306 /* If it is a typedef to current class, use the current
5307 class instead, as the typedef won't have any names inside
5308 it yet. */
5309 if (!COMPLETE_TYPE_P (new_scope)
5310 && currently_open_class (new_scope))
5311 new_scope = TYPE_MAIN_VARIANT (new_scope);
5313 /* Make sure we look in the right scope the next time through
5314 the loop. */
5315 parser->scope = new_scope;
5318 /* If parsing tentatively, replace the sequence of tokens that makes
5319 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5320 token. That way, should we re-parse the token stream, we will
5321 not have to repeat the effort required to do the parse, nor will
5322 we issue duplicate error messages. */
5323 if (success && start)
5325 cp_token *token;
5327 token = cp_lexer_token_at (parser->lexer, start);
5328 /* Reset the contents of the START token. */
5329 token->type = CPP_NESTED_NAME_SPECIFIER;
5330 /* Retrieve any deferred checks. Do not pop this access checks yet
5331 so the memory will not be reclaimed during token replacing below. */
5332 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5333 token->u.tree_check_value->value = parser->scope;
5334 token->u.tree_check_value->checks = get_deferred_access_checks ();
5335 token->u.tree_check_value->qualifying_scope =
5336 parser->qualifying_scope;
5337 token->keyword = RID_MAX;
5339 /* Purge all subsequent tokens. */
5340 cp_lexer_purge_tokens_after (parser->lexer, start);
5343 if (start)
5344 pop_to_parent_deferring_access_checks ();
5346 return success ? parser->scope : NULL_TREE;
5349 /* Parse a nested-name-specifier. See
5350 cp_parser_nested_name_specifier_opt for details. This function
5351 behaves identically, except that it will an issue an error if no
5352 nested-name-specifier is present. */
5354 static tree
5355 cp_parser_nested_name_specifier (cp_parser *parser,
5356 bool typename_keyword_p,
5357 bool check_dependency_p,
5358 bool type_p,
5359 bool is_declaration)
5361 tree scope;
5363 /* Look for the nested-name-specifier. */
5364 scope = cp_parser_nested_name_specifier_opt (parser,
5365 typename_keyword_p,
5366 check_dependency_p,
5367 type_p,
5368 is_declaration);
5369 /* If it was not present, issue an error message. */
5370 if (!scope)
5372 cp_parser_error (parser, "expected nested-name-specifier");
5373 parser->scope = NULL_TREE;
5376 return scope;
5379 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5380 this is either a class-name or a namespace-name (which corresponds
5381 to the class-or-namespace-name production in the grammar). For
5382 C++0x, it can also be a type-name that refers to an enumeration
5383 type or a simple-template-id.
5385 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5386 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5387 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5388 TYPE_P is TRUE iff the next name should be taken as a class-name,
5389 even the same name is declared to be another entity in the same
5390 scope.
5392 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5393 specified by the class-or-namespace-name. If neither is found the
5394 ERROR_MARK_NODE is returned. */
5396 static tree
5397 cp_parser_qualifying_entity (cp_parser *parser,
5398 bool typename_keyword_p,
5399 bool template_keyword_p,
5400 bool check_dependency_p,
5401 bool type_p,
5402 bool is_declaration)
5404 tree saved_scope;
5405 tree saved_qualifying_scope;
5406 tree saved_object_scope;
5407 tree scope;
5408 bool only_class_p;
5409 bool successful_parse_p;
5411 /* DR 743: decltype can appear in a nested-name-specifier. */
5412 if (cp_lexer_next_token_is_decltype (parser->lexer))
5414 scope = cp_parser_decltype (parser);
5415 if (TREE_CODE (scope) != ENUMERAL_TYPE
5416 && !MAYBE_CLASS_TYPE_P (scope))
5418 cp_parser_simulate_error (parser);
5419 return error_mark_node;
5421 if (TYPE_NAME (scope))
5422 scope = TYPE_NAME (scope);
5423 return scope;
5426 /* Before we try to parse the class-name, we must save away the
5427 current PARSER->SCOPE since cp_parser_class_name will destroy
5428 it. */
5429 saved_scope = parser->scope;
5430 saved_qualifying_scope = parser->qualifying_scope;
5431 saved_object_scope = parser->object_scope;
5432 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5433 there is no need to look for a namespace-name. */
5434 only_class_p = template_keyword_p
5435 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5436 if (!only_class_p)
5437 cp_parser_parse_tentatively (parser);
5438 scope = cp_parser_class_name (parser,
5439 typename_keyword_p,
5440 template_keyword_p,
5441 type_p ? class_type : none_type,
5442 check_dependency_p,
5443 /*class_head_p=*/false,
5444 is_declaration);
5445 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5446 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5447 if (!only_class_p
5448 && cxx_dialect != cxx98
5449 && !successful_parse_p)
5451 /* Restore the saved scope. */
5452 parser->scope = saved_scope;
5453 parser->qualifying_scope = saved_qualifying_scope;
5454 parser->object_scope = saved_object_scope;
5456 /* Parse tentatively. */
5457 cp_parser_parse_tentatively (parser);
5459 /* Parse a type-name */
5460 scope = cp_parser_type_name (parser);
5462 /* "If the name found does not designate a namespace or a class,
5463 enumeration, or dependent type, the program is ill-formed."
5465 We cover classes and dependent types above and namespaces below,
5466 so this code is only looking for enums. */
5467 if (!scope || TREE_CODE (scope) != TYPE_DECL
5468 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5469 cp_parser_simulate_error (parser);
5471 successful_parse_p = cp_parser_parse_definitely (parser);
5473 /* If that didn't work, try for a namespace-name. */
5474 if (!only_class_p && !successful_parse_p)
5476 /* Restore the saved scope. */
5477 parser->scope = saved_scope;
5478 parser->qualifying_scope = saved_qualifying_scope;
5479 parser->object_scope = saved_object_scope;
5480 /* If we are not looking at an identifier followed by the scope
5481 resolution operator, then this is not part of a
5482 nested-name-specifier. (Note that this function is only used
5483 to parse the components of a nested-name-specifier.) */
5484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5485 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5486 return error_mark_node;
5487 scope = cp_parser_namespace_name (parser);
5490 return scope;
5493 /* Parse a postfix-expression.
5495 postfix-expression:
5496 primary-expression
5497 postfix-expression [ expression ]
5498 postfix-expression ( expression-list [opt] )
5499 simple-type-specifier ( expression-list [opt] )
5500 typename :: [opt] nested-name-specifier identifier
5501 ( expression-list [opt] )
5502 typename :: [opt] nested-name-specifier template [opt] template-id
5503 ( expression-list [opt] )
5504 postfix-expression . template [opt] id-expression
5505 postfix-expression -> template [opt] id-expression
5506 postfix-expression . pseudo-destructor-name
5507 postfix-expression -> pseudo-destructor-name
5508 postfix-expression ++
5509 postfix-expression --
5510 dynamic_cast < type-id > ( expression )
5511 static_cast < type-id > ( expression )
5512 reinterpret_cast < type-id > ( expression )
5513 const_cast < type-id > ( expression )
5514 typeid ( expression )
5515 typeid ( type-id )
5517 GNU Extension:
5519 postfix-expression:
5520 ( type-id ) { initializer-list , [opt] }
5522 This extension is a GNU version of the C99 compound-literal
5523 construct. (The C99 grammar uses `type-name' instead of `type-id',
5524 but they are essentially the same concept.)
5526 If ADDRESS_P is true, the postfix expression is the operand of the
5527 `&' operator. CAST_P is true if this expression is the target of a
5528 cast.
5530 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5531 class member access expressions [expr.ref].
5533 Returns a representation of the expression. */
5535 static tree
5536 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5537 bool member_access_only_p, bool decltype_p,
5538 cp_id_kind * pidk_return)
5540 cp_token *token;
5541 enum rid keyword;
5542 cp_id_kind idk = CP_ID_KIND_NONE;
5543 tree postfix_expression = NULL_TREE;
5544 bool is_member_access = false;
5546 /* Peek at the next token. */
5547 token = cp_lexer_peek_token (parser->lexer);
5548 /* Some of the productions are determined by keywords. */
5549 keyword = token->keyword;
5550 switch (keyword)
5552 case RID_DYNCAST:
5553 case RID_STATCAST:
5554 case RID_REINTCAST:
5555 case RID_CONSTCAST:
5557 tree type;
5558 tree expression;
5559 const char *saved_message;
5560 bool saved_in_type_id_in_expr_p;
5562 /* All of these can be handled in the same way from the point
5563 of view of parsing. Begin by consuming the token
5564 identifying the cast. */
5565 cp_lexer_consume_token (parser->lexer);
5567 /* New types cannot be defined in the cast. */
5568 saved_message = parser->type_definition_forbidden_message;
5569 parser->type_definition_forbidden_message
5570 = G_("types may not be defined in casts");
5572 /* Look for the opening `<'. */
5573 cp_parser_require (parser, CPP_LESS, RT_LESS);
5574 /* Parse the type to which we are casting. */
5575 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5576 parser->in_type_id_in_expr_p = true;
5577 type = cp_parser_type_id (parser);
5578 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5579 /* Look for the closing `>'. */
5580 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5581 /* Restore the old message. */
5582 parser->type_definition_forbidden_message = saved_message;
5584 /* And the expression which is being cast. */
5585 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5586 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5587 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5589 /* Only type conversions to integral or enumeration types
5590 can be used in constant-expressions. */
5591 if (!cast_valid_in_integral_constant_expression_p (type)
5592 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5593 return error_mark_node;
5595 switch (keyword)
5597 case RID_DYNCAST:
5598 postfix_expression
5599 = build_dynamic_cast (type, expression, tf_warning_or_error);
5600 break;
5601 case RID_STATCAST:
5602 postfix_expression
5603 = build_static_cast (type, expression, tf_warning_or_error);
5604 break;
5605 case RID_REINTCAST:
5606 postfix_expression
5607 = build_reinterpret_cast (type, expression,
5608 tf_warning_or_error);
5609 break;
5610 case RID_CONSTCAST:
5611 postfix_expression
5612 = build_const_cast (type, expression, tf_warning_or_error);
5613 break;
5614 default:
5615 gcc_unreachable ();
5618 break;
5620 case RID_TYPEID:
5622 tree type;
5623 const char *saved_message;
5624 bool saved_in_type_id_in_expr_p;
5626 /* Consume the `typeid' token. */
5627 cp_lexer_consume_token (parser->lexer);
5628 /* Look for the `(' token. */
5629 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5630 /* Types cannot be defined in a `typeid' expression. */
5631 saved_message = parser->type_definition_forbidden_message;
5632 parser->type_definition_forbidden_message
5633 = G_("types may not be defined in a %<typeid%> expression");
5634 /* We can't be sure yet whether we're looking at a type-id or an
5635 expression. */
5636 cp_parser_parse_tentatively (parser);
5637 /* Try a type-id first. */
5638 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5639 parser->in_type_id_in_expr_p = true;
5640 type = cp_parser_type_id (parser);
5641 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5642 /* Look for the `)' token. Otherwise, we can't be sure that
5643 we're not looking at an expression: consider `typeid (int
5644 (3))', for example. */
5645 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5646 /* If all went well, simply lookup the type-id. */
5647 if (cp_parser_parse_definitely (parser))
5648 postfix_expression = get_typeid (type, tf_warning_or_error);
5649 /* Otherwise, fall back to the expression variant. */
5650 else
5652 tree expression;
5654 /* Look for an expression. */
5655 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5656 /* Compute its typeid. */
5657 postfix_expression = build_typeid (expression, tf_warning_or_error);
5658 /* Look for the `)' token. */
5659 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5661 /* Restore the saved message. */
5662 parser->type_definition_forbidden_message = saved_message;
5663 /* `typeid' may not appear in an integral constant expression. */
5664 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5665 return error_mark_node;
5667 break;
5669 case RID_TYPENAME:
5671 tree type;
5672 /* The syntax permitted here is the same permitted for an
5673 elaborated-type-specifier. */
5674 type = cp_parser_elaborated_type_specifier (parser,
5675 /*is_friend=*/false,
5676 /*is_declaration=*/false);
5677 postfix_expression = cp_parser_functional_cast (parser, type);
5679 break;
5681 case RID_BUILTIN_SHUFFLE:
5683 vec<tree, va_gc> *vec;
5684 unsigned int i;
5685 tree p;
5686 location_t loc = token->location;
5688 cp_lexer_consume_token (parser->lexer);
5689 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5690 /*cast_p=*/false, /*allow_expansion_p=*/true,
5691 /*non_constant_p=*/NULL);
5692 if (vec == NULL)
5693 return error_mark_node;
5695 FOR_EACH_VEC_ELT (*vec, i, p)
5696 mark_exp_read (p);
5698 if (vec->length () == 2)
5699 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5700 tf_warning_or_error);
5701 else if (vec->length () == 3)
5702 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5703 tf_warning_or_error);
5704 else
5706 error_at (loc, "wrong number of arguments to "
5707 "%<__builtin_shuffle%>");
5708 return error_mark_node;
5710 break;
5713 default:
5715 tree type;
5717 /* If the next thing is a simple-type-specifier, we may be
5718 looking at a functional cast. We could also be looking at
5719 an id-expression. So, we try the functional cast, and if
5720 that doesn't work we fall back to the primary-expression. */
5721 cp_parser_parse_tentatively (parser);
5722 /* Look for the simple-type-specifier. */
5723 type = cp_parser_simple_type_specifier (parser,
5724 /*decl_specs=*/NULL,
5725 CP_PARSER_FLAGS_NONE);
5726 /* Parse the cast itself. */
5727 if (!cp_parser_error_occurred (parser))
5728 postfix_expression
5729 = cp_parser_functional_cast (parser, type);
5730 /* If that worked, we're done. */
5731 if (cp_parser_parse_definitely (parser))
5732 break;
5734 /* If the functional-cast didn't work out, try a
5735 compound-literal. */
5736 if (cp_parser_allow_gnu_extensions_p (parser)
5737 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5739 tree initializer = NULL_TREE;
5740 bool saved_in_type_id_in_expr_p;
5742 cp_parser_parse_tentatively (parser);
5743 /* Consume the `('. */
5744 cp_lexer_consume_token (parser->lexer);
5745 /* Parse the type. */
5746 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5747 parser->in_type_id_in_expr_p = true;
5748 type = cp_parser_type_id (parser);
5749 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5750 /* Look for the `)'. */
5751 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5752 /* If things aren't going well, there's no need to
5753 keep going. */
5754 if (!cp_parser_error_occurred (parser))
5756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5758 bool non_constant_p;
5759 /* Parse the brace-enclosed initializer list. */
5760 initializer = cp_parser_braced_list (parser,
5761 &non_constant_p);
5763 else
5764 cp_parser_simulate_error (parser);
5766 /* If that worked, we're definitely looking at a
5767 compound-literal expression. */
5768 if (cp_parser_parse_definitely (parser))
5770 /* Warn the user that a compound literal is not
5771 allowed in standard C++. */
5772 pedwarn (input_location, OPT_Wpedantic,
5773 "ISO C++ forbids compound-literals");
5774 /* For simplicity, we disallow compound literals in
5775 constant-expressions. We could
5776 allow compound literals of integer type, whose
5777 initializer was a constant, in constant
5778 expressions. Permitting that usage, as a further
5779 extension, would not change the meaning of any
5780 currently accepted programs. (Of course, as
5781 compound literals are not part of ISO C++, the
5782 standard has nothing to say.) */
5783 if (cp_parser_non_integral_constant_expression (parser,
5784 NIC_NCC))
5786 postfix_expression = error_mark_node;
5787 break;
5789 /* Form the representation of the compound-literal. */
5790 postfix_expression
5791 = finish_compound_literal (type, initializer,
5792 tf_warning_or_error);
5793 break;
5797 /* It must be a primary-expression. */
5798 postfix_expression
5799 = cp_parser_primary_expression (parser, address_p, cast_p,
5800 /*template_arg_p=*/false,
5801 decltype_p,
5802 &idk);
5804 break;
5807 /* Note that we don't need to worry about calling build_cplus_new on a
5808 class-valued CALL_EXPR in decltype when it isn't the end of the
5809 postfix-expression; unary_complex_lvalue will take care of that for
5810 all these cases. */
5812 /* Keep looping until the postfix-expression is complete. */
5813 while (true)
5815 if (idk == CP_ID_KIND_UNQUALIFIED
5816 && identifier_p (postfix_expression)
5817 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5818 /* It is not a Koenig lookup function call. */
5819 postfix_expression
5820 = unqualified_name_lookup_error (postfix_expression);
5822 /* Peek at the next token. */
5823 token = cp_lexer_peek_token (parser->lexer);
5825 switch (token->type)
5827 case CPP_OPEN_SQUARE:
5828 if (cp_next_tokens_can_be_std_attribute_p (parser))
5830 cp_parser_error (parser,
5831 "two consecutive %<[%> shall "
5832 "only introduce an attribute");
5833 return error_mark_node;
5835 postfix_expression
5836 = cp_parser_postfix_open_square_expression (parser,
5837 postfix_expression,
5838 false,
5839 decltype_p);
5840 idk = CP_ID_KIND_NONE;
5841 is_member_access = false;
5842 break;
5844 case CPP_OPEN_PAREN:
5845 /* postfix-expression ( expression-list [opt] ) */
5847 bool koenig_p;
5848 bool is_builtin_constant_p;
5849 bool saved_integral_constant_expression_p = false;
5850 bool saved_non_integral_constant_expression_p = false;
5851 tsubst_flags_t complain = complain_flags (decltype_p);
5852 vec<tree, va_gc> *args;
5854 is_member_access = false;
5856 is_builtin_constant_p
5857 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5858 if (is_builtin_constant_p)
5860 /* The whole point of __builtin_constant_p is to allow
5861 non-constant expressions to appear as arguments. */
5862 saved_integral_constant_expression_p
5863 = parser->integral_constant_expression_p;
5864 saved_non_integral_constant_expression_p
5865 = parser->non_integral_constant_expression_p;
5866 parser->integral_constant_expression_p = false;
5868 args = (cp_parser_parenthesized_expression_list
5869 (parser, non_attr,
5870 /*cast_p=*/false, /*allow_expansion_p=*/true,
5871 /*non_constant_p=*/NULL));
5872 if (is_builtin_constant_p)
5874 parser->integral_constant_expression_p
5875 = saved_integral_constant_expression_p;
5876 parser->non_integral_constant_expression_p
5877 = saved_non_integral_constant_expression_p;
5880 if (args == NULL)
5882 postfix_expression = error_mark_node;
5883 break;
5886 /* Function calls are not permitted in
5887 constant-expressions. */
5888 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5889 && cp_parser_non_integral_constant_expression (parser,
5890 NIC_FUNC_CALL))
5892 postfix_expression = error_mark_node;
5893 release_tree_vector (args);
5894 break;
5897 koenig_p = false;
5898 if (idk == CP_ID_KIND_UNQUALIFIED
5899 || idk == CP_ID_KIND_TEMPLATE_ID)
5901 if (identifier_p (postfix_expression))
5903 if (!args->is_empty ())
5905 koenig_p = true;
5906 if (!any_type_dependent_arguments_p (args))
5907 postfix_expression
5908 = perform_koenig_lookup (postfix_expression, args,
5909 /*include_std=*/false,
5910 complain);
5912 else
5913 postfix_expression
5914 = unqualified_fn_lookup_error (postfix_expression);
5916 /* We do not perform argument-dependent lookup if
5917 normal lookup finds a non-function, in accordance
5918 with the expected resolution of DR 218. */
5919 else if (!args->is_empty ()
5920 && is_overloaded_fn (postfix_expression))
5922 tree fn = get_first_fn (postfix_expression);
5923 fn = STRIP_TEMPLATE (fn);
5925 /* Do not do argument dependent lookup if regular
5926 lookup finds a member function or a block-scope
5927 function declaration. [basic.lookup.argdep]/3 */
5928 if (!DECL_FUNCTION_MEMBER_P (fn)
5929 && !DECL_LOCAL_FUNCTION_P (fn))
5931 koenig_p = true;
5932 if (!any_type_dependent_arguments_p (args))
5933 postfix_expression
5934 = perform_koenig_lookup (postfix_expression, args,
5935 /*include_std=*/false,
5936 complain);
5941 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5943 tree instance = TREE_OPERAND (postfix_expression, 0);
5944 tree fn = TREE_OPERAND (postfix_expression, 1);
5946 if (processing_template_decl
5947 && (type_dependent_expression_p (instance)
5948 || (!BASELINK_P (fn)
5949 && TREE_CODE (fn) != FIELD_DECL)
5950 || type_dependent_expression_p (fn)
5951 || any_type_dependent_arguments_p (args)))
5953 postfix_expression
5954 = build_nt_call_vec (postfix_expression, args);
5955 release_tree_vector (args);
5956 break;
5959 if (BASELINK_P (fn))
5961 postfix_expression
5962 = (build_new_method_call
5963 (instance, fn, &args, NULL_TREE,
5964 (idk == CP_ID_KIND_QUALIFIED
5965 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5966 : LOOKUP_NORMAL),
5967 /*fn_p=*/NULL,
5968 complain));
5970 else
5971 postfix_expression
5972 = finish_call_expr (postfix_expression, &args,
5973 /*disallow_virtual=*/false,
5974 /*koenig_p=*/false,
5975 complain);
5977 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5978 || TREE_CODE (postfix_expression) == MEMBER_REF
5979 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5980 postfix_expression = (build_offset_ref_call_from_tree
5981 (postfix_expression, &args,
5982 complain));
5983 else if (idk == CP_ID_KIND_QUALIFIED)
5984 /* A call to a static class member, or a namespace-scope
5985 function. */
5986 postfix_expression
5987 = finish_call_expr (postfix_expression, &args,
5988 /*disallow_virtual=*/true,
5989 koenig_p,
5990 complain);
5991 else
5992 /* All other function calls. */
5993 postfix_expression
5994 = finish_call_expr (postfix_expression, &args,
5995 /*disallow_virtual=*/false,
5996 koenig_p,
5997 complain);
5999 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6000 idk = CP_ID_KIND_NONE;
6002 release_tree_vector (args);
6004 break;
6006 case CPP_DOT:
6007 case CPP_DEREF:
6008 /* postfix-expression . template [opt] id-expression
6009 postfix-expression . pseudo-destructor-name
6010 postfix-expression -> template [opt] id-expression
6011 postfix-expression -> pseudo-destructor-name */
6013 /* Consume the `.' or `->' operator. */
6014 cp_lexer_consume_token (parser->lexer);
6016 postfix_expression
6017 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6018 postfix_expression,
6019 false, &idk,
6020 token->location);
6022 is_member_access = true;
6023 break;
6025 case CPP_PLUS_PLUS:
6026 /* postfix-expression ++ */
6027 /* Consume the `++' token. */
6028 cp_lexer_consume_token (parser->lexer);
6029 /* Generate a representation for the complete expression. */
6030 postfix_expression
6031 = finish_increment_expr (postfix_expression,
6032 POSTINCREMENT_EXPR);
6033 /* Increments may not appear in constant-expressions. */
6034 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6035 postfix_expression = error_mark_node;
6036 idk = CP_ID_KIND_NONE;
6037 is_member_access = false;
6038 break;
6040 case CPP_MINUS_MINUS:
6041 /* postfix-expression -- */
6042 /* Consume the `--' token. */
6043 cp_lexer_consume_token (parser->lexer);
6044 /* Generate a representation for the complete expression. */
6045 postfix_expression
6046 = finish_increment_expr (postfix_expression,
6047 POSTDECREMENT_EXPR);
6048 /* Decrements may not appear in constant-expressions. */
6049 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6050 postfix_expression = error_mark_node;
6051 idk = CP_ID_KIND_NONE;
6052 is_member_access = false;
6053 break;
6055 default:
6056 if (pidk_return != NULL)
6057 * pidk_return = idk;
6058 if (member_access_only_p)
6059 return is_member_access? postfix_expression : error_mark_node;
6060 else
6061 return postfix_expression;
6065 /* We should never get here. */
6066 gcc_unreachable ();
6067 return error_mark_node;
6070 /* This function parses Cilk Plus array notations. If a normal array expr. is
6071 parsed then the array index is passed back to the caller through *INIT_INDEX
6072 and the function returns a NULL_TREE. If array notation expr. is parsed,
6073 then *INIT_INDEX is ignored by the caller and the function returns
6074 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6075 error_mark_node. */
6077 static tree
6078 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6079 tree array_value)
6081 cp_token *token = NULL;
6082 tree length_index, stride = NULL_TREE, value_tree, array_type;
6083 if (!array_value || array_value == error_mark_node)
6085 cp_parser_skip_to_end_of_statement (parser);
6086 return error_mark_node;
6089 array_type = TREE_TYPE (array_value);
6091 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6092 parser->colon_corrects_to_scope_p = false;
6093 token = cp_lexer_peek_token (parser->lexer);
6095 if (!token)
6097 cp_parser_error (parser, "expected %<:%> or numeral");
6098 return error_mark_node;
6100 else if (token->type == CPP_COLON)
6102 /* Consume the ':'. */
6103 cp_lexer_consume_token (parser->lexer);
6105 /* If we are here, then we have a case like this A[:]. */
6106 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6108 cp_parser_error (parser, "expected %<]%>");
6109 cp_parser_skip_to_end_of_statement (parser);
6110 return error_mark_node;
6112 *init_index = NULL_TREE;
6113 stride = NULL_TREE;
6114 length_index = NULL_TREE;
6116 else
6118 /* If we are here, then there are three valid possibilities:
6119 1. ARRAY [ EXP ]
6120 2. ARRAY [ EXP : EXP ]
6121 3. ARRAY [ EXP : EXP : EXP ] */
6123 *init_index = cp_parser_expression (parser, false, NULL);
6124 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6126 /* This indicates that we have a normal array expression. */
6127 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6128 return NULL_TREE;
6131 /* Consume the ':'. */
6132 cp_lexer_consume_token (parser->lexer);
6133 length_index = cp_parser_expression (parser, false, NULL);
6134 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6136 cp_lexer_consume_token (parser->lexer);
6137 stride = cp_parser_expression (parser, false, NULL);
6140 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6142 if (*init_index == error_mark_node || length_index == error_mark_node
6143 || stride == error_mark_node)
6145 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6146 cp_lexer_consume_token (parser->lexer);
6147 return error_mark_node;
6149 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6151 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6152 length_index, stride, array_type);
6153 return value_tree;
6156 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6157 by cp_parser_builtin_offsetof. We're looking for
6159 postfix-expression [ expression ]
6160 postfix-expression [ braced-init-list ] (C++11)
6162 FOR_OFFSETOF is set if we're being called in that context, which
6163 changes how we deal with integer constant expressions. */
6165 static tree
6166 cp_parser_postfix_open_square_expression (cp_parser *parser,
6167 tree postfix_expression,
6168 bool for_offsetof,
6169 bool decltype_p)
6171 tree index = NULL_TREE;
6172 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6174 /* Consume the `[' token. */
6175 cp_lexer_consume_token (parser->lexer);
6177 /* Parse the index expression. */
6178 /* ??? For offsetof, there is a question of what to allow here. If
6179 offsetof is not being used in an integral constant expression context,
6180 then we *could* get the right answer by computing the value at runtime.
6181 If we are in an integral constant expression context, then we might
6182 could accept any constant expression; hard to say without analysis.
6183 Rather than open the barn door too wide right away, allow only integer
6184 constant expressions here. */
6185 if (for_offsetof)
6186 index = cp_parser_constant_expression (parser, false, NULL);
6187 else
6189 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6191 bool expr_nonconst_p;
6192 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6193 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6194 if (flag_enable_cilkplus
6195 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6197 error_at (cp_lexer_peek_token (parser->lexer)->location,
6198 "braced list index is not allowed with array "
6199 "notation");
6200 cp_parser_skip_to_end_of_statement (parser);
6201 return error_mark_node;
6204 else if (flag_enable_cilkplus)
6206 /* Here are have these two options:
6207 ARRAY[EXP : EXP] - Array notation expr with default
6208 stride of 1.
6209 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6210 stride. */
6211 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6212 postfix_expression);
6213 if (an_exp)
6214 return an_exp;
6216 else
6217 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6220 /* Look for the closing `]'. */
6221 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6223 /* Build the ARRAY_REF. */
6224 postfix_expression = grok_array_decl (loc, postfix_expression,
6225 index, decltype_p);
6227 /* When not doing offsetof, array references are not permitted in
6228 constant-expressions. */
6229 if (!for_offsetof
6230 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6231 postfix_expression = error_mark_node;
6233 return postfix_expression;
6236 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6237 by cp_parser_builtin_offsetof. We're looking for
6239 postfix-expression . template [opt] id-expression
6240 postfix-expression . pseudo-destructor-name
6241 postfix-expression -> template [opt] id-expression
6242 postfix-expression -> pseudo-destructor-name
6244 FOR_OFFSETOF is set if we're being called in that context. That sorta
6245 limits what of the above we'll actually accept, but nevermind.
6246 TOKEN_TYPE is the "." or "->" token, which will already have been
6247 removed from the stream. */
6249 static tree
6250 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6251 enum cpp_ttype token_type,
6252 tree postfix_expression,
6253 bool for_offsetof, cp_id_kind *idk,
6254 location_t location)
6256 tree name;
6257 bool dependent_p;
6258 bool pseudo_destructor_p;
6259 tree scope = NULL_TREE;
6261 /* If this is a `->' operator, dereference the pointer. */
6262 if (token_type == CPP_DEREF)
6263 postfix_expression = build_x_arrow (location, postfix_expression,
6264 tf_warning_or_error);
6265 /* Check to see whether or not the expression is type-dependent. */
6266 dependent_p = type_dependent_expression_p (postfix_expression);
6267 /* The identifier following the `->' or `.' is not qualified. */
6268 parser->scope = NULL_TREE;
6269 parser->qualifying_scope = NULL_TREE;
6270 parser->object_scope = NULL_TREE;
6271 *idk = CP_ID_KIND_NONE;
6273 /* Enter the scope corresponding to the type of the object
6274 given by the POSTFIX_EXPRESSION. */
6275 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6277 scope = TREE_TYPE (postfix_expression);
6278 /* According to the standard, no expression should ever have
6279 reference type. Unfortunately, we do not currently match
6280 the standard in this respect in that our internal representation
6281 of an expression may have reference type even when the standard
6282 says it does not. Therefore, we have to manually obtain the
6283 underlying type here. */
6284 scope = non_reference (scope);
6285 /* The type of the POSTFIX_EXPRESSION must be complete. */
6286 if (scope == unknown_type_node)
6288 error_at (location, "%qE does not have class type",
6289 postfix_expression);
6290 scope = NULL_TREE;
6292 /* Unlike the object expression in other contexts, *this is not
6293 required to be of complete type for purposes of class member
6294 access (5.2.5) outside the member function body. */
6295 else if (postfix_expression != current_class_ref
6296 && !(processing_template_decl && scope == current_class_type))
6297 scope = complete_type_or_else (scope, NULL_TREE);
6298 /* Let the name lookup machinery know that we are processing a
6299 class member access expression. */
6300 parser->context->object_type = scope;
6301 /* If something went wrong, we want to be able to discern that case,
6302 as opposed to the case where there was no SCOPE due to the type
6303 of expression being dependent. */
6304 if (!scope)
6305 scope = error_mark_node;
6306 /* If the SCOPE was erroneous, make the various semantic analysis
6307 functions exit quickly -- and without issuing additional error
6308 messages. */
6309 if (scope == error_mark_node)
6310 postfix_expression = error_mark_node;
6313 /* Assume this expression is not a pseudo-destructor access. */
6314 pseudo_destructor_p = false;
6316 /* If the SCOPE is a scalar type, then, if this is a valid program,
6317 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6318 is type dependent, it can be pseudo-destructor-name or something else.
6319 Try to parse it as pseudo-destructor-name first. */
6320 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6322 tree s;
6323 tree type;
6325 cp_parser_parse_tentatively (parser);
6326 /* Parse the pseudo-destructor-name. */
6327 s = NULL_TREE;
6328 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6329 &s, &type);
6330 if (dependent_p
6331 && (cp_parser_error_occurred (parser)
6332 || !SCALAR_TYPE_P (type)))
6333 cp_parser_abort_tentative_parse (parser);
6334 else if (cp_parser_parse_definitely (parser))
6336 pseudo_destructor_p = true;
6337 postfix_expression
6338 = finish_pseudo_destructor_expr (postfix_expression,
6339 s, type);
6343 if (!pseudo_destructor_p)
6345 /* If the SCOPE is not a scalar type, we are looking at an
6346 ordinary class member access expression, rather than a
6347 pseudo-destructor-name. */
6348 bool template_p;
6349 cp_token *token = cp_lexer_peek_token (parser->lexer);
6350 /* Parse the id-expression. */
6351 name = (cp_parser_id_expression
6352 (parser,
6353 cp_parser_optional_template_keyword (parser),
6354 /*check_dependency_p=*/true,
6355 &template_p,
6356 /*declarator_p=*/false,
6357 /*optional_p=*/false));
6358 /* In general, build a SCOPE_REF if the member name is qualified.
6359 However, if the name was not dependent and has already been
6360 resolved; there is no need to build the SCOPE_REF. For example;
6362 struct X { void f(); };
6363 template <typename T> void f(T* t) { t->X::f(); }
6365 Even though "t" is dependent, "X::f" is not and has been resolved
6366 to a BASELINK; there is no need to include scope information. */
6368 /* But we do need to remember that there was an explicit scope for
6369 virtual function calls. */
6370 if (parser->scope)
6371 *idk = CP_ID_KIND_QUALIFIED;
6373 /* If the name is a template-id that names a type, we will get a
6374 TYPE_DECL here. That is invalid code. */
6375 if (TREE_CODE (name) == TYPE_DECL)
6377 error_at (token->location, "invalid use of %qD", name);
6378 postfix_expression = error_mark_node;
6380 else
6382 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6384 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6386 error_at (token->location, "%<%D::%D%> is not a class member",
6387 parser->scope, name);
6388 postfix_expression = error_mark_node;
6390 else
6391 name = build_qualified_name (/*type=*/NULL_TREE,
6392 parser->scope,
6393 name,
6394 template_p);
6395 parser->scope = NULL_TREE;
6396 parser->qualifying_scope = NULL_TREE;
6397 parser->object_scope = NULL_TREE;
6399 if (parser->scope && name && BASELINK_P (name))
6400 adjust_result_of_qualified_name_lookup
6401 (name, parser->scope, scope);
6402 postfix_expression
6403 = finish_class_member_access_expr (postfix_expression, name,
6404 template_p,
6405 tf_warning_or_error);
6409 /* We no longer need to look up names in the scope of the object on
6410 the left-hand side of the `.' or `->' operator. */
6411 parser->context->object_type = NULL_TREE;
6413 /* Outside of offsetof, these operators may not appear in
6414 constant-expressions. */
6415 if (!for_offsetof
6416 && (cp_parser_non_integral_constant_expression
6417 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6418 postfix_expression = error_mark_node;
6420 return postfix_expression;
6423 /* Parse a parenthesized expression-list.
6425 expression-list:
6426 assignment-expression
6427 expression-list, assignment-expression
6429 attribute-list:
6430 expression-list
6431 identifier
6432 identifier, expression-list
6434 CAST_P is true if this expression is the target of a cast.
6436 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6437 argument pack.
6439 Returns a vector of trees. Each element is a representation of an
6440 assignment-expression. NULL is returned if the ( and or ) are
6441 missing. An empty, but allocated, vector is returned on no
6442 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6443 if we are parsing an attribute list for an attribute that wants a
6444 plain identifier argument, normal_attr for an attribute that wants
6445 an expression, or non_attr if we aren't parsing an attribute list. If
6446 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6447 not all of the expressions in the list were constant. */
6449 static vec<tree, va_gc> *
6450 cp_parser_parenthesized_expression_list (cp_parser* parser,
6451 int is_attribute_list,
6452 bool cast_p,
6453 bool allow_expansion_p,
6454 bool *non_constant_p)
6456 vec<tree, va_gc> *expression_list;
6457 bool fold_expr_p = is_attribute_list != non_attr;
6458 tree identifier = NULL_TREE;
6459 bool saved_greater_than_is_operator_p;
6461 /* Assume all the expressions will be constant. */
6462 if (non_constant_p)
6463 *non_constant_p = false;
6465 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6466 return NULL;
6468 expression_list = make_tree_vector ();
6470 /* Within a parenthesized expression, a `>' token is always
6471 the greater-than operator. */
6472 saved_greater_than_is_operator_p
6473 = parser->greater_than_is_operator_p;
6474 parser->greater_than_is_operator_p = true;
6476 /* Consume expressions until there are no more. */
6477 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6478 while (true)
6480 tree expr;
6482 /* At the beginning of attribute lists, check to see if the
6483 next token is an identifier. */
6484 if (is_attribute_list == id_attr
6485 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6487 cp_token *token;
6489 /* Consume the identifier. */
6490 token = cp_lexer_consume_token (parser->lexer);
6491 /* Save the identifier. */
6492 identifier = token->u.value;
6494 else
6496 bool expr_non_constant_p;
6498 /* Parse the next assignment-expression. */
6499 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6501 /* A braced-init-list. */
6502 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6503 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6504 if (non_constant_p && expr_non_constant_p)
6505 *non_constant_p = true;
6507 else if (non_constant_p)
6509 expr = (cp_parser_constant_expression
6510 (parser, /*allow_non_constant_p=*/true,
6511 &expr_non_constant_p));
6512 if (expr_non_constant_p)
6513 *non_constant_p = true;
6515 else
6516 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6518 if (fold_expr_p)
6519 expr = fold_non_dependent_expr (expr);
6521 /* If we have an ellipsis, then this is an expression
6522 expansion. */
6523 if (allow_expansion_p
6524 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6526 /* Consume the `...'. */
6527 cp_lexer_consume_token (parser->lexer);
6529 /* Build the argument pack. */
6530 expr = make_pack_expansion (expr);
6533 /* Add it to the list. We add error_mark_node
6534 expressions to the list, so that we can still tell if
6535 the correct form for a parenthesized expression-list
6536 is found. That gives better errors. */
6537 vec_safe_push (expression_list, expr);
6539 if (expr == error_mark_node)
6540 goto skip_comma;
6543 /* After the first item, attribute lists look the same as
6544 expression lists. */
6545 is_attribute_list = non_attr;
6547 get_comma:;
6548 /* If the next token isn't a `,', then we are done. */
6549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6550 break;
6552 /* Otherwise, consume the `,' and keep going. */
6553 cp_lexer_consume_token (parser->lexer);
6556 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6558 int ending;
6560 skip_comma:;
6561 /* We try and resync to an unnested comma, as that will give the
6562 user better diagnostics. */
6563 ending = cp_parser_skip_to_closing_parenthesis (parser,
6564 /*recovering=*/true,
6565 /*or_comma=*/true,
6566 /*consume_paren=*/true);
6567 if (ending < 0)
6568 goto get_comma;
6569 if (!ending)
6571 parser->greater_than_is_operator_p
6572 = saved_greater_than_is_operator_p;
6573 return NULL;
6577 parser->greater_than_is_operator_p
6578 = saved_greater_than_is_operator_p;
6580 if (identifier)
6581 vec_safe_insert (expression_list, 0, identifier);
6583 return expression_list;
6586 /* Parse a pseudo-destructor-name.
6588 pseudo-destructor-name:
6589 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6590 :: [opt] nested-name-specifier template template-id :: ~ type-name
6591 :: [opt] nested-name-specifier [opt] ~ type-name
6593 If either of the first two productions is used, sets *SCOPE to the
6594 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6595 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6596 or ERROR_MARK_NODE if the parse fails. */
6598 static void
6599 cp_parser_pseudo_destructor_name (cp_parser* parser,
6600 tree object,
6601 tree* scope,
6602 tree* type)
6604 bool nested_name_specifier_p;
6606 /* Handle ~auto. */
6607 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6608 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6609 && !type_dependent_expression_p (object))
6611 if (cxx_dialect < cxx1y)
6612 pedwarn (input_location, 0,
6613 "%<~auto%> only available with "
6614 "-std=c++1y or -std=gnu++1y");
6615 cp_lexer_consume_token (parser->lexer);
6616 cp_lexer_consume_token (parser->lexer);
6617 *scope = NULL_TREE;
6618 *type = TREE_TYPE (object);
6619 return;
6622 /* Assume that things will not work out. */
6623 *type = error_mark_node;
6625 /* Look for the optional `::' operator. */
6626 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6627 /* Look for the optional nested-name-specifier. */
6628 nested_name_specifier_p
6629 = (cp_parser_nested_name_specifier_opt (parser,
6630 /*typename_keyword_p=*/false,
6631 /*check_dependency_p=*/true,
6632 /*type_p=*/false,
6633 /*is_declaration=*/false)
6634 != NULL_TREE);
6635 /* Now, if we saw a nested-name-specifier, we might be doing the
6636 second production. */
6637 if (nested_name_specifier_p
6638 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6640 /* Consume the `template' keyword. */
6641 cp_lexer_consume_token (parser->lexer);
6642 /* Parse the template-id. */
6643 cp_parser_template_id (parser,
6644 /*template_keyword_p=*/true,
6645 /*check_dependency_p=*/false,
6646 class_type,
6647 /*is_declaration=*/true);
6648 /* Look for the `::' token. */
6649 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6651 /* If the next token is not a `~', then there might be some
6652 additional qualification. */
6653 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6655 /* At this point, we're looking for "type-name :: ~". The type-name
6656 must not be a class-name, since this is a pseudo-destructor. So,
6657 it must be either an enum-name, or a typedef-name -- both of which
6658 are just identifiers. So, we peek ahead to check that the "::"
6659 and "~" tokens are present; if they are not, then we can avoid
6660 calling type_name. */
6661 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6662 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6663 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6665 cp_parser_error (parser, "non-scalar type");
6666 return;
6669 /* Look for the type-name. */
6670 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6671 if (*scope == error_mark_node)
6672 return;
6674 /* Look for the `::' token. */
6675 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6677 else
6678 *scope = NULL_TREE;
6680 /* Look for the `~'. */
6681 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6683 /* Once we see the ~, this has to be a pseudo-destructor. */
6684 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6685 cp_parser_commit_to_tentative_parse (parser);
6687 /* Look for the type-name again. We are not responsible for
6688 checking that it matches the first type-name. */
6689 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6692 /* Parse a unary-expression.
6694 unary-expression:
6695 postfix-expression
6696 ++ cast-expression
6697 -- cast-expression
6698 unary-operator cast-expression
6699 sizeof unary-expression
6700 sizeof ( type-id )
6701 alignof ( type-id ) [C++0x]
6702 new-expression
6703 delete-expression
6705 GNU Extensions:
6707 unary-expression:
6708 __extension__ cast-expression
6709 __alignof__ unary-expression
6710 __alignof__ ( type-id )
6711 alignof unary-expression [C++0x]
6712 __real__ cast-expression
6713 __imag__ cast-expression
6714 && identifier
6715 sizeof ( type-id ) { initializer-list , [opt] }
6716 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6717 __alignof__ ( type-id ) { initializer-list , [opt] }
6719 ADDRESS_P is true iff the unary-expression is appearing as the
6720 operand of the `&' operator. CAST_P is true if this expression is
6721 the target of a cast.
6723 Returns a representation of the expression. */
6725 static tree
6726 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6727 bool decltype_p, cp_id_kind * pidk)
6729 cp_token *token;
6730 enum tree_code unary_operator;
6732 /* Peek at the next token. */
6733 token = cp_lexer_peek_token (parser->lexer);
6734 /* Some keywords give away the kind of expression. */
6735 if (token->type == CPP_KEYWORD)
6737 enum rid keyword = token->keyword;
6739 switch (keyword)
6741 case RID_ALIGNOF:
6742 case RID_SIZEOF:
6744 tree operand, ret;
6745 enum tree_code op;
6746 location_t first_loc;
6748 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6749 /* Consume the token. */
6750 cp_lexer_consume_token (parser->lexer);
6751 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6752 /* Parse the operand. */
6753 operand = cp_parser_sizeof_operand (parser, keyword);
6755 if (TYPE_P (operand))
6756 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6757 else
6759 /* ISO C++ defines alignof only with types, not with
6760 expressions. So pedwarn if alignof is used with a non-
6761 type expression. However, __alignof__ is ok. */
6762 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6763 pedwarn (token->location, OPT_Wpedantic,
6764 "ISO C++ does not allow %<alignof%> "
6765 "with a non-type");
6767 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6769 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6770 SIZEOF_EXPR with the original operand. */
6771 if (op == SIZEOF_EXPR && ret != error_mark_node)
6773 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6775 if (!processing_template_decl && TYPE_P (operand))
6777 ret = build_min (SIZEOF_EXPR, size_type_node,
6778 build1 (NOP_EXPR, operand,
6779 error_mark_node));
6780 SIZEOF_EXPR_TYPE_P (ret) = 1;
6782 else
6783 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6784 TREE_SIDE_EFFECTS (ret) = 0;
6785 TREE_READONLY (ret) = 1;
6787 SET_EXPR_LOCATION (ret, first_loc);
6789 return ret;
6792 case RID_NEW:
6793 return cp_parser_new_expression (parser);
6795 case RID_DELETE:
6796 return cp_parser_delete_expression (parser);
6798 case RID_EXTENSION:
6800 /* The saved value of the PEDANTIC flag. */
6801 int saved_pedantic;
6802 tree expr;
6804 /* Save away the PEDANTIC flag. */
6805 cp_parser_extension_opt (parser, &saved_pedantic);
6806 /* Parse the cast-expression. */
6807 expr = cp_parser_simple_cast_expression (parser);
6808 /* Restore the PEDANTIC flag. */
6809 pedantic = saved_pedantic;
6811 return expr;
6814 case RID_REALPART:
6815 case RID_IMAGPART:
6817 tree expression;
6819 /* Consume the `__real__' or `__imag__' token. */
6820 cp_lexer_consume_token (parser->lexer);
6821 /* Parse the cast-expression. */
6822 expression = cp_parser_simple_cast_expression (parser);
6823 /* Create the complete representation. */
6824 return build_x_unary_op (token->location,
6825 (keyword == RID_REALPART
6826 ? REALPART_EXPR : IMAGPART_EXPR),
6827 expression,
6828 tf_warning_or_error);
6830 break;
6832 case RID_TRANSACTION_ATOMIC:
6833 case RID_TRANSACTION_RELAXED:
6834 return cp_parser_transaction_expression (parser, keyword);
6836 case RID_NOEXCEPT:
6838 tree expr;
6839 const char *saved_message;
6840 bool saved_integral_constant_expression_p;
6841 bool saved_non_integral_constant_expression_p;
6842 bool saved_greater_than_is_operator_p;
6844 cp_lexer_consume_token (parser->lexer);
6845 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6847 saved_message = parser->type_definition_forbidden_message;
6848 parser->type_definition_forbidden_message
6849 = G_("types may not be defined in %<noexcept%> expressions");
6851 saved_integral_constant_expression_p
6852 = parser->integral_constant_expression_p;
6853 saved_non_integral_constant_expression_p
6854 = parser->non_integral_constant_expression_p;
6855 parser->integral_constant_expression_p = false;
6857 saved_greater_than_is_operator_p
6858 = parser->greater_than_is_operator_p;
6859 parser->greater_than_is_operator_p = true;
6861 ++cp_unevaluated_operand;
6862 ++c_inhibit_evaluation_warnings;
6863 expr = cp_parser_expression (parser, false, NULL);
6864 --c_inhibit_evaluation_warnings;
6865 --cp_unevaluated_operand;
6867 parser->greater_than_is_operator_p
6868 = saved_greater_than_is_operator_p;
6870 parser->integral_constant_expression_p
6871 = saved_integral_constant_expression_p;
6872 parser->non_integral_constant_expression_p
6873 = saved_non_integral_constant_expression_p;
6875 parser->type_definition_forbidden_message = saved_message;
6877 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6878 return finish_noexcept_expr (expr, tf_warning_or_error);
6881 default:
6882 break;
6886 /* Look for the `:: new' and `:: delete', which also signal the
6887 beginning of a new-expression, or delete-expression,
6888 respectively. If the next token is `::', then it might be one of
6889 these. */
6890 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6892 enum rid keyword;
6894 /* See if the token after the `::' is one of the keywords in
6895 which we're interested. */
6896 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6897 /* If it's `new', we have a new-expression. */
6898 if (keyword == RID_NEW)
6899 return cp_parser_new_expression (parser);
6900 /* Similarly, for `delete'. */
6901 else if (keyword == RID_DELETE)
6902 return cp_parser_delete_expression (parser);
6905 /* Look for a unary operator. */
6906 unary_operator = cp_parser_unary_operator (token);
6907 /* The `++' and `--' operators can be handled similarly, even though
6908 they are not technically unary-operators in the grammar. */
6909 if (unary_operator == ERROR_MARK)
6911 if (token->type == CPP_PLUS_PLUS)
6912 unary_operator = PREINCREMENT_EXPR;
6913 else if (token->type == CPP_MINUS_MINUS)
6914 unary_operator = PREDECREMENT_EXPR;
6915 /* Handle the GNU address-of-label extension. */
6916 else if (cp_parser_allow_gnu_extensions_p (parser)
6917 && token->type == CPP_AND_AND)
6919 tree identifier;
6920 tree expression;
6921 location_t loc = token->location;
6923 /* Consume the '&&' token. */
6924 cp_lexer_consume_token (parser->lexer);
6925 /* Look for the identifier. */
6926 identifier = cp_parser_identifier (parser);
6927 /* Create an expression representing the address. */
6928 expression = finish_label_address_expr (identifier, loc);
6929 if (cp_parser_non_integral_constant_expression (parser,
6930 NIC_ADDR_LABEL))
6931 expression = error_mark_node;
6932 return expression;
6935 if (unary_operator != ERROR_MARK)
6937 tree cast_expression;
6938 tree expression = error_mark_node;
6939 non_integral_constant non_constant_p = NIC_NONE;
6940 location_t loc = token->location;
6941 tsubst_flags_t complain = complain_flags (decltype_p);
6943 /* Consume the operator token. */
6944 token = cp_lexer_consume_token (parser->lexer);
6945 /* Parse the cast-expression. */
6946 cast_expression
6947 = cp_parser_cast_expression (parser,
6948 unary_operator == ADDR_EXPR,
6949 /*cast_p=*/false,
6950 /*decltype*/false,
6951 pidk);
6952 /* Now, build an appropriate representation. */
6953 switch (unary_operator)
6955 case INDIRECT_REF:
6956 non_constant_p = NIC_STAR;
6957 expression = build_x_indirect_ref (loc, cast_expression,
6958 RO_UNARY_STAR,
6959 complain);
6960 break;
6962 case ADDR_EXPR:
6963 non_constant_p = NIC_ADDR;
6964 /* Fall through. */
6965 case BIT_NOT_EXPR:
6966 expression = build_x_unary_op (loc, unary_operator,
6967 cast_expression,
6968 complain);
6969 break;
6971 case PREINCREMENT_EXPR:
6972 case PREDECREMENT_EXPR:
6973 non_constant_p = unary_operator == PREINCREMENT_EXPR
6974 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6975 /* Fall through. */
6976 case UNARY_PLUS_EXPR:
6977 case NEGATE_EXPR:
6978 case TRUTH_NOT_EXPR:
6979 expression = finish_unary_op_expr (loc, unary_operator,
6980 cast_expression, complain);
6981 break;
6983 default:
6984 gcc_unreachable ();
6987 if (non_constant_p != NIC_NONE
6988 && cp_parser_non_integral_constant_expression (parser,
6989 non_constant_p))
6990 expression = error_mark_node;
6992 return expression;
6995 return cp_parser_postfix_expression (parser, address_p, cast_p,
6996 /*member_access_only_p=*/false,
6997 decltype_p,
6998 pidk);
7001 static inline tree
7002 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7003 cp_id_kind * pidk)
7005 return cp_parser_unary_expression (parser, address_p, cast_p,
7006 /*decltype*/false, pidk);
7009 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7010 unary-operator, the corresponding tree code is returned. */
7012 static enum tree_code
7013 cp_parser_unary_operator (cp_token* token)
7015 switch (token->type)
7017 case CPP_MULT:
7018 return INDIRECT_REF;
7020 case CPP_AND:
7021 return ADDR_EXPR;
7023 case CPP_PLUS:
7024 return UNARY_PLUS_EXPR;
7026 case CPP_MINUS:
7027 return NEGATE_EXPR;
7029 case CPP_NOT:
7030 return TRUTH_NOT_EXPR;
7032 case CPP_COMPL:
7033 return BIT_NOT_EXPR;
7035 default:
7036 return ERROR_MARK;
7040 /* Parse a new-expression.
7042 new-expression:
7043 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7044 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7046 Returns a representation of the expression. */
7048 static tree
7049 cp_parser_new_expression (cp_parser* parser)
7051 bool global_scope_p;
7052 vec<tree, va_gc> *placement;
7053 tree type;
7054 vec<tree, va_gc> *initializer;
7055 tree nelts = NULL_TREE;
7056 tree ret;
7058 /* Look for the optional `::' operator. */
7059 global_scope_p
7060 = (cp_parser_global_scope_opt (parser,
7061 /*current_scope_valid_p=*/false)
7062 != NULL_TREE);
7063 /* Look for the `new' operator. */
7064 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7065 /* There's no easy way to tell a new-placement from the
7066 `( type-id )' construct. */
7067 cp_parser_parse_tentatively (parser);
7068 /* Look for a new-placement. */
7069 placement = cp_parser_new_placement (parser);
7070 /* If that didn't work out, there's no new-placement. */
7071 if (!cp_parser_parse_definitely (parser))
7073 if (placement != NULL)
7074 release_tree_vector (placement);
7075 placement = NULL;
7078 /* If the next token is a `(', then we have a parenthesized
7079 type-id. */
7080 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7082 cp_token *token;
7083 const char *saved_message = parser->type_definition_forbidden_message;
7085 /* Consume the `('. */
7086 cp_lexer_consume_token (parser->lexer);
7088 /* Parse the type-id. */
7089 parser->type_definition_forbidden_message
7090 = G_("types may not be defined in a new-expression");
7091 type = cp_parser_type_id (parser);
7092 parser->type_definition_forbidden_message = saved_message;
7094 /* Look for the closing `)'. */
7095 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7096 token = cp_lexer_peek_token (parser->lexer);
7097 /* There should not be a direct-new-declarator in this production,
7098 but GCC used to allowed this, so we check and emit a sensible error
7099 message for this case. */
7100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7102 error_at (token->location,
7103 "array bound forbidden after parenthesized type-id");
7104 inform (token->location,
7105 "try removing the parentheses around the type-id");
7106 cp_parser_direct_new_declarator (parser);
7109 /* Otherwise, there must be a new-type-id. */
7110 else
7111 type = cp_parser_new_type_id (parser, &nelts);
7113 /* If the next token is a `(' or '{', then we have a new-initializer. */
7114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7115 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7116 initializer = cp_parser_new_initializer (parser);
7117 else
7118 initializer = NULL;
7120 /* A new-expression may not appear in an integral constant
7121 expression. */
7122 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7123 ret = error_mark_node;
7124 else
7126 /* Create a representation of the new-expression. */
7127 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7128 tf_warning_or_error);
7131 if (placement != NULL)
7132 release_tree_vector (placement);
7133 if (initializer != NULL)
7134 release_tree_vector (initializer);
7136 return ret;
7139 /* Parse a new-placement.
7141 new-placement:
7142 ( expression-list )
7144 Returns the same representation as for an expression-list. */
7146 static vec<tree, va_gc> *
7147 cp_parser_new_placement (cp_parser* parser)
7149 vec<tree, va_gc> *expression_list;
7151 /* Parse the expression-list. */
7152 expression_list = (cp_parser_parenthesized_expression_list
7153 (parser, non_attr, /*cast_p=*/false,
7154 /*allow_expansion_p=*/true,
7155 /*non_constant_p=*/NULL));
7157 return expression_list;
7160 /* Parse a new-type-id.
7162 new-type-id:
7163 type-specifier-seq new-declarator [opt]
7165 Returns the TYPE allocated. If the new-type-id indicates an array
7166 type, *NELTS is set to the number of elements in the last array
7167 bound; the TYPE will not include the last array bound. */
7169 static tree
7170 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7172 cp_decl_specifier_seq type_specifier_seq;
7173 cp_declarator *new_declarator;
7174 cp_declarator *declarator;
7175 cp_declarator *outer_declarator;
7176 const char *saved_message;
7178 /* The type-specifier sequence must not contain type definitions.
7179 (It cannot contain declarations of new types either, but if they
7180 are not definitions we will catch that because they are not
7181 complete.) */
7182 saved_message = parser->type_definition_forbidden_message;
7183 parser->type_definition_forbidden_message
7184 = G_("types may not be defined in a new-type-id");
7185 /* Parse the type-specifier-seq. */
7186 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7187 /*is_trailing_return=*/false,
7188 &type_specifier_seq);
7189 /* Restore the old message. */
7190 parser->type_definition_forbidden_message = saved_message;
7192 if (type_specifier_seq.type == error_mark_node)
7193 return error_mark_node;
7195 /* Parse the new-declarator. */
7196 new_declarator = cp_parser_new_declarator_opt (parser);
7198 /* Determine the number of elements in the last array dimension, if
7199 any. */
7200 *nelts = NULL_TREE;
7201 /* Skip down to the last array dimension. */
7202 declarator = new_declarator;
7203 outer_declarator = NULL;
7204 while (declarator && (declarator->kind == cdk_pointer
7205 || declarator->kind == cdk_ptrmem))
7207 outer_declarator = declarator;
7208 declarator = declarator->declarator;
7210 while (declarator
7211 && declarator->kind == cdk_array
7212 && declarator->declarator
7213 && declarator->declarator->kind == cdk_array)
7215 outer_declarator = declarator;
7216 declarator = declarator->declarator;
7219 if (declarator && declarator->kind == cdk_array)
7221 *nelts = declarator->u.array.bounds;
7222 if (*nelts == error_mark_node)
7223 *nelts = integer_one_node;
7225 if (outer_declarator)
7226 outer_declarator->declarator = declarator->declarator;
7227 else
7228 new_declarator = NULL;
7231 return groktypename (&type_specifier_seq, new_declarator, false);
7234 /* Parse an (optional) new-declarator.
7236 new-declarator:
7237 ptr-operator new-declarator [opt]
7238 direct-new-declarator
7240 Returns the declarator. */
7242 static cp_declarator *
7243 cp_parser_new_declarator_opt (cp_parser* parser)
7245 enum tree_code code;
7246 tree type, std_attributes = NULL_TREE;
7247 cp_cv_quals cv_quals;
7249 /* We don't know if there's a ptr-operator next, or not. */
7250 cp_parser_parse_tentatively (parser);
7251 /* Look for a ptr-operator. */
7252 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7253 /* If that worked, look for more new-declarators. */
7254 if (cp_parser_parse_definitely (parser))
7256 cp_declarator *declarator;
7258 /* Parse another optional declarator. */
7259 declarator = cp_parser_new_declarator_opt (parser);
7261 declarator = cp_parser_make_indirect_declarator
7262 (code, type, cv_quals, declarator, std_attributes);
7264 return declarator;
7267 /* If the next token is a `[', there is a direct-new-declarator. */
7268 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7269 return cp_parser_direct_new_declarator (parser);
7271 return NULL;
7274 /* Parse a direct-new-declarator.
7276 direct-new-declarator:
7277 [ expression ]
7278 direct-new-declarator [constant-expression]
7282 static cp_declarator *
7283 cp_parser_direct_new_declarator (cp_parser* parser)
7285 cp_declarator *declarator = NULL;
7287 while (true)
7289 tree expression;
7290 cp_token *token;
7292 /* Look for the opening `['. */
7293 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7295 token = cp_lexer_peek_token (parser->lexer);
7296 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7297 /* The standard requires that the expression have integral
7298 type. DR 74 adds enumeration types. We believe that the
7299 real intent is that these expressions be handled like the
7300 expression in a `switch' condition, which also allows
7301 classes with a single conversion to integral or
7302 enumeration type. */
7303 if (!processing_template_decl)
7305 expression
7306 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7307 expression,
7308 /*complain=*/true);
7309 if (!expression)
7311 error_at (token->location,
7312 "expression in new-declarator must have integral "
7313 "or enumeration type");
7314 expression = error_mark_node;
7318 /* Look for the closing `]'. */
7319 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7321 /* Add this bound to the declarator. */
7322 declarator = make_array_declarator (declarator, expression);
7324 /* If the next token is not a `[', then there are no more
7325 bounds. */
7326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7327 break;
7330 return declarator;
7333 /* Parse a new-initializer.
7335 new-initializer:
7336 ( expression-list [opt] )
7337 braced-init-list
7339 Returns a representation of the expression-list. */
7341 static vec<tree, va_gc> *
7342 cp_parser_new_initializer (cp_parser* parser)
7344 vec<tree, va_gc> *expression_list;
7346 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7348 tree t;
7349 bool expr_non_constant_p;
7350 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7351 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7352 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7353 expression_list = make_tree_vector_single (t);
7355 else
7356 expression_list = (cp_parser_parenthesized_expression_list
7357 (parser, non_attr, /*cast_p=*/false,
7358 /*allow_expansion_p=*/true,
7359 /*non_constant_p=*/NULL));
7361 return expression_list;
7364 /* Parse a delete-expression.
7366 delete-expression:
7367 :: [opt] delete cast-expression
7368 :: [opt] delete [ ] cast-expression
7370 Returns a representation of the expression. */
7372 static tree
7373 cp_parser_delete_expression (cp_parser* parser)
7375 bool global_scope_p;
7376 bool array_p;
7377 tree expression;
7379 /* Look for the optional `::' operator. */
7380 global_scope_p
7381 = (cp_parser_global_scope_opt (parser,
7382 /*current_scope_valid_p=*/false)
7383 != NULL_TREE);
7384 /* Look for the `delete' keyword. */
7385 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7386 /* See if the array syntax is in use. */
7387 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7389 /* Consume the `[' token. */
7390 cp_lexer_consume_token (parser->lexer);
7391 /* Look for the `]' token. */
7392 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7393 /* Remember that this is the `[]' construct. */
7394 array_p = true;
7396 else
7397 array_p = false;
7399 /* Parse the cast-expression. */
7400 expression = cp_parser_simple_cast_expression (parser);
7402 /* A delete-expression may not appear in an integral constant
7403 expression. */
7404 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7405 return error_mark_node;
7407 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7408 tf_warning_or_error);
7411 /* Returns true if TOKEN may start a cast-expression and false
7412 otherwise. */
7414 static bool
7415 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7417 cp_token *token = cp_lexer_peek_token (parser->lexer);
7418 switch (token->type)
7420 case CPP_COMMA:
7421 case CPP_SEMICOLON:
7422 case CPP_QUERY:
7423 case CPP_COLON:
7424 case CPP_CLOSE_SQUARE:
7425 case CPP_CLOSE_PAREN:
7426 case CPP_CLOSE_BRACE:
7427 case CPP_DOT:
7428 case CPP_DOT_STAR:
7429 case CPP_DEREF:
7430 case CPP_DEREF_STAR:
7431 case CPP_DIV:
7432 case CPP_MOD:
7433 case CPP_LSHIFT:
7434 case CPP_RSHIFT:
7435 case CPP_LESS:
7436 case CPP_GREATER:
7437 case CPP_LESS_EQ:
7438 case CPP_GREATER_EQ:
7439 case CPP_EQ_EQ:
7440 case CPP_NOT_EQ:
7441 case CPP_EQ:
7442 case CPP_MULT_EQ:
7443 case CPP_DIV_EQ:
7444 case CPP_MOD_EQ:
7445 case CPP_PLUS_EQ:
7446 case CPP_MINUS_EQ:
7447 case CPP_RSHIFT_EQ:
7448 case CPP_LSHIFT_EQ:
7449 case CPP_AND_EQ:
7450 case CPP_XOR_EQ:
7451 case CPP_OR_EQ:
7452 case CPP_XOR:
7453 case CPP_OR:
7454 case CPP_OR_OR:
7455 case CPP_EOF:
7456 return false;
7458 case CPP_OPEN_PAREN:
7459 /* In ((type ()) () the last () isn't a valid cast-expression,
7460 so the whole must be parsed as postfix-expression. */
7461 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7462 != CPP_CLOSE_PAREN;
7464 /* '[' may start a primary-expression in obj-c++. */
7465 case CPP_OPEN_SQUARE:
7466 return c_dialect_objc ();
7468 default:
7469 return true;
7473 /* Parse a cast-expression.
7475 cast-expression:
7476 unary-expression
7477 ( type-id ) cast-expression
7479 ADDRESS_P is true iff the unary-expression is appearing as the
7480 operand of the `&' operator. CAST_P is true if this expression is
7481 the target of a cast.
7483 Returns a representation of the expression. */
7485 static tree
7486 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7487 bool decltype_p, cp_id_kind * pidk)
7489 /* If it's a `(', then we might be looking at a cast. */
7490 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7492 tree type = NULL_TREE;
7493 tree expr = NULL_TREE;
7494 bool compound_literal_p;
7495 const char *saved_message;
7497 /* There's no way to know yet whether or not this is a cast.
7498 For example, `(int (3))' is a unary-expression, while `(int)
7499 3' is a cast. So, we resort to parsing tentatively. */
7500 cp_parser_parse_tentatively (parser);
7501 /* Types may not be defined in a cast. */
7502 saved_message = parser->type_definition_forbidden_message;
7503 parser->type_definition_forbidden_message
7504 = G_("types may not be defined in casts");
7505 /* Consume the `('. */
7506 cp_lexer_consume_token (parser->lexer);
7507 /* A very tricky bit is that `(struct S) { 3 }' is a
7508 compound-literal (which we permit in C++ as an extension).
7509 But, that construct is not a cast-expression -- it is a
7510 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7511 is legal; if the compound-literal were a cast-expression,
7512 you'd need an extra set of parentheses.) But, if we parse
7513 the type-id, and it happens to be a class-specifier, then we
7514 will commit to the parse at that point, because we cannot
7515 undo the action that is done when creating a new class. So,
7516 then we cannot back up and do a postfix-expression.
7518 Therefore, we scan ahead to the closing `)', and check to see
7519 if the token after the `)' is a `{'. If so, we are not
7520 looking at a cast-expression.
7522 Save tokens so that we can put them back. */
7523 cp_lexer_save_tokens (parser->lexer);
7524 /* Skip tokens until the next token is a closing parenthesis.
7525 If we find the closing `)', and the next token is a `{', then
7526 we are looking at a compound-literal. */
7527 compound_literal_p
7528 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7529 /*consume_paren=*/true)
7530 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7531 /* Roll back the tokens we skipped. */
7532 cp_lexer_rollback_tokens (parser->lexer);
7533 /* If we were looking at a compound-literal, simulate an error
7534 so that the call to cp_parser_parse_definitely below will
7535 fail. */
7536 if (compound_literal_p)
7537 cp_parser_simulate_error (parser);
7538 else
7540 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7541 parser->in_type_id_in_expr_p = true;
7542 /* Look for the type-id. */
7543 type = cp_parser_type_id (parser);
7544 /* Look for the closing `)'. */
7545 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7546 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7549 /* Restore the saved message. */
7550 parser->type_definition_forbidden_message = saved_message;
7552 /* At this point this can only be either a cast or a
7553 parenthesized ctor such as `(T ())' that looks like a cast to
7554 function returning T. */
7555 if (!cp_parser_error_occurred (parser)
7556 && cp_parser_tokens_start_cast_expression (parser))
7558 cp_parser_parse_definitely (parser);
7559 expr = cp_parser_cast_expression (parser,
7560 /*address_p=*/false,
7561 /*cast_p=*/true,
7562 /*decltype_p=*/false,
7563 pidk);
7565 /* Warn about old-style casts, if so requested. */
7566 if (warn_old_style_cast
7567 && !in_system_header
7568 && !VOID_TYPE_P (type)
7569 && current_lang_name != lang_name_c)
7570 warning (OPT_Wold_style_cast, "use of old-style cast");
7572 /* Only type conversions to integral or enumeration types
7573 can be used in constant-expressions. */
7574 if (!cast_valid_in_integral_constant_expression_p (type)
7575 && cp_parser_non_integral_constant_expression (parser,
7576 NIC_CAST))
7577 return error_mark_node;
7579 /* Perform the cast. */
7580 expr = build_c_cast (input_location, type, expr);
7581 return expr;
7583 else
7584 cp_parser_abort_tentative_parse (parser);
7587 /* If we get here, then it's not a cast, so it must be a
7588 unary-expression. */
7589 return cp_parser_unary_expression (parser, address_p, cast_p,
7590 decltype_p, pidk);
7593 /* Parse a binary expression of the general form:
7595 pm-expression:
7596 cast-expression
7597 pm-expression .* cast-expression
7598 pm-expression ->* cast-expression
7600 multiplicative-expression:
7601 pm-expression
7602 multiplicative-expression * pm-expression
7603 multiplicative-expression / pm-expression
7604 multiplicative-expression % pm-expression
7606 additive-expression:
7607 multiplicative-expression
7608 additive-expression + multiplicative-expression
7609 additive-expression - multiplicative-expression
7611 shift-expression:
7612 additive-expression
7613 shift-expression << additive-expression
7614 shift-expression >> additive-expression
7616 relational-expression:
7617 shift-expression
7618 relational-expression < shift-expression
7619 relational-expression > shift-expression
7620 relational-expression <= shift-expression
7621 relational-expression >= shift-expression
7623 GNU Extension:
7625 relational-expression:
7626 relational-expression <? shift-expression
7627 relational-expression >? shift-expression
7629 equality-expression:
7630 relational-expression
7631 equality-expression == relational-expression
7632 equality-expression != relational-expression
7634 and-expression:
7635 equality-expression
7636 and-expression & equality-expression
7638 exclusive-or-expression:
7639 and-expression
7640 exclusive-or-expression ^ and-expression
7642 inclusive-or-expression:
7643 exclusive-or-expression
7644 inclusive-or-expression | exclusive-or-expression
7646 logical-and-expression:
7647 inclusive-or-expression
7648 logical-and-expression && inclusive-or-expression
7650 logical-or-expression:
7651 logical-and-expression
7652 logical-or-expression || logical-and-expression
7654 All these are implemented with a single function like:
7656 binary-expression:
7657 simple-cast-expression
7658 binary-expression <token> binary-expression
7660 CAST_P is true if this expression is the target of a cast.
7662 The binops_by_token map is used to get the tree codes for each <token> type.
7663 binary-expressions are associated according to a precedence table. */
7665 #define TOKEN_PRECEDENCE(token) \
7666 (((token->type == CPP_GREATER \
7667 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7668 && !parser->greater_than_is_operator_p) \
7669 ? PREC_NOT_OPERATOR \
7670 : binops_by_token[token->type].prec)
7672 static tree
7673 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7674 bool no_toplevel_fold_p,
7675 bool decltype_p,
7676 enum cp_parser_prec prec,
7677 cp_id_kind * pidk)
7679 cp_parser_expression_stack stack;
7680 cp_parser_expression_stack_entry *sp = &stack[0];
7681 cp_parser_expression_stack_entry current;
7682 tree rhs;
7683 cp_token *token;
7684 enum tree_code rhs_type;
7685 enum cp_parser_prec new_prec, lookahead_prec;
7686 tree overload;
7688 /* Parse the first expression. */
7689 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7690 cast_p, decltype_p, pidk);
7691 current.lhs_type = ERROR_MARK;
7692 current.prec = prec;
7694 if (cp_parser_error_occurred (parser))
7695 return error_mark_node;
7697 for (;;)
7699 /* Get an operator token. */
7700 token = cp_lexer_peek_token (parser->lexer);
7702 if (warn_cxx0x_compat
7703 && token->type == CPP_RSHIFT
7704 && !parser->greater_than_is_operator_p)
7706 if (warning_at (token->location, OPT_Wc__0x_compat,
7707 "%<>>%> operator is treated"
7708 " as two right angle brackets in C++11"))
7709 inform (token->location,
7710 "suggest parentheses around %<>>%> expression");
7713 new_prec = TOKEN_PRECEDENCE (token);
7715 /* Popping an entry off the stack means we completed a subexpression:
7716 - either we found a token which is not an operator (`>' where it is not
7717 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7718 will happen repeatedly;
7719 - or, we found an operator which has lower priority. This is the case
7720 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7721 parsing `3 * 4'. */
7722 if (new_prec <= current.prec)
7724 if (sp == stack)
7725 break;
7726 else
7727 goto pop;
7730 get_rhs:
7731 current.tree_type = binops_by_token[token->type].tree_type;
7732 current.loc = token->location;
7734 /* We used the operator token. */
7735 cp_lexer_consume_token (parser->lexer);
7737 /* For "false && x" or "true || x", x will never be executed;
7738 disable warnings while evaluating it. */
7739 if (current.tree_type == TRUTH_ANDIF_EXPR)
7740 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7741 else if (current.tree_type == TRUTH_ORIF_EXPR)
7742 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7744 /* Extract another operand. It may be the RHS of this expression
7745 or the LHS of a new, higher priority expression. */
7746 rhs = cp_parser_simple_cast_expression (parser);
7747 rhs_type = ERROR_MARK;
7749 /* Get another operator token. Look up its precedence to avoid
7750 building a useless (immediately popped) stack entry for common
7751 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7752 token = cp_lexer_peek_token (parser->lexer);
7753 lookahead_prec = TOKEN_PRECEDENCE (token);
7754 if (lookahead_prec > new_prec)
7756 /* ... and prepare to parse the RHS of the new, higher priority
7757 expression. Since precedence levels on the stack are
7758 monotonically increasing, we do not have to care about
7759 stack overflows. */
7760 *sp = current;
7761 ++sp;
7762 current.lhs = rhs;
7763 current.lhs_type = rhs_type;
7764 current.prec = new_prec;
7765 new_prec = lookahead_prec;
7766 goto get_rhs;
7768 pop:
7769 lookahead_prec = new_prec;
7770 /* If the stack is not empty, we have parsed into LHS the right side
7771 (`4' in the example above) of an expression we had suspended.
7772 We can use the information on the stack to recover the LHS (`3')
7773 from the stack together with the tree code (`MULT_EXPR'), and
7774 the precedence of the higher level subexpression
7775 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7776 which will be used to actually build the additive expression. */
7777 rhs = current.lhs;
7778 rhs_type = current.lhs_type;
7779 --sp;
7780 current = *sp;
7783 /* Undo the disabling of warnings done above. */
7784 if (current.tree_type == TRUTH_ANDIF_EXPR)
7785 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7786 else if (current.tree_type == TRUTH_ORIF_EXPR)
7787 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7789 overload = NULL;
7790 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7791 ERROR_MARK for everything that is not a binary expression.
7792 This makes warn_about_parentheses miss some warnings that
7793 involve unary operators. For unary expressions we should
7794 pass the correct tree_code unless the unary expression was
7795 surrounded by parentheses.
7797 if (no_toplevel_fold_p
7798 && lookahead_prec <= current.prec
7799 && sp == stack
7800 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7801 current.lhs = build2 (current.tree_type, boolean_type_node,
7802 current.lhs, rhs);
7803 else
7804 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7805 current.lhs, current.lhs_type,
7806 rhs, rhs_type, &overload,
7807 complain_flags (decltype_p));
7808 current.lhs_type = current.tree_type;
7809 if (EXPR_P (current.lhs))
7810 SET_EXPR_LOCATION (current.lhs, current.loc);
7812 /* If the binary operator required the use of an overloaded operator,
7813 then this expression cannot be an integral constant-expression.
7814 An overloaded operator can be used even if both operands are
7815 otherwise permissible in an integral constant-expression if at
7816 least one of the operands is of enumeration type. */
7818 if (overload
7819 && cp_parser_non_integral_constant_expression (parser,
7820 NIC_OVERLOADED))
7821 return error_mark_node;
7824 return current.lhs;
7827 static tree
7828 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7829 bool no_toplevel_fold_p,
7830 enum cp_parser_prec prec,
7831 cp_id_kind * pidk)
7833 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
7834 /*decltype*/false, prec, pidk);
7837 /* Parse the `? expression : assignment-expression' part of a
7838 conditional-expression. The LOGICAL_OR_EXPR is the
7839 logical-or-expression that started the conditional-expression.
7840 Returns a representation of the entire conditional-expression.
7842 This routine is used by cp_parser_assignment_expression.
7844 ? expression : assignment-expression
7846 GNU Extensions:
7848 ? : assignment-expression */
7850 static tree
7851 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7853 tree expr;
7854 tree assignment_expr;
7855 struct cp_token *token;
7856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7858 /* Consume the `?' token. */
7859 cp_lexer_consume_token (parser->lexer);
7860 token = cp_lexer_peek_token (parser->lexer);
7861 if (cp_parser_allow_gnu_extensions_p (parser)
7862 && token->type == CPP_COLON)
7864 pedwarn (token->location, OPT_Wpedantic,
7865 "ISO C++ does not allow ?: with omitted middle operand");
7866 /* Implicit true clause. */
7867 expr = NULL_TREE;
7868 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7869 warn_for_omitted_condop (token->location, logical_or_expr);
7871 else
7873 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7874 parser->colon_corrects_to_scope_p = false;
7875 /* Parse the expression. */
7876 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7877 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7878 c_inhibit_evaluation_warnings +=
7879 ((logical_or_expr == truthvalue_true_node)
7880 - (logical_or_expr == truthvalue_false_node));
7881 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7884 /* The next token should be a `:'. */
7885 cp_parser_require (parser, CPP_COLON, RT_COLON);
7886 /* Parse the assignment-expression. */
7887 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7888 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7890 /* Build the conditional-expression. */
7891 return build_x_conditional_expr (loc, logical_or_expr,
7892 expr,
7893 assignment_expr,
7894 tf_warning_or_error);
7897 /* Parse an assignment-expression.
7899 assignment-expression:
7900 conditional-expression
7901 logical-or-expression assignment-operator assignment_expression
7902 throw-expression
7904 CAST_P is true if this expression is the target of a cast.
7905 DECLTYPE_P is true if this expression is the operand of decltype.
7907 Returns a representation for the expression. */
7909 static tree
7910 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7911 bool decltype_p, cp_id_kind * pidk)
7913 tree expr;
7915 /* If the next token is the `throw' keyword, then we're looking at
7916 a throw-expression. */
7917 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7918 expr = cp_parser_throw_expression (parser);
7919 /* Otherwise, it must be that we are looking at a
7920 logical-or-expression. */
7921 else
7923 /* Parse the binary expressions (logical-or-expression). */
7924 expr = cp_parser_binary_expression (parser, cast_p, false,
7925 decltype_p,
7926 PREC_NOT_OPERATOR, pidk);
7927 /* If the next token is a `?' then we're actually looking at a
7928 conditional-expression. */
7929 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7930 return cp_parser_question_colon_clause (parser, expr);
7931 else
7933 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7935 /* If it's an assignment-operator, we're using the second
7936 production. */
7937 enum tree_code assignment_operator
7938 = cp_parser_assignment_operator_opt (parser);
7939 if (assignment_operator != ERROR_MARK)
7941 bool non_constant_p;
7942 location_t saved_input_location;
7944 /* Parse the right-hand side of the assignment. */
7945 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7947 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7948 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7950 /* An assignment may not appear in a
7951 constant-expression. */
7952 if (cp_parser_non_integral_constant_expression (parser,
7953 NIC_ASSIGNMENT))
7954 return error_mark_node;
7955 /* Build the assignment expression. Its default
7956 location is the location of the '=' token. */
7957 saved_input_location = input_location;
7958 input_location = loc;
7959 expr = build_x_modify_expr (loc, expr,
7960 assignment_operator,
7961 rhs,
7962 complain_flags (decltype_p));
7963 input_location = saved_input_location;
7968 return expr;
7971 static tree
7972 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7973 cp_id_kind * pidk)
7975 return cp_parser_assignment_expression (parser, cast_p,
7976 /*decltype*/false, pidk);
7979 /* Parse an (optional) assignment-operator.
7981 assignment-operator: one of
7982 = *= /= %= += -= >>= <<= &= ^= |=
7984 GNU Extension:
7986 assignment-operator: one of
7987 <?= >?=
7989 If the next token is an assignment operator, the corresponding tree
7990 code is returned, and the token is consumed. For example, for
7991 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7992 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7993 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7994 operator, ERROR_MARK is returned. */
7996 static enum tree_code
7997 cp_parser_assignment_operator_opt (cp_parser* parser)
7999 enum tree_code op;
8000 cp_token *token;
8002 /* Peek at the next token. */
8003 token = cp_lexer_peek_token (parser->lexer);
8005 switch (token->type)
8007 case CPP_EQ:
8008 op = NOP_EXPR;
8009 break;
8011 case CPP_MULT_EQ:
8012 op = MULT_EXPR;
8013 break;
8015 case CPP_DIV_EQ:
8016 op = TRUNC_DIV_EXPR;
8017 break;
8019 case CPP_MOD_EQ:
8020 op = TRUNC_MOD_EXPR;
8021 break;
8023 case CPP_PLUS_EQ:
8024 op = PLUS_EXPR;
8025 break;
8027 case CPP_MINUS_EQ:
8028 op = MINUS_EXPR;
8029 break;
8031 case CPP_RSHIFT_EQ:
8032 op = RSHIFT_EXPR;
8033 break;
8035 case CPP_LSHIFT_EQ:
8036 op = LSHIFT_EXPR;
8037 break;
8039 case CPP_AND_EQ:
8040 op = BIT_AND_EXPR;
8041 break;
8043 case CPP_XOR_EQ:
8044 op = BIT_XOR_EXPR;
8045 break;
8047 case CPP_OR_EQ:
8048 op = BIT_IOR_EXPR;
8049 break;
8051 default:
8052 /* Nothing else is an assignment operator. */
8053 op = ERROR_MARK;
8056 /* If it was an assignment operator, consume it. */
8057 if (op != ERROR_MARK)
8058 cp_lexer_consume_token (parser->lexer);
8060 return op;
8063 /* Parse an expression.
8065 expression:
8066 assignment-expression
8067 expression , assignment-expression
8069 CAST_P is true if this expression is the target of a cast.
8070 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8071 except possibly parenthesized or on the RHS of a comma (N3276).
8073 Returns a representation of the expression. */
8075 static tree
8076 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8077 cp_id_kind * pidk)
8079 tree expression = NULL_TREE;
8080 location_t loc = UNKNOWN_LOCATION;
8082 while (true)
8084 tree assignment_expression;
8086 /* Parse the next assignment-expression. */
8087 assignment_expression
8088 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8090 /* We don't create a temporary for a call that is the immediate operand
8091 of decltype or on the RHS of a comma. But when we see a comma, we
8092 need to create a temporary for a call on the LHS. */
8093 if (decltype_p && !processing_template_decl
8094 && TREE_CODE (assignment_expression) == CALL_EXPR
8095 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8096 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8097 assignment_expression
8098 = build_cplus_new (TREE_TYPE (assignment_expression),
8099 assignment_expression, tf_warning_or_error);
8101 /* If this is the first assignment-expression, we can just
8102 save it away. */
8103 if (!expression)
8104 expression = assignment_expression;
8105 else
8106 expression = build_x_compound_expr (loc, expression,
8107 assignment_expression,
8108 complain_flags (decltype_p));
8109 /* If the next token is not a comma, then we are done with the
8110 expression. */
8111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8112 break;
8113 /* Consume the `,'. */
8114 loc = cp_lexer_peek_token (parser->lexer)->location;
8115 cp_lexer_consume_token (parser->lexer);
8116 /* A comma operator cannot appear in a constant-expression. */
8117 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8118 expression = error_mark_node;
8121 return expression;
8124 static inline tree
8125 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8127 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8130 /* Parse a constant-expression.
8132 constant-expression:
8133 conditional-expression
8135 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8136 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8137 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8138 is false, NON_CONSTANT_P should be NULL. */
8140 static tree
8141 cp_parser_constant_expression (cp_parser* parser,
8142 bool allow_non_constant_p,
8143 bool *non_constant_p)
8145 bool saved_integral_constant_expression_p;
8146 bool saved_allow_non_integral_constant_expression_p;
8147 bool saved_non_integral_constant_expression_p;
8148 tree expression;
8150 /* It might seem that we could simply parse the
8151 conditional-expression, and then check to see if it were
8152 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8153 one that the compiler can figure out is constant, possibly after
8154 doing some simplifications or optimizations. The standard has a
8155 precise definition of constant-expression, and we must honor
8156 that, even though it is somewhat more restrictive.
8158 For example:
8160 int i[(2, 3)];
8162 is not a legal declaration, because `(2, 3)' is not a
8163 constant-expression. The `,' operator is forbidden in a
8164 constant-expression. However, GCC's constant-folding machinery
8165 will fold this operation to an INTEGER_CST for `3'. */
8167 /* Save the old settings. */
8168 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8169 saved_allow_non_integral_constant_expression_p
8170 = parser->allow_non_integral_constant_expression_p;
8171 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8172 /* We are now parsing a constant-expression. */
8173 parser->integral_constant_expression_p = true;
8174 parser->allow_non_integral_constant_expression_p
8175 = (allow_non_constant_p || cxx_dialect >= cxx11);
8176 parser->non_integral_constant_expression_p = false;
8177 /* Although the grammar says "conditional-expression", we parse an
8178 "assignment-expression", which also permits "throw-expression"
8179 and the use of assignment operators. In the case that
8180 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8181 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8182 actually essential that we look for an assignment-expression.
8183 For example, cp_parser_initializer_clauses uses this function to
8184 determine whether a particular assignment-expression is in fact
8185 constant. */
8186 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8187 /* Restore the old settings. */
8188 parser->integral_constant_expression_p
8189 = saved_integral_constant_expression_p;
8190 parser->allow_non_integral_constant_expression_p
8191 = saved_allow_non_integral_constant_expression_p;
8192 if (cxx_dialect >= cxx11)
8194 /* Require an rvalue constant expression here; that's what our
8195 callers expect. Reference constant expressions are handled
8196 separately in e.g. cp_parser_template_argument. */
8197 bool is_const = potential_rvalue_constant_expression (expression);
8198 parser->non_integral_constant_expression_p = !is_const;
8199 if (!is_const && !allow_non_constant_p)
8200 require_potential_rvalue_constant_expression (expression);
8202 if (allow_non_constant_p)
8203 *non_constant_p = parser->non_integral_constant_expression_p;
8204 parser->non_integral_constant_expression_p
8205 = saved_non_integral_constant_expression_p;
8207 return expression;
8210 /* Parse __builtin_offsetof.
8212 offsetof-expression:
8213 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8215 offsetof-member-designator:
8216 id-expression
8217 | offsetof-member-designator "." id-expression
8218 | offsetof-member-designator "[" expression "]"
8219 | offsetof-member-designator "->" id-expression */
8221 static tree
8222 cp_parser_builtin_offsetof (cp_parser *parser)
8224 int save_ice_p, save_non_ice_p;
8225 tree type, expr;
8226 cp_id_kind dummy;
8227 cp_token *token;
8229 /* We're about to accept non-integral-constant things, but will
8230 definitely yield an integral constant expression. Save and
8231 restore these values around our local parsing. */
8232 save_ice_p = parser->integral_constant_expression_p;
8233 save_non_ice_p = parser->non_integral_constant_expression_p;
8235 /* Consume the "__builtin_offsetof" token. */
8236 cp_lexer_consume_token (parser->lexer);
8237 /* Consume the opening `('. */
8238 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8239 /* Parse the type-id. */
8240 type = cp_parser_type_id (parser);
8241 /* Look for the `,'. */
8242 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8243 token = cp_lexer_peek_token (parser->lexer);
8245 /* Build the (type *)null that begins the traditional offsetof macro. */
8246 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8247 tf_warning_or_error);
8249 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8250 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8251 true, &dummy, token->location);
8252 while (true)
8254 token = cp_lexer_peek_token (parser->lexer);
8255 switch (token->type)
8257 case CPP_OPEN_SQUARE:
8258 /* offsetof-member-designator "[" expression "]" */
8259 expr = cp_parser_postfix_open_square_expression (parser, expr,
8260 true, false);
8261 break;
8263 case CPP_DEREF:
8264 /* offsetof-member-designator "->" identifier */
8265 expr = grok_array_decl (token->location, expr,
8266 integer_zero_node, false);
8267 /* FALLTHRU */
8269 case CPP_DOT:
8270 /* offsetof-member-designator "." identifier */
8271 cp_lexer_consume_token (parser->lexer);
8272 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8273 expr, true, &dummy,
8274 token->location);
8275 break;
8277 case CPP_CLOSE_PAREN:
8278 /* Consume the ")" token. */
8279 cp_lexer_consume_token (parser->lexer);
8280 goto success;
8282 default:
8283 /* Error. We know the following require will fail, but
8284 that gives the proper error message. */
8285 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8286 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8287 expr = error_mark_node;
8288 goto failure;
8292 success:
8293 /* If we're processing a template, we can't finish the semantics yet.
8294 Otherwise we can fold the entire expression now. */
8295 if (processing_template_decl)
8296 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8297 else
8298 expr = finish_offsetof (expr);
8300 failure:
8301 parser->integral_constant_expression_p = save_ice_p;
8302 parser->non_integral_constant_expression_p = save_non_ice_p;
8304 return expr;
8307 /* Parse a trait expression.
8309 Returns a representation of the expression, the underlying type
8310 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8312 static tree
8313 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8315 cp_trait_kind kind;
8316 tree type1, type2 = NULL_TREE;
8317 bool binary = false;
8318 cp_decl_specifier_seq decl_specs;
8320 switch (keyword)
8322 case RID_HAS_NOTHROW_ASSIGN:
8323 kind = CPTK_HAS_NOTHROW_ASSIGN;
8324 break;
8325 case RID_HAS_NOTHROW_CONSTRUCTOR:
8326 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8327 break;
8328 case RID_HAS_NOTHROW_COPY:
8329 kind = CPTK_HAS_NOTHROW_COPY;
8330 break;
8331 case RID_HAS_TRIVIAL_ASSIGN:
8332 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8333 break;
8334 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8335 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8336 break;
8337 case RID_HAS_TRIVIAL_COPY:
8338 kind = CPTK_HAS_TRIVIAL_COPY;
8339 break;
8340 case RID_HAS_TRIVIAL_DESTRUCTOR:
8341 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8342 break;
8343 case RID_HAS_VIRTUAL_DESTRUCTOR:
8344 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8345 break;
8346 case RID_IS_ABSTRACT:
8347 kind = CPTK_IS_ABSTRACT;
8348 break;
8349 case RID_IS_BASE_OF:
8350 kind = CPTK_IS_BASE_OF;
8351 binary = true;
8352 break;
8353 case RID_IS_CLASS:
8354 kind = CPTK_IS_CLASS;
8355 break;
8356 case RID_IS_CONVERTIBLE_TO:
8357 kind = CPTK_IS_CONVERTIBLE_TO;
8358 binary = true;
8359 break;
8360 case RID_IS_EMPTY:
8361 kind = CPTK_IS_EMPTY;
8362 break;
8363 case RID_IS_ENUM:
8364 kind = CPTK_IS_ENUM;
8365 break;
8366 case RID_IS_FINAL:
8367 kind = CPTK_IS_FINAL;
8368 break;
8369 case RID_IS_LITERAL_TYPE:
8370 kind = CPTK_IS_LITERAL_TYPE;
8371 break;
8372 case RID_IS_POD:
8373 kind = CPTK_IS_POD;
8374 break;
8375 case RID_IS_POLYMORPHIC:
8376 kind = CPTK_IS_POLYMORPHIC;
8377 break;
8378 case RID_IS_STD_LAYOUT:
8379 kind = CPTK_IS_STD_LAYOUT;
8380 break;
8381 case RID_IS_TRIVIAL:
8382 kind = CPTK_IS_TRIVIAL;
8383 break;
8384 case RID_IS_UNION:
8385 kind = CPTK_IS_UNION;
8386 break;
8387 case RID_UNDERLYING_TYPE:
8388 kind = CPTK_UNDERLYING_TYPE;
8389 break;
8390 case RID_BASES:
8391 kind = CPTK_BASES;
8392 break;
8393 case RID_DIRECT_BASES:
8394 kind = CPTK_DIRECT_BASES;
8395 break;
8396 default:
8397 gcc_unreachable ();
8400 /* Consume the token. */
8401 cp_lexer_consume_token (parser->lexer);
8403 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8405 type1 = cp_parser_type_id (parser);
8407 if (type1 == error_mark_node)
8408 return error_mark_node;
8410 /* Build a trivial decl-specifier-seq. */
8411 clear_decl_specs (&decl_specs);
8412 decl_specs.type = type1;
8414 /* Call grokdeclarator to figure out what type this is. */
8415 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8416 /*initialized=*/0, /*attrlist=*/NULL);
8418 if (binary)
8420 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8422 type2 = cp_parser_type_id (parser);
8424 if (type2 == error_mark_node)
8425 return error_mark_node;
8427 /* Build a trivial decl-specifier-seq. */
8428 clear_decl_specs (&decl_specs);
8429 decl_specs.type = type2;
8431 /* Call grokdeclarator to figure out what type this is. */
8432 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8433 /*initialized=*/0, /*attrlist=*/NULL);
8436 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8438 /* Complete the trait expression, which may mean either processing
8439 the trait expr now or saving it for template instantiation. */
8440 switch(kind)
8442 case CPTK_UNDERLYING_TYPE:
8443 return finish_underlying_type (type1);
8444 case CPTK_BASES:
8445 return finish_bases (type1, false);
8446 case CPTK_DIRECT_BASES:
8447 return finish_bases (type1, true);
8448 default:
8449 return finish_trait_expr (kind, type1, type2);
8453 /* Lambdas that appear in variable initializer or default argument scope
8454 get that in their mangling, so we need to record it. We might as well
8455 use the count for function and namespace scopes as well. */
8456 static GTY(()) tree lambda_scope;
8457 static GTY(()) int lambda_count;
8458 typedef struct GTY(()) tree_int
8460 tree t;
8461 int i;
8462 } tree_int;
8463 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8465 static void
8466 start_lambda_scope (tree decl)
8468 tree_int ti;
8469 gcc_assert (decl);
8470 /* Once we're inside a function, we ignore other scopes and just push
8471 the function again so that popping works properly. */
8472 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8473 decl = current_function_decl;
8474 ti.t = lambda_scope;
8475 ti.i = lambda_count;
8476 vec_safe_push (lambda_scope_stack, ti);
8477 if (lambda_scope != decl)
8479 /* Don't reset the count if we're still in the same function. */
8480 lambda_scope = decl;
8481 lambda_count = 0;
8485 static void
8486 record_lambda_scope (tree lambda)
8488 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8489 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8492 static void
8493 finish_lambda_scope (void)
8495 tree_int *p = &lambda_scope_stack->last ();
8496 if (lambda_scope != p->t)
8498 lambda_scope = p->t;
8499 lambda_count = p->i;
8501 lambda_scope_stack->pop ();
8504 /* Parse a lambda expression.
8506 lambda-expression:
8507 lambda-introducer lambda-declarator [opt] compound-statement
8509 Returns a representation of the expression. */
8511 static tree
8512 cp_parser_lambda_expression (cp_parser* parser)
8514 tree lambda_expr = build_lambda_expr ();
8515 tree type;
8516 bool ok;
8518 LAMBDA_EXPR_LOCATION (lambda_expr)
8519 = cp_lexer_peek_token (parser->lexer)->location;
8521 if (cp_unevaluated_operand)
8522 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8523 "lambda-expression in unevaluated context");
8525 /* We may be in the middle of deferred access check. Disable
8526 it now. */
8527 push_deferring_access_checks (dk_no_deferred);
8529 cp_parser_lambda_introducer (parser, lambda_expr);
8531 type = begin_lambda_type (lambda_expr);
8532 if (type == error_mark_node)
8533 return error_mark_node;
8535 record_lambda_scope (lambda_expr);
8537 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8538 determine_visibility (TYPE_NAME (type));
8540 /* Now that we've started the type, add the capture fields for any
8541 explicit captures. */
8542 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8545 /* Inside the class, surrounding template-parameter-lists do not apply. */
8546 unsigned int saved_num_template_parameter_lists
8547 = parser->num_template_parameter_lists;
8548 unsigned char in_statement = parser->in_statement;
8549 bool in_switch_statement_p = parser->in_switch_statement_p;
8551 parser->num_template_parameter_lists = 0;
8552 parser->in_statement = 0;
8553 parser->in_switch_statement_p = false;
8555 /* By virtue of defining a local class, a lambda expression has access to
8556 the private variables of enclosing classes. */
8558 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8560 if (ok)
8561 cp_parser_lambda_body (parser, lambda_expr);
8562 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8563 cp_parser_skip_to_end_of_block_or_statement (parser);
8565 /* The capture list was built up in reverse order; fix that now. */
8566 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8567 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8569 if (ok)
8570 maybe_add_lambda_conv_op (type);
8572 type = finish_struct (type, /*attributes=*/NULL_TREE);
8574 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8575 parser->in_statement = in_statement;
8576 parser->in_switch_statement_p = in_switch_statement_p;
8579 pop_deferring_access_checks ();
8581 /* This field is only used during parsing of the lambda. */
8582 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8584 /* This lambda shouldn't have any proxies left at this point. */
8585 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8586 /* And now that we're done, push proxies for an enclosing lambda. */
8587 insert_pending_capture_proxies ();
8589 if (ok)
8590 return build_lambda_object (lambda_expr);
8591 else
8592 return error_mark_node;
8595 /* Parse the beginning of a lambda expression.
8597 lambda-introducer:
8598 [ lambda-capture [opt] ]
8600 LAMBDA_EXPR is the current representation of the lambda expression. */
8602 static void
8603 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8605 /* Need commas after the first capture. */
8606 bool first = true;
8608 /* Eat the leading `['. */
8609 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8611 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8612 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8613 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8614 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8615 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8616 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8618 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8620 cp_lexer_consume_token (parser->lexer);
8621 first = false;
8624 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8626 cp_token* capture_token;
8627 tree capture_id;
8628 tree capture_init_expr;
8629 cp_id_kind idk = CP_ID_KIND_NONE;
8630 bool explicit_init_p = false;
8632 enum capture_kind_type
8634 BY_COPY,
8635 BY_REFERENCE
8637 enum capture_kind_type capture_kind = BY_COPY;
8639 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8641 error ("expected end of capture-list");
8642 return;
8645 if (first)
8646 first = false;
8647 else
8648 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8650 /* Possibly capture `this'. */
8651 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8653 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8654 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8655 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8656 "with by-copy capture default");
8657 cp_lexer_consume_token (parser->lexer);
8658 add_capture (lambda_expr,
8659 /*id=*/this_identifier,
8660 /*initializer=*/finish_this_expr(),
8661 /*by_reference_p=*/false,
8662 explicit_init_p);
8663 continue;
8666 /* Remember whether we want to capture as a reference or not. */
8667 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8669 capture_kind = BY_REFERENCE;
8670 cp_lexer_consume_token (parser->lexer);
8673 /* Get the identifier. */
8674 capture_token = cp_lexer_peek_token (parser->lexer);
8675 capture_id = cp_parser_identifier (parser);
8677 if (capture_id == error_mark_node)
8678 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8679 delimiters, but I modified this to stop on unnested ']' as well. It
8680 was already changed to stop on unnested '}', so the
8681 "closing_parenthesis" name is no more misleading with my change. */
8683 cp_parser_skip_to_closing_parenthesis (parser,
8684 /*recovering=*/true,
8685 /*or_comma=*/true,
8686 /*consume_paren=*/true);
8687 break;
8690 /* Find the initializer for this capture. */
8691 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8692 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8693 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8695 bool direct, non_constant;
8696 /* An explicit initializer exists. */
8697 if (cxx_dialect < cxx1y)
8698 pedwarn (input_location, 0,
8699 "lambda capture initializers "
8700 "only available with -std=c++1y or -std=gnu++1y");
8701 capture_init_expr = cp_parser_initializer (parser, &direct,
8702 &non_constant);
8703 explicit_init_p = true;
8705 else
8707 const char* error_msg;
8709 /* Turn the identifier into an id-expression. */
8710 capture_init_expr
8711 = cp_parser_lookup_name
8712 (parser,
8713 capture_id,
8714 none_type,
8715 /*is_template=*/false,
8716 /*is_namespace=*/false,
8717 /*check_dependency=*/true,
8718 /*ambiguous_decls=*/NULL,
8719 capture_token->location);
8721 if (capture_init_expr == error_mark_node)
8723 unqualified_name_lookup_error (capture_id);
8724 continue;
8726 else if (DECL_P (capture_init_expr)
8727 && (!VAR_P (capture_init_expr)
8728 && TREE_CODE (capture_init_expr) != PARM_DECL))
8730 error_at (capture_token->location,
8731 "capture of non-variable %qD ",
8732 capture_init_expr);
8733 inform (0, "%q+#D declared here", capture_init_expr);
8734 continue;
8736 if (VAR_P (capture_init_expr)
8737 && decl_storage_duration (capture_init_expr) != dk_auto)
8739 pedwarn (capture_token->location, 0, "capture of variable "
8740 "%qD with non-automatic storage duration",
8741 capture_init_expr);
8742 inform (0, "%q+#D declared here", capture_init_expr);
8743 continue;
8746 capture_init_expr
8747 = finish_id_expression
8748 (capture_id,
8749 capture_init_expr,
8750 parser->scope,
8751 &idk,
8752 /*integral_constant_expression_p=*/false,
8753 /*allow_non_integral_constant_expression_p=*/false,
8754 /*non_integral_constant_expression_p=*/NULL,
8755 /*template_p=*/false,
8756 /*done=*/true,
8757 /*address_p=*/false,
8758 /*template_arg_p=*/false,
8759 &error_msg,
8760 capture_token->location);
8763 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8764 && !explicit_init_p)
8766 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8767 && capture_kind == BY_COPY)
8768 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8769 "of %qD redundant with by-copy capture default",
8770 capture_id);
8771 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8772 && capture_kind == BY_REFERENCE)
8773 pedwarn (capture_token->location, 0, "explicit by-reference "
8774 "capture of %qD redundant with by-reference capture "
8775 "default", capture_id);
8778 add_capture (lambda_expr,
8779 capture_id,
8780 capture_init_expr,
8781 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8782 explicit_init_p);
8785 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8788 /* Parse the (optional) middle of a lambda expression.
8790 lambda-declarator:
8791 ( parameter-declaration-clause [opt] )
8792 attribute-specifier [opt]
8793 mutable [opt]
8794 exception-specification [opt]
8795 lambda-return-type-clause [opt]
8797 LAMBDA_EXPR is the current representation of the lambda expression. */
8799 static bool
8800 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8802 /* 5.1.1.4 of the standard says:
8803 If a lambda-expression does not include a lambda-declarator, it is as if
8804 the lambda-declarator were ().
8805 This means an empty parameter list, no attributes, and no exception
8806 specification. */
8807 tree param_list = void_list_node;
8808 tree attributes = NULL_TREE;
8809 tree exception_spec = NULL_TREE;
8810 tree t;
8812 /* The lambda-declarator is optional, but must begin with an opening
8813 parenthesis if present. */
8814 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8816 cp_lexer_consume_token (parser->lexer);
8818 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8820 /* Parse parameters. */
8821 param_list = cp_parser_parameter_declaration_clause (parser);
8823 /* Default arguments shall not be specified in the
8824 parameter-declaration-clause of a lambda-declarator. */
8825 for (t = param_list; t; t = TREE_CHAIN (t))
8826 if (TREE_PURPOSE (t))
8827 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8828 "default argument specified for lambda parameter");
8830 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8832 attributes = cp_parser_attributes_opt (parser);
8834 /* Parse optional `mutable' keyword. */
8835 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8837 cp_lexer_consume_token (parser->lexer);
8838 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8841 /* Parse optional exception specification. */
8842 exception_spec = cp_parser_exception_specification_opt (parser);
8844 /* Parse optional trailing return type. */
8845 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8847 cp_lexer_consume_token (parser->lexer);
8848 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8849 = cp_parser_trailing_type_id (parser);
8852 /* The function parameters must be in scope all the way until after the
8853 trailing-return-type in case of decltype. */
8854 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8855 pop_binding (DECL_NAME (t), t);
8857 leave_scope ();
8860 /* Create the function call operator.
8862 Messing with declarators like this is no uglier than building up the
8863 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8864 other code. */
8866 cp_decl_specifier_seq return_type_specs;
8867 cp_declarator* declarator;
8868 tree fco;
8869 int quals;
8870 void *p;
8872 clear_decl_specs (&return_type_specs);
8873 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8874 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8875 else
8876 /* Maybe we will deduce the return type later. */
8877 return_type_specs.type = make_auto ();
8879 p = obstack_alloc (&declarator_obstack, 0);
8881 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8882 sfk_none);
8884 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8885 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8886 declarator = make_call_declarator (declarator, param_list, quals,
8887 VIRT_SPEC_UNSPECIFIED,
8888 REF_QUAL_NONE,
8889 exception_spec,
8890 /*late_return_type=*/NULL_TREE);
8891 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8893 fco = grokmethod (&return_type_specs,
8894 declarator,
8895 attributes);
8896 if (fco != error_mark_node)
8898 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8899 DECL_ARTIFICIAL (fco) = 1;
8900 /* Give the object parameter a different name. */
8901 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8904 finish_member_declaration (fco);
8906 obstack_free (&declarator_obstack, p);
8908 return (fco != error_mark_node);
8912 /* Parse the body of a lambda expression, which is simply
8914 compound-statement
8916 but which requires special handling.
8917 LAMBDA_EXPR is the current representation of the lambda expression. */
8919 static void
8920 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8922 bool nested = (current_function_decl != NULL_TREE);
8923 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8924 if (nested)
8925 push_function_context ();
8926 else
8927 /* Still increment function_depth so that we don't GC in the
8928 middle of an expression. */
8929 ++function_depth;
8930 /* Clear this in case we're in the middle of a default argument. */
8931 parser->local_variables_forbidden_p = false;
8933 /* Finish the function call operator
8934 - class_specifier
8935 + late_parsing_for_member
8936 + function_definition_after_declarator
8937 + ctor_initializer_opt_and_function_body */
8939 tree fco = lambda_function (lambda_expr);
8940 tree body;
8941 bool done = false;
8942 tree compound_stmt;
8943 tree cap;
8945 /* Let the front end know that we are going to be defining this
8946 function. */
8947 start_preparsed_function (fco,
8948 NULL_TREE,
8949 SF_PRE_PARSED | SF_INCLASS_INLINE);
8951 start_lambda_scope (fco);
8952 body = begin_function_body ();
8954 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8955 goto out;
8957 /* Push the proxies for any explicit captures. */
8958 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8959 cap = TREE_CHAIN (cap))
8960 build_capture_proxy (TREE_PURPOSE (cap));
8962 compound_stmt = begin_compound_stmt (0);
8964 /* 5.1.1.4 of the standard says:
8965 If a lambda-expression does not include a trailing-return-type, it
8966 is as if the trailing-return-type denotes the following type:
8967 * if the compound-statement is of the form
8968 { return attribute-specifier [opt] expression ; }
8969 the type of the returned expression after lvalue-to-rvalue
8970 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8971 (_conv.array_ 4.2), and function-to-pointer conversion
8972 (_conv.func_ 4.3);
8973 * otherwise, void. */
8975 /* In a lambda that has neither a lambda-return-type-clause
8976 nor a deducible form, errors should be reported for return statements
8977 in the body. Since we used void as the placeholder return type, parsing
8978 the body as usual will give such desired behavior. */
8979 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8980 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8981 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8983 tree expr = NULL_TREE;
8984 cp_id_kind idk = CP_ID_KIND_NONE;
8986 /* Parse tentatively in case there's more after the initial return
8987 statement. */
8988 cp_parser_parse_tentatively (parser);
8990 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8992 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8994 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8995 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8997 if (cp_parser_parse_definitely (parser))
8999 if (!processing_template_decl)
9000 apply_deduced_return_type (fco, lambda_return_type (expr));
9002 /* Will get error here if type not deduced yet. */
9003 finish_return_stmt (expr);
9005 done = true;
9009 if (!done)
9011 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9012 cp_parser_label_declaration (parser);
9013 cp_parser_statement_seq_opt (parser, NULL_TREE);
9014 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9017 finish_compound_stmt (compound_stmt);
9019 out:
9020 finish_function_body (body);
9021 finish_lambda_scope ();
9023 /* Finish the function and generate code for it if necessary. */
9024 expand_or_defer_fn (finish_function (/*inline*/2));
9027 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9028 if (nested)
9029 pop_function_context();
9030 else
9031 --function_depth;
9034 /* Statements [gram.stmt.stmt] */
9036 /* Parse a statement.
9038 statement:
9039 labeled-statement
9040 expression-statement
9041 compound-statement
9042 selection-statement
9043 iteration-statement
9044 jump-statement
9045 declaration-statement
9046 try-block
9048 C++11:
9050 statement:
9051 labeled-statement
9052 attribute-specifier-seq (opt) expression-statement
9053 attribute-specifier-seq (opt) compound-statement
9054 attribute-specifier-seq (opt) selection-statement
9055 attribute-specifier-seq (opt) iteration-statement
9056 attribute-specifier-seq (opt) jump-statement
9057 declaration-statement
9058 attribute-specifier-seq (opt) try-block
9060 TM Extension:
9062 statement:
9063 atomic-statement
9065 IN_COMPOUND is true when the statement is nested inside a
9066 cp_parser_compound_statement; this matters for certain pragmas.
9068 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9069 is a (possibly labeled) if statement which is not enclosed in braces
9070 and has an else clause. This is used to implement -Wparentheses. */
9072 static void
9073 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9074 bool in_compound, bool *if_p)
9076 tree statement, std_attrs = NULL_TREE;
9077 cp_token *token;
9078 location_t statement_location, attrs_location;
9080 restart:
9081 if (if_p != NULL)
9082 *if_p = false;
9083 /* There is no statement yet. */
9084 statement = NULL_TREE;
9086 cp_lexer_save_tokens (parser->lexer);
9087 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9088 if (c_dialect_objc ())
9089 /* In obj-c++, seing '[[' might be the either the beginning of
9090 c++11 attributes, or a nested objc-message-expression. So
9091 let's parse the c++11 attributes tentatively. */
9092 cp_parser_parse_tentatively (parser);
9093 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9094 if (c_dialect_objc ())
9096 if (!cp_parser_parse_definitely (parser))
9097 std_attrs = NULL_TREE;
9100 /* Peek at the next token. */
9101 token = cp_lexer_peek_token (parser->lexer);
9102 /* Remember the location of the first token in the statement. */
9103 statement_location = token->location;
9104 /* If this is a keyword, then that will often determine what kind of
9105 statement we have. */
9106 if (token->type == CPP_KEYWORD)
9108 enum rid keyword = token->keyword;
9110 switch (keyword)
9112 case RID_CASE:
9113 case RID_DEFAULT:
9114 /* Looks like a labeled-statement with a case label.
9115 Parse the label, and then use tail recursion to parse
9116 the statement. */
9117 cp_parser_label_for_labeled_statement (parser, std_attrs);
9118 goto restart;
9120 case RID_IF:
9121 case RID_SWITCH:
9122 statement = cp_parser_selection_statement (parser, if_p);
9123 break;
9125 case RID_WHILE:
9126 case RID_DO:
9127 case RID_FOR:
9128 statement = cp_parser_iteration_statement (parser);
9129 break;
9131 case RID_BREAK:
9132 case RID_CONTINUE:
9133 case RID_RETURN:
9134 case RID_GOTO:
9135 statement = cp_parser_jump_statement (parser);
9136 break;
9138 /* Objective-C++ exception-handling constructs. */
9139 case RID_AT_TRY:
9140 case RID_AT_CATCH:
9141 case RID_AT_FINALLY:
9142 case RID_AT_SYNCHRONIZED:
9143 case RID_AT_THROW:
9144 statement = cp_parser_objc_statement (parser);
9145 break;
9147 case RID_TRY:
9148 statement = cp_parser_try_block (parser);
9149 break;
9151 case RID_NAMESPACE:
9152 /* This must be a namespace alias definition. */
9153 cp_parser_declaration_statement (parser);
9154 return;
9156 case RID_TRANSACTION_ATOMIC:
9157 case RID_TRANSACTION_RELAXED:
9158 statement = cp_parser_transaction (parser, keyword);
9159 break;
9160 case RID_TRANSACTION_CANCEL:
9161 statement = cp_parser_transaction_cancel (parser);
9162 break;
9164 default:
9165 /* It might be a keyword like `int' that can start a
9166 declaration-statement. */
9167 break;
9170 else if (token->type == CPP_NAME)
9172 /* If the next token is a `:', then we are looking at a
9173 labeled-statement. */
9174 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9175 if (token->type == CPP_COLON)
9177 /* Looks like a labeled-statement with an ordinary label.
9178 Parse the label, and then use tail recursion to parse
9179 the statement. */
9181 cp_parser_label_for_labeled_statement (parser, std_attrs);
9182 goto restart;
9185 /* Anything that starts with a `{' must be a compound-statement. */
9186 else if (token->type == CPP_OPEN_BRACE)
9187 statement = cp_parser_compound_statement (parser, NULL, false, false);
9188 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9189 a statement all its own. */
9190 else if (token->type == CPP_PRAGMA)
9192 /* Only certain OpenMP pragmas are attached to statements, and thus
9193 are considered statements themselves. All others are not. In
9194 the context of a compound, accept the pragma as a "statement" and
9195 return so that we can check for a close brace. Otherwise we
9196 require a real statement and must go back and read one. */
9197 if (in_compound)
9198 cp_parser_pragma (parser, pragma_compound);
9199 else if (!cp_parser_pragma (parser, pragma_stmt))
9200 goto restart;
9201 return;
9203 else if (token->type == CPP_EOF)
9205 cp_parser_error (parser, "expected statement");
9206 return;
9209 /* Everything else must be a declaration-statement or an
9210 expression-statement. Try for the declaration-statement
9211 first, unless we are looking at a `;', in which case we know that
9212 we have an expression-statement. */
9213 if (!statement)
9215 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9217 if (std_attrs != NULL_TREE)
9219 /* Attributes should be parsed as part of the the
9220 declaration, so let's un-parse them. */
9221 cp_lexer_rollback_tokens (parser->lexer);
9222 std_attrs = NULL_TREE;
9225 cp_parser_parse_tentatively (parser);
9226 /* Try to parse the declaration-statement. */
9227 cp_parser_declaration_statement (parser);
9228 /* If that worked, we're done. */
9229 if (cp_parser_parse_definitely (parser))
9230 return;
9232 /* Look for an expression-statement instead. */
9233 statement = cp_parser_expression_statement (parser, in_statement_expr);
9236 /* Set the line number for the statement. */
9237 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9238 SET_EXPR_LOCATION (statement, statement_location);
9240 /* Note that for now, we don't do anything with c++11 statements
9241 parsed at this level. */
9242 if (std_attrs != NULL_TREE)
9243 warning_at (attrs_location,
9244 OPT_Wattributes,
9245 "attributes at the beginning of statement are ignored");
9248 /* Parse the label for a labeled-statement, i.e.
9250 identifier :
9251 case constant-expression :
9252 default :
9254 GNU Extension:
9255 case constant-expression ... constant-expression : statement
9257 When a label is parsed without errors, the label is added to the
9258 parse tree by the finish_* functions, so this function doesn't
9259 have to return the label. */
9261 static void
9262 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9264 cp_token *token;
9265 tree label = NULL_TREE;
9266 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9268 /* The next token should be an identifier. */
9269 token = cp_lexer_peek_token (parser->lexer);
9270 if (token->type != CPP_NAME
9271 && token->type != CPP_KEYWORD)
9273 cp_parser_error (parser, "expected labeled-statement");
9274 return;
9277 parser->colon_corrects_to_scope_p = false;
9278 switch (token->keyword)
9280 case RID_CASE:
9282 tree expr, expr_hi;
9283 cp_token *ellipsis;
9285 /* Consume the `case' token. */
9286 cp_lexer_consume_token (parser->lexer);
9287 /* Parse the constant-expression. */
9288 expr = cp_parser_constant_expression (parser,
9289 /*allow_non_constant_p=*/false,
9290 NULL);
9292 ellipsis = cp_lexer_peek_token (parser->lexer);
9293 if (ellipsis->type == CPP_ELLIPSIS)
9295 /* Consume the `...' token. */
9296 cp_lexer_consume_token (parser->lexer);
9297 expr_hi =
9298 cp_parser_constant_expression (parser,
9299 /*allow_non_constant_p=*/false,
9300 NULL);
9301 /* We don't need to emit warnings here, as the common code
9302 will do this for us. */
9304 else
9305 expr_hi = NULL_TREE;
9307 if (parser->in_switch_statement_p)
9308 finish_case_label (token->location, expr, expr_hi);
9309 else
9310 error_at (token->location,
9311 "case label %qE not within a switch statement",
9312 expr);
9314 break;
9316 case RID_DEFAULT:
9317 /* Consume the `default' token. */
9318 cp_lexer_consume_token (parser->lexer);
9320 if (parser->in_switch_statement_p)
9321 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9322 else
9323 error_at (token->location, "case label not within a switch statement");
9324 break;
9326 default:
9327 /* Anything else must be an ordinary label. */
9328 label = finish_label_stmt (cp_parser_identifier (parser));
9329 break;
9332 /* Require the `:' token. */
9333 cp_parser_require (parser, CPP_COLON, RT_COLON);
9335 /* An ordinary label may optionally be followed by attributes.
9336 However, this is only permitted if the attributes are then
9337 followed by a semicolon. This is because, for backward
9338 compatibility, when parsing
9339 lab: __attribute__ ((unused)) int i;
9340 we want the attribute to attach to "i", not "lab". */
9341 if (label != NULL_TREE
9342 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9344 tree attrs;
9345 cp_parser_parse_tentatively (parser);
9346 attrs = cp_parser_gnu_attributes_opt (parser);
9347 if (attrs == NULL_TREE
9348 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9349 cp_parser_abort_tentative_parse (parser);
9350 else if (!cp_parser_parse_definitely (parser))
9352 else
9353 attributes = chainon (attributes, attrs);
9356 if (attributes != NULL_TREE)
9357 cplus_decl_attributes (&label, attributes, 0);
9359 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9362 /* Parse an expression-statement.
9364 expression-statement:
9365 expression [opt] ;
9367 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9368 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9369 indicates whether this expression-statement is part of an
9370 expression statement. */
9372 static tree
9373 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9375 tree statement = NULL_TREE;
9376 cp_token *token = cp_lexer_peek_token (parser->lexer);
9378 /* If the next token is a ';', then there is no expression
9379 statement. */
9380 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9382 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9383 if (statement == error_mark_node
9384 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9386 cp_parser_skip_to_end_of_block_or_statement (parser);
9387 return error_mark_node;
9391 /* Give a helpful message for "A<T>::type t;" and the like. */
9392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9393 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9395 if (TREE_CODE (statement) == SCOPE_REF)
9396 error_at (token->location, "need %<typename%> before %qE because "
9397 "%qT is a dependent scope",
9398 statement, TREE_OPERAND (statement, 0));
9399 else if (is_overloaded_fn (statement)
9400 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9402 /* A::A a; */
9403 tree fn = get_first_fn (statement);
9404 error_at (token->location,
9405 "%<%T::%D%> names the constructor, not the type",
9406 DECL_CONTEXT (fn), DECL_NAME (fn));
9410 /* Consume the final `;'. */
9411 cp_parser_consume_semicolon_at_end_of_statement (parser);
9413 if (in_statement_expr
9414 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9415 /* This is the final expression statement of a statement
9416 expression. */
9417 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9418 else if (statement)
9419 statement = finish_expr_stmt (statement);
9420 else
9421 finish_stmt ();
9423 return statement;
9426 /* Parse a compound-statement.
9428 compound-statement:
9429 { statement-seq [opt] }
9431 GNU extension:
9433 compound-statement:
9434 { label-declaration-seq [opt] statement-seq [opt] }
9436 label-declaration-seq:
9437 label-declaration
9438 label-declaration-seq label-declaration
9440 Returns a tree representing the statement. */
9442 static tree
9443 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9444 bool in_try, bool function_body)
9446 tree compound_stmt;
9448 /* Consume the `{'. */
9449 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9450 return error_mark_node;
9451 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9452 && !function_body)
9453 pedwarn (input_location, OPT_Wpedantic,
9454 "compound-statement in constexpr function");
9455 /* Begin the compound-statement. */
9456 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9457 /* If the next keyword is `__label__' we have a label declaration. */
9458 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9459 cp_parser_label_declaration (parser);
9460 /* Parse an (optional) statement-seq. */
9461 cp_parser_statement_seq_opt (parser, in_statement_expr);
9462 /* Finish the compound-statement. */
9463 finish_compound_stmt (compound_stmt);
9464 /* Consume the `}'. */
9465 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9467 return compound_stmt;
9470 /* Parse an (optional) statement-seq.
9472 statement-seq:
9473 statement
9474 statement-seq [opt] statement */
9476 static void
9477 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9479 /* Scan statements until there aren't any more. */
9480 while (true)
9482 cp_token *token = cp_lexer_peek_token (parser->lexer);
9484 /* If we are looking at a `}', then we have run out of
9485 statements; the same is true if we have reached the end
9486 of file, or have stumbled upon a stray '@end'. */
9487 if (token->type == CPP_CLOSE_BRACE
9488 || token->type == CPP_EOF
9489 || token->type == CPP_PRAGMA_EOL
9490 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9491 break;
9493 /* If we are in a compound statement and find 'else' then
9494 something went wrong. */
9495 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9497 if (parser->in_statement & IN_IF_STMT)
9498 break;
9499 else
9501 token = cp_lexer_consume_token (parser->lexer);
9502 error_at (token->location, "%<else%> without a previous %<if%>");
9506 /* Parse the statement. */
9507 cp_parser_statement (parser, in_statement_expr, true, NULL);
9511 /* Parse a selection-statement.
9513 selection-statement:
9514 if ( condition ) statement
9515 if ( condition ) statement else statement
9516 switch ( condition ) statement
9518 Returns the new IF_STMT or SWITCH_STMT.
9520 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9521 is a (possibly labeled) if statement which is not enclosed in
9522 braces and has an else clause. This is used to implement
9523 -Wparentheses. */
9525 static tree
9526 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9528 cp_token *token;
9529 enum rid keyword;
9531 if (if_p != NULL)
9532 *if_p = false;
9534 /* Peek at the next token. */
9535 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9537 /* See what kind of keyword it is. */
9538 keyword = token->keyword;
9539 switch (keyword)
9541 case RID_IF:
9542 case RID_SWITCH:
9544 tree statement;
9545 tree condition;
9547 /* Look for the `('. */
9548 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9550 cp_parser_skip_to_end_of_statement (parser);
9551 return error_mark_node;
9554 /* Begin the selection-statement. */
9555 if (keyword == RID_IF)
9556 statement = begin_if_stmt ();
9557 else
9558 statement = begin_switch_stmt ();
9560 /* Parse the condition. */
9561 condition = cp_parser_condition (parser);
9562 /* Look for the `)'. */
9563 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9564 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9565 /*consume_paren=*/true);
9567 if (keyword == RID_IF)
9569 bool nested_if;
9570 unsigned char in_statement;
9572 /* Add the condition. */
9573 finish_if_stmt_cond (condition, statement);
9575 /* Parse the then-clause. */
9576 in_statement = parser->in_statement;
9577 parser->in_statement |= IN_IF_STMT;
9578 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9580 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9581 add_stmt (build_empty_stmt (loc));
9582 cp_lexer_consume_token (parser->lexer);
9583 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9584 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9585 "empty body in an %<if%> statement");
9586 nested_if = false;
9588 else
9589 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9590 parser->in_statement = in_statement;
9592 finish_then_clause (statement);
9594 /* If the next token is `else', parse the else-clause. */
9595 if (cp_lexer_next_token_is_keyword (parser->lexer,
9596 RID_ELSE))
9598 /* Consume the `else' keyword. */
9599 cp_lexer_consume_token (parser->lexer);
9600 begin_else_clause (statement);
9601 /* Parse the else-clause. */
9602 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9604 location_t loc;
9605 loc = cp_lexer_peek_token (parser->lexer)->location;
9606 warning_at (loc,
9607 OPT_Wempty_body, "suggest braces around "
9608 "empty body in an %<else%> statement");
9609 add_stmt (build_empty_stmt (loc));
9610 cp_lexer_consume_token (parser->lexer);
9612 else
9613 cp_parser_implicitly_scoped_statement (parser, NULL);
9615 finish_else_clause (statement);
9617 /* If we are currently parsing a then-clause, then
9618 IF_P will not be NULL. We set it to true to
9619 indicate that this if statement has an else clause.
9620 This may trigger the Wparentheses warning below
9621 when we get back up to the parent if statement. */
9622 if (if_p != NULL)
9623 *if_p = true;
9625 else
9627 /* This if statement does not have an else clause. If
9628 NESTED_IF is true, then the then-clause is an if
9629 statement which does have an else clause. We warn
9630 about the potential ambiguity. */
9631 if (nested_if)
9632 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9633 "suggest explicit braces to avoid ambiguous"
9634 " %<else%>");
9637 /* Now we're all done with the if-statement. */
9638 finish_if_stmt (statement);
9640 else
9642 bool in_switch_statement_p;
9643 unsigned char in_statement;
9645 /* Add the condition. */
9646 finish_switch_cond (condition, statement);
9648 /* Parse the body of the switch-statement. */
9649 in_switch_statement_p = parser->in_switch_statement_p;
9650 in_statement = parser->in_statement;
9651 parser->in_switch_statement_p = true;
9652 parser->in_statement |= IN_SWITCH_STMT;
9653 cp_parser_implicitly_scoped_statement (parser, NULL);
9654 parser->in_switch_statement_p = in_switch_statement_p;
9655 parser->in_statement = in_statement;
9657 /* Now we're all done with the switch-statement. */
9658 finish_switch_stmt (statement);
9661 return statement;
9663 break;
9665 default:
9666 cp_parser_error (parser, "expected selection-statement");
9667 return error_mark_node;
9671 /* Parse a condition.
9673 condition:
9674 expression
9675 type-specifier-seq declarator = initializer-clause
9676 type-specifier-seq declarator braced-init-list
9678 GNU Extension:
9680 condition:
9681 type-specifier-seq declarator asm-specification [opt]
9682 attributes [opt] = assignment-expression
9684 Returns the expression that should be tested. */
9686 static tree
9687 cp_parser_condition (cp_parser* parser)
9689 cp_decl_specifier_seq type_specifiers;
9690 const char *saved_message;
9691 int declares_class_or_enum;
9693 /* Try the declaration first. */
9694 cp_parser_parse_tentatively (parser);
9695 /* New types are not allowed in the type-specifier-seq for a
9696 condition. */
9697 saved_message = parser->type_definition_forbidden_message;
9698 parser->type_definition_forbidden_message
9699 = G_("types may not be defined in conditions");
9700 /* Parse the type-specifier-seq. */
9701 cp_parser_decl_specifier_seq (parser,
9702 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9703 &type_specifiers,
9704 &declares_class_or_enum);
9705 /* Restore the saved message. */
9706 parser->type_definition_forbidden_message = saved_message;
9707 /* If all is well, we might be looking at a declaration. */
9708 if (!cp_parser_error_occurred (parser))
9710 tree decl;
9711 tree asm_specification;
9712 tree attributes;
9713 cp_declarator *declarator;
9714 tree initializer = NULL_TREE;
9716 /* Parse the declarator. */
9717 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9718 /*ctor_dtor_or_conv_p=*/NULL,
9719 /*parenthesized_p=*/NULL,
9720 /*member_p=*/false);
9721 /* Parse the attributes. */
9722 attributes = cp_parser_attributes_opt (parser);
9723 /* Parse the asm-specification. */
9724 asm_specification = cp_parser_asm_specification_opt (parser);
9725 /* If the next token is not an `=' or '{', then we might still be
9726 looking at an expression. For example:
9728 if (A(a).x)
9730 looks like a decl-specifier-seq and a declarator -- but then
9731 there is no `=', so this is an expression. */
9732 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9733 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9734 cp_parser_simulate_error (parser);
9736 /* If we did see an `=' or '{', then we are looking at a declaration
9737 for sure. */
9738 if (cp_parser_parse_definitely (parser))
9740 tree pushed_scope;
9741 bool non_constant_p;
9742 bool flags = LOOKUP_ONLYCONVERTING;
9744 /* Create the declaration. */
9745 decl = start_decl (declarator, &type_specifiers,
9746 /*initialized_p=*/true,
9747 attributes, /*prefix_attributes=*/NULL_TREE,
9748 &pushed_scope);
9750 /* Parse the initializer. */
9751 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9753 initializer = cp_parser_braced_list (parser, &non_constant_p);
9754 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9755 flags = 0;
9757 else
9759 /* Consume the `='. */
9760 cp_parser_require (parser, CPP_EQ, RT_EQ);
9761 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9763 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9766 /* Process the initializer. */
9767 cp_finish_decl (decl,
9768 initializer, !non_constant_p,
9769 asm_specification,
9770 flags);
9772 if (pushed_scope)
9773 pop_scope (pushed_scope);
9775 return convert_from_reference (decl);
9778 /* If we didn't even get past the declarator successfully, we are
9779 definitely not looking at a declaration. */
9780 else
9781 cp_parser_abort_tentative_parse (parser);
9783 /* Otherwise, we are looking at an expression. */
9784 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9787 /* Parses a for-statement or range-for-statement until the closing ')',
9788 not included. */
9790 static tree
9791 cp_parser_for (cp_parser *parser)
9793 tree init, scope, decl;
9794 bool is_range_for;
9796 /* Begin the for-statement. */
9797 scope = begin_for_scope (&init);
9799 /* Parse the initialization. */
9800 is_range_for = cp_parser_for_init_statement (parser, &decl);
9802 if (is_range_for)
9803 return cp_parser_range_for (parser, scope, init, decl);
9804 else
9805 return cp_parser_c_for (parser, scope, init);
9808 static tree
9809 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9811 /* Normal for loop */
9812 tree condition = NULL_TREE;
9813 tree expression = NULL_TREE;
9814 tree stmt;
9816 stmt = begin_for_stmt (scope, init);
9817 /* The for-init-statement has already been parsed in
9818 cp_parser_for_init_statement, so no work is needed here. */
9819 finish_for_init_stmt (stmt);
9821 /* If there's a condition, process it. */
9822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9823 condition = cp_parser_condition (parser);
9824 finish_for_cond (condition, stmt);
9825 /* Look for the `;'. */
9826 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9828 /* If there's an expression, process it. */
9829 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9830 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9831 finish_for_expr (expression, stmt);
9833 return stmt;
9836 /* Tries to parse a range-based for-statement:
9838 range-based-for:
9839 decl-specifier-seq declarator : expression
9841 The decl-specifier-seq declarator and the `:' are already parsed by
9842 cp_parser_for_init_statement. If processing_template_decl it returns a
9843 newly created RANGE_FOR_STMT; if not, it is converted to a
9844 regular FOR_STMT. */
9846 static tree
9847 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9849 tree stmt, range_expr;
9851 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9853 bool expr_non_constant_p;
9854 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9856 else
9857 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9859 /* If in template, STMT is converted to a normal for-statement
9860 at instantiation. If not, it is done just ahead. */
9861 if (processing_template_decl)
9863 if (check_for_bare_parameter_packs (range_expr))
9864 range_expr = error_mark_node;
9865 stmt = begin_range_for_stmt (scope, init);
9866 finish_range_for_decl (stmt, range_decl, range_expr);
9867 if (!type_dependent_expression_p (range_expr)
9868 /* do_auto_deduction doesn't mess with template init-lists. */
9869 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9870 do_range_for_auto_deduction (range_decl, range_expr);
9872 else
9874 stmt = begin_for_stmt (scope, init);
9875 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9877 return stmt;
9880 /* Subroutine of cp_convert_range_for: given the initializer expression,
9881 builds up the range temporary. */
9883 static tree
9884 build_range_temp (tree range_expr)
9886 tree range_type, range_temp;
9888 /* Find out the type deduced by the declaration
9889 `auto &&__range = range_expr'. */
9890 range_type = cp_build_reference_type (make_auto (), true);
9891 range_type = do_auto_deduction (range_type, range_expr,
9892 type_uses_auto (range_type));
9894 /* Create the __range variable. */
9895 range_temp = build_decl (input_location, VAR_DECL,
9896 get_identifier ("__for_range"), range_type);
9897 TREE_USED (range_temp) = 1;
9898 DECL_ARTIFICIAL (range_temp) = 1;
9900 return range_temp;
9903 /* Used by cp_parser_range_for in template context: we aren't going to
9904 do a full conversion yet, but we still need to resolve auto in the
9905 type of the for-range-declaration if present. This is basically
9906 a shortcut version of cp_convert_range_for. */
9908 static void
9909 do_range_for_auto_deduction (tree decl, tree range_expr)
9911 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9912 if (auto_node)
9914 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9915 range_temp = convert_from_reference (build_range_temp (range_expr));
9916 iter_type = (cp_parser_perform_range_for_lookup
9917 (range_temp, &begin_dummy, &end_dummy));
9918 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9919 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9920 tf_warning_or_error);
9921 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9922 iter_decl, auto_node);
9926 /* Converts a range-based for-statement into a normal
9927 for-statement, as per the definition.
9929 for (RANGE_DECL : RANGE_EXPR)
9930 BLOCK
9932 should be equivalent to:
9935 auto &&__range = RANGE_EXPR;
9936 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9937 __begin != __end;
9938 ++__begin)
9940 RANGE_DECL = *__begin;
9941 BLOCK
9945 If RANGE_EXPR is an array:
9946 BEGIN_EXPR = __range
9947 END_EXPR = __range + ARRAY_SIZE(__range)
9948 Else if RANGE_EXPR has a member 'begin' or 'end':
9949 BEGIN_EXPR = __range.begin()
9950 END_EXPR = __range.end()
9951 Else:
9952 BEGIN_EXPR = begin(__range)
9953 END_EXPR = end(__range);
9955 If __range has a member 'begin' but not 'end', or vice versa, we must
9956 still use the second alternative (it will surely fail, however).
9957 When calling begin()/end() in the third alternative we must use
9958 argument dependent lookup, but always considering 'std' as an associated
9959 namespace. */
9961 tree
9962 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9964 tree begin, end;
9965 tree iter_type, begin_expr, end_expr;
9966 tree condition, expression;
9968 if (range_decl == error_mark_node || range_expr == error_mark_node)
9969 /* If an error happened previously do nothing or else a lot of
9970 unhelpful errors would be issued. */
9971 begin_expr = end_expr = iter_type = error_mark_node;
9972 else
9974 tree range_temp;
9976 if (TREE_CODE (range_expr) == VAR_DECL
9977 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
9978 /* Can't bind a reference to an array of runtime bound. */
9979 range_temp = range_expr;
9980 else
9982 range_temp = build_range_temp (range_expr);
9983 pushdecl (range_temp);
9984 cp_finish_decl (range_temp, range_expr,
9985 /*is_constant_init*/false, NULL_TREE,
9986 LOOKUP_ONLYCONVERTING);
9987 range_temp = convert_from_reference (range_temp);
9989 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9990 &begin_expr, &end_expr);
9993 /* The new for initialization statement. */
9994 begin = build_decl (input_location, VAR_DECL,
9995 get_identifier ("__for_begin"), iter_type);
9996 TREE_USED (begin) = 1;
9997 DECL_ARTIFICIAL (begin) = 1;
9998 pushdecl (begin);
9999 cp_finish_decl (begin, begin_expr,
10000 /*is_constant_init*/false, NULL_TREE,
10001 LOOKUP_ONLYCONVERTING);
10003 end = build_decl (input_location, VAR_DECL,
10004 get_identifier ("__for_end"), iter_type);
10005 TREE_USED (end) = 1;
10006 DECL_ARTIFICIAL (end) = 1;
10007 pushdecl (end);
10008 cp_finish_decl (end, end_expr,
10009 /*is_constant_init*/false, NULL_TREE,
10010 LOOKUP_ONLYCONVERTING);
10012 finish_for_init_stmt (statement);
10014 /* The new for condition. */
10015 condition = build_x_binary_op (input_location, NE_EXPR,
10016 begin, ERROR_MARK,
10017 end, ERROR_MARK,
10018 NULL, tf_warning_or_error);
10019 finish_for_cond (condition, statement);
10021 /* The new increment expression. */
10022 expression = finish_unary_op_expr (input_location,
10023 PREINCREMENT_EXPR, begin,
10024 tf_warning_or_error);
10025 finish_for_expr (expression, statement);
10027 /* The declaration is initialized with *__begin inside the loop body. */
10028 cp_finish_decl (range_decl,
10029 build_x_indirect_ref (input_location, begin, RO_NULL,
10030 tf_warning_or_error),
10031 /*is_constant_init*/false, NULL_TREE,
10032 LOOKUP_ONLYCONVERTING);
10034 return statement;
10037 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10038 We need to solve both at the same time because the method used
10039 depends on the existence of members begin or end.
10040 Returns the type deduced for the iterator expression. */
10042 static tree
10043 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10045 if (error_operand_p (range))
10047 *begin = *end = error_mark_node;
10048 return error_mark_node;
10051 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10053 error ("range-based %<for%> expression of type %qT "
10054 "has incomplete type", TREE_TYPE (range));
10055 *begin = *end = error_mark_node;
10056 return error_mark_node;
10058 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10060 /* If RANGE is an array, we will use pointer arithmetic. */
10061 *begin = range;
10062 *end = build_binary_op (input_location, PLUS_EXPR,
10063 range,
10064 array_type_nelts_top (TREE_TYPE (range)),
10066 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10068 else
10070 /* If it is not an array, we must do a bit of magic. */
10071 tree id_begin, id_end;
10072 tree member_begin, member_end;
10074 *begin = *end = error_mark_node;
10076 id_begin = get_identifier ("begin");
10077 id_end = get_identifier ("end");
10078 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10079 /*protect=*/2, /*want_type=*/false,
10080 tf_warning_or_error);
10081 member_end = lookup_member (TREE_TYPE (range), id_end,
10082 /*protect=*/2, /*want_type=*/false,
10083 tf_warning_or_error);
10085 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10087 /* Use the member functions. */
10088 if (member_begin != NULL_TREE)
10089 *begin = cp_parser_range_for_member_function (range, id_begin);
10090 else
10091 error ("range-based %<for%> expression of type %qT has an "
10092 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10094 if (member_end != NULL_TREE)
10095 *end = cp_parser_range_for_member_function (range, id_end);
10096 else
10097 error ("range-based %<for%> expression of type %qT has a "
10098 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10100 else
10102 /* Use global functions with ADL. */
10103 vec<tree, va_gc> *vec;
10104 vec = make_tree_vector ();
10106 vec_safe_push (vec, range);
10108 member_begin = perform_koenig_lookup (id_begin, vec,
10109 /*include_std=*/true,
10110 tf_warning_or_error);
10111 *begin = finish_call_expr (member_begin, &vec, false, true,
10112 tf_warning_or_error);
10113 member_end = perform_koenig_lookup (id_end, vec,
10114 /*include_std=*/true,
10115 tf_warning_or_error);
10116 *end = finish_call_expr (member_end, &vec, false, true,
10117 tf_warning_or_error);
10119 release_tree_vector (vec);
10122 /* Last common checks. */
10123 if (*begin == error_mark_node || *end == error_mark_node)
10125 /* If one of the expressions is an error do no more checks. */
10126 *begin = *end = error_mark_node;
10127 return error_mark_node;
10129 else
10131 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10132 /* The unqualified type of the __begin and __end temporaries should
10133 be the same, as required by the multiple auto declaration. */
10134 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10135 error ("inconsistent begin/end types in range-based %<for%> "
10136 "statement: %qT and %qT",
10137 TREE_TYPE (*begin), TREE_TYPE (*end));
10138 return iter_type;
10143 /* Helper function for cp_parser_perform_range_for_lookup.
10144 Builds a tree for RANGE.IDENTIFIER(). */
10146 static tree
10147 cp_parser_range_for_member_function (tree range, tree identifier)
10149 tree member, res;
10150 vec<tree, va_gc> *vec;
10152 member = finish_class_member_access_expr (range, identifier,
10153 false, tf_warning_or_error);
10154 if (member == error_mark_node)
10155 return error_mark_node;
10157 vec = make_tree_vector ();
10158 res = finish_call_expr (member, &vec,
10159 /*disallow_virtual=*/false,
10160 /*koenig_p=*/false,
10161 tf_warning_or_error);
10162 release_tree_vector (vec);
10163 return res;
10166 /* Parse an iteration-statement.
10168 iteration-statement:
10169 while ( condition ) statement
10170 do statement while ( expression ) ;
10171 for ( for-init-statement condition [opt] ; expression [opt] )
10172 statement
10174 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10176 static tree
10177 cp_parser_iteration_statement (cp_parser* parser)
10179 cp_token *token;
10180 enum rid keyword;
10181 tree statement;
10182 unsigned char in_statement;
10184 /* Peek at the next token. */
10185 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10186 if (!token)
10187 return error_mark_node;
10189 /* Remember whether or not we are already within an iteration
10190 statement. */
10191 in_statement = parser->in_statement;
10193 /* See what kind of keyword it is. */
10194 keyword = token->keyword;
10195 switch (keyword)
10197 case RID_WHILE:
10199 tree condition;
10201 /* Begin the while-statement. */
10202 statement = begin_while_stmt ();
10203 /* Look for the `('. */
10204 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10205 /* Parse the condition. */
10206 condition = cp_parser_condition (parser);
10207 finish_while_stmt_cond (condition, statement);
10208 /* Look for the `)'. */
10209 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10210 /* Parse the dependent statement. */
10211 parser->in_statement = IN_ITERATION_STMT;
10212 cp_parser_already_scoped_statement (parser);
10213 parser->in_statement = in_statement;
10214 /* We're done with the while-statement. */
10215 finish_while_stmt (statement);
10217 break;
10219 case RID_DO:
10221 tree expression;
10223 /* Begin the do-statement. */
10224 statement = begin_do_stmt ();
10225 /* Parse the body of the do-statement. */
10226 parser->in_statement = IN_ITERATION_STMT;
10227 cp_parser_implicitly_scoped_statement (parser, NULL);
10228 parser->in_statement = in_statement;
10229 finish_do_body (statement);
10230 /* Look for the `while' keyword. */
10231 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10232 /* Look for the `('. */
10233 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10234 /* Parse the expression. */
10235 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10236 /* We're done with the do-statement. */
10237 finish_do_stmt (expression, statement);
10238 /* Look for the `)'. */
10239 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10240 /* Look for the `;'. */
10241 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10243 break;
10245 case RID_FOR:
10247 /* Look for the `('. */
10248 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10250 statement = cp_parser_for (parser);
10252 /* Look for the `)'. */
10253 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10255 /* Parse the body of the for-statement. */
10256 parser->in_statement = IN_ITERATION_STMT;
10257 cp_parser_already_scoped_statement (parser);
10258 parser->in_statement = in_statement;
10260 /* We're done with the for-statement. */
10261 finish_for_stmt (statement);
10263 break;
10265 default:
10266 cp_parser_error (parser, "expected iteration-statement");
10267 statement = error_mark_node;
10268 break;
10271 return statement;
10274 /* Parse a for-init-statement or the declarator of a range-based-for.
10275 Returns true if a range-based-for declaration is seen.
10277 for-init-statement:
10278 expression-statement
10279 simple-declaration */
10281 static bool
10282 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10284 /* If the next token is a `;', then we have an empty
10285 expression-statement. Grammatically, this is also a
10286 simple-declaration, but an invalid one, because it does not
10287 declare anything. Therefore, if we did not handle this case
10288 specially, we would issue an error message about an invalid
10289 declaration. */
10290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10292 bool is_range_for = false;
10293 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10295 parser->colon_corrects_to_scope_p = false;
10297 /* We're going to speculatively look for a declaration, falling back
10298 to an expression, if necessary. */
10299 cp_parser_parse_tentatively (parser);
10300 /* Parse the declaration. */
10301 cp_parser_simple_declaration (parser,
10302 /*function_definition_allowed_p=*/false,
10303 decl);
10304 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10305 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10307 /* It is a range-for, consume the ':' */
10308 cp_lexer_consume_token (parser->lexer);
10309 is_range_for = true;
10310 if (cxx_dialect < cxx11)
10312 error_at (cp_lexer_peek_token (parser->lexer)->location,
10313 "range-based %<for%> loops are not allowed "
10314 "in C++98 mode");
10315 *decl = error_mark_node;
10318 else
10319 /* The ';' is not consumed yet because we told
10320 cp_parser_simple_declaration not to. */
10321 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10323 if (cp_parser_parse_definitely (parser))
10324 return is_range_for;
10325 /* If the tentative parse failed, then we shall need to look for an
10326 expression-statement. */
10328 /* If we are here, it is an expression-statement. */
10329 cp_parser_expression_statement (parser, NULL_TREE);
10330 return false;
10333 /* Parse a jump-statement.
10335 jump-statement:
10336 break ;
10337 continue ;
10338 return expression [opt] ;
10339 return braced-init-list ;
10340 goto identifier ;
10342 GNU extension:
10344 jump-statement:
10345 goto * expression ;
10347 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10349 static tree
10350 cp_parser_jump_statement (cp_parser* parser)
10352 tree statement = error_mark_node;
10353 cp_token *token;
10354 enum rid keyword;
10355 unsigned char in_statement;
10357 /* Peek at the next token. */
10358 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10359 if (!token)
10360 return error_mark_node;
10362 /* See what kind of keyword it is. */
10363 keyword = token->keyword;
10364 switch (keyword)
10366 case RID_BREAK:
10367 in_statement = parser->in_statement & ~IN_IF_STMT;
10368 switch (in_statement)
10370 case 0:
10371 error_at (token->location, "break statement not within loop or switch");
10372 break;
10373 default:
10374 gcc_assert ((in_statement & IN_SWITCH_STMT)
10375 || in_statement == IN_ITERATION_STMT);
10376 statement = finish_break_stmt ();
10377 break;
10378 case IN_OMP_BLOCK:
10379 error_at (token->location, "invalid exit from OpenMP structured block");
10380 break;
10381 case IN_OMP_FOR:
10382 error_at (token->location, "break statement used with OpenMP for loop");
10383 break;
10384 case IN_CILK_P_SIMD_FOR:
10385 error_at (token->location,
10386 "break statement within <#pragma simd> loop body");
10387 break;
10389 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10390 break;
10392 case RID_CONTINUE:
10393 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10395 case 0:
10396 error_at (token->location, "continue statement not within a loop");
10397 break;
10398 case IN_ITERATION_STMT:
10399 case IN_OMP_FOR:
10400 statement = finish_continue_stmt ();
10401 break;
10402 case IN_OMP_BLOCK:
10403 error_at (token->location, "invalid exit from OpenMP structured block");
10404 break;
10405 case IN_CILK_P_SIMD_FOR:
10406 error_at (token->location,
10407 "continue statement within <#pragma simd> loop body");
10408 break;
10409 default:
10410 gcc_unreachable ();
10412 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10413 break;
10415 case RID_RETURN:
10417 tree expr;
10418 bool expr_non_constant_p;
10420 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10422 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10423 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10425 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10426 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10427 else
10428 /* If the next token is a `;', then there is no
10429 expression. */
10430 expr = NULL_TREE;
10431 /* Build the return-statement. */
10432 statement = finish_return_stmt (expr);
10433 /* Look for the final `;'. */
10434 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10436 break;
10438 case RID_GOTO:
10439 /* Create the goto-statement. */
10440 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10442 /* Issue a warning about this use of a GNU extension. */
10443 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10444 /* Consume the '*' token. */
10445 cp_lexer_consume_token (parser->lexer);
10446 /* Parse the dependent expression. */
10447 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10449 else
10450 finish_goto_stmt (cp_parser_identifier (parser));
10451 /* Look for the final `;'. */
10452 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10453 break;
10455 default:
10456 cp_parser_error (parser, "expected jump-statement");
10457 break;
10460 return statement;
10463 /* Parse a declaration-statement.
10465 declaration-statement:
10466 block-declaration */
10468 static void
10469 cp_parser_declaration_statement (cp_parser* parser)
10471 void *p;
10473 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10474 p = obstack_alloc (&declarator_obstack, 0);
10476 /* Parse the block-declaration. */
10477 cp_parser_block_declaration (parser, /*statement_p=*/true);
10479 /* Free any declarators allocated. */
10480 obstack_free (&declarator_obstack, p);
10482 /* Finish off the statement. */
10483 finish_stmt ();
10486 /* Some dependent statements (like `if (cond) statement'), are
10487 implicitly in their own scope. In other words, if the statement is
10488 a single statement (as opposed to a compound-statement), it is
10489 none-the-less treated as if it were enclosed in braces. Any
10490 declarations appearing in the dependent statement are out of scope
10491 after control passes that point. This function parses a statement,
10492 but ensures that is in its own scope, even if it is not a
10493 compound-statement.
10495 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10496 is a (possibly labeled) if statement which is not enclosed in
10497 braces and has an else clause. This is used to implement
10498 -Wparentheses.
10500 Returns the new statement. */
10502 static tree
10503 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10505 tree statement;
10507 if (if_p != NULL)
10508 *if_p = false;
10510 /* Mark if () ; with a special NOP_EXPR. */
10511 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10513 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10514 cp_lexer_consume_token (parser->lexer);
10515 statement = add_stmt (build_empty_stmt (loc));
10517 /* if a compound is opened, we simply parse the statement directly. */
10518 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10519 statement = cp_parser_compound_statement (parser, NULL, false, false);
10520 /* If the token is not a `{', then we must take special action. */
10521 else
10523 /* Create a compound-statement. */
10524 statement = begin_compound_stmt (0);
10525 /* Parse the dependent-statement. */
10526 cp_parser_statement (parser, NULL_TREE, false, if_p);
10527 /* Finish the dummy compound-statement. */
10528 finish_compound_stmt (statement);
10531 /* Return the statement. */
10532 return statement;
10535 /* For some dependent statements (like `while (cond) statement'), we
10536 have already created a scope. Therefore, even if the dependent
10537 statement is a compound-statement, we do not want to create another
10538 scope. */
10540 static void
10541 cp_parser_already_scoped_statement (cp_parser* parser)
10543 /* If the token is a `{', then we must take special action. */
10544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10545 cp_parser_statement (parser, NULL_TREE, false, NULL);
10546 else
10548 /* Avoid calling cp_parser_compound_statement, so that we
10549 don't create a new scope. Do everything else by hand. */
10550 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10551 /* If the next keyword is `__label__' we have a label declaration. */
10552 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10553 cp_parser_label_declaration (parser);
10554 /* Parse an (optional) statement-seq. */
10555 cp_parser_statement_seq_opt (parser, NULL_TREE);
10556 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10560 /* Declarations [gram.dcl.dcl] */
10562 /* Parse an optional declaration-sequence.
10564 declaration-seq:
10565 declaration
10566 declaration-seq declaration */
10568 static void
10569 cp_parser_declaration_seq_opt (cp_parser* parser)
10571 while (true)
10573 cp_token *token;
10575 token = cp_lexer_peek_token (parser->lexer);
10577 if (token->type == CPP_CLOSE_BRACE
10578 || token->type == CPP_EOF
10579 || token->type == CPP_PRAGMA_EOL)
10580 break;
10582 if (token->type == CPP_SEMICOLON)
10584 /* A declaration consisting of a single semicolon is
10585 invalid. Allow it unless we're being pedantic. */
10586 cp_lexer_consume_token (parser->lexer);
10587 if (!in_system_header)
10588 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10589 continue;
10592 /* If we're entering or exiting a region that's implicitly
10593 extern "C", modify the lang context appropriately. */
10594 if (!parser->implicit_extern_c && token->implicit_extern_c)
10596 push_lang_context (lang_name_c);
10597 parser->implicit_extern_c = true;
10599 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10601 pop_lang_context ();
10602 parser->implicit_extern_c = false;
10605 if (token->type == CPP_PRAGMA)
10607 /* A top-level declaration can consist solely of a #pragma.
10608 A nested declaration cannot, so this is done here and not
10609 in cp_parser_declaration. (A #pragma at block scope is
10610 handled in cp_parser_statement.) */
10611 cp_parser_pragma (parser, pragma_external);
10612 continue;
10615 /* Parse the declaration itself. */
10616 cp_parser_declaration (parser);
10620 /* Parse a declaration.
10622 declaration:
10623 block-declaration
10624 function-definition
10625 template-declaration
10626 explicit-instantiation
10627 explicit-specialization
10628 linkage-specification
10629 namespace-definition
10631 GNU extension:
10633 declaration:
10634 __extension__ declaration */
10636 static void
10637 cp_parser_declaration (cp_parser* parser)
10639 cp_token token1;
10640 cp_token token2;
10641 int saved_pedantic;
10642 void *p;
10643 tree attributes = NULL_TREE;
10645 /* Check for the `__extension__' keyword. */
10646 if (cp_parser_extension_opt (parser, &saved_pedantic))
10648 /* Parse the qualified declaration. */
10649 cp_parser_declaration (parser);
10650 /* Restore the PEDANTIC flag. */
10651 pedantic = saved_pedantic;
10653 return;
10656 /* Try to figure out what kind of declaration is present. */
10657 token1 = *cp_lexer_peek_token (parser->lexer);
10659 if (token1.type != CPP_EOF)
10660 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10661 else
10663 token2.type = CPP_EOF;
10664 token2.keyword = RID_MAX;
10667 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10668 p = obstack_alloc (&declarator_obstack, 0);
10670 /* If the next token is `extern' and the following token is a string
10671 literal, then we have a linkage specification. */
10672 if (token1.keyword == RID_EXTERN
10673 && cp_parser_is_pure_string_literal (&token2))
10674 cp_parser_linkage_specification (parser);
10675 /* If the next token is `template', then we have either a template
10676 declaration, an explicit instantiation, or an explicit
10677 specialization. */
10678 else if (token1.keyword == RID_TEMPLATE)
10680 /* `template <>' indicates a template specialization. */
10681 if (token2.type == CPP_LESS
10682 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10683 cp_parser_explicit_specialization (parser);
10684 /* `template <' indicates a template declaration. */
10685 else if (token2.type == CPP_LESS)
10686 cp_parser_template_declaration (parser, /*member_p=*/false);
10687 /* Anything else must be an explicit instantiation. */
10688 else
10689 cp_parser_explicit_instantiation (parser);
10691 /* If the next token is `export', then we have a template
10692 declaration. */
10693 else if (token1.keyword == RID_EXPORT)
10694 cp_parser_template_declaration (parser, /*member_p=*/false);
10695 /* If the next token is `extern', 'static' or 'inline' and the one
10696 after that is `template', we have a GNU extended explicit
10697 instantiation directive. */
10698 else if (cp_parser_allow_gnu_extensions_p (parser)
10699 && (token1.keyword == RID_EXTERN
10700 || token1.keyword == RID_STATIC
10701 || token1.keyword == RID_INLINE)
10702 && token2.keyword == RID_TEMPLATE)
10703 cp_parser_explicit_instantiation (parser);
10704 /* If the next token is `namespace', check for a named or unnamed
10705 namespace definition. */
10706 else if (token1.keyword == RID_NAMESPACE
10707 && (/* A named namespace definition. */
10708 (token2.type == CPP_NAME
10709 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10710 != CPP_EQ))
10711 /* An unnamed namespace definition. */
10712 || token2.type == CPP_OPEN_BRACE
10713 || token2.keyword == RID_ATTRIBUTE))
10714 cp_parser_namespace_definition (parser);
10715 /* An inline (associated) namespace definition. */
10716 else if (token1.keyword == RID_INLINE
10717 && token2.keyword == RID_NAMESPACE)
10718 cp_parser_namespace_definition (parser);
10719 /* Objective-C++ declaration/definition. */
10720 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10721 cp_parser_objc_declaration (parser, NULL_TREE);
10722 else if (c_dialect_objc ()
10723 && token1.keyword == RID_ATTRIBUTE
10724 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10725 cp_parser_objc_declaration (parser, attributes);
10726 /* We must have either a block declaration or a function
10727 definition. */
10728 else
10729 /* Try to parse a block-declaration, or a function-definition. */
10730 cp_parser_block_declaration (parser, /*statement_p=*/false);
10732 /* Free any declarators allocated. */
10733 obstack_free (&declarator_obstack, p);
10736 /* Parse a block-declaration.
10738 block-declaration:
10739 simple-declaration
10740 asm-definition
10741 namespace-alias-definition
10742 using-declaration
10743 using-directive
10745 GNU Extension:
10747 block-declaration:
10748 __extension__ block-declaration
10750 C++0x Extension:
10752 block-declaration:
10753 static_assert-declaration
10755 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10756 part of a declaration-statement. */
10758 static void
10759 cp_parser_block_declaration (cp_parser *parser,
10760 bool statement_p)
10762 cp_token *token1;
10763 int saved_pedantic;
10765 /* Check for the `__extension__' keyword. */
10766 if (cp_parser_extension_opt (parser, &saved_pedantic))
10768 /* Parse the qualified declaration. */
10769 cp_parser_block_declaration (parser, statement_p);
10770 /* Restore the PEDANTIC flag. */
10771 pedantic = saved_pedantic;
10773 return;
10776 /* Peek at the next token to figure out which kind of declaration is
10777 present. */
10778 token1 = cp_lexer_peek_token (parser->lexer);
10780 /* If the next keyword is `asm', we have an asm-definition. */
10781 if (token1->keyword == RID_ASM)
10783 if (statement_p)
10784 cp_parser_commit_to_tentative_parse (parser);
10785 cp_parser_asm_definition (parser);
10787 /* If the next keyword is `namespace', we have a
10788 namespace-alias-definition. */
10789 else if (token1->keyword == RID_NAMESPACE)
10790 cp_parser_namespace_alias_definition (parser);
10791 /* If the next keyword is `using', we have a
10792 using-declaration, a using-directive, or an alias-declaration. */
10793 else if (token1->keyword == RID_USING)
10795 cp_token *token2;
10797 if (statement_p)
10798 cp_parser_commit_to_tentative_parse (parser);
10799 /* If the token after `using' is `namespace', then we have a
10800 using-directive. */
10801 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10802 if (token2->keyword == RID_NAMESPACE)
10803 cp_parser_using_directive (parser);
10804 /* If the second token after 'using' is '=', then we have an
10805 alias-declaration. */
10806 else if (cxx_dialect >= cxx11
10807 && token2->type == CPP_NAME
10808 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10809 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10810 cp_parser_alias_declaration (parser);
10811 /* Otherwise, it's a using-declaration. */
10812 else
10813 cp_parser_using_declaration (parser,
10814 /*access_declaration_p=*/false);
10816 /* If the next keyword is `__label__' we have a misplaced label
10817 declaration. */
10818 else if (token1->keyword == RID_LABEL)
10820 cp_lexer_consume_token (parser->lexer);
10821 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10822 cp_parser_skip_to_end_of_statement (parser);
10823 /* If the next token is now a `;', consume it. */
10824 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10825 cp_lexer_consume_token (parser->lexer);
10827 /* If the next token is `static_assert' we have a static assertion. */
10828 else if (token1->keyword == RID_STATIC_ASSERT)
10829 cp_parser_static_assert (parser, /*member_p=*/false);
10830 /* Anything else must be a simple-declaration. */
10831 else
10832 cp_parser_simple_declaration (parser, !statement_p,
10833 /*maybe_range_for_decl*/NULL);
10836 /* Parse a simple-declaration.
10838 simple-declaration:
10839 decl-specifier-seq [opt] init-declarator-list [opt] ;
10841 init-declarator-list:
10842 init-declarator
10843 init-declarator-list , init-declarator
10845 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10846 function-definition as a simple-declaration.
10848 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10849 parsed declaration if it is an uninitialized single declarator not followed
10850 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10851 if present, will not be consumed. */
10853 static void
10854 cp_parser_simple_declaration (cp_parser* parser,
10855 bool function_definition_allowed_p,
10856 tree *maybe_range_for_decl)
10858 cp_decl_specifier_seq decl_specifiers;
10859 int declares_class_or_enum;
10860 bool saw_declarator;
10862 if (maybe_range_for_decl)
10863 *maybe_range_for_decl = NULL_TREE;
10865 /* Defer access checks until we know what is being declared; the
10866 checks for names appearing in the decl-specifier-seq should be
10867 done as if we were in the scope of the thing being declared. */
10868 push_deferring_access_checks (dk_deferred);
10870 /* Parse the decl-specifier-seq. We have to keep track of whether
10871 or not the decl-specifier-seq declares a named class or
10872 enumeration type, since that is the only case in which the
10873 init-declarator-list is allowed to be empty.
10875 [dcl.dcl]
10877 In a simple-declaration, the optional init-declarator-list can be
10878 omitted only when declaring a class or enumeration, that is when
10879 the decl-specifier-seq contains either a class-specifier, an
10880 elaborated-type-specifier, or an enum-specifier. */
10881 cp_parser_decl_specifier_seq (parser,
10882 CP_PARSER_FLAGS_OPTIONAL,
10883 &decl_specifiers,
10884 &declares_class_or_enum);
10885 /* We no longer need to defer access checks. */
10886 stop_deferring_access_checks ();
10888 /* In a block scope, a valid declaration must always have a
10889 decl-specifier-seq. By not trying to parse declarators, we can
10890 resolve the declaration/expression ambiguity more quickly. */
10891 if (!function_definition_allowed_p
10892 && !decl_specifiers.any_specifiers_p)
10894 cp_parser_error (parser, "expected declaration");
10895 goto done;
10898 /* If the next two tokens are both identifiers, the code is
10899 erroneous. The usual cause of this situation is code like:
10901 T t;
10903 where "T" should name a type -- but does not. */
10904 if (!decl_specifiers.any_type_specifiers_p
10905 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10907 /* If parsing tentatively, we should commit; we really are
10908 looking at a declaration. */
10909 cp_parser_commit_to_tentative_parse (parser);
10910 /* Give up. */
10911 goto done;
10914 /* If we have seen at least one decl-specifier, and the next token
10915 is not a parenthesis, then we must be looking at a declaration.
10916 (After "int (" we might be looking at a functional cast.) */
10917 if (decl_specifiers.any_specifiers_p
10918 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10919 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10920 && !cp_parser_error_occurred (parser))
10921 cp_parser_commit_to_tentative_parse (parser);
10923 /* Keep going until we hit the `;' at the end of the simple
10924 declaration. */
10925 saw_declarator = false;
10926 while (cp_lexer_next_token_is_not (parser->lexer,
10927 CPP_SEMICOLON))
10929 cp_token *token;
10930 bool function_definition_p;
10931 tree decl;
10933 if (saw_declarator)
10935 /* If we are processing next declarator, coma is expected */
10936 token = cp_lexer_peek_token (parser->lexer);
10937 gcc_assert (token->type == CPP_COMMA);
10938 cp_lexer_consume_token (parser->lexer);
10939 if (maybe_range_for_decl)
10940 *maybe_range_for_decl = error_mark_node;
10942 else
10943 saw_declarator = true;
10945 /* Parse the init-declarator. */
10946 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10947 /*checks=*/NULL,
10948 function_definition_allowed_p,
10949 /*member_p=*/false,
10950 declares_class_or_enum,
10951 &function_definition_p,
10952 maybe_range_for_decl);
10953 /* If an error occurred while parsing tentatively, exit quickly.
10954 (That usually happens when in the body of a function; each
10955 statement is treated as a declaration-statement until proven
10956 otherwise.) */
10957 if (cp_parser_error_occurred (parser))
10958 goto done;
10959 /* Handle function definitions specially. */
10960 if (function_definition_p)
10962 /* If the next token is a `,', then we are probably
10963 processing something like:
10965 void f() {}, *p;
10967 which is erroneous. */
10968 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10970 cp_token *token = cp_lexer_peek_token (parser->lexer);
10971 error_at (token->location,
10972 "mixing"
10973 " declarations and function-definitions is forbidden");
10975 /* Otherwise, we're done with the list of declarators. */
10976 else
10978 pop_deferring_access_checks ();
10979 return;
10982 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10983 *maybe_range_for_decl = decl;
10984 /* The next token should be either a `,' or a `;'. */
10985 token = cp_lexer_peek_token (parser->lexer);
10986 /* If it's a `,', there are more declarators to come. */
10987 if (token->type == CPP_COMMA)
10988 /* will be consumed next time around */;
10989 /* If it's a `;', we are done. */
10990 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10991 break;
10992 /* Anything else is an error. */
10993 else
10995 /* If we have already issued an error message we don't need
10996 to issue another one. */
10997 if (decl != error_mark_node
10998 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10999 cp_parser_error (parser, "expected %<,%> or %<;%>");
11000 /* Skip tokens until we reach the end of the statement. */
11001 cp_parser_skip_to_end_of_statement (parser);
11002 /* If the next token is now a `;', consume it. */
11003 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11004 cp_lexer_consume_token (parser->lexer);
11005 goto done;
11007 /* After the first time around, a function-definition is not
11008 allowed -- even if it was OK at first. For example:
11010 int i, f() {}
11012 is not valid. */
11013 function_definition_allowed_p = false;
11016 /* Issue an error message if no declarators are present, and the
11017 decl-specifier-seq does not itself declare a class or
11018 enumeration. */
11019 if (!saw_declarator)
11021 if (cp_parser_declares_only_class_p (parser))
11022 shadow_tag (&decl_specifiers);
11023 /* Perform any deferred access checks. */
11024 perform_deferred_access_checks (tf_warning_or_error);
11027 /* Consume the `;'. */
11028 if (!maybe_range_for_decl)
11029 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11031 done:
11032 pop_deferring_access_checks ();
11035 /* Parse a decl-specifier-seq.
11037 decl-specifier-seq:
11038 decl-specifier-seq [opt] decl-specifier
11039 decl-specifier attribute-specifier-seq [opt] (C++11)
11041 decl-specifier:
11042 storage-class-specifier
11043 type-specifier
11044 function-specifier
11045 friend
11046 typedef
11048 GNU Extension:
11050 decl-specifier:
11051 attributes
11053 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11055 The parser flags FLAGS is used to control type-specifier parsing.
11057 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11058 flags:
11060 1: one of the decl-specifiers is an elaborated-type-specifier
11061 (i.e., a type declaration)
11062 2: one of the decl-specifiers is an enum-specifier or a
11063 class-specifier (i.e., a type definition)
11067 static void
11068 cp_parser_decl_specifier_seq (cp_parser* parser,
11069 cp_parser_flags flags,
11070 cp_decl_specifier_seq *decl_specs,
11071 int* declares_class_or_enum)
11073 bool constructor_possible_p = !parser->in_declarator_p;
11074 bool found_decl_spec = false;
11075 cp_token *start_token = NULL;
11076 cp_decl_spec ds;
11078 /* Clear DECL_SPECS. */
11079 clear_decl_specs (decl_specs);
11081 /* Assume no class or enumeration type is declared. */
11082 *declares_class_or_enum = 0;
11084 /* Keep reading specifiers until there are no more to read. */
11085 while (true)
11087 bool constructor_p;
11088 cp_token *token;
11089 ds = ds_last;
11091 /* Peek at the next token. */
11092 token = cp_lexer_peek_token (parser->lexer);
11094 /* Save the first token of the decl spec list for error
11095 reporting. */
11096 if (!start_token)
11097 start_token = token;
11098 /* Handle attributes. */
11099 if (cp_next_tokens_can_be_attribute_p (parser))
11101 /* Parse the attributes. */
11102 tree attrs = cp_parser_attributes_opt (parser);
11104 /* In a sequence of declaration specifiers, c++11 attributes
11105 appertain to the type that precede them. In that case
11106 [dcl.spec]/1 says:
11108 The attribute-specifier-seq affects the type only for
11109 the declaration it appears in, not other declarations
11110 involving the same type.
11112 But for now let's force the user to position the
11113 attribute either at the beginning of the declaration or
11114 after the declarator-id, which would clearly mean that it
11115 applies to the declarator. */
11116 if (cxx11_attribute_p (attrs))
11118 if (!found_decl_spec)
11119 /* The c++11 attribute is at the beginning of the
11120 declaration. It appertains to the entity being
11121 declared. */;
11122 else
11124 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11126 /* This is an attribute following a
11127 class-specifier. */
11128 if (decl_specs->type_definition_p)
11129 warn_misplaced_attr_for_class_type (token->location,
11130 decl_specs->type);
11131 attrs = NULL_TREE;
11133 else
11135 decl_specs->std_attributes
11136 = chainon (decl_specs->std_attributes,
11137 attrs);
11138 if (decl_specs->locations[ds_std_attribute] == 0)
11139 decl_specs->locations[ds_std_attribute] = token->location;
11141 continue;
11145 decl_specs->attributes
11146 = chainon (decl_specs->attributes,
11147 attrs);
11148 if (decl_specs->locations[ds_attribute] == 0)
11149 decl_specs->locations[ds_attribute] = token->location;
11150 continue;
11152 /* Assume we will find a decl-specifier keyword. */
11153 found_decl_spec = true;
11154 /* If the next token is an appropriate keyword, we can simply
11155 add it to the list. */
11156 switch (token->keyword)
11158 /* decl-specifier:
11159 friend
11160 constexpr */
11161 case RID_FRIEND:
11162 if (!at_class_scope_p ())
11164 error_at (token->location, "%<friend%> used outside of class");
11165 cp_lexer_purge_token (parser->lexer);
11167 else
11169 ds = ds_friend;
11170 /* Consume the token. */
11171 cp_lexer_consume_token (parser->lexer);
11173 break;
11175 case RID_CONSTEXPR:
11176 ds = ds_constexpr;
11177 cp_lexer_consume_token (parser->lexer);
11178 break;
11180 /* function-specifier:
11181 inline
11182 virtual
11183 explicit */
11184 case RID_INLINE:
11185 case RID_VIRTUAL:
11186 case RID_EXPLICIT:
11187 cp_parser_function_specifier_opt (parser, decl_specs);
11188 break;
11190 /* decl-specifier:
11191 typedef */
11192 case RID_TYPEDEF:
11193 ds = ds_typedef;
11194 /* Consume the token. */
11195 cp_lexer_consume_token (parser->lexer);
11196 /* A constructor declarator cannot appear in a typedef. */
11197 constructor_possible_p = false;
11198 /* The "typedef" keyword can only occur in a declaration; we
11199 may as well commit at this point. */
11200 cp_parser_commit_to_tentative_parse (parser);
11202 if (decl_specs->storage_class != sc_none)
11203 decl_specs->conflicting_specifiers_p = true;
11204 break;
11206 /* storage-class-specifier:
11207 auto
11208 register
11209 static
11210 extern
11211 mutable
11213 GNU Extension:
11214 thread */
11215 case RID_AUTO:
11216 if (cxx_dialect == cxx98)
11218 /* Consume the token. */
11219 cp_lexer_consume_token (parser->lexer);
11221 /* Complain about `auto' as a storage specifier, if
11222 we're complaining about C++0x compatibility. */
11223 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11224 " changes meaning in C++11; please remove it");
11226 /* Set the storage class anyway. */
11227 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11228 token);
11230 else
11231 /* C++0x auto type-specifier. */
11232 found_decl_spec = false;
11233 break;
11235 case RID_REGISTER:
11236 case RID_STATIC:
11237 case RID_EXTERN:
11238 case RID_MUTABLE:
11239 /* Consume the token. */
11240 cp_lexer_consume_token (parser->lexer);
11241 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11242 token);
11243 break;
11244 case RID_THREAD:
11245 /* Consume the token. */
11246 ds = ds_thread;
11247 cp_lexer_consume_token (parser->lexer);
11248 break;
11250 default:
11251 /* We did not yet find a decl-specifier yet. */
11252 found_decl_spec = false;
11253 break;
11256 if (found_decl_spec
11257 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11258 && token->keyword != RID_CONSTEXPR)
11259 error ("decl-specifier invalid in condition");
11261 if (ds != ds_last)
11262 set_and_check_decl_spec_loc (decl_specs, ds, token);
11264 /* Constructors are a special case. The `S' in `S()' is not a
11265 decl-specifier; it is the beginning of the declarator. */
11266 constructor_p
11267 = (!found_decl_spec
11268 && constructor_possible_p
11269 && (cp_parser_constructor_declarator_p
11270 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11272 /* If we don't have a DECL_SPEC yet, then we must be looking at
11273 a type-specifier. */
11274 if (!found_decl_spec && !constructor_p)
11276 int decl_spec_declares_class_or_enum;
11277 bool is_cv_qualifier;
11278 tree type_spec;
11280 type_spec
11281 = cp_parser_type_specifier (parser, flags,
11282 decl_specs,
11283 /*is_declaration=*/true,
11284 &decl_spec_declares_class_or_enum,
11285 &is_cv_qualifier);
11286 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11288 /* If this type-specifier referenced a user-defined type
11289 (a typedef, class-name, etc.), then we can't allow any
11290 more such type-specifiers henceforth.
11292 [dcl.spec]
11294 The longest sequence of decl-specifiers that could
11295 possibly be a type name is taken as the
11296 decl-specifier-seq of a declaration. The sequence shall
11297 be self-consistent as described below.
11299 [dcl.type]
11301 As a general rule, at most one type-specifier is allowed
11302 in the complete decl-specifier-seq of a declaration. The
11303 only exceptions are the following:
11305 -- const or volatile can be combined with any other
11306 type-specifier.
11308 -- signed or unsigned can be combined with char, long,
11309 short, or int.
11311 -- ..
11313 Example:
11315 typedef char* Pc;
11316 void g (const int Pc);
11318 Here, Pc is *not* part of the decl-specifier seq; it's
11319 the declarator. Therefore, once we see a type-specifier
11320 (other than a cv-qualifier), we forbid any additional
11321 user-defined types. We *do* still allow things like `int
11322 int' to be considered a decl-specifier-seq, and issue the
11323 error message later. */
11324 if (type_spec && !is_cv_qualifier)
11325 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11326 /* A constructor declarator cannot follow a type-specifier. */
11327 if (type_spec)
11329 constructor_possible_p = false;
11330 found_decl_spec = true;
11331 if (!is_cv_qualifier)
11332 decl_specs->any_type_specifiers_p = true;
11336 /* If we still do not have a DECL_SPEC, then there are no more
11337 decl-specifiers. */
11338 if (!found_decl_spec)
11339 break;
11341 decl_specs->any_specifiers_p = true;
11342 /* After we see one decl-specifier, further decl-specifiers are
11343 always optional. */
11344 flags |= CP_PARSER_FLAGS_OPTIONAL;
11347 /* Don't allow a friend specifier with a class definition. */
11348 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11349 && (*declares_class_or_enum & 2))
11350 error_at (decl_specs->locations[ds_friend],
11351 "class definition may not be declared a friend");
11354 /* Parse an (optional) storage-class-specifier.
11356 storage-class-specifier:
11357 auto
11358 register
11359 static
11360 extern
11361 mutable
11363 GNU Extension:
11365 storage-class-specifier:
11366 thread
11368 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11370 static tree
11371 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11373 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11375 case RID_AUTO:
11376 if (cxx_dialect != cxx98)
11377 return NULL_TREE;
11378 /* Fall through for C++98. */
11380 case RID_REGISTER:
11381 case RID_STATIC:
11382 case RID_EXTERN:
11383 case RID_MUTABLE:
11384 case RID_THREAD:
11385 /* Consume the token. */
11386 return cp_lexer_consume_token (parser->lexer)->u.value;
11388 default:
11389 return NULL_TREE;
11393 /* Parse an (optional) function-specifier.
11395 function-specifier:
11396 inline
11397 virtual
11398 explicit
11400 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11401 Updates DECL_SPECS, if it is non-NULL. */
11403 static tree
11404 cp_parser_function_specifier_opt (cp_parser* parser,
11405 cp_decl_specifier_seq *decl_specs)
11407 cp_token *token = cp_lexer_peek_token (parser->lexer);
11408 switch (token->keyword)
11410 case RID_INLINE:
11411 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11412 break;
11414 case RID_VIRTUAL:
11415 /* 14.5.2.3 [temp.mem]
11417 A member function template shall not be virtual. */
11418 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11419 error_at (token->location, "templates may not be %<virtual%>");
11420 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11421 break;
11423 case RID_EXPLICIT:
11424 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11425 break;
11427 default:
11428 return NULL_TREE;
11431 /* Consume the token. */
11432 return cp_lexer_consume_token (parser->lexer)->u.value;
11435 /* Parse a linkage-specification.
11437 linkage-specification:
11438 extern string-literal { declaration-seq [opt] }
11439 extern string-literal declaration */
11441 static void
11442 cp_parser_linkage_specification (cp_parser* parser)
11444 tree linkage;
11446 /* Look for the `extern' keyword. */
11447 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11449 /* Look for the string-literal. */
11450 linkage = cp_parser_string_literal (parser, false, false);
11452 /* Transform the literal into an identifier. If the literal is a
11453 wide-character string, or contains embedded NULs, then we can't
11454 handle it as the user wants. */
11455 if (strlen (TREE_STRING_POINTER (linkage))
11456 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11458 cp_parser_error (parser, "invalid linkage-specification");
11459 /* Assume C++ linkage. */
11460 linkage = lang_name_cplusplus;
11462 else
11463 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11465 /* We're now using the new linkage. */
11466 push_lang_context (linkage);
11468 /* If the next token is a `{', then we're using the first
11469 production. */
11470 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11472 /* Consume the `{' token. */
11473 cp_lexer_consume_token (parser->lexer);
11474 /* Parse the declarations. */
11475 cp_parser_declaration_seq_opt (parser);
11476 /* Look for the closing `}'. */
11477 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11479 /* Otherwise, there's just one declaration. */
11480 else
11482 bool saved_in_unbraced_linkage_specification_p;
11484 saved_in_unbraced_linkage_specification_p
11485 = parser->in_unbraced_linkage_specification_p;
11486 parser->in_unbraced_linkage_specification_p = true;
11487 cp_parser_declaration (parser);
11488 parser->in_unbraced_linkage_specification_p
11489 = saved_in_unbraced_linkage_specification_p;
11492 /* We're done with the linkage-specification. */
11493 pop_lang_context ();
11496 /* Parse a static_assert-declaration.
11498 static_assert-declaration:
11499 static_assert ( constant-expression , string-literal ) ;
11501 If MEMBER_P, this static_assert is a class member. */
11503 static void
11504 cp_parser_static_assert(cp_parser *parser, bool member_p)
11506 tree condition;
11507 tree message;
11508 cp_token *token;
11509 location_t saved_loc;
11510 bool dummy;
11512 /* Peek at the `static_assert' token so we can keep track of exactly
11513 where the static assertion started. */
11514 token = cp_lexer_peek_token (parser->lexer);
11515 saved_loc = token->location;
11517 /* Look for the `static_assert' keyword. */
11518 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11519 RT_STATIC_ASSERT))
11520 return;
11522 /* We know we are in a static assertion; commit to any tentative
11523 parse. */
11524 if (cp_parser_parsing_tentatively (parser))
11525 cp_parser_commit_to_tentative_parse (parser);
11527 /* Parse the `(' starting the static assertion condition. */
11528 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11530 /* Parse the constant-expression. Allow a non-constant expression
11531 here in order to give better diagnostics in finish_static_assert. */
11532 condition =
11533 cp_parser_constant_expression (parser,
11534 /*allow_non_constant_p=*/true,
11535 /*non_constant_p=*/&dummy);
11537 /* Parse the separating `,'. */
11538 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11540 /* Parse the string-literal message. */
11541 message = cp_parser_string_literal (parser,
11542 /*translate=*/false,
11543 /*wide_ok=*/true);
11545 /* A `)' completes the static assertion. */
11546 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11547 cp_parser_skip_to_closing_parenthesis (parser,
11548 /*recovering=*/true,
11549 /*or_comma=*/false,
11550 /*consume_paren=*/true);
11552 /* A semicolon terminates the declaration. */
11553 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11555 /* Complete the static assertion, which may mean either processing
11556 the static assert now or saving it for template instantiation. */
11557 finish_static_assert (condition, message, saved_loc, member_p);
11560 /* Parse the expression in decltype ( expression ). */
11562 static tree
11563 cp_parser_decltype_expr (cp_parser *parser,
11564 bool &id_expression_or_member_access_p)
11566 cp_token *id_expr_start_token;
11567 tree expr;
11569 /* First, try parsing an id-expression. */
11570 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11571 cp_parser_parse_tentatively (parser);
11572 expr = cp_parser_id_expression (parser,
11573 /*template_keyword_p=*/false,
11574 /*check_dependency_p=*/true,
11575 /*template_p=*/NULL,
11576 /*declarator_p=*/false,
11577 /*optional_p=*/false);
11579 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11581 bool non_integral_constant_expression_p = false;
11582 tree id_expression = expr;
11583 cp_id_kind idk;
11584 const char *error_msg;
11586 if (identifier_p (expr))
11587 /* Lookup the name we got back from the id-expression. */
11588 expr = cp_parser_lookup_name (parser, expr,
11589 none_type,
11590 /*is_template=*/false,
11591 /*is_namespace=*/false,
11592 /*check_dependency=*/true,
11593 /*ambiguous_decls=*/NULL,
11594 id_expr_start_token->location);
11596 if (expr
11597 && expr != error_mark_node
11598 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11599 && TREE_CODE (expr) != TYPE_DECL
11600 && (TREE_CODE (expr) != BIT_NOT_EXPR
11601 || !TYPE_P (TREE_OPERAND (expr, 0)))
11602 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11604 /* Complete lookup of the id-expression. */
11605 expr = (finish_id_expression
11606 (id_expression, expr, parser->scope, &idk,
11607 /*integral_constant_expression_p=*/false,
11608 /*allow_non_integral_constant_expression_p=*/true,
11609 &non_integral_constant_expression_p,
11610 /*template_p=*/false,
11611 /*done=*/true,
11612 /*address_p=*/false,
11613 /*template_arg_p=*/false,
11614 &error_msg,
11615 id_expr_start_token->location));
11617 if (expr == error_mark_node)
11618 /* We found an id-expression, but it was something that we
11619 should not have found. This is an error, not something
11620 we can recover from, so note that we found an
11621 id-expression and we'll recover as gracefully as
11622 possible. */
11623 id_expression_or_member_access_p = true;
11626 if (expr
11627 && expr != error_mark_node
11628 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11629 /* We have an id-expression. */
11630 id_expression_or_member_access_p = true;
11633 if (!id_expression_or_member_access_p)
11635 /* Abort the id-expression parse. */
11636 cp_parser_abort_tentative_parse (parser);
11638 /* Parsing tentatively, again. */
11639 cp_parser_parse_tentatively (parser);
11641 /* Parse a class member access. */
11642 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11643 /*cast_p=*/false, /*decltype*/true,
11644 /*member_access_only_p=*/true, NULL);
11646 if (expr
11647 && expr != error_mark_node
11648 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11649 /* We have an id-expression. */
11650 id_expression_or_member_access_p = true;
11653 if (id_expression_or_member_access_p)
11654 /* We have parsed the complete id-expression or member access. */
11655 cp_parser_parse_definitely (parser);
11656 else
11658 /* Abort our attempt to parse an id-expression or member access
11659 expression. */
11660 cp_parser_abort_tentative_parse (parser);
11662 /* Parse a full expression. */
11663 expr = cp_parser_expression (parser, /*cast_p=*/false,
11664 /*decltype*/true, NULL);
11667 return expr;
11670 /* Parse a `decltype' type. Returns the type.
11672 simple-type-specifier:
11673 decltype ( expression )
11674 C++14 proposal:
11675 decltype ( auto ) */
11677 static tree
11678 cp_parser_decltype (cp_parser *parser)
11680 tree expr;
11681 bool id_expression_or_member_access_p = false;
11682 const char *saved_message;
11683 bool saved_integral_constant_expression_p;
11684 bool saved_non_integral_constant_expression_p;
11685 bool saved_greater_than_is_operator_p;
11686 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11688 if (start_token->type == CPP_DECLTYPE)
11690 /* Already parsed. */
11691 cp_lexer_consume_token (parser->lexer);
11692 return start_token->u.value;
11695 /* Look for the `decltype' token. */
11696 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11697 return error_mark_node;
11699 /* Parse the opening `('. */
11700 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11701 return error_mark_node;
11703 /* decltype (auto) */
11704 if (cxx_dialect >= cxx1y
11705 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
11707 cp_lexer_consume_token (parser->lexer);
11708 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11709 return error_mark_node;
11710 expr = make_decltype_auto ();
11711 AUTO_IS_DECLTYPE (expr) = true;
11712 goto rewrite;
11715 /* Types cannot be defined in a `decltype' expression. Save away the
11716 old message. */
11717 saved_message = parser->type_definition_forbidden_message;
11719 /* And create the new one. */
11720 parser->type_definition_forbidden_message
11721 = G_("types may not be defined in %<decltype%> expressions");
11723 /* The restrictions on constant-expressions do not apply inside
11724 decltype expressions. */
11725 saved_integral_constant_expression_p
11726 = parser->integral_constant_expression_p;
11727 saved_non_integral_constant_expression_p
11728 = parser->non_integral_constant_expression_p;
11729 parser->integral_constant_expression_p = false;
11731 /* Within a parenthesized expression, a `>' token is always
11732 the greater-than operator. */
11733 saved_greater_than_is_operator_p
11734 = parser->greater_than_is_operator_p;
11735 parser->greater_than_is_operator_p = true;
11737 /* Do not actually evaluate the expression. */
11738 ++cp_unevaluated_operand;
11740 /* Do not warn about problems with the expression. */
11741 ++c_inhibit_evaluation_warnings;
11743 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
11745 /* Go back to evaluating expressions. */
11746 --cp_unevaluated_operand;
11747 --c_inhibit_evaluation_warnings;
11749 /* The `>' token might be the end of a template-id or
11750 template-parameter-list now. */
11751 parser->greater_than_is_operator_p
11752 = saved_greater_than_is_operator_p;
11754 /* Restore the old message and the integral constant expression
11755 flags. */
11756 parser->type_definition_forbidden_message = saved_message;
11757 parser->integral_constant_expression_p
11758 = saved_integral_constant_expression_p;
11759 parser->non_integral_constant_expression_p
11760 = saved_non_integral_constant_expression_p;
11762 /* Parse to the closing `)'. */
11763 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11765 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11766 /*consume_paren=*/true);
11767 return error_mark_node;
11770 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11771 tf_warning_or_error);
11773 rewrite:
11774 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11775 it again. */
11776 start_token->type = CPP_DECLTYPE;
11777 start_token->u.value = expr;
11778 start_token->keyword = RID_MAX;
11779 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11781 return expr;
11784 /* Special member functions [gram.special] */
11786 /* Parse a conversion-function-id.
11788 conversion-function-id:
11789 operator conversion-type-id
11791 Returns an IDENTIFIER_NODE representing the operator. */
11793 static tree
11794 cp_parser_conversion_function_id (cp_parser* parser)
11796 tree type;
11797 tree saved_scope;
11798 tree saved_qualifying_scope;
11799 tree saved_object_scope;
11800 tree pushed_scope = NULL_TREE;
11802 /* Look for the `operator' token. */
11803 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11804 return error_mark_node;
11805 /* When we parse the conversion-type-id, the current scope will be
11806 reset. However, we need that information in able to look up the
11807 conversion function later, so we save it here. */
11808 saved_scope = parser->scope;
11809 saved_qualifying_scope = parser->qualifying_scope;
11810 saved_object_scope = parser->object_scope;
11811 /* We must enter the scope of the class so that the names of
11812 entities declared within the class are available in the
11813 conversion-type-id. For example, consider:
11815 struct S {
11816 typedef int I;
11817 operator I();
11820 S::operator I() { ... }
11822 In order to see that `I' is a type-name in the definition, we
11823 must be in the scope of `S'. */
11824 if (saved_scope)
11825 pushed_scope = push_scope (saved_scope);
11826 /* Parse the conversion-type-id. */
11827 type = cp_parser_conversion_type_id (parser);
11828 /* Leave the scope of the class, if any. */
11829 if (pushed_scope)
11830 pop_scope (pushed_scope);
11831 /* Restore the saved scope. */
11832 parser->scope = saved_scope;
11833 parser->qualifying_scope = saved_qualifying_scope;
11834 parser->object_scope = saved_object_scope;
11835 /* If the TYPE is invalid, indicate failure. */
11836 if (type == error_mark_node)
11837 return error_mark_node;
11838 return mangle_conv_op_name_for_type (type);
11841 /* Parse a conversion-type-id:
11843 conversion-type-id:
11844 type-specifier-seq conversion-declarator [opt]
11846 Returns the TYPE specified. */
11848 static tree
11849 cp_parser_conversion_type_id (cp_parser* parser)
11851 tree attributes;
11852 cp_decl_specifier_seq type_specifiers;
11853 cp_declarator *declarator;
11854 tree type_specified;
11855 const char *saved_message;
11857 /* Parse the attributes. */
11858 attributes = cp_parser_attributes_opt (parser);
11860 saved_message = parser->type_definition_forbidden_message;
11861 parser->type_definition_forbidden_message
11862 = G_("types may not be defined in a conversion-type-id");
11864 /* Parse the type-specifiers. */
11865 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11866 /*is_trailing_return=*/false,
11867 &type_specifiers);
11869 parser->type_definition_forbidden_message = saved_message;
11871 /* If that didn't work, stop. */
11872 if (type_specifiers.type == error_mark_node)
11873 return error_mark_node;
11874 /* Parse the conversion-declarator. */
11875 declarator = cp_parser_conversion_declarator_opt (parser);
11877 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11878 /*initialized=*/0, &attributes);
11879 if (attributes)
11880 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11882 /* Don't give this error when parsing tentatively. This happens to
11883 work because we always parse this definitively once. */
11884 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11885 && type_uses_auto (type_specified))
11887 if (cxx_dialect < cxx1y)
11889 error ("invalid use of %<auto%> in conversion operator");
11890 return error_mark_node;
11892 else if (template_parm_scope_p ())
11893 warning (0, "use of %<auto%> in member template "
11894 "conversion operator can never be deduced");
11897 return type_specified;
11900 /* Parse an (optional) conversion-declarator.
11902 conversion-declarator:
11903 ptr-operator conversion-declarator [opt]
11907 static cp_declarator *
11908 cp_parser_conversion_declarator_opt (cp_parser* parser)
11910 enum tree_code code;
11911 tree class_type, std_attributes = NULL_TREE;
11912 cp_cv_quals cv_quals;
11914 /* We don't know if there's a ptr-operator next, or not. */
11915 cp_parser_parse_tentatively (parser);
11916 /* Try the ptr-operator. */
11917 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
11918 &std_attributes);
11919 /* If it worked, look for more conversion-declarators. */
11920 if (cp_parser_parse_definitely (parser))
11922 cp_declarator *declarator;
11924 /* Parse another optional declarator. */
11925 declarator = cp_parser_conversion_declarator_opt (parser);
11927 declarator = cp_parser_make_indirect_declarator
11928 (code, class_type, cv_quals, declarator, std_attributes);
11930 return declarator;
11933 return NULL;
11936 /* Parse an (optional) ctor-initializer.
11938 ctor-initializer:
11939 : mem-initializer-list
11941 Returns TRUE iff the ctor-initializer was actually present. */
11943 static bool
11944 cp_parser_ctor_initializer_opt (cp_parser* parser)
11946 /* If the next token is not a `:', then there is no
11947 ctor-initializer. */
11948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11950 /* Do default initialization of any bases and members. */
11951 if (DECL_CONSTRUCTOR_P (current_function_decl))
11952 finish_mem_initializers (NULL_TREE);
11954 return false;
11957 /* Consume the `:' token. */
11958 cp_lexer_consume_token (parser->lexer);
11959 /* And the mem-initializer-list. */
11960 cp_parser_mem_initializer_list (parser);
11962 return true;
11965 /* Parse a mem-initializer-list.
11967 mem-initializer-list:
11968 mem-initializer ... [opt]
11969 mem-initializer ... [opt] , mem-initializer-list */
11971 static void
11972 cp_parser_mem_initializer_list (cp_parser* parser)
11974 tree mem_initializer_list = NULL_TREE;
11975 tree target_ctor = error_mark_node;
11976 cp_token *token = cp_lexer_peek_token (parser->lexer);
11978 /* Let the semantic analysis code know that we are starting the
11979 mem-initializer-list. */
11980 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11981 error_at (token->location,
11982 "only constructors take member initializers");
11984 /* Loop through the list. */
11985 while (true)
11987 tree mem_initializer;
11989 token = cp_lexer_peek_token (parser->lexer);
11990 /* Parse the mem-initializer. */
11991 mem_initializer = cp_parser_mem_initializer (parser);
11992 /* If the next token is a `...', we're expanding member initializers. */
11993 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11995 /* Consume the `...'. */
11996 cp_lexer_consume_token (parser->lexer);
11998 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11999 can be expanded but members cannot. */
12000 if (mem_initializer != error_mark_node
12001 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12003 error_at (token->location,
12004 "cannot expand initializer for member %<%D%>",
12005 TREE_PURPOSE (mem_initializer));
12006 mem_initializer = error_mark_node;
12009 /* Construct the pack expansion type. */
12010 if (mem_initializer != error_mark_node)
12011 mem_initializer = make_pack_expansion (mem_initializer);
12013 if (target_ctor != error_mark_node
12014 && mem_initializer != error_mark_node)
12016 error ("mem-initializer for %qD follows constructor delegation",
12017 TREE_PURPOSE (mem_initializer));
12018 mem_initializer = error_mark_node;
12020 /* Look for a target constructor. */
12021 if (mem_initializer != error_mark_node
12022 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12023 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12025 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12026 if (mem_initializer_list)
12028 error ("constructor delegation follows mem-initializer for %qD",
12029 TREE_PURPOSE (mem_initializer_list));
12030 mem_initializer = error_mark_node;
12032 target_ctor = mem_initializer;
12034 /* Add it to the list, unless it was erroneous. */
12035 if (mem_initializer != error_mark_node)
12037 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12038 mem_initializer_list = mem_initializer;
12040 /* If the next token is not a `,', we're done. */
12041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12042 break;
12043 /* Consume the `,' token. */
12044 cp_lexer_consume_token (parser->lexer);
12047 /* Perform semantic analysis. */
12048 if (DECL_CONSTRUCTOR_P (current_function_decl))
12049 finish_mem_initializers (mem_initializer_list);
12052 /* Parse a mem-initializer.
12054 mem-initializer:
12055 mem-initializer-id ( expression-list [opt] )
12056 mem-initializer-id braced-init-list
12058 GNU extension:
12060 mem-initializer:
12061 ( expression-list [opt] )
12063 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12064 class) or FIELD_DECL (for a non-static data member) to initialize;
12065 the TREE_VALUE is the expression-list. An empty initialization
12066 list is represented by void_list_node. */
12068 static tree
12069 cp_parser_mem_initializer (cp_parser* parser)
12071 tree mem_initializer_id;
12072 tree expression_list;
12073 tree member;
12074 cp_token *token = cp_lexer_peek_token (parser->lexer);
12076 /* Find out what is being initialized. */
12077 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12079 permerror (token->location,
12080 "anachronistic old-style base class initializer");
12081 mem_initializer_id = NULL_TREE;
12083 else
12085 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12086 if (mem_initializer_id == error_mark_node)
12087 return mem_initializer_id;
12089 member = expand_member_init (mem_initializer_id);
12090 if (member && !DECL_P (member))
12091 in_base_initializer = 1;
12093 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12095 bool expr_non_constant_p;
12096 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12097 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12098 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12099 expression_list = build_tree_list (NULL_TREE, expression_list);
12101 else
12103 vec<tree, va_gc> *vec;
12104 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12105 /*cast_p=*/false,
12106 /*allow_expansion_p=*/true,
12107 /*non_constant_p=*/NULL);
12108 if (vec == NULL)
12109 return error_mark_node;
12110 expression_list = build_tree_list_vec (vec);
12111 release_tree_vector (vec);
12114 if (expression_list == error_mark_node)
12115 return error_mark_node;
12116 if (!expression_list)
12117 expression_list = void_type_node;
12119 in_base_initializer = 0;
12121 return member ? build_tree_list (member, expression_list) : error_mark_node;
12124 /* Parse a mem-initializer-id.
12126 mem-initializer-id:
12127 :: [opt] nested-name-specifier [opt] class-name
12128 identifier
12130 Returns a TYPE indicating the class to be initializer for the first
12131 production. Returns an IDENTIFIER_NODE indicating the data member
12132 to be initialized for the second production. */
12134 static tree
12135 cp_parser_mem_initializer_id (cp_parser* parser)
12137 bool global_scope_p;
12138 bool nested_name_specifier_p;
12139 bool template_p = false;
12140 tree id;
12142 cp_token *token = cp_lexer_peek_token (parser->lexer);
12144 /* `typename' is not allowed in this context ([temp.res]). */
12145 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12147 error_at (token->location,
12148 "keyword %<typename%> not allowed in this context (a qualified "
12149 "member initializer is implicitly a type)");
12150 cp_lexer_consume_token (parser->lexer);
12152 /* Look for the optional `::' operator. */
12153 global_scope_p
12154 = (cp_parser_global_scope_opt (parser,
12155 /*current_scope_valid_p=*/false)
12156 != NULL_TREE);
12157 /* Look for the optional nested-name-specifier. The simplest way to
12158 implement:
12160 [temp.res]
12162 The keyword `typename' is not permitted in a base-specifier or
12163 mem-initializer; in these contexts a qualified name that
12164 depends on a template-parameter is implicitly assumed to be a
12165 type name.
12167 is to assume that we have seen the `typename' keyword at this
12168 point. */
12169 nested_name_specifier_p
12170 = (cp_parser_nested_name_specifier_opt (parser,
12171 /*typename_keyword_p=*/true,
12172 /*check_dependency_p=*/true,
12173 /*type_p=*/true,
12174 /*is_declaration=*/true)
12175 != NULL_TREE);
12176 if (nested_name_specifier_p)
12177 template_p = cp_parser_optional_template_keyword (parser);
12178 /* If there is a `::' operator or a nested-name-specifier, then we
12179 are definitely looking for a class-name. */
12180 if (global_scope_p || nested_name_specifier_p)
12181 return cp_parser_class_name (parser,
12182 /*typename_keyword_p=*/true,
12183 /*template_keyword_p=*/template_p,
12184 typename_type,
12185 /*check_dependency_p=*/true,
12186 /*class_head_p=*/false,
12187 /*is_declaration=*/true);
12188 /* Otherwise, we could also be looking for an ordinary identifier. */
12189 cp_parser_parse_tentatively (parser);
12190 /* Try a class-name. */
12191 id = cp_parser_class_name (parser,
12192 /*typename_keyword_p=*/true,
12193 /*template_keyword_p=*/false,
12194 none_type,
12195 /*check_dependency_p=*/true,
12196 /*class_head_p=*/false,
12197 /*is_declaration=*/true);
12198 /* If we found one, we're done. */
12199 if (cp_parser_parse_definitely (parser))
12200 return id;
12201 /* Otherwise, look for an ordinary identifier. */
12202 return cp_parser_identifier (parser);
12205 /* Overloading [gram.over] */
12207 /* Parse an operator-function-id.
12209 operator-function-id:
12210 operator operator
12212 Returns an IDENTIFIER_NODE for the operator which is a
12213 human-readable spelling of the identifier, e.g., `operator +'. */
12215 static tree
12216 cp_parser_operator_function_id (cp_parser* parser)
12218 /* Look for the `operator' keyword. */
12219 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12220 return error_mark_node;
12221 /* And then the name of the operator itself. */
12222 return cp_parser_operator (parser);
12225 /* Return an identifier node for a user-defined literal operator.
12226 The suffix identifier is chained to the operator name identifier. */
12228 static tree
12229 cp_literal_operator_id (const char* name)
12231 tree identifier;
12232 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12233 + strlen (name) + 10);
12234 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12235 identifier = get_identifier (buffer);
12237 return identifier;
12240 /* Parse an operator.
12242 operator:
12243 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12244 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12245 || ++ -- , ->* -> () []
12247 GNU Extensions:
12249 operator:
12250 <? >? <?= >?=
12252 Returns an IDENTIFIER_NODE for the operator which is a
12253 human-readable spelling of the identifier, e.g., `operator +'. */
12255 static tree
12256 cp_parser_operator (cp_parser* parser)
12258 tree id = NULL_TREE;
12259 cp_token *token;
12260 bool bad_encoding_prefix = false;
12261 int string_len = 2;
12263 /* Peek at the next token. */
12264 token = cp_lexer_peek_token (parser->lexer);
12265 /* Figure out which operator we have. */
12266 switch (token->type)
12268 case CPP_KEYWORD:
12270 enum tree_code op;
12272 /* The keyword should be either `new' or `delete'. */
12273 if (token->keyword == RID_NEW)
12274 op = NEW_EXPR;
12275 else if (token->keyword == RID_DELETE)
12276 op = DELETE_EXPR;
12277 else
12278 break;
12280 /* Consume the `new' or `delete' token. */
12281 cp_lexer_consume_token (parser->lexer);
12283 /* Peek at the next token. */
12284 token = cp_lexer_peek_token (parser->lexer);
12285 /* If it's a `[' token then this is the array variant of the
12286 operator. */
12287 if (token->type == CPP_OPEN_SQUARE)
12289 /* Consume the `[' token. */
12290 cp_lexer_consume_token (parser->lexer);
12291 /* Look for the `]' token. */
12292 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12293 id = ansi_opname (op == NEW_EXPR
12294 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12296 /* Otherwise, we have the non-array variant. */
12297 else
12298 id = ansi_opname (op);
12300 return id;
12303 case CPP_PLUS:
12304 id = ansi_opname (PLUS_EXPR);
12305 break;
12307 case CPP_MINUS:
12308 id = ansi_opname (MINUS_EXPR);
12309 break;
12311 case CPP_MULT:
12312 id = ansi_opname (MULT_EXPR);
12313 break;
12315 case CPP_DIV:
12316 id = ansi_opname (TRUNC_DIV_EXPR);
12317 break;
12319 case CPP_MOD:
12320 id = ansi_opname (TRUNC_MOD_EXPR);
12321 break;
12323 case CPP_XOR:
12324 id = ansi_opname (BIT_XOR_EXPR);
12325 break;
12327 case CPP_AND:
12328 id = ansi_opname (BIT_AND_EXPR);
12329 break;
12331 case CPP_OR:
12332 id = ansi_opname (BIT_IOR_EXPR);
12333 break;
12335 case CPP_COMPL:
12336 id = ansi_opname (BIT_NOT_EXPR);
12337 break;
12339 case CPP_NOT:
12340 id = ansi_opname (TRUTH_NOT_EXPR);
12341 break;
12343 case CPP_EQ:
12344 id = ansi_assopname (NOP_EXPR);
12345 break;
12347 case CPP_LESS:
12348 id = ansi_opname (LT_EXPR);
12349 break;
12351 case CPP_GREATER:
12352 id = ansi_opname (GT_EXPR);
12353 break;
12355 case CPP_PLUS_EQ:
12356 id = ansi_assopname (PLUS_EXPR);
12357 break;
12359 case CPP_MINUS_EQ:
12360 id = ansi_assopname (MINUS_EXPR);
12361 break;
12363 case CPP_MULT_EQ:
12364 id = ansi_assopname (MULT_EXPR);
12365 break;
12367 case CPP_DIV_EQ:
12368 id = ansi_assopname (TRUNC_DIV_EXPR);
12369 break;
12371 case CPP_MOD_EQ:
12372 id = ansi_assopname (TRUNC_MOD_EXPR);
12373 break;
12375 case CPP_XOR_EQ:
12376 id = ansi_assopname (BIT_XOR_EXPR);
12377 break;
12379 case CPP_AND_EQ:
12380 id = ansi_assopname (BIT_AND_EXPR);
12381 break;
12383 case CPP_OR_EQ:
12384 id = ansi_assopname (BIT_IOR_EXPR);
12385 break;
12387 case CPP_LSHIFT:
12388 id = ansi_opname (LSHIFT_EXPR);
12389 break;
12391 case CPP_RSHIFT:
12392 id = ansi_opname (RSHIFT_EXPR);
12393 break;
12395 case CPP_LSHIFT_EQ:
12396 id = ansi_assopname (LSHIFT_EXPR);
12397 break;
12399 case CPP_RSHIFT_EQ:
12400 id = ansi_assopname (RSHIFT_EXPR);
12401 break;
12403 case CPP_EQ_EQ:
12404 id = ansi_opname (EQ_EXPR);
12405 break;
12407 case CPP_NOT_EQ:
12408 id = ansi_opname (NE_EXPR);
12409 break;
12411 case CPP_LESS_EQ:
12412 id = ansi_opname (LE_EXPR);
12413 break;
12415 case CPP_GREATER_EQ:
12416 id = ansi_opname (GE_EXPR);
12417 break;
12419 case CPP_AND_AND:
12420 id = ansi_opname (TRUTH_ANDIF_EXPR);
12421 break;
12423 case CPP_OR_OR:
12424 id = ansi_opname (TRUTH_ORIF_EXPR);
12425 break;
12427 case CPP_PLUS_PLUS:
12428 id = ansi_opname (POSTINCREMENT_EXPR);
12429 break;
12431 case CPP_MINUS_MINUS:
12432 id = ansi_opname (PREDECREMENT_EXPR);
12433 break;
12435 case CPP_COMMA:
12436 id = ansi_opname (COMPOUND_EXPR);
12437 break;
12439 case CPP_DEREF_STAR:
12440 id = ansi_opname (MEMBER_REF);
12441 break;
12443 case CPP_DEREF:
12444 id = ansi_opname (COMPONENT_REF);
12445 break;
12447 case CPP_OPEN_PAREN:
12448 /* Consume the `('. */
12449 cp_lexer_consume_token (parser->lexer);
12450 /* Look for the matching `)'. */
12451 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12452 return ansi_opname (CALL_EXPR);
12454 case CPP_OPEN_SQUARE:
12455 /* Consume the `['. */
12456 cp_lexer_consume_token (parser->lexer);
12457 /* Look for the matching `]'. */
12458 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12459 return ansi_opname (ARRAY_REF);
12461 case CPP_WSTRING:
12462 string_len = 3;
12463 case CPP_STRING16:
12464 case CPP_STRING32:
12465 string_len = 5;
12466 case CPP_UTF8STRING:
12467 string_len = 4;
12468 bad_encoding_prefix = true;
12469 case CPP_STRING:
12470 if (cxx_dialect == cxx98)
12471 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12472 if (bad_encoding_prefix)
12473 error ("invalid encoding prefix in literal operator");
12474 if (TREE_STRING_LENGTH (token->u.value) > string_len)
12476 error ("expected empty string after %<operator%> keyword");
12477 return error_mark_node;
12479 /* Consume the string. */
12480 cp_lexer_consume_token (parser->lexer);
12481 /* Look for the suffix identifier. */
12482 token = cp_lexer_peek_token (parser->lexer);
12483 if (token->type == CPP_NAME)
12485 id = cp_parser_identifier (parser);
12486 if (id != error_mark_node)
12488 const char *name = IDENTIFIER_POINTER (id);
12489 return cp_literal_operator_id (name);
12492 else if (token->type == CPP_KEYWORD)
12494 error ("unexpected keyword;"
12495 " remove space between quotes and suffix identifier");
12496 return error_mark_node;
12498 else
12500 error ("expected suffix identifier");
12501 return error_mark_node;
12504 case CPP_WSTRING_USERDEF:
12505 string_len = 3;
12506 case CPP_STRING16_USERDEF:
12507 case CPP_STRING32_USERDEF:
12508 string_len = 5;
12509 case CPP_UTF8STRING_USERDEF:
12510 string_len = 4;
12511 bad_encoding_prefix = true;
12512 case CPP_STRING_USERDEF:
12513 if (cxx_dialect == cxx98)
12514 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12515 if (bad_encoding_prefix)
12516 error ("invalid encoding prefix in literal operator");
12518 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12519 if (TREE_STRING_LENGTH (string_tree) > string_len)
12521 error ("expected empty string after %<operator%> keyword");
12522 return error_mark_node;
12524 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12525 /* Consume the user-defined string literal. */
12526 cp_lexer_consume_token (parser->lexer);
12527 if (id != error_mark_node)
12529 const char *name = IDENTIFIER_POINTER (id);
12530 return cp_literal_operator_id (name);
12532 else
12533 return error_mark_node;
12536 default:
12537 /* Anything else is an error. */
12538 break;
12541 /* If we have selected an identifier, we need to consume the
12542 operator token. */
12543 if (id)
12544 cp_lexer_consume_token (parser->lexer);
12545 /* Otherwise, no valid operator name was present. */
12546 else
12548 cp_parser_error (parser, "expected operator");
12549 id = error_mark_node;
12552 return id;
12555 /* Parse a template-declaration.
12557 template-declaration:
12558 export [opt] template < template-parameter-list > declaration
12560 If MEMBER_P is TRUE, this template-declaration occurs within a
12561 class-specifier.
12563 The grammar rule given by the standard isn't correct. What
12564 is really meant is:
12566 template-declaration:
12567 export [opt] template-parameter-list-seq
12568 decl-specifier-seq [opt] init-declarator [opt] ;
12569 export [opt] template-parameter-list-seq
12570 function-definition
12572 template-parameter-list-seq:
12573 template-parameter-list-seq [opt]
12574 template < template-parameter-list > */
12576 static void
12577 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12579 /* Check for `export'. */
12580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12582 /* Consume the `export' token. */
12583 cp_lexer_consume_token (parser->lexer);
12584 /* Warn that we do not support `export'. */
12585 warning (0, "keyword %<export%> not implemented, and will be ignored");
12588 cp_parser_template_declaration_after_export (parser, member_p);
12591 /* Parse a template-parameter-list.
12593 template-parameter-list:
12594 template-parameter
12595 template-parameter-list , template-parameter
12597 Returns a TREE_LIST. Each node represents a template parameter.
12598 The nodes are connected via their TREE_CHAINs. */
12600 static tree
12601 cp_parser_template_parameter_list (cp_parser* parser)
12603 tree parameter_list = NULL_TREE;
12605 begin_template_parm_list ();
12607 /* The loop below parses the template parms. We first need to know
12608 the total number of template parms to be able to compute proper
12609 canonical types of each dependent type. So after the loop, when
12610 we know the total number of template parms,
12611 end_template_parm_list computes the proper canonical types and
12612 fixes up the dependent types accordingly. */
12613 while (true)
12615 tree parameter;
12616 bool is_non_type;
12617 bool is_parameter_pack;
12618 location_t parm_loc;
12620 /* Parse the template-parameter. */
12621 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12622 parameter = cp_parser_template_parameter (parser,
12623 &is_non_type,
12624 &is_parameter_pack);
12625 /* Add it to the list. */
12626 if (parameter != error_mark_node)
12627 parameter_list = process_template_parm (parameter_list,
12628 parm_loc,
12629 parameter,
12630 is_non_type,
12631 is_parameter_pack);
12632 else
12634 tree err_parm = build_tree_list (parameter, parameter);
12635 parameter_list = chainon (parameter_list, err_parm);
12638 /* If the next token is not a `,', we're done. */
12639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12640 break;
12641 /* Otherwise, consume the `,' token. */
12642 cp_lexer_consume_token (parser->lexer);
12645 return end_template_parm_list (parameter_list);
12648 /* Parse a template-parameter.
12650 template-parameter:
12651 type-parameter
12652 parameter-declaration
12654 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12655 the parameter. The TREE_PURPOSE is the default value, if any.
12656 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12657 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12658 set to true iff this parameter is a parameter pack. */
12660 static tree
12661 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12662 bool *is_parameter_pack)
12664 cp_token *token;
12665 cp_parameter_declarator *parameter_declarator;
12666 cp_declarator *id_declarator;
12667 tree parm;
12669 /* Assume it is a type parameter or a template parameter. */
12670 *is_non_type = false;
12671 /* Assume it not a parameter pack. */
12672 *is_parameter_pack = false;
12673 /* Peek at the next token. */
12674 token = cp_lexer_peek_token (parser->lexer);
12675 /* If it is `class' or `template', we have a type-parameter. */
12676 if (token->keyword == RID_TEMPLATE)
12677 return cp_parser_type_parameter (parser, is_parameter_pack);
12678 /* If it is `class' or `typename' we do not know yet whether it is a
12679 type parameter or a non-type parameter. Consider:
12681 template <typename T, typename T::X X> ...
12685 template <class C, class D*> ...
12687 Here, the first parameter is a type parameter, and the second is
12688 a non-type parameter. We can tell by looking at the token after
12689 the identifier -- if it is a `,', `=', or `>' then we have a type
12690 parameter. */
12691 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12693 /* Peek at the token after `class' or `typename'. */
12694 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12695 /* If it's an ellipsis, we have a template type parameter
12696 pack. */
12697 if (token->type == CPP_ELLIPSIS)
12698 return cp_parser_type_parameter (parser, is_parameter_pack);
12699 /* If it's an identifier, skip it. */
12700 if (token->type == CPP_NAME)
12701 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12702 /* Now, see if the token looks like the end of a template
12703 parameter. */
12704 if (token->type == CPP_COMMA
12705 || token->type == CPP_EQ
12706 || token->type == CPP_GREATER)
12707 return cp_parser_type_parameter (parser, is_parameter_pack);
12710 /* Otherwise, it is a non-type parameter.
12712 [temp.param]
12714 When parsing a default template-argument for a non-type
12715 template-parameter, the first non-nested `>' is taken as the end
12716 of the template parameter-list rather than a greater-than
12717 operator. */
12718 *is_non_type = true;
12719 parameter_declarator
12720 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12721 /*parenthesized_p=*/NULL);
12723 /* If the parameter declaration is marked as a parameter pack, set
12724 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12725 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12726 grokdeclarator. */
12727 if (parameter_declarator
12728 && parameter_declarator->declarator
12729 && parameter_declarator->declarator->parameter_pack_p)
12731 *is_parameter_pack = true;
12732 parameter_declarator->declarator->parameter_pack_p = false;
12735 if (parameter_declarator
12736 && parameter_declarator->default_argument)
12738 /* Can happen in some cases of erroneous input (c++/34892). */
12739 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12740 /* Consume the `...' for better error recovery. */
12741 cp_lexer_consume_token (parser->lexer);
12743 /* If the next token is an ellipsis, and we don't already have it
12744 marked as a parameter pack, then we have a parameter pack (that
12745 has no declarator). */
12746 else if (!*is_parameter_pack
12747 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12748 && (declarator_can_be_parameter_pack
12749 (parameter_declarator->declarator)))
12751 /* Consume the `...'. */
12752 cp_lexer_consume_token (parser->lexer);
12753 maybe_warn_variadic_templates ();
12755 *is_parameter_pack = true;
12757 /* We might end up with a pack expansion as the type of the non-type
12758 template parameter, in which case this is a non-type template
12759 parameter pack. */
12760 else if (parameter_declarator
12761 && parameter_declarator->decl_specifiers.type
12762 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12764 *is_parameter_pack = true;
12765 parameter_declarator->decl_specifiers.type =
12766 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12769 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12771 /* Parameter packs cannot have default arguments. However, a
12772 user may try to do so, so we'll parse them and give an
12773 appropriate diagnostic here. */
12775 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12777 /* Find the name of the parameter pack. */
12778 id_declarator = parameter_declarator->declarator;
12779 while (id_declarator && id_declarator->kind != cdk_id)
12780 id_declarator = id_declarator->declarator;
12782 if (id_declarator && id_declarator->kind == cdk_id)
12783 error_at (start_token->location,
12784 "template parameter pack %qD cannot have a default argument",
12785 id_declarator->u.id.unqualified_name);
12786 else
12787 error_at (start_token->location,
12788 "template parameter pack cannot have a default argument");
12790 /* Parse the default argument, but throw away the result. */
12791 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12794 parm = grokdeclarator (parameter_declarator->declarator,
12795 &parameter_declarator->decl_specifiers,
12796 TPARM, /*initialized=*/0,
12797 /*attrlist=*/NULL);
12798 if (parm == error_mark_node)
12799 return error_mark_node;
12801 return build_tree_list (parameter_declarator->default_argument, parm);
12804 /* Parse a type-parameter.
12806 type-parameter:
12807 class identifier [opt]
12808 class identifier [opt] = type-id
12809 typename identifier [opt]
12810 typename identifier [opt] = type-id
12811 template < template-parameter-list > class identifier [opt]
12812 template < template-parameter-list > class identifier [opt]
12813 = id-expression
12815 GNU Extension (variadic templates):
12817 type-parameter:
12818 class ... identifier [opt]
12819 typename ... identifier [opt]
12821 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12822 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12823 the declaration of the parameter.
12825 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12827 static tree
12828 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12830 cp_token *token;
12831 tree parameter;
12833 /* Look for a keyword to tell us what kind of parameter this is. */
12834 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12835 if (!token)
12836 return error_mark_node;
12838 switch (token->keyword)
12840 case RID_CLASS:
12841 case RID_TYPENAME:
12843 tree identifier;
12844 tree default_argument;
12846 /* If the next token is an ellipsis, we have a template
12847 argument pack. */
12848 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12850 /* Consume the `...' token. */
12851 cp_lexer_consume_token (parser->lexer);
12852 maybe_warn_variadic_templates ();
12854 *is_parameter_pack = true;
12857 /* If the next token is an identifier, then it names the
12858 parameter. */
12859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12860 identifier = cp_parser_identifier (parser);
12861 else
12862 identifier = NULL_TREE;
12864 /* Create the parameter. */
12865 parameter = finish_template_type_parm (class_type_node, identifier);
12867 /* If the next token is an `=', we have a default argument. */
12868 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12870 /* Consume the `=' token. */
12871 cp_lexer_consume_token (parser->lexer);
12872 /* Parse the default-argument. */
12873 push_deferring_access_checks (dk_no_deferred);
12874 default_argument = cp_parser_type_id (parser);
12876 /* Template parameter packs cannot have default
12877 arguments. */
12878 if (*is_parameter_pack)
12880 if (identifier)
12881 error_at (token->location,
12882 "template parameter pack %qD cannot have a "
12883 "default argument", identifier);
12884 else
12885 error_at (token->location,
12886 "template parameter packs cannot have "
12887 "default arguments");
12888 default_argument = NULL_TREE;
12890 pop_deferring_access_checks ();
12892 else
12893 default_argument = NULL_TREE;
12895 /* Create the combined representation of the parameter and the
12896 default argument. */
12897 parameter = build_tree_list (default_argument, parameter);
12899 break;
12901 case RID_TEMPLATE:
12903 tree identifier;
12904 tree default_argument;
12906 /* Look for the `<'. */
12907 cp_parser_require (parser, CPP_LESS, RT_LESS);
12908 /* Parse the template-parameter-list. */
12909 cp_parser_template_parameter_list (parser);
12910 /* Look for the `>'. */
12911 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12912 /* Look for the `class' keyword. */
12913 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12914 /* If the next token is an ellipsis, we have a template
12915 argument pack. */
12916 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12918 /* Consume the `...' token. */
12919 cp_lexer_consume_token (parser->lexer);
12920 maybe_warn_variadic_templates ();
12922 *is_parameter_pack = true;
12924 /* If the next token is an `=', then there is a
12925 default-argument. If the next token is a `>', we are at
12926 the end of the parameter-list. If the next token is a `,',
12927 then we are at the end of this parameter. */
12928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12929 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12930 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12932 identifier = cp_parser_identifier (parser);
12933 /* Treat invalid names as if the parameter were nameless. */
12934 if (identifier == error_mark_node)
12935 identifier = NULL_TREE;
12937 else
12938 identifier = NULL_TREE;
12940 /* Create the template parameter. */
12941 parameter = finish_template_template_parm (class_type_node,
12942 identifier);
12944 /* If the next token is an `=', then there is a
12945 default-argument. */
12946 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12948 bool is_template;
12950 /* Consume the `='. */
12951 cp_lexer_consume_token (parser->lexer);
12952 /* Parse the id-expression. */
12953 push_deferring_access_checks (dk_no_deferred);
12954 /* save token before parsing the id-expression, for error
12955 reporting */
12956 token = cp_lexer_peek_token (parser->lexer);
12957 default_argument
12958 = cp_parser_id_expression (parser,
12959 /*template_keyword_p=*/false,
12960 /*check_dependency_p=*/true,
12961 /*template_p=*/&is_template,
12962 /*declarator_p=*/false,
12963 /*optional_p=*/false);
12964 if (TREE_CODE (default_argument) == TYPE_DECL)
12965 /* If the id-expression was a template-id that refers to
12966 a template-class, we already have the declaration here,
12967 so no further lookup is needed. */
12969 else
12970 /* Look up the name. */
12971 default_argument
12972 = cp_parser_lookup_name (parser, default_argument,
12973 none_type,
12974 /*is_template=*/is_template,
12975 /*is_namespace=*/false,
12976 /*check_dependency=*/true,
12977 /*ambiguous_decls=*/NULL,
12978 token->location);
12979 /* See if the default argument is valid. */
12980 default_argument
12981 = check_template_template_default_arg (default_argument);
12983 /* Template parameter packs cannot have default
12984 arguments. */
12985 if (*is_parameter_pack)
12987 if (identifier)
12988 error_at (token->location,
12989 "template parameter pack %qD cannot "
12990 "have a default argument",
12991 identifier);
12992 else
12993 error_at (token->location, "template parameter packs cannot "
12994 "have default arguments");
12995 default_argument = NULL_TREE;
12997 pop_deferring_access_checks ();
12999 else
13000 default_argument = NULL_TREE;
13002 /* Create the combined representation of the parameter and the
13003 default argument. */
13004 parameter = build_tree_list (default_argument, parameter);
13006 break;
13008 default:
13009 gcc_unreachable ();
13010 break;
13013 return parameter;
13016 /* Parse a template-id.
13018 template-id:
13019 template-name < template-argument-list [opt] >
13021 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13022 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13023 returned. Otherwise, if the template-name names a function, or set
13024 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13025 names a class, returns a TYPE_DECL for the specialization.
13027 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13028 uninstantiated templates. */
13030 static tree
13031 cp_parser_template_id (cp_parser *parser,
13032 bool template_keyword_p,
13033 bool check_dependency_p,
13034 enum tag_types tag_type,
13035 bool is_declaration)
13037 int i;
13038 tree templ;
13039 tree arguments;
13040 tree template_id;
13041 cp_token_position start_of_id = 0;
13042 deferred_access_check *chk;
13043 vec<deferred_access_check, va_gc> *access_check;
13044 cp_token *next_token = NULL, *next_token_2 = NULL;
13045 bool is_identifier;
13047 /* If the next token corresponds to a template-id, there is no need
13048 to reparse it. */
13049 next_token = cp_lexer_peek_token (parser->lexer);
13050 if (next_token->type == CPP_TEMPLATE_ID)
13052 struct tree_check *check_value;
13054 /* Get the stored value. */
13055 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13056 /* Perform any access checks that were deferred. */
13057 access_check = check_value->checks;
13058 if (access_check)
13060 FOR_EACH_VEC_ELT (*access_check, i, chk)
13061 perform_or_defer_access_check (chk->binfo,
13062 chk->decl,
13063 chk->diag_decl,
13064 tf_warning_or_error);
13066 /* Return the stored value. */
13067 return check_value->value;
13070 /* Avoid performing name lookup if there is no possibility of
13071 finding a template-id. */
13072 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13073 || (next_token->type == CPP_NAME
13074 && !cp_parser_nth_token_starts_template_argument_list_p
13075 (parser, 2)))
13077 cp_parser_error (parser, "expected template-id");
13078 return error_mark_node;
13081 /* Remember where the template-id starts. */
13082 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13083 start_of_id = cp_lexer_token_position (parser->lexer, false);
13085 push_deferring_access_checks (dk_deferred);
13087 /* Parse the template-name. */
13088 is_identifier = false;
13089 templ = cp_parser_template_name (parser, template_keyword_p,
13090 check_dependency_p,
13091 is_declaration,
13092 tag_type,
13093 &is_identifier);
13094 if (templ == error_mark_node || is_identifier)
13096 pop_deferring_access_checks ();
13097 return templ;
13100 /* If we find the sequence `[:' after a template-name, it's probably
13101 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13102 parse correctly the argument list. */
13103 next_token = cp_lexer_peek_token (parser->lexer);
13104 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13105 if (next_token->type == CPP_OPEN_SQUARE
13106 && next_token->flags & DIGRAPH
13107 && next_token_2->type == CPP_COLON
13108 && !(next_token_2->flags & PREV_WHITE))
13110 cp_parser_parse_tentatively (parser);
13111 /* Change `:' into `::'. */
13112 next_token_2->type = CPP_SCOPE;
13113 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13114 CPP_LESS. */
13115 cp_lexer_consume_token (parser->lexer);
13117 /* Parse the arguments. */
13118 arguments = cp_parser_enclosed_template_argument_list (parser);
13119 if (!cp_parser_parse_definitely (parser))
13121 /* If we couldn't parse an argument list, then we revert our changes
13122 and return simply an error. Maybe this is not a template-id
13123 after all. */
13124 next_token_2->type = CPP_COLON;
13125 cp_parser_error (parser, "expected %<<%>");
13126 pop_deferring_access_checks ();
13127 return error_mark_node;
13129 /* Otherwise, emit an error about the invalid digraph, but continue
13130 parsing because we got our argument list. */
13131 if (permerror (next_token->location,
13132 "%<<::%> cannot begin a template-argument list"))
13134 static bool hint = false;
13135 inform (next_token->location,
13136 "%<<:%> is an alternate spelling for %<[%>."
13137 " Insert whitespace between %<<%> and %<::%>");
13138 if (!hint && !flag_permissive)
13140 inform (next_token->location, "(if you use %<-fpermissive%> "
13141 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13142 "accept your code)");
13143 hint = true;
13147 else
13149 /* Look for the `<' that starts the template-argument-list. */
13150 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13152 pop_deferring_access_checks ();
13153 return error_mark_node;
13155 /* Parse the arguments. */
13156 arguments = cp_parser_enclosed_template_argument_list (parser);
13159 /* Build a representation of the specialization. */
13160 if (identifier_p (templ))
13161 template_id = build_min_nt_loc (next_token->location,
13162 TEMPLATE_ID_EXPR,
13163 templ, arguments);
13164 else if (DECL_TYPE_TEMPLATE_P (templ)
13165 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13167 bool entering_scope;
13168 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13169 template (rather than some instantiation thereof) only if
13170 is not nested within some other construct. For example, in
13171 "template <typename T> void f(T) { A<T>::", A<T> is just an
13172 instantiation of A. */
13173 entering_scope = (template_parm_scope_p ()
13174 && cp_lexer_next_token_is (parser->lexer,
13175 CPP_SCOPE));
13176 template_id
13177 = finish_template_type (templ, arguments, entering_scope);
13179 else
13181 /* If it's not a class-template or a template-template, it should be
13182 a function-template. */
13183 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13184 || TREE_CODE (templ) == OVERLOAD
13185 || BASELINK_P (templ)));
13187 template_id = lookup_template_function (templ, arguments);
13190 /* If parsing tentatively, replace the sequence of tokens that makes
13191 up the template-id with a CPP_TEMPLATE_ID token. That way,
13192 should we re-parse the token stream, we will not have to repeat
13193 the effort required to do the parse, nor will we issue duplicate
13194 error messages about problems during instantiation of the
13195 template. */
13196 if (start_of_id)
13198 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13200 /* Reset the contents of the START_OF_ID token. */
13201 token->type = CPP_TEMPLATE_ID;
13202 /* Retrieve any deferred checks. Do not pop this access checks yet
13203 so the memory will not be reclaimed during token replacing below. */
13204 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13205 token->u.tree_check_value->value = template_id;
13206 token->u.tree_check_value->checks = get_deferred_access_checks ();
13207 token->keyword = RID_MAX;
13209 /* Purge all subsequent tokens. */
13210 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13212 /* ??? Can we actually assume that, if template_id ==
13213 error_mark_node, we will have issued a diagnostic to the
13214 user, as opposed to simply marking the tentative parse as
13215 failed? */
13216 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13217 error_at (token->location, "parse error in template argument list");
13220 pop_to_parent_deferring_access_checks ();
13221 return template_id;
13224 /* Parse a template-name.
13226 template-name:
13227 identifier
13229 The standard should actually say:
13231 template-name:
13232 identifier
13233 operator-function-id
13235 A defect report has been filed about this issue.
13237 A conversion-function-id cannot be a template name because they cannot
13238 be part of a template-id. In fact, looking at this code:
13240 a.operator K<int>()
13242 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13243 It is impossible to call a templated conversion-function-id with an
13244 explicit argument list, since the only allowed template parameter is
13245 the type to which it is converting.
13247 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13248 `template' keyword, in a construction like:
13250 T::template f<3>()
13252 In that case `f' is taken to be a template-name, even though there
13253 is no way of knowing for sure.
13255 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13256 name refers to a set of overloaded functions, at least one of which
13257 is a template, or an IDENTIFIER_NODE with the name of the template,
13258 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13259 names are looked up inside uninstantiated templates. */
13261 static tree
13262 cp_parser_template_name (cp_parser* parser,
13263 bool template_keyword_p,
13264 bool check_dependency_p,
13265 bool is_declaration,
13266 enum tag_types tag_type,
13267 bool *is_identifier)
13269 tree identifier;
13270 tree decl;
13271 tree fns;
13272 cp_token *token = cp_lexer_peek_token (parser->lexer);
13274 /* If the next token is `operator', then we have either an
13275 operator-function-id or a conversion-function-id. */
13276 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13278 /* We don't know whether we're looking at an
13279 operator-function-id or a conversion-function-id. */
13280 cp_parser_parse_tentatively (parser);
13281 /* Try an operator-function-id. */
13282 identifier = cp_parser_operator_function_id (parser);
13283 /* If that didn't work, try a conversion-function-id. */
13284 if (!cp_parser_parse_definitely (parser))
13286 cp_parser_error (parser, "expected template-name");
13287 return error_mark_node;
13290 /* Look for the identifier. */
13291 else
13292 identifier = cp_parser_identifier (parser);
13294 /* If we didn't find an identifier, we don't have a template-id. */
13295 if (identifier == error_mark_node)
13296 return error_mark_node;
13298 /* If the name immediately followed the `template' keyword, then it
13299 is a template-name. However, if the next token is not `<', then
13300 we do not treat it as a template-name, since it is not being used
13301 as part of a template-id. This enables us to handle constructs
13302 like:
13304 template <typename T> struct S { S(); };
13305 template <typename T> S<T>::S();
13307 correctly. We would treat `S' as a template -- if it were `S<T>'
13308 -- but we do not if there is no `<'. */
13310 if (processing_template_decl
13311 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13313 /* In a declaration, in a dependent context, we pretend that the
13314 "template" keyword was present in order to improve error
13315 recovery. For example, given:
13317 template <typename T> void f(T::X<int>);
13319 we want to treat "X<int>" as a template-id. */
13320 if (is_declaration
13321 && !template_keyword_p
13322 && parser->scope && TYPE_P (parser->scope)
13323 && check_dependency_p
13324 && dependent_scope_p (parser->scope)
13325 /* Do not do this for dtors (or ctors), since they never
13326 need the template keyword before their name. */
13327 && !constructor_name_p (identifier, parser->scope))
13329 cp_token_position start = 0;
13331 /* Explain what went wrong. */
13332 error_at (token->location, "non-template %qD used as template",
13333 identifier);
13334 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13335 parser->scope, identifier);
13336 /* If parsing tentatively, find the location of the "<" token. */
13337 if (cp_parser_simulate_error (parser))
13338 start = cp_lexer_token_position (parser->lexer, true);
13339 /* Parse the template arguments so that we can issue error
13340 messages about them. */
13341 cp_lexer_consume_token (parser->lexer);
13342 cp_parser_enclosed_template_argument_list (parser);
13343 /* Skip tokens until we find a good place from which to
13344 continue parsing. */
13345 cp_parser_skip_to_closing_parenthesis (parser,
13346 /*recovering=*/true,
13347 /*or_comma=*/true,
13348 /*consume_paren=*/false);
13349 /* If parsing tentatively, permanently remove the
13350 template argument list. That will prevent duplicate
13351 error messages from being issued about the missing
13352 "template" keyword. */
13353 if (start)
13354 cp_lexer_purge_tokens_after (parser->lexer, start);
13355 if (is_identifier)
13356 *is_identifier = true;
13357 return identifier;
13360 /* If the "template" keyword is present, then there is generally
13361 no point in doing name-lookup, so we just return IDENTIFIER.
13362 But, if the qualifying scope is non-dependent then we can
13363 (and must) do name-lookup normally. */
13364 if (template_keyword_p
13365 && (!parser->scope
13366 || (TYPE_P (parser->scope)
13367 && dependent_type_p (parser->scope))))
13368 return identifier;
13371 /* Look up the name. */
13372 decl = cp_parser_lookup_name (parser, identifier,
13373 tag_type,
13374 /*is_template=*/true,
13375 /*is_namespace=*/false,
13376 check_dependency_p,
13377 /*ambiguous_decls=*/NULL,
13378 token->location);
13380 /* If DECL is a template, then the name was a template-name. */
13381 if (TREE_CODE (decl) == TEMPLATE_DECL)
13383 else
13385 tree fn = NULL_TREE;
13387 /* The standard does not explicitly indicate whether a name that
13388 names a set of overloaded declarations, some of which are
13389 templates, is a template-name. However, such a name should
13390 be a template-name; otherwise, there is no way to form a
13391 template-id for the overloaded templates. */
13392 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13393 if (TREE_CODE (fns) == OVERLOAD)
13394 for (fn = fns; fn; fn = OVL_NEXT (fn))
13395 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13396 break;
13398 if (!fn)
13400 /* The name does not name a template. */
13401 cp_parser_error (parser, "expected template-name");
13402 return error_mark_node;
13406 /* If DECL is dependent, and refers to a function, then just return
13407 its name; we will look it up again during template instantiation. */
13408 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13410 tree scope = ovl_scope (decl);
13411 if (TYPE_P (scope) && dependent_type_p (scope))
13412 return identifier;
13415 return decl;
13418 /* Parse a template-argument-list.
13420 template-argument-list:
13421 template-argument ... [opt]
13422 template-argument-list , template-argument ... [opt]
13424 Returns a TREE_VEC containing the arguments. */
13426 static tree
13427 cp_parser_template_argument_list (cp_parser* parser)
13429 tree fixed_args[10];
13430 unsigned n_args = 0;
13431 unsigned alloced = 10;
13432 tree *arg_ary = fixed_args;
13433 tree vec;
13434 bool saved_in_template_argument_list_p;
13435 bool saved_ice_p;
13436 bool saved_non_ice_p;
13438 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13439 parser->in_template_argument_list_p = true;
13440 /* Even if the template-id appears in an integral
13441 constant-expression, the contents of the argument list do
13442 not. */
13443 saved_ice_p = parser->integral_constant_expression_p;
13444 parser->integral_constant_expression_p = false;
13445 saved_non_ice_p = parser->non_integral_constant_expression_p;
13446 parser->non_integral_constant_expression_p = false;
13448 /* Parse the arguments. */
13451 tree argument;
13453 if (n_args)
13454 /* Consume the comma. */
13455 cp_lexer_consume_token (parser->lexer);
13457 /* Parse the template-argument. */
13458 argument = cp_parser_template_argument (parser);
13460 /* If the next token is an ellipsis, we're expanding a template
13461 argument pack. */
13462 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13464 if (argument == error_mark_node)
13466 cp_token *token = cp_lexer_peek_token (parser->lexer);
13467 error_at (token->location,
13468 "expected parameter pack before %<...%>");
13470 /* Consume the `...' token. */
13471 cp_lexer_consume_token (parser->lexer);
13473 /* Make the argument into a TYPE_PACK_EXPANSION or
13474 EXPR_PACK_EXPANSION. */
13475 argument = make_pack_expansion (argument);
13478 if (n_args == alloced)
13480 alloced *= 2;
13482 if (arg_ary == fixed_args)
13484 arg_ary = XNEWVEC (tree, alloced);
13485 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13487 else
13488 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13490 arg_ary[n_args++] = argument;
13492 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13494 vec = make_tree_vec (n_args);
13496 while (n_args--)
13497 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13499 if (arg_ary != fixed_args)
13500 free (arg_ary);
13501 parser->non_integral_constant_expression_p = saved_non_ice_p;
13502 parser->integral_constant_expression_p = saved_ice_p;
13503 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13504 #ifdef ENABLE_CHECKING
13505 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13506 #endif
13507 return vec;
13510 /* Parse a template-argument.
13512 template-argument:
13513 assignment-expression
13514 type-id
13515 id-expression
13517 The representation is that of an assignment-expression, type-id, or
13518 id-expression -- except that the qualified id-expression is
13519 evaluated, so that the value returned is either a DECL or an
13520 OVERLOAD.
13522 Although the standard says "assignment-expression", it forbids
13523 throw-expressions or assignments in the template argument.
13524 Therefore, we use "conditional-expression" instead. */
13526 static tree
13527 cp_parser_template_argument (cp_parser* parser)
13529 tree argument;
13530 bool template_p;
13531 bool address_p;
13532 bool maybe_type_id = false;
13533 cp_token *token = NULL, *argument_start_token = NULL;
13534 location_t loc = 0;
13535 cp_id_kind idk;
13537 /* There's really no way to know what we're looking at, so we just
13538 try each alternative in order.
13540 [temp.arg]
13542 In a template-argument, an ambiguity between a type-id and an
13543 expression is resolved to a type-id, regardless of the form of
13544 the corresponding template-parameter.
13546 Therefore, we try a type-id first. */
13547 cp_parser_parse_tentatively (parser);
13548 argument = cp_parser_template_type_arg (parser);
13549 /* If there was no error parsing the type-id but the next token is a
13550 '>>', our behavior depends on which dialect of C++ we're
13551 parsing. In C++98, we probably found a typo for '> >'. But there
13552 are type-id which are also valid expressions. For instance:
13554 struct X { int operator >> (int); };
13555 template <int V> struct Foo {};
13556 Foo<X () >> 5> r;
13558 Here 'X()' is a valid type-id of a function type, but the user just
13559 wanted to write the expression "X() >> 5". Thus, we remember that we
13560 found a valid type-id, but we still try to parse the argument as an
13561 expression to see what happens.
13563 In C++0x, the '>>' will be considered two separate '>'
13564 tokens. */
13565 if (!cp_parser_error_occurred (parser)
13566 && cxx_dialect == cxx98
13567 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13569 maybe_type_id = true;
13570 cp_parser_abort_tentative_parse (parser);
13572 else
13574 /* If the next token isn't a `,' or a `>', then this argument wasn't
13575 really finished. This means that the argument is not a valid
13576 type-id. */
13577 if (!cp_parser_next_token_ends_template_argument_p (parser))
13578 cp_parser_error (parser, "expected template-argument");
13579 /* If that worked, we're done. */
13580 if (cp_parser_parse_definitely (parser))
13581 return argument;
13583 /* We're still not sure what the argument will be. */
13584 cp_parser_parse_tentatively (parser);
13585 /* Try a template. */
13586 argument_start_token = cp_lexer_peek_token (parser->lexer);
13587 argument = cp_parser_id_expression (parser,
13588 /*template_keyword_p=*/false,
13589 /*check_dependency_p=*/true,
13590 &template_p,
13591 /*declarator_p=*/false,
13592 /*optional_p=*/false);
13593 /* If the next token isn't a `,' or a `>', then this argument wasn't
13594 really finished. */
13595 if (!cp_parser_next_token_ends_template_argument_p (parser))
13596 cp_parser_error (parser, "expected template-argument");
13597 if (!cp_parser_error_occurred (parser))
13599 /* Figure out what is being referred to. If the id-expression
13600 was for a class template specialization, then we will have a
13601 TYPE_DECL at this point. There is no need to do name lookup
13602 at this point in that case. */
13603 if (TREE_CODE (argument) != TYPE_DECL)
13604 argument = cp_parser_lookup_name (parser, argument,
13605 none_type,
13606 /*is_template=*/template_p,
13607 /*is_namespace=*/false,
13608 /*check_dependency=*/true,
13609 /*ambiguous_decls=*/NULL,
13610 argument_start_token->location);
13611 if (TREE_CODE (argument) != TEMPLATE_DECL
13612 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13613 cp_parser_error (parser, "expected template-name");
13615 if (cp_parser_parse_definitely (parser))
13616 return argument;
13617 /* It must be a non-type argument. There permitted cases are given
13618 in [temp.arg.nontype]:
13620 -- an integral constant-expression of integral or enumeration
13621 type; or
13623 -- the name of a non-type template-parameter; or
13625 -- the name of an object or function with external linkage...
13627 -- the address of an object or function with external linkage...
13629 -- a pointer to member... */
13630 /* Look for a non-type template parameter. */
13631 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13633 cp_parser_parse_tentatively (parser);
13634 argument = cp_parser_primary_expression (parser,
13635 /*address_p=*/false,
13636 /*cast_p=*/false,
13637 /*template_arg_p=*/true,
13638 &idk);
13639 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13640 || !cp_parser_next_token_ends_template_argument_p (parser))
13641 cp_parser_simulate_error (parser);
13642 if (cp_parser_parse_definitely (parser))
13643 return argument;
13646 /* If the next token is "&", the argument must be the address of an
13647 object or function with external linkage. */
13648 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13649 if (address_p)
13651 loc = cp_lexer_peek_token (parser->lexer)->location;
13652 cp_lexer_consume_token (parser->lexer);
13654 /* See if we might have an id-expression. */
13655 token = cp_lexer_peek_token (parser->lexer);
13656 if (token->type == CPP_NAME
13657 || token->keyword == RID_OPERATOR
13658 || token->type == CPP_SCOPE
13659 || token->type == CPP_TEMPLATE_ID
13660 || token->type == CPP_NESTED_NAME_SPECIFIER)
13662 cp_parser_parse_tentatively (parser);
13663 argument = cp_parser_primary_expression (parser,
13664 address_p,
13665 /*cast_p=*/false,
13666 /*template_arg_p=*/true,
13667 &idk);
13668 if (cp_parser_error_occurred (parser)
13669 || !cp_parser_next_token_ends_template_argument_p (parser))
13670 cp_parser_abort_tentative_parse (parser);
13671 else
13673 tree probe;
13675 if (INDIRECT_REF_P (argument))
13677 gcc_assert (REFERENCE_REF_P (argument));
13678 argument = TREE_OPERAND (argument, 0);
13681 /* If we're in a template, we represent a qualified-id referring
13682 to a static data member as a SCOPE_REF even if the scope isn't
13683 dependent so that we can check access control later. */
13684 probe = argument;
13685 if (TREE_CODE (probe) == SCOPE_REF)
13686 probe = TREE_OPERAND (probe, 1);
13687 if (VAR_P (probe))
13689 /* A variable without external linkage might still be a
13690 valid constant-expression, so no error is issued here
13691 if the external-linkage check fails. */
13692 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13693 cp_parser_simulate_error (parser);
13695 else if (is_overloaded_fn (argument))
13696 /* All overloaded functions are allowed; if the external
13697 linkage test does not pass, an error will be issued
13698 later. */
13700 else if (address_p
13701 && (TREE_CODE (argument) == OFFSET_REF
13702 || TREE_CODE (argument) == SCOPE_REF))
13703 /* A pointer-to-member. */
13705 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13707 else
13708 cp_parser_simulate_error (parser);
13710 if (cp_parser_parse_definitely (parser))
13712 if (address_p)
13713 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13714 tf_warning_or_error);
13715 return argument;
13719 /* If the argument started with "&", there are no other valid
13720 alternatives at this point. */
13721 if (address_p)
13723 cp_parser_error (parser, "invalid non-type template argument");
13724 return error_mark_node;
13727 /* If the argument wasn't successfully parsed as a type-id followed
13728 by '>>', the argument can only be a constant expression now.
13729 Otherwise, we try parsing the constant-expression tentatively,
13730 because the argument could really be a type-id. */
13731 if (maybe_type_id)
13732 cp_parser_parse_tentatively (parser);
13733 argument = cp_parser_constant_expression (parser,
13734 /*allow_non_constant_p=*/false,
13735 /*non_constant_p=*/NULL);
13736 if (!maybe_type_id)
13737 return argument;
13738 if (!cp_parser_next_token_ends_template_argument_p (parser))
13739 cp_parser_error (parser, "expected template-argument");
13740 if (cp_parser_parse_definitely (parser))
13741 return argument;
13742 /* We did our best to parse the argument as a non type-id, but that
13743 was the only alternative that matched (albeit with a '>' after
13744 it). We can assume it's just a typo from the user, and a
13745 diagnostic will then be issued. */
13746 return cp_parser_template_type_arg (parser);
13749 /* Parse an explicit-instantiation.
13751 explicit-instantiation:
13752 template declaration
13754 Although the standard says `declaration', what it really means is:
13756 explicit-instantiation:
13757 template decl-specifier-seq [opt] declarator [opt] ;
13759 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13760 supposed to be allowed. A defect report has been filed about this
13761 issue.
13763 GNU Extension:
13765 explicit-instantiation:
13766 storage-class-specifier template
13767 decl-specifier-seq [opt] declarator [opt] ;
13768 function-specifier template
13769 decl-specifier-seq [opt] declarator [opt] ; */
13771 static void
13772 cp_parser_explicit_instantiation (cp_parser* parser)
13774 int declares_class_or_enum;
13775 cp_decl_specifier_seq decl_specifiers;
13776 tree extension_specifier = NULL_TREE;
13778 timevar_push (TV_TEMPLATE_INST);
13780 /* Look for an (optional) storage-class-specifier or
13781 function-specifier. */
13782 if (cp_parser_allow_gnu_extensions_p (parser))
13784 extension_specifier
13785 = cp_parser_storage_class_specifier_opt (parser);
13786 if (!extension_specifier)
13787 extension_specifier
13788 = cp_parser_function_specifier_opt (parser,
13789 /*decl_specs=*/NULL);
13792 /* Look for the `template' keyword. */
13793 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13794 /* Let the front end know that we are processing an explicit
13795 instantiation. */
13796 begin_explicit_instantiation ();
13797 /* [temp.explicit] says that we are supposed to ignore access
13798 control while processing explicit instantiation directives. */
13799 push_deferring_access_checks (dk_no_check);
13800 /* Parse a decl-specifier-seq. */
13801 cp_parser_decl_specifier_seq (parser,
13802 CP_PARSER_FLAGS_OPTIONAL,
13803 &decl_specifiers,
13804 &declares_class_or_enum);
13805 /* If there was exactly one decl-specifier, and it declared a class,
13806 and there's no declarator, then we have an explicit type
13807 instantiation. */
13808 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13810 tree type;
13812 type = check_tag_decl (&decl_specifiers,
13813 /*explicit_type_instantiation_p=*/true);
13814 /* Turn access control back on for names used during
13815 template instantiation. */
13816 pop_deferring_access_checks ();
13817 if (type)
13818 do_type_instantiation (type, extension_specifier,
13819 /*complain=*/tf_error);
13821 else
13823 cp_declarator *declarator;
13824 tree decl;
13826 /* Parse the declarator. */
13827 declarator
13828 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13829 /*ctor_dtor_or_conv_p=*/NULL,
13830 /*parenthesized_p=*/NULL,
13831 /*member_p=*/false);
13832 if (declares_class_or_enum & 2)
13833 cp_parser_check_for_definition_in_return_type (declarator,
13834 decl_specifiers.type,
13835 decl_specifiers.locations[ds_type_spec]);
13836 if (declarator != cp_error_declarator)
13838 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13839 permerror (decl_specifiers.locations[ds_inline],
13840 "explicit instantiation shall not use"
13841 " %<inline%> specifier");
13842 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13843 permerror (decl_specifiers.locations[ds_constexpr],
13844 "explicit instantiation shall not use"
13845 " %<constexpr%> specifier");
13847 decl = grokdeclarator (declarator, &decl_specifiers,
13848 NORMAL, 0, &decl_specifiers.attributes);
13849 /* Turn access control back on for names used during
13850 template instantiation. */
13851 pop_deferring_access_checks ();
13852 /* Do the explicit instantiation. */
13853 do_decl_instantiation (decl, extension_specifier);
13855 else
13857 pop_deferring_access_checks ();
13858 /* Skip the body of the explicit instantiation. */
13859 cp_parser_skip_to_end_of_statement (parser);
13862 /* We're done with the instantiation. */
13863 end_explicit_instantiation ();
13865 cp_parser_consume_semicolon_at_end_of_statement (parser);
13867 timevar_pop (TV_TEMPLATE_INST);
13870 /* Parse an explicit-specialization.
13872 explicit-specialization:
13873 template < > declaration
13875 Although the standard says `declaration', what it really means is:
13877 explicit-specialization:
13878 template <> decl-specifier [opt] init-declarator [opt] ;
13879 template <> function-definition
13880 template <> explicit-specialization
13881 template <> template-declaration */
13883 static void
13884 cp_parser_explicit_specialization (cp_parser* parser)
13886 bool need_lang_pop;
13887 cp_token *token = cp_lexer_peek_token (parser->lexer);
13889 /* Look for the `template' keyword. */
13890 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13891 /* Look for the `<'. */
13892 cp_parser_require (parser, CPP_LESS, RT_LESS);
13893 /* Look for the `>'. */
13894 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13895 /* We have processed another parameter list. */
13896 ++parser->num_template_parameter_lists;
13897 /* [temp]
13899 A template ... explicit specialization ... shall not have C
13900 linkage. */
13901 if (current_lang_name == lang_name_c)
13903 error_at (token->location, "template specialization with C linkage");
13904 /* Give it C++ linkage to avoid confusing other parts of the
13905 front end. */
13906 push_lang_context (lang_name_cplusplus);
13907 need_lang_pop = true;
13909 else
13910 need_lang_pop = false;
13911 /* Let the front end know that we are beginning a specialization. */
13912 if (!begin_specialization ())
13914 end_specialization ();
13915 return;
13918 /* If the next keyword is `template', we need to figure out whether
13919 or not we're looking a template-declaration. */
13920 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13922 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13923 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13924 cp_parser_template_declaration_after_export (parser,
13925 /*member_p=*/false);
13926 else
13927 cp_parser_explicit_specialization (parser);
13929 else
13930 /* Parse the dependent declaration. */
13931 cp_parser_single_declaration (parser,
13932 /*checks=*/NULL,
13933 /*member_p=*/false,
13934 /*explicit_specialization_p=*/true,
13935 /*friend_p=*/NULL);
13936 /* We're done with the specialization. */
13937 end_specialization ();
13938 /* For the erroneous case of a template with C linkage, we pushed an
13939 implicit C++ linkage scope; exit that scope now. */
13940 if (need_lang_pop)
13941 pop_lang_context ();
13942 /* We're done with this parameter list. */
13943 --parser->num_template_parameter_lists;
13946 /* Parse a type-specifier.
13948 type-specifier:
13949 simple-type-specifier
13950 class-specifier
13951 enum-specifier
13952 elaborated-type-specifier
13953 cv-qualifier
13955 GNU Extension:
13957 type-specifier:
13958 __complex__
13960 Returns a representation of the type-specifier. For a
13961 class-specifier, enum-specifier, or elaborated-type-specifier, a
13962 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13964 The parser flags FLAGS is used to control type-specifier parsing.
13966 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13967 in a decl-specifier-seq.
13969 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13970 class-specifier, enum-specifier, or elaborated-type-specifier, then
13971 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13972 if a type is declared; 2 if it is defined. Otherwise, it is set to
13973 zero.
13975 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13976 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13977 is set to FALSE. */
13979 static tree
13980 cp_parser_type_specifier (cp_parser* parser,
13981 cp_parser_flags flags,
13982 cp_decl_specifier_seq *decl_specs,
13983 bool is_declaration,
13984 int* declares_class_or_enum,
13985 bool* is_cv_qualifier)
13987 tree type_spec = NULL_TREE;
13988 cp_token *token;
13989 enum rid keyword;
13990 cp_decl_spec ds = ds_last;
13992 /* Assume this type-specifier does not declare a new type. */
13993 if (declares_class_or_enum)
13994 *declares_class_or_enum = 0;
13995 /* And that it does not specify a cv-qualifier. */
13996 if (is_cv_qualifier)
13997 *is_cv_qualifier = false;
13998 /* Peek at the next token. */
13999 token = cp_lexer_peek_token (parser->lexer);
14001 /* If we're looking at a keyword, we can use that to guide the
14002 production we choose. */
14003 keyword = token->keyword;
14004 switch (keyword)
14006 case RID_ENUM:
14007 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14008 goto elaborated_type_specifier;
14010 /* Look for the enum-specifier. */
14011 type_spec = cp_parser_enum_specifier (parser);
14012 /* If that worked, we're done. */
14013 if (type_spec)
14015 if (declares_class_or_enum)
14016 *declares_class_or_enum = 2;
14017 if (decl_specs)
14018 cp_parser_set_decl_spec_type (decl_specs,
14019 type_spec,
14020 token,
14021 /*type_definition_p=*/true);
14022 return type_spec;
14024 else
14025 goto elaborated_type_specifier;
14027 /* Any of these indicate either a class-specifier, or an
14028 elaborated-type-specifier. */
14029 case RID_CLASS:
14030 case RID_STRUCT:
14031 case RID_UNION:
14032 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14033 goto elaborated_type_specifier;
14035 /* Parse tentatively so that we can back up if we don't find a
14036 class-specifier. */
14037 cp_parser_parse_tentatively (parser);
14038 /* Look for the class-specifier. */
14039 type_spec = cp_parser_class_specifier (parser);
14040 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14041 /* If that worked, we're done. */
14042 if (cp_parser_parse_definitely (parser))
14044 if (declares_class_or_enum)
14045 *declares_class_or_enum = 2;
14046 if (decl_specs)
14047 cp_parser_set_decl_spec_type (decl_specs,
14048 type_spec,
14049 token,
14050 /*type_definition_p=*/true);
14051 return type_spec;
14054 /* Fall through. */
14055 elaborated_type_specifier:
14056 /* We're declaring (not defining) a class or enum. */
14057 if (declares_class_or_enum)
14058 *declares_class_or_enum = 1;
14060 /* Fall through. */
14061 case RID_TYPENAME:
14062 /* Look for an elaborated-type-specifier. */
14063 type_spec
14064 = (cp_parser_elaborated_type_specifier
14065 (parser,
14066 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14067 is_declaration));
14068 if (decl_specs)
14069 cp_parser_set_decl_spec_type (decl_specs,
14070 type_spec,
14071 token,
14072 /*type_definition_p=*/false);
14073 return type_spec;
14075 case RID_CONST:
14076 ds = ds_const;
14077 if (is_cv_qualifier)
14078 *is_cv_qualifier = true;
14079 break;
14081 case RID_VOLATILE:
14082 ds = ds_volatile;
14083 if (is_cv_qualifier)
14084 *is_cv_qualifier = true;
14085 break;
14087 case RID_RESTRICT:
14088 ds = ds_restrict;
14089 if (is_cv_qualifier)
14090 *is_cv_qualifier = true;
14091 break;
14093 case RID_COMPLEX:
14094 /* The `__complex__' keyword is a GNU extension. */
14095 ds = ds_complex;
14096 break;
14098 default:
14099 break;
14102 /* Handle simple keywords. */
14103 if (ds != ds_last)
14105 if (decl_specs)
14107 set_and_check_decl_spec_loc (decl_specs, ds, token);
14108 decl_specs->any_specifiers_p = true;
14110 return cp_lexer_consume_token (parser->lexer)->u.value;
14113 /* If we do not already have a type-specifier, assume we are looking
14114 at a simple-type-specifier. */
14115 type_spec = cp_parser_simple_type_specifier (parser,
14116 decl_specs,
14117 flags);
14119 /* If we didn't find a type-specifier, and a type-specifier was not
14120 optional in this context, issue an error message. */
14121 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14123 cp_parser_error (parser, "expected type specifier");
14124 return error_mark_node;
14127 return type_spec;
14130 /* Parse a simple-type-specifier.
14132 simple-type-specifier:
14133 :: [opt] nested-name-specifier [opt] type-name
14134 :: [opt] nested-name-specifier template template-id
14135 char
14136 wchar_t
14137 bool
14138 short
14140 long
14141 signed
14142 unsigned
14143 float
14144 double
14145 void
14147 C++0x Extension:
14149 simple-type-specifier:
14150 auto
14151 decltype ( expression )
14152 char16_t
14153 char32_t
14154 __underlying_type ( type-id )
14156 GNU Extension:
14158 simple-type-specifier:
14159 __int128
14160 __typeof__ unary-expression
14161 __typeof__ ( type-id )
14162 __typeof__ ( type-id ) { initializer-list , [opt] }
14164 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14165 appropriately updated. */
14167 static tree
14168 cp_parser_simple_type_specifier (cp_parser* parser,
14169 cp_decl_specifier_seq *decl_specs,
14170 cp_parser_flags flags)
14172 tree type = NULL_TREE;
14173 cp_token *token;
14175 /* Peek at the next token. */
14176 token = cp_lexer_peek_token (parser->lexer);
14178 /* If we're looking at a keyword, things are easy. */
14179 switch (token->keyword)
14181 case RID_CHAR:
14182 if (decl_specs)
14183 decl_specs->explicit_char_p = true;
14184 type = char_type_node;
14185 break;
14186 case RID_CHAR16:
14187 type = char16_type_node;
14188 break;
14189 case RID_CHAR32:
14190 type = char32_type_node;
14191 break;
14192 case RID_WCHAR:
14193 type = wchar_type_node;
14194 break;
14195 case RID_BOOL:
14196 type = boolean_type_node;
14197 break;
14198 case RID_SHORT:
14199 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14200 type = short_integer_type_node;
14201 break;
14202 case RID_INT:
14203 if (decl_specs)
14204 decl_specs->explicit_int_p = true;
14205 type = integer_type_node;
14206 break;
14207 case RID_INT128:
14208 if (!int128_integer_type_node)
14209 break;
14210 if (decl_specs)
14211 decl_specs->explicit_int128_p = true;
14212 type = int128_integer_type_node;
14213 break;
14214 case RID_LONG:
14215 if (decl_specs)
14216 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14217 type = long_integer_type_node;
14218 break;
14219 case RID_SIGNED:
14220 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14221 type = integer_type_node;
14222 break;
14223 case RID_UNSIGNED:
14224 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14225 type = unsigned_type_node;
14226 break;
14227 case RID_FLOAT:
14228 type = float_type_node;
14229 break;
14230 case RID_DOUBLE:
14231 type = double_type_node;
14232 break;
14233 case RID_VOID:
14234 type = void_type_node;
14235 break;
14237 case RID_AUTO:
14238 maybe_warn_cpp0x (CPP0X_AUTO);
14239 type = make_auto ();
14240 break;
14242 case RID_DECLTYPE:
14243 /* Since DR 743, decltype can either be a simple-type-specifier by
14244 itself or begin a nested-name-specifier. Parsing it will replace
14245 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14246 handling below decide what to do. */
14247 cp_parser_decltype (parser);
14248 cp_lexer_set_token_position (parser->lexer, token);
14249 break;
14251 case RID_TYPEOF:
14252 /* Consume the `typeof' token. */
14253 cp_lexer_consume_token (parser->lexer);
14254 /* Parse the operand to `typeof'. */
14255 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14256 /* If it is not already a TYPE, take its type. */
14257 if (!TYPE_P (type))
14258 type = finish_typeof (type);
14260 if (decl_specs)
14261 cp_parser_set_decl_spec_type (decl_specs, type,
14262 token,
14263 /*type_definition_p=*/false);
14265 return type;
14267 case RID_UNDERLYING_TYPE:
14268 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14269 if (decl_specs)
14270 cp_parser_set_decl_spec_type (decl_specs, type,
14271 token,
14272 /*type_definition_p=*/false);
14274 return type;
14276 case RID_BASES:
14277 case RID_DIRECT_BASES:
14278 type = cp_parser_trait_expr (parser, token->keyword);
14279 if (decl_specs)
14280 cp_parser_set_decl_spec_type (decl_specs, type,
14281 token,
14282 /*type_definition_p=*/false);
14283 return type;
14284 default:
14285 break;
14288 /* If token is an already-parsed decltype not followed by ::,
14289 it's a simple-type-specifier. */
14290 if (token->type == CPP_DECLTYPE
14291 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14293 type = token->u.value;
14294 if (decl_specs)
14295 cp_parser_set_decl_spec_type (decl_specs, type,
14296 token,
14297 /*type_definition_p=*/false);
14298 cp_lexer_consume_token (parser->lexer);
14299 return type;
14302 /* If the type-specifier was for a built-in type, we're done. */
14303 if (type)
14305 /* Record the type. */
14306 if (decl_specs
14307 && (token->keyword != RID_SIGNED
14308 && token->keyword != RID_UNSIGNED
14309 && token->keyword != RID_SHORT
14310 && token->keyword != RID_LONG))
14311 cp_parser_set_decl_spec_type (decl_specs,
14312 type,
14313 token,
14314 /*type_definition_p=*/false);
14315 if (decl_specs)
14316 decl_specs->any_specifiers_p = true;
14318 /* Consume the token. */
14319 cp_lexer_consume_token (parser->lexer);
14321 /* There is no valid C++ program where a non-template type is
14322 followed by a "<". That usually indicates that the user thought
14323 that the type was a template. */
14324 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14325 token->location);
14327 return TYPE_NAME (type);
14330 /* The type-specifier must be a user-defined type. */
14331 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14333 bool qualified_p;
14334 bool global_p;
14336 /* Don't gobble tokens or issue error messages if this is an
14337 optional type-specifier. */
14338 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14339 cp_parser_parse_tentatively (parser);
14341 /* Look for the optional `::' operator. */
14342 global_p
14343 = (cp_parser_global_scope_opt (parser,
14344 /*current_scope_valid_p=*/false)
14345 != NULL_TREE);
14346 /* Look for the nested-name specifier. */
14347 qualified_p
14348 = (cp_parser_nested_name_specifier_opt (parser,
14349 /*typename_keyword_p=*/false,
14350 /*check_dependency_p=*/true,
14351 /*type_p=*/false,
14352 /*is_declaration=*/false)
14353 != NULL_TREE);
14354 token = cp_lexer_peek_token (parser->lexer);
14355 /* If we have seen a nested-name-specifier, and the next token
14356 is `template', then we are using the template-id production. */
14357 if (parser->scope
14358 && cp_parser_optional_template_keyword (parser))
14360 /* Look for the template-id. */
14361 type = cp_parser_template_id (parser,
14362 /*template_keyword_p=*/true,
14363 /*check_dependency_p=*/true,
14364 none_type,
14365 /*is_declaration=*/false);
14366 /* If the template-id did not name a type, we are out of
14367 luck. */
14368 if (TREE_CODE (type) != TYPE_DECL)
14370 cp_parser_error (parser, "expected template-id for type");
14371 type = NULL_TREE;
14374 /* Otherwise, look for a type-name. */
14375 else
14376 type = cp_parser_type_name (parser);
14377 /* Keep track of all name-lookups performed in class scopes. */
14378 if (type
14379 && !global_p
14380 && !qualified_p
14381 && TREE_CODE (type) == TYPE_DECL
14382 && identifier_p (DECL_NAME (type)))
14383 maybe_note_name_used_in_class (DECL_NAME (type), type);
14384 /* If it didn't work out, we don't have a TYPE. */
14385 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14386 && !cp_parser_parse_definitely (parser))
14387 type = NULL_TREE;
14388 if (type && decl_specs)
14389 cp_parser_set_decl_spec_type (decl_specs, type,
14390 token,
14391 /*type_definition_p=*/false);
14394 /* If we didn't get a type-name, issue an error message. */
14395 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14397 cp_parser_error (parser, "expected type-name");
14398 return error_mark_node;
14401 if (type && type != error_mark_node)
14403 /* See if TYPE is an Objective-C type, and if so, parse and
14404 accept any protocol references following it. Do this before
14405 the cp_parser_check_for_invalid_template_id() call, because
14406 Objective-C types can be followed by '<...>' which would
14407 enclose protocol names rather than template arguments, and so
14408 everything is fine. */
14409 if (c_dialect_objc () && !parser->scope
14410 && (objc_is_id (type) || objc_is_class_name (type)))
14412 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14413 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14415 /* Clobber the "unqualified" type previously entered into
14416 DECL_SPECS with the new, improved protocol-qualified version. */
14417 if (decl_specs)
14418 decl_specs->type = qual_type;
14420 return qual_type;
14423 /* There is no valid C++ program where a non-template type is
14424 followed by a "<". That usually indicates that the user
14425 thought that the type was a template. */
14426 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14427 none_type,
14428 token->location);
14431 return type;
14434 /* Parse a type-name.
14436 type-name:
14437 class-name
14438 enum-name
14439 typedef-name
14440 simple-template-id [in c++0x]
14442 enum-name:
14443 identifier
14445 typedef-name:
14446 identifier
14448 Returns a TYPE_DECL for the type. */
14450 static tree
14451 cp_parser_type_name (cp_parser* parser)
14453 tree type_decl;
14455 /* We can't know yet whether it is a class-name or not. */
14456 cp_parser_parse_tentatively (parser);
14457 /* Try a class-name. */
14458 type_decl = cp_parser_class_name (parser,
14459 /*typename_keyword_p=*/false,
14460 /*template_keyword_p=*/false,
14461 none_type,
14462 /*check_dependency_p=*/true,
14463 /*class_head_p=*/false,
14464 /*is_declaration=*/false);
14465 /* If it's not a class-name, keep looking. */
14466 if (!cp_parser_parse_definitely (parser))
14468 if (cxx_dialect < cxx11)
14469 /* It must be a typedef-name or an enum-name. */
14470 return cp_parser_nonclass_name (parser);
14472 cp_parser_parse_tentatively (parser);
14473 /* It is either a simple-template-id representing an
14474 instantiation of an alias template... */
14475 type_decl = cp_parser_template_id (parser,
14476 /*template_keyword_p=*/false,
14477 /*check_dependency_p=*/false,
14478 none_type,
14479 /*is_declaration=*/false);
14480 /* Note that this must be an instantiation of an alias template
14481 because [temp.names]/6 says:
14483 A template-id that names an alias template specialization
14484 is a type-name.
14486 Whereas [temp.names]/7 says:
14488 A simple-template-id that names a class template
14489 specialization is a class-name. */
14490 if (type_decl != NULL_TREE
14491 && TREE_CODE (type_decl) == TYPE_DECL
14492 && TYPE_DECL_ALIAS_P (type_decl))
14493 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14494 else
14495 cp_parser_simulate_error (parser);
14497 if (!cp_parser_parse_definitely (parser))
14498 /* ... Or a typedef-name or an enum-name. */
14499 return cp_parser_nonclass_name (parser);
14502 return type_decl;
14505 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14507 enum-name:
14508 identifier
14510 typedef-name:
14511 identifier
14513 Returns a TYPE_DECL for the type. */
14515 static tree
14516 cp_parser_nonclass_name (cp_parser* parser)
14518 tree type_decl;
14519 tree identifier;
14521 cp_token *token = cp_lexer_peek_token (parser->lexer);
14522 identifier = cp_parser_identifier (parser);
14523 if (identifier == error_mark_node)
14524 return error_mark_node;
14526 /* Look up the type-name. */
14527 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14529 if (TREE_CODE (type_decl) == USING_DECL)
14531 if (!DECL_DEPENDENT_P (type_decl))
14532 type_decl = strip_using_decl (type_decl);
14533 else if (USING_DECL_TYPENAME_P (type_decl))
14535 /* We have found a type introduced by a using
14536 declaration at class scope that refers to a dependent
14537 type.
14539 using typename :: [opt] nested-name-specifier unqualified-id ;
14541 type_decl = make_typename_type (TREE_TYPE (type_decl),
14542 DECL_NAME (type_decl),
14543 typename_type, tf_error);
14544 if (type_decl != error_mark_node)
14545 type_decl = TYPE_NAME (type_decl);
14549 if (TREE_CODE (type_decl) != TYPE_DECL
14550 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14552 /* See if this is an Objective-C type. */
14553 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14554 tree type = objc_get_protocol_qualified_type (identifier, protos);
14555 if (type)
14556 type_decl = TYPE_NAME (type);
14559 /* Issue an error if we did not find a type-name. */
14560 if (TREE_CODE (type_decl) != TYPE_DECL
14561 /* In Objective-C, we have the complication that class names are
14562 normally type names and start declarations (eg, the
14563 "NSObject" in "NSObject *object;"), but can be used in an
14564 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14565 is an expression. So, a classname followed by a dot is not a
14566 valid type-name. */
14567 || (objc_is_class_name (TREE_TYPE (type_decl))
14568 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14570 if (!cp_parser_simulate_error (parser))
14571 cp_parser_name_lookup_error (parser, identifier, type_decl,
14572 NLE_TYPE, token->location);
14573 return error_mark_node;
14575 /* Remember that the name was used in the definition of the
14576 current class so that we can check later to see if the
14577 meaning would have been different after the class was
14578 entirely defined. */
14579 else if (type_decl != error_mark_node
14580 && !parser->scope)
14581 maybe_note_name_used_in_class (identifier, type_decl);
14583 return type_decl;
14586 /* Parse an elaborated-type-specifier. Note that the grammar given
14587 here incorporates the resolution to DR68.
14589 elaborated-type-specifier:
14590 class-key :: [opt] nested-name-specifier [opt] identifier
14591 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14592 enum-key :: [opt] nested-name-specifier [opt] identifier
14593 typename :: [opt] nested-name-specifier identifier
14594 typename :: [opt] nested-name-specifier template [opt]
14595 template-id
14597 GNU extension:
14599 elaborated-type-specifier:
14600 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14601 class-key attributes :: [opt] nested-name-specifier [opt]
14602 template [opt] template-id
14603 enum attributes :: [opt] nested-name-specifier [opt] identifier
14605 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14606 declared `friend'. If IS_DECLARATION is TRUE, then this
14607 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14608 something is being declared.
14610 Returns the TYPE specified. */
14612 static tree
14613 cp_parser_elaborated_type_specifier (cp_parser* parser,
14614 bool is_friend,
14615 bool is_declaration)
14617 enum tag_types tag_type;
14618 tree identifier;
14619 tree type = NULL_TREE;
14620 tree attributes = NULL_TREE;
14621 tree globalscope;
14622 cp_token *token = NULL;
14624 /* See if we're looking at the `enum' keyword. */
14625 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14627 /* Consume the `enum' token. */
14628 cp_lexer_consume_token (parser->lexer);
14629 /* Remember that it's an enumeration type. */
14630 tag_type = enum_type;
14631 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14632 enums) is used here. */
14633 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14634 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14636 pedwarn (input_location, 0, "elaborated-type-specifier "
14637 "for a scoped enum must not use the %<%D%> keyword",
14638 cp_lexer_peek_token (parser->lexer)->u.value);
14639 /* Consume the `struct' or `class' and parse it anyway. */
14640 cp_lexer_consume_token (parser->lexer);
14642 /* Parse the attributes. */
14643 attributes = cp_parser_attributes_opt (parser);
14645 /* Or, it might be `typename'. */
14646 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14647 RID_TYPENAME))
14649 /* Consume the `typename' token. */
14650 cp_lexer_consume_token (parser->lexer);
14651 /* Remember that it's a `typename' type. */
14652 tag_type = typename_type;
14654 /* Otherwise it must be a class-key. */
14655 else
14657 tag_type = cp_parser_class_key (parser);
14658 if (tag_type == none_type)
14659 return error_mark_node;
14660 /* Parse the attributes. */
14661 attributes = cp_parser_attributes_opt (parser);
14664 /* Look for the `::' operator. */
14665 globalscope = cp_parser_global_scope_opt (parser,
14666 /*current_scope_valid_p=*/false);
14667 /* Look for the nested-name-specifier. */
14668 if (tag_type == typename_type && !globalscope)
14670 if (!cp_parser_nested_name_specifier (parser,
14671 /*typename_keyword_p=*/true,
14672 /*check_dependency_p=*/true,
14673 /*type_p=*/true,
14674 is_declaration))
14675 return error_mark_node;
14677 else
14678 /* Even though `typename' is not present, the proposed resolution
14679 to Core Issue 180 says that in `class A<T>::B', `B' should be
14680 considered a type-name, even if `A<T>' is dependent. */
14681 cp_parser_nested_name_specifier_opt (parser,
14682 /*typename_keyword_p=*/true,
14683 /*check_dependency_p=*/true,
14684 /*type_p=*/true,
14685 is_declaration);
14686 /* For everything but enumeration types, consider a template-id.
14687 For an enumeration type, consider only a plain identifier. */
14688 if (tag_type != enum_type)
14690 bool template_p = false;
14691 tree decl;
14693 /* Allow the `template' keyword. */
14694 template_p = cp_parser_optional_template_keyword (parser);
14695 /* If we didn't see `template', we don't know if there's a
14696 template-id or not. */
14697 if (!template_p)
14698 cp_parser_parse_tentatively (parser);
14699 /* Parse the template-id. */
14700 token = cp_lexer_peek_token (parser->lexer);
14701 decl = cp_parser_template_id (parser, template_p,
14702 /*check_dependency_p=*/true,
14703 tag_type,
14704 is_declaration);
14705 /* If we didn't find a template-id, look for an ordinary
14706 identifier. */
14707 if (!template_p && !cp_parser_parse_definitely (parser))
14709 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14710 in effect, then we must assume that, upon instantiation, the
14711 template will correspond to a class. */
14712 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14713 && tag_type == typename_type)
14714 type = make_typename_type (parser->scope, decl,
14715 typename_type,
14716 /*complain=*/tf_error);
14717 /* If the `typename' keyword is in effect and DECL is not a type
14718 decl, then type is non existent. */
14719 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14721 else if (TREE_CODE (decl) == TYPE_DECL)
14722 type = check_elaborated_type_specifier (tag_type, decl,
14723 /*allow_template_p=*/true);
14724 else if (decl == error_mark_node)
14725 type = error_mark_node;
14728 if (!type)
14730 token = cp_lexer_peek_token (parser->lexer);
14731 identifier = cp_parser_identifier (parser);
14733 if (identifier == error_mark_node)
14735 parser->scope = NULL_TREE;
14736 return error_mark_node;
14739 /* For a `typename', we needn't call xref_tag. */
14740 if (tag_type == typename_type
14741 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14742 return cp_parser_make_typename_type (parser, parser->scope,
14743 identifier,
14744 token->location);
14745 /* Look up a qualified name in the usual way. */
14746 if (parser->scope)
14748 tree decl;
14749 tree ambiguous_decls;
14751 decl = cp_parser_lookup_name (parser, identifier,
14752 tag_type,
14753 /*is_template=*/false,
14754 /*is_namespace=*/false,
14755 /*check_dependency=*/true,
14756 &ambiguous_decls,
14757 token->location);
14759 /* If the lookup was ambiguous, an error will already have been
14760 issued. */
14761 if (ambiguous_decls)
14762 return error_mark_node;
14764 /* If we are parsing friend declaration, DECL may be a
14765 TEMPLATE_DECL tree node here. However, we need to check
14766 whether this TEMPLATE_DECL results in valid code. Consider
14767 the following example:
14769 namespace N {
14770 template <class T> class C {};
14772 class X {
14773 template <class T> friend class N::C; // #1, valid code
14775 template <class T> class Y {
14776 friend class N::C; // #2, invalid code
14779 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14780 name lookup of `N::C'. We see that friend declaration must
14781 be template for the code to be valid. Note that
14782 processing_template_decl does not work here since it is
14783 always 1 for the above two cases. */
14785 decl = (cp_parser_maybe_treat_template_as_class
14786 (decl, /*tag_name_p=*/is_friend
14787 && parser->num_template_parameter_lists));
14789 if (TREE_CODE (decl) != TYPE_DECL)
14791 cp_parser_diagnose_invalid_type_name (parser,
14792 parser->scope,
14793 identifier,
14794 token->location);
14795 return error_mark_node;
14798 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14800 bool allow_template = (parser->num_template_parameter_lists
14801 || DECL_SELF_REFERENCE_P (decl));
14802 type = check_elaborated_type_specifier (tag_type, decl,
14803 allow_template);
14805 if (type == error_mark_node)
14806 return error_mark_node;
14809 /* Forward declarations of nested types, such as
14811 class C1::C2;
14812 class C1::C2::C3;
14814 are invalid unless all components preceding the final '::'
14815 are complete. If all enclosing types are complete, these
14816 declarations become merely pointless.
14818 Invalid forward declarations of nested types are errors
14819 caught elsewhere in parsing. Those that are pointless arrive
14820 here. */
14822 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14823 && !is_friend && !processing_explicit_instantiation)
14824 warning (0, "declaration %qD does not declare anything", decl);
14826 type = TREE_TYPE (decl);
14828 else
14830 /* An elaborated-type-specifier sometimes introduces a new type and
14831 sometimes names an existing type. Normally, the rule is that it
14832 introduces a new type only if there is not an existing type of
14833 the same name already in scope. For example, given:
14835 struct S {};
14836 void f() { struct S s; }
14838 the `struct S' in the body of `f' is the same `struct S' as in
14839 the global scope; the existing definition is used. However, if
14840 there were no global declaration, this would introduce a new
14841 local class named `S'.
14843 An exception to this rule applies to the following code:
14845 namespace N { struct S; }
14847 Here, the elaborated-type-specifier names a new type
14848 unconditionally; even if there is already an `S' in the
14849 containing scope this declaration names a new type.
14850 This exception only applies if the elaborated-type-specifier
14851 forms the complete declaration:
14853 [class.name]
14855 A declaration consisting solely of `class-key identifier ;' is
14856 either a redeclaration of the name in the current scope or a
14857 forward declaration of the identifier as a class name. It
14858 introduces the name into the current scope.
14860 We are in this situation precisely when the next token is a `;'.
14862 An exception to the exception is that a `friend' declaration does
14863 *not* name a new type; i.e., given:
14865 struct S { friend struct T; };
14867 `T' is not a new type in the scope of `S'.
14869 Also, `new struct S' or `sizeof (struct S)' never results in the
14870 definition of a new type; a new type can only be declared in a
14871 declaration context. */
14873 tag_scope ts;
14874 bool template_p;
14876 if (is_friend)
14877 /* Friends have special name lookup rules. */
14878 ts = ts_within_enclosing_non_class;
14879 else if (is_declaration
14880 && cp_lexer_next_token_is (parser->lexer,
14881 CPP_SEMICOLON))
14882 /* This is a `class-key identifier ;' */
14883 ts = ts_current;
14884 else
14885 ts = ts_global;
14887 template_p =
14888 (parser->num_template_parameter_lists
14889 && (cp_parser_next_token_starts_class_definition_p (parser)
14890 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14891 /* An unqualified name was used to reference this type, so
14892 there were no qualifying templates. */
14893 if (!cp_parser_check_template_parameters (parser,
14894 /*num_templates=*/0,
14895 token->location,
14896 /*declarator=*/NULL))
14897 return error_mark_node;
14898 type = xref_tag (tag_type, identifier, ts, template_p);
14902 if (type == error_mark_node)
14903 return error_mark_node;
14905 /* Allow attributes on forward declarations of classes. */
14906 if (attributes)
14908 if (TREE_CODE (type) == TYPENAME_TYPE)
14909 warning (OPT_Wattributes,
14910 "attributes ignored on uninstantiated type");
14911 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14912 && ! processing_explicit_instantiation)
14913 warning (OPT_Wattributes,
14914 "attributes ignored on template instantiation");
14915 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14916 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14917 else
14918 warning (OPT_Wattributes,
14919 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14922 if (tag_type != enum_type)
14924 /* Indicate whether this class was declared as a `class' or as a
14925 `struct'. */
14926 if (TREE_CODE (type) == RECORD_TYPE)
14927 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14928 cp_parser_check_class_key (tag_type, type);
14931 /* A "<" cannot follow an elaborated type specifier. If that
14932 happens, the user was probably trying to form a template-id. */
14933 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14934 token->location);
14936 return type;
14939 /* Parse an enum-specifier.
14941 enum-specifier:
14942 enum-head { enumerator-list [opt] }
14943 enum-head { enumerator-list , } [C++0x]
14945 enum-head:
14946 enum-key identifier [opt] enum-base [opt]
14947 enum-key nested-name-specifier identifier enum-base [opt]
14949 enum-key:
14950 enum
14951 enum class [C++0x]
14952 enum struct [C++0x]
14954 enum-base: [C++0x]
14955 : type-specifier-seq
14957 opaque-enum-specifier:
14958 enum-key identifier enum-base [opt] ;
14960 GNU Extensions:
14961 enum-key attributes[opt] identifier [opt] enum-base [opt]
14962 { enumerator-list [opt] }attributes[opt]
14963 enum-key attributes[opt] identifier [opt] enum-base [opt]
14964 { enumerator-list, }attributes[opt] [C++0x]
14966 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14967 if the token stream isn't an enum-specifier after all. */
14969 static tree
14970 cp_parser_enum_specifier (cp_parser* parser)
14972 tree identifier;
14973 tree type = NULL_TREE;
14974 tree prev_scope;
14975 tree nested_name_specifier = NULL_TREE;
14976 tree attributes;
14977 bool scoped_enum_p = false;
14978 bool has_underlying_type = false;
14979 bool nested_being_defined = false;
14980 bool new_value_list = false;
14981 bool is_new_type = false;
14982 bool is_anonymous = false;
14983 tree underlying_type = NULL_TREE;
14984 cp_token *type_start_token = NULL;
14985 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14987 parser->colon_corrects_to_scope_p = false;
14989 /* Parse tentatively so that we can back up if we don't find a
14990 enum-specifier. */
14991 cp_parser_parse_tentatively (parser);
14993 /* Caller guarantees that the current token is 'enum', an identifier
14994 possibly follows, and the token after that is an opening brace.
14995 If we don't have an identifier, fabricate an anonymous name for
14996 the enumeration being defined. */
14997 cp_lexer_consume_token (parser->lexer);
14999 /* Parse the "class" or "struct", which indicates a scoped
15000 enumeration type in C++0x. */
15001 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15002 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15004 if (cxx_dialect < cxx11)
15005 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15007 /* Consume the `struct' or `class' token. */
15008 cp_lexer_consume_token (parser->lexer);
15010 scoped_enum_p = true;
15013 attributes = cp_parser_attributes_opt (parser);
15015 /* Clear the qualification. */
15016 parser->scope = NULL_TREE;
15017 parser->qualifying_scope = NULL_TREE;
15018 parser->object_scope = NULL_TREE;
15020 /* Figure out in what scope the declaration is being placed. */
15021 prev_scope = current_scope ();
15023 type_start_token = cp_lexer_peek_token (parser->lexer);
15025 push_deferring_access_checks (dk_no_check);
15026 nested_name_specifier
15027 = cp_parser_nested_name_specifier_opt (parser,
15028 /*typename_keyword_p=*/true,
15029 /*check_dependency_p=*/false,
15030 /*type_p=*/false,
15031 /*is_declaration=*/false);
15033 if (nested_name_specifier)
15035 tree name;
15037 identifier = cp_parser_identifier (parser);
15038 name = cp_parser_lookup_name (parser, identifier,
15039 enum_type,
15040 /*is_template=*/false,
15041 /*is_namespace=*/false,
15042 /*check_dependency=*/true,
15043 /*ambiguous_decls=*/NULL,
15044 input_location);
15045 if (name && name != error_mark_node)
15047 type = TREE_TYPE (name);
15048 if (TREE_CODE (type) == TYPENAME_TYPE)
15050 /* Are template enums allowed in ISO? */
15051 if (template_parm_scope_p ())
15052 pedwarn (type_start_token->location, OPT_Wpedantic,
15053 "%qD is an enumeration template", name);
15054 /* ignore a typename reference, for it will be solved by name
15055 in start_enum. */
15056 type = NULL_TREE;
15059 else if (nested_name_specifier == error_mark_node)
15060 /* We already issued an error. */;
15061 else
15062 error_at (type_start_token->location,
15063 "%qD is not an enumerator-name", identifier);
15065 else
15067 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15068 identifier = cp_parser_identifier (parser);
15069 else
15071 identifier = make_anon_name ();
15072 is_anonymous = true;
15073 if (scoped_enum_p)
15074 error_at (type_start_token->location,
15075 "anonymous scoped enum is not allowed");
15078 pop_deferring_access_checks ();
15080 /* Check for the `:' that denotes a specified underlying type in C++0x.
15081 Note that a ':' could also indicate a bitfield width, however. */
15082 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15084 cp_decl_specifier_seq type_specifiers;
15086 /* Consume the `:'. */
15087 cp_lexer_consume_token (parser->lexer);
15089 /* Parse the type-specifier-seq. */
15090 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15091 /*is_trailing_return=*/false,
15092 &type_specifiers);
15094 /* At this point this is surely not elaborated type specifier. */
15095 if (!cp_parser_parse_definitely (parser))
15096 return NULL_TREE;
15098 if (cxx_dialect < cxx11)
15099 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15101 has_underlying_type = true;
15103 /* If that didn't work, stop. */
15104 if (type_specifiers.type != error_mark_node)
15106 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15107 /*initialized=*/0, NULL);
15108 if (underlying_type == error_mark_node)
15109 underlying_type = NULL_TREE;
15113 /* Look for the `{' but don't consume it yet. */
15114 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15116 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15118 cp_parser_error (parser, "expected %<{%>");
15119 if (has_underlying_type)
15121 type = NULL_TREE;
15122 goto out;
15125 /* An opaque-enum-specifier must have a ';' here. */
15126 if ((scoped_enum_p || underlying_type)
15127 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15129 cp_parser_error (parser, "expected %<;%> or %<{%>");
15130 if (has_underlying_type)
15132 type = NULL_TREE;
15133 goto out;
15138 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15139 return NULL_TREE;
15141 if (nested_name_specifier)
15143 if (CLASS_TYPE_P (nested_name_specifier))
15145 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15146 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15147 push_scope (nested_name_specifier);
15149 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15151 push_nested_namespace (nested_name_specifier);
15155 /* Issue an error message if type-definitions are forbidden here. */
15156 if (!cp_parser_check_type_definition (parser))
15157 type = error_mark_node;
15158 else
15159 /* Create the new type. We do this before consuming the opening
15160 brace so the enum will be recorded as being on the line of its
15161 tag (or the 'enum' keyword, if there is no tag). */
15162 type = start_enum (identifier, type, underlying_type,
15163 scoped_enum_p, &is_new_type);
15165 /* If the next token is not '{' it is an opaque-enum-specifier or an
15166 elaborated-type-specifier. */
15167 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15169 timevar_push (TV_PARSE_ENUM);
15170 if (nested_name_specifier
15171 && nested_name_specifier != error_mark_node)
15173 /* The following catches invalid code such as:
15174 enum class S<int>::E { A, B, C }; */
15175 if (!processing_specialization
15176 && CLASS_TYPE_P (nested_name_specifier)
15177 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15178 error_at (type_start_token->location, "cannot add an enumerator "
15179 "list to a template instantiation");
15181 /* If that scope does not contain the scope in which the
15182 class was originally declared, the program is invalid. */
15183 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
15185 if (at_namespace_scope_p ())
15186 error_at (type_start_token->location,
15187 "declaration of %qD in namespace %qD which does not "
15188 "enclose %qD",
15189 type, prev_scope, nested_name_specifier);
15190 else
15191 error_at (type_start_token->location,
15192 "declaration of %qD in %qD which does not enclose %qD",
15193 type, prev_scope, nested_name_specifier);
15194 type = error_mark_node;
15198 if (scoped_enum_p)
15199 begin_scope (sk_scoped_enum, type);
15201 /* Consume the opening brace. */
15202 cp_lexer_consume_token (parser->lexer);
15204 if (type == error_mark_node)
15205 ; /* Nothing to add */
15206 else if (OPAQUE_ENUM_P (type)
15207 || (cxx_dialect > cxx98 && processing_specialization))
15209 new_value_list = true;
15210 SET_OPAQUE_ENUM_P (type, false);
15211 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15213 else
15215 error_at (type_start_token->location, "multiple definition of %q#T", type);
15216 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15217 "previous definition here");
15218 type = error_mark_node;
15221 if (type == error_mark_node)
15222 cp_parser_skip_to_end_of_block_or_statement (parser);
15223 /* If the next token is not '}', then there are some enumerators. */
15224 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15226 if (is_anonymous && !scoped_enum_p)
15227 pedwarn (type_start_token->location, OPT_Wpedantic,
15228 "ISO C++ forbids empty anonymous enum");
15230 else
15231 cp_parser_enumerator_list (parser, type);
15233 /* Consume the final '}'. */
15234 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15236 if (scoped_enum_p)
15237 finish_scope ();
15238 timevar_pop (TV_PARSE_ENUM);
15240 else
15242 /* If a ';' follows, then it is an opaque-enum-specifier
15243 and additional restrictions apply. */
15244 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15246 if (is_anonymous)
15247 error_at (type_start_token->location,
15248 "opaque-enum-specifier without name");
15249 else if (nested_name_specifier)
15250 error_at (type_start_token->location,
15251 "opaque-enum-specifier must use a simple identifier");
15255 /* Look for trailing attributes to apply to this enumeration, and
15256 apply them if appropriate. */
15257 if (cp_parser_allow_gnu_extensions_p (parser))
15259 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15260 trailing_attr = chainon (trailing_attr, attributes);
15261 cplus_decl_attributes (&type,
15262 trailing_attr,
15263 (int) ATTR_FLAG_TYPE_IN_PLACE);
15266 /* Finish up the enumeration. */
15267 if (type != error_mark_node)
15269 if (new_value_list)
15270 finish_enum_value_list (type);
15271 if (is_new_type)
15272 finish_enum (type);
15275 if (nested_name_specifier)
15277 if (CLASS_TYPE_P (nested_name_specifier))
15279 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15280 pop_scope (nested_name_specifier);
15282 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15284 pop_nested_namespace (nested_name_specifier);
15287 out:
15288 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15289 return type;
15292 /* Parse an enumerator-list. The enumerators all have the indicated
15293 TYPE.
15295 enumerator-list:
15296 enumerator-definition
15297 enumerator-list , enumerator-definition */
15299 static void
15300 cp_parser_enumerator_list (cp_parser* parser, tree type)
15302 while (true)
15304 /* Parse an enumerator-definition. */
15305 cp_parser_enumerator_definition (parser, type);
15307 /* If the next token is not a ',', we've reached the end of
15308 the list. */
15309 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15310 break;
15311 /* Otherwise, consume the `,' and keep going. */
15312 cp_lexer_consume_token (parser->lexer);
15313 /* If the next token is a `}', there is a trailing comma. */
15314 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15316 if (cxx_dialect < cxx11 && !in_system_header)
15317 pedwarn (input_location, OPT_Wpedantic,
15318 "comma at end of enumerator list");
15319 break;
15324 /* Parse an enumerator-definition. The enumerator has the indicated
15325 TYPE.
15327 enumerator-definition:
15328 enumerator
15329 enumerator = constant-expression
15331 enumerator:
15332 identifier */
15334 static void
15335 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15337 tree identifier;
15338 tree value;
15339 location_t loc;
15341 /* Save the input location because we are interested in the location
15342 of the identifier and not the location of the explicit value. */
15343 loc = cp_lexer_peek_token (parser->lexer)->location;
15345 /* Look for the identifier. */
15346 identifier = cp_parser_identifier (parser);
15347 if (identifier == error_mark_node)
15348 return;
15350 /* If the next token is an '=', then there is an explicit value. */
15351 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15353 /* Consume the `=' token. */
15354 cp_lexer_consume_token (parser->lexer);
15355 /* Parse the value. */
15356 value = cp_parser_constant_expression (parser,
15357 /*allow_non_constant_p=*/false,
15358 NULL);
15360 else
15361 value = NULL_TREE;
15363 /* If we are processing a template, make sure the initializer of the
15364 enumerator doesn't contain any bare template parameter pack. */
15365 if (check_for_bare_parameter_packs (value))
15366 value = error_mark_node;
15368 /* integral_constant_value will pull out this expression, so make sure
15369 it's folded as appropriate. */
15370 value = fold_non_dependent_expr (value);
15372 /* Create the enumerator. */
15373 build_enumerator (identifier, value, type, loc);
15376 /* Parse a namespace-name.
15378 namespace-name:
15379 original-namespace-name
15380 namespace-alias
15382 Returns the NAMESPACE_DECL for the namespace. */
15384 static tree
15385 cp_parser_namespace_name (cp_parser* parser)
15387 tree identifier;
15388 tree namespace_decl;
15390 cp_token *token = cp_lexer_peek_token (parser->lexer);
15392 /* Get the name of the namespace. */
15393 identifier = cp_parser_identifier (parser);
15394 if (identifier == error_mark_node)
15395 return error_mark_node;
15397 /* Look up the identifier in the currently active scope. Look only
15398 for namespaces, due to:
15400 [basic.lookup.udir]
15402 When looking up a namespace-name in a using-directive or alias
15403 definition, only namespace names are considered.
15405 And:
15407 [basic.lookup.qual]
15409 During the lookup of a name preceding the :: scope resolution
15410 operator, object, function, and enumerator names are ignored.
15412 (Note that cp_parser_qualifying_entity only calls this
15413 function if the token after the name is the scope resolution
15414 operator.) */
15415 namespace_decl = cp_parser_lookup_name (parser, identifier,
15416 none_type,
15417 /*is_template=*/false,
15418 /*is_namespace=*/true,
15419 /*check_dependency=*/true,
15420 /*ambiguous_decls=*/NULL,
15421 token->location);
15422 /* If it's not a namespace, issue an error. */
15423 if (namespace_decl == error_mark_node
15424 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15426 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15427 error_at (token->location, "%qD is not a namespace-name", identifier);
15428 cp_parser_error (parser, "expected namespace-name");
15429 namespace_decl = error_mark_node;
15432 return namespace_decl;
15435 /* Parse a namespace-definition.
15437 namespace-definition:
15438 named-namespace-definition
15439 unnamed-namespace-definition
15441 named-namespace-definition:
15442 original-namespace-definition
15443 extension-namespace-definition
15445 original-namespace-definition:
15446 namespace identifier { namespace-body }
15448 extension-namespace-definition:
15449 namespace original-namespace-name { namespace-body }
15451 unnamed-namespace-definition:
15452 namespace { namespace-body } */
15454 static void
15455 cp_parser_namespace_definition (cp_parser* parser)
15457 tree identifier, attribs;
15458 bool has_visibility;
15459 bool is_inline;
15461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15463 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15464 is_inline = true;
15465 cp_lexer_consume_token (parser->lexer);
15467 else
15468 is_inline = false;
15470 /* Look for the `namespace' keyword. */
15471 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15473 /* Get the name of the namespace. We do not attempt to distinguish
15474 between an original-namespace-definition and an
15475 extension-namespace-definition at this point. The semantic
15476 analysis routines are responsible for that. */
15477 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15478 identifier = cp_parser_identifier (parser);
15479 else
15480 identifier = NULL_TREE;
15482 /* Parse any specified attributes. */
15483 attribs = cp_parser_attributes_opt (parser);
15485 /* Look for the `{' to start the namespace. */
15486 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15487 /* Start the namespace. */
15488 push_namespace (identifier);
15490 /* "inline namespace" is equivalent to a stub namespace definition
15491 followed by a strong using directive. */
15492 if (is_inline)
15494 tree name_space = current_namespace;
15495 /* Set up namespace association. */
15496 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15497 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15498 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15499 /* Import the contents of the inline namespace. */
15500 pop_namespace ();
15501 do_using_directive (name_space);
15502 push_namespace (identifier);
15505 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15507 /* Parse the body of the namespace. */
15508 cp_parser_namespace_body (parser);
15510 if (has_visibility)
15511 pop_visibility (1);
15513 /* Finish the namespace. */
15514 pop_namespace ();
15515 /* Look for the final `}'. */
15516 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15519 /* Parse a namespace-body.
15521 namespace-body:
15522 declaration-seq [opt] */
15524 static void
15525 cp_parser_namespace_body (cp_parser* parser)
15527 cp_parser_declaration_seq_opt (parser);
15530 /* Parse a namespace-alias-definition.
15532 namespace-alias-definition:
15533 namespace identifier = qualified-namespace-specifier ; */
15535 static void
15536 cp_parser_namespace_alias_definition (cp_parser* parser)
15538 tree identifier;
15539 tree namespace_specifier;
15541 cp_token *token = cp_lexer_peek_token (parser->lexer);
15543 /* Look for the `namespace' keyword. */
15544 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15545 /* Look for the identifier. */
15546 identifier = cp_parser_identifier (parser);
15547 if (identifier == error_mark_node)
15548 return;
15549 /* Look for the `=' token. */
15550 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15551 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15553 error_at (token->location, "%<namespace%> definition is not allowed here");
15554 /* Skip the definition. */
15555 cp_lexer_consume_token (parser->lexer);
15556 if (cp_parser_skip_to_closing_brace (parser))
15557 cp_lexer_consume_token (parser->lexer);
15558 return;
15560 cp_parser_require (parser, CPP_EQ, RT_EQ);
15561 /* Look for the qualified-namespace-specifier. */
15562 namespace_specifier
15563 = cp_parser_qualified_namespace_specifier (parser);
15564 /* Look for the `;' token. */
15565 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15567 /* Register the alias in the symbol table. */
15568 do_namespace_alias (identifier, namespace_specifier);
15571 /* Parse a qualified-namespace-specifier.
15573 qualified-namespace-specifier:
15574 :: [opt] nested-name-specifier [opt] namespace-name
15576 Returns a NAMESPACE_DECL corresponding to the specified
15577 namespace. */
15579 static tree
15580 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15582 /* Look for the optional `::'. */
15583 cp_parser_global_scope_opt (parser,
15584 /*current_scope_valid_p=*/false);
15586 /* Look for the optional nested-name-specifier. */
15587 cp_parser_nested_name_specifier_opt (parser,
15588 /*typename_keyword_p=*/false,
15589 /*check_dependency_p=*/true,
15590 /*type_p=*/false,
15591 /*is_declaration=*/true);
15593 return cp_parser_namespace_name (parser);
15596 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15597 access declaration.
15599 using-declaration:
15600 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15601 using :: unqualified-id ;
15603 access-declaration:
15604 qualified-id ;
15608 static bool
15609 cp_parser_using_declaration (cp_parser* parser,
15610 bool access_declaration_p)
15612 cp_token *token;
15613 bool typename_p = false;
15614 bool global_scope_p;
15615 tree decl;
15616 tree identifier;
15617 tree qscope;
15618 int oldcount = errorcount;
15619 cp_token *diag_token = NULL;
15621 if (access_declaration_p)
15623 diag_token = cp_lexer_peek_token (parser->lexer);
15624 cp_parser_parse_tentatively (parser);
15626 else
15628 /* Look for the `using' keyword. */
15629 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15631 /* Peek at the next token. */
15632 token = cp_lexer_peek_token (parser->lexer);
15633 /* See if it's `typename'. */
15634 if (token->keyword == RID_TYPENAME)
15636 /* Remember that we've seen it. */
15637 typename_p = true;
15638 /* Consume the `typename' token. */
15639 cp_lexer_consume_token (parser->lexer);
15643 /* Look for the optional global scope qualification. */
15644 global_scope_p
15645 = (cp_parser_global_scope_opt (parser,
15646 /*current_scope_valid_p=*/false)
15647 != NULL_TREE);
15649 /* If we saw `typename', or didn't see `::', then there must be a
15650 nested-name-specifier present. */
15651 if (typename_p || !global_scope_p)
15652 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15653 /*check_dependency_p=*/true,
15654 /*type_p=*/false,
15655 /*is_declaration=*/true);
15656 /* Otherwise, we could be in either of the two productions. In that
15657 case, treat the nested-name-specifier as optional. */
15658 else
15659 qscope = cp_parser_nested_name_specifier_opt (parser,
15660 /*typename_keyword_p=*/false,
15661 /*check_dependency_p=*/true,
15662 /*type_p=*/false,
15663 /*is_declaration=*/true);
15664 if (!qscope)
15665 qscope = global_namespace;
15667 if (access_declaration_p && cp_parser_error_occurred (parser))
15668 /* Something has already gone wrong; there's no need to parse
15669 further. Since an error has occurred, the return value of
15670 cp_parser_parse_definitely will be false, as required. */
15671 return cp_parser_parse_definitely (parser);
15673 token = cp_lexer_peek_token (parser->lexer);
15674 /* Parse the unqualified-id. */
15675 identifier = cp_parser_unqualified_id (parser,
15676 /*template_keyword_p=*/false,
15677 /*check_dependency_p=*/true,
15678 /*declarator_p=*/true,
15679 /*optional_p=*/false);
15681 if (access_declaration_p)
15683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15684 cp_parser_simulate_error (parser);
15685 if (!cp_parser_parse_definitely (parser))
15686 return false;
15689 /* The function we call to handle a using-declaration is different
15690 depending on what scope we are in. */
15691 if (qscope == error_mark_node || identifier == error_mark_node)
15693 else if (!identifier_p (identifier)
15694 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15695 /* [namespace.udecl]
15697 A using declaration shall not name a template-id. */
15698 error_at (token->location,
15699 "a template-id may not appear in a using-declaration");
15700 else
15702 if (at_class_scope_p ())
15704 /* Create the USING_DECL. */
15705 decl = do_class_using_decl (parser->scope, identifier);
15707 if (decl && typename_p)
15708 USING_DECL_TYPENAME_P (decl) = 1;
15710 if (check_for_bare_parameter_packs (decl))
15711 return false;
15712 else
15713 /* Add it to the list of members in this class. */
15714 finish_member_declaration (decl);
15716 else
15718 decl = cp_parser_lookup_name_simple (parser,
15719 identifier,
15720 token->location);
15721 if (decl == error_mark_node)
15722 cp_parser_name_lookup_error (parser, identifier,
15723 decl, NLE_NULL,
15724 token->location);
15725 else if (check_for_bare_parameter_packs (decl))
15726 return false;
15727 else if (!at_namespace_scope_p ())
15728 do_local_using_decl (decl, qscope, identifier);
15729 else
15730 do_toplevel_using_decl (decl, qscope, identifier);
15734 /* Look for the final `;'. */
15735 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15737 if (access_declaration_p && errorcount == oldcount)
15738 warning_at (diag_token->location, OPT_Wdeprecated,
15739 "access declarations are deprecated "
15740 "in favour of using-declarations; "
15741 "suggestion: add the %<using%> keyword");
15743 return true;
15746 /* Parse an alias-declaration.
15748 alias-declaration:
15749 using identifier attribute-specifier-seq [opt] = type-id */
15751 static tree
15752 cp_parser_alias_declaration (cp_parser* parser)
15754 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15755 location_t id_location;
15756 cp_declarator *declarator;
15757 cp_decl_specifier_seq decl_specs;
15758 bool member_p;
15759 const char *saved_message = NULL;
15761 /* Look for the `using' keyword. */
15762 cp_token *using_token
15763 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15764 if (using_token == NULL)
15765 return error_mark_node;
15767 id_location = cp_lexer_peek_token (parser->lexer)->location;
15768 id = cp_parser_identifier (parser);
15769 if (id == error_mark_node)
15770 return error_mark_node;
15772 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15773 attributes = cp_parser_attributes_opt (parser);
15774 if (attributes == error_mark_node)
15775 return error_mark_node;
15777 cp_parser_require (parser, CPP_EQ, RT_EQ);
15779 if (cp_parser_error_occurred (parser))
15780 return error_mark_node;
15782 cp_parser_commit_to_tentative_parse (parser);
15784 /* Now we are going to parse the type-id of the declaration. */
15787 [dcl.type]/3 says:
15789 "A type-specifier-seq shall not define a class or enumeration
15790 unless it appears in the type-id of an alias-declaration (7.1.3) that
15791 is not the declaration of a template-declaration."
15793 In other words, if we currently are in an alias template, the
15794 type-id should not define a type.
15796 So let's set parser->type_definition_forbidden_message in that
15797 case; cp_parser_check_type_definition (called by
15798 cp_parser_class_specifier) will then emit an error if a type is
15799 defined in the type-id. */
15800 if (parser->num_template_parameter_lists)
15802 saved_message = parser->type_definition_forbidden_message;
15803 parser->type_definition_forbidden_message =
15804 G_("types may not be defined in alias template declarations");
15807 type = cp_parser_type_id (parser);
15809 /* Restore the error message if need be. */
15810 if (parser->num_template_parameter_lists)
15811 parser->type_definition_forbidden_message = saved_message;
15813 if (type == error_mark_node)
15815 cp_parser_skip_to_end_of_block_or_statement (parser);
15816 return error_mark_node;
15819 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15821 if (cp_parser_error_occurred (parser))
15823 cp_parser_skip_to_end_of_block_or_statement (parser);
15824 return error_mark_node;
15827 /* A typedef-name can also be introduced by an alias-declaration. The
15828 identifier following the using keyword becomes a typedef-name. It has
15829 the same semantics as if it were introduced by the typedef
15830 specifier. In particular, it does not define a new type and it shall
15831 not appear in the type-id. */
15833 clear_decl_specs (&decl_specs);
15834 decl_specs.type = type;
15835 if (attributes != NULL_TREE)
15837 decl_specs.attributes = attributes;
15838 set_and_check_decl_spec_loc (&decl_specs,
15839 ds_attribute,
15840 attrs_token);
15842 set_and_check_decl_spec_loc (&decl_specs,
15843 ds_typedef,
15844 using_token);
15845 set_and_check_decl_spec_loc (&decl_specs,
15846 ds_alias,
15847 using_token);
15849 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15850 declarator->id_loc = id_location;
15852 member_p = at_class_scope_p ();
15853 if (member_p)
15854 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15855 NULL_TREE, attributes);
15856 else
15857 decl = start_decl (declarator, &decl_specs, 0,
15858 attributes, NULL_TREE, &pushed_scope);
15859 if (decl == error_mark_node)
15860 return decl;
15862 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15864 if (pushed_scope)
15865 pop_scope (pushed_scope);
15867 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15868 added into the symbol table; otherwise, return the TYPE_DECL. */
15869 if (DECL_LANG_SPECIFIC (decl)
15870 && DECL_TEMPLATE_INFO (decl)
15871 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15873 decl = DECL_TI_TEMPLATE (decl);
15874 if (member_p)
15875 check_member_template (decl);
15878 return decl;
15881 /* Parse a using-directive.
15883 using-directive:
15884 using namespace :: [opt] nested-name-specifier [opt]
15885 namespace-name ; */
15887 static void
15888 cp_parser_using_directive (cp_parser* parser)
15890 tree namespace_decl;
15891 tree attribs;
15893 /* Look for the `using' keyword. */
15894 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15895 /* And the `namespace' keyword. */
15896 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15897 /* Look for the optional `::' operator. */
15898 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15899 /* And the optional nested-name-specifier. */
15900 cp_parser_nested_name_specifier_opt (parser,
15901 /*typename_keyword_p=*/false,
15902 /*check_dependency_p=*/true,
15903 /*type_p=*/false,
15904 /*is_declaration=*/true);
15905 /* Get the namespace being used. */
15906 namespace_decl = cp_parser_namespace_name (parser);
15907 /* And any specified attributes. */
15908 attribs = cp_parser_attributes_opt (parser);
15909 /* Update the symbol table. */
15910 parse_using_directive (namespace_decl, attribs);
15911 /* Look for the final `;'. */
15912 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15915 /* Parse an asm-definition.
15917 asm-definition:
15918 asm ( string-literal ) ;
15920 GNU Extension:
15922 asm-definition:
15923 asm volatile [opt] ( string-literal ) ;
15924 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15925 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15926 : asm-operand-list [opt] ) ;
15927 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15928 : asm-operand-list [opt]
15929 : asm-clobber-list [opt] ) ;
15930 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15931 : asm-clobber-list [opt]
15932 : asm-goto-list ) ; */
15934 static void
15935 cp_parser_asm_definition (cp_parser* parser)
15937 tree string;
15938 tree outputs = NULL_TREE;
15939 tree inputs = NULL_TREE;
15940 tree clobbers = NULL_TREE;
15941 tree labels = NULL_TREE;
15942 tree asm_stmt;
15943 bool volatile_p = false;
15944 bool extended_p = false;
15945 bool invalid_inputs_p = false;
15946 bool invalid_outputs_p = false;
15947 bool goto_p = false;
15948 required_token missing = RT_NONE;
15950 /* Look for the `asm' keyword. */
15951 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15952 /* See if the next token is `volatile'. */
15953 if (cp_parser_allow_gnu_extensions_p (parser)
15954 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15956 /* Remember that we saw the `volatile' keyword. */
15957 volatile_p = true;
15958 /* Consume the token. */
15959 cp_lexer_consume_token (parser->lexer);
15961 if (cp_parser_allow_gnu_extensions_p (parser)
15962 && parser->in_function_body
15963 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15965 /* Remember that we saw the `goto' keyword. */
15966 goto_p = true;
15967 /* Consume the token. */
15968 cp_lexer_consume_token (parser->lexer);
15970 /* Look for the opening `('. */
15971 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15972 return;
15973 /* Look for the string. */
15974 string = cp_parser_string_literal (parser, false, false);
15975 if (string == error_mark_node)
15977 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15978 /*consume_paren=*/true);
15979 return;
15982 /* If we're allowing GNU extensions, check for the extended assembly
15983 syntax. Unfortunately, the `:' tokens need not be separated by
15984 a space in C, and so, for compatibility, we tolerate that here
15985 too. Doing that means that we have to treat the `::' operator as
15986 two `:' tokens. */
15987 if (cp_parser_allow_gnu_extensions_p (parser)
15988 && parser->in_function_body
15989 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15990 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15992 bool inputs_p = false;
15993 bool clobbers_p = false;
15994 bool labels_p = false;
15996 /* The extended syntax was used. */
15997 extended_p = true;
15999 /* Look for outputs. */
16000 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16002 /* Consume the `:'. */
16003 cp_lexer_consume_token (parser->lexer);
16004 /* Parse the output-operands. */
16005 if (cp_lexer_next_token_is_not (parser->lexer,
16006 CPP_COLON)
16007 && cp_lexer_next_token_is_not (parser->lexer,
16008 CPP_SCOPE)
16009 && cp_lexer_next_token_is_not (parser->lexer,
16010 CPP_CLOSE_PAREN)
16011 && !goto_p)
16012 outputs = cp_parser_asm_operand_list (parser);
16014 if (outputs == error_mark_node)
16015 invalid_outputs_p = true;
16017 /* If the next token is `::', there are no outputs, and the
16018 next token is the beginning of the inputs. */
16019 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16020 /* The inputs are coming next. */
16021 inputs_p = true;
16023 /* Look for inputs. */
16024 if (inputs_p
16025 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16027 /* Consume the `:' or `::'. */
16028 cp_lexer_consume_token (parser->lexer);
16029 /* Parse the output-operands. */
16030 if (cp_lexer_next_token_is_not (parser->lexer,
16031 CPP_COLON)
16032 && cp_lexer_next_token_is_not (parser->lexer,
16033 CPP_SCOPE)
16034 && cp_lexer_next_token_is_not (parser->lexer,
16035 CPP_CLOSE_PAREN))
16036 inputs = cp_parser_asm_operand_list (parser);
16038 if (inputs == error_mark_node)
16039 invalid_inputs_p = true;
16041 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16042 /* The clobbers are coming next. */
16043 clobbers_p = true;
16045 /* Look for clobbers. */
16046 if (clobbers_p
16047 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16049 clobbers_p = true;
16050 /* Consume the `:' or `::'. */
16051 cp_lexer_consume_token (parser->lexer);
16052 /* Parse the clobbers. */
16053 if (cp_lexer_next_token_is_not (parser->lexer,
16054 CPP_COLON)
16055 && cp_lexer_next_token_is_not (parser->lexer,
16056 CPP_CLOSE_PAREN))
16057 clobbers = cp_parser_asm_clobber_list (parser);
16059 else if (goto_p
16060 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16061 /* The labels are coming next. */
16062 labels_p = true;
16064 /* Look for labels. */
16065 if (labels_p
16066 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16068 labels_p = true;
16069 /* Consume the `:' or `::'. */
16070 cp_lexer_consume_token (parser->lexer);
16071 /* Parse the labels. */
16072 labels = cp_parser_asm_label_list (parser);
16075 if (goto_p && !labels_p)
16076 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16078 else if (goto_p)
16079 missing = RT_COLON_SCOPE;
16081 /* Look for the closing `)'. */
16082 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16083 missing ? missing : RT_CLOSE_PAREN))
16084 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16085 /*consume_paren=*/true);
16086 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16088 if (!invalid_inputs_p && !invalid_outputs_p)
16090 /* Create the ASM_EXPR. */
16091 if (parser->in_function_body)
16093 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16094 inputs, clobbers, labels);
16095 /* If the extended syntax was not used, mark the ASM_EXPR. */
16096 if (!extended_p)
16098 tree temp = asm_stmt;
16099 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16100 temp = TREE_OPERAND (temp, 0);
16102 ASM_INPUT_P (temp) = 1;
16105 else
16106 add_asm_node (string);
16110 /* Declarators [gram.dcl.decl] */
16112 /* Parse an init-declarator.
16114 init-declarator:
16115 declarator initializer [opt]
16117 GNU Extension:
16119 init-declarator:
16120 declarator asm-specification [opt] attributes [opt] initializer [opt]
16122 function-definition:
16123 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16124 function-body
16125 decl-specifier-seq [opt] declarator function-try-block
16127 GNU Extension:
16129 function-definition:
16130 __extension__ function-definition
16132 TM Extension:
16134 function-definition:
16135 decl-specifier-seq [opt] declarator function-transaction-block
16137 The DECL_SPECIFIERS apply to this declarator. Returns a
16138 representation of the entity declared. If MEMBER_P is TRUE, then
16139 this declarator appears in a class scope. The new DECL created by
16140 this declarator is returned.
16142 The CHECKS are access checks that should be performed once we know
16143 what entity is being declared (and, therefore, what classes have
16144 befriended it).
16146 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16147 for a function-definition here as well. If the declarator is a
16148 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16149 be TRUE upon return. By that point, the function-definition will
16150 have been completely parsed.
16152 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16153 is FALSE.
16155 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16156 parsed declaration if it is an uninitialized single declarator not followed
16157 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16158 if present, will not be consumed. If returned, this declarator will be
16159 created with SD_INITIALIZED but will not call cp_finish_decl. */
16161 static tree
16162 cp_parser_init_declarator (cp_parser* parser,
16163 cp_decl_specifier_seq *decl_specifiers,
16164 vec<deferred_access_check, va_gc> *checks,
16165 bool function_definition_allowed_p,
16166 bool member_p,
16167 int declares_class_or_enum,
16168 bool* function_definition_p,
16169 tree* maybe_range_for_decl)
16171 cp_token *token = NULL, *asm_spec_start_token = NULL,
16172 *attributes_start_token = NULL;
16173 cp_declarator *declarator;
16174 tree prefix_attributes;
16175 tree attributes = NULL;
16176 tree asm_specification;
16177 tree initializer;
16178 tree decl = NULL_TREE;
16179 tree scope;
16180 int is_initialized;
16181 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16182 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16183 "(...)". */
16184 enum cpp_ttype initialization_kind;
16185 bool is_direct_init = false;
16186 bool is_non_constant_init;
16187 int ctor_dtor_or_conv_p;
16188 bool friend_p;
16189 tree pushed_scope = NULL_TREE;
16190 bool range_for_decl_p = false;
16192 /* Gather the attributes that were provided with the
16193 decl-specifiers. */
16194 prefix_attributes = decl_specifiers->attributes;
16196 /* Assume that this is not the declarator for a function
16197 definition. */
16198 if (function_definition_p)
16199 *function_definition_p = false;
16201 /* Defer access checks while parsing the declarator; we cannot know
16202 what names are accessible until we know what is being
16203 declared. */
16204 resume_deferring_access_checks ();
16206 /* Parse the declarator. */
16207 token = cp_lexer_peek_token (parser->lexer);
16208 declarator
16209 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16210 &ctor_dtor_or_conv_p,
16211 /*parenthesized_p=*/NULL,
16212 member_p);
16213 /* Gather up the deferred checks. */
16214 stop_deferring_access_checks ();
16216 /* If the DECLARATOR was erroneous, there's no need to go
16217 further. */
16218 if (declarator == cp_error_declarator)
16219 return error_mark_node;
16221 /* Check that the number of template-parameter-lists is OK. */
16222 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16223 token->location))
16224 return error_mark_node;
16226 if (declares_class_or_enum & 2)
16227 cp_parser_check_for_definition_in_return_type (declarator,
16228 decl_specifiers->type,
16229 decl_specifiers->locations[ds_type_spec]);
16231 /* Figure out what scope the entity declared by the DECLARATOR is
16232 located in. `grokdeclarator' sometimes changes the scope, so
16233 we compute it now. */
16234 scope = get_scope_of_declarator (declarator);
16236 /* Perform any lookups in the declared type which were thought to be
16237 dependent, but are not in the scope of the declarator. */
16238 decl_specifiers->type
16239 = maybe_update_decl_type (decl_specifiers->type, scope);
16241 /* If we're allowing GNU extensions, look for an
16242 asm-specification. */
16243 if (cp_parser_allow_gnu_extensions_p (parser))
16245 /* Look for an asm-specification. */
16246 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16247 asm_specification = cp_parser_asm_specification_opt (parser);
16249 else
16250 asm_specification = NULL_TREE;
16252 /* Look for attributes. */
16253 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16254 attributes = cp_parser_attributes_opt (parser);
16256 /* Peek at the next token. */
16257 token = cp_lexer_peek_token (parser->lexer);
16258 /* Check to see if the token indicates the start of a
16259 function-definition. */
16260 if (function_declarator_p (declarator)
16261 && cp_parser_token_starts_function_definition_p (token))
16263 if (!function_definition_allowed_p)
16265 /* If a function-definition should not appear here, issue an
16266 error message. */
16267 cp_parser_error (parser,
16268 "a function-definition is not allowed here");
16269 return error_mark_node;
16271 else
16273 location_t func_brace_location
16274 = cp_lexer_peek_token (parser->lexer)->location;
16276 /* Neither attributes nor an asm-specification are allowed
16277 on a function-definition. */
16278 if (asm_specification)
16279 error_at (asm_spec_start_token->location,
16280 "an asm-specification is not allowed "
16281 "on a function-definition");
16282 if (attributes)
16283 error_at (attributes_start_token->location,
16284 "attributes are not allowed on a function-definition");
16285 /* This is a function-definition. */
16286 *function_definition_p = true;
16288 /* Parse the function definition. */
16289 if (member_p)
16290 decl = cp_parser_save_member_function_body (parser,
16291 decl_specifiers,
16292 declarator,
16293 prefix_attributes);
16294 else
16295 decl
16296 = (cp_parser_function_definition_from_specifiers_and_declarator
16297 (parser, decl_specifiers, prefix_attributes, declarator));
16299 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16301 /* This is where the prologue starts... */
16302 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16303 = func_brace_location;
16306 return decl;
16310 /* [dcl.dcl]
16312 Only in function declarations for constructors, destructors, and
16313 type conversions can the decl-specifier-seq be omitted.
16315 We explicitly postpone this check past the point where we handle
16316 function-definitions because we tolerate function-definitions
16317 that are missing their return types in some modes. */
16318 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16320 cp_parser_error (parser,
16321 "expected constructor, destructor, or type conversion");
16322 return error_mark_node;
16325 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16326 if (token->type == CPP_EQ
16327 || token->type == CPP_OPEN_PAREN
16328 || token->type == CPP_OPEN_BRACE)
16330 is_initialized = SD_INITIALIZED;
16331 initialization_kind = token->type;
16332 if (maybe_range_for_decl)
16333 *maybe_range_for_decl = error_mark_node;
16335 if (token->type == CPP_EQ
16336 && function_declarator_p (declarator))
16338 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16339 if (t2->keyword == RID_DEFAULT)
16340 is_initialized = SD_DEFAULTED;
16341 else if (t2->keyword == RID_DELETE)
16342 is_initialized = SD_DELETED;
16345 else
16347 /* If the init-declarator isn't initialized and isn't followed by a
16348 `,' or `;', it's not a valid init-declarator. */
16349 if (token->type != CPP_COMMA
16350 && token->type != CPP_SEMICOLON)
16352 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16353 range_for_decl_p = true;
16354 else
16356 cp_parser_error (parser, "expected initializer");
16357 return error_mark_node;
16360 is_initialized = SD_UNINITIALIZED;
16361 initialization_kind = CPP_EOF;
16364 /* Because start_decl has side-effects, we should only call it if we
16365 know we're going ahead. By this point, we know that we cannot
16366 possibly be looking at any other construct. */
16367 cp_parser_commit_to_tentative_parse (parser);
16369 /* If the decl specifiers were bad, issue an error now that we're
16370 sure this was intended to be a declarator. Then continue
16371 declaring the variable(s), as int, to try to cut down on further
16372 errors. */
16373 if (decl_specifiers->any_specifiers_p
16374 && decl_specifiers->type == error_mark_node)
16376 cp_parser_error (parser, "invalid type in declaration");
16377 decl_specifiers->type = integer_type_node;
16380 /* Check to see whether or not this declaration is a friend. */
16381 friend_p = cp_parser_friend_p (decl_specifiers);
16383 /* Enter the newly declared entry in the symbol table. If we're
16384 processing a declaration in a class-specifier, we wait until
16385 after processing the initializer. */
16386 if (!member_p)
16388 if (parser->in_unbraced_linkage_specification_p)
16389 decl_specifiers->storage_class = sc_extern;
16390 decl = start_decl (declarator, decl_specifiers,
16391 range_for_decl_p? SD_INITIALIZED : is_initialized,
16392 attributes, prefix_attributes,
16393 &pushed_scope);
16394 /* Adjust location of decl if declarator->id_loc is more appropriate:
16395 set, and decl wasn't merged with another decl, in which case its
16396 location would be different from input_location, and more accurate. */
16397 if (DECL_P (decl)
16398 && declarator->id_loc != UNKNOWN_LOCATION
16399 && DECL_SOURCE_LOCATION (decl) == input_location)
16400 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16402 else if (scope)
16403 /* Enter the SCOPE. That way unqualified names appearing in the
16404 initializer will be looked up in SCOPE. */
16405 pushed_scope = push_scope (scope);
16407 /* Perform deferred access control checks, now that we know in which
16408 SCOPE the declared entity resides. */
16409 if (!member_p && decl)
16411 tree saved_current_function_decl = NULL_TREE;
16413 /* If the entity being declared is a function, pretend that we
16414 are in its scope. If it is a `friend', it may have access to
16415 things that would not otherwise be accessible. */
16416 if (TREE_CODE (decl) == FUNCTION_DECL)
16418 saved_current_function_decl = current_function_decl;
16419 current_function_decl = decl;
16422 /* Perform access checks for template parameters. */
16423 cp_parser_perform_template_parameter_access_checks (checks);
16425 /* Perform the access control checks for the declarator and the
16426 decl-specifiers. */
16427 perform_deferred_access_checks (tf_warning_or_error);
16429 /* Restore the saved value. */
16430 if (TREE_CODE (decl) == FUNCTION_DECL)
16431 current_function_decl = saved_current_function_decl;
16434 /* Parse the initializer. */
16435 initializer = NULL_TREE;
16436 is_direct_init = false;
16437 is_non_constant_init = true;
16438 if (is_initialized)
16440 if (function_declarator_p (declarator))
16442 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16443 if (initialization_kind == CPP_EQ)
16444 initializer = cp_parser_pure_specifier (parser);
16445 else
16447 /* If the declaration was erroneous, we don't really
16448 know what the user intended, so just silently
16449 consume the initializer. */
16450 if (decl != error_mark_node)
16451 error_at (initializer_start_token->location,
16452 "initializer provided for function");
16453 cp_parser_skip_to_closing_parenthesis (parser,
16454 /*recovering=*/true,
16455 /*or_comma=*/false,
16456 /*consume_paren=*/true);
16459 else
16461 /* We want to record the extra mangling scope for in-class
16462 initializers of class members and initializers of static data
16463 member templates. The former involves deferring
16464 parsing of the initializer until end of class as with default
16465 arguments. So right here we only handle the latter. */
16466 if (!member_p && processing_template_decl)
16467 start_lambda_scope (decl);
16468 initializer = cp_parser_initializer (parser,
16469 &is_direct_init,
16470 &is_non_constant_init);
16471 if (!member_p && processing_template_decl)
16472 finish_lambda_scope ();
16473 if (initializer == error_mark_node)
16474 cp_parser_skip_to_end_of_statement (parser);
16478 /* The old parser allows attributes to appear after a parenthesized
16479 initializer. Mark Mitchell proposed removing this functionality
16480 on the GCC mailing lists on 2002-08-13. This parser accepts the
16481 attributes -- but ignores them. */
16482 if (cp_parser_allow_gnu_extensions_p (parser)
16483 && initialization_kind == CPP_OPEN_PAREN)
16484 if (cp_parser_attributes_opt (parser))
16485 warning (OPT_Wattributes,
16486 "attributes after parenthesized initializer ignored");
16488 /* For an in-class declaration, use `grokfield' to create the
16489 declaration. */
16490 if (member_p)
16492 if (pushed_scope)
16494 pop_scope (pushed_scope);
16495 pushed_scope = NULL_TREE;
16497 decl = grokfield (declarator, decl_specifiers,
16498 initializer, !is_non_constant_init,
16499 /*asmspec=*/NULL_TREE,
16500 prefix_attributes);
16501 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16502 cp_parser_save_default_args (parser, decl);
16505 /* Finish processing the declaration. But, skip member
16506 declarations. */
16507 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16509 cp_finish_decl (decl,
16510 initializer, !is_non_constant_init,
16511 asm_specification,
16512 /* If the initializer is in parentheses, then this is
16513 a direct-initialization, which means that an
16514 `explicit' constructor is OK. Otherwise, an
16515 `explicit' constructor cannot be used. */
16516 ((is_direct_init || !is_initialized)
16517 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16519 else if ((cxx_dialect != cxx98) && friend_p
16520 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16521 /* Core issue #226 (C++0x only): A default template-argument
16522 shall not be specified in a friend class template
16523 declaration. */
16524 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16525 /*is_partial=*/false, /*is_friend_decl=*/1);
16527 if (!friend_p && pushed_scope)
16528 pop_scope (pushed_scope);
16530 return decl;
16533 /* Parse a declarator.
16535 declarator:
16536 direct-declarator
16537 ptr-operator declarator
16539 abstract-declarator:
16540 ptr-operator abstract-declarator [opt]
16541 direct-abstract-declarator
16543 GNU Extensions:
16545 declarator:
16546 attributes [opt] direct-declarator
16547 attributes [opt] ptr-operator declarator
16549 abstract-declarator:
16550 attributes [opt] ptr-operator abstract-declarator [opt]
16551 attributes [opt] direct-abstract-declarator
16553 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16554 detect constructor, destructor or conversion operators. It is set
16555 to -1 if the declarator is a name, and +1 if it is a
16556 function. Otherwise it is set to zero. Usually you just want to
16557 test for >0, but internally the negative value is used.
16559 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16560 a decl-specifier-seq unless it declares a constructor, destructor,
16561 or conversion. It might seem that we could check this condition in
16562 semantic analysis, rather than parsing, but that makes it difficult
16563 to handle something like `f()'. We want to notice that there are
16564 no decl-specifiers, and therefore realize that this is an
16565 expression, not a declaration.)
16567 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16568 the declarator is a direct-declarator of the form "(...)".
16570 MEMBER_P is true iff this declarator is a member-declarator. */
16572 static cp_declarator *
16573 cp_parser_declarator (cp_parser* parser,
16574 cp_parser_declarator_kind dcl_kind,
16575 int* ctor_dtor_or_conv_p,
16576 bool* parenthesized_p,
16577 bool member_p)
16579 cp_declarator *declarator;
16580 enum tree_code code;
16581 cp_cv_quals cv_quals;
16582 tree class_type;
16583 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16585 /* Assume this is not a constructor, destructor, or type-conversion
16586 operator. */
16587 if (ctor_dtor_or_conv_p)
16588 *ctor_dtor_or_conv_p = 0;
16590 if (cp_parser_allow_gnu_extensions_p (parser))
16591 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16593 /* Check for the ptr-operator production. */
16594 cp_parser_parse_tentatively (parser);
16595 /* Parse the ptr-operator. */
16596 code = cp_parser_ptr_operator (parser,
16597 &class_type,
16598 &cv_quals,
16599 &std_attributes);
16601 /* If that worked, then we have a ptr-operator. */
16602 if (cp_parser_parse_definitely (parser))
16604 /* If a ptr-operator was found, then this declarator was not
16605 parenthesized. */
16606 if (parenthesized_p)
16607 *parenthesized_p = true;
16608 /* The dependent declarator is optional if we are parsing an
16609 abstract-declarator. */
16610 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16611 cp_parser_parse_tentatively (parser);
16613 /* Parse the dependent declarator. */
16614 declarator = cp_parser_declarator (parser, dcl_kind,
16615 /*ctor_dtor_or_conv_p=*/NULL,
16616 /*parenthesized_p=*/NULL,
16617 /*member_p=*/false);
16619 /* If we are parsing an abstract-declarator, we must handle the
16620 case where the dependent declarator is absent. */
16621 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16622 && !cp_parser_parse_definitely (parser))
16623 declarator = NULL;
16625 declarator = cp_parser_make_indirect_declarator
16626 (code, class_type, cv_quals, declarator, std_attributes);
16628 /* Everything else is a direct-declarator. */
16629 else
16631 if (parenthesized_p)
16632 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16633 CPP_OPEN_PAREN);
16634 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16635 ctor_dtor_or_conv_p,
16636 member_p);
16639 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16640 declarator->attributes = gnu_attributes;
16641 return declarator;
16644 /* Parse a direct-declarator or direct-abstract-declarator.
16646 direct-declarator:
16647 declarator-id
16648 direct-declarator ( parameter-declaration-clause )
16649 cv-qualifier-seq [opt]
16650 ref-qualifier [opt]
16651 exception-specification [opt]
16652 direct-declarator [ constant-expression [opt] ]
16653 ( declarator )
16655 direct-abstract-declarator:
16656 direct-abstract-declarator [opt]
16657 ( parameter-declaration-clause )
16658 cv-qualifier-seq [opt]
16659 ref-qualifier [opt]
16660 exception-specification [opt]
16661 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16662 ( abstract-declarator )
16664 Returns a representation of the declarator. DCL_KIND is
16665 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16666 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16667 we are parsing a direct-declarator. It is
16668 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16669 of ambiguity we prefer an abstract declarator, as per
16670 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16671 cp_parser_declarator. */
16673 static cp_declarator *
16674 cp_parser_direct_declarator (cp_parser* parser,
16675 cp_parser_declarator_kind dcl_kind,
16676 int* ctor_dtor_or_conv_p,
16677 bool member_p)
16679 cp_token *token;
16680 cp_declarator *declarator = NULL;
16681 tree scope = NULL_TREE;
16682 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16683 bool saved_in_declarator_p = parser->in_declarator_p;
16684 bool first = true;
16685 tree pushed_scope = NULL_TREE;
16687 while (true)
16689 /* Peek at the next token. */
16690 token = cp_lexer_peek_token (parser->lexer);
16691 if (token->type == CPP_OPEN_PAREN)
16693 /* This is either a parameter-declaration-clause, or a
16694 parenthesized declarator. When we know we are parsing a
16695 named declarator, it must be a parenthesized declarator
16696 if FIRST is true. For instance, `(int)' is a
16697 parameter-declaration-clause, with an omitted
16698 direct-abstract-declarator. But `((*))', is a
16699 parenthesized abstract declarator. Finally, when T is a
16700 template parameter `(T)' is a
16701 parameter-declaration-clause, and not a parenthesized
16702 named declarator.
16704 We first try and parse a parameter-declaration-clause,
16705 and then try a nested declarator (if FIRST is true).
16707 It is not an error for it not to be a
16708 parameter-declaration-clause, even when FIRST is
16709 false. Consider,
16711 int i (int);
16712 int i (3);
16714 The first is the declaration of a function while the
16715 second is the definition of a variable, including its
16716 initializer.
16718 Having seen only the parenthesis, we cannot know which of
16719 these two alternatives should be selected. Even more
16720 complex are examples like:
16722 int i (int (a));
16723 int i (int (3));
16725 The former is a function-declaration; the latter is a
16726 variable initialization.
16728 Thus again, we try a parameter-declaration-clause, and if
16729 that fails, we back out and return. */
16731 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16733 tree params;
16734 unsigned saved_num_template_parameter_lists;
16735 bool is_declarator = false;
16736 tree t;
16738 /* In a member-declarator, the only valid interpretation
16739 of a parenthesis is the start of a
16740 parameter-declaration-clause. (It is invalid to
16741 initialize a static data member with a parenthesized
16742 initializer; only the "=" form of initialization is
16743 permitted.) */
16744 if (!member_p)
16745 cp_parser_parse_tentatively (parser);
16747 /* Consume the `('. */
16748 cp_lexer_consume_token (parser->lexer);
16749 if (first)
16751 /* If this is going to be an abstract declarator, we're
16752 in a declarator and we can't have default args. */
16753 parser->default_arg_ok_p = false;
16754 parser->in_declarator_p = true;
16757 /* Inside the function parameter list, surrounding
16758 template-parameter-lists do not apply. */
16759 saved_num_template_parameter_lists
16760 = parser->num_template_parameter_lists;
16761 parser->num_template_parameter_lists = 0;
16763 begin_scope (sk_function_parms, NULL_TREE);
16765 /* Parse the parameter-declaration-clause. */
16766 params = cp_parser_parameter_declaration_clause (parser);
16768 parser->num_template_parameter_lists
16769 = saved_num_template_parameter_lists;
16771 /* Consume the `)'. */
16772 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16774 /* If all went well, parse the cv-qualifier-seq,
16775 ref-qualifier and the exception-specification. */
16776 if (member_p || cp_parser_parse_definitely (parser))
16778 cp_cv_quals cv_quals;
16779 cp_virt_specifiers virt_specifiers;
16780 cp_ref_qualifier ref_qual;
16781 tree exception_specification;
16782 tree late_return;
16783 tree attrs;
16784 bool memfn = (member_p || (pushed_scope
16785 && CLASS_TYPE_P (pushed_scope)));
16787 is_declarator = true;
16789 if (ctor_dtor_or_conv_p)
16790 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16791 first = false;
16793 /* Parse the cv-qualifier-seq. */
16794 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16795 /* Parse the ref-qualifier. */
16796 ref_qual = cp_parser_ref_qualifier_opt (parser);
16797 /* And the exception-specification. */
16798 exception_specification
16799 = cp_parser_exception_specification_opt (parser);
16801 attrs = cp_parser_std_attribute_spec_seq (parser);
16803 late_return = (cp_parser_late_return_type_opt
16804 (parser, memfn ? cv_quals : -1));
16806 /* Parse the virt-specifier-seq. */
16807 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16809 /* Create the function-declarator. */
16810 declarator = make_call_declarator (declarator,
16811 params,
16812 cv_quals,
16813 virt_specifiers,
16814 ref_qual,
16815 exception_specification,
16816 late_return);
16817 declarator->std_attributes = attrs;
16818 /* Any subsequent parameter lists are to do with
16819 return type, so are not those of the declared
16820 function. */
16821 parser->default_arg_ok_p = false;
16824 /* Remove the function parms from scope. */
16825 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16826 pop_binding (DECL_NAME (t), t);
16827 leave_scope();
16829 if (is_declarator)
16830 /* Repeat the main loop. */
16831 continue;
16834 /* If this is the first, we can try a parenthesized
16835 declarator. */
16836 if (first)
16838 bool saved_in_type_id_in_expr_p;
16840 parser->default_arg_ok_p = saved_default_arg_ok_p;
16841 parser->in_declarator_p = saved_in_declarator_p;
16843 /* Consume the `('. */
16844 cp_lexer_consume_token (parser->lexer);
16845 /* Parse the nested declarator. */
16846 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16847 parser->in_type_id_in_expr_p = true;
16848 declarator
16849 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16850 /*parenthesized_p=*/NULL,
16851 member_p);
16852 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16853 first = false;
16854 /* Expect a `)'. */
16855 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16856 declarator = cp_error_declarator;
16857 if (declarator == cp_error_declarator)
16858 break;
16860 goto handle_declarator;
16862 /* Otherwise, we must be done. */
16863 else
16864 break;
16866 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16867 && token->type == CPP_OPEN_SQUARE
16868 && !cp_next_tokens_can_be_attribute_p (parser))
16870 /* Parse an array-declarator. */
16871 tree bounds, attrs;
16873 if (ctor_dtor_or_conv_p)
16874 *ctor_dtor_or_conv_p = 0;
16876 first = false;
16877 parser->default_arg_ok_p = false;
16878 parser->in_declarator_p = true;
16879 /* Consume the `['. */
16880 cp_lexer_consume_token (parser->lexer);
16881 /* Peek at the next token. */
16882 token = cp_lexer_peek_token (parser->lexer);
16883 /* If the next token is `]', then there is no
16884 constant-expression. */
16885 if (token->type != CPP_CLOSE_SQUARE)
16887 bool non_constant_p;
16888 bounds
16889 = cp_parser_constant_expression (parser,
16890 /*allow_non_constant=*/true,
16891 &non_constant_p);
16892 if (!non_constant_p)
16893 /* OK */;
16894 else if (error_operand_p (bounds))
16895 /* Already gave an error. */;
16896 else if (!parser->in_function_body
16897 || current_binding_level->kind == sk_function_parms)
16899 /* Normally, the array bound must be an integral constant
16900 expression. However, as an extension, we allow VLAs
16901 in function scopes as long as they aren't part of a
16902 parameter declaration. */
16903 cp_parser_error (parser,
16904 "array bound is not an integer constant");
16905 bounds = error_mark_node;
16907 else if (processing_template_decl)
16909 /* Remember this wasn't a constant-expression. */
16910 bounds = build_nop (TREE_TYPE (bounds), bounds);
16911 TREE_SIDE_EFFECTS (bounds) = 1;
16914 else
16915 bounds = NULL_TREE;
16916 /* Look for the closing `]'. */
16917 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16919 declarator = cp_error_declarator;
16920 break;
16923 attrs = cp_parser_std_attribute_spec_seq (parser);
16924 declarator = make_array_declarator (declarator, bounds);
16925 declarator->std_attributes = attrs;
16927 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16930 tree qualifying_scope;
16931 tree unqualified_name;
16932 tree attrs;
16933 special_function_kind sfk;
16934 bool abstract_ok;
16935 bool pack_expansion_p = false;
16936 cp_token *declarator_id_start_token;
16938 /* Parse a declarator-id */
16939 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16940 if (abstract_ok)
16942 cp_parser_parse_tentatively (parser);
16944 /* If we see an ellipsis, we should be looking at a
16945 parameter pack. */
16946 if (token->type == CPP_ELLIPSIS)
16948 /* Consume the `...' */
16949 cp_lexer_consume_token (parser->lexer);
16951 pack_expansion_p = true;
16955 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16956 unqualified_name
16957 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16958 qualifying_scope = parser->scope;
16959 if (abstract_ok)
16961 bool okay = false;
16963 if (!unqualified_name && pack_expansion_p)
16965 /* Check whether an error occurred. */
16966 okay = !cp_parser_error_occurred (parser);
16968 /* We already consumed the ellipsis to mark a
16969 parameter pack, but we have no way to report it,
16970 so abort the tentative parse. We will be exiting
16971 immediately anyway. */
16972 cp_parser_abort_tentative_parse (parser);
16974 else
16975 okay = cp_parser_parse_definitely (parser);
16977 if (!okay)
16978 unqualified_name = error_mark_node;
16979 else if (unqualified_name
16980 && (qualifying_scope
16981 || (!identifier_p (unqualified_name))))
16983 cp_parser_error (parser, "expected unqualified-id");
16984 unqualified_name = error_mark_node;
16988 if (!unqualified_name)
16989 return NULL;
16990 if (unqualified_name == error_mark_node)
16992 declarator = cp_error_declarator;
16993 pack_expansion_p = false;
16994 declarator->parameter_pack_p = false;
16995 break;
16998 attrs = cp_parser_std_attribute_spec_seq (parser);
17000 if (qualifying_scope && at_namespace_scope_p ()
17001 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17003 /* In the declaration of a member of a template class
17004 outside of the class itself, the SCOPE will sometimes
17005 be a TYPENAME_TYPE. For example, given:
17007 template <typename T>
17008 int S<T>::R::i = 3;
17010 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17011 this context, we must resolve S<T>::R to an ordinary
17012 type, rather than a typename type.
17014 The reason we normally avoid resolving TYPENAME_TYPEs
17015 is that a specialization of `S' might render
17016 `S<T>::R' not a type. However, if `S' is
17017 specialized, then this `i' will not be used, so there
17018 is no harm in resolving the types here. */
17019 tree type;
17021 /* Resolve the TYPENAME_TYPE. */
17022 type = resolve_typename_type (qualifying_scope,
17023 /*only_current_p=*/false);
17024 /* If that failed, the declarator is invalid. */
17025 if (TREE_CODE (type) == TYPENAME_TYPE)
17027 if (typedef_variant_p (type))
17028 error_at (declarator_id_start_token->location,
17029 "cannot define member of dependent typedef "
17030 "%qT", type);
17031 else
17032 error_at (declarator_id_start_token->location,
17033 "%<%T::%E%> is not a type",
17034 TYPE_CONTEXT (qualifying_scope),
17035 TYPE_IDENTIFIER (qualifying_scope));
17037 qualifying_scope = type;
17040 sfk = sfk_none;
17042 if (unqualified_name)
17044 tree class_type;
17046 if (qualifying_scope
17047 && CLASS_TYPE_P (qualifying_scope))
17048 class_type = qualifying_scope;
17049 else
17050 class_type = current_class_type;
17052 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17054 tree name_type = TREE_TYPE (unqualified_name);
17055 if (class_type && same_type_p (name_type, class_type))
17057 if (qualifying_scope
17058 && CLASSTYPE_USE_TEMPLATE (name_type))
17060 error_at (declarator_id_start_token->location,
17061 "invalid use of constructor as a template");
17062 inform (declarator_id_start_token->location,
17063 "use %<%T::%D%> instead of %<%T::%D%> to "
17064 "name the constructor in a qualified name",
17065 class_type,
17066 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17067 class_type, name_type);
17068 declarator = cp_error_declarator;
17069 break;
17071 else
17072 unqualified_name = constructor_name (class_type);
17074 else
17076 /* We do not attempt to print the declarator
17077 here because we do not have enough
17078 information about its original syntactic
17079 form. */
17080 cp_parser_error (parser, "invalid declarator");
17081 declarator = cp_error_declarator;
17082 break;
17086 if (class_type)
17088 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17089 sfk = sfk_destructor;
17090 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17091 sfk = sfk_conversion;
17092 else if (/* There's no way to declare a constructor
17093 for an anonymous type, even if the type
17094 got a name for linkage purposes. */
17095 !TYPE_WAS_ANONYMOUS (class_type)
17096 && constructor_name_p (unqualified_name,
17097 class_type))
17099 unqualified_name = constructor_name (class_type);
17100 sfk = sfk_constructor;
17102 else if (is_overloaded_fn (unqualified_name)
17103 && DECL_CONSTRUCTOR_P (get_first_fn
17104 (unqualified_name)))
17105 sfk = sfk_constructor;
17107 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17108 *ctor_dtor_or_conv_p = -1;
17111 declarator = make_id_declarator (qualifying_scope,
17112 unqualified_name,
17113 sfk);
17114 declarator->std_attributes = attrs;
17115 declarator->id_loc = token->location;
17116 declarator->parameter_pack_p = pack_expansion_p;
17118 if (pack_expansion_p)
17119 maybe_warn_variadic_templates ();
17122 handle_declarator:;
17123 scope = get_scope_of_declarator (declarator);
17124 if (scope)
17126 /* Any names that appear after the declarator-id for a
17127 member are looked up in the containing scope. */
17128 if (at_function_scope_p ())
17130 /* But declarations with qualified-ids can't appear in a
17131 function. */
17132 cp_parser_error (parser, "qualified-id in declaration");
17133 break;
17135 pushed_scope = push_scope (scope);
17137 parser->in_declarator_p = true;
17138 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17139 || (declarator && declarator->kind == cdk_id))
17140 /* Default args are only allowed on function
17141 declarations. */
17142 parser->default_arg_ok_p = saved_default_arg_ok_p;
17143 else
17144 parser->default_arg_ok_p = false;
17146 first = false;
17148 /* We're done. */
17149 else
17150 break;
17153 /* For an abstract declarator, we might wind up with nothing at this
17154 point. That's an error; the declarator is not optional. */
17155 if (!declarator)
17156 cp_parser_error (parser, "expected declarator");
17158 /* If we entered a scope, we must exit it now. */
17159 if (pushed_scope)
17160 pop_scope (pushed_scope);
17162 parser->default_arg_ok_p = saved_default_arg_ok_p;
17163 parser->in_declarator_p = saved_in_declarator_p;
17165 return declarator;
17168 /* Parse a ptr-operator.
17170 ptr-operator:
17171 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17172 * cv-qualifier-seq [opt]
17174 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17175 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17177 GNU Extension:
17179 ptr-operator:
17180 & cv-qualifier-seq [opt]
17182 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17183 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17184 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17185 filled in with the TYPE containing the member. *CV_QUALS is
17186 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17187 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17188 Note that the tree codes returned by this function have nothing
17189 to do with the types of trees that will be eventually be created
17190 to represent the pointer or reference type being parsed. They are
17191 just constants with suggestive names. */
17192 static enum tree_code
17193 cp_parser_ptr_operator (cp_parser* parser,
17194 tree* type,
17195 cp_cv_quals *cv_quals,
17196 tree *attributes)
17198 enum tree_code code = ERROR_MARK;
17199 cp_token *token;
17200 tree attrs = NULL_TREE;
17202 /* Assume that it's not a pointer-to-member. */
17203 *type = NULL_TREE;
17204 /* And that there are no cv-qualifiers. */
17205 *cv_quals = TYPE_UNQUALIFIED;
17207 /* Peek at the next token. */
17208 token = cp_lexer_peek_token (parser->lexer);
17210 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17211 if (token->type == CPP_MULT)
17212 code = INDIRECT_REF;
17213 else if (token->type == CPP_AND)
17214 code = ADDR_EXPR;
17215 else if ((cxx_dialect != cxx98) &&
17216 token->type == CPP_AND_AND) /* C++0x only */
17217 code = NON_LVALUE_EXPR;
17219 if (code != ERROR_MARK)
17221 /* Consume the `*', `&' or `&&'. */
17222 cp_lexer_consume_token (parser->lexer);
17224 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17225 `&', if we are allowing GNU extensions. (The only qualifier
17226 that can legally appear after `&' is `restrict', but that is
17227 enforced during semantic analysis. */
17228 if (code == INDIRECT_REF
17229 || cp_parser_allow_gnu_extensions_p (parser))
17230 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17232 attrs = cp_parser_std_attribute_spec_seq (parser);
17233 if (attributes != NULL)
17234 *attributes = attrs;
17236 else
17238 /* Try the pointer-to-member case. */
17239 cp_parser_parse_tentatively (parser);
17240 /* Look for the optional `::' operator. */
17241 cp_parser_global_scope_opt (parser,
17242 /*current_scope_valid_p=*/false);
17243 /* Look for the nested-name specifier. */
17244 token = cp_lexer_peek_token (parser->lexer);
17245 cp_parser_nested_name_specifier (parser,
17246 /*typename_keyword_p=*/false,
17247 /*check_dependency_p=*/true,
17248 /*type_p=*/false,
17249 /*is_declaration=*/false);
17250 /* If we found it, and the next token is a `*', then we are
17251 indeed looking at a pointer-to-member operator. */
17252 if (!cp_parser_error_occurred (parser)
17253 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17255 /* Indicate that the `*' operator was used. */
17256 code = INDIRECT_REF;
17258 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17259 error_at (token->location, "%qD is a namespace", parser->scope);
17260 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17261 error_at (token->location, "cannot form pointer to member of "
17262 "non-class %q#T", parser->scope);
17263 else
17265 /* The type of which the member is a member is given by the
17266 current SCOPE. */
17267 *type = parser->scope;
17268 /* The next name will not be qualified. */
17269 parser->scope = NULL_TREE;
17270 parser->qualifying_scope = NULL_TREE;
17271 parser->object_scope = NULL_TREE;
17272 /* Look for optional c++11 attributes. */
17273 attrs = cp_parser_std_attribute_spec_seq (parser);
17274 if (attributes != NULL)
17275 *attributes = attrs;
17276 /* Look for the optional cv-qualifier-seq. */
17277 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17280 /* If that didn't work we don't have a ptr-operator. */
17281 if (!cp_parser_parse_definitely (parser))
17282 cp_parser_error (parser, "expected ptr-operator");
17285 return code;
17288 /* Parse an (optional) cv-qualifier-seq.
17290 cv-qualifier-seq:
17291 cv-qualifier cv-qualifier-seq [opt]
17293 cv-qualifier:
17294 const
17295 volatile
17297 GNU Extension:
17299 cv-qualifier:
17300 __restrict__
17302 Returns a bitmask representing the cv-qualifiers. */
17304 static cp_cv_quals
17305 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17307 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17309 while (true)
17311 cp_token *token;
17312 cp_cv_quals cv_qualifier;
17314 /* Peek at the next token. */
17315 token = cp_lexer_peek_token (parser->lexer);
17316 /* See if it's a cv-qualifier. */
17317 switch (token->keyword)
17319 case RID_CONST:
17320 cv_qualifier = TYPE_QUAL_CONST;
17321 break;
17323 case RID_VOLATILE:
17324 cv_qualifier = TYPE_QUAL_VOLATILE;
17325 break;
17327 case RID_RESTRICT:
17328 cv_qualifier = TYPE_QUAL_RESTRICT;
17329 break;
17331 default:
17332 cv_qualifier = TYPE_UNQUALIFIED;
17333 break;
17336 if (!cv_qualifier)
17337 break;
17339 if (cv_quals & cv_qualifier)
17341 error_at (token->location, "duplicate cv-qualifier");
17342 cp_lexer_purge_token (parser->lexer);
17344 else
17346 cp_lexer_consume_token (parser->lexer);
17347 cv_quals |= cv_qualifier;
17351 return cv_quals;
17354 /* Parse an (optional) ref-qualifier
17356 ref-qualifier:
17360 Returns cp_ref_qualifier representing ref-qualifier. */
17362 static cp_ref_qualifier
17363 cp_parser_ref_qualifier_opt (cp_parser* parser)
17365 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17367 while (true)
17369 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17370 cp_token *token = cp_lexer_peek_token (parser->lexer);
17372 switch (token->type)
17374 case CPP_AND:
17375 curr_ref_qual = REF_QUAL_LVALUE;
17376 break;
17378 case CPP_AND_AND:
17379 curr_ref_qual = REF_QUAL_RVALUE;
17380 break;
17382 default:
17383 curr_ref_qual = REF_QUAL_NONE;
17384 break;
17387 if (!curr_ref_qual)
17388 break;
17389 else if (ref_qual)
17391 error_at (token->location, "multiple ref-qualifiers");
17392 cp_lexer_purge_token (parser->lexer);
17394 else
17396 ref_qual = curr_ref_qual;
17397 cp_lexer_consume_token (parser->lexer);
17401 return ref_qual;
17404 /* Parse an (optional) virt-specifier-seq.
17406 virt-specifier-seq:
17407 virt-specifier virt-specifier-seq [opt]
17409 virt-specifier:
17410 override
17411 final
17413 Returns a bitmask representing the virt-specifiers. */
17415 static cp_virt_specifiers
17416 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17418 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17420 while (true)
17422 cp_token *token;
17423 cp_virt_specifiers virt_specifier;
17425 /* Peek at the next token. */
17426 token = cp_lexer_peek_token (parser->lexer);
17427 /* See if it's a virt-specifier-qualifier. */
17428 if (token->type != CPP_NAME)
17429 break;
17430 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17432 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17433 virt_specifier = VIRT_SPEC_OVERRIDE;
17435 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17437 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17438 virt_specifier = VIRT_SPEC_FINAL;
17440 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17442 virt_specifier = VIRT_SPEC_FINAL;
17444 else
17445 break;
17447 if (virt_specifiers & virt_specifier)
17449 error_at (token->location, "duplicate virt-specifier");
17450 cp_lexer_purge_token (parser->lexer);
17452 else
17454 cp_lexer_consume_token (parser->lexer);
17455 virt_specifiers |= virt_specifier;
17458 return virt_specifiers;
17461 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17462 is in scope even though it isn't real. */
17464 static void
17465 inject_this_parameter (tree ctype, cp_cv_quals quals)
17467 tree this_parm;
17469 if (current_class_ptr)
17471 /* We don't clear this between NSDMIs. Is it already what we want? */
17472 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17473 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17474 && cp_type_quals (type) == quals)
17475 return;
17478 this_parm = build_this_parm (ctype, quals);
17479 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17480 current_class_ptr = NULL_TREE;
17481 current_class_ref
17482 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17483 current_class_ptr = this_parm;
17486 /* Return true iff our current scope is a non-static data member
17487 initializer. */
17489 bool
17490 parsing_nsdmi (void)
17492 /* We recognize NSDMI context by the context-less 'this' pointer set up
17493 by the function above. */
17494 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17495 return true;
17496 return false;
17499 /* Parse a late-specified return type, if any. This is not a separate
17500 non-terminal, but part of a function declarator, which looks like
17502 -> trailing-type-specifier-seq abstract-declarator(opt)
17504 Returns the type indicated by the type-id.
17506 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17507 function. */
17509 static tree
17510 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
17512 cp_token *token;
17513 tree type;
17515 /* Peek at the next token. */
17516 token = cp_lexer_peek_token (parser->lexer);
17517 /* A late-specified return type is indicated by an initial '->'. */
17518 if (token->type != CPP_DEREF)
17519 return NULL_TREE;
17521 /* Consume the ->. */
17522 cp_lexer_consume_token (parser->lexer);
17524 tree save_ccp = current_class_ptr;
17525 tree save_ccr = current_class_ref;
17526 if (quals >= 0)
17528 /* DR 1207: 'this' is in scope in the trailing return type. */
17529 inject_this_parameter (current_class_type, quals);
17532 type = cp_parser_trailing_type_id (parser);
17534 if (quals >= 0)
17536 current_class_ptr = save_ccp;
17537 current_class_ref = save_ccr;
17540 return type;
17543 /* Parse a declarator-id.
17545 declarator-id:
17546 id-expression
17547 :: [opt] nested-name-specifier [opt] type-name
17549 In the `id-expression' case, the value returned is as for
17550 cp_parser_id_expression if the id-expression was an unqualified-id.
17551 If the id-expression was a qualified-id, then a SCOPE_REF is
17552 returned. The first operand is the scope (either a NAMESPACE_DECL
17553 or TREE_TYPE), but the second is still just a representation of an
17554 unqualified-id. */
17556 static tree
17557 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17559 tree id;
17560 /* The expression must be an id-expression. Assume that qualified
17561 names are the names of types so that:
17563 template <class T>
17564 int S<T>::R::i = 3;
17566 will work; we must treat `S<T>::R' as the name of a type.
17567 Similarly, assume that qualified names are templates, where
17568 required, so that:
17570 template <class T>
17571 int S<T>::R<T>::i = 3;
17573 will work, too. */
17574 id = cp_parser_id_expression (parser,
17575 /*template_keyword_p=*/false,
17576 /*check_dependency_p=*/false,
17577 /*template_p=*/NULL,
17578 /*declarator_p=*/true,
17579 optional_p);
17580 if (id && BASELINK_P (id))
17581 id = BASELINK_FUNCTIONS (id);
17582 return id;
17585 /* Parse a type-id.
17587 type-id:
17588 type-specifier-seq abstract-declarator [opt]
17590 Returns the TYPE specified. */
17592 static tree
17593 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17594 bool is_trailing_return)
17596 cp_decl_specifier_seq type_specifier_seq;
17597 cp_declarator *abstract_declarator;
17599 /* Parse the type-specifier-seq. */
17600 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17601 is_trailing_return,
17602 &type_specifier_seq);
17603 if (type_specifier_seq.type == error_mark_node)
17604 return error_mark_node;
17606 /* There might or might not be an abstract declarator. */
17607 cp_parser_parse_tentatively (parser);
17608 /* Look for the declarator. */
17609 abstract_declarator
17610 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17611 /*parenthesized_p=*/NULL,
17612 /*member_p=*/false);
17613 /* Check to see if there really was a declarator. */
17614 if (!cp_parser_parse_definitely (parser))
17615 abstract_declarator = NULL;
17617 if (type_specifier_seq.type
17618 && cxx_dialect < cxx1y
17619 && type_uses_auto (type_specifier_seq.type))
17621 /* A type-id with type 'auto' is only ok if the abstract declarator
17622 is a function declarator with a late-specified return type. */
17623 if (abstract_declarator
17624 && abstract_declarator->kind == cdk_function
17625 && abstract_declarator->u.function.late_return_type)
17626 /* OK */;
17627 else
17629 error ("invalid use of %<auto%>");
17630 return error_mark_node;
17634 return groktypename (&type_specifier_seq, abstract_declarator,
17635 is_template_arg);
17638 static tree cp_parser_type_id (cp_parser *parser)
17640 return cp_parser_type_id_1 (parser, false, false);
17643 static tree cp_parser_template_type_arg (cp_parser *parser)
17645 tree r;
17646 const char *saved_message = parser->type_definition_forbidden_message;
17647 parser->type_definition_forbidden_message
17648 = G_("types may not be defined in template arguments");
17649 r = cp_parser_type_id_1 (parser, true, false);
17650 parser->type_definition_forbidden_message = saved_message;
17651 return r;
17654 static tree cp_parser_trailing_type_id (cp_parser *parser)
17656 return cp_parser_type_id_1 (parser, false, true);
17659 /* Parse a type-specifier-seq.
17661 type-specifier-seq:
17662 type-specifier type-specifier-seq [opt]
17664 GNU extension:
17666 type-specifier-seq:
17667 attributes type-specifier-seq [opt]
17669 If IS_DECLARATION is true, we are at the start of a "condition" or
17670 exception-declaration, so we might be followed by a declarator-id.
17672 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17673 i.e. we've just seen "->".
17675 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17677 static void
17678 cp_parser_type_specifier_seq (cp_parser* parser,
17679 bool is_declaration,
17680 bool is_trailing_return,
17681 cp_decl_specifier_seq *type_specifier_seq)
17683 bool seen_type_specifier = false;
17684 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17685 cp_token *start_token = NULL;
17687 /* Clear the TYPE_SPECIFIER_SEQ. */
17688 clear_decl_specs (type_specifier_seq);
17690 /* In the context of a trailing return type, enum E { } is an
17691 elaborated-type-specifier followed by a function-body, not an
17692 enum-specifier. */
17693 if (is_trailing_return)
17694 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17696 /* Parse the type-specifiers and attributes. */
17697 while (true)
17699 tree type_specifier;
17700 bool is_cv_qualifier;
17702 /* Check for attributes first. */
17703 if (cp_next_tokens_can_be_attribute_p (parser))
17705 type_specifier_seq->attributes =
17706 chainon (type_specifier_seq->attributes,
17707 cp_parser_attributes_opt (parser));
17708 continue;
17711 /* record the token of the beginning of the type specifier seq,
17712 for error reporting purposes*/
17713 if (!start_token)
17714 start_token = cp_lexer_peek_token (parser->lexer);
17716 /* Look for the type-specifier. */
17717 type_specifier = cp_parser_type_specifier (parser,
17718 flags,
17719 type_specifier_seq,
17720 /*is_declaration=*/false,
17721 NULL,
17722 &is_cv_qualifier);
17723 if (!type_specifier)
17725 /* If the first type-specifier could not be found, this is not a
17726 type-specifier-seq at all. */
17727 if (!seen_type_specifier)
17729 cp_parser_error (parser, "expected type-specifier");
17730 type_specifier_seq->type = error_mark_node;
17731 return;
17733 /* If subsequent type-specifiers could not be found, the
17734 type-specifier-seq is complete. */
17735 break;
17738 seen_type_specifier = true;
17739 /* The standard says that a condition can be:
17741 type-specifier-seq declarator = assignment-expression
17743 However, given:
17745 struct S {};
17746 if (int S = ...)
17748 we should treat the "S" as a declarator, not as a
17749 type-specifier. The standard doesn't say that explicitly for
17750 type-specifier-seq, but it does say that for
17751 decl-specifier-seq in an ordinary declaration. Perhaps it
17752 would be clearer just to allow a decl-specifier-seq here, and
17753 then add a semantic restriction that if any decl-specifiers
17754 that are not type-specifiers appear, the program is invalid. */
17755 if (is_declaration && !is_cv_qualifier)
17756 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17760 /* Parse a parameter-declaration-clause.
17762 parameter-declaration-clause:
17763 parameter-declaration-list [opt] ... [opt]
17764 parameter-declaration-list , ...
17766 Returns a representation for the parameter declarations. A return
17767 value of NULL indicates a parameter-declaration-clause consisting
17768 only of an ellipsis. */
17770 static tree
17771 cp_parser_parameter_declaration_clause (cp_parser* parser)
17773 tree parameters;
17774 cp_token *token;
17775 bool ellipsis_p;
17776 bool is_error;
17778 /* Peek at the next token. */
17779 token = cp_lexer_peek_token (parser->lexer);
17780 /* Check for trivial parameter-declaration-clauses. */
17781 if (token->type == CPP_ELLIPSIS)
17783 /* Consume the `...' token. */
17784 cp_lexer_consume_token (parser->lexer);
17785 return NULL_TREE;
17787 else if (token->type == CPP_CLOSE_PAREN)
17788 /* There are no parameters. */
17790 #ifndef NO_IMPLICIT_EXTERN_C
17791 if (in_system_header && current_class_type == NULL
17792 && current_lang_name == lang_name_c)
17793 return NULL_TREE;
17794 else
17795 #endif
17796 return void_list_node;
17798 /* Check for `(void)', too, which is a special case. */
17799 else if (token->keyword == RID_VOID
17800 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17801 == CPP_CLOSE_PAREN))
17803 /* Consume the `void' token. */
17804 cp_lexer_consume_token (parser->lexer);
17805 /* There are no parameters. */
17806 return void_list_node;
17809 /* Parse the parameter-declaration-list. */
17810 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17811 /* If a parse error occurred while parsing the
17812 parameter-declaration-list, then the entire
17813 parameter-declaration-clause is erroneous. */
17814 if (is_error)
17815 return NULL;
17817 /* Peek at the next token. */
17818 token = cp_lexer_peek_token (parser->lexer);
17819 /* If it's a `,', the clause should terminate with an ellipsis. */
17820 if (token->type == CPP_COMMA)
17822 /* Consume the `,'. */
17823 cp_lexer_consume_token (parser->lexer);
17824 /* Expect an ellipsis. */
17825 ellipsis_p
17826 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17828 /* It might also be `...' if the optional trailing `,' was
17829 omitted. */
17830 else if (token->type == CPP_ELLIPSIS)
17832 /* Consume the `...' token. */
17833 cp_lexer_consume_token (parser->lexer);
17834 /* And remember that we saw it. */
17835 ellipsis_p = true;
17837 else
17838 ellipsis_p = false;
17840 /* Finish the parameter list. */
17841 if (!ellipsis_p)
17842 parameters = chainon (parameters, void_list_node);
17844 return parameters;
17847 /* Parse a parameter-declaration-list.
17849 parameter-declaration-list:
17850 parameter-declaration
17851 parameter-declaration-list , parameter-declaration
17853 Returns a representation of the parameter-declaration-list, as for
17854 cp_parser_parameter_declaration_clause. However, the
17855 `void_list_node' is never appended to the list. Upon return,
17856 *IS_ERROR will be true iff an error occurred. */
17858 static tree
17859 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17861 tree parameters = NULL_TREE;
17862 tree *tail = &parameters;
17863 bool saved_in_unbraced_linkage_specification_p;
17864 int index = 0;
17866 /* Assume all will go well. */
17867 *is_error = false;
17868 /* The special considerations that apply to a function within an
17869 unbraced linkage specifications do not apply to the parameters
17870 to the function. */
17871 saved_in_unbraced_linkage_specification_p
17872 = parser->in_unbraced_linkage_specification_p;
17873 parser->in_unbraced_linkage_specification_p = false;
17875 /* Look for more parameters. */
17876 while (true)
17878 cp_parameter_declarator *parameter;
17879 tree decl = error_mark_node;
17880 bool parenthesized_p = false;
17881 /* Parse the parameter. */
17882 parameter
17883 = cp_parser_parameter_declaration (parser,
17884 /*template_parm_p=*/false,
17885 &parenthesized_p);
17887 /* We don't know yet if the enclosing context is deprecated, so wait
17888 and warn in grokparms if appropriate. */
17889 deprecated_state = DEPRECATED_SUPPRESS;
17891 if (parameter)
17892 decl = grokdeclarator (parameter->declarator,
17893 &parameter->decl_specifiers,
17894 PARM,
17895 parameter->default_argument != NULL_TREE,
17896 &parameter->decl_specifiers.attributes);
17898 deprecated_state = DEPRECATED_NORMAL;
17900 /* If a parse error occurred parsing the parameter declaration,
17901 then the entire parameter-declaration-list is erroneous. */
17902 if (decl == error_mark_node)
17904 *is_error = true;
17905 parameters = error_mark_node;
17906 break;
17909 if (parameter->decl_specifiers.attributes)
17910 cplus_decl_attributes (&decl,
17911 parameter->decl_specifiers.attributes,
17913 if (DECL_NAME (decl))
17914 decl = pushdecl (decl);
17916 if (decl != error_mark_node)
17918 retrofit_lang_decl (decl);
17919 DECL_PARM_INDEX (decl) = ++index;
17920 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17923 /* Add the new parameter to the list. */
17924 *tail = build_tree_list (parameter->default_argument, decl);
17925 tail = &TREE_CHAIN (*tail);
17927 /* Peek at the next token. */
17928 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17929 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17930 /* These are for Objective-C++ */
17931 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17932 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17933 /* The parameter-declaration-list is complete. */
17934 break;
17935 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17937 cp_token *token;
17939 /* Peek at the next token. */
17940 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17941 /* If it's an ellipsis, then the list is complete. */
17942 if (token->type == CPP_ELLIPSIS)
17943 break;
17944 /* Otherwise, there must be more parameters. Consume the
17945 `,'. */
17946 cp_lexer_consume_token (parser->lexer);
17947 /* When parsing something like:
17949 int i(float f, double d)
17951 we can tell after seeing the declaration for "f" that we
17952 are not looking at an initialization of a variable "i",
17953 but rather at the declaration of a function "i".
17955 Due to the fact that the parsing of template arguments
17956 (as specified to a template-id) requires backtracking we
17957 cannot use this technique when inside a template argument
17958 list. */
17959 if (!parser->in_template_argument_list_p
17960 && !parser->in_type_id_in_expr_p
17961 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17962 /* However, a parameter-declaration of the form
17963 "float(f)" (which is a valid declaration of a
17964 parameter "f") can also be interpreted as an
17965 expression (the conversion of "f" to "float"). */
17966 && !parenthesized_p)
17967 cp_parser_commit_to_tentative_parse (parser);
17969 else
17971 cp_parser_error (parser, "expected %<,%> or %<...%>");
17972 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17973 cp_parser_skip_to_closing_parenthesis (parser,
17974 /*recovering=*/true,
17975 /*or_comma=*/false,
17976 /*consume_paren=*/false);
17977 break;
17981 parser->in_unbraced_linkage_specification_p
17982 = saved_in_unbraced_linkage_specification_p;
17984 return parameters;
17987 /* Parse a parameter declaration.
17989 parameter-declaration:
17990 decl-specifier-seq ... [opt] declarator
17991 decl-specifier-seq declarator = assignment-expression
17992 decl-specifier-seq ... [opt] abstract-declarator [opt]
17993 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17995 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17996 declares a template parameter. (In that case, a non-nested `>'
17997 token encountered during the parsing of the assignment-expression
17998 is not interpreted as a greater-than operator.)
18000 Returns a representation of the parameter, or NULL if an error
18001 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18002 true iff the declarator is of the form "(p)". */
18004 static cp_parameter_declarator *
18005 cp_parser_parameter_declaration (cp_parser *parser,
18006 bool template_parm_p,
18007 bool *parenthesized_p)
18009 int declares_class_or_enum;
18010 cp_decl_specifier_seq decl_specifiers;
18011 cp_declarator *declarator;
18012 tree default_argument;
18013 cp_token *token = NULL, *declarator_token_start = NULL;
18014 const char *saved_message;
18016 /* In a template parameter, `>' is not an operator.
18018 [temp.param]
18020 When parsing a default template-argument for a non-type
18021 template-parameter, the first non-nested `>' is taken as the end
18022 of the template parameter-list rather than a greater-than
18023 operator. */
18025 /* Type definitions may not appear in parameter types. */
18026 saved_message = parser->type_definition_forbidden_message;
18027 parser->type_definition_forbidden_message
18028 = G_("types may not be defined in parameter types");
18030 /* Parse the declaration-specifiers. */
18031 cp_parser_decl_specifier_seq (parser,
18032 CP_PARSER_FLAGS_NONE,
18033 &decl_specifiers,
18034 &declares_class_or_enum);
18036 /* Complain about missing 'typename' or other invalid type names. */
18037 if (!decl_specifiers.any_type_specifiers_p
18038 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18039 decl_specifiers.type = error_mark_node;
18041 /* If an error occurred, there's no reason to attempt to parse the
18042 rest of the declaration. */
18043 if (cp_parser_error_occurred (parser))
18045 parser->type_definition_forbidden_message = saved_message;
18046 return NULL;
18049 /* Peek at the next token. */
18050 token = cp_lexer_peek_token (parser->lexer);
18052 /* If the next token is a `)', `,', `=', `>', or `...', then there
18053 is no declarator. However, when variadic templates are enabled,
18054 there may be a declarator following `...'. */
18055 if (token->type == CPP_CLOSE_PAREN
18056 || token->type == CPP_COMMA
18057 || token->type == CPP_EQ
18058 || token->type == CPP_GREATER)
18060 declarator = NULL;
18061 if (parenthesized_p)
18062 *parenthesized_p = false;
18064 /* Otherwise, there should be a declarator. */
18065 else
18067 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18068 parser->default_arg_ok_p = false;
18070 /* After seeing a decl-specifier-seq, if the next token is not a
18071 "(", there is no possibility that the code is a valid
18072 expression. Therefore, if parsing tentatively, we commit at
18073 this point. */
18074 if (!parser->in_template_argument_list_p
18075 /* In an expression context, having seen:
18077 (int((char ...
18079 we cannot be sure whether we are looking at a
18080 function-type (taking a "char" as a parameter) or a cast
18081 of some object of type "char" to "int". */
18082 && !parser->in_type_id_in_expr_p
18083 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18084 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18085 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18086 cp_parser_commit_to_tentative_parse (parser);
18087 /* Parse the declarator. */
18088 declarator_token_start = token;
18089 declarator = cp_parser_declarator (parser,
18090 CP_PARSER_DECLARATOR_EITHER,
18091 /*ctor_dtor_or_conv_p=*/NULL,
18092 parenthesized_p,
18093 /*member_p=*/false);
18094 parser->default_arg_ok_p = saved_default_arg_ok_p;
18095 /* After the declarator, allow more attributes. */
18096 decl_specifiers.attributes
18097 = chainon (decl_specifiers.attributes,
18098 cp_parser_attributes_opt (parser));
18101 /* If the next token is an ellipsis, and we have not seen a
18102 declarator name, and the type of the declarator contains parameter
18103 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18104 a parameter pack expansion expression. Otherwise, leave the
18105 ellipsis for a C-style variadic function. */
18106 token = cp_lexer_peek_token (parser->lexer);
18107 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18109 tree type = decl_specifiers.type;
18111 if (type && DECL_P (type))
18112 type = TREE_TYPE (type);
18114 if (type
18115 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18116 && declarator_can_be_parameter_pack (declarator)
18117 && (!declarator || !declarator->parameter_pack_p)
18118 && uses_parameter_packs (type))
18120 /* Consume the `...'. */
18121 cp_lexer_consume_token (parser->lexer);
18122 maybe_warn_variadic_templates ();
18124 /* Build a pack expansion type */
18125 if (declarator)
18126 declarator->parameter_pack_p = true;
18127 else
18128 decl_specifiers.type = make_pack_expansion (type);
18132 /* The restriction on defining new types applies only to the type
18133 of the parameter, not to the default argument. */
18134 parser->type_definition_forbidden_message = saved_message;
18136 /* If the next token is `=', then process a default argument. */
18137 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18139 token = cp_lexer_peek_token (parser->lexer);
18140 /* If we are defining a class, then the tokens that make up the
18141 default argument must be saved and processed later. */
18142 if (!template_parm_p && at_class_scope_p ()
18143 && TYPE_BEING_DEFINED (current_class_type)
18144 && !LAMBDA_TYPE_P (current_class_type))
18145 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18146 /* Outside of a class definition, we can just parse the
18147 assignment-expression. */
18148 else
18149 default_argument
18150 = cp_parser_default_argument (parser, template_parm_p);
18152 if (!parser->default_arg_ok_p)
18154 if (flag_permissive)
18155 warning (0, "deprecated use of default argument for parameter of non-function");
18156 else
18158 error_at (token->location,
18159 "default arguments are only "
18160 "permitted for function parameters");
18161 default_argument = NULL_TREE;
18164 else if ((declarator && declarator->parameter_pack_p)
18165 || (decl_specifiers.type
18166 && PACK_EXPANSION_P (decl_specifiers.type)))
18168 /* Find the name of the parameter pack. */
18169 cp_declarator *id_declarator = declarator;
18170 while (id_declarator && id_declarator->kind != cdk_id)
18171 id_declarator = id_declarator->declarator;
18173 if (id_declarator && id_declarator->kind == cdk_id)
18174 error_at (declarator_token_start->location,
18175 template_parm_p
18176 ? G_("template parameter pack %qD "
18177 "cannot have a default argument")
18178 : G_("parameter pack %qD cannot have "
18179 "a default argument"),
18180 id_declarator->u.id.unqualified_name);
18181 else
18182 error_at (declarator_token_start->location,
18183 template_parm_p
18184 ? G_("template parameter pack cannot have "
18185 "a default argument")
18186 : G_("parameter pack cannot have a "
18187 "default argument"));
18189 default_argument = NULL_TREE;
18192 else
18193 default_argument = NULL_TREE;
18195 return make_parameter_declarator (&decl_specifiers,
18196 declarator,
18197 default_argument);
18200 /* Parse a default argument and return it.
18202 TEMPLATE_PARM_P is true if this is a default argument for a
18203 non-type template parameter. */
18204 static tree
18205 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18207 tree default_argument = NULL_TREE;
18208 bool saved_greater_than_is_operator_p;
18209 bool saved_local_variables_forbidden_p;
18210 bool non_constant_p, is_direct_init;
18212 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18213 set correctly. */
18214 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18215 parser->greater_than_is_operator_p = !template_parm_p;
18216 /* Local variable names (and the `this' keyword) may not
18217 appear in a default argument. */
18218 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18219 parser->local_variables_forbidden_p = true;
18220 /* Parse the assignment-expression. */
18221 if (template_parm_p)
18222 push_deferring_access_checks (dk_no_deferred);
18223 default_argument
18224 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18225 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18226 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18227 if (template_parm_p)
18228 pop_deferring_access_checks ();
18229 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18230 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18232 return default_argument;
18235 /* Parse a function-body.
18237 function-body:
18238 compound_statement */
18240 static void
18241 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18243 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18246 /* Parse a ctor-initializer-opt followed by a function-body. Return
18247 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18248 is true we are parsing a function-try-block. */
18250 static bool
18251 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18252 bool in_function_try_block)
18254 tree body, list;
18255 bool ctor_initializer_p;
18256 const bool check_body_p =
18257 DECL_CONSTRUCTOR_P (current_function_decl)
18258 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18259 tree last = NULL;
18261 /* Begin the function body. */
18262 body = begin_function_body ();
18263 /* Parse the optional ctor-initializer. */
18264 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18266 /* If we're parsing a constexpr constructor definition, we need
18267 to check that the constructor body is indeed empty. However,
18268 before we get to cp_parser_function_body lot of junk has been
18269 generated, so we can't just check that we have an empty block.
18270 Rather we take a snapshot of the outermost block, and check whether
18271 cp_parser_function_body changed its state. */
18272 if (check_body_p)
18274 list = cur_stmt_list;
18275 if (STATEMENT_LIST_TAIL (list))
18276 last = STATEMENT_LIST_TAIL (list)->stmt;
18278 /* Parse the function-body. */
18279 cp_parser_function_body (parser, in_function_try_block);
18280 if (check_body_p)
18281 check_constexpr_ctor_body (last, list);
18282 /* Finish the function body. */
18283 finish_function_body (body);
18285 return ctor_initializer_p;
18288 /* Parse an initializer.
18290 initializer:
18291 = initializer-clause
18292 ( expression-list )
18294 Returns an expression representing the initializer. If no
18295 initializer is present, NULL_TREE is returned.
18297 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18298 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18299 set to TRUE if there is no initializer present. If there is an
18300 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18301 is set to true; otherwise it is set to false. */
18303 static tree
18304 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18305 bool* non_constant_p)
18307 cp_token *token;
18308 tree init;
18310 /* Peek at the next token. */
18311 token = cp_lexer_peek_token (parser->lexer);
18313 /* Let our caller know whether or not this initializer was
18314 parenthesized. */
18315 *is_direct_init = (token->type != CPP_EQ);
18316 /* Assume that the initializer is constant. */
18317 *non_constant_p = false;
18319 if (token->type == CPP_EQ)
18321 /* Consume the `='. */
18322 cp_lexer_consume_token (parser->lexer);
18323 /* Parse the initializer-clause. */
18324 init = cp_parser_initializer_clause (parser, non_constant_p);
18326 else if (token->type == CPP_OPEN_PAREN)
18328 vec<tree, va_gc> *vec;
18329 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18330 /*cast_p=*/false,
18331 /*allow_expansion_p=*/true,
18332 non_constant_p);
18333 if (vec == NULL)
18334 return error_mark_node;
18335 init = build_tree_list_vec (vec);
18336 release_tree_vector (vec);
18338 else if (token->type == CPP_OPEN_BRACE)
18340 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18341 init = cp_parser_braced_list (parser, non_constant_p);
18342 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18344 else
18346 /* Anything else is an error. */
18347 cp_parser_error (parser, "expected initializer");
18348 init = error_mark_node;
18351 return init;
18354 /* Parse an initializer-clause.
18356 initializer-clause:
18357 assignment-expression
18358 braced-init-list
18360 Returns an expression representing the initializer.
18362 If the `assignment-expression' production is used the value
18363 returned is simply a representation for the expression.
18365 Otherwise, calls cp_parser_braced_list. */
18367 static tree
18368 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18370 tree initializer;
18372 /* Assume the expression is constant. */
18373 *non_constant_p = false;
18375 /* If it is not a `{', then we are looking at an
18376 assignment-expression. */
18377 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18379 initializer
18380 = cp_parser_constant_expression (parser,
18381 /*allow_non_constant_p=*/true,
18382 non_constant_p);
18384 else
18385 initializer = cp_parser_braced_list (parser, non_constant_p);
18387 return initializer;
18390 /* Parse a brace-enclosed initializer list.
18392 braced-init-list:
18393 { initializer-list , [opt] }
18396 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18397 the elements of the initializer-list (or NULL, if the last
18398 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18399 NULL_TREE. There is no way to detect whether or not the optional
18400 trailing `,' was provided. NON_CONSTANT_P is as for
18401 cp_parser_initializer. */
18403 static tree
18404 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18406 tree initializer;
18408 /* Consume the `{' token. */
18409 cp_lexer_consume_token (parser->lexer);
18410 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18411 initializer = make_node (CONSTRUCTOR);
18412 /* If it's not a `}', then there is a non-trivial initializer. */
18413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18415 /* Parse the initializer list. */
18416 CONSTRUCTOR_ELTS (initializer)
18417 = cp_parser_initializer_list (parser, non_constant_p);
18418 /* A trailing `,' token is allowed. */
18419 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18420 cp_lexer_consume_token (parser->lexer);
18422 else
18423 *non_constant_p = false;
18424 /* Now, there should be a trailing `}'. */
18425 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18426 TREE_TYPE (initializer) = init_list_type_node;
18427 return initializer;
18430 /* Parse an initializer-list.
18432 initializer-list:
18433 initializer-clause ... [opt]
18434 initializer-list , initializer-clause ... [opt]
18436 GNU Extension:
18438 initializer-list:
18439 designation initializer-clause ...[opt]
18440 initializer-list , designation initializer-clause ...[opt]
18442 designation:
18443 . identifier =
18444 identifier :
18445 [ constant-expression ] =
18447 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18448 for the initializer. If the INDEX of the elt is non-NULL, it is the
18449 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18450 as for cp_parser_initializer. */
18452 static vec<constructor_elt, va_gc> *
18453 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18455 vec<constructor_elt, va_gc> *v = NULL;
18457 /* Assume all of the expressions are constant. */
18458 *non_constant_p = false;
18460 /* Parse the rest of the list. */
18461 while (true)
18463 cp_token *token;
18464 tree designator;
18465 tree initializer;
18466 bool clause_non_constant_p;
18468 /* If the next token is an identifier and the following one is a
18469 colon, we are looking at the GNU designated-initializer
18470 syntax. */
18471 if (cp_parser_allow_gnu_extensions_p (parser)
18472 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18473 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18475 /* Warn the user that they are using an extension. */
18476 pedwarn (input_location, OPT_Wpedantic,
18477 "ISO C++ does not allow designated initializers");
18478 /* Consume the identifier. */
18479 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18480 /* Consume the `:'. */
18481 cp_lexer_consume_token (parser->lexer);
18483 /* Also handle the C99 syntax, '. id ='. */
18484 else if (cp_parser_allow_gnu_extensions_p (parser)
18485 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18486 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
18487 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
18489 /* Warn the user that they are using an extension. */
18490 pedwarn (input_location, OPT_Wpedantic,
18491 "ISO C++ does not allow C99 designated initializers");
18492 /* Consume the `.'. */
18493 cp_lexer_consume_token (parser->lexer);
18494 /* Consume the identifier. */
18495 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18496 /* Consume the `='. */
18497 cp_lexer_consume_token (parser->lexer);
18499 /* Also handle C99 array designators, '[ const ] ='. */
18500 else if (cp_parser_allow_gnu_extensions_p (parser)
18501 && !c_dialect_objc ()
18502 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18504 /* In C++11, [ could start a lambda-introducer. */
18505 bool non_const = false;
18507 cp_parser_parse_tentatively (parser);
18508 cp_lexer_consume_token (parser->lexer);
18509 designator = cp_parser_constant_expression (parser, true, &non_const);
18510 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18511 cp_parser_require (parser, CPP_EQ, RT_EQ);
18512 if (!cp_parser_parse_definitely (parser))
18513 designator = NULL_TREE;
18514 else if (non_const)
18515 require_potential_rvalue_constant_expression (designator);
18517 else
18518 designator = NULL_TREE;
18520 /* Parse the initializer. */
18521 initializer = cp_parser_initializer_clause (parser,
18522 &clause_non_constant_p);
18523 /* If any clause is non-constant, so is the entire initializer. */
18524 if (clause_non_constant_p)
18525 *non_constant_p = true;
18527 /* If we have an ellipsis, this is an initializer pack
18528 expansion. */
18529 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18531 /* Consume the `...'. */
18532 cp_lexer_consume_token (parser->lexer);
18534 /* Turn the initializer into an initializer expansion. */
18535 initializer = make_pack_expansion (initializer);
18538 /* Add it to the vector. */
18539 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
18541 /* If the next token is not a comma, we have reached the end of
18542 the list. */
18543 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18544 break;
18546 /* Peek at the next token. */
18547 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18548 /* If the next token is a `}', then we're still done. An
18549 initializer-clause can have a trailing `,' after the
18550 initializer-list and before the closing `}'. */
18551 if (token->type == CPP_CLOSE_BRACE)
18552 break;
18554 /* Consume the `,' token. */
18555 cp_lexer_consume_token (parser->lexer);
18558 return v;
18561 /* Classes [gram.class] */
18563 /* Parse a class-name.
18565 class-name:
18566 identifier
18567 template-id
18569 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18570 to indicate that names looked up in dependent types should be
18571 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
18572 keyword has been used to indicate that the name that appears next
18573 is a template. TAG_TYPE indicates the explicit tag given before
18574 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18575 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18576 is the class being defined in a class-head.
18578 Returns the TYPE_DECL representing the class. */
18580 static tree
18581 cp_parser_class_name (cp_parser *parser,
18582 bool typename_keyword_p,
18583 bool template_keyword_p,
18584 enum tag_types tag_type,
18585 bool check_dependency_p,
18586 bool class_head_p,
18587 bool is_declaration)
18589 tree decl;
18590 tree scope;
18591 bool typename_p;
18592 cp_token *token;
18593 tree identifier = NULL_TREE;
18595 /* All class-names start with an identifier. */
18596 token = cp_lexer_peek_token (parser->lexer);
18597 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18599 cp_parser_error (parser, "expected class-name");
18600 return error_mark_node;
18603 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18604 to a template-id, so we save it here. */
18605 scope = parser->scope;
18606 if (scope == error_mark_node)
18607 return error_mark_node;
18609 /* Any name names a type if we're following the `typename' keyword
18610 in a qualified name where the enclosing scope is type-dependent. */
18611 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18612 && dependent_type_p (scope));
18613 /* Handle the common case (an identifier, but not a template-id)
18614 efficiently. */
18615 if (token->type == CPP_NAME
18616 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18618 cp_token *identifier_token;
18619 bool ambiguous_p;
18621 /* Look for the identifier. */
18622 identifier_token = cp_lexer_peek_token (parser->lexer);
18623 ambiguous_p = identifier_token->ambiguous_p;
18624 identifier = cp_parser_identifier (parser);
18625 /* If the next token isn't an identifier, we are certainly not
18626 looking at a class-name. */
18627 if (identifier == error_mark_node)
18628 decl = error_mark_node;
18629 /* If we know this is a type-name, there's no need to look it
18630 up. */
18631 else if (typename_p)
18632 decl = identifier;
18633 else
18635 tree ambiguous_decls;
18636 /* If we already know that this lookup is ambiguous, then
18637 we've already issued an error message; there's no reason
18638 to check again. */
18639 if (ambiguous_p)
18641 cp_parser_simulate_error (parser);
18642 return error_mark_node;
18644 /* If the next token is a `::', then the name must be a type
18645 name.
18647 [basic.lookup.qual]
18649 During the lookup for a name preceding the :: scope
18650 resolution operator, object, function, and enumerator
18651 names are ignored. */
18652 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18653 tag_type = typename_type;
18654 /* Look up the name. */
18655 decl = cp_parser_lookup_name (parser, identifier,
18656 tag_type,
18657 /*is_template=*/false,
18658 /*is_namespace=*/false,
18659 check_dependency_p,
18660 &ambiguous_decls,
18661 identifier_token->location);
18662 if (ambiguous_decls)
18664 if (cp_parser_parsing_tentatively (parser))
18665 cp_parser_simulate_error (parser);
18666 return error_mark_node;
18670 else
18672 /* Try a template-id. */
18673 decl = cp_parser_template_id (parser, template_keyword_p,
18674 check_dependency_p,
18675 tag_type,
18676 is_declaration);
18677 if (decl == error_mark_node)
18678 return error_mark_node;
18681 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18683 /* If this is a typename, create a TYPENAME_TYPE. */
18684 if (typename_p && decl != error_mark_node)
18686 decl = make_typename_type (scope, decl, typename_type,
18687 /*complain=*/tf_error);
18688 if (decl != error_mark_node)
18689 decl = TYPE_NAME (decl);
18692 decl = strip_using_decl (decl);
18694 /* Check to see that it is really the name of a class. */
18695 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18696 && identifier_p (TREE_OPERAND (decl, 0))
18697 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18698 /* Situations like this:
18700 template <typename T> struct A {
18701 typename T::template X<int>::I i;
18704 are problematic. Is `T::template X<int>' a class-name? The
18705 standard does not seem to be definitive, but there is no other
18706 valid interpretation of the following `::'. Therefore, those
18707 names are considered class-names. */
18709 decl = make_typename_type (scope, decl, tag_type, tf_error);
18710 if (decl != error_mark_node)
18711 decl = TYPE_NAME (decl);
18713 else if (TREE_CODE (decl) != TYPE_DECL
18714 || TREE_TYPE (decl) == error_mark_node
18715 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18716 /* In Objective-C 2.0, a classname followed by '.' starts a
18717 dot-syntax expression, and it's not a type-name. */
18718 || (c_dialect_objc ()
18719 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18720 && objc_is_class_name (decl)))
18721 decl = error_mark_node;
18723 if (decl == error_mark_node)
18724 cp_parser_error (parser, "expected class-name");
18725 else if (identifier && !parser->scope)
18726 maybe_note_name_used_in_class (identifier, decl);
18728 return decl;
18731 /* Parse a class-specifier.
18733 class-specifier:
18734 class-head { member-specification [opt] }
18736 Returns the TREE_TYPE representing the class. */
18738 static tree
18739 cp_parser_class_specifier_1 (cp_parser* parser)
18741 tree type;
18742 tree attributes = NULL_TREE;
18743 bool nested_name_specifier_p;
18744 unsigned saved_num_template_parameter_lists;
18745 bool saved_in_function_body;
18746 unsigned char in_statement;
18747 bool in_switch_statement_p;
18748 bool saved_in_unbraced_linkage_specification_p;
18749 tree old_scope = NULL_TREE;
18750 tree scope = NULL_TREE;
18751 cp_token *closing_brace;
18753 push_deferring_access_checks (dk_no_deferred);
18755 /* Parse the class-head. */
18756 type = cp_parser_class_head (parser,
18757 &nested_name_specifier_p);
18758 /* If the class-head was a semantic disaster, skip the entire body
18759 of the class. */
18760 if (!type)
18762 cp_parser_skip_to_end_of_block_or_statement (parser);
18763 pop_deferring_access_checks ();
18764 return error_mark_node;
18767 /* Look for the `{'. */
18768 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18770 pop_deferring_access_checks ();
18771 return error_mark_node;
18774 /* Issue an error message if type-definitions are forbidden here. */
18775 cp_parser_check_type_definition (parser);
18776 /* Remember that we are defining one more class. */
18777 ++parser->num_classes_being_defined;
18778 /* Inside the class, surrounding template-parameter-lists do not
18779 apply. */
18780 saved_num_template_parameter_lists
18781 = parser->num_template_parameter_lists;
18782 parser->num_template_parameter_lists = 0;
18783 /* We are not in a function body. */
18784 saved_in_function_body = parser->in_function_body;
18785 parser->in_function_body = false;
18786 /* Or in a loop. */
18787 in_statement = parser->in_statement;
18788 parser->in_statement = 0;
18789 /* Or in a switch. */
18790 in_switch_statement_p = parser->in_switch_statement_p;
18791 parser->in_switch_statement_p = false;
18792 /* We are not immediately inside an extern "lang" block. */
18793 saved_in_unbraced_linkage_specification_p
18794 = parser->in_unbraced_linkage_specification_p;
18795 parser->in_unbraced_linkage_specification_p = false;
18797 /* Start the class. */
18798 if (nested_name_specifier_p)
18800 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18801 old_scope = push_inner_scope (scope);
18803 type = begin_class_definition (type);
18805 if (type == error_mark_node)
18806 /* If the type is erroneous, skip the entire body of the class. */
18807 cp_parser_skip_to_closing_brace (parser);
18808 else
18809 /* Parse the member-specification. */
18810 cp_parser_member_specification_opt (parser);
18812 /* Look for the trailing `}'. */
18813 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18814 /* Look for trailing attributes to apply to this class. */
18815 if (cp_parser_allow_gnu_extensions_p (parser))
18816 attributes = cp_parser_gnu_attributes_opt (parser);
18817 if (type != error_mark_node)
18818 type = finish_struct (type, attributes);
18819 if (nested_name_specifier_p)
18820 pop_inner_scope (old_scope, scope);
18822 /* We've finished a type definition. Check for the common syntax
18823 error of forgetting a semicolon after the definition. We need to
18824 be careful, as we can't just check for not-a-semicolon and be done
18825 with it; the user might have typed:
18827 class X { } c = ...;
18828 class X { } *p = ...;
18830 and so forth. Instead, enumerate all the possible tokens that
18831 might follow this production; if we don't see one of them, then
18832 complain and silently insert the semicolon. */
18834 cp_token *token = cp_lexer_peek_token (parser->lexer);
18835 bool want_semicolon = true;
18837 if (cp_next_tokens_can_be_std_attribute_p (parser))
18838 /* Don't try to parse c++11 attributes here. As per the
18839 grammar, that should be a task for
18840 cp_parser_decl_specifier_seq. */
18841 want_semicolon = false;
18843 switch (token->type)
18845 case CPP_NAME:
18846 case CPP_SEMICOLON:
18847 case CPP_MULT:
18848 case CPP_AND:
18849 case CPP_OPEN_PAREN:
18850 case CPP_CLOSE_PAREN:
18851 case CPP_COMMA:
18852 want_semicolon = false;
18853 break;
18855 /* While it's legal for type qualifiers and storage class
18856 specifiers to follow type definitions in the grammar, only
18857 compiler testsuites contain code like that. Assume that if
18858 we see such code, then what we're really seeing is a case
18859 like:
18861 class X { }
18862 const <type> var = ...;
18866 class Y { }
18867 static <type> func (...) ...
18869 i.e. the qualifier or specifier applies to the next
18870 declaration. To do so, however, we need to look ahead one
18871 more token to see if *that* token is a type specifier.
18873 This code could be improved to handle:
18875 class Z { }
18876 static const <type> var = ...; */
18877 case CPP_KEYWORD:
18878 if (keyword_is_decl_specifier (token->keyword))
18880 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18882 /* Handling user-defined types here would be nice, but very
18883 tricky. */
18884 want_semicolon
18885 = (lookahead->type == CPP_KEYWORD
18886 && keyword_begins_type_specifier (lookahead->keyword));
18888 break;
18889 default:
18890 break;
18893 /* If we don't have a type, then something is very wrong and we
18894 shouldn't try to do anything clever. Likewise for not seeing the
18895 closing brace. */
18896 if (closing_brace && TYPE_P (type) && want_semicolon)
18898 cp_token_position prev
18899 = cp_lexer_previous_token_position (parser->lexer);
18900 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18901 location_t loc = prev_token->location;
18903 if (CLASSTYPE_DECLARED_CLASS (type))
18904 error_at (loc, "expected %<;%> after class definition");
18905 else if (TREE_CODE (type) == RECORD_TYPE)
18906 error_at (loc, "expected %<;%> after struct definition");
18907 else if (TREE_CODE (type) == UNION_TYPE)
18908 error_at (loc, "expected %<;%> after union definition");
18909 else
18910 gcc_unreachable ();
18912 /* Unget one token and smash it to look as though we encountered
18913 a semicolon in the input stream. */
18914 cp_lexer_set_token_position (parser->lexer, prev);
18915 token = cp_lexer_peek_token (parser->lexer);
18916 token->type = CPP_SEMICOLON;
18917 token->keyword = RID_MAX;
18921 /* If this class is not itself within the scope of another class,
18922 then we need to parse the bodies of all of the queued function
18923 definitions. Note that the queued functions defined in a class
18924 are not always processed immediately following the
18925 class-specifier for that class. Consider:
18927 struct A {
18928 struct B { void f() { sizeof (A); } };
18931 If `f' were processed before the processing of `A' were
18932 completed, there would be no way to compute the size of `A'.
18933 Note that the nesting we are interested in here is lexical --
18934 not the semantic nesting given by TYPE_CONTEXT. In particular,
18935 for:
18937 struct A { struct B; };
18938 struct A::B { void f() { } };
18940 there is no need to delay the parsing of `A::B::f'. */
18941 if (--parser->num_classes_being_defined == 0)
18943 tree decl;
18944 tree class_type = NULL_TREE;
18945 tree pushed_scope = NULL_TREE;
18946 unsigned ix;
18947 cp_default_arg_entry *e;
18948 tree save_ccp, save_ccr;
18950 /* In a first pass, parse default arguments to the functions.
18951 Then, in a second pass, parse the bodies of the functions.
18952 This two-phased approach handles cases like:
18954 struct S {
18955 void f() { g(); }
18956 void g(int i = 3);
18960 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
18962 decl = e->decl;
18963 /* If there are default arguments that have not yet been processed,
18964 take care of them now. */
18965 if (class_type != e->class_type)
18967 if (pushed_scope)
18968 pop_scope (pushed_scope);
18969 class_type = e->class_type;
18970 pushed_scope = push_scope (class_type);
18972 /* Make sure that any template parameters are in scope. */
18973 maybe_begin_member_template_processing (decl);
18974 /* Parse the default argument expressions. */
18975 cp_parser_late_parsing_default_args (parser, decl);
18976 /* Remove any template parameters from the symbol table. */
18977 maybe_end_member_template_processing ();
18979 vec_safe_truncate (unparsed_funs_with_default_args, 0);
18980 /* Now parse any NSDMIs. */
18981 save_ccp = current_class_ptr;
18982 save_ccr = current_class_ref;
18983 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
18985 if (class_type != DECL_CONTEXT (decl))
18987 if (pushed_scope)
18988 pop_scope (pushed_scope);
18989 class_type = DECL_CONTEXT (decl);
18990 pushed_scope = push_scope (class_type);
18992 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18993 cp_parser_late_parsing_nsdmi (parser, decl);
18995 vec_safe_truncate (unparsed_nsdmis, 0);
18996 current_class_ptr = save_ccp;
18997 current_class_ref = save_ccr;
18998 if (pushed_scope)
18999 pop_scope (pushed_scope);
19000 /* Now parse the body of the functions. */
19001 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19002 cp_parser_late_parsing_for_member (parser, decl);
19003 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19006 /* Put back any saved access checks. */
19007 pop_deferring_access_checks ();
19009 /* Restore saved state. */
19010 parser->in_switch_statement_p = in_switch_statement_p;
19011 parser->in_statement = in_statement;
19012 parser->in_function_body = saved_in_function_body;
19013 parser->num_template_parameter_lists
19014 = saved_num_template_parameter_lists;
19015 parser->in_unbraced_linkage_specification_p
19016 = saved_in_unbraced_linkage_specification_p;
19018 return type;
19021 static tree
19022 cp_parser_class_specifier (cp_parser* parser)
19024 tree ret;
19025 timevar_push (TV_PARSE_STRUCT);
19026 ret = cp_parser_class_specifier_1 (parser);
19027 timevar_pop (TV_PARSE_STRUCT);
19028 return ret;
19031 /* Parse a class-head.
19033 class-head:
19034 class-key identifier [opt] base-clause [opt]
19035 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19036 class-key nested-name-specifier [opt] template-id
19037 base-clause [opt]
19039 class-virt-specifier:
19040 final
19042 GNU Extensions:
19043 class-key attributes identifier [opt] base-clause [opt]
19044 class-key attributes nested-name-specifier identifier base-clause [opt]
19045 class-key attributes nested-name-specifier [opt] template-id
19046 base-clause [opt]
19048 Upon return BASES is initialized to the list of base classes (or
19049 NULL, if there are none) in the same form returned by
19050 cp_parser_base_clause.
19052 Returns the TYPE of the indicated class. Sets
19053 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19054 involving a nested-name-specifier was used, and FALSE otherwise.
19056 Returns error_mark_node if this is not a class-head.
19058 Returns NULL_TREE if the class-head is syntactically valid, but
19059 semantically invalid in a way that means we should skip the entire
19060 body of the class. */
19062 static tree
19063 cp_parser_class_head (cp_parser* parser,
19064 bool* nested_name_specifier_p)
19066 tree nested_name_specifier;
19067 enum tag_types class_key;
19068 tree id = NULL_TREE;
19069 tree type = NULL_TREE;
19070 tree attributes;
19071 tree bases;
19072 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19073 bool template_id_p = false;
19074 bool qualified_p = false;
19075 bool invalid_nested_name_p = false;
19076 bool invalid_explicit_specialization_p = false;
19077 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19078 tree pushed_scope = NULL_TREE;
19079 unsigned num_templates;
19080 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19081 /* Assume no nested-name-specifier will be present. */
19082 *nested_name_specifier_p = false;
19083 /* Assume no template parameter lists will be used in defining the
19084 type. */
19085 num_templates = 0;
19086 parser->colon_corrects_to_scope_p = false;
19088 /* Look for the class-key. */
19089 class_key = cp_parser_class_key (parser);
19090 if (class_key == none_type)
19091 return error_mark_node;
19093 /* Parse the attributes. */
19094 attributes = cp_parser_attributes_opt (parser);
19096 /* If the next token is `::', that is invalid -- but sometimes
19097 people do try to write:
19099 struct ::S {};
19101 Handle this gracefully by accepting the extra qualifier, and then
19102 issuing an error about it later if this really is a
19103 class-head. If it turns out just to be an elaborated type
19104 specifier, remain silent. */
19105 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19106 qualified_p = true;
19108 push_deferring_access_checks (dk_no_check);
19110 /* Determine the name of the class. Begin by looking for an
19111 optional nested-name-specifier. */
19112 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19113 nested_name_specifier
19114 = cp_parser_nested_name_specifier_opt (parser,
19115 /*typename_keyword_p=*/false,
19116 /*check_dependency_p=*/false,
19117 /*type_p=*/true,
19118 /*is_declaration=*/false);
19119 /* If there was a nested-name-specifier, then there *must* be an
19120 identifier. */
19121 if (nested_name_specifier)
19123 type_start_token = cp_lexer_peek_token (parser->lexer);
19124 /* Although the grammar says `identifier', it really means
19125 `class-name' or `template-name'. You are only allowed to
19126 define a class that has already been declared with this
19127 syntax.
19129 The proposed resolution for Core Issue 180 says that wherever
19130 you see `class T::X' you should treat `X' as a type-name.
19132 It is OK to define an inaccessible class; for example:
19134 class A { class B; };
19135 class A::B {};
19137 We do not know if we will see a class-name, or a
19138 template-name. We look for a class-name first, in case the
19139 class-name is a template-id; if we looked for the
19140 template-name first we would stop after the template-name. */
19141 cp_parser_parse_tentatively (parser);
19142 type = cp_parser_class_name (parser,
19143 /*typename_keyword_p=*/false,
19144 /*template_keyword_p=*/false,
19145 class_type,
19146 /*check_dependency_p=*/false,
19147 /*class_head_p=*/true,
19148 /*is_declaration=*/false);
19149 /* If that didn't work, ignore the nested-name-specifier. */
19150 if (!cp_parser_parse_definitely (parser))
19152 invalid_nested_name_p = true;
19153 type_start_token = cp_lexer_peek_token (parser->lexer);
19154 id = cp_parser_identifier (parser);
19155 if (id == error_mark_node)
19156 id = NULL_TREE;
19158 /* If we could not find a corresponding TYPE, treat this
19159 declaration like an unqualified declaration. */
19160 if (type == error_mark_node)
19161 nested_name_specifier = NULL_TREE;
19162 /* Otherwise, count the number of templates used in TYPE and its
19163 containing scopes. */
19164 else
19166 tree scope;
19168 for (scope = TREE_TYPE (type);
19169 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19170 scope = get_containing_scope (scope))
19171 if (TYPE_P (scope)
19172 && CLASS_TYPE_P (scope)
19173 && CLASSTYPE_TEMPLATE_INFO (scope)
19174 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19175 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19176 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19177 ++num_templates;
19180 /* Otherwise, the identifier is optional. */
19181 else
19183 /* We don't know whether what comes next is a template-id,
19184 an identifier, or nothing at all. */
19185 cp_parser_parse_tentatively (parser);
19186 /* Check for a template-id. */
19187 type_start_token = cp_lexer_peek_token (parser->lexer);
19188 id = cp_parser_template_id (parser,
19189 /*template_keyword_p=*/false,
19190 /*check_dependency_p=*/true,
19191 class_key,
19192 /*is_declaration=*/true);
19193 /* If that didn't work, it could still be an identifier. */
19194 if (!cp_parser_parse_definitely (parser))
19196 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19198 type_start_token = cp_lexer_peek_token (parser->lexer);
19199 id = cp_parser_identifier (parser);
19201 else
19202 id = NULL_TREE;
19204 else
19206 template_id_p = true;
19207 ++num_templates;
19211 pop_deferring_access_checks ();
19213 if (id)
19215 cp_parser_check_for_invalid_template_id (parser, id,
19216 class_key,
19217 type_start_token->location);
19219 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19221 /* If it's not a `:' or a `{' then we can't really be looking at a
19222 class-head, since a class-head only appears as part of a
19223 class-specifier. We have to detect this situation before calling
19224 xref_tag, since that has irreversible side-effects. */
19225 if (!cp_parser_next_token_starts_class_definition_p (parser))
19227 cp_parser_error (parser, "expected %<{%> or %<:%>");
19228 type = error_mark_node;
19229 goto out;
19232 /* At this point, we're going ahead with the class-specifier, even
19233 if some other problem occurs. */
19234 cp_parser_commit_to_tentative_parse (parser);
19235 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19237 cp_parser_error (parser,
19238 "cannot specify %<override%> for a class");
19239 type = error_mark_node;
19240 goto out;
19242 /* Issue the error about the overly-qualified name now. */
19243 if (qualified_p)
19245 cp_parser_error (parser,
19246 "global qualification of class name is invalid");
19247 type = error_mark_node;
19248 goto out;
19250 else if (invalid_nested_name_p)
19252 cp_parser_error (parser,
19253 "qualified name does not name a class");
19254 type = error_mark_node;
19255 goto out;
19257 else if (nested_name_specifier)
19259 tree scope;
19261 /* Reject typedef-names in class heads. */
19262 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19264 error_at (type_start_token->location,
19265 "invalid class name in declaration of %qD",
19266 type);
19267 type = NULL_TREE;
19268 goto done;
19271 /* Figure out in what scope the declaration is being placed. */
19272 scope = current_scope ();
19273 /* If that scope does not contain the scope in which the
19274 class was originally declared, the program is invalid. */
19275 if (scope && !is_ancestor (scope, nested_name_specifier))
19277 if (at_namespace_scope_p ())
19278 error_at (type_start_token->location,
19279 "declaration of %qD in namespace %qD which does not "
19280 "enclose %qD",
19281 type, scope, nested_name_specifier);
19282 else
19283 error_at (type_start_token->location,
19284 "declaration of %qD in %qD which does not enclose %qD",
19285 type, scope, nested_name_specifier);
19286 type = NULL_TREE;
19287 goto done;
19289 /* [dcl.meaning]
19291 A declarator-id shall not be qualified except for the
19292 definition of a ... nested class outside of its class
19293 ... [or] the definition or explicit instantiation of a
19294 class member of a namespace outside of its namespace. */
19295 if (scope == nested_name_specifier)
19297 permerror (nested_name_specifier_token_start->location,
19298 "extra qualification not allowed");
19299 nested_name_specifier = NULL_TREE;
19300 num_templates = 0;
19303 /* An explicit-specialization must be preceded by "template <>". If
19304 it is not, try to recover gracefully. */
19305 if (at_namespace_scope_p ()
19306 && parser->num_template_parameter_lists == 0
19307 && template_id_p)
19309 error_at (type_start_token->location,
19310 "an explicit specialization must be preceded by %<template <>%>");
19311 invalid_explicit_specialization_p = true;
19312 /* Take the same action that would have been taken by
19313 cp_parser_explicit_specialization. */
19314 ++parser->num_template_parameter_lists;
19315 begin_specialization ();
19317 /* There must be no "return" statements between this point and the
19318 end of this function; set "type "to the correct return value and
19319 use "goto done;" to return. */
19320 /* Make sure that the right number of template parameters were
19321 present. */
19322 if (!cp_parser_check_template_parameters (parser, num_templates,
19323 type_start_token->location,
19324 /*declarator=*/NULL))
19326 /* If something went wrong, there is no point in even trying to
19327 process the class-definition. */
19328 type = NULL_TREE;
19329 goto done;
19332 /* Look up the type. */
19333 if (template_id_p)
19335 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19336 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19337 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19339 error_at (type_start_token->location,
19340 "function template %qD redeclared as a class template", id);
19341 type = error_mark_node;
19343 else
19345 type = TREE_TYPE (id);
19346 type = maybe_process_partial_specialization (type);
19348 if (nested_name_specifier)
19349 pushed_scope = push_scope (nested_name_specifier);
19351 else if (nested_name_specifier)
19353 tree class_type;
19355 /* Given:
19357 template <typename T> struct S { struct T };
19358 template <typename T> struct S<T>::T { };
19360 we will get a TYPENAME_TYPE when processing the definition of
19361 `S::T'. We need to resolve it to the actual type before we
19362 try to define it. */
19363 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19365 class_type = resolve_typename_type (TREE_TYPE (type),
19366 /*only_current_p=*/false);
19367 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19368 type = TYPE_NAME (class_type);
19369 else
19371 cp_parser_error (parser, "could not resolve typename type");
19372 type = error_mark_node;
19376 if (maybe_process_partial_specialization (TREE_TYPE (type))
19377 == error_mark_node)
19379 type = NULL_TREE;
19380 goto done;
19383 class_type = current_class_type;
19384 /* Enter the scope indicated by the nested-name-specifier. */
19385 pushed_scope = push_scope (nested_name_specifier);
19386 /* Get the canonical version of this type. */
19387 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19388 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
19389 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19391 type = push_template_decl (type);
19392 if (type == error_mark_node)
19394 type = NULL_TREE;
19395 goto done;
19399 type = TREE_TYPE (type);
19400 *nested_name_specifier_p = true;
19402 else /* The name is not a nested name. */
19404 /* If the class was unnamed, create a dummy name. */
19405 if (!id)
19406 id = make_anon_name ();
19407 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19408 parser->num_template_parameter_lists);
19411 /* Indicate whether this class was declared as a `class' or as a
19412 `struct'. */
19413 if (TREE_CODE (type) == RECORD_TYPE)
19414 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19415 cp_parser_check_class_key (class_key, type);
19417 /* If this type was already complete, and we see another definition,
19418 that's an error. */
19419 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19421 error_at (type_start_token->location, "redefinition of %q#T",
19422 type);
19423 error_at (type_start_token->location, "previous definition of %q+#T",
19424 type);
19425 type = NULL_TREE;
19426 goto done;
19428 else if (type == error_mark_node)
19429 type = NULL_TREE;
19431 if (type)
19433 /* Apply attributes now, before any use of the class as a template
19434 argument in its base list. */
19435 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19436 fixup_attribute_variants (type);
19439 /* We will have entered the scope containing the class; the names of
19440 base classes should be looked up in that context. For example:
19442 struct A { struct B {}; struct C; };
19443 struct A::C : B {};
19445 is valid. */
19447 /* Get the list of base-classes, if there is one. */
19448 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19449 bases = cp_parser_base_clause (parser);
19450 else
19451 bases = NULL_TREE;
19453 /* If we're really defining a class, process the base classes.
19454 If they're invalid, fail. */
19455 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19456 && !xref_basetypes (type, bases))
19457 type = NULL_TREE;
19459 done:
19460 /* Leave the scope given by the nested-name-specifier. We will
19461 enter the class scope itself while processing the members. */
19462 if (pushed_scope)
19463 pop_scope (pushed_scope);
19465 if (invalid_explicit_specialization_p)
19467 end_specialization ();
19468 --parser->num_template_parameter_lists;
19471 if (type)
19472 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19473 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
19474 CLASSTYPE_FINAL (type) = 1;
19475 out:
19476 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19477 return type;
19480 /* Parse a class-key.
19482 class-key:
19483 class
19484 struct
19485 union
19487 Returns the kind of class-key specified, or none_type to indicate
19488 error. */
19490 static enum tag_types
19491 cp_parser_class_key (cp_parser* parser)
19493 cp_token *token;
19494 enum tag_types tag_type;
19496 /* Look for the class-key. */
19497 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
19498 if (!token)
19499 return none_type;
19501 /* Check to see if the TOKEN is a class-key. */
19502 tag_type = cp_parser_token_is_class_key (token);
19503 if (!tag_type)
19504 cp_parser_error (parser, "expected class-key");
19505 return tag_type;
19508 /* Parse an (optional) member-specification.
19510 member-specification:
19511 member-declaration member-specification [opt]
19512 access-specifier : member-specification [opt] */
19514 static void
19515 cp_parser_member_specification_opt (cp_parser* parser)
19517 while (true)
19519 cp_token *token;
19520 enum rid keyword;
19522 /* Peek at the next token. */
19523 token = cp_lexer_peek_token (parser->lexer);
19524 /* If it's a `}', or EOF then we've seen all the members. */
19525 if (token->type == CPP_CLOSE_BRACE
19526 || token->type == CPP_EOF
19527 || token->type == CPP_PRAGMA_EOL)
19528 break;
19530 /* See if this token is a keyword. */
19531 keyword = token->keyword;
19532 switch (keyword)
19534 case RID_PUBLIC:
19535 case RID_PROTECTED:
19536 case RID_PRIVATE:
19537 /* Consume the access-specifier. */
19538 cp_lexer_consume_token (parser->lexer);
19539 /* Remember which access-specifier is active. */
19540 current_access_specifier = token->u.value;
19541 /* Look for the `:'. */
19542 cp_parser_require (parser, CPP_COLON, RT_COLON);
19543 break;
19545 default:
19546 /* Accept #pragmas at class scope. */
19547 if (token->type == CPP_PRAGMA)
19549 cp_parser_pragma (parser, pragma_external);
19550 break;
19553 /* Otherwise, the next construction must be a
19554 member-declaration. */
19555 cp_parser_member_declaration (parser);
19560 /* Parse a member-declaration.
19562 member-declaration:
19563 decl-specifier-seq [opt] member-declarator-list [opt] ;
19564 function-definition ; [opt]
19565 :: [opt] nested-name-specifier template [opt] unqualified-id ;
19566 using-declaration
19567 template-declaration
19568 alias-declaration
19570 member-declarator-list:
19571 member-declarator
19572 member-declarator-list , member-declarator
19574 member-declarator:
19575 declarator pure-specifier [opt]
19576 declarator constant-initializer [opt]
19577 identifier [opt] : constant-expression
19579 GNU Extensions:
19581 member-declaration:
19582 __extension__ member-declaration
19584 member-declarator:
19585 declarator attributes [opt] pure-specifier [opt]
19586 declarator attributes [opt] constant-initializer [opt]
19587 identifier [opt] attributes [opt] : constant-expression
19589 C++0x Extensions:
19591 member-declaration:
19592 static_assert-declaration */
19594 static void
19595 cp_parser_member_declaration (cp_parser* parser)
19597 cp_decl_specifier_seq decl_specifiers;
19598 tree prefix_attributes;
19599 tree decl;
19600 int declares_class_or_enum;
19601 bool friend_p;
19602 cp_token *token = NULL;
19603 cp_token *decl_spec_token_start = NULL;
19604 cp_token *initializer_token_start = NULL;
19605 int saved_pedantic;
19606 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19608 /* Check for the `__extension__' keyword. */
19609 if (cp_parser_extension_opt (parser, &saved_pedantic))
19611 /* Recurse. */
19612 cp_parser_member_declaration (parser);
19613 /* Restore the old value of the PEDANTIC flag. */
19614 pedantic = saved_pedantic;
19616 return;
19619 /* Check for a template-declaration. */
19620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19622 /* An explicit specialization here is an error condition, and we
19623 expect the specialization handler to detect and report this. */
19624 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19625 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19626 cp_parser_explicit_specialization (parser);
19627 else
19628 cp_parser_template_declaration (parser, /*member_p=*/true);
19630 return;
19633 /* Check for a using-declaration. */
19634 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19636 if (cxx_dialect < cxx11)
19638 /* Parse the using-declaration. */
19639 cp_parser_using_declaration (parser,
19640 /*access_declaration_p=*/false);
19641 return;
19643 else
19645 tree decl;
19646 bool alias_decl_expected;
19647 cp_parser_parse_tentatively (parser);
19648 decl = cp_parser_alias_declaration (parser);
19649 /* Note that if we actually see the '=' token after the
19650 identifier, cp_parser_alias_declaration commits the
19651 tentative parse. In that case, we really expects an
19652 alias-declaration. Otherwise, we expect a using
19653 declaration. */
19654 alias_decl_expected =
19655 !cp_parser_uncommitted_to_tentative_parse_p (parser);
19656 cp_parser_parse_definitely (parser);
19658 if (alias_decl_expected)
19659 finish_member_declaration (decl);
19660 else
19661 cp_parser_using_declaration (parser,
19662 /*access_declaration_p=*/false);
19663 return;
19667 /* Check for @defs. */
19668 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19670 tree ivar, member;
19671 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19672 ivar = ivar_chains;
19673 while (ivar)
19675 member = ivar;
19676 ivar = TREE_CHAIN (member);
19677 TREE_CHAIN (member) = NULL_TREE;
19678 finish_member_declaration (member);
19680 return;
19683 /* If the next token is `static_assert' we have a static assertion. */
19684 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19686 cp_parser_static_assert (parser, /*member_p=*/true);
19687 return;
19690 parser->colon_corrects_to_scope_p = false;
19692 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19693 goto out;
19695 /* Parse the decl-specifier-seq. */
19696 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19697 cp_parser_decl_specifier_seq (parser,
19698 CP_PARSER_FLAGS_OPTIONAL,
19699 &decl_specifiers,
19700 &declares_class_or_enum);
19701 /* Check for an invalid type-name. */
19702 if (!decl_specifiers.any_type_specifiers_p
19703 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19704 goto out;
19705 /* If there is no declarator, then the decl-specifier-seq should
19706 specify a type. */
19707 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19709 /* If there was no decl-specifier-seq, and the next token is a
19710 `;', then we have something like:
19712 struct S { ; };
19714 [class.mem]
19716 Each member-declaration shall declare at least one member
19717 name of the class. */
19718 if (!decl_specifiers.any_specifiers_p)
19720 cp_token *token = cp_lexer_peek_token (parser->lexer);
19721 if (!in_system_header_at (token->location))
19722 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19724 else
19726 tree type;
19728 /* See if this declaration is a friend. */
19729 friend_p = cp_parser_friend_p (&decl_specifiers);
19730 /* If there were decl-specifiers, check to see if there was
19731 a class-declaration. */
19732 type = check_tag_decl (&decl_specifiers,
19733 /*explicit_type_instantiation_p=*/false);
19734 /* Nested classes have already been added to the class, but
19735 a `friend' needs to be explicitly registered. */
19736 if (friend_p)
19738 /* If the `friend' keyword was present, the friend must
19739 be introduced with a class-key. */
19740 if (!declares_class_or_enum && cxx_dialect < cxx11)
19741 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19742 "in C++03 a class-key must be used "
19743 "when declaring a friend");
19744 /* In this case:
19746 template <typename T> struct A {
19747 friend struct A<T>::B;
19750 A<T>::B will be represented by a TYPENAME_TYPE, and
19751 therefore not recognized by check_tag_decl. */
19752 if (!type)
19754 type = decl_specifiers.type;
19755 if (type && TREE_CODE (type) == TYPE_DECL)
19756 type = TREE_TYPE (type);
19758 if (!type || !TYPE_P (type))
19759 error_at (decl_spec_token_start->location,
19760 "friend declaration does not name a class or "
19761 "function");
19762 else
19763 make_friend_class (current_class_type, type,
19764 /*complain=*/true);
19766 /* If there is no TYPE, an error message will already have
19767 been issued. */
19768 else if (!type || type == error_mark_node)
19770 /* An anonymous aggregate has to be handled specially; such
19771 a declaration really declares a data member (with a
19772 particular type), as opposed to a nested class. */
19773 else if (ANON_AGGR_TYPE_P (type))
19775 /* C++11 9.5/6. */
19776 if (decl_specifiers.storage_class != sc_none)
19777 error_at (decl_spec_token_start->location,
19778 "a storage class on an anonymous aggregate "
19779 "in class scope is not allowed");
19781 /* Remove constructors and such from TYPE, now that we
19782 know it is an anonymous aggregate. */
19783 fixup_anonymous_aggr (type);
19784 /* And make the corresponding data member. */
19785 decl = build_decl (decl_spec_token_start->location,
19786 FIELD_DECL, NULL_TREE, type);
19787 /* Add it to the class. */
19788 finish_member_declaration (decl);
19790 else
19791 cp_parser_check_access_in_redeclaration
19792 (TYPE_NAME (type),
19793 decl_spec_token_start->location);
19796 else
19798 bool assume_semicolon = false;
19800 /* Clear attributes from the decl_specifiers but keep them
19801 around as prefix attributes that apply them to the entity
19802 being declared. */
19803 prefix_attributes = decl_specifiers.attributes;
19804 decl_specifiers.attributes = NULL_TREE;
19806 /* See if these declarations will be friends. */
19807 friend_p = cp_parser_friend_p (&decl_specifiers);
19809 /* Keep going until we hit the `;' at the end of the
19810 declaration. */
19811 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19813 tree attributes = NULL_TREE;
19814 tree first_attribute;
19816 /* Peek at the next token. */
19817 token = cp_lexer_peek_token (parser->lexer);
19819 /* Check for a bitfield declaration. */
19820 if (token->type == CPP_COLON
19821 || (token->type == CPP_NAME
19822 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19823 == CPP_COLON))
19825 tree identifier;
19826 tree width;
19828 /* Get the name of the bitfield. Note that we cannot just
19829 check TOKEN here because it may have been invalidated by
19830 the call to cp_lexer_peek_nth_token above. */
19831 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19832 identifier = cp_parser_identifier (parser);
19833 else
19834 identifier = NULL_TREE;
19836 /* Consume the `:' token. */
19837 cp_lexer_consume_token (parser->lexer);
19838 /* Get the width of the bitfield. */
19839 width
19840 = cp_parser_constant_expression (parser,
19841 /*allow_non_constant=*/false,
19842 NULL);
19844 /* Look for attributes that apply to the bitfield. */
19845 attributes = cp_parser_attributes_opt (parser);
19846 /* Remember which attributes are prefix attributes and
19847 which are not. */
19848 first_attribute = attributes;
19849 /* Combine the attributes. */
19850 attributes = chainon (prefix_attributes, attributes);
19852 /* Create the bitfield declaration. */
19853 decl = grokbitfield (identifier
19854 ? make_id_declarator (NULL_TREE,
19855 identifier,
19856 sfk_none)
19857 : NULL,
19858 &decl_specifiers,
19859 width,
19860 attributes);
19862 else
19864 cp_declarator *declarator;
19865 tree initializer;
19866 tree asm_specification;
19867 int ctor_dtor_or_conv_p;
19869 /* Parse the declarator. */
19870 declarator
19871 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19872 &ctor_dtor_or_conv_p,
19873 /*parenthesized_p=*/NULL,
19874 /*member_p=*/true);
19876 /* If something went wrong parsing the declarator, make sure
19877 that we at least consume some tokens. */
19878 if (declarator == cp_error_declarator)
19880 /* Skip to the end of the statement. */
19881 cp_parser_skip_to_end_of_statement (parser);
19882 /* If the next token is not a semicolon, that is
19883 probably because we just skipped over the body of
19884 a function. So, we consume a semicolon if
19885 present, but do not issue an error message if it
19886 is not present. */
19887 if (cp_lexer_next_token_is (parser->lexer,
19888 CPP_SEMICOLON))
19889 cp_lexer_consume_token (parser->lexer);
19890 goto out;
19893 if (declares_class_or_enum & 2)
19894 cp_parser_check_for_definition_in_return_type
19895 (declarator, decl_specifiers.type,
19896 decl_specifiers.locations[ds_type_spec]);
19898 /* Look for an asm-specification. */
19899 asm_specification = cp_parser_asm_specification_opt (parser);
19900 /* Look for attributes that apply to the declaration. */
19901 attributes = cp_parser_attributes_opt (parser);
19902 /* Remember which attributes are prefix attributes and
19903 which are not. */
19904 first_attribute = attributes;
19905 /* Combine the attributes. */
19906 attributes = chainon (prefix_attributes, attributes);
19908 /* If it's an `=', then we have a constant-initializer or a
19909 pure-specifier. It is not correct to parse the
19910 initializer before registering the member declaration
19911 since the member declaration should be in scope while
19912 its initializer is processed. However, the rest of the
19913 front end does not yet provide an interface that allows
19914 us to handle this correctly. */
19915 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19917 /* In [class.mem]:
19919 A pure-specifier shall be used only in the declaration of
19920 a virtual function.
19922 A member-declarator can contain a constant-initializer
19923 only if it declares a static member of integral or
19924 enumeration type.
19926 Therefore, if the DECLARATOR is for a function, we look
19927 for a pure-specifier; otherwise, we look for a
19928 constant-initializer. When we call `grokfield', it will
19929 perform more stringent semantics checks. */
19930 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19931 if (function_declarator_p (declarator)
19932 || (decl_specifiers.type
19933 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19934 && declarator->kind == cdk_id
19935 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19936 == FUNCTION_TYPE)))
19937 initializer = cp_parser_pure_specifier (parser);
19938 else if (decl_specifiers.storage_class != sc_static)
19939 initializer = cp_parser_save_nsdmi (parser);
19940 else if (cxx_dialect >= cxx11)
19942 bool nonconst;
19943 /* Don't require a constant rvalue in C++11, since we
19944 might want a reference constant. We'll enforce
19945 constancy later. */
19946 cp_lexer_consume_token (parser->lexer);
19947 /* Parse the initializer. */
19948 initializer = cp_parser_initializer_clause (parser,
19949 &nonconst);
19951 else
19952 /* Parse the initializer. */
19953 initializer = cp_parser_constant_initializer (parser);
19955 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19956 && !function_declarator_p (declarator))
19958 bool x;
19959 if (decl_specifiers.storage_class != sc_static)
19960 initializer = cp_parser_save_nsdmi (parser);
19961 else
19962 initializer = cp_parser_initializer (parser, &x, &x);
19964 /* Otherwise, there is no initializer. */
19965 else
19966 initializer = NULL_TREE;
19968 /* See if we are probably looking at a function
19969 definition. We are certainly not looking at a
19970 member-declarator. Calling `grokfield' has
19971 side-effects, so we must not do it unless we are sure
19972 that we are looking at a member-declarator. */
19973 if (cp_parser_token_starts_function_definition_p
19974 (cp_lexer_peek_token (parser->lexer)))
19976 /* The grammar does not allow a pure-specifier to be
19977 used when a member function is defined. (It is
19978 possible that this fact is an oversight in the
19979 standard, since a pure function may be defined
19980 outside of the class-specifier. */
19981 if (initializer && initializer_token_start)
19982 error_at (initializer_token_start->location,
19983 "pure-specifier on function-definition");
19984 decl = cp_parser_save_member_function_body (parser,
19985 &decl_specifiers,
19986 declarator,
19987 attributes);
19988 /* If the member was not a friend, declare it here. */
19989 if (!friend_p)
19990 finish_member_declaration (decl);
19991 /* Peek at the next token. */
19992 token = cp_lexer_peek_token (parser->lexer);
19993 /* If the next token is a semicolon, consume it. */
19994 if (token->type == CPP_SEMICOLON)
19995 cp_lexer_consume_token (parser->lexer);
19996 goto out;
19998 else
19999 if (declarator->kind == cdk_function)
20000 declarator->id_loc = token->location;
20001 /* Create the declaration. */
20002 decl = grokfield (declarator, &decl_specifiers,
20003 initializer, /*init_const_expr_p=*/true,
20004 asm_specification,
20005 attributes);
20008 /* Reset PREFIX_ATTRIBUTES. */
20009 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20010 attributes = TREE_CHAIN (attributes);
20011 if (attributes)
20012 TREE_CHAIN (attributes) = NULL_TREE;
20014 /* If there is any qualification still in effect, clear it
20015 now; we will be starting fresh with the next declarator. */
20016 parser->scope = NULL_TREE;
20017 parser->qualifying_scope = NULL_TREE;
20018 parser->object_scope = NULL_TREE;
20019 /* If it's a `,', then there are more declarators. */
20020 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20022 cp_lexer_consume_token (parser->lexer);
20023 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20025 cp_token *token = cp_lexer_previous_token (parser->lexer);
20026 error_at (token->location,
20027 "stray %<,%> at end of member declaration");
20030 /* If the next token isn't a `;', then we have a parse error. */
20031 else if (cp_lexer_next_token_is_not (parser->lexer,
20032 CPP_SEMICOLON))
20034 /* The next token might be a ways away from where the
20035 actual semicolon is missing. Find the previous token
20036 and use that for our error position. */
20037 cp_token *token = cp_lexer_previous_token (parser->lexer);
20038 error_at (token->location,
20039 "expected %<;%> at end of member declaration");
20041 /* Assume that the user meant to provide a semicolon. If
20042 we were to cp_parser_skip_to_end_of_statement, we might
20043 skip to a semicolon inside a member function definition
20044 and issue nonsensical error messages. */
20045 assume_semicolon = true;
20048 if (decl)
20050 /* Add DECL to the list of members. */
20051 if (!friend_p)
20052 finish_member_declaration (decl);
20054 if (TREE_CODE (decl) == FUNCTION_DECL)
20055 cp_parser_save_default_args (parser, decl);
20056 else if (TREE_CODE (decl) == FIELD_DECL
20057 && !DECL_C_BIT_FIELD (decl)
20058 && DECL_INITIAL (decl))
20059 /* Add DECL to the queue of NSDMI to be parsed later. */
20060 vec_safe_push (unparsed_nsdmis, decl);
20063 if (assume_semicolon)
20064 goto out;
20068 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20069 out:
20070 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20073 /* Parse a pure-specifier.
20075 pure-specifier:
20078 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20079 Otherwise, ERROR_MARK_NODE is returned. */
20081 static tree
20082 cp_parser_pure_specifier (cp_parser* parser)
20084 cp_token *token;
20086 /* Look for the `=' token. */
20087 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20088 return error_mark_node;
20089 /* Look for the `0' token. */
20090 token = cp_lexer_peek_token (parser->lexer);
20092 if (token->type == CPP_EOF
20093 || token->type == CPP_PRAGMA_EOL)
20094 return error_mark_node;
20096 cp_lexer_consume_token (parser->lexer);
20098 /* Accept = default or = delete in c++0x mode. */
20099 if (token->keyword == RID_DEFAULT
20100 || token->keyword == RID_DELETE)
20102 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20103 return token->u.value;
20106 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20107 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20109 cp_parser_error (parser,
20110 "invalid pure specifier (only %<= 0%> is allowed)");
20111 cp_parser_skip_to_end_of_statement (parser);
20112 return error_mark_node;
20114 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20116 error_at (token->location, "templates may not be %<virtual%>");
20117 return error_mark_node;
20120 return integer_zero_node;
20123 /* Parse a constant-initializer.
20125 constant-initializer:
20126 = constant-expression
20128 Returns a representation of the constant-expression. */
20130 static tree
20131 cp_parser_constant_initializer (cp_parser* parser)
20133 /* Look for the `=' token. */
20134 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20135 return error_mark_node;
20137 /* It is invalid to write:
20139 struct S { static const int i = { 7 }; };
20142 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20144 cp_parser_error (parser,
20145 "a brace-enclosed initializer is not allowed here");
20146 /* Consume the opening brace. */
20147 cp_lexer_consume_token (parser->lexer);
20148 /* Skip the initializer. */
20149 cp_parser_skip_to_closing_brace (parser);
20150 /* Look for the trailing `}'. */
20151 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20153 return error_mark_node;
20156 return cp_parser_constant_expression (parser,
20157 /*allow_non_constant=*/false,
20158 NULL);
20161 /* Derived classes [gram.class.derived] */
20163 /* Parse a base-clause.
20165 base-clause:
20166 : base-specifier-list
20168 base-specifier-list:
20169 base-specifier ... [opt]
20170 base-specifier-list , base-specifier ... [opt]
20172 Returns a TREE_LIST representing the base-classes, in the order in
20173 which they were declared. The representation of each node is as
20174 described by cp_parser_base_specifier.
20176 In the case that no bases are specified, this function will return
20177 NULL_TREE, not ERROR_MARK_NODE. */
20179 static tree
20180 cp_parser_base_clause (cp_parser* parser)
20182 tree bases = NULL_TREE;
20184 /* Look for the `:' that begins the list. */
20185 cp_parser_require (parser, CPP_COLON, RT_COLON);
20187 /* Scan the base-specifier-list. */
20188 while (true)
20190 cp_token *token;
20191 tree base;
20192 bool pack_expansion_p = false;
20194 /* Look for the base-specifier. */
20195 base = cp_parser_base_specifier (parser);
20196 /* Look for the (optional) ellipsis. */
20197 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20199 /* Consume the `...'. */
20200 cp_lexer_consume_token (parser->lexer);
20202 pack_expansion_p = true;
20205 /* Add BASE to the front of the list. */
20206 if (base && base != error_mark_node)
20208 if (pack_expansion_p)
20209 /* Make this a pack expansion type. */
20210 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20212 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20214 TREE_CHAIN (base) = bases;
20215 bases = base;
20218 /* Peek at the next token. */
20219 token = cp_lexer_peek_token (parser->lexer);
20220 /* If it's not a comma, then the list is complete. */
20221 if (token->type != CPP_COMMA)
20222 break;
20223 /* Consume the `,'. */
20224 cp_lexer_consume_token (parser->lexer);
20227 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20228 base class had a qualified name. However, the next name that
20229 appears is certainly not qualified. */
20230 parser->scope = NULL_TREE;
20231 parser->qualifying_scope = NULL_TREE;
20232 parser->object_scope = NULL_TREE;
20234 return nreverse (bases);
20237 /* Parse a base-specifier.
20239 base-specifier:
20240 :: [opt] nested-name-specifier [opt] class-name
20241 virtual access-specifier [opt] :: [opt] nested-name-specifier
20242 [opt] class-name
20243 access-specifier virtual [opt] :: [opt] nested-name-specifier
20244 [opt] class-name
20246 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20247 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20248 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20249 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20251 static tree
20252 cp_parser_base_specifier (cp_parser* parser)
20254 cp_token *token;
20255 bool done = false;
20256 bool virtual_p = false;
20257 bool duplicate_virtual_error_issued_p = false;
20258 bool duplicate_access_error_issued_p = false;
20259 bool class_scope_p, template_p;
20260 tree access = access_default_node;
20261 tree type;
20263 /* Process the optional `virtual' and `access-specifier'. */
20264 while (!done)
20266 /* Peek at the next token. */
20267 token = cp_lexer_peek_token (parser->lexer);
20268 /* Process `virtual'. */
20269 switch (token->keyword)
20271 case RID_VIRTUAL:
20272 /* If `virtual' appears more than once, issue an error. */
20273 if (virtual_p && !duplicate_virtual_error_issued_p)
20275 cp_parser_error (parser,
20276 "%<virtual%> specified more than once in base-specified");
20277 duplicate_virtual_error_issued_p = true;
20280 virtual_p = true;
20282 /* Consume the `virtual' token. */
20283 cp_lexer_consume_token (parser->lexer);
20285 break;
20287 case RID_PUBLIC:
20288 case RID_PROTECTED:
20289 case RID_PRIVATE:
20290 /* If more than one access specifier appears, issue an
20291 error. */
20292 if (access != access_default_node
20293 && !duplicate_access_error_issued_p)
20295 cp_parser_error (parser,
20296 "more than one access specifier in base-specified");
20297 duplicate_access_error_issued_p = true;
20300 access = ridpointers[(int) token->keyword];
20302 /* Consume the access-specifier. */
20303 cp_lexer_consume_token (parser->lexer);
20305 break;
20307 default:
20308 done = true;
20309 break;
20312 /* It is not uncommon to see programs mechanically, erroneously, use
20313 the 'typename' keyword to denote (dependent) qualified types
20314 as base classes. */
20315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20317 token = cp_lexer_peek_token (parser->lexer);
20318 if (!processing_template_decl)
20319 error_at (token->location,
20320 "keyword %<typename%> not allowed outside of templates");
20321 else
20322 error_at (token->location,
20323 "keyword %<typename%> not allowed in this context "
20324 "(the base class is implicitly a type)");
20325 cp_lexer_consume_token (parser->lexer);
20328 /* Look for the optional `::' operator. */
20329 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20330 /* Look for the nested-name-specifier. The simplest way to
20331 implement:
20333 [temp.res]
20335 The keyword `typename' is not permitted in a base-specifier or
20336 mem-initializer; in these contexts a qualified name that
20337 depends on a template-parameter is implicitly assumed to be a
20338 type name.
20340 is to pretend that we have seen the `typename' keyword at this
20341 point. */
20342 cp_parser_nested_name_specifier_opt (parser,
20343 /*typename_keyword_p=*/true,
20344 /*check_dependency_p=*/true,
20345 typename_type,
20346 /*is_declaration=*/true);
20347 /* If the base class is given by a qualified name, assume that names
20348 we see are type names or templates, as appropriate. */
20349 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20350 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20352 if (!parser->scope
20353 && cp_lexer_next_token_is_decltype (parser->lexer))
20354 /* DR 950 allows decltype as a base-specifier. */
20355 type = cp_parser_decltype (parser);
20356 else
20358 /* Otherwise, look for the class-name. */
20359 type = cp_parser_class_name (parser,
20360 class_scope_p,
20361 template_p,
20362 typename_type,
20363 /*check_dependency_p=*/true,
20364 /*class_head_p=*/false,
20365 /*is_declaration=*/true);
20366 type = TREE_TYPE (type);
20369 if (type == error_mark_node)
20370 return error_mark_node;
20372 return finish_base_specifier (type, access, virtual_p);
20375 /* Exception handling [gram.exception] */
20377 /* Parse an (optional) noexcept-specification.
20379 noexcept-specification:
20380 noexcept ( constant-expression ) [opt]
20382 If no noexcept-specification is present, returns NULL_TREE.
20383 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20384 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20385 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20386 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20387 in which case a boolean condition is returned instead. */
20389 static tree
20390 cp_parser_noexcept_specification_opt (cp_parser* parser,
20391 bool require_constexpr,
20392 bool* consumed_expr,
20393 bool return_cond)
20395 cp_token *token;
20396 const char *saved_message;
20398 /* Peek at the next token. */
20399 token = cp_lexer_peek_token (parser->lexer);
20401 /* Is it a noexcept-specification? */
20402 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20404 tree expr;
20405 cp_lexer_consume_token (parser->lexer);
20407 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20409 cp_lexer_consume_token (parser->lexer);
20411 if (require_constexpr)
20413 /* Types may not be defined in an exception-specification. */
20414 saved_message = parser->type_definition_forbidden_message;
20415 parser->type_definition_forbidden_message
20416 = G_("types may not be defined in an exception-specification");
20418 expr = cp_parser_constant_expression (parser, false, NULL);
20420 /* Restore the saved message. */
20421 parser->type_definition_forbidden_message = saved_message;
20423 else
20425 expr = cp_parser_expression (parser, false, NULL);
20426 *consumed_expr = true;
20429 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20431 else
20433 expr = boolean_true_node;
20434 if (!require_constexpr)
20435 *consumed_expr = false;
20438 /* We cannot build a noexcept-spec right away because this will check
20439 that expr is a constexpr. */
20440 if (!return_cond)
20441 return build_noexcept_spec (expr, tf_warning_or_error);
20442 else
20443 return expr;
20445 else
20446 return NULL_TREE;
20449 /* Parse an (optional) exception-specification.
20451 exception-specification:
20452 throw ( type-id-list [opt] )
20454 Returns a TREE_LIST representing the exception-specification. The
20455 TREE_VALUE of each node is a type. */
20457 static tree
20458 cp_parser_exception_specification_opt (cp_parser* parser)
20460 cp_token *token;
20461 tree type_id_list;
20462 const char *saved_message;
20464 /* Peek at the next token. */
20465 token = cp_lexer_peek_token (parser->lexer);
20467 /* Is it a noexcept-specification? */
20468 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
20469 false);
20470 if (type_id_list != NULL_TREE)
20471 return type_id_list;
20473 /* If it's not `throw', then there's no exception-specification. */
20474 if (!cp_parser_is_keyword (token, RID_THROW))
20475 return NULL_TREE;
20477 #if 0
20478 /* Enable this once a lot of code has transitioned to noexcept? */
20479 if (cxx_dialect >= cxx11 && !in_system_header)
20480 warning (OPT_Wdeprecated, "dynamic exception specifications are "
20481 "deprecated in C++0x; use %<noexcept%> instead");
20482 #endif
20484 /* Consume the `throw'. */
20485 cp_lexer_consume_token (parser->lexer);
20487 /* Look for the `('. */
20488 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20490 /* Peek at the next token. */
20491 token = cp_lexer_peek_token (parser->lexer);
20492 /* If it's not a `)', then there is a type-id-list. */
20493 if (token->type != CPP_CLOSE_PAREN)
20495 /* Types may not be defined in an exception-specification. */
20496 saved_message = parser->type_definition_forbidden_message;
20497 parser->type_definition_forbidden_message
20498 = G_("types may not be defined in an exception-specification");
20499 /* Parse the type-id-list. */
20500 type_id_list = cp_parser_type_id_list (parser);
20501 /* Restore the saved message. */
20502 parser->type_definition_forbidden_message = saved_message;
20504 else
20505 type_id_list = empty_except_spec;
20507 /* Look for the `)'. */
20508 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20510 return type_id_list;
20513 /* Parse an (optional) type-id-list.
20515 type-id-list:
20516 type-id ... [opt]
20517 type-id-list , type-id ... [opt]
20519 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
20520 in the order that the types were presented. */
20522 static tree
20523 cp_parser_type_id_list (cp_parser* parser)
20525 tree types = NULL_TREE;
20527 while (true)
20529 cp_token *token;
20530 tree type;
20532 /* Get the next type-id. */
20533 type = cp_parser_type_id (parser);
20534 /* Parse the optional ellipsis. */
20535 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20537 /* Consume the `...'. */
20538 cp_lexer_consume_token (parser->lexer);
20540 /* Turn the type into a pack expansion expression. */
20541 type = make_pack_expansion (type);
20543 /* Add it to the list. */
20544 types = add_exception_specifier (types, type, /*complain=*/1);
20545 /* Peek at the next token. */
20546 token = cp_lexer_peek_token (parser->lexer);
20547 /* If it is not a `,', we are done. */
20548 if (token->type != CPP_COMMA)
20549 break;
20550 /* Consume the `,'. */
20551 cp_lexer_consume_token (parser->lexer);
20554 return nreverse (types);
20557 /* Parse a try-block.
20559 try-block:
20560 try compound-statement handler-seq */
20562 static tree
20563 cp_parser_try_block (cp_parser* parser)
20565 tree try_block;
20567 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
20568 try_block = begin_try_block ();
20569 cp_parser_compound_statement (parser, NULL, true, false);
20570 finish_try_block (try_block);
20571 cp_parser_handler_seq (parser);
20572 finish_handler_sequence (try_block);
20574 return try_block;
20577 /* Parse a function-try-block.
20579 function-try-block:
20580 try ctor-initializer [opt] function-body handler-seq */
20582 static bool
20583 cp_parser_function_try_block (cp_parser* parser)
20585 tree compound_stmt;
20586 tree try_block;
20587 bool ctor_initializer_p;
20589 /* Look for the `try' keyword. */
20590 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
20591 return false;
20592 /* Let the rest of the front end know where we are. */
20593 try_block = begin_function_try_block (&compound_stmt);
20594 /* Parse the function-body. */
20595 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
20596 (parser, /*in_function_try_block=*/true);
20597 /* We're done with the `try' part. */
20598 finish_function_try_block (try_block);
20599 /* Parse the handlers. */
20600 cp_parser_handler_seq (parser);
20601 /* We're done with the handlers. */
20602 finish_function_handler_sequence (try_block, compound_stmt);
20604 return ctor_initializer_p;
20607 /* Parse a handler-seq.
20609 handler-seq:
20610 handler handler-seq [opt] */
20612 static void
20613 cp_parser_handler_seq (cp_parser* parser)
20615 while (true)
20617 cp_token *token;
20619 /* Parse the handler. */
20620 cp_parser_handler (parser);
20621 /* Peek at the next token. */
20622 token = cp_lexer_peek_token (parser->lexer);
20623 /* If it's not `catch' then there are no more handlers. */
20624 if (!cp_parser_is_keyword (token, RID_CATCH))
20625 break;
20629 /* Parse a handler.
20631 handler:
20632 catch ( exception-declaration ) compound-statement */
20634 static void
20635 cp_parser_handler (cp_parser* parser)
20637 tree handler;
20638 tree declaration;
20640 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20641 handler = begin_handler ();
20642 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20643 declaration = cp_parser_exception_declaration (parser);
20644 finish_handler_parms (declaration, handler);
20645 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20646 cp_parser_compound_statement (parser, NULL, false, false);
20647 finish_handler (handler);
20650 /* Parse an exception-declaration.
20652 exception-declaration:
20653 type-specifier-seq declarator
20654 type-specifier-seq abstract-declarator
20655 type-specifier-seq
20658 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20659 ellipsis variant is used. */
20661 static tree
20662 cp_parser_exception_declaration (cp_parser* parser)
20664 cp_decl_specifier_seq type_specifiers;
20665 cp_declarator *declarator;
20666 const char *saved_message;
20668 /* If it's an ellipsis, it's easy to handle. */
20669 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20671 /* Consume the `...' token. */
20672 cp_lexer_consume_token (parser->lexer);
20673 return NULL_TREE;
20676 /* Types may not be defined in exception-declarations. */
20677 saved_message = parser->type_definition_forbidden_message;
20678 parser->type_definition_forbidden_message
20679 = G_("types may not be defined in exception-declarations");
20681 /* Parse the type-specifier-seq. */
20682 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20683 /*is_trailing_return=*/false,
20684 &type_specifiers);
20685 /* If it's a `)', then there is no declarator. */
20686 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20687 declarator = NULL;
20688 else
20689 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20690 /*ctor_dtor_or_conv_p=*/NULL,
20691 /*parenthesized_p=*/NULL,
20692 /*member_p=*/false);
20694 /* Restore the saved message. */
20695 parser->type_definition_forbidden_message = saved_message;
20697 if (!type_specifiers.any_specifiers_p)
20698 return error_mark_node;
20700 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20703 /* Parse a throw-expression.
20705 throw-expression:
20706 throw assignment-expression [opt]
20708 Returns a THROW_EXPR representing the throw-expression. */
20710 static tree
20711 cp_parser_throw_expression (cp_parser* parser)
20713 tree expression;
20714 cp_token* token;
20716 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20717 token = cp_lexer_peek_token (parser->lexer);
20718 /* Figure out whether or not there is an assignment-expression
20719 following the "throw" keyword. */
20720 if (token->type == CPP_COMMA
20721 || token->type == CPP_SEMICOLON
20722 || token->type == CPP_CLOSE_PAREN
20723 || token->type == CPP_CLOSE_SQUARE
20724 || token->type == CPP_CLOSE_BRACE
20725 || token->type == CPP_COLON)
20726 expression = NULL_TREE;
20727 else
20728 expression = cp_parser_assignment_expression (parser,
20729 /*cast_p=*/false, NULL);
20731 return build_throw (expression);
20734 /* GNU Extensions */
20736 /* Parse an (optional) asm-specification.
20738 asm-specification:
20739 asm ( string-literal )
20741 If the asm-specification is present, returns a STRING_CST
20742 corresponding to the string-literal. Otherwise, returns
20743 NULL_TREE. */
20745 static tree
20746 cp_parser_asm_specification_opt (cp_parser* parser)
20748 cp_token *token;
20749 tree asm_specification;
20751 /* Peek at the next token. */
20752 token = cp_lexer_peek_token (parser->lexer);
20753 /* If the next token isn't the `asm' keyword, then there's no
20754 asm-specification. */
20755 if (!cp_parser_is_keyword (token, RID_ASM))
20756 return NULL_TREE;
20758 /* Consume the `asm' token. */
20759 cp_lexer_consume_token (parser->lexer);
20760 /* Look for the `('. */
20761 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20763 /* Look for the string-literal. */
20764 asm_specification = cp_parser_string_literal (parser, false, false);
20766 /* Look for the `)'. */
20767 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20769 return asm_specification;
20772 /* Parse an asm-operand-list.
20774 asm-operand-list:
20775 asm-operand
20776 asm-operand-list , asm-operand
20778 asm-operand:
20779 string-literal ( expression )
20780 [ string-literal ] string-literal ( expression )
20782 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20783 each node is the expression. The TREE_PURPOSE is itself a
20784 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20785 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20786 is a STRING_CST for the string literal before the parenthesis. Returns
20787 ERROR_MARK_NODE if any of the operands are invalid. */
20789 static tree
20790 cp_parser_asm_operand_list (cp_parser* parser)
20792 tree asm_operands = NULL_TREE;
20793 bool invalid_operands = false;
20795 while (true)
20797 tree string_literal;
20798 tree expression;
20799 tree name;
20801 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20803 /* Consume the `[' token. */
20804 cp_lexer_consume_token (parser->lexer);
20805 /* Read the operand name. */
20806 name = cp_parser_identifier (parser);
20807 if (name != error_mark_node)
20808 name = build_string (IDENTIFIER_LENGTH (name),
20809 IDENTIFIER_POINTER (name));
20810 /* Look for the closing `]'. */
20811 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20813 else
20814 name = NULL_TREE;
20815 /* Look for the string-literal. */
20816 string_literal = cp_parser_string_literal (parser, false, false);
20818 /* Look for the `('. */
20819 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20820 /* Parse the expression. */
20821 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20822 /* Look for the `)'. */
20823 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20825 if (name == error_mark_node
20826 || string_literal == error_mark_node
20827 || expression == error_mark_node)
20828 invalid_operands = true;
20830 /* Add this operand to the list. */
20831 asm_operands = tree_cons (build_tree_list (name, string_literal),
20832 expression,
20833 asm_operands);
20834 /* If the next token is not a `,', there are no more
20835 operands. */
20836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20837 break;
20838 /* Consume the `,'. */
20839 cp_lexer_consume_token (parser->lexer);
20842 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20845 /* Parse an asm-clobber-list.
20847 asm-clobber-list:
20848 string-literal
20849 asm-clobber-list , string-literal
20851 Returns a TREE_LIST, indicating the clobbers in the order that they
20852 appeared. The TREE_VALUE of each node is a STRING_CST. */
20854 static tree
20855 cp_parser_asm_clobber_list (cp_parser* parser)
20857 tree clobbers = NULL_TREE;
20859 while (true)
20861 tree string_literal;
20863 /* Look for the string literal. */
20864 string_literal = cp_parser_string_literal (parser, false, false);
20865 /* Add it to the list. */
20866 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20867 /* If the next token is not a `,', then the list is
20868 complete. */
20869 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20870 break;
20871 /* Consume the `,' token. */
20872 cp_lexer_consume_token (parser->lexer);
20875 return clobbers;
20878 /* Parse an asm-label-list.
20880 asm-label-list:
20881 identifier
20882 asm-label-list , identifier
20884 Returns a TREE_LIST, indicating the labels in the order that they
20885 appeared. The TREE_VALUE of each node is a label. */
20887 static tree
20888 cp_parser_asm_label_list (cp_parser* parser)
20890 tree labels = NULL_TREE;
20892 while (true)
20894 tree identifier, label, name;
20896 /* Look for the identifier. */
20897 identifier = cp_parser_identifier (parser);
20898 if (!error_operand_p (identifier))
20900 label = lookup_label (identifier);
20901 if (TREE_CODE (label) == LABEL_DECL)
20903 TREE_USED (label) = 1;
20904 check_goto (label);
20905 name = build_string (IDENTIFIER_LENGTH (identifier),
20906 IDENTIFIER_POINTER (identifier));
20907 labels = tree_cons (name, label, labels);
20910 /* If the next token is not a `,', then the list is
20911 complete. */
20912 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20913 break;
20914 /* Consume the `,' token. */
20915 cp_lexer_consume_token (parser->lexer);
20918 return nreverse (labels);
20921 /* Return TRUE iff the next tokens in the stream are possibly the
20922 beginning of a GNU extension attribute. */
20924 static bool
20925 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
20927 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
20930 /* Return TRUE iff the next tokens in the stream are possibly the
20931 beginning of a standard C++-11 attribute specifier. */
20933 static bool
20934 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
20936 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
20939 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20940 beginning of a standard C++-11 attribute specifier. */
20942 static bool
20943 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
20945 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20947 return (cxx_dialect >= cxx11
20948 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
20949 || (token->type == CPP_OPEN_SQUARE
20950 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
20951 && token->type == CPP_OPEN_SQUARE)));
20954 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20955 beginning of a GNU extension attribute. */
20957 static bool
20958 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
20960 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20962 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
20965 /* Return true iff the next tokens can be the beginning of either a
20966 GNU attribute list, or a standard C++11 attribute sequence. */
20968 static bool
20969 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
20971 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
20972 || cp_next_tokens_can_be_std_attribute_p (parser));
20975 /* Return true iff the next Nth tokens can be the beginning of either
20976 a GNU attribute list, or a standard C++11 attribute sequence. */
20978 static bool
20979 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
20981 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
20982 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
20985 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20986 of GNU attributes, or return NULL. */
20988 static tree
20989 cp_parser_attributes_opt (cp_parser *parser)
20991 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
20992 return cp_parser_gnu_attributes_opt (parser);
20993 return cp_parser_std_attribute_spec_seq (parser);
20996 /* Parse an (optional) series of attributes.
20998 attributes:
20999 attributes attribute
21001 attribute:
21002 __attribute__ (( attribute-list [opt] ))
21004 The return value is as for cp_parser_gnu_attribute_list. */
21006 static tree
21007 cp_parser_gnu_attributes_opt (cp_parser* parser)
21009 tree attributes = NULL_TREE;
21011 while (true)
21013 cp_token *token;
21014 tree attribute_list;
21015 bool ok = true;
21017 /* Peek at the next token. */
21018 token = cp_lexer_peek_token (parser->lexer);
21019 /* If it's not `__attribute__', then we're done. */
21020 if (token->keyword != RID_ATTRIBUTE)
21021 break;
21023 /* Consume the `__attribute__' keyword. */
21024 cp_lexer_consume_token (parser->lexer);
21025 /* Look for the two `(' tokens. */
21026 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21027 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21029 /* Peek at the next token. */
21030 token = cp_lexer_peek_token (parser->lexer);
21031 if (token->type != CPP_CLOSE_PAREN)
21032 /* Parse the attribute-list. */
21033 attribute_list = cp_parser_gnu_attribute_list (parser);
21034 else
21035 /* If the next token is a `)', then there is no attribute
21036 list. */
21037 attribute_list = NULL;
21039 /* Look for the two `)' tokens. */
21040 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21041 ok = false;
21042 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21043 ok = false;
21044 if (!ok)
21045 cp_parser_skip_to_end_of_statement (parser);
21047 /* Add these new attributes to the list. */
21048 attributes = chainon (attributes, attribute_list);
21051 return attributes;
21054 /* Parse a GNU attribute-list.
21056 attribute-list:
21057 attribute
21058 attribute-list , attribute
21060 attribute:
21061 identifier
21062 identifier ( identifier )
21063 identifier ( identifier , expression-list )
21064 identifier ( expression-list )
21066 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21067 to an attribute. The TREE_PURPOSE of each node is the identifier
21068 indicating which attribute is in use. The TREE_VALUE represents
21069 the arguments, if any. */
21071 static tree
21072 cp_parser_gnu_attribute_list (cp_parser* parser)
21074 tree attribute_list = NULL_TREE;
21075 bool save_translate_strings_p = parser->translate_strings_p;
21077 parser->translate_strings_p = false;
21078 while (true)
21080 cp_token *token;
21081 tree identifier;
21082 tree attribute;
21084 /* Look for the identifier. We also allow keywords here; for
21085 example `__attribute__ ((const))' is legal. */
21086 token = cp_lexer_peek_token (parser->lexer);
21087 if (token->type == CPP_NAME
21088 || token->type == CPP_KEYWORD)
21090 tree arguments = NULL_TREE;
21092 /* Consume the token. */
21093 token = cp_lexer_consume_token (parser->lexer);
21095 /* Save away the identifier that indicates which attribute
21096 this is. */
21097 identifier = (token->type == CPP_KEYWORD)
21098 /* For keywords, use the canonical spelling, not the
21099 parsed identifier. */
21100 ? ridpointers[(int) token->keyword]
21101 : token->u.value;
21103 attribute = build_tree_list (identifier, NULL_TREE);
21105 /* Peek at the next token. */
21106 token = cp_lexer_peek_token (parser->lexer);
21107 /* If it's an `(', then parse the attribute arguments. */
21108 if (token->type == CPP_OPEN_PAREN)
21110 vec<tree, va_gc> *vec;
21111 int attr_flag = (attribute_takes_identifier_p (identifier)
21112 ? id_attr : normal_attr);
21113 vec = cp_parser_parenthesized_expression_list
21114 (parser, attr_flag, /*cast_p=*/false,
21115 /*allow_expansion_p=*/false,
21116 /*non_constant_p=*/NULL);
21117 if (vec == NULL)
21118 arguments = error_mark_node;
21119 else
21121 arguments = build_tree_list_vec (vec);
21122 release_tree_vector (vec);
21124 /* Save the arguments away. */
21125 TREE_VALUE (attribute) = arguments;
21128 if (arguments != error_mark_node)
21130 /* Add this attribute to the list. */
21131 TREE_CHAIN (attribute) = attribute_list;
21132 attribute_list = attribute;
21135 token = cp_lexer_peek_token (parser->lexer);
21137 /* Now, look for more attributes. If the next token isn't a
21138 `,', we're done. */
21139 if (token->type != CPP_COMMA)
21140 break;
21142 /* Consume the comma and keep going. */
21143 cp_lexer_consume_token (parser->lexer);
21145 parser->translate_strings_p = save_translate_strings_p;
21147 /* We built up the list in reverse order. */
21148 return nreverse (attribute_list);
21151 /* Parse a standard C++11 attribute.
21153 The returned representation is a TREE_LIST which TREE_PURPOSE is
21154 the scoped name of the attribute, and the TREE_VALUE is its
21155 arguments list.
21157 Note that the scoped name of the attribute is itself a TREE_LIST
21158 which TREE_PURPOSE is the namespace of the attribute, and
21159 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21160 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21161 and which TREE_PURPOSE is directly the attribute name.
21163 Clients of the attribute code should use get_attribute_namespace
21164 and get_attribute_name to get the actual namespace and name of
21165 attributes, regardless of their being GNU or C++11 attributes.
21167 attribute:
21168 attribute-token attribute-argument-clause [opt]
21170 attribute-token:
21171 identifier
21172 attribute-scoped-token
21174 attribute-scoped-token:
21175 attribute-namespace :: identifier
21177 attribute-namespace:
21178 identifier
21180 attribute-argument-clause:
21181 ( balanced-token-seq )
21183 balanced-token-seq:
21184 balanced-token [opt]
21185 balanced-token-seq balanced-token
21187 balanced-token:
21188 ( balanced-token-seq )
21189 [ balanced-token-seq ]
21190 { balanced-token-seq }. */
21192 static tree
21193 cp_parser_std_attribute (cp_parser *parser)
21195 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21196 cp_token *token;
21198 /* First, parse name of the the attribute, a.k.a
21199 attribute-token. */
21201 token = cp_lexer_peek_token (parser->lexer);
21202 if (token->type == CPP_NAME)
21203 attr_id = token->u.value;
21204 else if (token->type == CPP_KEYWORD)
21205 attr_id = ridpointers[(int) token->keyword];
21206 else if (token->flags & NAMED_OP)
21207 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21209 if (attr_id == NULL_TREE)
21210 return NULL_TREE;
21212 cp_lexer_consume_token (parser->lexer);
21214 token = cp_lexer_peek_token (parser->lexer);
21215 if (token->type == CPP_SCOPE)
21217 /* We are seeing a scoped attribute token. */
21219 cp_lexer_consume_token (parser->lexer);
21220 attr_ns = attr_id;
21222 token = cp_lexer_consume_token (parser->lexer);
21223 if (token->type == CPP_NAME)
21224 attr_id = token->u.value;
21225 else if (token->type == CPP_KEYWORD)
21226 attr_id = ridpointers[(int) token->keyword];
21227 else
21229 error_at (token->location,
21230 "expected an identifier for the attribute name");
21231 return error_mark_node;
21233 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21234 NULL_TREE);
21235 token = cp_lexer_peek_token (parser->lexer);
21237 else
21239 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21240 NULL_TREE);
21241 /* C++11 noreturn attribute is equivalent to GNU's. */
21242 if (is_attribute_p ("noreturn", attr_id))
21243 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21246 /* Now parse the optional argument clause of the attribute. */
21248 if (token->type != CPP_OPEN_PAREN)
21249 return attribute;
21252 vec<tree, va_gc> *vec;
21253 int attr_flag = normal_attr;
21255 if (attr_ns == get_identifier ("gnu")
21256 && attribute_takes_identifier_p (attr_id))
21257 /* A GNU attribute that takes an identifier in parameter. */
21258 attr_flag = id_attr;
21260 vec = cp_parser_parenthesized_expression_list
21261 (parser, attr_flag, /*cast_p=*/false,
21262 /*allow_expansion_p=*/true,
21263 /*non_constant_p=*/NULL);
21264 if (vec == NULL)
21265 arguments = error_mark_node;
21266 else
21268 arguments = build_tree_list_vec (vec);
21269 release_tree_vector (vec);
21272 if (arguments == error_mark_node)
21273 attribute = error_mark_node;
21274 else
21275 TREE_VALUE (attribute) = arguments;
21278 return attribute;
21281 /* Parse a list of standard C++-11 attributes.
21283 attribute-list:
21284 attribute [opt]
21285 attribute-list , attribute[opt]
21286 attribute ...
21287 attribute-list , attribute ...
21290 static tree
21291 cp_parser_std_attribute_list (cp_parser *parser)
21293 tree attributes = NULL_TREE, attribute = NULL_TREE;
21294 cp_token *token = NULL;
21296 while (true)
21298 attribute = cp_parser_std_attribute (parser);
21299 if (attribute == error_mark_node)
21300 break;
21301 if (attribute != NULL_TREE)
21303 TREE_CHAIN (attribute) = attributes;
21304 attributes = attribute;
21306 token = cp_lexer_peek_token (parser->lexer);
21307 if (token->type != CPP_COMMA)
21308 break;
21309 cp_lexer_consume_token (parser->lexer);
21311 attributes = nreverse (attributes);
21312 return attributes;
21315 /* Parse a standard C++-11 attribute specifier.
21317 attribute-specifier:
21318 [ [ attribute-list ] ]
21319 alignment-specifier
21321 alignment-specifier:
21322 alignas ( type-id ... [opt] )
21323 alignas ( alignment-expression ... [opt] ). */
21325 static tree
21326 cp_parser_std_attribute_spec (cp_parser *parser)
21328 tree attributes = NULL_TREE;
21329 cp_token *token = cp_lexer_peek_token (parser->lexer);
21331 if (token->type == CPP_OPEN_SQUARE
21332 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21334 cp_lexer_consume_token (parser->lexer);
21335 cp_lexer_consume_token (parser->lexer);
21337 attributes = cp_parser_std_attribute_list (parser);
21339 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21340 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21341 cp_parser_skip_to_end_of_statement (parser);
21342 else
21343 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21344 when we are sure that we have actually parsed them. */
21345 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21347 else
21349 tree alignas_expr;
21351 /* Look for an alignment-specifier. */
21353 token = cp_lexer_peek_token (parser->lexer);
21355 if (token->type != CPP_KEYWORD
21356 || token->keyword != RID_ALIGNAS)
21357 return NULL_TREE;
21359 cp_lexer_consume_token (parser->lexer);
21360 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21362 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
21364 cp_parser_error (parser, "expected %<(%>");
21365 return error_mark_node;
21368 cp_parser_parse_tentatively (parser);
21369 alignas_expr = cp_parser_type_id (parser);
21371 if (!cp_parser_parse_definitely (parser))
21373 gcc_assert (alignas_expr == error_mark_node
21374 || alignas_expr == NULL_TREE);
21376 alignas_expr =
21377 cp_parser_assignment_expression (parser, /*cast_p=*/false,
21378 /**cp_id_kind=*/NULL);
21379 if (alignas_expr == NULL_TREE
21380 || alignas_expr == error_mark_node)
21381 return alignas_expr;
21384 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
21386 cp_parser_error (parser, "expected %<)%>");
21387 return error_mark_node;
21390 alignas_expr = cxx_alignas_expr (alignas_expr);
21392 /* Build the C++-11 representation of an 'aligned'
21393 attribute. */
21394 attributes =
21395 build_tree_list (build_tree_list (get_identifier ("gnu"),
21396 get_identifier ("aligned")),
21397 build_tree_list (NULL_TREE, alignas_expr));
21400 return attributes;
21403 /* Parse a standard C++-11 attribute-specifier-seq.
21405 attribute-specifier-seq:
21406 attribute-specifier-seq [opt] attribute-specifier
21409 static tree
21410 cp_parser_std_attribute_spec_seq (cp_parser *parser)
21412 tree attr_specs = NULL;
21414 while (true)
21416 tree attr_spec = cp_parser_std_attribute_spec (parser);
21417 if (attr_spec == NULL_TREE)
21418 break;
21419 if (attr_spec == error_mark_node)
21420 return error_mark_node;
21422 TREE_CHAIN (attr_spec) = attr_specs;
21423 attr_specs = attr_spec;
21426 attr_specs = nreverse (attr_specs);
21427 return attr_specs;
21430 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
21431 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
21432 current value of the PEDANTIC flag, regardless of whether or not
21433 the `__extension__' keyword is present. The caller is responsible
21434 for restoring the value of the PEDANTIC flag. */
21436 static bool
21437 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
21439 /* Save the old value of the PEDANTIC flag. */
21440 *saved_pedantic = pedantic;
21442 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
21444 /* Consume the `__extension__' token. */
21445 cp_lexer_consume_token (parser->lexer);
21446 /* We're not being pedantic while the `__extension__' keyword is
21447 in effect. */
21448 pedantic = 0;
21450 return true;
21453 return false;
21456 /* Parse a label declaration.
21458 label-declaration:
21459 __label__ label-declarator-seq ;
21461 label-declarator-seq:
21462 identifier , label-declarator-seq
21463 identifier */
21465 static void
21466 cp_parser_label_declaration (cp_parser* parser)
21468 /* Look for the `__label__' keyword. */
21469 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
21471 while (true)
21473 tree identifier;
21475 /* Look for an identifier. */
21476 identifier = cp_parser_identifier (parser);
21477 /* If we failed, stop. */
21478 if (identifier == error_mark_node)
21479 break;
21480 /* Declare it as a label. */
21481 finish_label_decl (identifier);
21482 /* If the next token is a `;', stop. */
21483 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21484 break;
21485 /* Look for the `,' separating the label declarations. */
21486 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
21489 /* Look for the final `;'. */
21490 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21493 /* Support Functions */
21495 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21496 NAME should have one of the representations used for an
21497 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21498 is returned. If PARSER->SCOPE is a dependent type, then a
21499 SCOPE_REF is returned.
21501 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21502 returned; the name was already resolved when the TEMPLATE_ID_EXPR
21503 was formed. Abstractly, such entities should not be passed to this
21504 function, because they do not need to be looked up, but it is
21505 simpler to check for this special case here, rather than at the
21506 call-sites.
21508 In cases not explicitly covered above, this function returns a
21509 DECL, OVERLOAD, or baselink representing the result of the lookup.
21510 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21511 is returned.
21513 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21514 (e.g., "struct") that was used. In that case bindings that do not
21515 refer to types are ignored.
21517 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21518 ignored.
21520 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21521 are ignored.
21523 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21524 types.
21526 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21527 TREE_LIST of candidates if name-lookup results in an ambiguity, and
21528 NULL_TREE otherwise. */
21530 static tree
21531 cp_parser_lookup_name (cp_parser *parser, tree name,
21532 enum tag_types tag_type,
21533 bool is_template,
21534 bool is_namespace,
21535 bool check_dependency,
21536 tree *ambiguous_decls,
21537 location_t name_location)
21539 tree decl;
21540 tree object_type = parser->context->object_type;
21542 /* Assume that the lookup will be unambiguous. */
21543 if (ambiguous_decls)
21544 *ambiguous_decls = NULL_TREE;
21546 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21547 no longer valid. Note that if we are parsing tentatively, and
21548 the parse fails, OBJECT_TYPE will be automatically restored. */
21549 parser->context->object_type = NULL_TREE;
21551 if (name == error_mark_node)
21552 return error_mark_node;
21554 /* A template-id has already been resolved; there is no lookup to
21555 do. */
21556 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
21557 return name;
21558 if (BASELINK_P (name))
21560 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
21561 == TEMPLATE_ID_EXPR);
21562 return name;
21565 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
21566 it should already have been checked to make sure that the name
21567 used matches the type being destroyed. */
21568 if (TREE_CODE (name) == BIT_NOT_EXPR)
21570 tree type;
21572 /* Figure out to which type this destructor applies. */
21573 if (parser->scope)
21574 type = parser->scope;
21575 else if (object_type)
21576 type = object_type;
21577 else
21578 type = current_class_type;
21579 /* If that's not a class type, there is no destructor. */
21580 if (!type || !CLASS_TYPE_P (type))
21581 return error_mark_node;
21582 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
21583 lazily_declare_fn (sfk_destructor, type);
21584 if (!CLASSTYPE_DESTRUCTORS (type))
21585 return error_mark_node;
21586 /* If it was a class type, return the destructor. */
21587 return CLASSTYPE_DESTRUCTORS (type);
21590 /* By this point, the NAME should be an ordinary identifier. If
21591 the id-expression was a qualified name, the qualifying scope is
21592 stored in PARSER->SCOPE at this point. */
21593 gcc_assert (identifier_p (name));
21595 /* Perform the lookup. */
21596 if (parser->scope)
21598 bool dependent_p;
21600 if (parser->scope == error_mark_node)
21601 return error_mark_node;
21603 /* If the SCOPE is dependent, the lookup must be deferred until
21604 the template is instantiated -- unless we are explicitly
21605 looking up names in uninstantiated templates. Even then, we
21606 cannot look up the name if the scope is not a class type; it
21607 might, for example, be a template type parameter. */
21608 dependent_p = (TYPE_P (parser->scope)
21609 && dependent_scope_p (parser->scope));
21610 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21611 && dependent_p)
21612 /* Defer lookup. */
21613 decl = error_mark_node;
21614 else
21616 tree pushed_scope = NULL_TREE;
21618 /* If PARSER->SCOPE is a dependent type, then it must be a
21619 class type, and we must not be checking dependencies;
21620 otherwise, we would have processed this lookup above. So
21621 that PARSER->SCOPE is not considered a dependent base by
21622 lookup_member, we must enter the scope here. */
21623 if (dependent_p)
21624 pushed_scope = push_scope (parser->scope);
21626 /* If the PARSER->SCOPE is a template specialization, it
21627 may be instantiated during name lookup. In that case,
21628 errors may be issued. Even if we rollback the current
21629 tentative parse, those errors are valid. */
21630 decl = lookup_qualified_name (parser->scope, name,
21631 tag_type != none_type,
21632 /*complain=*/true);
21634 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21635 lookup result and the nested-name-specifier nominates a class C:
21636 * if the name specified after the nested-name-specifier, when
21637 looked up in C, is the injected-class-name of C (Clause 9), or
21638 * if the name specified after the nested-name-specifier is the
21639 same as the identifier or the simple-template-id's template-
21640 name in the last component of the nested-name-specifier,
21641 the name is instead considered to name the constructor of
21642 class C. [ Note: for example, the constructor is not an
21643 acceptable lookup result in an elaborated-type-specifier so
21644 the constructor would not be used in place of the
21645 injected-class-name. --end note ] Such a constructor name
21646 shall be used only in the declarator-id of a declaration that
21647 names a constructor or in a using-declaration. */
21648 if (tag_type == none_type
21649 && DECL_SELF_REFERENCE_P (decl)
21650 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21651 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21652 tag_type != none_type,
21653 /*complain=*/true);
21655 /* If we have a single function from a using decl, pull it out. */
21656 if (TREE_CODE (decl) == OVERLOAD
21657 && !really_overloaded_fn (decl))
21658 decl = OVL_FUNCTION (decl);
21660 if (pushed_scope)
21661 pop_scope (pushed_scope);
21664 /* If the scope is a dependent type and either we deferred lookup or
21665 we did lookup but didn't find the name, rememeber the name. */
21666 if (decl == error_mark_node && TYPE_P (parser->scope)
21667 && dependent_type_p (parser->scope))
21669 if (tag_type)
21671 tree type;
21673 /* The resolution to Core Issue 180 says that `struct
21674 A::B' should be considered a type-name, even if `A'
21675 is dependent. */
21676 type = make_typename_type (parser->scope, name, tag_type,
21677 /*complain=*/tf_error);
21678 decl = TYPE_NAME (type);
21680 else if (is_template
21681 && (cp_parser_next_token_ends_template_argument_p (parser)
21682 || cp_lexer_next_token_is (parser->lexer,
21683 CPP_CLOSE_PAREN)))
21684 decl = make_unbound_class_template (parser->scope,
21685 name, NULL_TREE,
21686 /*complain=*/tf_error);
21687 else
21688 decl = build_qualified_name (/*type=*/NULL_TREE,
21689 parser->scope, name,
21690 is_template);
21692 parser->qualifying_scope = parser->scope;
21693 parser->object_scope = NULL_TREE;
21695 else if (object_type)
21697 tree object_decl = NULL_TREE;
21698 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21699 OBJECT_TYPE is not a class. */
21700 if (CLASS_TYPE_P (object_type))
21701 /* If the OBJECT_TYPE is a template specialization, it may
21702 be instantiated during name lookup. In that case, errors
21703 may be issued. Even if we rollback the current tentative
21704 parse, those errors are valid. */
21705 object_decl = lookup_member (object_type,
21706 name,
21707 /*protect=*/0,
21708 tag_type != none_type,
21709 tf_warning_or_error);
21710 /* Look it up in the enclosing context, too. */
21711 decl = lookup_name_real (name, tag_type != none_type,
21712 /*nonclass=*/0,
21713 /*block_p=*/true, is_namespace, 0);
21714 parser->object_scope = object_type;
21715 parser->qualifying_scope = NULL_TREE;
21716 if (object_decl)
21717 decl = object_decl;
21719 else
21721 decl = lookup_name_real (name, tag_type != none_type,
21722 /*nonclass=*/0,
21723 /*block_p=*/true, is_namespace, 0);
21724 parser->qualifying_scope = NULL_TREE;
21725 parser->object_scope = NULL_TREE;
21728 /* If the lookup failed, let our caller know. */
21729 if (!decl || decl == error_mark_node)
21730 return error_mark_node;
21732 /* Pull out the template from an injected-class-name (or multiple). */
21733 if (is_template)
21734 decl = maybe_get_template_decl_from_type_decl (decl);
21736 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21737 if (TREE_CODE (decl) == TREE_LIST)
21739 if (ambiguous_decls)
21740 *ambiguous_decls = decl;
21741 /* The error message we have to print is too complicated for
21742 cp_parser_error, so we incorporate its actions directly. */
21743 if (!cp_parser_simulate_error (parser))
21745 error_at (name_location, "reference to %qD is ambiguous",
21746 name);
21747 print_candidates (decl);
21749 return error_mark_node;
21752 gcc_assert (DECL_P (decl)
21753 || TREE_CODE (decl) == OVERLOAD
21754 || TREE_CODE (decl) == SCOPE_REF
21755 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21756 || BASELINK_P (decl));
21758 /* If we have resolved the name of a member declaration, check to
21759 see if the declaration is accessible. When the name resolves to
21760 set of overloaded functions, accessibility is checked when
21761 overload resolution is done.
21763 During an explicit instantiation, access is not checked at all,
21764 as per [temp.explicit]. */
21765 if (DECL_P (decl))
21766 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21768 maybe_record_typedef_use (decl);
21770 return decl;
21773 /* Like cp_parser_lookup_name, but for use in the typical case where
21774 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21775 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21777 static tree
21778 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21780 return cp_parser_lookup_name (parser, name,
21781 none_type,
21782 /*is_template=*/false,
21783 /*is_namespace=*/false,
21784 /*check_dependency=*/true,
21785 /*ambiguous_decls=*/NULL,
21786 location);
21789 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21790 the current context, return the TYPE_DECL. If TAG_NAME_P is
21791 true, the DECL indicates the class being defined in a class-head,
21792 or declared in an elaborated-type-specifier.
21794 Otherwise, return DECL. */
21796 static tree
21797 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
21799 /* If the TEMPLATE_DECL is being declared as part of a class-head,
21800 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21802 struct A {
21803 template <typename T> struct B;
21806 template <typename T> struct A::B {};
21808 Similarly, in an elaborated-type-specifier:
21810 namespace N { struct X{}; }
21812 struct A {
21813 template <typename T> friend struct N::X;
21816 However, if the DECL refers to a class type, and we are in
21817 the scope of the class, then the name lookup automatically
21818 finds the TYPE_DECL created by build_self_reference rather
21819 than a TEMPLATE_DECL. For example, in:
21821 template <class T> struct S {
21822 S s;
21825 there is no need to handle such case. */
21827 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
21828 return DECL_TEMPLATE_RESULT (decl);
21830 return decl;
21833 /* If too many, or too few, template-parameter lists apply to the
21834 declarator, issue an error message. Returns TRUE if all went well,
21835 and FALSE otherwise. */
21837 static bool
21838 cp_parser_check_declarator_template_parameters (cp_parser* parser,
21839 cp_declarator *declarator,
21840 location_t declarator_location)
21842 switch (declarator->kind)
21844 case cdk_id:
21846 unsigned num_templates = 0;
21847 tree scope = declarator->u.id.qualifying_scope;
21849 if (scope)
21850 num_templates = num_template_headers_for_class (scope);
21851 else if (TREE_CODE (declarator->u.id.unqualified_name)
21852 == TEMPLATE_ID_EXPR)
21853 /* If the DECLARATOR has the form `X<y>' then it uses one
21854 additional level of template parameters. */
21855 ++num_templates;
21857 return cp_parser_check_template_parameters
21858 (parser, num_templates, declarator_location, declarator);
21861 case cdk_function:
21862 case cdk_array:
21863 case cdk_pointer:
21864 case cdk_reference:
21865 case cdk_ptrmem:
21866 return (cp_parser_check_declarator_template_parameters
21867 (parser, declarator->declarator, declarator_location));
21869 case cdk_error:
21870 return true;
21872 default:
21873 gcc_unreachable ();
21875 return false;
21878 /* NUM_TEMPLATES were used in the current declaration. If that is
21879 invalid, return FALSE and issue an error messages. Otherwise,
21880 return TRUE. If DECLARATOR is non-NULL, then we are checking a
21881 declarator and we can print more accurate diagnostics. */
21883 static bool
21884 cp_parser_check_template_parameters (cp_parser* parser,
21885 unsigned num_templates,
21886 location_t location,
21887 cp_declarator *declarator)
21889 /* If there are the same number of template classes and parameter
21890 lists, that's OK. */
21891 if (parser->num_template_parameter_lists == num_templates)
21892 return true;
21893 /* If there are more, but only one more, then we are referring to a
21894 member template. That's OK too. */
21895 if (parser->num_template_parameter_lists == num_templates + 1)
21896 return true;
21897 /* If there are more template classes than parameter lists, we have
21898 something like:
21900 template <class T> void S<T>::R<T>::f (); */
21901 if (parser->num_template_parameter_lists < num_templates)
21903 if (declarator && !current_function_decl)
21904 error_at (location, "specializing member %<%T::%E%> "
21905 "requires %<template<>%> syntax",
21906 declarator->u.id.qualifying_scope,
21907 declarator->u.id.unqualified_name);
21908 else if (declarator)
21909 error_at (location, "invalid declaration of %<%T::%E%>",
21910 declarator->u.id.qualifying_scope,
21911 declarator->u.id.unqualified_name);
21912 else
21913 error_at (location, "too few template-parameter-lists");
21914 return false;
21916 /* Otherwise, there are too many template parameter lists. We have
21917 something like:
21919 template <class T> template <class U> void S::f(); */
21920 error_at (location, "too many template-parameter-lists");
21921 return false;
21924 /* Parse an optional `::' token indicating that the following name is
21925 from the global namespace. If so, PARSER->SCOPE is set to the
21926 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21927 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21928 Returns the new value of PARSER->SCOPE, if the `::' token is
21929 present, and NULL_TREE otherwise. */
21931 static tree
21932 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
21934 cp_token *token;
21936 /* Peek at the next token. */
21937 token = cp_lexer_peek_token (parser->lexer);
21938 /* If we're looking at a `::' token then we're starting from the
21939 global namespace, not our current location. */
21940 if (token->type == CPP_SCOPE)
21942 /* Consume the `::' token. */
21943 cp_lexer_consume_token (parser->lexer);
21944 /* Set the SCOPE so that we know where to start the lookup. */
21945 parser->scope = global_namespace;
21946 parser->qualifying_scope = global_namespace;
21947 parser->object_scope = NULL_TREE;
21949 return parser->scope;
21951 else if (!current_scope_valid_p)
21953 parser->scope = NULL_TREE;
21954 parser->qualifying_scope = NULL_TREE;
21955 parser->object_scope = NULL_TREE;
21958 return NULL_TREE;
21961 /* Returns TRUE if the upcoming token sequence is the start of a
21962 constructor declarator. If FRIEND_P is true, the declarator is
21963 preceded by the `friend' specifier. */
21965 static bool
21966 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
21968 bool constructor_p;
21969 tree nested_name_specifier;
21970 cp_token *next_token;
21972 /* The common case is that this is not a constructor declarator, so
21973 try to avoid doing lots of work if at all possible. It's not
21974 valid declare a constructor at function scope. */
21975 if (parser->in_function_body)
21976 return false;
21977 /* And only certain tokens can begin a constructor declarator. */
21978 next_token = cp_lexer_peek_token (parser->lexer);
21979 if (next_token->type != CPP_NAME
21980 && next_token->type != CPP_SCOPE
21981 && next_token->type != CPP_NESTED_NAME_SPECIFIER
21982 && next_token->type != CPP_TEMPLATE_ID)
21983 return false;
21985 /* Parse tentatively; we are going to roll back all of the tokens
21986 consumed here. */
21987 cp_parser_parse_tentatively (parser);
21988 /* Assume that we are looking at a constructor declarator. */
21989 constructor_p = true;
21991 /* Look for the optional `::' operator. */
21992 cp_parser_global_scope_opt (parser,
21993 /*current_scope_valid_p=*/false);
21994 /* Look for the nested-name-specifier. */
21995 nested_name_specifier
21996 = (cp_parser_nested_name_specifier_opt (parser,
21997 /*typename_keyword_p=*/false,
21998 /*check_dependency_p=*/false,
21999 /*type_p=*/false,
22000 /*is_declaration=*/false));
22001 /* Outside of a class-specifier, there must be a
22002 nested-name-specifier. */
22003 if (!nested_name_specifier &&
22004 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
22005 || friend_p))
22006 constructor_p = false;
22007 else if (nested_name_specifier == error_mark_node)
22008 constructor_p = false;
22010 /* If we have a class scope, this is easy; DR 147 says that S::S always
22011 names the constructor, and no other qualified name could. */
22012 if (constructor_p && nested_name_specifier
22013 && CLASS_TYPE_P (nested_name_specifier))
22015 tree id = cp_parser_unqualified_id (parser,
22016 /*template_keyword_p=*/false,
22017 /*check_dependency_p=*/false,
22018 /*declarator_p=*/true,
22019 /*optional_p=*/false);
22020 if (is_overloaded_fn (id))
22021 id = DECL_NAME (get_first_fn (id));
22022 if (!constructor_name_p (id, nested_name_specifier))
22023 constructor_p = false;
22025 /* If we still think that this might be a constructor-declarator,
22026 look for a class-name. */
22027 else if (constructor_p)
22029 /* If we have:
22031 template <typename T> struct S {
22032 S();
22035 we must recognize that the nested `S' names a class. */
22036 tree type_decl;
22037 type_decl = cp_parser_class_name (parser,
22038 /*typename_keyword_p=*/false,
22039 /*template_keyword_p=*/false,
22040 none_type,
22041 /*check_dependency_p=*/false,
22042 /*class_head_p=*/false,
22043 /*is_declaration=*/false);
22044 /* If there was no class-name, then this is not a constructor. */
22045 constructor_p = !cp_parser_error_occurred (parser);
22047 /* If we're still considering a constructor, we have to see a `(',
22048 to begin the parameter-declaration-clause, followed by either a
22049 `)', an `...', or a decl-specifier. We need to check for a
22050 type-specifier to avoid being fooled into thinking that:
22052 S (f) (int);
22054 is a constructor. (It is actually a function named `f' that
22055 takes one parameter (of type `int') and returns a value of type
22056 `S'. */
22057 if (constructor_p
22058 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22059 constructor_p = false;
22061 if (constructor_p
22062 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22063 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22064 /* A parameter declaration begins with a decl-specifier,
22065 which is either the "attribute" keyword, a storage class
22066 specifier, or (usually) a type-specifier. */
22067 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22069 tree type;
22070 tree pushed_scope = NULL_TREE;
22071 unsigned saved_num_template_parameter_lists;
22073 /* Names appearing in the type-specifier should be looked up
22074 in the scope of the class. */
22075 if (current_class_type)
22076 type = NULL_TREE;
22077 else
22079 type = TREE_TYPE (type_decl);
22080 if (TREE_CODE (type) == TYPENAME_TYPE)
22082 type = resolve_typename_type (type,
22083 /*only_current_p=*/false);
22084 if (TREE_CODE (type) == TYPENAME_TYPE)
22086 cp_parser_abort_tentative_parse (parser);
22087 return false;
22090 pushed_scope = push_scope (type);
22093 /* Inside the constructor parameter list, surrounding
22094 template-parameter-lists do not apply. */
22095 saved_num_template_parameter_lists
22096 = parser->num_template_parameter_lists;
22097 parser->num_template_parameter_lists = 0;
22099 /* Look for the type-specifier. */
22100 cp_parser_type_specifier (parser,
22101 CP_PARSER_FLAGS_NONE,
22102 /*decl_specs=*/NULL,
22103 /*is_declarator=*/true,
22104 /*declares_class_or_enum=*/NULL,
22105 /*is_cv_qualifier=*/NULL);
22107 parser->num_template_parameter_lists
22108 = saved_num_template_parameter_lists;
22110 /* Leave the scope of the class. */
22111 if (pushed_scope)
22112 pop_scope (pushed_scope);
22114 constructor_p = !cp_parser_error_occurred (parser);
22118 /* We did not really want to consume any tokens. */
22119 cp_parser_abort_tentative_parse (parser);
22121 return constructor_p;
22124 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22125 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22126 they must be performed once we are in the scope of the function.
22128 Returns the function defined. */
22130 static tree
22131 cp_parser_function_definition_from_specifiers_and_declarator
22132 (cp_parser* parser,
22133 cp_decl_specifier_seq *decl_specifiers,
22134 tree attributes,
22135 const cp_declarator *declarator)
22137 tree fn;
22138 bool success_p;
22140 /* Begin the function-definition. */
22141 success_p = start_function (decl_specifiers, declarator, attributes);
22143 /* The things we're about to see are not directly qualified by any
22144 template headers we've seen thus far. */
22145 reset_specialization ();
22147 /* If there were names looked up in the decl-specifier-seq that we
22148 did not check, check them now. We must wait until we are in the
22149 scope of the function to perform the checks, since the function
22150 might be a friend. */
22151 perform_deferred_access_checks (tf_warning_or_error);
22153 if (!success_p)
22155 /* Skip the entire function. */
22156 cp_parser_skip_to_end_of_block_or_statement (parser);
22157 fn = error_mark_node;
22159 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
22161 /* Seen already, skip it. An error message has already been output. */
22162 cp_parser_skip_to_end_of_block_or_statement (parser);
22163 fn = current_function_decl;
22164 current_function_decl = NULL_TREE;
22165 /* If this is a function from a class, pop the nested class. */
22166 if (current_class_name)
22167 pop_nested_class ();
22169 else
22171 timevar_id_t tv;
22172 if (DECL_DECLARED_INLINE_P (current_function_decl))
22173 tv = TV_PARSE_INLINE;
22174 else
22175 tv = TV_PARSE_FUNC;
22176 timevar_push (tv);
22177 fn = cp_parser_function_definition_after_declarator (parser,
22178 /*inline_p=*/false);
22179 timevar_pop (tv);
22182 return fn;
22185 /* Parse the part of a function-definition that follows the
22186 declarator. INLINE_P is TRUE iff this function is an inline
22187 function defined within a class-specifier.
22189 Returns the function defined. */
22191 static tree
22192 cp_parser_function_definition_after_declarator (cp_parser* parser,
22193 bool inline_p)
22195 tree fn;
22196 bool ctor_initializer_p = false;
22197 bool saved_in_unbraced_linkage_specification_p;
22198 bool saved_in_function_body;
22199 unsigned saved_num_template_parameter_lists;
22200 cp_token *token;
22202 saved_in_function_body = parser->in_function_body;
22203 parser->in_function_body = true;
22204 /* If the next token is `return', then the code may be trying to
22205 make use of the "named return value" extension that G++ used to
22206 support. */
22207 token = cp_lexer_peek_token (parser->lexer);
22208 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22210 /* Consume the `return' keyword. */
22211 cp_lexer_consume_token (parser->lexer);
22212 /* Look for the identifier that indicates what value is to be
22213 returned. */
22214 cp_parser_identifier (parser);
22215 /* Issue an error message. */
22216 error_at (token->location,
22217 "named return values are no longer supported");
22218 /* Skip tokens until we reach the start of the function body. */
22219 while (true)
22221 cp_token *token = cp_lexer_peek_token (parser->lexer);
22222 if (token->type == CPP_OPEN_BRACE
22223 || token->type == CPP_EOF
22224 || token->type == CPP_PRAGMA_EOL)
22225 break;
22226 cp_lexer_consume_token (parser->lexer);
22229 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22230 anything declared inside `f'. */
22231 saved_in_unbraced_linkage_specification_p
22232 = parser->in_unbraced_linkage_specification_p;
22233 parser->in_unbraced_linkage_specification_p = false;
22234 /* Inside the function, surrounding template-parameter-lists do not
22235 apply. */
22236 saved_num_template_parameter_lists
22237 = parser->num_template_parameter_lists;
22238 parser->num_template_parameter_lists = 0;
22240 start_lambda_scope (current_function_decl);
22242 /* If the next token is `try', `__transaction_atomic', or
22243 `__transaction_relaxed`, then we are looking at either function-try-block
22244 or function-transaction-block. Note that all of these include the
22245 function-body. */
22246 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22247 ctor_initializer_p = cp_parser_function_transaction (parser,
22248 RID_TRANSACTION_ATOMIC);
22249 else if (cp_lexer_next_token_is_keyword (parser->lexer,
22250 RID_TRANSACTION_RELAXED))
22251 ctor_initializer_p = cp_parser_function_transaction (parser,
22252 RID_TRANSACTION_RELAXED);
22253 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22254 ctor_initializer_p = cp_parser_function_try_block (parser);
22255 else
22256 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22257 (parser, /*in_function_try_block=*/false);
22259 finish_lambda_scope ();
22261 /* Finish the function. */
22262 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22263 (inline_p ? 2 : 0));
22264 /* Generate code for it, if necessary. */
22265 expand_or_defer_fn (fn);
22266 /* Restore the saved values. */
22267 parser->in_unbraced_linkage_specification_p
22268 = saved_in_unbraced_linkage_specification_p;
22269 parser->num_template_parameter_lists
22270 = saved_num_template_parameter_lists;
22271 parser->in_function_body = saved_in_function_body;
22273 return fn;
22276 /* Parse a template-declaration, assuming that the `export' (and
22277 `extern') keywords, if present, has already been scanned. MEMBER_P
22278 is as for cp_parser_template_declaration. */
22280 static void
22281 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22283 tree decl = NULL_TREE;
22284 vec<deferred_access_check, va_gc> *checks;
22285 tree parameter_list;
22286 bool friend_p = false;
22287 bool need_lang_pop;
22288 cp_token *token;
22290 /* Look for the `template' keyword. */
22291 token = cp_lexer_peek_token (parser->lexer);
22292 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22293 return;
22295 /* And the `<'. */
22296 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22297 return;
22298 if (at_class_scope_p () && current_function_decl)
22300 /* 14.5.2.2 [temp.mem]
22302 A local class shall not have member templates. */
22303 error_at (token->location,
22304 "invalid declaration of member template in local class");
22305 cp_parser_skip_to_end_of_block_or_statement (parser);
22306 return;
22308 /* [temp]
22310 A template ... shall not have C linkage. */
22311 if (current_lang_name == lang_name_c)
22313 error_at (token->location, "template with C linkage");
22314 /* Give it C++ linkage to avoid confusing other parts of the
22315 front end. */
22316 push_lang_context (lang_name_cplusplus);
22317 need_lang_pop = true;
22319 else
22320 need_lang_pop = false;
22322 /* We cannot perform access checks on the template parameter
22323 declarations until we know what is being declared, just as we
22324 cannot check the decl-specifier list. */
22325 push_deferring_access_checks (dk_deferred);
22327 /* If the next token is `>', then we have an invalid
22328 specialization. Rather than complain about an invalid template
22329 parameter, issue an error message here. */
22330 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
22332 cp_parser_error (parser, "invalid explicit specialization");
22333 begin_specialization ();
22334 parameter_list = NULL_TREE;
22336 else
22338 /* Parse the template parameters. */
22339 parameter_list = cp_parser_template_parameter_list (parser);
22342 /* Get the deferred access checks from the parameter list. These
22343 will be checked once we know what is being declared, as for a
22344 member template the checks must be performed in the scope of the
22345 class containing the member. */
22346 checks = get_deferred_access_checks ();
22348 /* Look for the `>'. */
22349 cp_parser_skip_to_end_of_template_parameter_list (parser);
22350 /* We just processed one more parameter list. */
22351 ++parser->num_template_parameter_lists;
22352 /* If the next token is `template', there are more template
22353 parameters. */
22354 if (cp_lexer_next_token_is_keyword (parser->lexer,
22355 RID_TEMPLATE))
22356 cp_parser_template_declaration_after_export (parser, member_p);
22357 else if (cxx_dialect >= cxx11
22358 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22359 decl = cp_parser_alias_declaration (parser);
22360 else
22362 /* There are no access checks when parsing a template, as we do not
22363 know if a specialization will be a friend. */
22364 push_deferring_access_checks (dk_no_check);
22365 token = cp_lexer_peek_token (parser->lexer);
22366 decl = cp_parser_single_declaration (parser,
22367 checks,
22368 member_p,
22369 /*explicit_specialization_p=*/false,
22370 &friend_p);
22371 pop_deferring_access_checks ();
22373 /* If this is a member template declaration, let the front
22374 end know. */
22375 if (member_p && !friend_p && decl)
22377 if (TREE_CODE (decl) == TYPE_DECL)
22378 cp_parser_check_access_in_redeclaration (decl, token->location);
22380 decl = finish_member_template_decl (decl);
22382 else if (friend_p && decl
22383 && DECL_DECLARES_TYPE_P (decl))
22384 make_friend_class (current_class_type, TREE_TYPE (decl),
22385 /*complain=*/true);
22387 /* We are done with the current parameter list. */
22388 --parser->num_template_parameter_lists;
22390 pop_deferring_access_checks ();
22392 /* Finish up. */
22393 finish_template_decl (parameter_list);
22395 /* Check the template arguments for a literal operator template. */
22396 if (decl
22397 && DECL_DECLARES_FUNCTION_P (decl)
22398 && UDLIT_OPER_P (DECL_NAME (decl)))
22400 bool ok = true;
22401 if (parameter_list == NULL_TREE)
22402 ok = false;
22403 else
22405 int num_parms = TREE_VEC_LENGTH (parameter_list);
22406 if (num_parms == 1)
22408 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
22409 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22410 if (TREE_TYPE (parm) != char_type_node
22411 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22412 ok = false;
22414 else if (num_parms == 2 && cxx_dialect >= cxx1y)
22416 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
22417 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
22418 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
22419 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22420 if (TREE_TYPE (parm) != TREE_TYPE (type)
22421 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22422 ok = false;
22424 else
22425 ok = false;
22427 if (!ok)
22428 error ("literal operator template %qD has invalid parameter list."
22429 " Expected non-type template argument pack <char...>"
22430 " or <typename CharT, CharT...>",
22431 decl);
22433 /* Register member declarations. */
22434 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
22435 finish_member_declaration (decl);
22436 /* For the erroneous case of a template with C linkage, we pushed an
22437 implicit C++ linkage scope; exit that scope now. */
22438 if (need_lang_pop)
22439 pop_lang_context ();
22440 /* If DECL is a function template, we must return to parse it later.
22441 (Even though there is no definition, there might be default
22442 arguments that need handling.) */
22443 if (member_p && decl
22444 && DECL_DECLARES_FUNCTION_P (decl))
22445 vec_safe_push (unparsed_funs_with_definitions, decl);
22448 /* Perform the deferred access checks from a template-parameter-list.
22449 CHECKS is a TREE_LIST of access checks, as returned by
22450 get_deferred_access_checks. */
22452 static void
22453 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
22455 ++processing_template_parmlist;
22456 perform_access_checks (checks, tf_warning_or_error);
22457 --processing_template_parmlist;
22460 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22461 `function-definition' sequence that follows a template header.
22462 If MEMBER_P is true, this declaration appears in a class scope.
22464 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
22465 *FRIEND_P is set to TRUE iff the declaration is a friend. */
22467 static tree
22468 cp_parser_single_declaration (cp_parser* parser,
22469 vec<deferred_access_check, va_gc> *checks,
22470 bool member_p,
22471 bool explicit_specialization_p,
22472 bool* friend_p)
22474 int declares_class_or_enum;
22475 tree decl = NULL_TREE;
22476 cp_decl_specifier_seq decl_specifiers;
22477 bool function_definition_p = false;
22478 cp_token *decl_spec_token_start;
22480 /* This function is only used when processing a template
22481 declaration. */
22482 gcc_assert (innermost_scope_kind () == sk_template_parms
22483 || innermost_scope_kind () == sk_template_spec);
22485 /* Defer access checks until we know what is being declared. */
22486 push_deferring_access_checks (dk_deferred);
22488 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22489 alternative. */
22490 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22491 cp_parser_decl_specifier_seq (parser,
22492 CP_PARSER_FLAGS_OPTIONAL,
22493 &decl_specifiers,
22494 &declares_class_or_enum);
22495 if (friend_p)
22496 *friend_p = cp_parser_friend_p (&decl_specifiers);
22498 /* There are no template typedefs. */
22499 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
22501 error_at (decl_spec_token_start->location,
22502 "template declaration of %<typedef%>");
22503 decl = error_mark_node;
22506 /* Gather up the access checks that occurred the
22507 decl-specifier-seq. */
22508 stop_deferring_access_checks ();
22510 /* Check for the declaration of a template class. */
22511 if (declares_class_or_enum)
22513 if (cp_parser_declares_only_class_p (parser))
22515 decl = shadow_tag (&decl_specifiers);
22517 /* In this case:
22519 struct C {
22520 friend template <typename T> struct A<T>::B;
22523 A<T>::B will be represented by a TYPENAME_TYPE, and
22524 therefore not recognized by shadow_tag. */
22525 if (friend_p && *friend_p
22526 && !decl
22527 && decl_specifiers.type
22528 && TYPE_P (decl_specifiers.type))
22529 decl = decl_specifiers.type;
22531 if (decl && decl != error_mark_node)
22532 decl = TYPE_NAME (decl);
22533 else
22534 decl = error_mark_node;
22536 /* Perform access checks for template parameters. */
22537 cp_parser_perform_template_parameter_access_checks (checks);
22541 /* Complain about missing 'typename' or other invalid type names. */
22542 if (!decl_specifiers.any_type_specifiers_p
22543 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22545 /* cp_parser_parse_and_diagnose_invalid_type_name calls
22546 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22547 the rest of this declaration. */
22548 decl = error_mark_node;
22549 goto out;
22552 /* If it's not a template class, try for a template function. If
22553 the next token is a `;', then this declaration does not declare
22554 anything. But, if there were errors in the decl-specifiers, then
22555 the error might well have come from an attempted class-specifier.
22556 In that case, there's no need to warn about a missing declarator. */
22557 if (!decl
22558 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
22559 || decl_specifiers.type != error_mark_node))
22561 decl = cp_parser_init_declarator (parser,
22562 &decl_specifiers,
22563 checks,
22564 /*function_definition_allowed_p=*/true,
22565 member_p,
22566 declares_class_or_enum,
22567 &function_definition_p,
22568 NULL);
22570 /* 7.1.1-1 [dcl.stc]
22572 A storage-class-specifier shall not be specified in an explicit
22573 specialization... */
22574 if (decl
22575 && explicit_specialization_p
22576 && decl_specifiers.storage_class != sc_none)
22578 error_at (decl_spec_token_start->location,
22579 "explicit template specialization cannot have a storage class");
22580 decl = error_mark_node;
22583 if (decl && VAR_P (decl))
22584 check_template_variable (decl);
22587 /* Look for a trailing `;' after the declaration. */
22588 if (!function_definition_p
22589 && (decl == error_mark_node
22590 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
22591 cp_parser_skip_to_end_of_block_or_statement (parser);
22593 out:
22594 pop_deferring_access_checks ();
22596 /* Clear any current qualification; whatever comes next is the start
22597 of something new. */
22598 parser->scope = NULL_TREE;
22599 parser->qualifying_scope = NULL_TREE;
22600 parser->object_scope = NULL_TREE;
22602 return decl;
22605 /* Parse a cast-expression that is not the operand of a unary "&". */
22607 static tree
22608 cp_parser_simple_cast_expression (cp_parser *parser)
22610 return cp_parser_cast_expression (parser, /*address_p=*/false,
22611 /*cast_p=*/false, /*decltype*/false, NULL);
22614 /* Parse a functional cast to TYPE. Returns an expression
22615 representing the cast. */
22617 static tree
22618 cp_parser_functional_cast (cp_parser* parser, tree type)
22620 vec<tree, va_gc> *vec;
22621 tree expression_list;
22622 tree cast;
22623 bool nonconst_p;
22625 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22627 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22628 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22629 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22630 if (TREE_CODE (type) == TYPE_DECL)
22631 type = TREE_TYPE (type);
22632 return finish_compound_literal (type, expression_list,
22633 tf_warning_or_error);
22637 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22638 /*cast_p=*/true,
22639 /*allow_expansion_p=*/true,
22640 /*non_constant_p=*/NULL);
22641 if (vec == NULL)
22642 expression_list = error_mark_node;
22643 else
22645 expression_list = build_tree_list_vec (vec);
22646 release_tree_vector (vec);
22649 cast = build_functional_cast (type, expression_list,
22650 tf_warning_or_error);
22651 /* [expr.const]/1: In an integral constant expression "only type
22652 conversions to integral or enumeration type can be used". */
22653 if (TREE_CODE (type) == TYPE_DECL)
22654 type = TREE_TYPE (type);
22655 if (cast != error_mark_node
22656 && !cast_valid_in_integral_constant_expression_p (type)
22657 && cp_parser_non_integral_constant_expression (parser,
22658 NIC_CONSTRUCTOR))
22659 return error_mark_node;
22660 return cast;
22663 /* Save the tokens that make up the body of a member function defined
22664 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22665 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22666 specifiers applied to the declaration. Returns the FUNCTION_DECL
22667 for the member function. */
22669 static tree
22670 cp_parser_save_member_function_body (cp_parser* parser,
22671 cp_decl_specifier_seq *decl_specifiers,
22672 cp_declarator *declarator,
22673 tree attributes)
22675 cp_token *first;
22676 cp_token *last;
22677 tree fn;
22679 /* Create the FUNCTION_DECL. */
22680 fn = grokmethod (decl_specifiers, declarator, attributes);
22681 /* If something went badly wrong, bail out now. */
22682 if (fn == error_mark_node)
22684 /* If there's a function-body, skip it. */
22685 if (cp_parser_token_starts_function_definition_p
22686 (cp_lexer_peek_token (parser->lexer)))
22687 cp_parser_skip_to_end_of_block_or_statement (parser);
22688 return error_mark_node;
22691 /* Remember it, if there default args to post process. */
22692 cp_parser_save_default_args (parser, fn);
22694 /* Save away the tokens that make up the body of the
22695 function. */
22696 first = parser->lexer->next_token;
22697 /* Handle function try blocks. */
22698 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22699 cp_lexer_consume_token (parser->lexer);
22700 /* We can have braced-init-list mem-initializers before the fn body. */
22701 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22703 cp_lexer_consume_token (parser->lexer);
22704 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22706 /* cache_group will stop after an un-nested { } pair, too. */
22707 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22708 break;
22710 /* variadic mem-inits have ... after the ')'. */
22711 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22712 cp_lexer_consume_token (parser->lexer);
22715 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22716 /* Handle function try blocks. */
22717 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22718 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22719 last = parser->lexer->next_token;
22721 /* Save away the inline definition; we will process it when the
22722 class is complete. */
22723 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22724 DECL_PENDING_INLINE_P (fn) = 1;
22726 /* We need to know that this was defined in the class, so that
22727 friend templates are handled correctly. */
22728 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22730 /* Add FN to the queue of functions to be parsed later. */
22731 vec_safe_push (unparsed_funs_with_definitions, fn);
22733 return fn;
22736 /* Save the tokens that make up the in-class initializer for a non-static
22737 data member. Returns a DEFAULT_ARG. */
22739 static tree
22740 cp_parser_save_nsdmi (cp_parser* parser)
22742 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22745 /* Parse a template-argument-list, as well as the trailing ">" (but
22746 not the opening "<"). See cp_parser_template_argument_list for the
22747 return value. */
22749 static tree
22750 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22752 tree arguments;
22753 tree saved_scope;
22754 tree saved_qualifying_scope;
22755 tree saved_object_scope;
22756 bool saved_greater_than_is_operator_p;
22757 int saved_unevaluated_operand;
22758 int saved_inhibit_evaluation_warnings;
22760 /* [temp.names]
22762 When parsing a template-id, the first non-nested `>' is taken as
22763 the end of the template-argument-list rather than a greater-than
22764 operator. */
22765 saved_greater_than_is_operator_p
22766 = parser->greater_than_is_operator_p;
22767 parser->greater_than_is_operator_p = false;
22768 /* Parsing the argument list may modify SCOPE, so we save it
22769 here. */
22770 saved_scope = parser->scope;
22771 saved_qualifying_scope = parser->qualifying_scope;
22772 saved_object_scope = parser->object_scope;
22773 /* We need to evaluate the template arguments, even though this
22774 template-id may be nested within a "sizeof". */
22775 saved_unevaluated_operand = cp_unevaluated_operand;
22776 cp_unevaluated_operand = 0;
22777 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22778 c_inhibit_evaluation_warnings = 0;
22779 /* Parse the template-argument-list itself. */
22780 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
22781 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22782 arguments = NULL_TREE;
22783 else
22784 arguments = cp_parser_template_argument_list (parser);
22785 /* Look for the `>' that ends the template-argument-list. If we find
22786 a '>>' instead, it's probably just a typo. */
22787 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22789 if (cxx_dialect != cxx98)
22791 /* In C++0x, a `>>' in a template argument list or cast
22792 expression is considered to be two separate `>'
22793 tokens. So, change the current token to a `>', but don't
22794 consume it: it will be consumed later when the outer
22795 template argument list (or cast expression) is parsed.
22796 Note that this replacement of `>' for `>>' is necessary
22797 even if we are parsing tentatively: in the tentative
22798 case, after calling
22799 cp_parser_enclosed_template_argument_list we will always
22800 throw away all of the template arguments and the first
22801 closing `>', either because the template argument list
22802 was erroneous or because we are replacing those tokens
22803 with a CPP_TEMPLATE_ID token. The second `>' (which will
22804 not have been thrown away) is needed either to close an
22805 outer template argument list or to complete a new-style
22806 cast. */
22807 cp_token *token = cp_lexer_peek_token (parser->lexer);
22808 token->type = CPP_GREATER;
22810 else if (!saved_greater_than_is_operator_p)
22812 /* If we're in a nested template argument list, the '>>' has
22813 to be a typo for '> >'. We emit the error message, but we
22814 continue parsing and we push a '>' as next token, so that
22815 the argument list will be parsed correctly. Note that the
22816 global source location is still on the token before the
22817 '>>', so we need to say explicitly where we want it. */
22818 cp_token *token = cp_lexer_peek_token (parser->lexer);
22819 error_at (token->location, "%<>>%> should be %<> >%> "
22820 "within a nested template argument list");
22822 token->type = CPP_GREATER;
22824 else
22826 /* If this is not a nested template argument list, the '>>'
22827 is a typo for '>'. Emit an error message and continue.
22828 Same deal about the token location, but here we can get it
22829 right by consuming the '>>' before issuing the diagnostic. */
22830 cp_token *token = cp_lexer_consume_token (parser->lexer);
22831 error_at (token->location,
22832 "spurious %<>>%>, use %<>%> to terminate "
22833 "a template argument list");
22836 else
22837 cp_parser_skip_to_end_of_template_parameter_list (parser);
22838 /* The `>' token might be a greater-than operator again now. */
22839 parser->greater_than_is_operator_p
22840 = saved_greater_than_is_operator_p;
22841 /* Restore the SAVED_SCOPE. */
22842 parser->scope = saved_scope;
22843 parser->qualifying_scope = saved_qualifying_scope;
22844 parser->object_scope = saved_object_scope;
22845 cp_unevaluated_operand = saved_unevaluated_operand;
22846 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22848 return arguments;
22851 /* MEMBER_FUNCTION is a member function, or a friend. If default
22852 arguments, or the body of the function have not yet been parsed,
22853 parse them now. */
22855 static void
22856 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
22858 timevar_push (TV_PARSE_INMETH);
22859 /* If this member is a template, get the underlying
22860 FUNCTION_DECL. */
22861 if (DECL_FUNCTION_TEMPLATE_P (member_function))
22862 member_function = DECL_TEMPLATE_RESULT (member_function);
22864 /* There should not be any class definitions in progress at this
22865 point; the bodies of members are only parsed outside of all class
22866 definitions. */
22867 gcc_assert (parser->num_classes_being_defined == 0);
22868 /* While we're parsing the member functions we might encounter more
22869 classes. We want to handle them right away, but we don't want
22870 them getting mixed up with functions that are currently in the
22871 queue. */
22872 push_unparsed_function_queues (parser);
22874 /* Make sure that any template parameters are in scope. */
22875 maybe_begin_member_template_processing (member_function);
22877 /* If the body of the function has not yet been parsed, parse it
22878 now. */
22879 if (DECL_PENDING_INLINE_P (member_function))
22881 tree function_scope;
22882 cp_token_cache *tokens;
22884 /* The function is no longer pending; we are processing it. */
22885 tokens = DECL_PENDING_INLINE_INFO (member_function);
22886 DECL_PENDING_INLINE_INFO (member_function) = NULL;
22887 DECL_PENDING_INLINE_P (member_function) = 0;
22889 /* If this is a local class, enter the scope of the containing
22890 function. */
22891 function_scope = current_function_decl;
22892 if (function_scope)
22893 push_function_context ();
22895 /* Push the body of the function onto the lexer stack. */
22896 cp_parser_push_lexer_for_tokens (parser, tokens);
22898 /* Let the front end know that we going to be defining this
22899 function. */
22900 start_preparsed_function (member_function, NULL_TREE,
22901 SF_PRE_PARSED | SF_INCLASS_INLINE);
22903 /* Don't do access checking if it is a templated function. */
22904 if (processing_template_decl)
22905 push_deferring_access_checks (dk_no_check);
22907 /* Now, parse the body of the function. */
22908 cp_parser_function_definition_after_declarator (parser,
22909 /*inline_p=*/true);
22911 if (processing_template_decl)
22912 pop_deferring_access_checks ();
22914 /* Leave the scope of the containing function. */
22915 if (function_scope)
22916 pop_function_context ();
22917 cp_parser_pop_lexer (parser);
22920 /* Remove any template parameters from the symbol table. */
22921 maybe_end_member_template_processing ();
22923 /* Restore the queue. */
22924 pop_unparsed_function_queues (parser);
22925 timevar_pop (TV_PARSE_INMETH);
22928 /* If DECL contains any default args, remember it on the unparsed
22929 functions queue. */
22931 static void
22932 cp_parser_save_default_args (cp_parser* parser, tree decl)
22934 tree probe;
22936 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
22937 probe;
22938 probe = TREE_CHAIN (probe))
22939 if (TREE_PURPOSE (probe))
22941 cp_default_arg_entry entry = {current_class_type, decl};
22942 vec_safe_push (unparsed_funs_with_default_args, entry);
22943 break;
22947 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22948 which is either a FIELD_DECL or PARM_DECL. Parse it and return
22949 the result. For a PARM_DECL, PARMTYPE is the corresponding type
22950 from the parameter-type-list. */
22952 static tree
22953 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
22954 tree default_arg, tree parmtype)
22956 cp_token_cache *tokens;
22957 tree parsed_arg;
22958 bool dummy;
22960 if (default_arg == error_mark_node)
22961 return error_mark_node;
22963 /* Push the saved tokens for the default argument onto the parser's
22964 lexer stack. */
22965 tokens = DEFARG_TOKENS (default_arg);
22966 cp_parser_push_lexer_for_tokens (parser, tokens);
22968 start_lambda_scope (decl);
22970 /* Parse the default argument. */
22971 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
22972 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
22973 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22975 finish_lambda_scope ();
22977 if (parsed_arg == error_mark_node)
22978 cp_parser_skip_to_end_of_statement (parser);
22980 if (!processing_template_decl)
22982 /* In a non-template class, check conversions now. In a template,
22983 we'll wait and instantiate these as needed. */
22984 if (TREE_CODE (decl) == PARM_DECL)
22985 parsed_arg = check_default_argument (parmtype, parsed_arg);
22986 else
22988 int flags = LOOKUP_IMPLICIT;
22989 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
22990 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
22991 flags = LOOKUP_NORMAL;
22992 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
22996 /* If the token stream has not been completely used up, then
22997 there was extra junk after the end of the default
22998 argument. */
22999 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23001 if (TREE_CODE (decl) == PARM_DECL)
23002 cp_parser_error (parser, "expected %<,%>");
23003 else
23004 cp_parser_error (parser, "expected %<;%>");
23007 /* Revert to the main lexer. */
23008 cp_parser_pop_lexer (parser);
23010 return parsed_arg;
23013 /* FIELD is a non-static data member with an initializer which we saved for
23014 later; parse it now. */
23016 static void
23017 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23019 tree def;
23021 push_unparsed_function_queues (parser);
23022 def = cp_parser_late_parse_one_default_arg (parser, field,
23023 DECL_INITIAL (field),
23024 NULL_TREE);
23025 pop_unparsed_function_queues (parser);
23027 DECL_INITIAL (field) = def;
23030 /* FN is a FUNCTION_DECL which may contains a parameter with an
23031 unparsed DEFAULT_ARG. Parse the default args now. This function
23032 assumes that the current scope is the scope in which the default
23033 argument should be processed. */
23035 static void
23036 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23038 bool saved_local_variables_forbidden_p;
23039 tree parm, parmdecl;
23041 /* While we're parsing the default args, we might (due to the
23042 statement expression extension) encounter more classes. We want
23043 to handle them right away, but we don't want them getting mixed
23044 up with default args that are currently in the queue. */
23045 push_unparsed_function_queues (parser);
23047 /* Local variable names (and the `this' keyword) may not appear
23048 in a default argument. */
23049 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23050 parser->local_variables_forbidden_p = true;
23052 push_defarg_context (fn);
23054 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23055 parmdecl = DECL_ARGUMENTS (fn);
23056 parm && parm != void_list_node;
23057 parm = TREE_CHAIN (parm),
23058 parmdecl = DECL_CHAIN (parmdecl))
23060 tree default_arg = TREE_PURPOSE (parm);
23061 tree parsed_arg;
23062 vec<tree, va_gc> *insts;
23063 tree copy;
23064 unsigned ix;
23066 if (!default_arg)
23067 continue;
23069 if (TREE_CODE (default_arg) != DEFAULT_ARG)
23070 /* This can happen for a friend declaration for a function
23071 already declared with default arguments. */
23072 continue;
23074 parsed_arg
23075 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
23076 default_arg,
23077 TREE_VALUE (parm));
23078 if (parsed_arg == error_mark_node)
23080 continue;
23083 TREE_PURPOSE (parm) = parsed_arg;
23085 /* Update any instantiations we've already created. */
23086 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
23087 vec_safe_iterate (insts, ix, &copy); ix++)
23088 TREE_PURPOSE (copy) = parsed_arg;
23091 pop_defarg_context ();
23093 /* Make sure no default arg is missing. */
23094 check_default_args (fn);
23096 /* Restore the state of local_variables_forbidden_p. */
23097 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23099 /* Restore the queue. */
23100 pop_unparsed_function_queues (parser);
23103 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23105 sizeof ... ( identifier )
23107 where the 'sizeof' token has already been consumed. */
23109 static tree
23110 cp_parser_sizeof_pack (cp_parser *parser)
23112 /* Consume the `...'. */
23113 cp_lexer_consume_token (parser->lexer);
23114 maybe_warn_variadic_templates ();
23116 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
23117 if (paren)
23118 cp_lexer_consume_token (parser->lexer);
23119 else
23120 permerror (cp_lexer_peek_token (parser->lexer)->location,
23121 "%<sizeof...%> argument must be surrounded by parentheses");
23123 cp_token *token = cp_lexer_peek_token (parser->lexer);
23124 tree name = cp_parser_identifier (parser);
23125 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
23126 if (expr == error_mark_node)
23127 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
23128 token->location);
23129 if (TREE_CODE (expr) == TYPE_DECL)
23130 expr = TREE_TYPE (expr);
23131 else if (TREE_CODE (expr) == CONST_DECL)
23132 expr = DECL_INITIAL (expr);
23133 expr = make_pack_expansion (expr);
23135 if (paren)
23136 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23138 return expr;
23141 /* Parse the operand of `sizeof' (or a similar operator). Returns
23142 either a TYPE or an expression, depending on the form of the
23143 input. The KEYWORD indicates which kind of expression we have
23144 encountered. */
23146 static tree
23147 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
23149 tree expr = NULL_TREE;
23150 const char *saved_message;
23151 char *tmp;
23152 bool saved_integral_constant_expression_p;
23153 bool saved_non_integral_constant_expression_p;
23155 /* If it's a `...', then we are computing the length of a parameter
23156 pack. */
23157 if (keyword == RID_SIZEOF
23158 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23159 return cp_parser_sizeof_pack (parser);
23161 /* Types cannot be defined in a `sizeof' expression. Save away the
23162 old message. */
23163 saved_message = parser->type_definition_forbidden_message;
23164 /* And create the new one. */
23165 tmp = concat ("types may not be defined in %<",
23166 IDENTIFIER_POINTER (ridpointers[keyword]),
23167 "%> expressions", NULL);
23168 parser->type_definition_forbidden_message = tmp;
23170 /* The restrictions on constant-expressions do not apply inside
23171 sizeof expressions. */
23172 saved_integral_constant_expression_p
23173 = parser->integral_constant_expression_p;
23174 saved_non_integral_constant_expression_p
23175 = parser->non_integral_constant_expression_p;
23176 parser->integral_constant_expression_p = false;
23178 /* Do not actually evaluate the expression. */
23179 ++cp_unevaluated_operand;
23180 ++c_inhibit_evaluation_warnings;
23181 /* If it's a `(', then we might be looking at the type-id
23182 construction. */
23183 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23185 tree type = NULL_TREE;
23186 bool compound_literal_p;
23188 /* We can't be sure yet whether we're looking at a type-id or an
23189 expression. */
23190 cp_parser_parse_tentatively (parser);
23191 /* Consume the `('. */
23192 cp_lexer_consume_token (parser->lexer);
23193 /* Note: as a GNU Extension, compound literals are considered
23194 postfix-expressions as they are in C99, so they are valid
23195 arguments to sizeof. See comment in cp_parser_cast_expression
23196 for details. */
23197 cp_lexer_save_tokens (parser->lexer);
23198 /* Skip tokens until the next token is a closing parenthesis.
23199 If we find the closing `)', and the next token is a `{', then
23200 we are looking at a compound-literal. */
23201 compound_literal_p
23202 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
23203 /*consume_paren=*/true)
23204 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
23205 /* Roll back the tokens we skipped. */
23206 cp_lexer_rollback_tokens (parser->lexer);
23207 /* If we were looking at a compound-literal, simulate an error
23208 so that the call to cp_parser_parse_definitely below will
23209 fail. */
23210 if (compound_literal_p)
23211 cp_parser_simulate_error (parser);
23212 else
23214 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23215 parser->in_type_id_in_expr_p = true;
23216 /* Look for the type-id. */
23217 type = cp_parser_type_id (parser);
23218 /* Look for the closing `)'. */
23219 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23220 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23223 /* If all went well, then we're done. */
23224 if (cp_parser_parse_definitely (parser))
23226 cp_decl_specifier_seq decl_specs;
23228 /* Build a trivial decl-specifier-seq. */
23229 clear_decl_specs (&decl_specs);
23230 decl_specs.type = type;
23232 /* Call grokdeclarator to figure out what type this is. */
23233 expr = grokdeclarator (NULL,
23234 &decl_specs,
23235 TYPENAME,
23236 /*initialized=*/0,
23237 /*attrlist=*/NULL);
23241 /* If the type-id production did not work out, then we must be
23242 looking at the unary-expression production. */
23243 if (!expr)
23244 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23245 /*cast_p=*/false, NULL);
23247 /* Go back to evaluating expressions. */
23248 --cp_unevaluated_operand;
23249 --c_inhibit_evaluation_warnings;
23251 /* Free the message we created. */
23252 free (tmp);
23253 /* And restore the old one. */
23254 parser->type_definition_forbidden_message = saved_message;
23255 parser->integral_constant_expression_p
23256 = saved_integral_constant_expression_p;
23257 parser->non_integral_constant_expression_p
23258 = saved_non_integral_constant_expression_p;
23260 return expr;
23263 /* If the current declaration has no declarator, return true. */
23265 static bool
23266 cp_parser_declares_only_class_p (cp_parser *parser)
23268 /* If the next token is a `;' or a `,' then there is no
23269 declarator. */
23270 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23271 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23274 /* Update the DECL_SPECS to reflect the storage class indicated by
23275 KEYWORD. */
23277 static void
23278 cp_parser_set_storage_class (cp_parser *parser,
23279 cp_decl_specifier_seq *decl_specs,
23280 enum rid keyword,
23281 cp_token *token)
23283 cp_storage_class storage_class;
23285 if (parser->in_unbraced_linkage_specification_p)
23287 error_at (token->location, "invalid use of %qD in linkage specification",
23288 ridpointers[keyword]);
23289 return;
23291 else if (decl_specs->storage_class != sc_none)
23293 decl_specs->conflicting_specifiers_p = true;
23294 return;
23297 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
23298 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
23299 && decl_specs->gnu_thread_keyword_p)
23301 pedwarn (decl_specs->locations[ds_thread], 0,
23302 "%<__thread%> before %qD", ridpointers[keyword]);
23305 switch (keyword)
23307 case RID_AUTO:
23308 storage_class = sc_auto;
23309 break;
23310 case RID_REGISTER:
23311 storage_class = sc_register;
23312 break;
23313 case RID_STATIC:
23314 storage_class = sc_static;
23315 break;
23316 case RID_EXTERN:
23317 storage_class = sc_extern;
23318 break;
23319 case RID_MUTABLE:
23320 storage_class = sc_mutable;
23321 break;
23322 default:
23323 gcc_unreachable ();
23325 decl_specs->storage_class = storage_class;
23326 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
23328 /* A storage class specifier cannot be applied alongside a typedef
23329 specifier. If there is a typedef specifier present then set
23330 conflicting_specifiers_p which will trigger an error later
23331 on in grokdeclarator. */
23332 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
23333 decl_specs->conflicting_specifiers_p = true;
23336 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
23337 is true, the type is a class or enum definition. */
23339 static void
23340 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
23341 tree type_spec,
23342 cp_token *token,
23343 bool type_definition_p)
23345 decl_specs->any_specifiers_p = true;
23347 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
23348 (with, for example, in "typedef int wchar_t;") we remember that
23349 this is what happened. In system headers, we ignore these
23350 declarations so that G++ can work with system headers that are not
23351 C++-safe. */
23352 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
23353 && !type_definition_p
23354 && (type_spec == boolean_type_node
23355 || type_spec == char16_type_node
23356 || type_spec == char32_type_node
23357 || type_spec == wchar_type_node)
23358 && (decl_specs->type
23359 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
23360 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
23361 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
23362 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
23364 decl_specs->redefined_builtin_type = type_spec;
23365 set_and_check_decl_spec_loc (decl_specs,
23366 ds_redefined_builtin_type_spec,
23367 token);
23368 if (!decl_specs->type)
23370 decl_specs->type = type_spec;
23371 decl_specs->type_definition_p = false;
23372 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
23375 else if (decl_specs->type)
23376 decl_specs->multiple_types_p = true;
23377 else
23379 decl_specs->type = type_spec;
23380 decl_specs->type_definition_p = type_definition_p;
23381 decl_specs->redefined_builtin_type = NULL_TREE;
23382 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
23386 /* True iff TOKEN is the GNU keyword __thread. */
23388 static bool
23389 token_is__thread (cp_token *token)
23391 gcc_assert (token->keyword == RID_THREAD);
23392 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
23395 /* Set the location for a declarator specifier and check if it is
23396 duplicated.
23398 DECL_SPECS is the sequence of declarator specifiers onto which to
23399 set the location.
23401 DS is the single declarator specifier to set which location is to
23402 be set onto the existing sequence of declarators.
23404 LOCATION is the location for the declarator specifier to
23405 consider. */
23407 static void
23408 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
23409 cp_decl_spec ds, cp_token *token)
23411 gcc_assert (ds < ds_last);
23413 if (decl_specs == NULL)
23414 return;
23416 source_location location = token->location;
23418 if (decl_specs->locations[ds] == 0)
23420 decl_specs->locations[ds] = location;
23421 if (ds == ds_thread)
23422 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
23424 else
23426 if (ds == ds_long)
23428 if (decl_specs->locations[ds_long_long] != 0)
23429 error_at (location,
23430 "%<long long long%> is too long for GCC");
23431 else
23433 decl_specs->locations[ds_long_long] = location;
23434 pedwarn_cxx98 (location,
23435 OPT_Wlong_long,
23436 "ISO C++ 1998 does not support %<long long%>");
23439 else if (ds == ds_thread)
23441 bool gnu = token_is__thread (token);
23442 if (gnu != decl_specs->gnu_thread_keyword_p)
23443 error_at (location,
23444 "both %<__thread%> and %<thread_local%> specified");
23445 else
23446 error_at (location, "duplicate %qD", token->u.value);
23448 else
23450 static const char *const decl_spec_names[] = {
23451 "signed",
23452 "unsigned",
23453 "short",
23454 "long",
23455 "const",
23456 "volatile",
23457 "restrict",
23458 "inline",
23459 "virtual",
23460 "explicit",
23461 "friend",
23462 "typedef",
23463 "using",
23464 "constexpr",
23465 "__complex"
23467 error_at (location,
23468 "duplicate %qs", decl_spec_names[ds]);
23473 /* Return true iff the declarator specifier DS is present in the
23474 sequence of declarator specifiers DECL_SPECS. */
23476 bool
23477 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
23478 cp_decl_spec ds)
23480 gcc_assert (ds < ds_last);
23482 if (decl_specs == NULL)
23483 return false;
23485 return decl_specs->locations[ds] != 0;
23488 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23489 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
23491 static bool
23492 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
23494 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
23497 /* Issue an error message indicating that TOKEN_DESC was expected.
23498 If KEYWORD is true, it indicated this function is called by
23499 cp_parser_require_keword and the required token can only be
23500 a indicated keyword. */
23502 static void
23503 cp_parser_required_error (cp_parser *parser,
23504 required_token token_desc,
23505 bool keyword)
23507 switch (token_desc)
23509 case RT_NEW:
23510 cp_parser_error (parser, "expected %<new%>");
23511 return;
23512 case RT_DELETE:
23513 cp_parser_error (parser, "expected %<delete%>");
23514 return;
23515 case RT_RETURN:
23516 cp_parser_error (parser, "expected %<return%>");
23517 return;
23518 case RT_WHILE:
23519 cp_parser_error (parser, "expected %<while%>");
23520 return;
23521 case RT_EXTERN:
23522 cp_parser_error (parser, "expected %<extern%>");
23523 return;
23524 case RT_STATIC_ASSERT:
23525 cp_parser_error (parser, "expected %<static_assert%>");
23526 return;
23527 case RT_DECLTYPE:
23528 cp_parser_error (parser, "expected %<decltype%>");
23529 return;
23530 case RT_OPERATOR:
23531 cp_parser_error (parser, "expected %<operator%>");
23532 return;
23533 case RT_CLASS:
23534 cp_parser_error (parser, "expected %<class%>");
23535 return;
23536 case RT_TEMPLATE:
23537 cp_parser_error (parser, "expected %<template%>");
23538 return;
23539 case RT_NAMESPACE:
23540 cp_parser_error (parser, "expected %<namespace%>");
23541 return;
23542 case RT_USING:
23543 cp_parser_error (parser, "expected %<using%>");
23544 return;
23545 case RT_ASM:
23546 cp_parser_error (parser, "expected %<asm%>");
23547 return;
23548 case RT_TRY:
23549 cp_parser_error (parser, "expected %<try%>");
23550 return;
23551 case RT_CATCH:
23552 cp_parser_error (parser, "expected %<catch%>");
23553 return;
23554 case RT_THROW:
23555 cp_parser_error (parser, "expected %<throw%>");
23556 return;
23557 case RT_LABEL:
23558 cp_parser_error (parser, "expected %<__label__%>");
23559 return;
23560 case RT_AT_TRY:
23561 cp_parser_error (parser, "expected %<@try%>");
23562 return;
23563 case RT_AT_SYNCHRONIZED:
23564 cp_parser_error (parser, "expected %<@synchronized%>");
23565 return;
23566 case RT_AT_THROW:
23567 cp_parser_error (parser, "expected %<@throw%>");
23568 return;
23569 case RT_TRANSACTION_ATOMIC:
23570 cp_parser_error (parser, "expected %<__transaction_atomic%>");
23571 return;
23572 case RT_TRANSACTION_RELAXED:
23573 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
23574 return;
23575 default:
23576 break;
23578 if (!keyword)
23580 switch (token_desc)
23582 case RT_SEMICOLON:
23583 cp_parser_error (parser, "expected %<;%>");
23584 return;
23585 case RT_OPEN_PAREN:
23586 cp_parser_error (parser, "expected %<(%>");
23587 return;
23588 case RT_CLOSE_BRACE:
23589 cp_parser_error (parser, "expected %<}%>");
23590 return;
23591 case RT_OPEN_BRACE:
23592 cp_parser_error (parser, "expected %<{%>");
23593 return;
23594 case RT_CLOSE_SQUARE:
23595 cp_parser_error (parser, "expected %<]%>");
23596 return;
23597 case RT_OPEN_SQUARE:
23598 cp_parser_error (parser, "expected %<[%>");
23599 return;
23600 case RT_COMMA:
23601 cp_parser_error (parser, "expected %<,%>");
23602 return;
23603 case RT_SCOPE:
23604 cp_parser_error (parser, "expected %<::%>");
23605 return;
23606 case RT_LESS:
23607 cp_parser_error (parser, "expected %<<%>");
23608 return;
23609 case RT_GREATER:
23610 cp_parser_error (parser, "expected %<>%>");
23611 return;
23612 case RT_EQ:
23613 cp_parser_error (parser, "expected %<=%>");
23614 return;
23615 case RT_ELLIPSIS:
23616 cp_parser_error (parser, "expected %<...%>");
23617 return;
23618 case RT_MULT:
23619 cp_parser_error (parser, "expected %<*%>");
23620 return;
23621 case RT_COMPL:
23622 cp_parser_error (parser, "expected %<~%>");
23623 return;
23624 case RT_COLON:
23625 cp_parser_error (parser, "expected %<:%>");
23626 return;
23627 case RT_COLON_SCOPE:
23628 cp_parser_error (parser, "expected %<:%> or %<::%>");
23629 return;
23630 case RT_CLOSE_PAREN:
23631 cp_parser_error (parser, "expected %<)%>");
23632 return;
23633 case RT_COMMA_CLOSE_PAREN:
23634 cp_parser_error (parser, "expected %<,%> or %<)%>");
23635 return;
23636 case RT_PRAGMA_EOL:
23637 cp_parser_error (parser, "expected end of line");
23638 return;
23639 case RT_NAME:
23640 cp_parser_error (parser, "expected identifier");
23641 return;
23642 case RT_SELECT:
23643 cp_parser_error (parser, "expected selection-statement");
23644 return;
23645 case RT_INTERATION:
23646 cp_parser_error (parser, "expected iteration-statement");
23647 return;
23648 case RT_JUMP:
23649 cp_parser_error (parser, "expected jump-statement");
23650 return;
23651 case RT_CLASS_KEY:
23652 cp_parser_error (parser, "expected class-key");
23653 return;
23654 case RT_CLASS_TYPENAME_TEMPLATE:
23655 cp_parser_error (parser,
23656 "expected %<class%>, %<typename%>, or %<template%>");
23657 return;
23658 default:
23659 gcc_unreachable ();
23662 else
23663 gcc_unreachable ();
23668 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23669 issue an error message indicating that TOKEN_DESC was expected.
23671 Returns the token consumed, if the token had the appropriate type.
23672 Otherwise, returns NULL. */
23674 static cp_token *
23675 cp_parser_require (cp_parser* parser,
23676 enum cpp_ttype type,
23677 required_token token_desc)
23679 if (cp_lexer_next_token_is (parser->lexer, type))
23680 return cp_lexer_consume_token (parser->lexer);
23681 else
23683 /* Output the MESSAGE -- unless we're parsing tentatively. */
23684 if (!cp_parser_simulate_error (parser))
23685 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23686 return NULL;
23690 /* An error message is produced if the next token is not '>'.
23691 All further tokens are skipped until the desired token is
23692 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23694 static void
23695 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23697 /* Current level of '< ... >'. */
23698 unsigned level = 0;
23699 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23700 unsigned nesting_depth = 0;
23702 /* Are we ready, yet? If not, issue error message. */
23703 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23704 return;
23706 /* Skip tokens until the desired token is found. */
23707 while (true)
23709 /* Peek at the next token. */
23710 switch (cp_lexer_peek_token (parser->lexer)->type)
23712 case CPP_LESS:
23713 if (!nesting_depth)
23714 ++level;
23715 break;
23717 case CPP_RSHIFT:
23718 if (cxx_dialect == cxx98)
23719 /* C++0x views the `>>' operator as two `>' tokens, but
23720 C++98 does not. */
23721 break;
23722 else if (!nesting_depth && level-- == 0)
23724 /* We've hit a `>>' where the first `>' closes the
23725 template argument list, and the second `>' is
23726 spurious. Just consume the `>>' and stop; we've
23727 already produced at least one error. */
23728 cp_lexer_consume_token (parser->lexer);
23729 return;
23731 /* Fall through for C++0x, so we handle the second `>' in
23732 the `>>'. */
23734 case CPP_GREATER:
23735 if (!nesting_depth && level-- == 0)
23737 /* We've reached the token we want, consume it and stop. */
23738 cp_lexer_consume_token (parser->lexer);
23739 return;
23741 break;
23743 case CPP_OPEN_PAREN:
23744 case CPP_OPEN_SQUARE:
23745 ++nesting_depth;
23746 break;
23748 case CPP_CLOSE_PAREN:
23749 case CPP_CLOSE_SQUARE:
23750 if (nesting_depth-- == 0)
23751 return;
23752 break;
23754 case CPP_EOF:
23755 case CPP_PRAGMA_EOL:
23756 case CPP_SEMICOLON:
23757 case CPP_OPEN_BRACE:
23758 case CPP_CLOSE_BRACE:
23759 /* The '>' was probably forgotten, don't look further. */
23760 return;
23762 default:
23763 break;
23766 /* Consume this token. */
23767 cp_lexer_consume_token (parser->lexer);
23771 /* If the next token is the indicated keyword, consume it. Otherwise,
23772 issue an error message indicating that TOKEN_DESC was expected.
23774 Returns the token consumed, if the token had the appropriate type.
23775 Otherwise, returns NULL. */
23777 static cp_token *
23778 cp_parser_require_keyword (cp_parser* parser,
23779 enum rid keyword,
23780 required_token token_desc)
23782 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
23784 if (token && token->keyword != keyword)
23786 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
23787 return NULL;
23790 return token;
23793 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23794 function-definition. */
23796 static bool
23797 cp_parser_token_starts_function_definition_p (cp_token* token)
23799 return (/* An ordinary function-body begins with an `{'. */
23800 token->type == CPP_OPEN_BRACE
23801 /* A ctor-initializer begins with a `:'. */
23802 || token->type == CPP_COLON
23803 /* A function-try-block begins with `try'. */
23804 || token->keyword == RID_TRY
23805 /* A function-transaction-block begins with `__transaction_atomic'
23806 or `__transaction_relaxed'. */
23807 || token->keyword == RID_TRANSACTION_ATOMIC
23808 || token->keyword == RID_TRANSACTION_RELAXED
23809 /* The named return value extension begins with `return'. */
23810 || token->keyword == RID_RETURN);
23813 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23814 definition. */
23816 static bool
23817 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
23819 cp_token *token;
23821 token = cp_lexer_peek_token (parser->lexer);
23822 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
23825 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23826 C++0x) ending a template-argument. */
23828 static bool
23829 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
23831 cp_token *token;
23833 token = cp_lexer_peek_token (parser->lexer);
23834 return (token->type == CPP_COMMA
23835 || token->type == CPP_GREATER
23836 || token->type == CPP_ELLIPSIS
23837 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
23840 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23841 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
23843 static bool
23844 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
23845 size_t n)
23847 cp_token *token;
23849 token = cp_lexer_peek_nth_token (parser->lexer, n);
23850 if (token->type == CPP_LESS)
23851 return true;
23852 /* Check for the sequence `<::' in the original code. It would be lexed as
23853 `[:', where `[' is a digraph, and there is no whitespace before
23854 `:'. */
23855 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
23857 cp_token *token2;
23858 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
23859 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
23860 return true;
23862 return false;
23865 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23866 or none_type otherwise. */
23868 static enum tag_types
23869 cp_parser_token_is_class_key (cp_token* token)
23871 switch (token->keyword)
23873 case RID_CLASS:
23874 return class_type;
23875 case RID_STRUCT:
23876 return record_type;
23877 case RID_UNION:
23878 return union_type;
23880 default:
23881 return none_type;
23885 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
23887 static void
23888 cp_parser_check_class_key (enum tag_types class_key, tree type)
23890 if (type == error_mark_node)
23891 return;
23892 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
23894 if (permerror (input_location, "%qs tag used in naming %q#T",
23895 class_key == union_type ? "union"
23896 : class_key == record_type ? "struct" : "class",
23897 type))
23898 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
23899 "%q#T was previously declared here", type);
23903 /* Issue an error message if DECL is redeclared with different
23904 access than its original declaration [class.access.spec/3].
23905 This applies to nested classes and nested class templates.
23906 [class.mem/1]. */
23908 static void
23909 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
23911 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
23912 return;
23914 if ((TREE_PRIVATE (decl)
23915 != (current_access_specifier == access_private_node))
23916 || (TREE_PROTECTED (decl)
23917 != (current_access_specifier == access_protected_node)))
23918 error_at (location, "%qD redeclared with different access", decl);
23921 /* Look for the `template' keyword, as a syntactic disambiguator.
23922 Return TRUE iff it is present, in which case it will be
23923 consumed. */
23925 static bool
23926 cp_parser_optional_template_keyword (cp_parser *parser)
23928 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23930 /* In C++98 the `template' keyword can only be used within templates;
23931 outside templates the parser can always figure out what is a
23932 template and what is not. In C++11, per the resolution of DR 468,
23933 `template' is allowed in cases where it is not strictly necessary. */
23934 if (!processing_template_decl
23935 && pedantic && cxx_dialect == cxx98)
23937 cp_token *token = cp_lexer_peek_token (parser->lexer);
23938 pedwarn (token->location, OPT_Wpedantic,
23939 "in C++98 %<template%> (as a disambiguator) is only "
23940 "allowed within templates");
23941 /* If this part of the token stream is rescanned, the same
23942 error message would be generated. So, we purge the token
23943 from the stream. */
23944 cp_lexer_purge_token (parser->lexer);
23945 return false;
23947 else
23949 /* Consume the `template' keyword. */
23950 cp_lexer_consume_token (parser->lexer);
23951 return true;
23954 return false;
23957 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
23958 set PARSER->SCOPE, and perform other related actions. */
23960 static void
23961 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
23963 int i;
23964 struct tree_check *check_value;
23965 deferred_access_check *chk;
23966 vec<deferred_access_check, va_gc> *checks;
23968 /* Get the stored value. */
23969 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
23970 /* Perform any access checks that were deferred. */
23971 checks = check_value->checks;
23972 if (checks)
23974 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
23975 perform_or_defer_access_check (chk->binfo,
23976 chk->decl,
23977 chk->diag_decl, tf_warning_or_error);
23979 /* Set the scope from the stored value. */
23980 parser->scope = check_value->value;
23981 parser->qualifying_scope = check_value->qualifying_scope;
23982 parser->object_scope = NULL_TREE;
23985 /* Consume tokens up through a non-nested END token. Returns TRUE if we
23986 encounter the end of a block before what we were looking for. */
23988 static bool
23989 cp_parser_cache_group (cp_parser *parser,
23990 enum cpp_ttype end,
23991 unsigned depth)
23993 while (true)
23995 cp_token *token = cp_lexer_peek_token (parser->lexer);
23997 /* Abort a parenthesized expression if we encounter a semicolon. */
23998 if ((end == CPP_CLOSE_PAREN || depth == 0)
23999 && token->type == CPP_SEMICOLON)
24000 return true;
24001 /* If we've reached the end of the file, stop. */
24002 if (token->type == CPP_EOF
24003 || (end != CPP_PRAGMA_EOL
24004 && token->type == CPP_PRAGMA_EOL))
24005 return true;
24006 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24007 /* We've hit the end of an enclosing block, so there's been some
24008 kind of syntax error. */
24009 return true;
24011 /* Consume the token. */
24012 cp_lexer_consume_token (parser->lexer);
24013 /* See if it starts a new group. */
24014 if (token->type == CPP_OPEN_BRACE)
24016 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24017 /* In theory this should probably check end == '}', but
24018 cp_parser_save_member_function_body needs it to exit
24019 after either '}' or ')' when called with ')'. */
24020 if (depth == 0)
24021 return false;
24023 else if (token->type == CPP_OPEN_PAREN)
24025 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24026 if (depth == 0 && end == CPP_CLOSE_PAREN)
24027 return false;
24029 else if (token->type == CPP_PRAGMA)
24030 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24031 else if (token->type == end)
24032 return false;
24036 /* Like above, for caching a default argument or NSDMI. Both of these are
24037 terminated by a non-nested comma, but it can be unclear whether or not a
24038 comma is nested in a template argument list unless we do more parsing.
24039 In order to handle this ambiguity, when we encounter a ',' after a '<'
24040 we try to parse what follows as a parameter-declaration-list (in the
24041 case of a default argument) or a member-declarator (in the case of an
24042 NSDMI). If that succeeds, then we stop caching. */
24044 static tree
24045 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24047 unsigned depth = 0;
24048 int maybe_template_id = 0;
24049 cp_token *first_token;
24050 cp_token *token;
24051 tree default_argument;
24053 /* Add tokens until we have processed the entire default
24054 argument. We add the range [first_token, token). */
24055 first_token = cp_lexer_peek_token (parser->lexer);
24056 if (first_token->type == CPP_OPEN_BRACE)
24058 /* For list-initialization, this is straightforward. */
24059 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24060 token = cp_lexer_peek_token (parser->lexer);
24062 else while (true)
24064 bool done = false;
24066 /* Peek at the next token. */
24067 token = cp_lexer_peek_token (parser->lexer);
24068 /* What we do depends on what token we have. */
24069 switch (token->type)
24071 /* In valid code, a default argument must be
24072 immediately followed by a `,' `)', or `...'. */
24073 case CPP_COMMA:
24074 if (depth == 0 && maybe_template_id)
24076 /* If we've seen a '<', we might be in a
24077 template-argument-list. Until Core issue 325 is
24078 resolved, we don't know how this situation ought
24079 to be handled, so try to DTRT. We check whether
24080 what comes after the comma is a valid parameter
24081 declaration list. If it is, then the comma ends
24082 the default argument; otherwise the default
24083 argument continues. */
24084 bool error = false;
24085 tree t;
24087 /* Set ITALP so cp_parser_parameter_declaration_list
24088 doesn't decide to commit to this parse. */
24089 bool saved_italp = parser->in_template_argument_list_p;
24090 parser->in_template_argument_list_p = true;
24092 cp_parser_parse_tentatively (parser);
24093 cp_lexer_consume_token (parser->lexer);
24095 if (nsdmi)
24097 int ctor_dtor_or_conv_p;
24098 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24099 &ctor_dtor_or_conv_p,
24100 /*parenthesized_p=*/NULL,
24101 /*member_p=*/true);
24103 else
24105 begin_scope (sk_function_parms, NULL_TREE);
24106 cp_parser_parameter_declaration_list (parser, &error);
24107 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
24108 pop_binding (DECL_NAME (t), t);
24109 leave_scope ();
24111 if (!cp_parser_error_occurred (parser) && !error)
24112 done = true;
24113 cp_parser_abort_tentative_parse (parser);
24115 parser->in_template_argument_list_p = saved_italp;
24116 break;
24118 case CPP_CLOSE_PAREN:
24119 case CPP_ELLIPSIS:
24120 /* If we run into a non-nested `;', `}', or `]',
24121 then the code is invalid -- but the default
24122 argument is certainly over. */
24123 case CPP_SEMICOLON:
24124 case CPP_CLOSE_BRACE:
24125 case CPP_CLOSE_SQUARE:
24126 if (depth == 0)
24127 done = true;
24128 /* Update DEPTH, if necessary. */
24129 else if (token->type == CPP_CLOSE_PAREN
24130 || token->type == CPP_CLOSE_BRACE
24131 || token->type == CPP_CLOSE_SQUARE)
24132 --depth;
24133 break;
24135 case CPP_OPEN_PAREN:
24136 case CPP_OPEN_SQUARE:
24137 case CPP_OPEN_BRACE:
24138 ++depth;
24139 break;
24141 case CPP_LESS:
24142 if (depth == 0)
24143 /* This might be the comparison operator, or it might
24144 start a template argument list. */
24145 ++maybe_template_id;
24146 break;
24148 case CPP_RSHIFT:
24149 if (cxx_dialect == cxx98)
24150 break;
24151 /* Fall through for C++0x, which treats the `>>'
24152 operator like two `>' tokens in certain
24153 cases. */
24155 case CPP_GREATER:
24156 if (depth == 0)
24158 /* This might be an operator, or it might close a
24159 template argument list. But if a previous '<'
24160 started a template argument list, this will have
24161 closed it, so we can't be in one anymore. */
24162 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
24163 if (maybe_template_id < 0)
24164 maybe_template_id = 0;
24166 break;
24168 /* If we run out of tokens, issue an error message. */
24169 case CPP_EOF:
24170 case CPP_PRAGMA_EOL:
24171 error_at (token->location, "file ends in default argument");
24172 done = true;
24173 break;
24175 case CPP_NAME:
24176 case CPP_SCOPE:
24177 /* In these cases, we should look for template-ids.
24178 For example, if the default argument is
24179 `X<int, double>()', we need to do name lookup to
24180 figure out whether or not `X' is a template; if
24181 so, the `,' does not end the default argument.
24183 That is not yet done. */
24184 break;
24186 default:
24187 break;
24190 /* If we've reached the end, stop. */
24191 if (done)
24192 break;
24194 /* Add the token to the token block. */
24195 token = cp_lexer_consume_token (parser->lexer);
24198 /* Create a DEFAULT_ARG to represent the unparsed default
24199 argument. */
24200 default_argument = make_node (DEFAULT_ARG);
24201 DEFARG_TOKENS (default_argument)
24202 = cp_token_cache_new (first_token, token);
24203 DEFARG_INSTANTIATIONS (default_argument) = NULL;
24205 return default_argument;
24208 /* Begin parsing tentatively. We always save tokens while parsing
24209 tentatively so that if the tentative parsing fails we can restore the
24210 tokens. */
24212 static void
24213 cp_parser_parse_tentatively (cp_parser* parser)
24215 /* Enter a new parsing context. */
24216 parser->context = cp_parser_context_new (parser->context);
24217 /* Begin saving tokens. */
24218 cp_lexer_save_tokens (parser->lexer);
24219 /* In order to avoid repetitive access control error messages,
24220 access checks are queued up until we are no longer parsing
24221 tentatively. */
24222 push_deferring_access_checks (dk_deferred);
24225 /* Commit to the currently active tentative parse. */
24227 static void
24228 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24230 cp_parser_context *context;
24231 cp_lexer *lexer;
24233 /* Mark all of the levels as committed. */
24234 lexer = parser->lexer;
24235 for (context = parser->context; context->next; context = context->next)
24237 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24238 break;
24239 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24240 while (!cp_lexer_saving_tokens (lexer))
24241 lexer = lexer->next;
24242 cp_lexer_commit_tokens (lexer);
24246 /* Abort the currently active tentative parse. All consumed tokens
24247 will be rolled back, and no diagnostics will be issued. */
24249 static void
24250 cp_parser_abort_tentative_parse (cp_parser* parser)
24252 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
24253 || errorcount > 0);
24254 cp_parser_simulate_error (parser);
24255 /* Now, pretend that we want to see if the construct was
24256 successfully parsed. */
24257 cp_parser_parse_definitely (parser);
24260 /* Stop parsing tentatively. If a parse error has occurred, restore the
24261 token stream. Otherwise, commit to the tokens we have consumed.
24262 Returns true if no error occurred; false otherwise. */
24264 static bool
24265 cp_parser_parse_definitely (cp_parser* parser)
24267 bool error_occurred;
24268 cp_parser_context *context;
24270 /* Remember whether or not an error occurred, since we are about to
24271 destroy that information. */
24272 error_occurred = cp_parser_error_occurred (parser);
24273 /* Remove the topmost context from the stack. */
24274 context = parser->context;
24275 parser->context = context->next;
24276 /* If no parse errors occurred, commit to the tentative parse. */
24277 if (!error_occurred)
24279 /* Commit to the tokens read tentatively, unless that was
24280 already done. */
24281 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
24282 cp_lexer_commit_tokens (parser->lexer);
24284 pop_to_parent_deferring_access_checks ();
24286 /* Otherwise, if errors occurred, roll back our state so that things
24287 are just as they were before we began the tentative parse. */
24288 else
24290 cp_lexer_rollback_tokens (parser->lexer);
24291 pop_deferring_access_checks ();
24293 /* Add the context to the front of the free list. */
24294 context->next = cp_parser_context_free_list;
24295 cp_parser_context_free_list = context;
24297 return !error_occurred;
24300 /* Returns true if we are parsing tentatively and are not committed to
24301 this tentative parse. */
24303 static bool
24304 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
24306 return (cp_parser_parsing_tentatively (parser)
24307 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
24310 /* Returns nonzero iff an error has occurred during the most recent
24311 tentative parse. */
24313 static bool
24314 cp_parser_error_occurred (cp_parser* parser)
24316 return (cp_parser_parsing_tentatively (parser)
24317 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
24320 /* Returns nonzero if GNU extensions are allowed. */
24322 static bool
24323 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
24325 return parser->allow_gnu_extensions_p;
24328 /* Objective-C++ Productions */
24331 /* Parse an Objective-C expression, which feeds into a primary-expression
24332 above.
24334 objc-expression:
24335 objc-message-expression
24336 objc-string-literal
24337 objc-encode-expression
24338 objc-protocol-expression
24339 objc-selector-expression
24341 Returns a tree representation of the expression. */
24343 static tree
24344 cp_parser_objc_expression (cp_parser* parser)
24346 /* Try to figure out what kind of declaration is present. */
24347 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24349 switch (kwd->type)
24351 case CPP_OPEN_SQUARE:
24352 return cp_parser_objc_message_expression (parser);
24354 case CPP_OBJC_STRING:
24355 kwd = cp_lexer_consume_token (parser->lexer);
24356 return objc_build_string_object (kwd->u.value);
24358 case CPP_KEYWORD:
24359 switch (kwd->keyword)
24361 case RID_AT_ENCODE:
24362 return cp_parser_objc_encode_expression (parser);
24364 case RID_AT_PROTOCOL:
24365 return cp_parser_objc_protocol_expression (parser);
24367 case RID_AT_SELECTOR:
24368 return cp_parser_objc_selector_expression (parser);
24370 default:
24371 break;
24373 default:
24374 error_at (kwd->location,
24375 "misplaced %<@%D%> Objective-C++ construct",
24376 kwd->u.value);
24377 cp_parser_skip_to_end_of_block_or_statement (parser);
24380 return error_mark_node;
24383 /* Parse an Objective-C message expression.
24385 objc-message-expression:
24386 [ objc-message-receiver objc-message-args ]
24388 Returns a representation of an Objective-C message. */
24390 static tree
24391 cp_parser_objc_message_expression (cp_parser* parser)
24393 tree receiver, messageargs;
24395 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
24396 receiver = cp_parser_objc_message_receiver (parser);
24397 messageargs = cp_parser_objc_message_args (parser);
24398 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24400 return objc_build_message_expr (receiver, messageargs);
24403 /* Parse an objc-message-receiver.
24405 objc-message-receiver:
24406 expression
24407 simple-type-specifier
24409 Returns a representation of the type or expression. */
24411 static tree
24412 cp_parser_objc_message_receiver (cp_parser* parser)
24414 tree rcv;
24416 /* An Objective-C message receiver may be either (1) a type
24417 or (2) an expression. */
24418 cp_parser_parse_tentatively (parser);
24419 rcv = cp_parser_expression (parser, false, NULL);
24421 if (cp_parser_parse_definitely (parser))
24422 return rcv;
24424 rcv = cp_parser_simple_type_specifier (parser,
24425 /*decl_specs=*/NULL,
24426 CP_PARSER_FLAGS_NONE);
24428 return objc_get_class_reference (rcv);
24431 /* Parse the arguments and selectors comprising an Objective-C message.
24433 objc-message-args:
24434 objc-selector
24435 objc-selector-args
24436 objc-selector-args , objc-comma-args
24438 objc-selector-args:
24439 objc-selector [opt] : assignment-expression
24440 objc-selector-args objc-selector [opt] : assignment-expression
24442 objc-comma-args:
24443 assignment-expression
24444 objc-comma-args , assignment-expression
24446 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
24447 selector arguments and TREE_VALUE containing a list of comma
24448 arguments. */
24450 static tree
24451 cp_parser_objc_message_args (cp_parser* parser)
24453 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
24454 bool maybe_unary_selector_p = true;
24455 cp_token *token = cp_lexer_peek_token (parser->lexer);
24457 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24459 tree selector = NULL_TREE, arg;
24461 if (token->type != CPP_COLON)
24462 selector = cp_parser_objc_selector (parser);
24464 /* Detect if we have a unary selector. */
24465 if (maybe_unary_selector_p
24466 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24467 return build_tree_list (selector, NULL_TREE);
24469 maybe_unary_selector_p = false;
24470 cp_parser_require (parser, CPP_COLON, RT_COLON);
24471 arg = cp_parser_assignment_expression (parser, false, NULL);
24473 sel_args
24474 = chainon (sel_args,
24475 build_tree_list (selector, arg));
24477 token = cp_lexer_peek_token (parser->lexer);
24480 /* Handle non-selector arguments, if any. */
24481 while (token->type == CPP_COMMA)
24483 tree arg;
24485 cp_lexer_consume_token (parser->lexer);
24486 arg = cp_parser_assignment_expression (parser, false, NULL);
24488 addl_args
24489 = chainon (addl_args,
24490 build_tree_list (NULL_TREE, arg));
24492 token = cp_lexer_peek_token (parser->lexer);
24495 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
24497 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
24498 return build_tree_list (error_mark_node, error_mark_node);
24501 return build_tree_list (sel_args, addl_args);
24504 /* Parse an Objective-C encode expression.
24506 objc-encode-expression:
24507 @encode objc-typename
24509 Returns an encoded representation of the type argument. */
24511 static tree
24512 cp_parser_objc_encode_expression (cp_parser* parser)
24514 tree type;
24515 cp_token *token;
24517 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
24518 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24519 token = cp_lexer_peek_token (parser->lexer);
24520 type = complete_type (cp_parser_type_id (parser));
24521 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24523 if (!type)
24525 error_at (token->location,
24526 "%<@encode%> must specify a type as an argument");
24527 return error_mark_node;
24530 /* This happens if we find @encode(T) (where T is a template
24531 typename or something dependent on a template typename) when
24532 parsing a template. In that case, we can't compile it
24533 immediately, but we rather create an AT_ENCODE_EXPR which will
24534 need to be instantiated when the template is used.
24536 if (dependent_type_p (type))
24538 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
24539 TREE_READONLY (value) = 1;
24540 return value;
24543 return objc_build_encode_expr (type);
24546 /* Parse an Objective-C @defs expression. */
24548 static tree
24549 cp_parser_objc_defs_expression (cp_parser *parser)
24551 tree name;
24553 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
24554 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24555 name = cp_parser_identifier (parser);
24556 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24558 return objc_get_class_ivars (name);
24561 /* Parse an Objective-C protocol expression.
24563 objc-protocol-expression:
24564 @protocol ( identifier )
24566 Returns a representation of the protocol expression. */
24568 static tree
24569 cp_parser_objc_protocol_expression (cp_parser* parser)
24571 tree proto;
24573 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24574 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24575 proto = cp_parser_identifier (parser);
24576 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24578 return objc_build_protocol_expr (proto);
24581 /* Parse an Objective-C selector expression.
24583 objc-selector-expression:
24584 @selector ( objc-method-signature )
24586 objc-method-signature:
24587 objc-selector
24588 objc-selector-seq
24590 objc-selector-seq:
24591 objc-selector :
24592 objc-selector-seq objc-selector :
24594 Returns a representation of the method selector. */
24596 static tree
24597 cp_parser_objc_selector_expression (cp_parser* parser)
24599 tree sel_seq = NULL_TREE;
24600 bool maybe_unary_selector_p = true;
24601 cp_token *token;
24602 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24604 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
24605 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24606 token = cp_lexer_peek_token (parser->lexer);
24608 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
24609 || token->type == CPP_SCOPE)
24611 tree selector = NULL_TREE;
24613 if (token->type != CPP_COLON
24614 || token->type == CPP_SCOPE)
24615 selector = cp_parser_objc_selector (parser);
24617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
24618 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
24620 /* Detect if we have a unary selector. */
24621 if (maybe_unary_selector_p)
24623 sel_seq = selector;
24624 goto finish_selector;
24626 else
24628 cp_parser_error (parser, "expected %<:%>");
24631 maybe_unary_selector_p = false;
24632 token = cp_lexer_consume_token (parser->lexer);
24634 if (token->type == CPP_SCOPE)
24636 sel_seq
24637 = chainon (sel_seq,
24638 build_tree_list (selector, NULL_TREE));
24639 sel_seq
24640 = chainon (sel_seq,
24641 build_tree_list (NULL_TREE, NULL_TREE));
24643 else
24644 sel_seq
24645 = chainon (sel_seq,
24646 build_tree_list (selector, NULL_TREE));
24648 token = cp_lexer_peek_token (parser->lexer);
24651 finish_selector:
24652 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24654 return objc_build_selector_expr (loc, sel_seq);
24657 /* Parse a list of identifiers.
24659 objc-identifier-list:
24660 identifier
24661 objc-identifier-list , identifier
24663 Returns a TREE_LIST of identifier nodes. */
24665 static tree
24666 cp_parser_objc_identifier_list (cp_parser* parser)
24668 tree identifier;
24669 tree list;
24670 cp_token *sep;
24672 identifier = cp_parser_identifier (parser);
24673 if (identifier == error_mark_node)
24674 return error_mark_node;
24676 list = build_tree_list (NULL_TREE, identifier);
24677 sep = cp_lexer_peek_token (parser->lexer);
24679 while (sep->type == CPP_COMMA)
24681 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24682 identifier = cp_parser_identifier (parser);
24683 if (identifier == error_mark_node)
24684 return list;
24686 list = chainon (list, build_tree_list (NULL_TREE,
24687 identifier));
24688 sep = cp_lexer_peek_token (parser->lexer);
24691 return list;
24694 /* Parse an Objective-C alias declaration.
24696 objc-alias-declaration:
24697 @compatibility_alias identifier identifier ;
24699 This function registers the alias mapping with the Objective-C front end.
24700 It returns nothing. */
24702 static void
24703 cp_parser_objc_alias_declaration (cp_parser* parser)
24705 tree alias, orig;
24707 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
24708 alias = cp_parser_identifier (parser);
24709 orig = cp_parser_identifier (parser);
24710 objc_declare_alias (alias, orig);
24711 cp_parser_consume_semicolon_at_end_of_statement (parser);
24714 /* Parse an Objective-C class forward-declaration.
24716 objc-class-declaration:
24717 @class objc-identifier-list ;
24719 The function registers the forward declarations with the Objective-C
24720 front end. It returns nothing. */
24722 static void
24723 cp_parser_objc_class_declaration (cp_parser* parser)
24725 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
24726 while (true)
24728 tree id;
24730 id = cp_parser_identifier (parser);
24731 if (id == error_mark_node)
24732 break;
24734 objc_declare_class (id);
24736 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24737 cp_lexer_consume_token (parser->lexer);
24738 else
24739 break;
24741 cp_parser_consume_semicolon_at_end_of_statement (parser);
24744 /* Parse a list of Objective-C protocol references.
24746 objc-protocol-refs-opt:
24747 objc-protocol-refs [opt]
24749 objc-protocol-refs:
24750 < objc-identifier-list >
24752 Returns a TREE_LIST of identifiers, if any. */
24754 static tree
24755 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
24757 tree protorefs = NULL_TREE;
24759 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
24761 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
24762 protorefs = cp_parser_objc_identifier_list (parser);
24763 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
24766 return protorefs;
24769 /* Parse a Objective-C visibility specification. */
24771 static void
24772 cp_parser_objc_visibility_spec (cp_parser* parser)
24774 cp_token *vis = cp_lexer_peek_token (parser->lexer);
24776 switch (vis->keyword)
24778 case RID_AT_PRIVATE:
24779 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
24780 break;
24781 case RID_AT_PROTECTED:
24782 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
24783 break;
24784 case RID_AT_PUBLIC:
24785 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
24786 break;
24787 case RID_AT_PACKAGE:
24788 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
24789 break;
24790 default:
24791 return;
24794 /* Eat '@private'/'@protected'/'@public'. */
24795 cp_lexer_consume_token (parser->lexer);
24798 /* Parse an Objective-C method type. Return 'true' if it is a class
24799 (+) method, and 'false' if it is an instance (-) method. */
24801 static inline bool
24802 cp_parser_objc_method_type (cp_parser* parser)
24804 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
24805 return true;
24806 else
24807 return false;
24810 /* Parse an Objective-C protocol qualifier. */
24812 static tree
24813 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
24815 tree quals = NULL_TREE, node;
24816 cp_token *token = cp_lexer_peek_token (parser->lexer);
24818 node = token->u.value;
24820 while (node && identifier_p (node)
24821 && (node == ridpointers [(int) RID_IN]
24822 || node == ridpointers [(int) RID_OUT]
24823 || node == ridpointers [(int) RID_INOUT]
24824 || node == ridpointers [(int) RID_BYCOPY]
24825 || node == ridpointers [(int) RID_BYREF]
24826 || node == ridpointers [(int) RID_ONEWAY]))
24828 quals = tree_cons (NULL_TREE, node, quals);
24829 cp_lexer_consume_token (parser->lexer);
24830 token = cp_lexer_peek_token (parser->lexer);
24831 node = token->u.value;
24834 return quals;
24837 /* Parse an Objective-C typename. */
24839 static tree
24840 cp_parser_objc_typename (cp_parser* parser)
24842 tree type_name = NULL_TREE;
24844 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24846 tree proto_quals, cp_type = NULL_TREE;
24848 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24849 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
24851 /* An ObjC type name may consist of just protocol qualifiers, in which
24852 case the type shall default to 'id'. */
24853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24855 cp_type = cp_parser_type_id (parser);
24857 /* If the type could not be parsed, an error has already
24858 been produced. For error recovery, behave as if it had
24859 not been specified, which will use the default type
24860 'id'. */
24861 if (cp_type == error_mark_node)
24863 cp_type = NULL_TREE;
24864 /* We need to skip to the closing parenthesis as
24865 cp_parser_type_id() does not seem to do it for
24866 us. */
24867 cp_parser_skip_to_closing_parenthesis (parser,
24868 /*recovering=*/true,
24869 /*or_comma=*/false,
24870 /*consume_paren=*/false);
24874 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24875 type_name = build_tree_list (proto_quals, cp_type);
24878 return type_name;
24881 /* Check to see if TYPE refers to an Objective-C selector name. */
24883 static bool
24884 cp_parser_objc_selector_p (enum cpp_ttype type)
24886 return (type == CPP_NAME || type == CPP_KEYWORD
24887 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
24888 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
24889 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
24890 || type == CPP_XOR || type == CPP_XOR_EQ);
24893 /* Parse an Objective-C selector. */
24895 static tree
24896 cp_parser_objc_selector (cp_parser* parser)
24898 cp_token *token = cp_lexer_consume_token (parser->lexer);
24900 if (!cp_parser_objc_selector_p (token->type))
24902 error_at (token->location, "invalid Objective-C++ selector name");
24903 return error_mark_node;
24906 /* C++ operator names are allowed to appear in ObjC selectors. */
24907 switch (token->type)
24909 case CPP_AND_AND: return get_identifier ("and");
24910 case CPP_AND_EQ: return get_identifier ("and_eq");
24911 case CPP_AND: return get_identifier ("bitand");
24912 case CPP_OR: return get_identifier ("bitor");
24913 case CPP_COMPL: return get_identifier ("compl");
24914 case CPP_NOT: return get_identifier ("not");
24915 case CPP_NOT_EQ: return get_identifier ("not_eq");
24916 case CPP_OR_OR: return get_identifier ("or");
24917 case CPP_OR_EQ: return get_identifier ("or_eq");
24918 case CPP_XOR: return get_identifier ("xor");
24919 case CPP_XOR_EQ: return get_identifier ("xor_eq");
24920 default: return token->u.value;
24924 /* Parse an Objective-C params list. */
24926 static tree
24927 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
24929 tree params = NULL_TREE;
24930 bool maybe_unary_selector_p = true;
24931 cp_token *token = cp_lexer_peek_token (parser->lexer);
24933 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24935 tree selector = NULL_TREE, type_name, identifier;
24936 tree parm_attr = NULL_TREE;
24938 if (token->keyword == RID_ATTRIBUTE)
24939 break;
24941 if (token->type != CPP_COLON)
24942 selector = cp_parser_objc_selector (parser);
24944 /* Detect if we have a unary selector. */
24945 if (maybe_unary_selector_p
24946 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24948 params = selector; /* Might be followed by attributes. */
24949 break;
24952 maybe_unary_selector_p = false;
24953 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24955 /* Something went quite wrong. There should be a colon
24956 here, but there is not. Stop parsing parameters. */
24957 break;
24959 type_name = cp_parser_objc_typename (parser);
24960 /* New ObjC allows attributes on parameters too. */
24961 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
24962 parm_attr = cp_parser_attributes_opt (parser);
24963 identifier = cp_parser_identifier (parser);
24965 params
24966 = chainon (params,
24967 objc_build_keyword_decl (selector,
24968 type_name,
24969 identifier,
24970 parm_attr));
24972 token = cp_lexer_peek_token (parser->lexer);
24975 if (params == NULL_TREE)
24977 cp_parser_error (parser, "objective-c++ method declaration is expected");
24978 return error_mark_node;
24981 /* We allow tail attributes for the method. */
24982 if (token->keyword == RID_ATTRIBUTE)
24984 *attributes = cp_parser_attributes_opt (parser);
24985 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24986 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24987 return params;
24988 cp_parser_error (parser,
24989 "method attributes must be specified at the end");
24990 return error_mark_node;
24993 if (params == NULL_TREE)
24995 cp_parser_error (parser, "objective-c++ method declaration is expected");
24996 return error_mark_node;
24998 return params;
25001 /* Parse the non-keyword Objective-C params. */
25003 static tree
25004 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25005 tree* attributes)
25007 tree params = make_node (TREE_LIST);
25008 cp_token *token = cp_lexer_peek_token (parser->lexer);
25009 *ellipsisp = false; /* Initially, assume no ellipsis. */
25011 while (token->type == CPP_COMMA)
25013 cp_parameter_declarator *parmdecl;
25014 tree parm;
25016 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25017 token = cp_lexer_peek_token (parser->lexer);
25019 if (token->type == CPP_ELLIPSIS)
25021 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25022 *ellipsisp = true;
25023 token = cp_lexer_peek_token (parser->lexer);
25024 break;
25027 /* TODO: parse attributes for tail parameters. */
25028 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25029 parm = grokdeclarator (parmdecl->declarator,
25030 &parmdecl->decl_specifiers,
25031 PARM, /*initialized=*/0,
25032 /*attrlist=*/NULL);
25034 chainon (params, build_tree_list (NULL_TREE, parm));
25035 token = cp_lexer_peek_token (parser->lexer);
25038 /* We allow tail attributes for the method. */
25039 if (token->keyword == RID_ATTRIBUTE)
25041 if (*attributes == NULL_TREE)
25043 *attributes = cp_parser_attributes_opt (parser);
25044 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25045 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25046 return params;
25048 else
25049 /* We have an error, but parse the attributes, so that we can
25050 carry on. */
25051 *attributes = cp_parser_attributes_opt (parser);
25053 cp_parser_error (parser,
25054 "method attributes must be specified at the end");
25055 return error_mark_node;
25058 return params;
25061 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25063 static void
25064 cp_parser_objc_interstitial_code (cp_parser* parser)
25066 cp_token *token = cp_lexer_peek_token (parser->lexer);
25068 /* If the next token is `extern' and the following token is a string
25069 literal, then we have a linkage specification. */
25070 if (token->keyword == RID_EXTERN
25071 && cp_parser_is_pure_string_literal
25072 (cp_lexer_peek_nth_token (parser->lexer, 2)))
25073 cp_parser_linkage_specification (parser);
25074 /* Handle #pragma, if any. */
25075 else if (token->type == CPP_PRAGMA)
25076 cp_parser_pragma (parser, pragma_external);
25077 /* Allow stray semicolons. */
25078 else if (token->type == CPP_SEMICOLON)
25079 cp_lexer_consume_token (parser->lexer);
25080 /* Mark methods as optional or required, when building protocols. */
25081 else if (token->keyword == RID_AT_OPTIONAL)
25083 cp_lexer_consume_token (parser->lexer);
25084 objc_set_method_opt (true);
25086 else if (token->keyword == RID_AT_REQUIRED)
25088 cp_lexer_consume_token (parser->lexer);
25089 objc_set_method_opt (false);
25091 else if (token->keyword == RID_NAMESPACE)
25092 cp_parser_namespace_definition (parser);
25093 /* Other stray characters must generate errors. */
25094 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
25096 cp_lexer_consume_token (parser->lexer);
25097 error ("stray %qs between Objective-C++ methods",
25098 token->type == CPP_OPEN_BRACE ? "{" : "}");
25100 /* Finally, try to parse a block-declaration, or a function-definition. */
25101 else
25102 cp_parser_block_declaration (parser, /*statement_p=*/false);
25105 /* Parse a method signature. */
25107 static tree
25108 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
25110 tree rettype, kwdparms, optparms;
25111 bool ellipsis = false;
25112 bool is_class_method;
25114 is_class_method = cp_parser_objc_method_type (parser);
25115 rettype = cp_parser_objc_typename (parser);
25116 *attributes = NULL_TREE;
25117 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
25118 if (kwdparms == error_mark_node)
25119 return error_mark_node;
25120 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
25121 if (optparms == error_mark_node)
25122 return error_mark_node;
25124 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
25127 static bool
25128 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
25130 tree tattr;
25131 cp_lexer_save_tokens (parser->lexer);
25132 tattr = cp_parser_attributes_opt (parser);
25133 gcc_assert (tattr) ;
25135 /* If the attributes are followed by a method introducer, this is not allowed.
25136 Dump the attributes and flag the situation. */
25137 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
25138 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
25139 return true;
25141 /* Otherwise, the attributes introduce some interstitial code, possibly so
25142 rewind to allow that check. */
25143 cp_lexer_rollback_tokens (parser->lexer);
25144 return false;
25147 /* Parse an Objective-C method prototype list. */
25149 static void
25150 cp_parser_objc_method_prototype_list (cp_parser* parser)
25152 cp_token *token = cp_lexer_peek_token (parser->lexer);
25154 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25156 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25158 tree attributes, sig;
25159 bool is_class_method;
25160 if (token->type == CPP_PLUS)
25161 is_class_method = true;
25162 else
25163 is_class_method = false;
25164 sig = cp_parser_objc_method_signature (parser, &attributes);
25165 if (sig == error_mark_node)
25167 cp_parser_skip_to_end_of_block_or_statement (parser);
25168 token = cp_lexer_peek_token (parser->lexer);
25169 continue;
25171 objc_add_method_declaration (is_class_method, sig, attributes);
25172 cp_parser_consume_semicolon_at_end_of_statement (parser);
25174 else if (token->keyword == RID_AT_PROPERTY)
25175 cp_parser_objc_at_property_declaration (parser);
25176 else if (token->keyword == RID_ATTRIBUTE
25177 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25178 warning_at (cp_lexer_peek_token (parser->lexer)->location,
25179 OPT_Wattributes,
25180 "prefix attributes are ignored for methods");
25181 else
25182 /* Allow for interspersed non-ObjC++ code. */
25183 cp_parser_objc_interstitial_code (parser);
25185 token = cp_lexer_peek_token (parser->lexer);
25188 if (token->type != CPP_EOF)
25189 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25190 else
25191 cp_parser_error (parser, "expected %<@end%>");
25193 objc_finish_interface ();
25196 /* Parse an Objective-C method definition list. */
25198 static void
25199 cp_parser_objc_method_definition_list (cp_parser* parser)
25201 cp_token *token = cp_lexer_peek_token (parser->lexer);
25203 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25205 tree meth;
25207 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25209 cp_token *ptk;
25210 tree sig, attribute;
25211 bool is_class_method;
25212 if (token->type == CPP_PLUS)
25213 is_class_method = true;
25214 else
25215 is_class_method = false;
25216 push_deferring_access_checks (dk_deferred);
25217 sig = cp_parser_objc_method_signature (parser, &attribute);
25218 if (sig == error_mark_node)
25220 cp_parser_skip_to_end_of_block_or_statement (parser);
25221 token = cp_lexer_peek_token (parser->lexer);
25222 continue;
25224 objc_start_method_definition (is_class_method, sig, attribute,
25225 NULL_TREE);
25227 /* For historical reasons, we accept an optional semicolon. */
25228 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25229 cp_lexer_consume_token (parser->lexer);
25231 ptk = cp_lexer_peek_token (parser->lexer);
25232 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25233 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25235 perform_deferred_access_checks (tf_warning_or_error);
25236 stop_deferring_access_checks ();
25237 meth = cp_parser_function_definition_after_declarator (parser,
25238 false);
25239 pop_deferring_access_checks ();
25240 objc_finish_method_definition (meth);
25243 /* The following case will be removed once @synthesize is
25244 completely implemented. */
25245 else if (token->keyword == RID_AT_PROPERTY)
25246 cp_parser_objc_at_property_declaration (parser);
25247 else if (token->keyword == RID_AT_SYNTHESIZE)
25248 cp_parser_objc_at_synthesize_declaration (parser);
25249 else if (token->keyword == RID_AT_DYNAMIC)
25250 cp_parser_objc_at_dynamic_declaration (parser);
25251 else if (token->keyword == RID_ATTRIBUTE
25252 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25253 warning_at (token->location, OPT_Wattributes,
25254 "prefix attributes are ignored for methods");
25255 else
25256 /* Allow for interspersed non-ObjC++ code. */
25257 cp_parser_objc_interstitial_code (parser);
25259 token = cp_lexer_peek_token (parser->lexer);
25262 if (token->type != CPP_EOF)
25263 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25264 else
25265 cp_parser_error (parser, "expected %<@end%>");
25267 objc_finish_implementation ();
25270 /* Parse Objective-C ivars. */
25272 static void
25273 cp_parser_objc_class_ivars (cp_parser* parser)
25275 cp_token *token = cp_lexer_peek_token (parser->lexer);
25277 if (token->type != CPP_OPEN_BRACE)
25278 return; /* No ivars specified. */
25280 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
25281 token = cp_lexer_peek_token (parser->lexer);
25283 while (token->type != CPP_CLOSE_BRACE
25284 && token->keyword != RID_AT_END && token->type != CPP_EOF)
25286 cp_decl_specifier_seq declspecs;
25287 int decl_class_or_enum_p;
25288 tree prefix_attributes;
25290 cp_parser_objc_visibility_spec (parser);
25292 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25293 break;
25295 cp_parser_decl_specifier_seq (parser,
25296 CP_PARSER_FLAGS_OPTIONAL,
25297 &declspecs,
25298 &decl_class_or_enum_p);
25300 /* auto, register, static, extern, mutable. */
25301 if (declspecs.storage_class != sc_none)
25303 cp_parser_error (parser, "invalid type for instance variable");
25304 declspecs.storage_class = sc_none;
25307 /* thread_local. */
25308 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25310 cp_parser_error (parser, "invalid type for instance variable");
25311 declspecs.locations[ds_thread] = 0;
25314 /* typedef. */
25315 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25317 cp_parser_error (parser, "invalid type for instance variable");
25318 declspecs.locations[ds_typedef] = 0;
25321 prefix_attributes = declspecs.attributes;
25322 declspecs.attributes = NULL_TREE;
25324 /* Keep going until we hit the `;' at the end of the
25325 declaration. */
25326 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25328 tree width = NULL_TREE, attributes, first_attribute, decl;
25329 cp_declarator *declarator = NULL;
25330 int ctor_dtor_or_conv_p;
25332 /* Check for a (possibly unnamed) bitfield declaration. */
25333 token = cp_lexer_peek_token (parser->lexer);
25334 if (token->type == CPP_COLON)
25335 goto eat_colon;
25337 if (token->type == CPP_NAME
25338 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25339 == CPP_COLON))
25341 /* Get the name of the bitfield. */
25342 declarator = make_id_declarator (NULL_TREE,
25343 cp_parser_identifier (parser),
25344 sfk_none);
25346 eat_colon:
25347 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25348 /* Get the width of the bitfield. */
25349 width
25350 = cp_parser_constant_expression (parser,
25351 /*allow_non_constant=*/false,
25352 NULL);
25354 else
25356 /* Parse the declarator. */
25357 declarator
25358 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25359 &ctor_dtor_or_conv_p,
25360 /*parenthesized_p=*/NULL,
25361 /*member_p=*/false);
25364 /* Look for attributes that apply to the ivar. */
25365 attributes = cp_parser_attributes_opt (parser);
25366 /* Remember which attributes are prefix attributes and
25367 which are not. */
25368 first_attribute = attributes;
25369 /* Combine the attributes. */
25370 attributes = chainon (prefix_attributes, attributes);
25372 if (width)
25373 /* Create the bitfield declaration. */
25374 decl = grokbitfield (declarator, &declspecs,
25375 width,
25376 attributes);
25377 else
25378 decl = grokfield (declarator, &declspecs,
25379 NULL_TREE, /*init_const_expr_p=*/false,
25380 NULL_TREE, attributes);
25382 /* Add the instance variable. */
25383 if (decl != error_mark_node && decl != NULL_TREE)
25384 objc_add_instance_variable (decl);
25386 /* Reset PREFIX_ATTRIBUTES. */
25387 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25388 attributes = TREE_CHAIN (attributes);
25389 if (attributes)
25390 TREE_CHAIN (attributes) = NULL_TREE;
25392 token = cp_lexer_peek_token (parser->lexer);
25394 if (token->type == CPP_COMMA)
25396 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25397 continue;
25399 break;
25402 cp_parser_consume_semicolon_at_end_of_statement (parser);
25403 token = cp_lexer_peek_token (parser->lexer);
25406 if (token->keyword == RID_AT_END)
25407 cp_parser_error (parser, "expected %<}%>");
25409 /* Do not consume the RID_AT_END, so it will be read again as terminating
25410 the @interface of @implementation. */
25411 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
25412 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
25414 /* For historical reasons, we accept an optional semicolon. */
25415 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25416 cp_lexer_consume_token (parser->lexer);
25419 /* Parse an Objective-C protocol declaration. */
25421 static void
25422 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
25424 tree proto, protorefs;
25425 cp_token *tok;
25427 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25428 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
25430 tok = cp_lexer_peek_token (parser->lexer);
25431 error_at (tok->location, "identifier expected after %<@protocol%>");
25432 cp_parser_consume_semicolon_at_end_of_statement (parser);
25433 return;
25436 /* See if we have a forward declaration or a definition. */
25437 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
25439 /* Try a forward declaration first. */
25440 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
25442 while (true)
25444 tree id;
25446 id = cp_parser_identifier (parser);
25447 if (id == error_mark_node)
25448 break;
25450 objc_declare_protocol (id, attributes);
25452 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25453 cp_lexer_consume_token (parser->lexer);
25454 else
25455 break;
25457 cp_parser_consume_semicolon_at_end_of_statement (parser);
25460 /* Ok, we got a full-fledged definition (or at least should). */
25461 else
25463 proto = cp_parser_identifier (parser);
25464 protorefs = cp_parser_objc_protocol_refs_opt (parser);
25465 objc_start_protocol (proto, protorefs, attributes);
25466 cp_parser_objc_method_prototype_list (parser);
25470 /* Parse an Objective-C superclass or category. */
25472 static void
25473 cp_parser_objc_superclass_or_category (cp_parser *parser,
25474 bool iface_p,
25475 tree *super,
25476 tree *categ, bool *is_class_extension)
25478 cp_token *next = cp_lexer_peek_token (parser->lexer);
25480 *super = *categ = NULL_TREE;
25481 *is_class_extension = false;
25482 if (next->type == CPP_COLON)
25484 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25485 *super = cp_parser_identifier (parser);
25487 else if (next->type == CPP_OPEN_PAREN)
25489 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25491 /* If there is no category name, and this is an @interface, we
25492 have a class extension. */
25493 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25495 *categ = NULL_TREE;
25496 *is_class_extension = true;
25498 else
25499 *categ = cp_parser_identifier (parser);
25501 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25505 /* Parse an Objective-C class interface. */
25507 static void
25508 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
25510 tree name, super, categ, protos;
25511 bool is_class_extension;
25513 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
25514 name = cp_parser_identifier (parser);
25515 if (name == error_mark_node)
25517 /* It's hard to recover because even if valid @interface stuff
25518 is to follow, we can't compile it (or validate it) if we
25519 don't even know which class it refers to. Let's assume this
25520 was a stray '@interface' token in the stream and skip it.
25522 return;
25524 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
25525 &is_class_extension);
25526 protos = cp_parser_objc_protocol_refs_opt (parser);
25528 /* We have either a class or a category on our hands. */
25529 if (categ || is_class_extension)
25530 objc_start_category_interface (name, categ, protos, attributes);
25531 else
25533 objc_start_class_interface (name, super, protos, attributes);
25534 /* Handle instance variable declarations, if any. */
25535 cp_parser_objc_class_ivars (parser);
25536 objc_continue_interface ();
25539 cp_parser_objc_method_prototype_list (parser);
25542 /* Parse an Objective-C class implementation. */
25544 static void
25545 cp_parser_objc_class_implementation (cp_parser* parser)
25547 tree name, super, categ;
25548 bool is_class_extension;
25550 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
25551 name = cp_parser_identifier (parser);
25552 if (name == error_mark_node)
25554 /* It's hard to recover because even if valid @implementation
25555 stuff is to follow, we can't compile it (or validate it) if
25556 we don't even know which class it refers to. Let's assume
25557 this was a stray '@implementation' token in the stream and
25558 skip it.
25560 return;
25562 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
25563 &is_class_extension);
25565 /* We have either a class or a category on our hands. */
25566 if (categ)
25567 objc_start_category_implementation (name, categ);
25568 else
25570 objc_start_class_implementation (name, super);
25571 /* Handle instance variable declarations, if any. */
25572 cp_parser_objc_class_ivars (parser);
25573 objc_continue_implementation ();
25576 cp_parser_objc_method_definition_list (parser);
25579 /* Consume the @end token and finish off the implementation. */
25581 static void
25582 cp_parser_objc_end_implementation (cp_parser* parser)
25584 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25585 objc_finish_implementation ();
25588 /* Parse an Objective-C declaration. */
25590 static void
25591 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
25593 /* Try to figure out what kind of declaration is present. */
25594 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25596 if (attributes)
25597 switch (kwd->keyword)
25599 case RID_AT_ALIAS:
25600 case RID_AT_CLASS:
25601 case RID_AT_END:
25602 error_at (kwd->location, "attributes may not be specified before"
25603 " the %<@%D%> Objective-C++ keyword",
25604 kwd->u.value);
25605 attributes = NULL;
25606 break;
25607 case RID_AT_IMPLEMENTATION:
25608 warning_at (kwd->location, OPT_Wattributes,
25609 "prefix attributes are ignored before %<@%D%>",
25610 kwd->u.value);
25611 attributes = NULL;
25612 default:
25613 break;
25616 switch (kwd->keyword)
25618 case RID_AT_ALIAS:
25619 cp_parser_objc_alias_declaration (parser);
25620 break;
25621 case RID_AT_CLASS:
25622 cp_parser_objc_class_declaration (parser);
25623 break;
25624 case RID_AT_PROTOCOL:
25625 cp_parser_objc_protocol_declaration (parser, attributes);
25626 break;
25627 case RID_AT_INTERFACE:
25628 cp_parser_objc_class_interface (parser, attributes);
25629 break;
25630 case RID_AT_IMPLEMENTATION:
25631 cp_parser_objc_class_implementation (parser);
25632 break;
25633 case RID_AT_END:
25634 cp_parser_objc_end_implementation (parser);
25635 break;
25636 default:
25637 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25638 kwd->u.value);
25639 cp_parser_skip_to_end_of_block_or_statement (parser);
25643 /* Parse an Objective-C try-catch-finally statement.
25645 objc-try-catch-finally-stmt:
25646 @try compound-statement objc-catch-clause-seq [opt]
25647 objc-finally-clause [opt]
25649 objc-catch-clause-seq:
25650 objc-catch-clause objc-catch-clause-seq [opt]
25652 objc-catch-clause:
25653 @catch ( objc-exception-declaration ) compound-statement
25655 objc-finally-clause:
25656 @finally compound-statement
25658 objc-exception-declaration:
25659 parameter-declaration
25660 '...'
25662 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25664 Returns NULL_TREE.
25666 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25667 for C. Keep them in sync. */
25669 static tree
25670 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
25672 location_t location;
25673 tree stmt;
25675 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25676 location = cp_lexer_peek_token (parser->lexer)->location;
25677 objc_maybe_warn_exceptions (location);
25678 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25679 node, lest it get absorbed into the surrounding block. */
25680 stmt = push_stmt_list ();
25681 cp_parser_compound_statement (parser, NULL, false, false);
25682 objc_begin_try_stmt (location, pop_stmt_list (stmt));
25684 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25686 cp_parameter_declarator *parm;
25687 tree parameter_declaration = error_mark_node;
25688 bool seen_open_paren = false;
25690 cp_lexer_consume_token (parser->lexer);
25691 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25692 seen_open_paren = true;
25693 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25695 /* We have "@catch (...)" (where the '...' are literally
25696 what is in the code). Skip the '...'.
25697 parameter_declaration is set to NULL_TREE, and
25698 objc_being_catch_clauses() knows that that means
25699 '...'. */
25700 cp_lexer_consume_token (parser->lexer);
25701 parameter_declaration = NULL_TREE;
25703 else
25705 /* We have "@catch (NSException *exception)" or something
25706 like that. Parse the parameter declaration. */
25707 parm = cp_parser_parameter_declaration (parser, false, NULL);
25708 if (parm == NULL)
25709 parameter_declaration = error_mark_node;
25710 else
25711 parameter_declaration = grokdeclarator (parm->declarator,
25712 &parm->decl_specifiers,
25713 PARM, /*initialized=*/0,
25714 /*attrlist=*/NULL);
25716 if (seen_open_paren)
25717 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25718 else
25720 /* If there was no open parenthesis, we are recovering from
25721 an error, and we are trying to figure out what mistake
25722 the user has made. */
25724 /* If there is an immediate closing parenthesis, the user
25725 probably forgot the opening one (ie, they typed "@catch
25726 NSException *e)". Parse the closing parenthesis and keep
25727 going. */
25728 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25729 cp_lexer_consume_token (parser->lexer);
25731 /* If these is no immediate closing parenthesis, the user
25732 probably doesn't know that parenthesis are required at
25733 all (ie, they typed "@catch NSException *e"). So, just
25734 forget about the closing parenthesis and keep going. */
25736 objc_begin_catch_clause (parameter_declaration);
25737 cp_parser_compound_statement (parser, NULL, false, false);
25738 objc_finish_catch_clause ();
25740 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
25742 cp_lexer_consume_token (parser->lexer);
25743 location = cp_lexer_peek_token (parser->lexer)->location;
25744 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25745 node, lest it get absorbed into the surrounding block. */
25746 stmt = push_stmt_list ();
25747 cp_parser_compound_statement (parser, NULL, false, false);
25748 objc_build_finally_clause (location, pop_stmt_list (stmt));
25751 return objc_finish_try_stmt ();
25754 /* Parse an Objective-C synchronized statement.
25756 objc-synchronized-stmt:
25757 @synchronized ( expression ) compound-statement
25759 Returns NULL_TREE. */
25761 static tree
25762 cp_parser_objc_synchronized_statement (cp_parser *parser)
25764 location_t location;
25765 tree lock, stmt;
25767 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
25769 location = cp_lexer_peek_token (parser->lexer)->location;
25770 objc_maybe_warn_exceptions (location);
25771 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25772 lock = cp_parser_expression (parser, false, NULL);
25773 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25775 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25776 node, lest it get absorbed into the surrounding block. */
25777 stmt = push_stmt_list ();
25778 cp_parser_compound_statement (parser, NULL, false, false);
25780 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
25783 /* Parse an Objective-C throw statement.
25785 objc-throw-stmt:
25786 @throw assignment-expression [opt] ;
25788 Returns a constructed '@throw' statement. */
25790 static tree
25791 cp_parser_objc_throw_statement (cp_parser *parser)
25793 tree expr = NULL_TREE;
25794 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25796 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
25798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25799 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25801 cp_parser_consume_semicolon_at_end_of_statement (parser);
25803 return objc_build_throw_stmt (loc, expr);
25806 /* Parse an Objective-C statement. */
25808 static tree
25809 cp_parser_objc_statement (cp_parser * parser)
25811 /* Try to figure out what kind of declaration is present. */
25812 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25814 switch (kwd->keyword)
25816 case RID_AT_TRY:
25817 return cp_parser_objc_try_catch_finally_statement (parser);
25818 case RID_AT_SYNCHRONIZED:
25819 return cp_parser_objc_synchronized_statement (parser);
25820 case RID_AT_THROW:
25821 return cp_parser_objc_throw_statement (parser);
25822 default:
25823 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25824 kwd->u.value);
25825 cp_parser_skip_to_end_of_block_or_statement (parser);
25828 return error_mark_node;
25831 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
25832 look ahead to see if an objc keyword follows the attributes. This
25833 is to detect the use of prefix attributes on ObjC @interface and
25834 @protocol. */
25836 static bool
25837 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
25839 cp_lexer_save_tokens (parser->lexer);
25840 *attrib = cp_parser_attributes_opt (parser);
25841 gcc_assert (*attrib);
25842 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
25844 cp_lexer_commit_tokens (parser->lexer);
25845 return true;
25847 cp_lexer_rollback_tokens (parser->lexer);
25848 return false;
25851 /* This routine is a minimal replacement for
25852 c_parser_struct_declaration () used when parsing the list of
25853 types/names or ObjC++ properties. For example, when parsing the
25854 code
25856 @property (readonly) int a, b, c;
25858 this function is responsible for parsing "int a, int b, int c" and
25859 returning the declarations as CHAIN of DECLs.
25861 TODO: Share this code with cp_parser_objc_class_ivars. It's very
25862 similar parsing. */
25863 static tree
25864 cp_parser_objc_struct_declaration (cp_parser *parser)
25866 tree decls = NULL_TREE;
25867 cp_decl_specifier_seq declspecs;
25868 int decl_class_or_enum_p;
25869 tree prefix_attributes;
25871 cp_parser_decl_specifier_seq (parser,
25872 CP_PARSER_FLAGS_NONE,
25873 &declspecs,
25874 &decl_class_or_enum_p);
25876 if (declspecs.type == error_mark_node)
25877 return error_mark_node;
25879 /* auto, register, static, extern, mutable. */
25880 if (declspecs.storage_class != sc_none)
25882 cp_parser_error (parser, "invalid type for property");
25883 declspecs.storage_class = sc_none;
25886 /* thread_local. */
25887 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25889 cp_parser_error (parser, "invalid type for property");
25890 declspecs.locations[ds_thread] = 0;
25893 /* typedef. */
25894 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25896 cp_parser_error (parser, "invalid type for property");
25897 declspecs.locations[ds_typedef] = 0;
25900 prefix_attributes = declspecs.attributes;
25901 declspecs.attributes = NULL_TREE;
25903 /* Keep going until we hit the `;' at the end of the declaration. */
25904 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25906 tree attributes, first_attribute, decl;
25907 cp_declarator *declarator;
25908 cp_token *token;
25910 /* Parse the declarator. */
25911 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25912 NULL, NULL, false);
25914 /* Look for attributes that apply to the ivar. */
25915 attributes = cp_parser_attributes_opt (parser);
25916 /* Remember which attributes are prefix attributes and
25917 which are not. */
25918 first_attribute = attributes;
25919 /* Combine the attributes. */
25920 attributes = chainon (prefix_attributes, attributes);
25922 decl = grokfield (declarator, &declspecs,
25923 NULL_TREE, /*init_const_expr_p=*/false,
25924 NULL_TREE, attributes);
25926 if (decl == error_mark_node || decl == NULL_TREE)
25927 return error_mark_node;
25929 /* Reset PREFIX_ATTRIBUTES. */
25930 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25931 attributes = TREE_CHAIN (attributes);
25932 if (attributes)
25933 TREE_CHAIN (attributes) = NULL_TREE;
25935 DECL_CHAIN (decl) = decls;
25936 decls = decl;
25938 token = cp_lexer_peek_token (parser->lexer);
25939 if (token->type == CPP_COMMA)
25941 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25942 continue;
25944 else
25945 break;
25947 return decls;
25950 /* Parse an Objective-C @property declaration. The syntax is:
25952 objc-property-declaration:
25953 '@property' objc-property-attributes[opt] struct-declaration ;
25955 objc-property-attributes:
25956 '(' objc-property-attribute-list ')'
25958 objc-property-attribute-list:
25959 objc-property-attribute
25960 objc-property-attribute-list, objc-property-attribute
25962 objc-property-attribute
25963 'getter' = identifier
25964 'setter' = identifier
25965 'readonly'
25966 'readwrite'
25967 'assign'
25968 'retain'
25969 'copy'
25970 'nonatomic'
25972 For example:
25973 @property NSString *name;
25974 @property (readonly) id object;
25975 @property (retain, nonatomic, getter=getTheName) id name;
25976 @property int a, b, c;
25978 PS: This function is identical to
25979 c_parser_objc_at_property_declaration for C. Keep them in sync. */
25980 static void
25981 cp_parser_objc_at_property_declaration (cp_parser *parser)
25983 /* The following variables hold the attributes of the properties as
25984 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
25985 seen. When we see an attribute, we set them to 'true' (if they
25986 are boolean properties) or to the identifier (if they have an
25987 argument, ie, for getter and setter). Note that here we only
25988 parse the list of attributes, check the syntax and accumulate the
25989 attributes that we find. objc_add_property_declaration() will
25990 then process the information. */
25991 bool property_assign = false;
25992 bool property_copy = false;
25993 tree property_getter_ident = NULL_TREE;
25994 bool property_nonatomic = false;
25995 bool property_readonly = false;
25996 bool property_readwrite = false;
25997 bool property_retain = false;
25998 tree property_setter_ident = NULL_TREE;
26000 /* 'properties' is the list of properties that we read. Usually a
26001 single one, but maybe more (eg, in "@property int a, b, c;" there
26002 are three). */
26003 tree properties;
26004 location_t loc;
26006 loc = cp_lexer_peek_token (parser->lexer)->location;
26008 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26010 /* Parse the optional attribute list... */
26011 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26013 /* Eat the '('. */
26014 cp_lexer_consume_token (parser->lexer);
26016 while (true)
26018 bool syntax_error = false;
26019 cp_token *token = cp_lexer_peek_token (parser->lexer);
26020 enum rid keyword;
26022 if (token->type != CPP_NAME)
26024 cp_parser_error (parser, "expected identifier");
26025 break;
26027 keyword = C_RID_CODE (token->u.value);
26028 cp_lexer_consume_token (parser->lexer);
26029 switch (keyword)
26031 case RID_ASSIGN: property_assign = true; break;
26032 case RID_COPY: property_copy = true; break;
26033 case RID_NONATOMIC: property_nonatomic = true; break;
26034 case RID_READONLY: property_readonly = true; break;
26035 case RID_READWRITE: property_readwrite = true; break;
26036 case RID_RETAIN: property_retain = true; break;
26038 case RID_GETTER:
26039 case RID_SETTER:
26040 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26042 if (keyword == RID_GETTER)
26043 cp_parser_error (parser,
26044 "missing %<=%> (after %<getter%> attribute)");
26045 else
26046 cp_parser_error (parser,
26047 "missing %<=%> (after %<setter%> attribute)");
26048 syntax_error = true;
26049 break;
26051 cp_lexer_consume_token (parser->lexer); /* eat the = */
26052 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
26054 cp_parser_error (parser, "expected identifier");
26055 syntax_error = true;
26056 break;
26058 if (keyword == RID_SETTER)
26060 if (property_setter_ident != NULL_TREE)
26062 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
26063 cp_lexer_consume_token (parser->lexer);
26065 else
26066 property_setter_ident = cp_parser_objc_selector (parser);
26067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26068 cp_parser_error (parser, "setter name must terminate with %<:%>");
26069 else
26070 cp_lexer_consume_token (parser->lexer);
26072 else
26074 if (property_getter_ident != NULL_TREE)
26076 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
26077 cp_lexer_consume_token (parser->lexer);
26079 else
26080 property_getter_ident = cp_parser_objc_selector (parser);
26082 break;
26083 default:
26084 cp_parser_error (parser, "unknown property attribute");
26085 syntax_error = true;
26086 break;
26089 if (syntax_error)
26090 break;
26092 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26093 cp_lexer_consume_token (parser->lexer);
26094 else
26095 break;
26098 /* FIXME: "@property (setter, assign);" will generate a spurious
26099 "error: expected ‘)’ before ‘,’ token". This is because
26100 cp_parser_require, unlike the C counterpart, will produce an
26101 error even if we are in error recovery. */
26102 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26104 cp_parser_skip_to_closing_parenthesis (parser,
26105 /*recovering=*/true,
26106 /*or_comma=*/false,
26107 /*consume_paren=*/true);
26111 /* ... and the property declaration(s). */
26112 properties = cp_parser_objc_struct_declaration (parser);
26114 if (properties == error_mark_node)
26116 cp_parser_skip_to_end_of_statement (parser);
26117 /* If the next token is now a `;', consume it. */
26118 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26119 cp_lexer_consume_token (parser->lexer);
26120 return;
26123 if (properties == NULL_TREE)
26124 cp_parser_error (parser, "expected identifier");
26125 else
26127 /* Comma-separated properties are chained together in
26128 reverse order; add them one by one. */
26129 properties = nreverse (properties);
26131 for (; properties; properties = TREE_CHAIN (properties))
26132 objc_add_property_declaration (loc, copy_node (properties),
26133 property_readonly, property_readwrite,
26134 property_assign, property_retain,
26135 property_copy, property_nonatomic,
26136 property_getter_ident, property_setter_ident);
26139 cp_parser_consume_semicolon_at_end_of_statement (parser);
26142 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26144 objc-synthesize-declaration:
26145 @synthesize objc-synthesize-identifier-list ;
26147 objc-synthesize-identifier-list:
26148 objc-synthesize-identifier
26149 objc-synthesize-identifier-list, objc-synthesize-identifier
26151 objc-synthesize-identifier
26152 identifier
26153 identifier = identifier
26155 For example:
26156 @synthesize MyProperty;
26157 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26159 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26160 for C. Keep them in sync.
26162 static void
26163 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
26165 tree list = NULL_TREE;
26166 location_t loc;
26167 loc = cp_lexer_peek_token (parser->lexer)->location;
26169 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
26170 while (true)
26172 tree property, ivar;
26173 property = cp_parser_identifier (parser);
26174 if (property == error_mark_node)
26176 cp_parser_consume_semicolon_at_end_of_statement (parser);
26177 return;
26179 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26181 cp_lexer_consume_token (parser->lexer);
26182 ivar = cp_parser_identifier (parser);
26183 if (ivar == error_mark_node)
26185 cp_parser_consume_semicolon_at_end_of_statement (parser);
26186 return;
26189 else
26190 ivar = NULL_TREE;
26191 list = chainon (list, build_tree_list (ivar, property));
26192 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26193 cp_lexer_consume_token (parser->lexer);
26194 else
26195 break;
26197 cp_parser_consume_semicolon_at_end_of_statement (parser);
26198 objc_add_synthesize_declaration (loc, list);
26201 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26203 objc-dynamic-declaration:
26204 @dynamic identifier-list ;
26206 For example:
26207 @dynamic MyProperty;
26208 @dynamic MyProperty, AnotherProperty;
26210 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26211 for C. Keep them in sync.
26213 static void
26214 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
26216 tree list = NULL_TREE;
26217 location_t loc;
26218 loc = cp_lexer_peek_token (parser->lexer)->location;
26220 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
26221 while (true)
26223 tree property;
26224 property = cp_parser_identifier (parser);
26225 if (property == error_mark_node)
26227 cp_parser_consume_semicolon_at_end_of_statement (parser);
26228 return;
26230 list = chainon (list, build_tree_list (NULL, property));
26231 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26232 cp_lexer_consume_token (parser->lexer);
26233 else
26234 break;
26236 cp_parser_consume_semicolon_at_end_of_statement (parser);
26237 objc_add_dynamic_declaration (loc, list);
26241 /* OpenMP 2.5 parsing routines. */
26243 /* Returns name of the next clause.
26244 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26245 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26246 returned and the token is consumed. */
26248 static pragma_omp_clause
26249 cp_parser_omp_clause_name (cp_parser *parser)
26251 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26253 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26254 result = PRAGMA_OMP_CLAUSE_IF;
26255 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26256 result = PRAGMA_OMP_CLAUSE_DEFAULT;
26257 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26258 result = PRAGMA_OMP_CLAUSE_PRIVATE;
26259 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26261 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26262 const char *p = IDENTIFIER_POINTER (id);
26264 switch (p[0])
26266 case 'c':
26267 if (!strcmp ("collapse", p))
26268 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
26269 else if (!strcmp ("copyin", p))
26270 result = PRAGMA_OMP_CLAUSE_COPYIN;
26271 else if (!strcmp ("copyprivate", p))
26272 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
26273 break;
26274 case 'f':
26275 if (!strcmp ("final", p))
26276 result = PRAGMA_OMP_CLAUSE_FINAL;
26277 else if (!strcmp ("firstprivate", p))
26278 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
26279 break;
26280 case 'l':
26281 if (!strcmp ("lastprivate", p))
26282 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
26283 break;
26284 case 'm':
26285 if (!strcmp ("mergeable", p))
26286 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
26287 break;
26288 case 'n':
26289 if (!strcmp ("nowait", p))
26290 result = PRAGMA_OMP_CLAUSE_NOWAIT;
26291 else if (!strcmp ("num_threads", p))
26292 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
26293 break;
26294 case 'o':
26295 if (!strcmp ("ordered", p))
26296 result = PRAGMA_OMP_CLAUSE_ORDERED;
26297 break;
26298 case 'r':
26299 if (!strcmp ("reduction", p))
26300 result = PRAGMA_OMP_CLAUSE_REDUCTION;
26301 break;
26302 case 's':
26303 if (!strcmp ("schedule", p))
26304 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
26305 else if (!strcmp ("shared", p))
26306 result = PRAGMA_OMP_CLAUSE_SHARED;
26307 break;
26308 case 'u':
26309 if (!strcmp ("untied", p))
26310 result = PRAGMA_OMP_CLAUSE_UNTIED;
26311 break;
26315 if (result != PRAGMA_OMP_CLAUSE_NONE)
26316 cp_lexer_consume_token (parser->lexer);
26318 return result;
26321 /* Validate that a clause of the given type does not already exist. */
26323 static void
26324 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
26325 const char *name, location_t location)
26327 tree c;
26329 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26330 if (OMP_CLAUSE_CODE (c) == code)
26332 error_at (location, "too many %qs clauses", name);
26333 break;
26337 /* OpenMP 2.5:
26338 variable-list:
26339 identifier
26340 variable-list , identifier
26342 In addition, we match a closing parenthesis. An opening parenthesis
26343 will have been consumed by the caller.
26345 If KIND is nonzero, create the appropriate node and install the decl
26346 in OMP_CLAUSE_DECL and add the node to the head of the list.
26348 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
26349 return the list created. */
26351 static tree
26352 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
26353 tree list)
26355 cp_token *token;
26356 while (1)
26358 tree name, decl;
26360 token = cp_lexer_peek_token (parser->lexer);
26361 name = cp_parser_id_expression (parser, /*template_p=*/false,
26362 /*check_dependency_p=*/true,
26363 /*template_p=*/NULL,
26364 /*declarator_p=*/false,
26365 /*optional_p=*/false);
26366 if (name == error_mark_node)
26367 goto skip_comma;
26369 decl = cp_parser_lookup_name_simple (parser, name, token->location);
26370 if (decl == error_mark_node)
26371 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
26372 token->location);
26373 else if (kind != 0)
26375 tree u = build_omp_clause (token->location, kind);
26376 OMP_CLAUSE_DECL (u) = decl;
26377 OMP_CLAUSE_CHAIN (u) = list;
26378 list = u;
26380 else
26381 list = tree_cons (decl, NULL_TREE, list);
26383 get_comma:
26384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26385 break;
26386 cp_lexer_consume_token (parser->lexer);
26389 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26391 int ending;
26393 /* Try to resync to an unnested comma. Copied from
26394 cp_parser_parenthesized_expression_list. */
26395 skip_comma:
26396 ending = cp_parser_skip_to_closing_parenthesis (parser,
26397 /*recovering=*/true,
26398 /*or_comma=*/true,
26399 /*consume_paren=*/true);
26400 if (ending < 0)
26401 goto get_comma;
26404 return list;
26407 /* Similarly, but expect leading and trailing parenthesis. This is a very
26408 common case for omp clauses. */
26410 static tree
26411 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
26413 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26414 return cp_parser_omp_var_list_no_open (parser, kind, list);
26415 return list;
26418 /* OpenMP 3.0:
26419 collapse ( constant-expression ) */
26421 static tree
26422 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
26424 tree c, num;
26425 location_t loc;
26426 HOST_WIDE_INT n;
26428 loc = cp_lexer_peek_token (parser->lexer)->location;
26429 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26430 return list;
26432 num = cp_parser_constant_expression (parser, false, NULL);
26434 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26435 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26436 /*or_comma=*/false,
26437 /*consume_paren=*/true);
26439 if (num == error_mark_node)
26440 return list;
26441 num = fold_non_dependent_expr (num);
26442 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
26443 || !host_integerp (num, 0)
26444 || (n = tree_low_cst (num, 0)) <= 0
26445 || (int) n != n)
26447 error_at (loc, "collapse argument needs positive constant integer expression");
26448 return list;
26451 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
26452 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
26453 OMP_CLAUSE_CHAIN (c) = list;
26454 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
26456 return c;
26459 /* OpenMP 2.5:
26460 default ( shared | none ) */
26462 static tree
26463 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
26465 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
26466 tree c;
26468 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26469 return list;
26470 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26472 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26473 const char *p = IDENTIFIER_POINTER (id);
26475 switch (p[0])
26477 case 'n':
26478 if (strcmp ("none", p) != 0)
26479 goto invalid_kind;
26480 kind = OMP_CLAUSE_DEFAULT_NONE;
26481 break;
26483 case 's':
26484 if (strcmp ("shared", p) != 0)
26485 goto invalid_kind;
26486 kind = OMP_CLAUSE_DEFAULT_SHARED;
26487 break;
26489 default:
26490 goto invalid_kind;
26493 cp_lexer_consume_token (parser->lexer);
26495 else
26497 invalid_kind:
26498 cp_parser_error (parser, "expected %<none%> or %<shared%>");
26501 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26502 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26503 /*or_comma=*/false,
26504 /*consume_paren=*/true);
26506 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
26507 return list;
26509 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
26510 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
26511 OMP_CLAUSE_CHAIN (c) = list;
26512 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
26514 return c;
26517 /* OpenMP 3.1:
26518 final ( expression ) */
26520 static tree
26521 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
26523 tree t, c;
26525 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26526 return list;
26528 t = cp_parser_condition (parser);
26530 if (t == error_mark_node
26531 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26532 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26533 /*or_comma=*/false,
26534 /*consume_paren=*/true);
26536 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
26538 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
26539 OMP_CLAUSE_FINAL_EXPR (c) = t;
26540 OMP_CLAUSE_CHAIN (c) = list;
26542 return c;
26545 /* OpenMP 2.5:
26546 if ( expression ) */
26548 static tree
26549 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
26551 tree t, c;
26553 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26554 return list;
26556 t = cp_parser_condition (parser);
26558 if (t == error_mark_node
26559 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26560 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26561 /*or_comma=*/false,
26562 /*consume_paren=*/true);
26564 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
26566 c = build_omp_clause (location, OMP_CLAUSE_IF);
26567 OMP_CLAUSE_IF_EXPR (c) = t;
26568 OMP_CLAUSE_CHAIN (c) = list;
26570 return c;
26573 /* OpenMP 3.1:
26574 mergeable */
26576 static tree
26577 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
26578 tree list, location_t location)
26580 tree c;
26582 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
26583 location);
26585 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
26586 OMP_CLAUSE_CHAIN (c) = list;
26587 return c;
26590 /* OpenMP 2.5:
26591 nowait */
26593 static tree
26594 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
26595 tree list, location_t location)
26597 tree c;
26599 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
26601 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
26602 OMP_CLAUSE_CHAIN (c) = list;
26603 return c;
26606 /* OpenMP 2.5:
26607 num_threads ( expression ) */
26609 static tree
26610 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
26611 location_t location)
26613 tree t, c;
26615 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26616 return list;
26618 t = cp_parser_expression (parser, false, NULL);
26620 if (t == error_mark_node
26621 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26622 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26623 /*or_comma=*/false,
26624 /*consume_paren=*/true);
26626 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
26627 "num_threads", location);
26629 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
26630 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
26631 OMP_CLAUSE_CHAIN (c) = list;
26633 return c;
26636 /* OpenMP 2.5:
26637 ordered */
26639 static tree
26640 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
26641 tree list, location_t location)
26643 tree c;
26645 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
26646 "ordered", location);
26648 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
26649 OMP_CLAUSE_CHAIN (c) = list;
26650 return c;
26653 /* OpenMP 2.5:
26654 reduction ( reduction-operator : variable-list )
26656 reduction-operator:
26657 One of: + * - & ^ | && ||
26659 OpenMP 3.1:
26661 reduction-operator:
26662 One of: + * - & ^ | && || min max */
26664 static tree
26665 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
26667 enum tree_code code;
26668 tree nlist, c;
26670 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26671 return list;
26673 switch (cp_lexer_peek_token (parser->lexer)->type)
26675 case CPP_PLUS:
26676 code = PLUS_EXPR;
26677 break;
26678 case CPP_MULT:
26679 code = MULT_EXPR;
26680 break;
26681 case CPP_MINUS:
26682 code = MINUS_EXPR;
26683 break;
26684 case CPP_AND:
26685 code = BIT_AND_EXPR;
26686 break;
26687 case CPP_XOR:
26688 code = BIT_XOR_EXPR;
26689 break;
26690 case CPP_OR:
26691 code = BIT_IOR_EXPR;
26692 break;
26693 case CPP_AND_AND:
26694 code = TRUTH_ANDIF_EXPR;
26695 break;
26696 case CPP_OR_OR:
26697 code = TRUTH_ORIF_EXPR;
26698 break;
26699 case CPP_NAME:
26701 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26702 const char *p = IDENTIFIER_POINTER (id);
26704 if (strcmp (p, "min") == 0)
26706 code = MIN_EXPR;
26707 break;
26709 if (strcmp (p, "max") == 0)
26711 code = MAX_EXPR;
26712 break;
26715 /* FALLTHROUGH */
26716 default:
26717 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26718 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26719 resync_fail:
26720 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26721 /*or_comma=*/false,
26722 /*consume_paren=*/true);
26723 return list;
26725 cp_lexer_consume_token (parser->lexer);
26727 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26728 goto resync_fail;
26730 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
26731 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
26732 OMP_CLAUSE_REDUCTION_CODE (c) = code;
26734 return nlist;
26737 /* OpenMP 2.5:
26738 schedule ( schedule-kind )
26739 schedule ( schedule-kind , expression )
26741 schedule-kind:
26742 static | dynamic | guided | runtime | auto */
26744 static tree
26745 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
26747 tree c, t;
26749 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26750 return list;
26752 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
26754 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26756 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26757 const char *p = IDENTIFIER_POINTER (id);
26759 switch (p[0])
26761 case 'd':
26762 if (strcmp ("dynamic", p) != 0)
26763 goto invalid_kind;
26764 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
26765 break;
26767 case 'g':
26768 if (strcmp ("guided", p) != 0)
26769 goto invalid_kind;
26770 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
26771 break;
26773 case 'r':
26774 if (strcmp ("runtime", p) != 0)
26775 goto invalid_kind;
26776 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
26777 break;
26779 default:
26780 goto invalid_kind;
26783 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
26784 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
26785 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
26786 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
26787 else
26788 goto invalid_kind;
26789 cp_lexer_consume_token (parser->lexer);
26791 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26793 cp_token *token;
26794 cp_lexer_consume_token (parser->lexer);
26796 token = cp_lexer_peek_token (parser->lexer);
26797 t = cp_parser_assignment_expression (parser, false, NULL);
26799 if (t == error_mark_node)
26800 goto resync_fail;
26801 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
26802 error_at (token->location, "schedule %<runtime%> does not take "
26803 "a %<chunk_size%> parameter");
26804 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
26805 error_at (token->location, "schedule %<auto%> does not take "
26806 "a %<chunk_size%> parameter");
26807 else
26808 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
26810 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26811 goto resync_fail;
26813 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
26814 goto resync_fail;
26816 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
26817 OMP_CLAUSE_CHAIN (c) = list;
26818 return c;
26820 invalid_kind:
26821 cp_parser_error (parser, "invalid schedule kind");
26822 resync_fail:
26823 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26824 /*or_comma=*/false,
26825 /*consume_paren=*/true);
26826 return list;
26829 /* OpenMP 3.0:
26830 untied */
26832 static tree
26833 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
26834 tree list, location_t location)
26836 tree c;
26838 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
26840 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
26841 OMP_CLAUSE_CHAIN (c) = list;
26842 return c;
26845 /* Parse all OpenMP clauses. The set clauses allowed by the directive
26846 is a bitmask in MASK. Return the list of clauses found; the result
26847 of clause default goes in *pdefault. */
26849 static tree
26850 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
26851 const char *where, cp_token *pragma_tok)
26853 tree clauses = NULL;
26854 bool first = true;
26855 cp_token *token = NULL;
26857 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
26859 pragma_omp_clause c_kind;
26860 const char *c_name;
26861 tree prev = clauses;
26863 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26864 cp_lexer_consume_token (parser->lexer);
26866 token = cp_lexer_peek_token (parser->lexer);
26867 c_kind = cp_parser_omp_clause_name (parser);
26868 first = false;
26870 switch (c_kind)
26872 case PRAGMA_OMP_CLAUSE_COLLAPSE:
26873 clauses = cp_parser_omp_clause_collapse (parser, clauses,
26874 token->location);
26875 c_name = "collapse";
26876 break;
26877 case PRAGMA_OMP_CLAUSE_COPYIN:
26878 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
26879 c_name = "copyin";
26880 break;
26881 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
26882 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
26883 clauses);
26884 c_name = "copyprivate";
26885 break;
26886 case PRAGMA_OMP_CLAUSE_DEFAULT:
26887 clauses = cp_parser_omp_clause_default (parser, clauses,
26888 token->location);
26889 c_name = "default";
26890 break;
26891 case PRAGMA_OMP_CLAUSE_FINAL:
26892 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
26893 c_name = "final";
26894 break;
26895 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
26896 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
26897 clauses);
26898 c_name = "firstprivate";
26899 break;
26900 case PRAGMA_OMP_CLAUSE_IF:
26901 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
26902 c_name = "if";
26903 break;
26904 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
26905 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
26906 clauses);
26907 c_name = "lastprivate";
26908 break;
26909 case PRAGMA_OMP_CLAUSE_MERGEABLE:
26910 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
26911 token->location);
26912 c_name = "mergeable";
26913 break;
26914 case PRAGMA_OMP_CLAUSE_NOWAIT:
26915 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
26916 c_name = "nowait";
26917 break;
26918 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
26919 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
26920 token->location);
26921 c_name = "num_threads";
26922 break;
26923 case PRAGMA_OMP_CLAUSE_ORDERED:
26924 clauses = cp_parser_omp_clause_ordered (parser, clauses,
26925 token->location);
26926 c_name = "ordered";
26927 break;
26928 case PRAGMA_OMP_CLAUSE_PRIVATE:
26929 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
26930 clauses);
26931 c_name = "private";
26932 break;
26933 case PRAGMA_OMP_CLAUSE_REDUCTION:
26934 clauses = cp_parser_omp_clause_reduction (parser, clauses);
26935 c_name = "reduction";
26936 break;
26937 case PRAGMA_OMP_CLAUSE_SCHEDULE:
26938 clauses = cp_parser_omp_clause_schedule (parser, clauses,
26939 token->location);
26940 c_name = "schedule";
26941 break;
26942 case PRAGMA_OMP_CLAUSE_SHARED:
26943 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
26944 clauses);
26945 c_name = "shared";
26946 break;
26947 case PRAGMA_OMP_CLAUSE_UNTIED:
26948 clauses = cp_parser_omp_clause_untied (parser, clauses,
26949 token->location);
26950 c_name = "nowait";
26951 break;
26952 default:
26953 cp_parser_error (parser, "expected %<#pragma omp%> clause");
26954 goto saw_error;
26957 if (((mask >> c_kind) & 1) == 0)
26959 /* Remove the invalid clause(s) from the list to avoid
26960 confusing the rest of the compiler. */
26961 clauses = prev;
26962 error_at (token->location, "%qs is not valid for %qs", c_name, where);
26965 saw_error:
26966 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26967 return finish_omp_clauses (clauses);
26970 /* OpenMP 2.5:
26971 structured-block:
26972 statement
26974 In practice, we're also interested in adding the statement to an
26975 outer node. So it is convenient if we work around the fact that
26976 cp_parser_statement calls add_stmt. */
26978 static unsigned
26979 cp_parser_begin_omp_structured_block (cp_parser *parser)
26981 unsigned save = parser->in_statement;
26983 /* Only move the values to IN_OMP_BLOCK if they weren't false.
26984 This preserves the "not within loop or switch" style error messages
26985 for nonsense cases like
26986 void foo() {
26987 #pragma omp single
26988 break;
26991 if (parser->in_statement)
26992 parser->in_statement = IN_OMP_BLOCK;
26994 return save;
26997 static void
26998 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
27000 parser->in_statement = save;
27003 static tree
27004 cp_parser_omp_structured_block (cp_parser *parser)
27006 tree stmt = begin_omp_structured_block ();
27007 unsigned int save = cp_parser_begin_omp_structured_block (parser);
27009 cp_parser_statement (parser, NULL_TREE, false, NULL);
27011 cp_parser_end_omp_structured_block (parser, save);
27012 return finish_omp_structured_block (stmt);
27015 /* OpenMP 2.5:
27016 # pragma omp atomic new-line
27017 expression-stmt
27019 expression-stmt:
27020 x binop= expr | x++ | ++x | x-- | --x
27021 binop:
27022 +, *, -, /, &, ^, |, <<, >>
27024 where x is an lvalue expression with scalar type.
27026 OpenMP 3.1:
27027 # pragma omp atomic new-line
27028 update-stmt
27030 # pragma omp atomic read new-line
27031 read-stmt
27033 # pragma omp atomic write new-line
27034 write-stmt
27036 # pragma omp atomic update new-line
27037 update-stmt
27039 # pragma omp atomic capture new-line
27040 capture-stmt
27042 # pragma omp atomic capture new-line
27043 capture-block
27045 read-stmt:
27046 v = x
27047 write-stmt:
27048 x = expr
27049 update-stmt:
27050 expression-stmt | x = x binop expr
27051 capture-stmt:
27052 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
27053 capture-block:
27054 { v = x; update-stmt; } | { update-stmt; v = x; }
27056 where x and v are lvalue expressions with scalar type. */
27058 static void
27059 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
27061 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
27062 tree rhs1 = NULL_TREE, orig_lhs;
27063 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
27064 bool structured_block = false;
27066 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27068 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27069 const char *p = IDENTIFIER_POINTER (id);
27071 if (!strcmp (p, "read"))
27072 code = OMP_ATOMIC_READ;
27073 else if (!strcmp (p, "write"))
27074 code = NOP_EXPR;
27075 else if (!strcmp (p, "update"))
27076 code = OMP_ATOMIC;
27077 else if (!strcmp (p, "capture"))
27078 code = OMP_ATOMIC_CAPTURE_NEW;
27079 else
27080 p = NULL;
27081 if (p)
27082 cp_lexer_consume_token (parser->lexer);
27084 cp_parser_require_pragma_eol (parser, pragma_tok);
27086 switch (code)
27088 case OMP_ATOMIC_READ:
27089 case NOP_EXPR: /* atomic write */
27090 v = cp_parser_unary_expression (parser, /*address_p=*/false,
27091 /*cast_p=*/false, NULL);
27092 if (v == error_mark_node)
27093 goto saw_error;
27094 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27095 goto saw_error;
27096 if (code == NOP_EXPR)
27097 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27098 else
27099 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
27100 /*cast_p=*/false, NULL);
27101 if (lhs == error_mark_node)
27102 goto saw_error;
27103 if (code == NOP_EXPR)
27105 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
27106 opcode. */
27107 code = OMP_ATOMIC;
27108 rhs = lhs;
27109 lhs = v;
27110 v = NULL_TREE;
27112 goto done;
27113 case OMP_ATOMIC_CAPTURE_NEW:
27114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27116 cp_lexer_consume_token (parser->lexer);
27117 structured_block = true;
27119 else
27121 v = cp_parser_unary_expression (parser, /*address_p=*/false,
27122 /*cast_p=*/false, NULL);
27123 if (v == error_mark_node)
27124 goto saw_error;
27125 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27126 goto saw_error;
27128 default:
27129 break;
27132 restart:
27133 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
27134 /*cast_p=*/false, NULL);
27135 orig_lhs = lhs;
27136 switch (TREE_CODE (lhs))
27138 case ERROR_MARK:
27139 goto saw_error;
27141 case POSTINCREMENT_EXPR:
27142 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
27143 code = OMP_ATOMIC_CAPTURE_OLD;
27144 /* FALLTHROUGH */
27145 case PREINCREMENT_EXPR:
27146 lhs = TREE_OPERAND (lhs, 0);
27147 opcode = PLUS_EXPR;
27148 rhs = integer_one_node;
27149 break;
27151 case POSTDECREMENT_EXPR:
27152 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
27153 code = OMP_ATOMIC_CAPTURE_OLD;
27154 /* FALLTHROUGH */
27155 case PREDECREMENT_EXPR:
27156 lhs = TREE_OPERAND (lhs, 0);
27157 opcode = MINUS_EXPR;
27158 rhs = integer_one_node;
27159 break;
27161 case COMPOUND_EXPR:
27162 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
27163 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
27164 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
27165 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
27166 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
27167 (TREE_OPERAND (lhs, 1), 0), 0)))
27168 == BOOLEAN_TYPE)
27169 /* Undo effects of boolean_increment for post {in,de}crement. */
27170 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
27171 /* FALLTHRU */
27172 case MODIFY_EXPR:
27173 if (TREE_CODE (lhs) == MODIFY_EXPR
27174 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
27176 /* Undo effects of boolean_increment. */
27177 if (integer_onep (TREE_OPERAND (lhs, 1)))
27179 /* This is pre or post increment. */
27180 rhs = TREE_OPERAND (lhs, 1);
27181 lhs = TREE_OPERAND (lhs, 0);
27182 opcode = NOP_EXPR;
27183 if (code == OMP_ATOMIC_CAPTURE_NEW
27184 && !structured_block
27185 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
27186 code = OMP_ATOMIC_CAPTURE_OLD;
27187 break;
27190 /* FALLTHRU */
27191 default:
27192 switch (cp_lexer_peek_token (parser->lexer)->type)
27194 case CPP_MULT_EQ:
27195 opcode = MULT_EXPR;
27196 break;
27197 case CPP_DIV_EQ:
27198 opcode = TRUNC_DIV_EXPR;
27199 break;
27200 case CPP_PLUS_EQ:
27201 opcode = PLUS_EXPR;
27202 break;
27203 case CPP_MINUS_EQ:
27204 opcode = MINUS_EXPR;
27205 break;
27206 case CPP_LSHIFT_EQ:
27207 opcode = LSHIFT_EXPR;
27208 break;
27209 case CPP_RSHIFT_EQ:
27210 opcode = RSHIFT_EXPR;
27211 break;
27212 case CPP_AND_EQ:
27213 opcode = BIT_AND_EXPR;
27214 break;
27215 case CPP_OR_EQ:
27216 opcode = BIT_IOR_EXPR;
27217 break;
27218 case CPP_XOR_EQ:
27219 opcode = BIT_XOR_EXPR;
27220 break;
27221 case CPP_EQ:
27222 if (structured_block || code == OMP_ATOMIC)
27224 enum cp_parser_prec oprec;
27225 cp_token *token;
27226 cp_lexer_consume_token (parser->lexer);
27227 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
27228 /*cast_p=*/false, NULL);
27229 if (rhs1 == error_mark_node)
27230 goto saw_error;
27231 token = cp_lexer_peek_token (parser->lexer);
27232 switch (token->type)
27234 case CPP_SEMICOLON:
27235 if (code == OMP_ATOMIC_CAPTURE_NEW)
27237 code = OMP_ATOMIC_CAPTURE_OLD;
27238 v = lhs;
27239 lhs = NULL_TREE;
27240 lhs1 = rhs1;
27241 rhs1 = NULL_TREE;
27242 cp_lexer_consume_token (parser->lexer);
27243 goto restart;
27245 cp_parser_error (parser,
27246 "invalid form of %<#pragma omp atomic%>");
27247 goto saw_error;
27248 case CPP_MULT:
27249 opcode = MULT_EXPR;
27250 break;
27251 case CPP_DIV:
27252 opcode = TRUNC_DIV_EXPR;
27253 break;
27254 case CPP_PLUS:
27255 opcode = PLUS_EXPR;
27256 break;
27257 case CPP_MINUS:
27258 opcode = MINUS_EXPR;
27259 break;
27260 case CPP_LSHIFT:
27261 opcode = LSHIFT_EXPR;
27262 break;
27263 case CPP_RSHIFT:
27264 opcode = RSHIFT_EXPR;
27265 break;
27266 case CPP_AND:
27267 opcode = BIT_AND_EXPR;
27268 break;
27269 case CPP_OR:
27270 opcode = BIT_IOR_EXPR;
27271 break;
27272 case CPP_XOR:
27273 opcode = BIT_XOR_EXPR;
27274 break;
27275 default:
27276 cp_parser_error (parser,
27277 "invalid operator for %<#pragma omp atomic%>");
27278 goto saw_error;
27280 oprec = TOKEN_PRECEDENCE (token);
27281 gcc_assert (oprec != PREC_NOT_OPERATOR);
27282 if (commutative_tree_code (opcode))
27283 oprec = (enum cp_parser_prec) (oprec - 1);
27284 cp_lexer_consume_token (parser->lexer);
27285 rhs = cp_parser_binary_expression (parser, false, false,
27286 oprec, NULL);
27287 if (rhs == error_mark_node)
27288 goto saw_error;
27289 goto stmt_done;
27291 /* FALLTHROUGH */
27292 default:
27293 cp_parser_error (parser,
27294 "invalid operator for %<#pragma omp atomic%>");
27295 goto saw_error;
27297 cp_lexer_consume_token (parser->lexer);
27299 rhs = cp_parser_expression (parser, false, NULL);
27300 if (rhs == error_mark_node)
27301 goto saw_error;
27302 break;
27304 stmt_done:
27305 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
27307 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
27308 goto saw_error;
27309 v = cp_parser_unary_expression (parser, /*address_p=*/false,
27310 /*cast_p=*/false, NULL);
27311 if (v == error_mark_node)
27312 goto saw_error;
27313 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27314 goto saw_error;
27315 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
27316 /*cast_p=*/false, NULL);
27317 if (lhs1 == error_mark_node)
27318 goto saw_error;
27320 if (structured_block)
27322 cp_parser_consume_semicolon_at_end_of_statement (parser);
27323 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
27325 done:
27326 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
27327 if (!structured_block)
27328 cp_parser_consume_semicolon_at_end_of_statement (parser);
27329 return;
27331 saw_error:
27332 cp_parser_skip_to_end_of_block_or_statement (parser);
27333 if (structured_block)
27335 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27336 cp_lexer_consume_token (parser->lexer);
27337 else if (code == OMP_ATOMIC_CAPTURE_NEW)
27339 cp_parser_skip_to_end_of_block_or_statement (parser);
27340 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27341 cp_lexer_consume_token (parser->lexer);
27347 /* OpenMP 2.5:
27348 # pragma omp barrier new-line */
27350 static void
27351 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
27353 cp_parser_require_pragma_eol (parser, pragma_tok);
27354 finish_omp_barrier ();
27357 /* OpenMP 2.5:
27358 # pragma omp critical [(name)] new-line
27359 structured-block */
27361 static tree
27362 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
27364 tree stmt, name = NULL;
27366 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27368 cp_lexer_consume_token (parser->lexer);
27370 name = cp_parser_identifier (parser);
27372 if (name == error_mark_node
27373 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27374 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27375 /*or_comma=*/false,
27376 /*consume_paren=*/true);
27377 if (name == error_mark_node)
27378 name = NULL;
27380 cp_parser_require_pragma_eol (parser, pragma_tok);
27382 stmt = cp_parser_omp_structured_block (parser);
27383 return c_finish_omp_critical (input_location, stmt, name);
27386 /* OpenMP 2.5:
27387 # pragma omp flush flush-vars[opt] new-line
27389 flush-vars:
27390 ( variable-list ) */
27392 static void
27393 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
27395 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27396 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27397 cp_parser_require_pragma_eol (parser, pragma_tok);
27399 finish_omp_flush ();
27402 /* Helper function, to parse omp for increment expression. */
27404 static tree
27405 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
27407 tree cond = cp_parser_binary_expression (parser, false, true,
27408 PREC_NOT_OPERATOR, NULL);
27409 if (cond == error_mark_node
27410 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27412 cp_parser_skip_to_end_of_statement (parser);
27413 return error_mark_node;
27416 switch (TREE_CODE (cond))
27418 case GT_EXPR:
27419 case GE_EXPR:
27420 case LT_EXPR:
27421 case LE_EXPR:
27422 break;
27423 default:
27424 return error_mark_node;
27427 /* If decl is an iterator, preserve LHS and RHS of the relational
27428 expr until finish_omp_for. */
27429 if (decl
27430 && (type_dependent_expression_p (decl)
27431 || CLASS_TYPE_P (TREE_TYPE (decl))))
27432 return cond;
27434 return build_x_binary_op (input_location, TREE_CODE (cond),
27435 TREE_OPERAND (cond, 0), ERROR_MARK,
27436 TREE_OPERAND (cond, 1), ERROR_MARK,
27437 /*overload=*/NULL, tf_warning_or_error);
27440 /* Helper function, to parse omp for increment expression. */
27442 static tree
27443 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
27445 cp_token *token = cp_lexer_peek_token (parser->lexer);
27446 enum tree_code op;
27447 tree lhs, rhs;
27448 cp_id_kind idk;
27449 bool decl_first;
27451 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
27453 op = (token->type == CPP_PLUS_PLUS
27454 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
27455 cp_lexer_consume_token (parser->lexer);
27456 lhs = cp_parser_simple_cast_expression (parser);
27457 if (lhs != decl)
27458 return error_mark_node;
27459 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
27462 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
27463 if (lhs != decl)
27464 return error_mark_node;
27466 token = cp_lexer_peek_token (parser->lexer);
27467 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
27469 op = (token->type == CPP_PLUS_PLUS
27470 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
27471 cp_lexer_consume_token (parser->lexer);
27472 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
27475 op = cp_parser_assignment_operator_opt (parser);
27476 if (op == ERROR_MARK)
27477 return error_mark_node;
27479 if (op != NOP_EXPR)
27481 rhs = cp_parser_assignment_expression (parser, false, NULL);
27482 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
27483 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
27486 lhs = cp_parser_binary_expression (parser, false, false,
27487 PREC_ADDITIVE_EXPRESSION, NULL);
27488 token = cp_lexer_peek_token (parser->lexer);
27489 decl_first = lhs == decl;
27490 if (decl_first)
27491 lhs = NULL_TREE;
27492 if (token->type != CPP_PLUS
27493 && token->type != CPP_MINUS)
27494 return error_mark_node;
27498 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
27499 cp_lexer_consume_token (parser->lexer);
27500 rhs = cp_parser_binary_expression (parser, false, false,
27501 PREC_ADDITIVE_EXPRESSION, NULL);
27502 token = cp_lexer_peek_token (parser->lexer);
27503 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
27505 if (lhs == NULL_TREE)
27507 if (op == PLUS_EXPR)
27508 lhs = rhs;
27509 else
27510 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
27511 tf_warning_or_error);
27513 else
27514 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
27515 ERROR_MARK, NULL, tf_warning_or_error);
27518 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
27520 if (!decl_first)
27522 if (rhs != decl || op == MINUS_EXPR)
27523 return error_mark_node;
27524 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
27526 else
27527 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
27529 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
27532 /* Parse the restricted form of the for statement allowed by OpenMP. */
27534 static tree
27535 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
27537 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
27538 tree real_decl, initv, condv, incrv, declv;
27539 tree this_pre_body, cl;
27540 location_t loc_first;
27541 bool collapse_err = false;
27542 int i, collapse = 1, nbraces = 0;
27543 vec<tree, va_gc> *for_block = make_tree_vector ();
27545 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
27546 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
27547 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
27549 gcc_assert (collapse >= 1);
27551 declv = make_tree_vec (collapse);
27552 initv = make_tree_vec (collapse);
27553 condv = make_tree_vec (collapse);
27554 incrv = make_tree_vec (collapse);
27556 loc_first = cp_lexer_peek_token (parser->lexer)->location;
27558 for (i = 0; i < collapse; i++)
27560 int bracecount = 0;
27561 bool add_private_clause = false;
27562 location_t loc;
27564 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27566 cp_parser_error (parser, "for statement expected");
27567 return NULL;
27569 loc = cp_lexer_consume_token (parser->lexer)->location;
27571 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27572 return NULL;
27574 init = decl = real_decl = NULL;
27575 this_pre_body = push_stmt_list ();
27576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27578 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
27580 init-expr:
27581 var = lb
27582 integer-type var = lb
27583 random-access-iterator-type var = lb
27584 pointer-type var = lb
27586 cp_decl_specifier_seq type_specifiers;
27588 /* First, try to parse as an initialized declaration. See
27589 cp_parser_condition, from whence the bulk of this is copied. */
27591 cp_parser_parse_tentatively (parser);
27592 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
27593 /*is_trailing_return=*/false,
27594 &type_specifiers);
27595 if (cp_parser_parse_definitely (parser))
27597 /* If parsing a type specifier seq succeeded, then this
27598 MUST be a initialized declaration. */
27599 tree asm_specification, attributes;
27600 cp_declarator *declarator;
27602 declarator = cp_parser_declarator (parser,
27603 CP_PARSER_DECLARATOR_NAMED,
27604 /*ctor_dtor_or_conv_p=*/NULL,
27605 /*parenthesized_p=*/NULL,
27606 /*member_p=*/false);
27607 attributes = cp_parser_attributes_opt (parser);
27608 asm_specification = cp_parser_asm_specification_opt (parser);
27610 if (declarator == cp_error_declarator)
27611 cp_parser_skip_to_end_of_statement (parser);
27613 else
27615 tree pushed_scope, auto_node;
27617 decl = start_decl (declarator, &type_specifiers,
27618 SD_INITIALIZED, attributes,
27619 /*prefix_attributes=*/NULL_TREE,
27620 &pushed_scope);
27622 auto_node = type_uses_auto (TREE_TYPE (decl));
27623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27625 if (cp_lexer_next_token_is (parser->lexer,
27626 CPP_OPEN_PAREN))
27627 error ("parenthesized initialization is not allowed in "
27628 "OpenMP %<for%> loop");
27629 else
27630 /* Trigger an error. */
27631 cp_parser_require (parser, CPP_EQ, RT_EQ);
27633 init = error_mark_node;
27634 cp_parser_skip_to_end_of_statement (parser);
27636 else if (CLASS_TYPE_P (TREE_TYPE (decl))
27637 || type_dependent_expression_p (decl)
27638 || auto_node)
27640 bool is_direct_init, is_non_constant_init;
27642 init = cp_parser_initializer (parser,
27643 &is_direct_init,
27644 &is_non_constant_init);
27646 if (auto_node)
27648 TREE_TYPE (decl)
27649 = do_auto_deduction (TREE_TYPE (decl), init,
27650 auto_node);
27652 if (!CLASS_TYPE_P (TREE_TYPE (decl))
27653 && !type_dependent_expression_p (decl))
27654 goto non_class;
27657 cp_finish_decl (decl, init, !is_non_constant_init,
27658 asm_specification,
27659 LOOKUP_ONLYCONVERTING);
27660 if (CLASS_TYPE_P (TREE_TYPE (decl)))
27662 vec_safe_push (for_block, this_pre_body);
27663 init = NULL_TREE;
27665 else
27666 init = pop_stmt_list (this_pre_body);
27667 this_pre_body = NULL_TREE;
27669 else
27671 /* Consume '='. */
27672 cp_lexer_consume_token (parser->lexer);
27673 init = cp_parser_assignment_expression (parser, false, NULL);
27675 non_class:
27676 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
27677 init = error_mark_node;
27678 else
27679 cp_finish_decl (decl, NULL_TREE,
27680 /*init_const_expr_p=*/false,
27681 asm_specification,
27682 LOOKUP_ONLYCONVERTING);
27685 if (pushed_scope)
27686 pop_scope (pushed_scope);
27689 else
27691 cp_id_kind idk;
27692 /* If parsing a type specifier sequence failed, then
27693 this MUST be a simple expression. */
27694 cp_parser_parse_tentatively (parser);
27695 decl = cp_parser_primary_expression (parser, false, false,
27696 false, &idk);
27697 if (!cp_parser_error_occurred (parser)
27698 && decl
27699 && DECL_P (decl)
27700 && CLASS_TYPE_P (TREE_TYPE (decl)))
27702 tree rhs;
27704 cp_parser_parse_definitely (parser);
27705 cp_parser_require (parser, CPP_EQ, RT_EQ);
27706 rhs = cp_parser_assignment_expression (parser, false, NULL);
27707 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
27708 decl, NOP_EXPR,
27709 rhs,
27710 tf_warning_or_error));
27711 add_private_clause = true;
27713 else
27715 decl = NULL;
27716 cp_parser_abort_tentative_parse (parser);
27717 init = cp_parser_expression (parser, false, NULL);
27718 if (init)
27720 if (TREE_CODE (init) == MODIFY_EXPR
27721 || TREE_CODE (init) == MODOP_EXPR)
27722 real_decl = TREE_OPERAND (init, 0);
27727 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27728 if (this_pre_body)
27730 this_pre_body = pop_stmt_list (this_pre_body);
27731 if (pre_body)
27733 tree t = pre_body;
27734 pre_body = push_stmt_list ();
27735 add_stmt (t);
27736 add_stmt (this_pre_body);
27737 pre_body = pop_stmt_list (pre_body);
27739 else
27740 pre_body = this_pre_body;
27743 if (decl)
27744 real_decl = decl;
27745 if (par_clauses != NULL && real_decl != NULL_TREE)
27747 tree *c;
27748 for (c = par_clauses; *c ; )
27749 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
27750 && OMP_CLAUSE_DECL (*c) == real_decl)
27752 error_at (loc, "iteration variable %qD"
27753 " should not be firstprivate", real_decl);
27754 *c = OMP_CLAUSE_CHAIN (*c);
27756 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
27757 && OMP_CLAUSE_DECL (*c) == real_decl)
27759 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27760 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
27761 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
27762 OMP_CLAUSE_DECL (l) = real_decl;
27763 OMP_CLAUSE_CHAIN (l) = clauses;
27764 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
27765 clauses = l;
27766 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
27767 CP_OMP_CLAUSE_INFO (*c) = NULL;
27768 add_private_clause = false;
27770 else
27772 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
27773 && OMP_CLAUSE_DECL (*c) == real_decl)
27774 add_private_clause = false;
27775 c = &OMP_CLAUSE_CHAIN (*c);
27779 if (add_private_clause)
27781 tree c;
27782 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27784 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
27785 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
27786 && OMP_CLAUSE_DECL (c) == decl)
27787 break;
27788 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
27789 && OMP_CLAUSE_DECL (c) == decl)
27790 error_at (loc, "iteration variable %qD "
27791 "should not be firstprivate",
27792 decl);
27793 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
27794 && OMP_CLAUSE_DECL (c) == decl)
27795 error_at (loc, "iteration variable %qD should not be reduction",
27796 decl);
27798 if (c == NULL)
27800 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
27801 OMP_CLAUSE_DECL (c) = decl;
27802 c = finish_omp_clauses (c);
27803 if (c)
27805 OMP_CLAUSE_CHAIN (c) = clauses;
27806 clauses = c;
27811 cond = NULL;
27812 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27813 cond = cp_parser_omp_for_cond (parser, decl);
27814 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27816 incr = NULL;
27817 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27819 /* If decl is an iterator, preserve the operator on decl
27820 until finish_omp_for. */
27821 if (real_decl
27822 && ((processing_template_decl
27823 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
27824 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
27825 incr = cp_parser_omp_for_incr (parser, real_decl);
27826 else
27827 incr = cp_parser_expression (parser, false, NULL);
27828 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
27829 SET_EXPR_LOCATION (incr, input_location);
27832 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27833 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27834 /*or_comma=*/false,
27835 /*consume_paren=*/true);
27837 TREE_VEC_ELT (declv, i) = decl;
27838 TREE_VEC_ELT (initv, i) = init;
27839 TREE_VEC_ELT (condv, i) = cond;
27840 TREE_VEC_ELT (incrv, i) = incr;
27842 if (i == collapse - 1)
27843 break;
27845 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27846 in between the collapsed for loops to be still considered perfectly
27847 nested. Hopefully the final version clarifies this.
27848 For now handle (multiple) {'s and empty statements. */
27849 cp_parser_parse_tentatively (parser);
27852 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27853 break;
27854 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27856 cp_lexer_consume_token (parser->lexer);
27857 bracecount++;
27859 else if (bracecount
27860 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27861 cp_lexer_consume_token (parser->lexer);
27862 else
27864 loc = cp_lexer_peek_token (parser->lexer)->location;
27865 error_at (loc, "not enough collapsed for loops");
27866 collapse_err = true;
27867 cp_parser_abort_tentative_parse (parser);
27868 declv = NULL_TREE;
27869 break;
27872 while (1);
27874 if (declv)
27876 cp_parser_parse_definitely (parser);
27877 nbraces += bracecount;
27881 /* Note that we saved the original contents of this flag when we entered
27882 the structured block, and so we don't need to re-save it here. */
27883 parser->in_statement = IN_OMP_FOR;
27885 /* Note that the grammar doesn't call for a structured block here,
27886 though the loop as a whole is a structured block. */
27887 body = push_stmt_list ();
27888 cp_parser_statement (parser, NULL_TREE, false, NULL);
27889 body = pop_stmt_list (body);
27891 if (declv == NULL_TREE)
27892 ret = NULL_TREE;
27893 else
27894 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
27895 pre_body, clauses);
27897 while (nbraces)
27899 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27901 cp_lexer_consume_token (parser->lexer);
27902 nbraces--;
27904 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27905 cp_lexer_consume_token (parser->lexer);
27906 else
27908 if (!collapse_err)
27910 error_at (cp_lexer_peek_token (parser->lexer)->location,
27911 "collapsed loops not perfectly nested");
27913 collapse_err = true;
27914 cp_parser_statement_seq_opt (parser, NULL);
27915 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27916 break;
27920 while (!for_block->is_empty ())
27921 add_stmt (pop_stmt_list (for_block->pop ()));
27922 release_tree_vector (for_block);
27924 return ret;
27927 /* OpenMP 2.5:
27928 #pragma omp for for-clause[optseq] new-line
27929 for-loop */
27931 #define OMP_FOR_CLAUSE_MASK \
27932 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27933 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27934 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27935 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27936 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
27937 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
27938 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
27939 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27941 static tree
27942 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
27944 tree clauses, sb, ret;
27945 unsigned int save;
27947 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
27948 "#pragma omp for", pragma_tok);
27950 sb = begin_omp_structured_block ();
27951 save = cp_parser_begin_omp_structured_block (parser);
27953 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
27955 cp_parser_end_omp_structured_block (parser, save);
27956 add_stmt (finish_omp_structured_block (sb));
27958 return ret;
27961 /* OpenMP 2.5:
27962 # pragma omp master new-line
27963 structured-block */
27965 static tree
27966 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
27968 cp_parser_require_pragma_eol (parser, pragma_tok);
27969 return c_finish_omp_master (input_location,
27970 cp_parser_omp_structured_block (parser));
27973 /* OpenMP 2.5:
27974 # pragma omp ordered new-line
27975 structured-block */
27977 static tree
27978 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
27980 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27981 cp_parser_require_pragma_eol (parser, pragma_tok);
27982 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
27985 /* OpenMP 2.5:
27987 section-scope:
27988 { section-sequence }
27990 section-sequence:
27991 section-directive[opt] structured-block
27992 section-sequence section-directive structured-block */
27994 static tree
27995 cp_parser_omp_sections_scope (cp_parser *parser)
27997 tree stmt, substmt;
27998 bool error_suppress = false;
27999 cp_token *tok;
28001 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
28002 return NULL_TREE;
28004 stmt = push_stmt_list ();
28006 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
28008 unsigned save;
28010 substmt = begin_omp_structured_block ();
28011 save = cp_parser_begin_omp_structured_block (parser);
28013 while (1)
28015 cp_parser_statement (parser, NULL_TREE, false, NULL);
28017 tok = cp_lexer_peek_token (parser->lexer);
28018 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
28019 break;
28020 if (tok->type == CPP_CLOSE_BRACE)
28021 break;
28022 if (tok->type == CPP_EOF)
28023 break;
28026 cp_parser_end_omp_structured_block (parser, save);
28027 substmt = finish_omp_structured_block (substmt);
28028 substmt = build1 (OMP_SECTION, void_type_node, substmt);
28029 add_stmt (substmt);
28032 while (1)
28034 tok = cp_lexer_peek_token (parser->lexer);
28035 if (tok->type == CPP_CLOSE_BRACE)
28036 break;
28037 if (tok->type == CPP_EOF)
28038 break;
28040 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
28042 cp_lexer_consume_token (parser->lexer);
28043 cp_parser_require_pragma_eol (parser, tok);
28044 error_suppress = false;
28046 else if (!error_suppress)
28048 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
28049 error_suppress = true;
28052 substmt = cp_parser_omp_structured_block (parser);
28053 substmt = build1 (OMP_SECTION, void_type_node, substmt);
28054 add_stmt (substmt);
28056 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
28058 substmt = pop_stmt_list (stmt);
28060 stmt = make_node (OMP_SECTIONS);
28061 TREE_TYPE (stmt) = void_type_node;
28062 OMP_SECTIONS_BODY (stmt) = substmt;
28064 add_stmt (stmt);
28065 return stmt;
28068 /* OpenMP 2.5:
28069 # pragma omp sections sections-clause[optseq] newline
28070 sections-scope */
28072 #define OMP_SECTIONS_CLAUSE_MASK \
28073 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
28074 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
28075 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
28076 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
28077 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
28079 static tree
28080 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
28082 tree clauses, ret;
28084 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
28085 "#pragma omp sections", pragma_tok);
28087 ret = cp_parser_omp_sections_scope (parser);
28088 if (ret)
28089 OMP_SECTIONS_CLAUSES (ret) = clauses;
28091 return ret;
28094 /* OpenMP 2.5:
28095 # pragma parallel parallel-clause new-line
28096 # pragma parallel for parallel-for-clause new-line
28097 # pragma parallel sections parallel-sections-clause new-line */
28099 #define OMP_PARALLEL_CLAUSE_MASK \
28100 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
28101 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
28102 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
28103 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
28104 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
28105 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
28106 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
28107 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
28109 static tree
28110 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
28112 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
28113 const char *p_name = "#pragma omp parallel";
28114 tree stmt, clauses, par_clause, ws_clause, block;
28115 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
28116 unsigned int save;
28117 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28119 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
28121 cp_lexer_consume_token (parser->lexer);
28122 p_kind = PRAGMA_OMP_PARALLEL_FOR;
28123 p_name = "#pragma omp parallel for";
28124 mask |= OMP_FOR_CLAUSE_MASK;
28125 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
28127 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28129 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28130 const char *p = IDENTIFIER_POINTER (id);
28131 if (strcmp (p, "sections") == 0)
28133 cp_lexer_consume_token (parser->lexer);
28134 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
28135 p_name = "#pragma omp parallel sections";
28136 mask |= OMP_SECTIONS_CLAUSE_MASK;
28137 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
28141 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
28142 block = begin_omp_parallel ();
28143 save = cp_parser_begin_omp_structured_block (parser);
28145 switch (p_kind)
28147 case PRAGMA_OMP_PARALLEL:
28148 cp_parser_statement (parser, NULL_TREE, false, NULL);
28149 par_clause = clauses;
28150 break;
28152 case PRAGMA_OMP_PARALLEL_FOR:
28153 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
28154 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
28155 break;
28157 case PRAGMA_OMP_PARALLEL_SECTIONS:
28158 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
28159 stmt = cp_parser_omp_sections_scope (parser);
28160 if (stmt)
28161 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
28162 break;
28164 default:
28165 gcc_unreachable ();
28168 cp_parser_end_omp_structured_block (parser, save);
28169 stmt = finish_omp_parallel (par_clause, block);
28170 if (p_kind != PRAGMA_OMP_PARALLEL)
28171 OMP_PARALLEL_COMBINED (stmt) = 1;
28172 return stmt;
28175 /* OpenMP 2.5:
28176 # pragma omp single single-clause[optseq] new-line
28177 structured-block */
28179 #define OMP_SINGLE_CLAUSE_MASK \
28180 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
28181 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
28182 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
28183 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
28185 static tree
28186 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
28188 tree stmt = make_node (OMP_SINGLE);
28189 TREE_TYPE (stmt) = void_type_node;
28191 OMP_SINGLE_CLAUSES (stmt)
28192 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
28193 "#pragma omp single", pragma_tok);
28194 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
28196 return add_stmt (stmt);
28199 /* OpenMP 3.0:
28200 # pragma omp task task-clause[optseq] new-line
28201 structured-block */
28203 #define OMP_TASK_CLAUSE_MASK \
28204 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
28205 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
28206 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
28207 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
28208 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
28209 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
28210 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
28211 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
28213 static tree
28214 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
28216 tree clauses, block;
28217 unsigned int save;
28219 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
28220 "#pragma omp task", pragma_tok);
28221 block = begin_omp_task ();
28222 save = cp_parser_begin_omp_structured_block (parser);
28223 cp_parser_statement (parser, NULL_TREE, false, NULL);
28224 cp_parser_end_omp_structured_block (parser, save);
28225 return finish_omp_task (clauses, block);
28228 /* OpenMP 3.0:
28229 # pragma omp taskwait new-line */
28231 static void
28232 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
28234 cp_parser_require_pragma_eol (parser, pragma_tok);
28235 finish_omp_taskwait ();
28238 /* OpenMP 3.1:
28239 # pragma omp taskyield new-line */
28241 static void
28242 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
28244 cp_parser_require_pragma_eol (parser, pragma_tok);
28245 finish_omp_taskyield ();
28248 /* OpenMP 2.5:
28249 # pragma omp threadprivate (variable-list) */
28251 static void
28252 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
28254 tree vars;
28256 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28257 cp_parser_require_pragma_eol (parser, pragma_tok);
28259 finish_omp_threadprivate (vars);
28262 /* Main entry point to OpenMP statement pragmas. */
28264 static void
28265 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
28267 tree stmt;
28269 switch (pragma_tok->pragma_kind)
28271 case PRAGMA_OMP_ATOMIC:
28272 cp_parser_omp_atomic (parser, pragma_tok);
28273 return;
28274 case PRAGMA_OMP_CRITICAL:
28275 stmt = cp_parser_omp_critical (parser, pragma_tok);
28276 break;
28277 case PRAGMA_OMP_FOR:
28278 stmt = cp_parser_omp_for (parser, pragma_tok);
28279 break;
28280 case PRAGMA_OMP_MASTER:
28281 stmt = cp_parser_omp_master (parser, pragma_tok);
28282 break;
28283 case PRAGMA_OMP_ORDERED:
28284 stmt = cp_parser_omp_ordered (parser, pragma_tok);
28285 break;
28286 case PRAGMA_OMP_PARALLEL:
28287 stmt = cp_parser_omp_parallel (parser, pragma_tok);
28288 break;
28289 case PRAGMA_OMP_SECTIONS:
28290 stmt = cp_parser_omp_sections (parser, pragma_tok);
28291 break;
28292 case PRAGMA_OMP_SINGLE:
28293 stmt = cp_parser_omp_single (parser, pragma_tok);
28294 break;
28295 case PRAGMA_OMP_TASK:
28296 stmt = cp_parser_omp_task (parser, pragma_tok);
28297 break;
28298 default:
28299 gcc_unreachable ();
28302 if (stmt)
28303 SET_EXPR_LOCATION (stmt, pragma_tok->location);
28306 /* Transactional Memory parsing routines. */
28308 /* Parse a transaction attribute.
28310 txn-attribute:
28311 attribute
28312 [ [ identifier ] ]
28314 ??? Simplify this when C++0x bracket attributes are
28315 implemented properly. */
28317 static tree
28318 cp_parser_txn_attribute_opt (cp_parser *parser)
28320 cp_token *token;
28321 tree attr_name, attr = NULL;
28323 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28324 return cp_parser_attributes_opt (parser);
28326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
28327 return NULL_TREE;
28328 cp_lexer_consume_token (parser->lexer);
28329 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
28330 goto error1;
28332 token = cp_lexer_peek_token (parser->lexer);
28333 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
28335 token = cp_lexer_consume_token (parser->lexer);
28337 attr_name = (token->type == CPP_KEYWORD
28338 /* For keywords, use the canonical spelling,
28339 not the parsed identifier. */
28340 ? ridpointers[(int) token->keyword]
28341 : token->u.value);
28342 attr = build_tree_list (attr_name, NULL_TREE);
28344 else
28345 cp_parser_error (parser, "expected identifier");
28347 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28348 error1:
28349 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28350 return attr;
28353 /* Parse a __transaction_atomic or __transaction_relaxed statement.
28355 transaction-statement:
28356 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
28357 compound-statement
28358 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
28361 static tree
28362 cp_parser_transaction (cp_parser *parser, enum rid keyword)
28364 unsigned char old_in = parser->in_transaction;
28365 unsigned char this_in = 1, new_in;
28366 cp_token *token;
28367 tree stmt, attrs, noex;
28369 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
28370 || keyword == RID_TRANSACTION_RELAXED);
28371 token = cp_parser_require_keyword (parser, keyword,
28372 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
28373 : RT_TRANSACTION_RELAXED));
28374 gcc_assert (token != NULL);
28376 if (keyword == RID_TRANSACTION_RELAXED)
28377 this_in |= TM_STMT_ATTR_RELAXED;
28378 else
28380 attrs = cp_parser_txn_attribute_opt (parser);
28381 if (attrs)
28382 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
28385 /* Parse a noexcept specification. */
28386 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
28388 /* Keep track if we're in the lexical scope of an outer transaction. */
28389 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
28391 stmt = begin_transaction_stmt (token->location, NULL, this_in);
28393 parser->in_transaction = new_in;
28394 cp_parser_compound_statement (parser, NULL, false, false);
28395 parser->in_transaction = old_in;
28397 finish_transaction_stmt (stmt, NULL, this_in, noex);
28399 return stmt;
28402 /* Parse a __transaction_atomic or __transaction_relaxed expression.
28404 transaction-expression:
28405 __transaction_atomic txn-noexcept-spec[opt] ( expression )
28406 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
28409 static tree
28410 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
28412 unsigned char old_in = parser->in_transaction;
28413 unsigned char this_in = 1;
28414 cp_token *token;
28415 tree expr, noex;
28416 bool noex_expr;
28418 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
28419 || keyword == RID_TRANSACTION_RELAXED);
28421 if (!flag_tm)
28422 error (keyword == RID_TRANSACTION_RELAXED
28423 ? G_("%<__transaction_relaxed%> without transactional memory "
28424 "support enabled")
28425 : G_("%<__transaction_atomic%> without transactional memory "
28426 "support enabled"));
28428 token = cp_parser_require_keyword (parser, keyword,
28429 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
28430 : RT_TRANSACTION_RELAXED));
28431 gcc_assert (token != NULL);
28433 if (keyword == RID_TRANSACTION_RELAXED)
28434 this_in |= TM_STMT_ATTR_RELAXED;
28436 /* Set this early. This might mean that we allow transaction_cancel in
28437 an expression that we find out later actually has to be a constexpr.
28438 However, we expect that cxx_constant_value will be able to deal with
28439 this; also, if the noexcept has no constexpr, then what we parse next
28440 really is a transaction's body. */
28441 parser->in_transaction = this_in;
28443 /* Parse a noexcept specification. */
28444 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
28445 true);
28447 if (!noex || !noex_expr
28448 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28450 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28452 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28453 expr = finish_parenthesized_expr (expr);
28455 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28457 else
28459 /* The only expression that is available got parsed for the noexcept
28460 already. noexcept is true then. */
28461 expr = noex;
28462 noex = boolean_true_node;
28465 expr = build_transaction_expr (token->location, expr, this_in, noex);
28466 parser->in_transaction = old_in;
28468 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
28469 return error_mark_node;
28471 return (flag_tm ? expr : error_mark_node);
28474 /* Parse a function-transaction-block.
28476 function-transaction-block:
28477 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
28478 function-body
28479 __transaction_atomic txn-attribute[opt] function-try-block
28480 __transaction_relaxed ctor-initializer[opt] function-body
28481 __transaction_relaxed function-try-block
28484 static bool
28485 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
28487 unsigned char old_in = parser->in_transaction;
28488 unsigned char new_in = 1;
28489 tree compound_stmt, stmt, attrs;
28490 bool ctor_initializer_p;
28491 cp_token *token;
28493 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
28494 || keyword == RID_TRANSACTION_RELAXED);
28495 token = cp_parser_require_keyword (parser, keyword,
28496 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
28497 : RT_TRANSACTION_RELAXED));
28498 gcc_assert (token != NULL);
28500 if (keyword == RID_TRANSACTION_RELAXED)
28501 new_in |= TM_STMT_ATTR_RELAXED;
28502 else
28504 attrs = cp_parser_txn_attribute_opt (parser);
28505 if (attrs)
28506 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
28509 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
28511 parser->in_transaction = new_in;
28513 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28514 ctor_initializer_p = cp_parser_function_try_block (parser);
28515 else
28516 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
28517 (parser, /*in_function_try_block=*/false);
28519 parser->in_transaction = old_in;
28521 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
28523 return ctor_initializer_p;
28526 /* Parse a __transaction_cancel statement.
28528 cancel-statement:
28529 __transaction_cancel txn-attribute[opt] ;
28530 __transaction_cancel txn-attribute[opt] throw-expression ;
28532 ??? Cancel and throw is not yet implemented. */
28534 static tree
28535 cp_parser_transaction_cancel (cp_parser *parser)
28537 cp_token *token;
28538 bool is_outer = false;
28539 tree stmt, attrs;
28541 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
28542 RT_TRANSACTION_CANCEL);
28543 gcc_assert (token != NULL);
28545 attrs = cp_parser_txn_attribute_opt (parser);
28546 if (attrs)
28547 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
28549 /* ??? Parse cancel-and-throw here. */
28551 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28553 if (!flag_tm)
28555 error_at (token->location, "%<__transaction_cancel%> without "
28556 "transactional memory support enabled");
28557 return error_mark_node;
28559 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
28561 error_at (token->location, "%<__transaction_cancel%> within a "
28562 "%<__transaction_relaxed%>");
28563 return error_mark_node;
28565 else if (is_outer)
28567 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
28568 && !is_tm_may_cancel_outer (current_function_decl))
28570 error_at (token->location, "outer %<__transaction_cancel%> not "
28571 "within outer %<__transaction_atomic%>");
28572 error_at (token->location,
28573 " or a %<transaction_may_cancel_outer%> function");
28574 return error_mark_node;
28577 else if (parser->in_transaction == 0)
28579 error_at (token->location, "%<__transaction_cancel%> not within "
28580 "%<__transaction_atomic%>");
28581 return error_mark_node;
28584 stmt = build_tm_abort_call (token->location, is_outer);
28585 add_stmt (stmt);
28586 finish_stmt ();
28588 return stmt;
28591 /* The parser. */
28593 static GTY (()) cp_parser *the_parser;
28596 /* Special handling for the first token or line in the file. The first
28597 thing in the file might be #pragma GCC pch_preprocess, which loads a
28598 PCH file, which is a GC collection point. So we need to handle this
28599 first pragma without benefit of an existing lexer structure.
28601 Always returns one token to the caller in *FIRST_TOKEN. This is
28602 either the true first token of the file, or the first token after
28603 the initial pragma. */
28605 static void
28606 cp_parser_initial_pragma (cp_token *first_token)
28608 tree name = NULL;
28610 cp_lexer_get_preprocessor_token (NULL, first_token);
28611 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
28612 return;
28614 cp_lexer_get_preprocessor_token (NULL, first_token);
28615 if (first_token->type == CPP_STRING)
28617 name = first_token->u.value;
28619 cp_lexer_get_preprocessor_token (NULL, first_token);
28620 if (first_token->type != CPP_PRAGMA_EOL)
28621 error_at (first_token->location,
28622 "junk at end of %<#pragma GCC pch_preprocess%>");
28624 else
28625 error_at (first_token->location, "expected string literal");
28627 /* Skip to the end of the pragma. */
28628 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
28629 cp_lexer_get_preprocessor_token (NULL, first_token);
28631 /* Now actually load the PCH file. */
28632 if (name)
28633 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
28635 /* Read one more token to return to our caller. We have to do this
28636 after reading the PCH file in, since its pointers have to be
28637 live. */
28638 cp_lexer_get_preprocessor_token (NULL, first_token);
28641 /* Normal parsing of a pragma token. Here we can (and must) use the
28642 regular lexer. */
28644 static bool
28645 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
28647 cp_token *pragma_tok;
28648 unsigned int id;
28650 pragma_tok = cp_lexer_consume_token (parser->lexer);
28651 gcc_assert (pragma_tok->type == CPP_PRAGMA);
28652 parser->lexer->in_pragma = true;
28654 id = pragma_tok->pragma_kind;
28655 switch (id)
28657 case PRAGMA_GCC_PCH_PREPROCESS:
28658 error_at (pragma_tok->location,
28659 "%<#pragma GCC pch_preprocess%> must be first");
28660 break;
28662 case PRAGMA_OMP_BARRIER:
28663 switch (context)
28665 case pragma_compound:
28666 cp_parser_omp_barrier (parser, pragma_tok);
28667 return false;
28668 case pragma_stmt:
28669 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
28670 "used in compound statements");
28671 break;
28672 default:
28673 goto bad_stmt;
28675 break;
28677 case PRAGMA_OMP_FLUSH:
28678 switch (context)
28680 case pragma_compound:
28681 cp_parser_omp_flush (parser, pragma_tok);
28682 return false;
28683 case pragma_stmt:
28684 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
28685 "used in compound statements");
28686 break;
28687 default:
28688 goto bad_stmt;
28690 break;
28692 case PRAGMA_OMP_TASKWAIT:
28693 switch (context)
28695 case pragma_compound:
28696 cp_parser_omp_taskwait (parser, pragma_tok);
28697 return false;
28698 case pragma_stmt:
28699 error_at (pragma_tok->location,
28700 "%<#pragma omp taskwait%> may only be "
28701 "used in compound statements");
28702 break;
28703 default:
28704 goto bad_stmt;
28706 break;
28708 case PRAGMA_OMP_TASKYIELD:
28709 switch (context)
28711 case pragma_compound:
28712 cp_parser_omp_taskyield (parser, pragma_tok);
28713 return false;
28714 case pragma_stmt:
28715 error_at (pragma_tok->location,
28716 "%<#pragma omp taskyield%> may only be "
28717 "used in compound statements");
28718 break;
28719 default:
28720 goto bad_stmt;
28722 break;
28724 case PRAGMA_OMP_THREADPRIVATE:
28725 cp_parser_omp_threadprivate (parser, pragma_tok);
28726 return false;
28728 case PRAGMA_OMP_ATOMIC:
28729 case PRAGMA_OMP_CRITICAL:
28730 case PRAGMA_OMP_FOR:
28731 case PRAGMA_OMP_MASTER:
28732 case PRAGMA_OMP_ORDERED:
28733 case PRAGMA_OMP_PARALLEL:
28734 case PRAGMA_OMP_SECTIONS:
28735 case PRAGMA_OMP_SINGLE:
28736 case PRAGMA_OMP_TASK:
28737 if (context == pragma_external)
28738 goto bad_stmt;
28739 cp_parser_omp_construct (parser, pragma_tok);
28740 return true;
28742 case PRAGMA_OMP_SECTION:
28743 error_at (pragma_tok->location,
28744 "%<#pragma omp section%> may only be used in "
28745 "%<#pragma omp sections%> construct");
28746 break;
28748 case PRAGMA_CILK_SIMD:
28749 if (context == pragma_external)
28751 error_at (pragma_tok->location,
28752 "%<#pragma simd%> must be inside a function");
28753 break;
28755 cp_parser_cilk_simd_construct (parser, pragma_tok);
28756 return true;
28758 default:
28759 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
28760 c_invoke_pragma_handler (id);
28761 break;
28763 bad_stmt:
28764 cp_parser_error (parser, "expected declaration specifiers");
28765 break;
28768 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28769 return false;
28772 /* The interface the pragma parsers have to the lexer. */
28774 enum cpp_ttype
28775 pragma_lex (tree *value)
28777 cp_token *tok;
28778 enum cpp_ttype ret;
28780 tok = cp_lexer_peek_token (the_parser->lexer);
28782 ret = tok->type;
28783 *value = tok->u.value;
28785 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
28786 ret = CPP_EOF;
28787 else if (ret == CPP_STRING)
28788 *value = cp_parser_string_literal (the_parser, false, false);
28789 else
28791 cp_lexer_consume_token (the_parser->lexer);
28792 if (ret == CPP_KEYWORD)
28793 ret = CPP_NAME;
28796 return ret;
28800 /* External interface. */
28802 /* Parse one entire translation unit. */
28804 void
28805 c_parse_file (void)
28807 static bool already_called = false;
28809 if (already_called)
28811 sorry ("inter-module optimizations not implemented for C++");
28812 return;
28814 already_called = true;
28816 the_parser = cp_parser_new ();
28817 push_deferring_access_checks (flag_access_control
28818 ? dk_no_deferred : dk_no_check);
28819 cp_parser_translation_unit (the_parser);
28820 the_parser = NULL;
28824 /* Parses the Cilk Plus #pragma simd vectorlength clause:
28825 Syntax:
28826 vectorlength ( constant-expression ) */
28828 static tree
28829 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses)
28831 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28832 tree expr;
28833 /* The vectorlength clause behaves exactly like OpenMP's safelen
28834 clause. Thus, vectorlength is represented as OMP 4.0
28835 safelen. */
28836 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength", loc);
28838 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28839 return error_mark_node;
28841 expr = cp_parser_constant_expression (parser, false, NULL);
28842 expr = maybe_constant_value (expr);
28844 if (TREE_CONSTANT (expr)
28845 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
28846 error_at (loc, "vectorlength must be a power of 2");
28847 else if (expr != error_mark_node)
28849 tree c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
28850 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
28851 OMP_CLAUSE_CHAIN (c) = clauses;
28852 clauses = c;
28855 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28856 return error_mark_node;
28857 return clauses;
28860 /* Handles the Cilk Plus #pragma simd linear clause.
28861 Syntax:
28862 linear ( simd-linear-variable-list )
28864 simd-linear-variable-list:
28865 simd-linear-variable
28866 simd-linear-variable-list , simd-linear-variable
28868 simd-linear-variable:
28869 id-expression
28870 id-expression : simd-linear-step
28872 simd-linear-step:
28873 conditional-expression */
28875 static tree
28876 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
28878 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28880 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28881 return clauses;
28882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28884 cp_parser_error (parser, "expected identifier");
28885 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
28886 return error_mark_node;
28889 while (1)
28891 cp_token *token = cp_lexer_peek_token (parser->lexer);
28892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28894 cp_parser_error (parser, "expected variable-name");
28895 clauses = error_mark_node;
28896 break;
28899 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
28900 false, false);
28901 tree decl = cp_parser_lookup_name_simple (parser, var_name,
28902 token->location);
28903 if (decl == error_mark_node)
28905 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
28906 token->location);
28907 clauses = error_mark_node;
28909 else
28911 tree e = NULL_TREE;
28912 tree step_size = integer_one_node;
28914 /* If present, parse the linear step. Otherwise, assume the default
28915 value of 1. */
28916 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
28918 cp_lexer_consume_token (parser->lexer);
28920 e = cp_parser_constant_expression (parser, false, NULL);
28921 e = maybe_constant_value (e);
28923 if (e == error_mark_node)
28925 /* If an error has occurred, then the whole pragma is
28926 considered ill-formed. Thus, no reason to keep
28927 parsing. */
28928 clauses = error_mark_node;
28929 break;
28931 else if (!TREE_TYPE (e) || !TREE_CONSTANT (e)
28932 || !INTEGRAL_TYPE_P (TREE_TYPE (e)))
28933 cp_parser_error (parser,
28934 "step size must be an integer constant");
28935 else
28936 step_size = e;
28939 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
28940 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
28941 OMP_CLAUSE_DECL (l) = decl;
28942 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
28943 OMP_CLAUSE_CHAIN (l) = clauses;
28944 clauses = l;
28946 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28947 cp_lexer_consume_token (parser->lexer);
28948 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28949 break;
28950 else
28952 error_at (cp_lexer_peek_token (parser->lexer)->location,
28953 "expected %<,%> or %<)%> after %qE", decl);
28954 clauses = error_mark_node;
28955 break;
28958 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
28959 return clauses;
28962 /* Returns the name of the next clause. If the clause is not
28963 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
28964 token is not consumed. Otherwise, the appropriate enum from the
28965 pragma_simd_clause is returned and the token is consumed. */
28967 static pragma_cilk_clause
28968 cp_parser_cilk_simd_clause_name (cp_parser *parser)
28970 pragma_cilk_clause clause_type;
28971 cp_token *token = cp_lexer_peek_token (parser->lexer);
28973 if (token->keyword == RID_PRIVATE)
28974 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
28975 else if (!token->u.value || token->type != CPP_NAME)
28976 return PRAGMA_CILK_CLAUSE_NONE;
28977 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
28978 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
28979 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
28980 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
28981 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
28982 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
28983 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
28984 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
28985 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
28986 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
28987 else
28988 return PRAGMA_CILK_CLAUSE_NONE;
28990 cp_lexer_consume_token (parser->lexer);
28991 return clause_type;
28994 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
28996 static tree
28997 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
28999 tree clauses = NULL_TREE;
29001 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
29002 && clauses != error_mark_node)
29004 pragma_cilk_clause c_kind;
29005 c_kind = cp_parser_cilk_simd_clause_name (parser);
29006 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
29007 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses);
29008 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
29009 clauses = cp_parser_cilk_simd_linear (parser, clauses);
29010 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
29011 /* Use the OpenMP 4.0 equivalent function. */
29012 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
29013 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
29014 /* Use the OpenMP 4.0 equivalent function. */
29015 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29016 clauses);
29017 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
29018 /* Use the OMP 4.0 equivalent function. */
29019 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29020 clauses);
29021 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
29022 /* Use the OMP 4.0 equivalent function. */
29023 clauses = cp_parser_omp_clause_reduction (parser, clauses);
29024 else
29026 clauses = error_mark_node;
29027 cp_parser_error (parser, "expected %<#pragma simd%> clause");
29028 break;
29032 cp_parser_skip_to_pragma_eol (parser, pragma_token);
29034 if (clauses == error_mark_node)
29035 return error_mark_node;
29036 else
29037 return c_finish_cilk_clauses (clauses);
29040 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
29042 static void
29043 cp_parser_cilk_simd_construct (cp_parser *parser, cp_token *pragma_token)
29045 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
29047 if (clauses == error_mark_node)
29048 return;
29050 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
29052 error_at (cp_lexer_peek_token (parser->lexer)->location,
29053 "for statement expected");
29054 return;
29057 tree sb = begin_omp_structured_block ();
29058 int save = cp_parser_begin_omp_structured_block (parser);
29059 cp_parser_cilk_for (parser, RID_FOR, clauses);
29060 cp_parser_end_omp_structured_block (parser, save);
29061 add_stmt (finish_omp_structured_block (sb));
29062 return;
29065 /* Parses the initializer of a for/_Cilk_for statement. The initial
29066 value is stored in *INIT, and the inital value's declaration is
29067 stored as DECL_EXPR in *PRE_BODY. */
29069 static tree
29070 cp_parser_simd_for_init_statement (cp_parser *parser, tree *init,
29071 tree *pre_body)
29073 cp_token *token = cp_lexer_peek_token (parser->lexer);
29074 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29075 tree decl = NULL_TREE;
29076 cp_decl_specifier_seq type_specifiers;
29077 tree this_pre_body = push_stmt_list ();
29078 if (token->type == CPP_SEMICOLON)
29080 error_at (loc, "expected induction variable");
29081 return error_mark_node;
29084 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC)
29085 || cp_lexer_next_token_is_keyword (parser->lexer, RID_REGISTER)
29086 || cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTERN)
29087 || cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE)
29088 || cp_lexer_next_token_is_keyword (parser->lexer, RID_THREAD))
29090 error_at (loc, "storage class is not allowed");
29091 cp_lexer_consume_token (parser->lexer);
29094 cp_parser_parse_tentatively (parser);
29095 cp_parser_type_specifier_seq (parser, true, false, &type_specifiers);
29096 if (cp_parser_parse_definitely (parser))
29098 cp_declarator *cp_decl;
29099 tree asm_spec, attr;
29100 cp_decl = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29101 NULL, NULL, false);
29102 attr = cp_parser_attributes_opt (parser);
29103 asm_spec = cp_parser_asm_specification_opt (parser);
29104 if (cp_decl == cp_error_declarator)
29105 cp_parser_skip_to_end_of_statement (parser);
29106 else
29108 tree pushed_scope, auto_node;
29109 decl = start_decl (cp_decl, &type_specifiers, SD_INITIALIZED, attr,
29110 NULL_TREE, &pushed_scope);
29111 auto_node = type_uses_auto (TREE_TYPE (decl));
29112 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29115 error_at (loc, "parenthesized initialization is "
29116 "not allowed in for-loop");
29117 else
29119 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29120 decl = error_mark_node;
29123 *init = error_mark_node;
29124 cp_parser_skip_to_end_of_statement (parser);
29126 else if (CLASS_TYPE_P (TREE_TYPE (decl)) || auto_node
29127 || type_dependent_expression_p (decl))
29129 bool is_direct_init, is_non_constant_init;
29130 *init = cp_parser_initializer (parser, &is_direct_init,
29131 &is_non_constant_init);
29132 if (auto_node)
29134 TREE_TYPE (decl)
29135 = do_auto_deduction (TREE_TYPE (decl), *init, auto_node);
29136 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29137 && !type_dependent_expression_p (decl))
29138 goto non_class;
29140 cp_finish_decl (decl, *init, !is_non_constant_init, asm_spec,
29141 LOOKUP_ONLYCONVERTING);
29142 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29143 *init = NULL_TREE;
29144 else
29145 *init = pop_stmt_list (this_pre_body);
29146 this_pre_body = NULL_TREE;
29148 else
29150 /* Consume the '='. */
29151 cp_lexer_consume_token (parser->lexer);
29152 *init = cp_parser_assignment_expression (parser, false, NULL);
29153 non_class:
29154 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29155 *init = error_mark_node;
29156 else
29157 cp_finish_decl (decl, NULL_TREE, false, asm_spec,
29158 LOOKUP_ONLYCONVERTING);
29159 if (decl != error_mark_node)
29160 DECL_INITIAL (decl) = (*init || *init != error_mark_node) ?
29161 *init : NULL_TREE;
29163 if (pushed_scope)
29164 pop_scope (pushed_scope);
29167 else
29169 cp_id_kind idk;
29170 cp_parser_parse_tentatively (parser);
29171 decl = cp_parser_primary_expression (parser, false, false,
29172 false, &idk);
29173 if (!cp_parser_error_occurred (parser) && decl && DECL_P (decl)
29174 && CLASS_TYPE_P (TREE_TYPE (decl)))
29176 tree rhs, new_expr;
29177 // ?? FIXME: I don't see any definition for *init in this
29178 // code path. ??
29179 gcc_unreachable ();
29180 cp_parser_parse_definitely (parser);
29181 cp_parser_require (parser, CPP_EQ, RT_EQ);
29182 rhs = cp_parser_assignment_expression (parser, false, NULL);
29183 new_expr = build_x_modify_expr (EXPR_LOCATION (rhs), decl,
29184 NOP_EXPR, rhs,
29185 tf_warning_or_error);
29186 finish_expr_stmt (new_expr);
29188 else
29190 if (decl != error_mark_node)
29191 decl = NULL;
29192 cp_parser_abort_tentative_parse (parser);
29193 *init = cp_parser_expression (parser, false, NULL);
29197 if (this_pre_body)
29198 this_pre_body = pop_stmt_list (this_pre_body);
29200 *pre_body = this_pre_body;
29201 return decl;
29204 /* Top-level function to parse _Cilk_for and the for statement
29205 following <#pragma simd>. */
29207 static tree
29208 cp_parser_cilk_for (cp_parser *parser, enum rid for_keyword, tree clauses)
29210 bool valid = true;
29211 tree cond = NULL_TREE;
29212 tree incr_expr = NULL_TREE;
29213 tree init = NULL_TREE, pre_body = NULL_TREE, decl;
29214 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29216 gcc_assert (for_keyword == RID_FOR);
29218 if (!cp_lexer_next_token_is_keyword (parser->lexer, for_keyword))
29220 if (for_keyword == RID_FOR)
29221 cp_parser_error (parser, "for statement expected");
29222 else
29223 cp_parser_error (parser, "_Cilk_for statement expected");
29224 return error_mark_node;
29226 cp_lexer_consume_token (parser->lexer);
29228 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29230 cp_parser_skip_to_end_of_statement (parser);
29231 return error_mark_node;
29234 /* Parse initialization. */
29235 if (for_keyword == RID_FOR)
29236 decl = cp_parser_simd_for_init_statement (parser, &init, &pre_body);
29238 if (decl == error_mark_node)
29239 valid = false;
29240 else if (!decl || (TREE_CODE (decl) != VAR_DECL
29241 && TREE_CODE (decl) != DECL_EXPR))
29243 error_at (loc, "%s-loop initializer does not declare a variable",
29244 for_keyword == RID_FOR ? "for" : "_Cilk_for");
29245 valid = false;
29246 decl = error_mark_node;
29248 else if (!processing_template_decl
29249 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl)
29250 && !DECL_INITIAL (decl)
29251 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
29253 error_at (loc, "control variable for the %s-loop needs to "
29254 "be initialized",
29255 for_keyword == RID_FOR ? "for" : "_Cilk_for");
29256 valid = false;
29258 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29260 error_at (loc, "%s-loop initializer cannot have multiple variable "
29261 "declarations", for_keyword == RID_FOR ? "for" : "_Cilk_for");
29262 cp_parser_skip_to_end_of_statement (parser);
29263 valid = false;
29266 if (!valid)
29268 /* Skip to the semicolon ending the init. */
29269 cp_parser_skip_to_end_of_statement (parser);
29272 /* Parse condition. */
29273 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29274 return error_mark_node;
29275 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29277 error_at (loc, "missing condition");
29278 cond = error_mark_node;
29280 else
29282 cond = cp_parser_condition (parser);
29283 cond = finish_cilk_for_cond (cond);
29286 if (cond == error_mark_node)
29287 valid = false;
29288 cp_parser_consume_semicolon_at_end_of_statement (parser);
29290 /* Parse increment. */
29291 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29293 error_at (loc, "missing increment");
29294 incr_expr = error_mark_node;
29296 else
29297 incr_expr = cp_parser_expression (parser, false, NULL);
29299 if (incr_expr == error_mark_node)
29301 cp_parser_skip_to_closing_parenthesis (parser, true, false, false);
29302 valid = false;
29305 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29307 cp_parser_skip_to_end_of_statement (parser);
29308 valid = false;
29311 if (!valid)
29313 gcc_assert (sorrycount || errorcount);
29314 return error_mark_node;
29317 if (for_keyword == RID_FOR)
29319 parser->in_statement = IN_CILK_P_SIMD_FOR;
29320 tree body = push_stmt_list ();
29321 cp_parser_statement (parser, NULL_TREE, false, NULL);
29322 body = pop_stmt_list (body);
29324 /* Check if the body satisfies all the requirement of a #pragma
29325 simd for body. If it is invalid, then do not make the OpenMP
29326 nodes, just return an error mark node. */
29327 if (!cpp_validate_cilk_plus_loop (body))
29328 return error_mark_node;
29330 return c_finish_cilk_simd_loop (loc, decl, init, cond, incr_expr,
29331 body, clauses,
29332 /*scan_body=*/false);
29334 else
29336 /* Handle _Cilk_for here when implemented. */
29337 gcc_unreachable ();
29338 return NULL_TREE;
29342 #include "gt-cp-parser.h"