PR c++/64487
[official-gcc.git] / gcc / cp / parser.c
blob22dff06eb6c7a87b6b6221c41fc5dab76fb96ae4
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2015 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 "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "hash-map.h"
40 #include "is-a.h"
41 #include "plugin-api.h"
42 #include "vec.h"
43 #include "hashtab.h"
44 #include "hash-set.h"
45 #include "machmode.h"
46 #include "hard-reg-set.h"
47 #include "input.h"
48 #include "function.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "c-family/c-common.h"
52 #include "c-family/c-objc.h"
53 #include "plugin.h"
54 #include "tree-pretty-print.h"
55 #include "parser.h"
56 #include "type-utils.h"
57 #include "omp-low.h"
60 /* The lexer. */
62 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
63 and c-lex.c) and the C++ parser. */
65 static cp_token eof_token =
67 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
70 /* The various kinds of non integral constant we encounter. */
71 typedef enum non_integral_constant {
72 NIC_NONE,
73 /* floating-point literal */
74 NIC_FLOAT,
75 /* %<this%> */
76 NIC_THIS,
77 /* %<__FUNCTION__%> */
78 NIC_FUNC_NAME,
79 /* %<__PRETTY_FUNCTION__%> */
80 NIC_PRETTY_FUNC,
81 /* %<__func__%> */
82 NIC_C99_FUNC,
83 /* "%<va_arg%> */
84 NIC_VA_ARG,
85 /* a cast */
86 NIC_CAST,
87 /* %<typeid%> operator */
88 NIC_TYPEID,
89 /* non-constant compound literals */
90 NIC_NCC,
91 /* a function call */
92 NIC_FUNC_CALL,
93 /* an increment */
94 NIC_INC,
95 /* an decrement */
96 NIC_DEC,
97 /* an array reference */
98 NIC_ARRAY_REF,
99 /* %<->%> */
100 NIC_ARROW,
101 /* %<.%> */
102 NIC_POINT,
103 /* the address of a label */
104 NIC_ADDR_LABEL,
105 /* %<*%> */
106 NIC_STAR,
107 /* %<&%> */
108 NIC_ADDR,
109 /* %<++%> */
110 NIC_PREINCREMENT,
111 /* %<--%> */
112 NIC_PREDECREMENT,
113 /* %<new%> */
114 NIC_NEW,
115 /* %<delete%> */
116 NIC_DEL,
117 /* calls to overloaded operators */
118 NIC_OVERLOADED,
119 /* an assignment */
120 NIC_ASSIGNMENT,
121 /* a comma operator */
122 NIC_COMMA,
123 /* a call to a constructor */
124 NIC_CONSTRUCTOR,
125 /* a transaction expression */
126 NIC_TRANSACTION
127 } non_integral_constant;
129 /* The various kinds of errors about name-lookup failing. */
130 typedef enum name_lookup_error {
131 /* NULL */
132 NLE_NULL,
133 /* is not a type */
134 NLE_TYPE,
135 /* is not a class or namespace */
136 NLE_CXX98,
137 /* is not a class, namespace, or enumeration */
138 NLE_NOT_CXX98
139 } name_lookup_error;
141 /* The various kinds of required token */
142 typedef enum required_token {
143 RT_NONE,
144 RT_SEMICOLON, /* ';' */
145 RT_OPEN_PAREN, /* '(' */
146 RT_CLOSE_BRACE, /* '}' */
147 RT_OPEN_BRACE, /* '{' */
148 RT_CLOSE_SQUARE, /* ']' */
149 RT_OPEN_SQUARE, /* '[' */
150 RT_COMMA, /* ',' */
151 RT_SCOPE, /* '::' */
152 RT_LESS, /* '<' */
153 RT_GREATER, /* '>' */
154 RT_EQ, /* '=' */
155 RT_ELLIPSIS, /* '...' */
156 RT_MULT, /* '*' */
157 RT_COMPL, /* '~' */
158 RT_COLON, /* ':' */
159 RT_COLON_SCOPE, /* ':' or '::' */
160 RT_CLOSE_PAREN, /* ')' */
161 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
162 RT_PRAGMA_EOL, /* end of line */
163 RT_NAME, /* identifier */
165 /* The type is CPP_KEYWORD */
166 RT_NEW, /* new */
167 RT_DELETE, /* delete */
168 RT_RETURN, /* return */
169 RT_WHILE, /* while */
170 RT_EXTERN, /* extern */
171 RT_STATIC_ASSERT, /* static_assert */
172 RT_DECLTYPE, /* decltype */
173 RT_OPERATOR, /* operator */
174 RT_CLASS, /* class */
175 RT_TEMPLATE, /* template */
176 RT_NAMESPACE, /* namespace */
177 RT_USING, /* using */
178 RT_ASM, /* asm */
179 RT_TRY, /* try */
180 RT_CATCH, /* catch */
181 RT_THROW, /* throw */
182 RT_LABEL, /* __label__ */
183 RT_AT_TRY, /* @try */
184 RT_AT_SYNCHRONIZED, /* @synchronized */
185 RT_AT_THROW, /* @throw */
187 RT_SELECT, /* selection-statement */
188 RT_INTERATION, /* iteration-statement */
189 RT_JUMP, /* jump-statement */
190 RT_CLASS_KEY, /* class-key */
191 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
192 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
193 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
194 RT_TRANSACTION_CANCEL /* __transaction_cancel */
195 } required_token;
197 /* Prototypes. */
199 static cp_lexer *cp_lexer_new_main
200 (void);
201 static cp_lexer *cp_lexer_new_from_tokens
202 (cp_token_cache *tokens);
203 static void cp_lexer_destroy
204 (cp_lexer *);
205 static int cp_lexer_saving_tokens
206 (const cp_lexer *);
207 static cp_token *cp_lexer_token_at
208 (cp_lexer *, cp_token_position);
209 static void cp_lexer_get_preprocessor_token
210 (cp_lexer *, cp_token *);
211 static inline cp_token *cp_lexer_peek_token
212 (cp_lexer *);
213 static cp_token *cp_lexer_peek_nth_token
214 (cp_lexer *, size_t);
215 static inline bool cp_lexer_next_token_is
216 (cp_lexer *, enum cpp_ttype);
217 static bool cp_lexer_next_token_is_not
218 (cp_lexer *, enum cpp_ttype);
219 static bool cp_lexer_next_token_is_keyword
220 (cp_lexer *, enum rid);
221 static cp_token *cp_lexer_consume_token
222 (cp_lexer *);
223 static void cp_lexer_purge_token
224 (cp_lexer *);
225 static void cp_lexer_purge_tokens_after
226 (cp_lexer *, cp_token_position);
227 static void cp_lexer_save_tokens
228 (cp_lexer *);
229 static void cp_lexer_commit_tokens
230 (cp_lexer *);
231 static void cp_lexer_rollback_tokens
232 (cp_lexer *);
233 static void cp_lexer_print_token
234 (FILE *, cp_token *);
235 static inline bool cp_lexer_debugging_p
236 (cp_lexer *);
237 static void cp_lexer_start_debugging
238 (cp_lexer *) ATTRIBUTE_UNUSED;
239 static void cp_lexer_stop_debugging
240 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static cp_token_cache *cp_token_cache_new
243 (cp_token *, cp_token *);
245 static void cp_parser_initial_pragma
246 (cp_token *);
248 static tree cp_literal_operator_id
249 (const char *);
251 static void cp_parser_cilk_simd
252 (cp_parser *, cp_token *);
253 static tree cp_parser_cilk_for
254 (cp_parser *, tree);
255 static bool cp_parser_omp_declare_reduction_exprs
256 (tree, cp_parser *);
257 static tree cp_parser_cilk_simd_vectorlength
258 (cp_parser *, tree, bool);
260 /* Manifest constants. */
261 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
262 #define CP_SAVED_TOKEN_STACK 5
264 /* Variables. */
266 /* The stream to which debugging output should be written. */
267 static FILE *cp_lexer_debug_stream;
269 /* Nonzero if we are parsing an unevaluated operand: an operand to
270 sizeof, typeof, or alignof. */
271 int cp_unevaluated_operand;
273 /* Dump up to NUM tokens in BUFFER to FILE starting with token
274 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
275 first token in BUFFER. If NUM is 0, dump all the tokens. If
276 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
277 highlighted by surrounding it in [[ ]]. */
279 static void
280 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
281 cp_token *start_token, unsigned num,
282 cp_token *curr_token)
284 unsigned i, nprinted;
285 cp_token *token;
286 bool do_print;
288 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
290 if (buffer == NULL)
291 return;
293 if (num == 0)
294 num = buffer->length ();
296 if (start_token == NULL)
297 start_token = buffer->address ();
299 if (start_token > buffer->address ())
301 cp_lexer_print_token (file, &(*buffer)[0]);
302 fprintf (file, " ... ");
305 do_print = false;
306 nprinted = 0;
307 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
309 if (token == start_token)
310 do_print = true;
312 if (!do_print)
313 continue;
315 nprinted++;
316 if (token == curr_token)
317 fprintf (file, "[[");
319 cp_lexer_print_token (file, token);
321 if (token == curr_token)
322 fprintf (file, "]]");
324 switch (token->type)
326 case CPP_SEMICOLON:
327 case CPP_OPEN_BRACE:
328 case CPP_CLOSE_BRACE:
329 case CPP_EOF:
330 fputc ('\n', file);
331 break;
333 default:
334 fputc (' ', file);
338 if (i == num && i < buffer->length ())
340 fprintf (file, " ... ");
341 cp_lexer_print_token (file, &buffer->last ());
344 fprintf (file, "\n");
348 /* Dump all tokens in BUFFER to stderr. */
350 void
351 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
353 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
356 DEBUG_FUNCTION void
357 debug (vec<cp_token, va_gc> &ref)
359 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
362 DEBUG_FUNCTION void
363 debug (vec<cp_token, va_gc> *ptr)
365 if (ptr)
366 debug (*ptr);
367 else
368 fprintf (stderr, "<nil>\n");
372 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
373 description for T. */
375 static void
376 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
378 if (t)
380 fprintf (file, "%s: ", desc);
381 print_node_brief (file, "", t, 0);
386 /* Dump parser context C to FILE. */
388 static void
389 cp_debug_print_context (FILE *file, cp_parser_context *c)
391 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
392 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
393 print_node_brief (file, "", c->object_type, 0);
394 fprintf (file, "}\n");
398 /* Print the stack of parsing contexts to FILE starting with FIRST. */
400 static void
401 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
403 unsigned i;
404 cp_parser_context *c;
406 fprintf (file, "Parsing context stack:\n");
407 for (i = 0, c = first; c; c = c->next, i++)
409 fprintf (file, "\t#%u: ", i);
410 cp_debug_print_context (file, c);
415 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
417 static void
418 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
420 if (flag)
421 fprintf (file, "%s: true\n", desc);
425 /* Print an unparsed function entry UF to FILE. */
427 static void
428 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
430 unsigned i;
431 cp_default_arg_entry *default_arg_fn;
432 tree fn;
434 fprintf (file, "\tFunctions with default args:\n");
435 for (i = 0;
436 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
437 i++)
439 fprintf (file, "\t\tClass type: ");
440 print_node_brief (file, "", default_arg_fn->class_type, 0);
441 fprintf (file, "\t\tDeclaration: ");
442 print_node_brief (file, "", default_arg_fn->decl, 0);
443 fprintf (file, "\n");
446 fprintf (file, "\n\tFunctions with definitions that require "
447 "post-processing\n\t\t");
448 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
450 print_node_brief (file, "", fn, 0);
451 fprintf (file, " ");
453 fprintf (file, "\n");
455 fprintf (file, "\n\tNon-static data members with initializers that require "
456 "post-processing\n\t\t");
457 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
459 print_node_brief (file, "", fn, 0);
460 fprintf (file, " ");
462 fprintf (file, "\n");
466 /* Print the stack of unparsed member functions S to FILE. */
468 static void
469 cp_debug_print_unparsed_queues (FILE *file,
470 vec<cp_unparsed_functions_entry, va_gc> *s)
472 unsigned i;
473 cp_unparsed_functions_entry *uf;
475 fprintf (file, "Unparsed functions\n");
476 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
478 fprintf (file, "#%u:\n", i);
479 cp_debug_print_unparsed_function (file, uf);
484 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
485 the given PARSER. If FILE is NULL, the output is printed on stderr. */
487 static void
488 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
490 cp_token *next_token, *first_token, *start_token;
492 if (file == NULL)
493 file = stderr;
495 next_token = parser->lexer->next_token;
496 first_token = parser->lexer->buffer->address ();
497 start_token = (next_token > first_token + window_size / 2)
498 ? next_token - window_size / 2
499 : first_token;
500 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
501 next_token);
505 /* Dump debugging information for the given PARSER. If FILE is NULL,
506 the output is printed on stderr. */
508 void
509 cp_debug_parser (FILE *file, cp_parser *parser)
511 const size_t window_size = 20;
512 cp_token *token;
513 expanded_location eloc;
515 if (file == NULL)
516 file = stderr;
518 fprintf (file, "Parser state\n\n");
519 fprintf (file, "Number of tokens: %u\n",
520 vec_safe_length (parser->lexer->buffer));
521 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
522 cp_debug_print_tree_if_set (file, "Object scope",
523 parser->object_scope);
524 cp_debug_print_tree_if_set (file, "Qualifying scope",
525 parser->qualifying_scope);
526 cp_debug_print_context_stack (file, parser->context);
527 cp_debug_print_flag (file, "Allow GNU extensions",
528 parser->allow_gnu_extensions_p);
529 cp_debug_print_flag (file, "'>' token is greater-than",
530 parser->greater_than_is_operator_p);
531 cp_debug_print_flag (file, "Default args allowed in current "
532 "parameter list", parser->default_arg_ok_p);
533 cp_debug_print_flag (file, "Parsing integral constant-expression",
534 parser->integral_constant_expression_p);
535 cp_debug_print_flag (file, "Allow non-constant expression in current "
536 "constant-expression",
537 parser->allow_non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Seen non-constant expression",
539 parser->non_integral_constant_expression_p);
540 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
541 "current context",
542 parser->local_variables_forbidden_p);
543 cp_debug_print_flag (file, "In unbraced linkage specification",
544 parser->in_unbraced_linkage_specification_p);
545 cp_debug_print_flag (file, "Parsing a declarator",
546 parser->in_declarator_p);
547 cp_debug_print_flag (file, "In template argument list",
548 parser->in_template_argument_list_p);
549 cp_debug_print_flag (file, "Parsing an iteration statement",
550 parser->in_statement & IN_ITERATION_STMT);
551 cp_debug_print_flag (file, "Parsing a switch statement",
552 parser->in_statement & IN_SWITCH_STMT);
553 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
554 parser->in_statement & IN_OMP_BLOCK);
555 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
556 parser->in_statement & IN_CILK_SIMD_FOR);
557 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
558 parser->in_statement & IN_OMP_FOR);
559 cp_debug_print_flag (file, "Parsing an if statement",
560 parser->in_statement & IN_IF_STMT);
561 cp_debug_print_flag (file, "Parsing a type-id in an expression "
562 "context", parser->in_type_id_in_expr_p);
563 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
564 parser->implicit_extern_c);
565 cp_debug_print_flag (file, "String expressions should be translated "
566 "to execution character set",
567 parser->translate_strings_p);
568 cp_debug_print_flag (file, "Parsing function body outside of a "
569 "local class", parser->in_function_body);
570 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
571 parser->colon_corrects_to_scope_p);
572 cp_debug_print_flag (file, "Colon doesn't start a class definition",
573 parser->colon_doesnt_start_class_def_p);
574 if (parser->type_definition_forbidden_message)
575 fprintf (file, "Error message for forbidden type definitions: %s\n",
576 parser->type_definition_forbidden_message);
577 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
578 fprintf (file, "Number of class definitions in progress: %u\n",
579 parser->num_classes_being_defined);
580 fprintf (file, "Number of template parameter lists for the current "
581 "declaration: %u\n", parser->num_template_parameter_lists);
582 cp_debug_parser_tokens (file, parser, window_size);
583 token = parser->lexer->next_token;
584 fprintf (file, "Next token to parse:\n");
585 fprintf (file, "\tToken: ");
586 cp_lexer_print_token (file, token);
587 eloc = expand_location (token->location);
588 fprintf (file, "\n\tFile: %s\n", eloc.file);
589 fprintf (file, "\tLine: %d\n", eloc.line);
590 fprintf (file, "\tColumn: %d\n", eloc.column);
593 DEBUG_FUNCTION void
594 debug (cp_parser &ref)
596 cp_debug_parser (stderr, &ref);
599 DEBUG_FUNCTION void
600 debug (cp_parser *ptr)
602 if (ptr)
603 debug (*ptr);
604 else
605 fprintf (stderr, "<nil>\n");
608 /* Allocate memory for a new lexer object and return it. */
610 static cp_lexer *
611 cp_lexer_alloc (void)
613 cp_lexer *lexer;
615 c_common_no_more_pch ();
617 /* Allocate the memory. */
618 lexer = ggc_cleared_alloc<cp_lexer> ();
620 /* Initially we are not debugging. */
621 lexer->debugging_p = false;
623 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
625 /* Create the buffer. */
626 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
628 return lexer;
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
633 preprocessor. */
635 static cp_lexer *
636 cp_lexer_new_main (void)
638 cp_lexer *lexer;
639 cp_token token;
641 /* It's possible that parsing the first pragma will load a PCH file,
642 which is a GC collection point. So we have to do that before
643 allocating any memory. */
644 cp_parser_initial_pragma (&token);
646 lexer = cp_lexer_alloc ();
648 /* Put the first token in the buffer. */
649 lexer->buffer->quick_push (token);
651 /* Get the remaining tokens from the preprocessor. */
652 while (token.type != CPP_EOF)
654 cp_lexer_get_preprocessor_token (lexer, &token);
655 vec_safe_push (lexer->buffer, token);
658 lexer->last_token = lexer->buffer->address ()
659 + lexer->buffer->length ()
660 - 1;
661 lexer->next_token = lexer->buffer->length ()
662 ? lexer->buffer->address ()
663 : &eof_token;
665 /* Subsequent preprocessor diagnostics should use compiler
666 diagnostic functions to get the compiler source location. */
667 done_lexing = true;
669 gcc_assert (!lexer->next_token->purged_p);
670 return lexer;
673 /* Create a new lexer whose token stream is primed with the tokens in
674 CACHE. When these tokens are exhausted, no new tokens will be read. */
676 static cp_lexer *
677 cp_lexer_new_from_tokens (cp_token_cache *cache)
679 cp_token *first = cache->first;
680 cp_token *last = cache->last;
681 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
683 /* We do not own the buffer. */
684 lexer->buffer = NULL;
685 lexer->next_token = first == last ? &eof_token : first;
686 lexer->last_token = last;
688 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
690 /* Initially we are not debugging. */
691 lexer->debugging_p = false;
693 gcc_assert (!lexer->next_token->purged_p);
694 return lexer;
697 /* Frees all resources associated with LEXER. */
699 static void
700 cp_lexer_destroy (cp_lexer *lexer)
702 vec_free (lexer->buffer);
703 lexer->saved_tokens.release ();
704 ggc_free (lexer);
707 /* Returns nonzero if debugging information should be output. */
709 static inline bool
710 cp_lexer_debugging_p (cp_lexer *lexer)
712 return lexer->debugging_p;
716 static inline cp_token_position
717 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
719 gcc_assert (!previous_p || lexer->next_token != &eof_token);
721 return lexer->next_token - previous_p;
724 static inline cp_token *
725 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
727 return pos;
730 static inline void
731 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
733 lexer->next_token = cp_lexer_token_at (lexer, pos);
736 static inline cp_token_position
737 cp_lexer_previous_token_position (cp_lexer *lexer)
739 if (lexer->next_token == &eof_token)
740 return lexer->last_token - 1;
741 else
742 return cp_lexer_token_position (lexer, true);
745 static inline cp_token *
746 cp_lexer_previous_token (cp_lexer *lexer)
748 cp_token_position tp = cp_lexer_previous_token_position (lexer);
750 return cp_lexer_token_at (lexer, tp);
753 /* nonzero if we are presently saving tokens. */
755 static inline int
756 cp_lexer_saving_tokens (const cp_lexer* lexer)
758 return lexer->saved_tokens.length () != 0;
761 /* Store the next token from the preprocessor in *TOKEN. Return true
762 if we reach EOF. If LEXER is NULL, assume we are handling an
763 initial #pragma pch_preprocess, and thus want the lexer to return
764 processed strings. */
766 static void
767 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
769 static int is_extern_c = 0;
771 /* Get a new token from the preprocessor. */
772 token->type
773 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
774 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
775 token->keyword = RID_MAX;
776 token->pragma_kind = PRAGMA_NONE;
777 token->purged_p = false;
778 token->error_reported = false;
780 /* On some systems, some header files are surrounded by an
781 implicit extern "C" block. Set a flag in the token if it
782 comes from such a header. */
783 is_extern_c += pending_lang_change;
784 pending_lang_change = 0;
785 token->implicit_extern_c = is_extern_c > 0;
787 /* Check to see if this token is a keyword. */
788 if (token->type == CPP_NAME)
790 if (C_IS_RESERVED_WORD (token->u.value))
792 /* Mark this token as a keyword. */
793 token->type = CPP_KEYWORD;
794 /* Record which keyword. */
795 token->keyword = C_RID_CODE (token->u.value);
797 else
799 if (warn_cxx0x_compat
800 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
801 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
803 /* Warn about the C++0x keyword (but still treat it as
804 an identifier). */
805 warning (OPT_Wc__0x_compat,
806 "identifier %qE is a keyword in C++11",
807 token->u.value);
809 /* Clear out the C_RID_CODE so we don't warn about this
810 particular identifier-turned-keyword again. */
811 C_SET_RID_CODE (token->u.value, RID_MAX);
814 token->keyword = RID_MAX;
817 else if (token->type == CPP_AT_NAME)
819 /* This only happens in Objective-C++; it must be a keyword. */
820 token->type = CPP_KEYWORD;
821 switch (C_RID_CODE (token->u.value))
823 /* Replace 'class' with '@class', 'private' with '@private',
824 etc. This prevents confusion with the C++ keyword
825 'class', and makes the tokens consistent with other
826 Objective-C 'AT' keywords. For example '@class' is
827 reported as RID_AT_CLASS which is consistent with
828 '@synchronized', which is reported as
829 RID_AT_SYNCHRONIZED.
831 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
832 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
833 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
834 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
835 case RID_THROW: token->keyword = RID_AT_THROW; break;
836 case RID_TRY: token->keyword = RID_AT_TRY; break;
837 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
838 default: token->keyword = C_RID_CODE (token->u.value);
841 else if (token->type == CPP_PRAGMA)
843 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
844 token->pragma_kind = ((enum pragma_kind)
845 TREE_INT_CST_LOW (token->u.value));
846 token->u.value = NULL_TREE;
850 /* Update the globals input_location and the input file stack from TOKEN. */
851 static inline void
852 cp_lexer_set_source_position_from_token (cp_token *token)
854 if (token->type != CPP_EOF)
856 input_location = token->location;
860 /* Update the globals input_location and the input file stack from LEXER. */
861 static inline void
862 cp_lexer_set_source_position (cp_lexer *lexer)
864 cp_token *token = cp_lexer_peek_token (lexer);
865 cp_lexer_set_source_position_from_token (token);
868 /* Return a pointer to the next token in the token stream, but do not
869 consume it. */
871 static inline cp_token *
872 cp_lexer_peek_token (cp_lexer *lexer)
874 if (cp_lexer_debugging_p (lexer))
876 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
877 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
878 putc ('\n', cp_lexer_debug_stream);
880 return lexer->next_token;
883 /* Return true if the next token has the indicated TYPE. */
885 static inline bool
886 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
888 return cp_lexer_peek_token (lexer)->type == type;
891 /* Return true if the next token does not have the indicated TYPE. */
893 static inline bool
894 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
896 return !cp_lexer_next_token_is (lexer, type);
899 /* Return true if the next token is the indicated KEYWORD. */
901 static inline bool
902 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
904 return cp_lexer_peek_token (lexer)->keyword == keyword;
907 static inline bool
908 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
910 return cp_lexer_peek_nth_token (lexer, n)->type == type;
913 static inline bool
914 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
916 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
919 /* Return true if the next token is not the indicated KEYWORD. */
921 static inline bool
922 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
924 return cp_lexer_peek_token (lexer)->keyword != keyword;
927 /* Return true if the next token is a keyword for a decl-specifier. */
929 static bool
930 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
932 cp_token *token;
934 token = cp_lexer_peek_token (lexer);
935 switch (token->keyword)
937 /* auto specifier: storage-class-specifier in C++,
938 simple-type-specifier in C++0x. */
939 case RID_AUTO:
940 /* Storage classes. */
941 case RID_REGISTER:
942 case RID_STATIC:
943 case RID_EXTERN:
944 case RID_MUTABLE:
945 case RID_THREAD:
946 /* Elaborated type specifiers. */
947 case RID_ENUM:
948 case RID_CLASS:
949 case RID_STRUCT:
950 case RID_UNION:
951 case RID_TYPENAME:
952 /* Simple type specifiers. */
953 case RID_CHAR:
954 case RID_CHAR16:
955 case RID_CHAR32:
956 case RID_WCHAR:
957 case RID_BOOL:
958 case RID_SHORT:
959 case RID_INT:
960 case RID_LONG:
961 case RID_SIGNED:
962 case RID_UNSIGNED:
963 case RID_FLOAT:
964 case RID_DOUBLE:
965 case RID_VOID:
966 /* GNU extensions. */
967 case RID_ATTRIBUTE:
968 case RID_TYPEOF:
969 /* C++0x extensions. */
970 case RID_DECLTYPE:
971 case RID_UNDERLYING_TYPE:
972 return true;
974 default:
975 if (token->keyword >= RID_FIRST_INT_N
976 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
977 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
978 return true;
979 return false;
983 /* Returns TRUE iff the token T begins a decltype type. */
985 static bool
986 token_is_decltype (cp_token *t)
988 return (t->keyword == RID_DECLTYPE
989 || t->type == CPP_DECLTYPE);
992 /* Returns TRUE iff the next token begins a decltype type. */
994 static bool
995 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
997 cp_token *t = cp_lexer_peek_token (lexer);
998 return token_is_decltype (t);
1001 /* Return a pointer to the Nth token in the token stream. If N is 1,
1002 then this is precisely equivalent to cp_lexer_peek_token (except
1003 that it is not inline). One would like to disallow that case, but
1004 there is one case (cp_parser_nth_token_starts_template_id) where
1005 the caller passes a variable for N and it might be 1. */
1007 static cp_token *
1008 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1010 cp_token *token;
1012 /* N is 1-based, not zero-based. */
1013 gcc_assert (n > 0);
1015 if (cp_lexer_debugging_p (lexer))
1016 fprintf (cp_lexer_debug_stream,
1017 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1019 --n;
1020 token = lexer->next_token;
1021 gcc_assert (!n || token != &eof_token);
1022 while (n != 0)
1024 ++token;
1025 if (token == lexer->last_token)
1027 token = &eof_token;
1028 break;
1031 if (!token->purged_p)
1032 --n;
1035 if (cp_lexer_debugging_p (lexer))
1037 cp_lexer_print_token (cp_lexer_debug_stream, token);
1038 putc ('\n', cp_lexer_debug_stream);
1041 return token;
1044 /* Return the next token, and advance the lexer's next_token pointer
1045 to point to the next non-purged token. */
1047 static cp_token *
1048 cp_lexer_consume_token (cp_lexer* lexer)
1050 cp_token *token = lexer->next_token;
1052 gcc_assert (token != &eof_token);
1053 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1057 lexer->next_token++;
1058 if (lexer->next_token == lexer->last_token)
1060 lexer->next_token = &eof_token;
1061 break;
1065 while (lexer->next_token->purged_p);
1067 cp_lexer_set_source_position_from_token (token);
1069 /* Provide debugging output. */
1070 if (cp_lexer_debugging_p (lexer))
1072 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1073 cp_lexer_print_token (cp_lexer_debug_stream, token);
1074 putc ('\n', cp_lexer_debug_stream);
1077 return token;
1080 /* Permanently remove the next token from the token stream, and
1081 advance the next_token pointer to refer to the next non-purged
1082 token. */
1084 static void
1085 cp_lexer_purge_token (cp_lexer *lexer)
1087 cp_token *tok = lexer->next_token;
1089 gcc_assert (tok != &eof_token);
1090 tok->purged_p = true;
1091 tok->location = UNKNOWN_LOCATION;
1092 tok->u.value = NULL_TREE;
1093 tok->keyword = RID_MAX;
1097 tok++;
1098 if (tok == lexer->last_token)
1100 tok = &eof_token;
1101 break;
1104 while (tok->purged_p);
1105 lexer->next_token = tok;
1108 /* Permanently remove all tokens after TOK, up to, but not
1109 including, the token that will be returned next by
1110 cp_lexer_peek_token. */
1112 static void
1113 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1115 cp_token *peek = lexer->next_token;
1117 if (peek == &eof_token)
1118 peek = lexer->last_token;
1120 gcc_assert (tok < peek);
1122 for ( tok += 1; tok != peek; tok += 1)
1124 tok->purged_p = true;
1125 tok->location = UNKNOWN_LOCATION;
1126 tok->u.value = NULL_TREE;
1127 tok->keyword = RID_MAX;
1131 /* Begin saving tokens. All tokens consumed after this point will be
1132 preserved. */
1134 static void
1135 cp_lexer_save_tokens (cp_lexer* lexer)
1137 /* Provide debugging output. */
1138 if (cp_lexer_debugging_p (lexer))
1139 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1141 lexer->saved_tokens.safe_push (lexer->next_token);
1144 /* Commit to the portion of the token stream most recently saved. */
1146 static void
1147 cp_lexer_commit_tokens (cp_lexer* lexer)
1149 /* Provide debugging output. */
1150 if (cp_lexer_debugging_p (lexer))
1151 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1153 lexer->saved_tokens.pop ();
1156 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1157 to the token stream. Stop saving tokens. */
1159 static void
1160 cp_lexer_rollback_tokens (cp_lexer* lexer)
1162 /* Provide debugging output. */
1163 if (cp_lexer_debugging_p (lexer))
1164 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1166 lexer->next_token = lexer->saved_tokens.pop ();
1169 /* RAII wrapper around the above functions, with sanity checking. Creating
1170 a variable saves tokens, which are committed when the variable is
1171 destroyed unless they are explicitly rolled back by calling the rollback
1172 member function. */
1174 struct saved_token_sentinel
1176 cp_lexer *lexer;
1177 unsigned len;
1178 bool commit;
1179 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1181 len = lexer->saved_tokens.length ();
1182 cp_lexer_save_tokens (lexer);
1184 void rollback ()
1186 cp_lexer_rollback_tokens (lexer);
1187 commit = false;
1189 ~saved_token_sentinel()
1191 if (commit)
1192 cp_lexer_commit_tokens (lexer);
1193 gcc_assert (lexer->saved_tokens.length () == len);
1197 /* Print a representation of the TOKEN on the STREAM. */
1199 static void
1200 cp_lexer_print_token (FILE * stream, cp_token *token)
1202 /* We don't use cpp_type2name here because the parser defines
1203 a few tokens of its own. */
1204 static const char *const token_names[] = {
1205 /* cpplib-defined token types */
1206 #define OP(e, s) #e,
1207 #define TK(e, s) #e,
1208 TTYPE_TABLE
1209 #undef OP
1210 #undef TK
1211 /* C++ parser token types - see "Manifest constants", above. */
1212 "KEYWORD",
1213 "TEMPLATE_ID",
1214 "NESTED_NAME_SPECIFIER",
1217 /* For some tokens, print the associated data. */
1218 switch (token->type)
1220 case CPP_KEYWORD:
1221 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1222 For example, `struct' is mapped to an INTEGER_CST. */
1223 if (!identifier_p (token->u.value))
1224 break;
1225 /* else fall through */
1226 case CPP_NAME:
1227 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1228 break;
1230 case CPP_STRING:
1231 case CPP_STRING16:
1232 case CPP_STRING32:
1233 case CPP_WSTRING:
1234 case CPP_UTF8STRING:
1235 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1236 break;
1238 case CPP_NUMBER:
1239 print_generic_expr (stream, token->u.value, 0);
1240 break;
1242 default:
1243 /* If we have a name for the token, print it out. Otherwise, we
1244 simply give the numeric code. */
1245 if (token->type < ARRAY_SIZE(token_names))
1246 fputs (token_names[token->type], stream);
1247 else
1248 fprintf (stream, "[%d]", token->type);
1249 break;
1253 DEBUG_FUNCTION void
1254 debug (cp_token &ref)
1256 cp_lexer_print_token (stderr, &ref);
1257 fprintf (stderr, "\n");
1260 DEBUG_FUNCTION void
1261 debug (cp_token *ptr)
1263 if (ptr)
1264 debug (*ptr);
1265 else
1266 fprintf (stderr, "<nil>\n");
1270 /* Start emitting debugging information. */
1272 static void
1273 cp_lexer_start_debugging (cp_lexer* lexer)
1275 lexer->debugging_p = true;
1276 cp_lexer_debug_stream = stderr;
1279 /* Stop emitting debugging information. */
1281 static void
1282 cp_lexer_stop_debugging (cp_lexer* lexer)
1284 lexer->debugging_p = false;
1285 cp_lexer_debug_stream = NULL;
1288 /* Create a new cp_token_cache, representing a range of tokens. */
1290 static cp_token_cache *
1291 cp_token_cache_new (cp_token *first, cp_token *last)
1293 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1294 cache->first = first;
1295 cache->last = last;
1296 return cache;
1299 /* Diagnose if #pragma omp declare simd isn't followed immediately
1300 by function declaration or definition. */
1302 static inline void
1303 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1305 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1307 error ("%<#pragma omp declare simd%> not immediately followed by "
1308 "function declaration or definition");
1309 parser->omp_declare_simd = NULL;
1313 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1314 and put that into "omp declare simd" attribute. */
1316 static inline void
1317 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1319 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1321 if (fndecl == error_mark_node)
1323 parser->omp_declare_simd = NULL;
1324 return;
1326 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1328 cp_ensure_no_omp_declare_simd (parser);
1329 return;
1334 /* Decl-specifiers. */
1336 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1338 static void
1339 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1341 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1344 /* Declarators. */
1346 /* Nothing other than the parser should be creating declarators;
1347 declarators are a semi-syntactic representation of C++ entities.
1348 Other parts of the front end that need to create entities (like
1349 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1351 static cp_declarator *make_call_declarator
1352 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1353 static cp_declarator *make_array_declarator
1354 (cp_declarator *, tree);
1355 static cp_declarator *make_pointer_declarator
1356 (cp_cv_quals, cp_declarator *, tree);
1357 static cp_declarator *make_reference_declarator
1358 (cp_cv_quals, cp_declarator *, bool, tree);
1359 static cp_parameter_declarator *make_parameter_declarator
1360 (cp_decl_specifier_seq *, cp_declarator *, tree);
1361 static cp_declarator *make_ptrmem_declarator
1362 (cp_cv_quals, tree, cp_declarator *, tree);
1364 /* An erroneous declarator. */
1365 static cp_declarator *cp_error_declarator;
1367 /* The obstack on which declarators and related data structures are
1368 allocated. */
1369 static struct obstack declarator_obstack;
1371 /* Alloc BYTES from the declarator memory pool. */
1373 static inline void *
1374 alloc_declarator (size_t bytes)
1376 return obstack_alloc (&declarator_obstack, bytes);
1379 /* Allocate a declarator of the indicated KIND. Clear fields that are
1380 common to all declarators. */
1382 static cp_declarator *
1383 make_declarator (cp_declarator_kind kind)
1385 cp_declarator *declarator;
1387 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1388 declarator->kind = kind;
1389 declarator->attributes = NULL_TREE;
1390 declarator->std_attributes = NULL_TREE;
1391 declarator->declarator = NULL;
1392 declarator->parameter_pack_p = false;
1393 declarator->id_loc = UNKNOWN_LOCATION;
1395 return declarator;
1398 /* Make a declarator for a generalized identifier. If
1399 QUALIFYING_SCOPE is non-NULL, the identifier is
1400 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1401 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1402 is, if any. */
1404 static cp_declarator *
1405 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1406 special_function_kind sfk)
1408 cp_declarator *declarator;
1410 /* It is valid to write:
1412 class C { void f(); };
1413 typedef C D;
1414 void D::f();
1416 The standard is not clear about whether `typedef const C D' is
1417 legal; as of 2002-09-15 the committee is considering that
1418 question. EDG 3.0 allows that syntax. Therefore, we do as
1419 well. */
1420 if (qualifying_scope && TYPE_P (qualifying_scope))
1421 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1423 gcc_assert (identifier_p (unqualified_name)
1424 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1425 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1427 declarator = make_declarator (cdk_id);
1428 declarator->u.id.qualifying_scope = qualifying_scope;
1429 declarator->u.id.unqualified_name = unqualified_name;
1430 declarator->u.id.sfk = sfk;
1432 return declarator;
1435 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1436 of modifiers such as const or volatile to apply to the pointer
1437 type, represented as identifiers. ATTRIBUTES represent the attributes that
1438 appertain to the pointer or reference. */
1440 cp_declarator *
1441 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1442 tree attributes)
1444 cp_declarator *declarator;
1446 declarator = make_declarator (cdk_pointer);
1447 declarator->declarator = target;
1448 declarator->u.pointer.qualifiers = cv_qualifiers;
1449 declarator->u.pointer.class_type = NULL_TREE;
1450 if (target)
1452 declarator->id_loc = target->id_loc;
1453 declarator->parameter_pack_p = target->parameter_pack_p;
1454 target->parameter_pack_p = false;
1456 else
1457 declarator->parameter_pack_p = false;
1459 declarator->std_attributes = attributes;
1461 return declarator;
1464 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1465 represent the attributes that appertain to the pointer or
1466 reference. */
1468 cp_declarator *
1469 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1470 bool rvalue_ref, tree attributes)
1472 cp_declarator *declarator;
1474 declarator = make_declarator (cdk_reference);
1475 declarator->declarator = target;
1476 declarator->u.reference.qualifiers = cv_qualifiers;
1477 declarator->u.reference.rvalue_ref = rvalue_ref;
1478 if (target)
1480 declarator->id_loc = target->id_loc;
1481 declarator->parameter_pack_p = target->parameter_pack_p;
1482 target->parameter_pack_p = false;
1484 else
1485 declarator->parameter_pack_p = false;
1487 declarator->std_attributes = attributes;
1489 return declarator;
1492 /* Like make_pointer_declarator -- but for a pointer to a non-static
1493 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1494 appertain to the pointer or reference. */
1496 cp_declarator *
1497 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1498 cp_declarator *pointee,
1499 tree attributes)
1501 cp_declarator *declarator;
1503 declarator = make_declarator (cdk_ptrmem);
1504 declarator->declarator = pointee;
1505 declarator->u.pointer.qualifiers = cv_qualifiers;
1506 declarator->u.pointer.class_type = class_type;
1508 if (pointee)
1510 declarator->parameter_pack_p = pointee->parameter_pack_p;
1511 pointee->parameter_pack_p = false;
1513 else
1514 declarator->parameter_pack_p = false;
1516 declarator->std_attributes = attributes;
1518 return declarator;
1521 /* Make a declarator for the function given by TARGET, with the
1522 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1523 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1524 indicates what exceptions can be thrown. */
1526 cp_declarator *
1527 make_call_declarator (cp_declarator *target,
1528 tree parms,
1529 cp_cv_quals cv_qualifiers,
1530 cp_virt_specifiers virt_specifiers,
1531 cp_ref_qualifier ref_qualifier,
1532 tree exception_specification,
1533 tree late_return_type)
1535 cp_declarator *declarator;
1537 declarator = make_declarator (cdk_function);
1538 declarator->declarator = target;
1539 declarator->u.function.parameters = parms;
1540 declarator->u.function.qualifiers = cv_qualifiers;
1541 declarator->u.function.virt_specifiers = virt_specifiers;
1542 declarator->u.function.ref_qualifier = ref_qualifier;
1543 declarator->u.function.exception_specification = exception_specification;
1544 declarator->u.function.late_return_type = late_return_type;
1545 if (target)
1547 declarator->id_loc = target->id_loc;
1548 declarator->parameter_pack_p = target->parameter_pack_p;
1549 target->parameter_pack_p = false;
1551 else
1552 declarator->parameter_pack_p = false;
1554 return declarator;
1557 /* Make a declarator for an array of BOUNDS elements, each of which is
1558 defined by ELEMENT. */
1560 cp_declarator *
1561 make_array_declarator (cp_declarator *element, tree bounds)
1563 cp_declarator *declarator;
1565 declarator = make_declarator (cdk_array);
1566 declarator->declarator = element;
1567 declarator->u.array.bounds = bounds;
1568 if (element)
1570 declarator->id_loc = element->id_loc;
1571 declarator->parameter_pack_p = element->parameter_pack_p;
1572 element->parameter_pack_p = false;
1574 else
1575 declarator->parameter_pack_p = false;
1577 return declarator;
1580 /* Determine whether the declarator we've seen so far can be a
1581 parameter pack, when followed by an ellipsis. */
1582 static bool
1583 declarator_can_be_parameter_pack (cp_declarator *declarator)
1585 /* Search for a declarator name, or any other declarator that goes
1586 after the point where the ellipsis could appear in a parameter
1587 pack. If we find any of these, then this declarator can not be
1588 made into a parameter pack. */
1589 bool found = false;
1590 while (declarator && !found)
1592 switch ((int)declarator->kind)
1594 case cdk_id:
1595 case cdk_array:
1596 found = true;
1597 break;
1599 case cdk_error:
1600 return true;
1602 default:
1603 declarator = declarator->declarator;
1604 break;
1608 return !found;
1611 cp_parameter_declarator *no_parameters;
1613 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1614 DECLARATOR and DEFAULT_ARGUMENT. */
1616 cp_parameter_declarator *
1617 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1618 cp_declarator *declarator,
1619 tree default_argument)
1621 cp_parameter_declarator *parameter;
1623 parameter = ((cp_parameter_declarator *)
1624 alloc_declarator (sizeof (cp_parameter_declarator)));
1625 parameter->next = NULL;
1626 if (decl_specifiers)
1627 parameter->decl_specifiers = *decl_specifiers;
1628 else
1629 clear_decl_specs (&parameter->decl_specifiers);
1630 parameter->declarator = declarator;
1631 parameter->default_argument = default_argument;
1632 parameter->ellipsis_p = false;
1634 return parameter;
1637 /* Returns true iff DECLARATOR is a declaration for a function. */
1639 static bool
1640 function_declarator_p (const cp_declarator *declarator)
1642 while (declarator)
1644 if (declarator->kind == cdk_function
1645 && declarator->declarator->kind == cdk_id)
1646 return true;
1647 if (declarator->kind == cdk_id
1648 || declarator->kind == cdk_error)
1649 return false;
1650 declarator = declarator->declarator;
1652 return false;
1655 /* The parser. */
1657 /* Overview
1658 --------
1660 A cp_parser parses the token stream as specified by the C++
1661 grammar. Its job is purely parsing, not semantic analysis. For
1662 example, the parser breaks the token stream into declarators,
1663 expressions, statements, and other similar syntactic constructs.
1664 It does not check that the types of the expressions on either side
1665 of an assignment-statement are compatible, or that a function is
1666 not declared with a parameter of type `void'.
1668 The parser invokes routines elsewhere in the compiler to perform
1669 semantic analysis and to build up the abstract syntax tree for the
1670 code processed.
1672 The parser (and the template instantiation code, which is, in a
1673 way, a close relative of parsing) are the only parts of the
1674 compiler that should be calling push_scope and pop_scope, or
1675 related functions. The parser (and template instantiation code)
1676 keeps track of what scope is presently active; everything else
1677 should simply honor that. (The code that generates static
1678 initializers may also need to set the scope, in order to check
1679 access control correctly when emitting the initializers.)
1681 Methodology
1682 -----------
1684 The parser is of the standard recursive-descent variety. Upcoming
1685 tokens in the token stream are examined in order to determine which
1686 production to use when parsing a non-terminal. Some C++ constructs
1687 require arbitrary look ahead to disambiguate. For example, it is
1688 impossible, in the general case, to tell whether a statement is an
1689 expression or declaration without scanning the entire statement.
1690 Therefore, the parser is capable of "parsing tentatively." When the
1691 parser is not sure what construct comes next, it enters this mode.
1692 Then, while we attempt to parse the construct, the parser queues up
1693 error messages, rather than issuing them immediately, and saves the
1694 tokens it consumes. If the construct is parsed successfully, the
1695 parser "commits", i.e., it issues any queued error messages and
1696 the tokens that were being preserved are permanently discarded.
1697 If, however, the construct is not parsed successfully, the parser
1698 rolls back its state completely so that it can resume parsing using
1699 a different alternative.
1701 Future Improvements
1702 -------------------
1704 The performance of the parser could probably be improved substantially.
1705 We could often eliminate the need to parse tentatively by looking ahead
1706 a little bit. In some places, this approach might not entirely eliminate
1707 the need to parse tentatively, but it might still speed up the average
1708 case. */
1710 /* Flags that are passed to some parsing functions. These values can
1711 be bitwise-ored together. */
1713 enum
1715 /* No flags. */
1716 CP_PARSER_FLAGS_NONE = 0x0,
1717 /* The construct is optional. If it is not present, then no error
1718 should be issued. */
1719 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1720 /* When parsing a type-specifier, treat user-defined type-names
1721 as non-type identifiers. */
1722 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1723 /* When parsing a type-specifier, do not try to parse a class-specifier
1724 or enum-specifier. */
1725 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1726 /* When parsing a decl-specifier-seq, only allow type-specifier or
1727 constexpr. */
1728 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1731 /* This type is used for parameters and variables which hold
1732 combinations of the above flags. */
1733 typedef int cp_parser_flags;
1735 /* The different kinds of declarators we want to parse. */
1737 typedef enum cp_parser_declarator_kind
1739 /* We want an abstract declarator. */
1740 CP_PARSER_DECLARATOR_ABSTRACT,
1741 /* We want a named declarator. */
1742 CP_PARSER_DECLARATOR_NAMED,
1743 /* We don't mind, but the name must be an unqualified-id. */
1744 CP_PARSER_DECLARATOR_EITHER
1745 } cp_parser_declarator_kind;
1747 /* The precedence values used to parse binary expressions. The minimum value
1748 of PREC must be 1, because zero is reserved to quickly discriminate
1749 binary operators from other tokens. */
1751 enum cp_parser_prec
1753 PREC_NOT_OPERATOR,
1754 PREC_LOGICAL_OR_EXPRESSION,
1755 PREC_LOGICAL_AND_EXPRESSION,
1756 PREC_INCLUSIVE_OR_EXPRESSION,
1757 PREC_EXCLUSIVE_OR_EXPRESSION,
1758 PREC_AND_EXPRESSION,
1759 PREC_EQUALITY_EXPRESSION,
1760 PREC_RELATIONAL_EXPRESSION,
1761 PREC_SHIFT_EXPRESSION,
1762 PREC_ADDITIVE_EXPRESSION,
1763 PREC_MULTIPLICATIVE_EXPRESSION,
1764 PREC_PM_EXPRESSION,
1765 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1768 /* A mapping from a token type to a corresponding tree node type, with a
1769 precedence value. */
1771 typedef struct cp_parser_binary_operations_map_node
1773 /* The token type. */
1774 enum cpp_ttype token_type;
1775 /* The corresponding tree code. */
1776 enum tree_code tree_type;
1777 /* The precedence of this operator. */
1778 enum cp_parser_prec prec;
1779 } cp_parser_binary_operations_map_node;
1781 typedef struct cp_parser_expression_stack_entry
1783 /* Left hand side of the binary operation we are currently
1784 parsing. */
1785 tree lhs;
1786 /* Original tree code for left hand side, if it was a binary
1787 expression itself (used for -Wparentheses). */
1788 enum tree_code lhs_type;
1789 /* Tree code for the binary operation we are parsing. */
1790 enum tree_code tree_type;
1791 /* Precedence of the binary operation we are parsing. */
1792 enum cp_parser_prec prec;
1793 /* Location of the binary operation we are parsing. */
1794 location_t loc;
1795 } cp_parser_expression_stack_entry;
1797 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1798 entries because precedence levels on the stack are monotonically
1799 increasing. */
1800 typedef struct cp_parser_expression_stack_entry
1801 cp_parser_expression_stack[NUM_PREC_VALUES];
1803 /* Prototypes. */
1805 /* Constructors and destructors. */
1807 static cp_parser_context *cp_parser_context_new
1808 (cp_parser_context *);
1810 /* Class variables. */
1812 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1814 /* The operator-precedence table used by cp_parser_binary_expression.
1815 Transformed into an associative array (binops_by_token) by
1816 cp_parser_new. */
1818 static const cp_parser_binary_operations_map_node binops[] = {
1819 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1820 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1822 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1823 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1824 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1826 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1827 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1829 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1830 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1832 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1833 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1834 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1835 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1837 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1838 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1840 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1842 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1844 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1846 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1848 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1851 /* The same as binops, but initialized by cp_parser_new so that
1852 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1853 for speed. */
1854 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1856 /* Constructors and destructors. */
1858 /* Construct a new context. The context below this one on the stack
1859 is given by NEXT. */
1861 static cp_parser_context *
1862 cp_parser_context_new (cp_parser_context* next)
1864 cp_parser_context *context;
1866 /* Allocate the storage. */
1867 if (cp_parser_context_free_list != NULL)
1869 /* Pull the first entry from the free list. */
1870 context = cp_parser_context_free_list;
1871 cp_parser_context_free_list = context->next;
1872 memset (context, 0, sizeof (*context));
1874 else
1875 context = ggc_cleared_alloc<cp_parser_context> ();
1877 /* No errors have occurred yet in this context. */
1878 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1879 /* If this is not the bottommost context, copy information that we
1880 need from the previous context. */
1881 if (next)
1883 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1884 expression, then we are parsing one in this context, too. */
1885 context->object_type = next->object_type;
1886 /* Thread the stack. */
1887 context->next = next;
1890 return context;
1893 /* Managing the unparsed function queues. */
1895 #define unparsed_funs_with_default_args \
1896 parser->unparsed_queues->last ().funs_with_default_args
1897 #define unparsed_funs_with_definitions \
1898 parser->unparsed_queues->last ().funs_with_definitions
1899 #define unparsed_nsdmis \
1900 parser->unparsed_queues->last ().nsdmis
1901 #define unparsed_classes \
1902 parser->unparsed_queues->last ().classes
1904 static void
1905 push_unparsed_function_queues (cp_parser *parser)
1907 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1908 vec_safe_push (parser->unparsed_queues, e);
1911 static void
1912 pop_unparsed_function_queues (cp_parser *parser)
1914 release_tree_vector (unparsed_funs_with_definitions);
1915 parser->unparsed_queues->pop ();
1918 /* Prototypes. */
1920 /* Constructors and destructors. */
1922 static cp_parser *cp_parser_new
1923 (void);
1925 /* Routines to parse various constructs.
1927 Those that return `tree' will return the error_mark_node (rather
1928 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1929 Sometimes, they will return an ordinary node if error-recovery was
1930 attempted, even though a parse error occurred. So, to check
1931 whether or not a parse error occurred, you should always use
1932 cp_parser_error_occurred. If the construct is optional (indicated
1933 either by an `_opt' in the name of the function that does the
1934 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1935 the construct is not present. */
1937 /* Lexical conventions [gram.lex] */
1939 static tree cp_parser_identifier
1940 (cp_parser *);
1941 static tree cp_parser_string_literal
1942 (cp_parser *, bool, bool, bool);
1943 static tree cp_parser_userdef_char_literal
1944 (cp_parser *);
1945 static tree cp_parser_userdef_string_literal
1946 (tree);
1947 static tree cp_parser_userdef_numeric_literal
1948 (cp_parser *);
1950 /* Basic concepts [gram.basic] */
1952 static bool cp_parser_translation_unit
1953 (cp_parser *);
1955 /* Expressions [gram.expr] */
1957 static tree cp_parser_primary_expression
1958 (cp_parser *, bool, bool, bool, cp_id_kind *);
1959 static tree cp_parser_id_expression
1960 (cp_parser *, bool, bool, bool *, bool, bool);
1961 static tree cp_parser_unqualified_id
1962 (cp_parser *, bool, bool, bool, bool);
1963 static tree cp_parser_nested_name_specifier_opt
1964 (cp_parser *, bool, bool, bool, bool);
1965 static tree cp_parser_nested_name_specifier
1966 (cp_parser *, bool, bool, bool, bool);
1967 static tree cp_parser_qualifying_entity
1968 (cp_parser *, bool, bool, bool, bool, bool);
1969 static tree cp_parser_postfix_expression
1970 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1971 static tree cp_parser_postfix_open_square_expression
1972 (cp_parser *, tree, bool, bool);
1973 static tree cp_parser_postfix_dot_deref_expression
1974 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1975 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1976 (cp_parser *, int, bool, bool, bool *, bool = false);
1977 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1978 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1979 static void cp_parser_pseudo_destructor_name
1980 (cp_parser *, tree, tree *, tree *);
1981 static tree cp_parser_unary_expression
1982 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1983 static enum tree_code cp_parser_unary_operator
1984 (cp_token *);
1985 static tree cp_parser_new_expression
1986 (cp_parser *);
1987 static vec<tree, va_gc> *cp_parser_new_placement
1988 (cp_parser *);
1989 static tree cp_parser_new_type_id
1990 (cp_parser *, tree *);
1991 static cp_declarator *cp_parser_new_declarator_opt
1992 (cp_parser *);
1993 static cp_declarator *cp_parser_direct_new_declarator
1994 (cp_parser *);
1995 static vec<tree, va_gc> *cp_parser_new_initializer
1996 (cp_parser *);
1997 static tree cp_parser_delete_expression
1998 (cp_parser *);
1999 static tree cp_parser_cast_expression
2000 (cp_parser *, bool, bool, bool, cp_id_kind *);
2001 static tree cp_parser_binary_expression
2002 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2003 static tree cp_parser_question_colon_clause
2004 (cp_parser *, tree);
2005 static tree cp_parser_assignment_expression
2006 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2007 static enum tree_code cp_parser_assignment_operator_opt
2008 (cp_parser *);
2009 static tree cp_parser_expression
2010 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2011 static tree cp_parser_constant_expression
2012 (cp_parser *, bool = false, bool * = NULL);
2013 static tree cp_parser_builtin_offsetof
2014 (cp_parser *);
2015 static tree cp_parser_lambda_expression
2016 (cp_parser *);
2017 static void cp_parser_lambda_introducer
2018 (cp_parser *, tree);
2019 static bool cp_parser_lambda_declarator_opt
2020 (cp_parser *, tree);
2021 static void cp_parser_lambda_body
2022 (cp_parser *, tree);
2024 /* Statements [gram.stmt.stmt] */
2026 static void cp_parser_statement
2027 (cp_parser *, tree, bool, bool *);
2028 static void cp_parser_label_for_labeled_statement
2029 (cp_parser *, tree);
2030 static tree cp_parser_expression_statement
2031 (cp_parser *, tree);
2032 static tree cp_parser_compound_statement
2033 (cp_parser *, tree, bool, bool);
2034 static void cp_parser_statement_seq_opt
2035 (cp_parser *, tree);
2036 static tree cp_parser_selection_statement
2037 (cp_parser *, bool *);
2038 static tree cp_parser_condition
2039 (cp_parser *);
2040 static tree cp_parser_iteration_statement
2041 (cp_parser *, bool);
2042 static bool cp_parser_for_init_statement
2043 (cp_parser *, tree *decl);
2044 static tree cp_parser_for
2045 (cp_parser *, bool);
2046 static tree cp_parser_c_for
2047 (cp_parser *, tree, tree, bool);
2048 static tree cp_parser_range_for
2049 (cp_parser *, tree, tree, tree, bool);
2050 static void do_range_for_auto_deduction
2051 (tree, tree);
2052 static tree cp_parser_perform_range_for_lookup
2053 (tree, tree *, tree *);
2054 static tree cp_parser_range_for_member_function
2055 (tree, tree);
2056 static tree cp_parser_jump_statement
2057 (cp_parser *);
2058 static void cp_parser_declaration_statement
2059 (cp_parser *);
2061 static tree cp_parser_implicitly_scoped_statement
2062 (cp_parser *, bool *);
2063 static void cp_parser_already_scoped_statement
2064 (cp_parser *);
2066 /* Declarations [gram.dcl.dcl] */
2068 static void cp_parser_declaration_seq_opt
2069 (cp_parser *);
2070 static void cp_parser_declaration
2071 (cp_parser *);
2072 static void cp_parser_block_declaration
2073 (cp_parser *, bool);
2074 static void cp_parser_simple_declaration
2075 (cp_parser *, bool, tree *);
2076 static void cp_parser_decl_specifier_seq
2077 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2078 static tree cp_parser_storage_class_specifier_opt
2079 (cp_parser *);
2080 static tree cp_parser_function_specifier_opt
2081 (cp_parser *, cp_decl_specifier_seq *);
2082 static tree cp_parser_type_specifier
2083 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2084 int *, bool *);
2085 static tree cp_parser_simple_type_specifier
2086 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2087 static tree cp_parser_type_name
2088 (cp_parser *);
2089 static tree cp_parser_nonclass_name
2090 (cp_parser* parser);
2091 static tree cp_parser_elaborated_type_specifier
2092 (cp_parser *, bool, bool);
2093 static tree cp_parser_enum_specifier
2094 (cp_parser *);
2095 static void cp_parser_enumerator_list
2096 (cp_parser *, tree);
2097 static void cp_parser_enumerator_definition
2098 (cp_parser *, tree);
2099 static tree cp_parser_namespace_name
2100 (cp_parser *);
2101 static void cp_parser_namespace_definition
2102 (cp_parser *);
2103 static void cp_parser_namespace_body
2104 (cp_parser *);
2105 static tree cp_parser_qualified_namespace_specifier
2106 (cp_parser *);
2107 static void cp_parser_namespace_alias_definition
2108 (cp_parser *);
2109 static bool cp_parser_using_declaration
2110 (cp_parser *, bool);
2111 static void cp_parser_using_directive
2112 (cp_parser *);
2113 static tree cp_parser_alias_declaration
2114 (cp_parser *);
2115 static void cp_parser_asm_definition
2116 (cp_parser *);
2117 static void cp_parser_linkage_specification
2118 (cp_parser *);
2119 static void cp_parser_static_assert
2120 (cp_parser *, bool);
2121 static tree cp_parser_decltype
2122 (cp_parser *);
2124 /* Declarators [gram.dcl.decl] */
2126 static tree cp_parser_init_declarator
2127 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2128 bool, bool, int, bool *, tree *, location_t *);
2129 static cp_declarator *cp_parser_declarator
2130 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2131 static cp_declarator *cp_parser_direct_declarator
2132 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2133 static enum tree_code cp_parser_ptr_operator
2134 (cp_parser *, tree *, cp_cv_quals *, tree *);
2135 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2136 (cp_parser *);
2137 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2138 (cp_parser *);
2139 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2140 (cp_parser *);
2141 static tree cp_parser_late_return_type_opt
2142 (cp_parser *, cp_declarator *, cp_cv_quals);
2143 static tree cp_parser_declarator_id
2144 (cp_parser *, bool);
2145 static tree cp_parser_type_id
2146 (cp_parser *);
2147 static tree cp_parser_template_type_arg
2148 (cp_parser *);
2149 static tree cp_parser_trailing_type_id (cp_parser *);
2150 static tree cp_parser_type_id_1
2151 (cp_parser *, bool, bool);
2152 static void cp_parser_type_specifier_seq
2153 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2154 static tree cp_parser_parameter_declaration_clause
2155 (cp_parser *);
2156 static tree cp_parser_parameter_declaration_list
2157 (cp_parser *, bool *);
2158 static cp_parameter_declarator *cp_parser_parameter_declaration
2159 (cp_parser *, bool, bool *);
2160 static tree cp_parser_default_argument
2161 (cp_parser *, bool);
2162 static void cp_parser_function_body
2163 (cp_parser *, bool);
2164 static tree cp_parser_initializer
2165 (cp_parser *, bool *, bool *);
2166 static tree cp_parser_initializer_clause
2167 (cp_parser *, bool *);
2168 static tree cp_parser_braced_list
2169 (cp_parser*, bool*);
2170 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2171 (cp_parser *, bool *);
2173 static bool cp_parser_ctor_initializer_opt_and_function_body
2174 (cp_parser *, bool);
2176 static tree cp_parser_late_parsing_omp_declare_simd
2177 (cp_parser *, tree);
2179 static tree cp_parser_late_parsing_cilk_simd_fn_info
2180 (cp_parser *, tree);
2182 static tree synthesize_implicit_template_parm
2183 (cp_parser *);
2184 static tree finish_fully_implicit_template
2185 (cp_parser *, tree);
2187 /* Classes [gram.class] */
2189 static tree cp_parser_class_name
2190 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2191 static tree cp_parser_class_specifier
2192 (cp_parser *);
2193 static tree cp_parser_class_head
2194 (cp_parser *, bool *);
2195 static enum tag_types cp_parser_class_key
2196 (cp_parser *);
2197 static void cp_parser_type_parameter_key
2198 (cp_parser* parser);
2199 static void cp_parser_member_specification_opt
2200 (cp_parser *);
2201 static void cp_parser_member_declaration
2202 (cp_parser *);
2203 static tree cp_parser_pure_specifier
2204 (cp_parser *);
2205 static tree cp_parser_constant_initializer
2206 (cp_parser *);
2208 /* Derived classes [gram.class.derived] */
2210 static tree cp_parser_base_clause
2211 (cp_parser *);
2212 static tree cp_parser_base_specifier
2213 (cp_parser *);
2215 /* Special member functions [gram.special] */
2217 static tree cp_parser_conversion_function_id
2218 (cp_parser *);
2219 static tree cp_parser_conversion_type_id
2220 (cp_parser *);
2221 static cp_declarator *cp_parser_conversion_declarator_opt
2222 (cp_parser *);
2223 static bool cp_parser_ctor_initializer_opt
2224 (cp_parser *);
2225 static void cp_parser_mem_initializer_list
2226 (cp_parser *);
2227 static tree cp_parser_mem_initializer
2228 (cp_parser *);
2229 static tree cp_parser_mem_initializer_id
2230 (cp_parser *);
2232 /* Overloading [gram.over] */
2234 static tree cp_parser_operator_function_id
2235 (cp_parser *);
2236 static tree cp_parser_operator
2237 (cp_parser *);
2239 /* Templates [gram.temp] */
2241 static void cp_parser_template_declaration
2242 (cp_parser *, bool);
2243 static tree cp_parser_template_parameter_list
2244 (cp_parser *);
2245 static tree cp_parser_template_parameter
2246 (cp_parser *, bool *, bool *);
2247 static tree cp_parser_type_parameter
2248 (cp_parser *, bool *);
2249 static tree cp_parser_template_id
2250 (cp_parser *, bool, bool, enum tag_types, bool);
2251 static tree cp_parser_template_name
2252 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2253 static tree cp_parser_template_argument_list
2254 (cp_parser *);
2255 static tree cp_parser_template_argument
2256 (cp_parser *);
2257 static void cp_parser_explicit_instantiation
2258 (cp_parser *);
2259 static void cp_parser_explicit_specialization
2260 (cp_parser *);
2262 /* Exception handling [gram.exception] */
2264 static tree cp_parser_try_block
2265 (cp_parser *);
2266 static bool cp_parser_function_try_block
2267 (cp_parser *);
2268 static void cp_parser_handler_seq
2269 (cp_parser *);
2270 static void cp_parser_handler
2271 (cp_parser *);
2272 static tree cp_parser_exception_declaration
2273 (cp_parser *);
2274 static tree cp_parser_throw_expression
2275 (cp_parser *);
2276 static tree cp_parser_exception_specification_opt
2277 (cp_parser *);
2278 static tree cp_parser_type_id_list
2279 (cp_parser *);
2281 /* GNU Extensions */
2283 static tree cp_parser_asm_specification_opt
2284 (cp_parser *);
2285 static tree cp_parser_asm_operand_list
2286 (cp_parser *);
2287 static tree cp_parser_asm_clobber_list
2288 (cp_parser *);
2289 static tree cp_parser_asm_label_list
2290 (cp_parser *);
2291 static bool cp_next_tokens_can_be_attribute_p
2292 (cp_parser *);
2293 static bool cp_next_tokens_can_be_gnu_attribute_p
2294 (cp_parser *);
2295 static bool cp_next_tokens_can_be_std_attribute_p
2296 (cp_parser *);
2297 static bool cp_nth_tokens_can_be_std_attribute_p
2298 (cp_parser *, size_t);
2299 static bool cp_nth_tokens_can_be_gnu_attribute_p
2300 (cp_parser *, size_t);
2301 static bool cp_nth_tokens_can_be_attribute_p
2302 (cp_parser *, size_t);
2303 static tree cp_parser_attributes_opt
2304 (cp_parser *);
2305 static tree cp_parser_gnu_attributes_opt
2306 (cp_parser *);
2307 static tree cp_parser_gnu_attribute_list
2308 (cp_parser *);
2309 static tree cp_parser_std_attribute
2310 (cp_parser *);
2311 static tree cp_parser_std_attribute_spec
2312 (cp_parser *);
2313 static tree cp_parser_std_attribute_spec_seq
2314 (cp_parser *);
2315 static bool cp_parser_extension_opt
2316 (cp_parser *, int *);
2317 static void cp_parser_label_declaration
2318 (cp_parser *);
2320 /* Transactional Memory Extensions */
2322 static tree cp_parser_transaction
2323 (cp_parser *, enum rid);
2324 static tree cp_parser_transaction_expression
2325 (cp_parser *, enum rid);
2326 static bool cp_parser_function_transaction
2327 (cp_parser *, enum rid);
2328 static tree cp_parser_transaction_cancel
2329 (cp_parser *);
2331 enum pragma_context {
2332 pragma_external,
2333 pragma_member,
2334 pragma_objc_icode,
2335 pragma_stmt,
2336 pragma_compound
2338 static bool cp_parser_pragma
2339 (cp_parser *, enum pragma_context);
2341 /* Objective-C++ Productions */
2343 static tree cp_parser_objc_message_receiver
2344 (cp_parser *);
2345 static tree cp_parser_objc_message_args
2346 (cp_parser *);
2347 static tree cp_parser_objc_message_expression
2348 (cp_parser *);
2349 static tree cp_parser_objc_encode_expression
2350 (cp_parser *);
2351 static tree cp_parser_objc_defs_expression
2352 (cp_parser *);
2353 static tree cp_parser_objc_protocol_expression
2354 (cp_parser *);
2355 static tree cp_parser_objc_selector_expression
2356 (cp_parser *);
2357 static tree cp_parser_objc_expression
2358 (cp_parser *);
2359 static bool cp_parser_objc_selector_p
2360 (enum cpp_ttype);
2361 static tree cp_parser_objc_selector
2362 (cp_parser *);
2363 static tree cp_parser_objc_protocol_refs_opt
2364 (cp_parser *);
2365 static void cp_parser_objc_declaration
2366 (cp_parser *, tree);
2367 static tree cp_parser_objc_statement
2368 (cp_parser *);
2369 static bool cp_parser_objc_valid_prefix_attributes
2370 (cp_parser *, tree *);
2371 static void cp_parser_objc_at_property_declaration
2372 (cp_parser *) ;
2373 static void cp_parser_objc_at_synthesize_declaration
2374 (cp_parser *) ;
2375 static void cp_parser_objc_at_dynamic_declaration
2376 (cp_parser *) ;
2377 static tree cp_parser_objc_struct_declaration
2378 (cp_parser *) ;
2380 /* Utility Routines */
2382 static tree cp_parser_lookup_name
2383 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2384 static tree cp_parser_lookup_name_simple
2385 (cp_parser *, tree, location_t);
2386 static tree cp_parser_maybe_treat_template_as_class
2387 (tree, bool);
2388 static bool cp_parser_check_declarator_template_parameters
2389 (cp_parser *, cp_declarator *, location_t);
2390 static bool cp_parser_check_template_parameters
2391 (cp_parser *, unsigned, location_t, cp_declarator *);
2392 static tree cp_parser_simple_cast_expression
2393 (cp_parser *);
2394 static tree cp_parser_global_scope_opt
2395 (cp_parser *, bool);
2396 static bool cp_parser_constructor_declarator_p
2397 (cp_parser *, bool);
2398 static tree cp_parser_function_definition_from_specifiers_and_declarator
2399 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2400 static tree cp_parser_function_definition_after_declarator
2401 (cp_parser *, bool);
2402 static void cp_parser_template_declaration_after_export
2403 (cp_parser *, bool);
2404 static void cp_parser_perform_template_parameter_access_checks
2405 (vec<deferred_access_check, va_gc> *);
2406 static tree cp_parser_single_declaration
2407 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2408 static tree cp_parser_functional_cast
2409 (cp_parser *, tree);
2410 static tree cp_parser_save_member_function_body
2411 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2412 static tree cp_parser_save_nsdmi
2413 (cp_parser *);
2414 static tree cp_parser_enclosed_template_argument_list
2415 (cp_parser *);
2416 static void cp_parser_save_default_args
2417 (cp_parser *, tree);
2418 static void cp_parser_late_parsing_for_member
2419 (cp_parser *, tree);
2420 static tree cp_parser_late_parse_one_default_arg
2421 (cp_parser *, tree, tree, tree);
2422 static void cp_parser_late_parsing_nsdmi
2423 (cp_parser *, tree);
2424 static void cp_parser_late_parsing_default_args
2425 (cp_parser *, tree);
2426 static tree cp_parser_sizeof_operand
2427 (cp_parser *, enum rid);
2428 static tree cp_parser_trait_expr
2429 (cp_parser *, enum rid);
2430 static bool cp_parser_declares_only_class_p
2431 (cp_parser *);
2432 static void cp_parser_set_storage_class
2433 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2434 static void cp_parser_set_decl_spec_type
2435 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2436 static void set_and_check_decl_spec_loc
2437 (cp_decl_specifier_seq *decl_specs,
2438 cp_decl_spec ds, cp_token *);
2439 static bool cp_parser_friend_p
2440 (const cp_decl_specifier_seq *);
2441 static void cp_parser_required_error
2442 (cp_parser *, required_token, bool);
2443 static cp_token *cp_parser_require
2444 (cp_parser *, enum cpp_ttype, required_token);
2445 static cp_token *cp_parser_require_keyword
2446 (cp_parser *, enum rid, required_token);
2447 static bool cp_parser_token_starts_function_definition_p
2448 (cp_token *);
2449 static bool cp_parser_next_token_starts_class_definition_p
2450 (cp_parser *);
2451 static bool cp_parser_next_token_ends_template_argument_p
2452 (cp_parser *);
2453 static bool cp_parser_nth_token_starts_template_argument_list_p
2454 (cp_parser *, size_t);
2455 static enum tag_types cp_parser_token_is_class_key
2456 (cp_token *);
2457 static enum tag_types cp_parser_token_is_type_parameter_key
2458 (cp_token *);
2459 static void cp_parser_check_class_key
2460 (enum tag_types, tree type);
2461 static void cp_parser_check_access_in_redeclaration
2462 (tree type, location_t location);
2463 static bool cp_parser_optional_template_keyword
2464 (cp_parser *);
2465 static void cp_parser_pre_parsed_nested_name_specifier
2466 (cp_parser *);
2467 static bool cp_parser_cache_group
2468 (cp_parser *, enum cpp_ttype, unsigned);
2469 static tree cp_parser_cache_defarg
2470 (cp_parser *parser, bool nsdmi);
2471 static void cp_parser_parse_tentatively
2472 (cp_parser *);
2473 static void cp_parser_commit_to_tentative_parse
2474 (cp_parser *);
2475 static void cp_parser_commit_to_topmost_tentative_parse
2476 (cp_parser *);
2477 static void cp_parser_abort_tentative_parse
2478 (cp_parser *);
2479 static bool cp_parser_parse_definitely
2480 (cp_parser *);
2481 static inline bool cp_parser_parsing_tentatively
2482 (cp_parser *);
2483 static bool cp_parser_uncommitted_to_tentative_parse_p
2484 (cp_parser *);
2485 static void cp_parser_error
2486 (cp_parser *, const char *);
2487 static void cp_parser_name_lookup_error
2488 (cp_parser *, tree, tree, name_lookup_error, location_t);
2489 static bool cp_parser_simulate_error
2490 (cp_parser *);
2491 static bool cp_parser_check_type_definition
2492 (cp_parser *);
2493 static void cp_parser_check_for_definition_in_return_type
2494 (cp_declarator *, tree, location_t type_location);
2495 static void cp_parser_check_for_invalid_template_id
2496 (cp_parser *, tree, enum tag_types, location_t location);
2497 static bool cp_parser_non_integral_constant_expression
2498 (cp_parser *, non_integral_constant);
2499 static void cp_parser_diagnose_invalid_type_name
2500 (cp_parser *, tree, location_t);
2501 static bool cp_parser_parse_and_diagnose_invalid_type_name
2502 (cp_parser *);
2503 static int cp_parser_skip_to_closing_parenthesis
2504 (cp_parser *, bool, bool, bool);
2505 static void cp_parser_skip_to_end_of_statement
2506 (cp_parser *);
2507 static void cp_parser_consume_semicolon_at_end_of_statement
2508 (cp_parser *);
2509 static void cp_parser_skip_to_end_of_block_or_statement
2510 (cp_parser *);
2511 static bool cp_parser_skip_to_closing_brace
2512 (cp_parser *);
2513 static void cp_parser_skip_to_end_of_template_parameter_list
2514 (cp_parser *);
2515 static void cp_parser_skip_to_pragma_eol
2516 (cp_parser*, cp_token *);
2517 static bool cp_parser_error_occurred
2518 (cp_parser *);
2519 static bool cp_parser_allow_gnu_extensions_p
2520 (cp_parser *);
2521 static bool cp_parser_is_pure_string_literal
2522 (cp_token *);
2523 static bool cp_parser_is_string_literal
2524 (cp_token *);
2525 static bool cp_parser_is_keyword
2526 (cp_token *, enum rid);
2527 static tree cp_parser_make_typename_type
2528 (cp_parser *, tree, location_t location);
2529 static cp_declarator * cp_parser_make_indirect_declarator
2530 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2531 static bool cp_parser_compound_literal_p
2532 (cp_parser *);
2533 static bool cp_parser_array_designator_p
2534 (cp_parser *);
2535 static bool cp_parser_skip_to_closing_square_bracket
2536 (cp_parser *);
2538 /* Returns nonzero if we are parsing tentatively. */
2540 static inline bool
2541 cp_parser_parsing_tentatively (cp_parser* parser)
2543 return parser->context->next != NULL;
2546 /* Returns nonzero if TOKEN is a string literal. */
2548 static bool
2549 cp_parser_is_pure_string_literal (cp_token* token)
2551 return (token->type == CPP_STRING ||
2552 token->type == CPP_STRING16 ||
2553 token->type == CPP_STRING32 ||
2554 token->type == CPP_WSTRING ||
2555 token->type == CPP_UTF8STRING);
2558 /* Returns nonzero if TOKEN is a string literal
2559 of a user-defined string literal. */
2561 static bool
2562 cp_parser_is_string_literal (cp_token* token)
2564 return (cp_parser_is_pure_string_literal (token) ||
2565 token->type == CPP_STRING_USERDEF ||
2566 token->type == CPP_STRING16_USERDEF ||
2567 token->type == CPP_STRING32_USERDEF ||
2568 token->type == CPP_WSTRING_USERDEF ||
2569 token->type == CPP_UTF8STRING_USERDEF);
2572 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2574 static bool
2575 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2577 return token->keyword == keyword;
2580 /* If not parsing tentatively, issue a diagnostic of the form
2581 FILE:LINE: MESSAGE before TOKEN
2582 where TOKEN is the next token in the input stream. MESSAGE
2583 (specified by the caller) is usually of the form "expected
2584 OTHER-TOKEN". */
2586 static void
2587 cp_parser_error (cp_parser* parser, const char* gmsgid)
2589 if (!cp_parser_simulate_error (parser))
2591 cp_token *token = cp_lexer_peek_token (parser->lexer);
2592 /* This diagnostic makes more sense if it is tagged to the line
2593 of the token we just peeked at. */
2594 cp_lexer_set_source_position_from_token (token);
2596 if (token->type == CPP_PRAGMA)
2598 error_at (token->location,
2599 "%<#pragma%> is not allowed here");
2600 cp_parser_skip_to_pragma_eol (parser, token);
2601 return;
2604 c_parse_error (gmsgid,
2605 /* Because c_parser_error does not understand
2606 CPP_KEYWORD, keywords are treated like
2607 identifiers. */
2608 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2609 token->u.value, token->flags);
2613 /* Issue an error about name-lookup failing. NAME is the
2614 IDENTIFIER_NODE DECL is the result of
2615 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2616 the thing that we hoped to find. */
2618 static void
2619 cp_parser_name_lookup_error (cp_parser* parser,
2620 tree name,
2621 tree decl,
2622 name_lookup_error desired,
2623 location_t location)
2625 /* If name lookup completely failed, tell the user that NAME was not
2626 declared. */
2627 if (decl == error_mark_node)
2629 if (parser->scope && parser->scope != global_namespace)
2630 error_at (location, "%<%E::%E%> has not been declared",
2631 parser->scope, name);
2632 else if (parser->scope == global_namespace)
2633 error_at (location, "%<::%E%> has not been declared", name);
2634 else if (parser->object_scope
2635 && !CLASS_TYPE_P (parser->object_scope))
2636 error_at (location, "request for member %qE in non-class type %qT",
2637 name, parser->object_scope);
2638 else if (parser->object_scope)
2639 error_at (location, "%<%T::%E%> has not been declared",
2640 parser->object_scope, name);
2641 else
2642 error_at (location, "%qE has not been declared", name);
2644 else if (parser->scope && parser->scope != global_namespace)
2646 switch (desired)
2648 case NLE_TYPE:
2649 error_at (location, "%<%E::%E%> is not a type",
2650 parser->scope, name);
2651 break;
2652 case NLE_CXX98:
2653 error_at (location, "%<%E::%E%> is not a class or namespace",
2654 parser->scope, name);
2655 break;
2656 case NLE_NOT_CXX98:
2657 error_at (location,
2658 "%<%E::%E%> is not a class, namespace, or enumeration",
2659 parser->scope, name);
2660 break;
2661 default:
2662 gcc_unreachable ();
2666 else if (parser->scope == global_namespace)
2668 switch (desired)
2670 case NLE_TYPE:
2671 error_at (location, "%<::%E%> is not a type", name);
2672 break;
2673 case NLE_CXX98:
2674 error_at (location, "%<::%E%> is not a class or namespace", name);
2675 break;
2676 case NLE_NOT_CXX98:
2677 error_at (location,
2678 "%<::%E%> is not a class, namespace, or enumeration",
2679 name);
2680 break;
2681 default:
2682 gcc_unreachable ();
2685 else
2687 switch (desired)
2689 case NLE_TYPE:
2690 error_at (location, "%qE is not a type", name);
2691 break;
2692 case NLE_CXX98:
2693 error_at (location, "%qE is not a class or namespace", name);
2694 break;
2695 case NLE_NOT_CXX98:
2696 error_at (location,
2697 "%qE is not a class, namespace, or enumeration", name);
2698 break;
2699 default:
2700 gcc_unreachable ();
2705 /* If we are parsing tentatively, remember that an error has occurred
2706 during this tentative parse. Returns true if the error was
2707 simulated; false if a message should be issued by the caller. */
2709 static bool
2710 cp_parser_simulate_error (cp_parser* parser)
2712 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2714 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2715 return true;
2717 return false;
2720 /* This function is called when a type is defined. If type
2721 definitions are forbidden at this point, an error message is
2722 issued. */
2724 static bool
2725 cp_parser_check_type_definition (cp_parser* parser)
2727 /* If types are forbidden here, issue a message. */
2728 if (parser->type_definition_forbidden_message)
2730 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2731 in the message need to be interpreted. */
2732 error (parser->type_definition_forbidden_message);
2733 return false;
2735 return true;
2738 /* This function is called when the DECLARATOR is processed. The TYPE
2739 was a type defined in the decl-specifiers. If it is invalid to
2740 define a type in the decl-specifiers for DECLARATOR, an error is
2741 issued. TYPE_LOCATION is the location of TYPE and is used
2742 for error reporting. */
2744 static void
2745 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2746 tree type, location_t type_location)
2748 /* [dcl.fct] forbids type definitions in return types.
2749 Unfortunately, it's not easy to know whether or not we are
2750 processing a return type until after the fact. */
2751 while (declarator
2752 && (declarator->kind == cdk_pointer
2753 || declarator->kind == cdk_reference
2754 || declarator->kind == cdk_ptrmem))
2755 declarator = declarator->declarator;
2756 if (declarator
2757 && declarator->kind == cdk_function)
2759 error_at (type_location,
2760 "new types may not be defined in a return type");
2761 inform (type_location,
2762 "(perhaps a semicolon is missing after the definition of %qT)",
2763 type);
2767 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2768 "<" in any valid C++ program. If the next token is indeed "<",
2769 issue a message warning the user about what appears to be an
2770 invalid attempt to form a template-id. LOCATION is the location
2771 of the type-specifier (TYPE) */
2773 static void
2774 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2775 tree type,
2776 enum tag_types tag_type,
2777 location_t location)
2779 cp_token_position start = 0;
2781 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2783 if (TYPE_P (type))
2784 error_at (location, "%qT is not a template", type);
2785 else if (identifier_p (type))
2787 if (tag_type != none_type)
2788 error_at (location, "%qE is not a class template", type);
2789 else
2790 error_at (location, "%qE is not a template", type);
2792 else
2793 error_at (location, "invalid template-id");
2794 /* Remember the location of the invalid "<". */
2795 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2796 start = cp_lexer_token_position (parser->lexer, true);
2797 /* Consume the "<". */
2798 cp_lexer_consume_token (parser->lexer);
2799 /* Parse the template arguments. */
2800 cp_parser_enclosed_template_argument_list (parser);
2801 /* Permanently remove the invalid template arguments so that
2802 this error message is not issued again. */
2803 if (start)
2804 cp_lexer_purge_tokens_after (parser->lexer, start);
2808 /* If parsing an integral constant-expression, issue an error message
2809 about the fact that THING appeared and return true. Otherwise,
2810 return false. In either case, set
2811 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2813 static bool
2814 cp_parser_non_integral_constant_expression (cp_parser *parser,
2815 non_integral_constant thing)
2817 parser->non_integral_constant_expression_p = true;
2818 if (parser->integral_constant_expression_p)
2820 if (!parser->allow_non_integral_constant_expression_p)
2822 const char *msg = NULL;
2823 switch (thing)
2825 case NIC_FLOAT:
2826 error ("floating-point literal "
2827 "cannot appear in a constant-expression");
2828 return true;
2829 case NIC_CAST:
2830 error ("a cast to a type other than an integral or "
2831 "enumeration type cannot appear in a "
2832 "constant-expression");
2833 return true;
2834 case NIC_TYPEID:
2835 error ("%<typeid%> operator "
2836 "cannot appear in a constant-expression");
2837 return true;
2838 case NIC_NCC:
2839 error ("non-constant compound literals "
2840 "cannot appear in a constant-expression");
2841 return true;
2842 case NIC_FUNC_CALL:
2843 error ("a function call "
2844 "cannot appear in a constant-expression");
2845 return true;
2846 case NIC_INC:
2847 error ("an increment "
2848 "cannot appear in a constant-expression");
2849 return true;
2850 case NIC_DEC:
2851 error ("an decrement "
2852 "cannot appear in a constant-expression");
2853 return true;
2854 case NIC_ARRAY_REF:
2855 error ("an array reference "
2856 "cannot appear in a constant-expression");
2857 return true;
2858 case NIC_ADDR_LABEL:
2859 error ("the address of a label "
2860 "cannot appear in a constant-expression");
2861 return true;
2862 case NIC_OVERLOADED:
2863 error ("calls to overloaded operators "
2864 "cannot appear in a constant-expression");
2865 return true;
2866 case NIC_ASSIGNMENT:
2867 error ("an assignment cannot appear in a constant-expression");
2868 return true;
2869 case NIC_COMMA:
2870 error ("a comma operator "
2871 "cannot appear in a constant-expression");
2872 return true;
2873 case NIC_CONSTRUCTOR:
2874 error ("a call to a constructor "
2875 "cannot appear in a constant-expression");
2876 return true;
2877 case NIC_TRANSACTION:
2878 error ("a transaction expression "
2879 "cannot appear in a constant-expression");
2880 return true;
2881 case NIC_THIS:
2882 msg = "this";
2883 break;
2884 case NIC_FUNC_NAME:
2885 msg = "__FUNCTION__";
2886 break;
2887 case NIC_PRETTY_FUNC:
2888 msg = "__PRETTY_FUNCTION__";
2889 break;
2890 case NIC_C99_FUNC:
2891 msg = "__func__";
2892 break;
2893 case NIC_VA_ARG:
2894 msg = "va_arg";
2895 break;
2896 case NIC_ARROW:
2897 msg = "->";
2898 break;
2899 case NIC_POINT:
2900 msg = ".";
2901 break;
2902 case NIC_STAR:
2903 msg = "*";
2904 break;
2905 case NIC_ADDR:
2906 msg = "&";
2907 break;
2908 case NIC_PREINCREMENT:
2909 msg = "++";
2910 break;
2911 case NIC_PREDECREMENT:
2912 msg = "--";
2913 break;
2914 case NIC_NEW:
2915 msg = "new";
2916 break;
2917 case NIC_DEL:
2918 msg = "delete";
2919 break;
2920 default:
2921 gcc_unreachable ();
2923 if (msg)
2924 error ("%qs cannot appear in a constant-expression", msg);
2925 return true;
2928 return false;
2931 /* Emit a diagnostic for an invalid type name. This function commits
2932 to the current active tentative parse, if any. (Otherwise, the
2933 problematic construct might be encountered again later, resulting
2934 in duplicate error messages.) LOCATION is the location of ID. */
2936 static void
2937 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2938 location_t location)
2940 tree decl, ambiguous_decls;
2941 cp_parser_commit_to_tentative_parse (parser);
2942 /* Try to lookup the identifier. */
2943 decl = cp_parser_lookup_name (parser, id, none_type,
2944 /*is_template=*/false,
2945 /*is_namespace=*/false,
2946 /*check_dependency=*/true,
2947 &ambiguous_decls, location);
2948 if (ambiguous_decls)
2949 /* If the lookup was ambiguous, an error will already have
2950 been issued. */
2951 return;
2952 /* If the lookup found a template-name, it means that the user forgot
2953 to specify an argument list. Emit a useful error message. */
2954 if (TREE_CODE (decl) == TEMPLATE_DECL)
2955 error_at (location,
2956 "invalid use of template-name %qE without an argument list",
2957 decl);
2958 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2959 error_at (location, "invalid use of destructor %qD as a type", id);
2960 else if (TREE_CODE (decl) == TYPE_DECL)
2961 /* Something like 'unsigned A a;' */
2962 error_at (location, "invalid combination of multiple type-specifiers");
2963 else if (!parser->scope)
2965 /* Issue an error message. */
2966 error_at (location, "%qE does not name a type", id);
2967 /* If we're in a template class, it's possible that the user was
2968 referring to a type from a base class. For example:
2970 template <typename T> struct A { typedef T X; };
2971 template <typename T> struct B : public A<T> { X x; };
2973 The user should have said "typename A<T>::X". */
2974 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2975 inform (location, "C++11 %<constexpr%> only available with "
2976 "-std=c++11 or -std=gnu++11");
2977 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2978 inform (location, "C++11 %<noexcept%> only available with "
2979 "-std=c++11 or -std=gnu++11");
2980 else if (cxx_dialect < cxx11
2981 && TREE_CODE (id) == IDENTIFIER_NODE
2982 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2983 inform (location, "C++11 %<thread_local%> only available with "
2984 "-std=c++11 or -std=gnu++11");
2985 else if (processing_template_decl && current_class_type
2986 && TYPE_BINFO (current_class_type))
2988 tree b;
2990 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2992 b = TREE_CHAIN (b))
2994 tree base_type = BINFO_TYPE (b);
2995 if (CLASS_TYPE_P (base_type)
2996 && dependent_type_p (base_type))
2998 tree field;
2999 /* Go from a particular instantiation of the
3000 template (which will have an empty TYPE_FIELDs),
3001 to the main version. */
3002 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3003 for (field = TYPE_FIELDS (base_type);
3004 field;
3005 field = DECL_CHAIN (field))
3006 if (TREE_CODE (field) == TYPE_DECL
3007 && DECL_NAME (field) == id)
3009 inform (location,
3010 "(perhaps %<typename %T::%E%> was intended)",
3011 BINFO_TYPE (b), id);
3012 break;
3014 if (field)
3015 break;
3020 /* Here we diagnose qualified-ids where the scope is actually correct,
3021 but the identifier does not resolve to a valid type name. */
3022 else if (parser->scope != error_mark_node)
3024 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3026 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3027 error_at (location_of (id),
3028 "%qE in namespace %qE does not name a template type",
3029 id, parser->scope);
3030 else
3031 error_at (location_of (id),
3032 "%qE in namespace %qE does not name a type",
3033 id, parser->scope);
3035 else if (CLASS_TYPE_P (parser->scope)
3036 && constructor_name_p (id, parser->scope))
3038 /* A<T>::A<T>() */
3039 error_at (location, "%<%T::%E%> names the constructor, not"
3040 " the type", parser->scope, id);
3041 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3042 error_at (location, "and %qT has no template constructors",
3043 parser->scope);
3045 else if (TYPE_P (parser->scope)
3046 && dependent_scope_p (parser->scope))
3047 error_at (location, "need %<typename%> before %<%T::%E%> because "
3048 "%qT is a dependent scope",
3049 parser->scope, id, parser->scope);
3050 else if (TYPE_P (parser->scope))
3052 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3053 error_at (location_of (id),
3054 "%qE in %q#T does not name a template type",
3055 id, parser->scope);
3056 else
3057 error_at (location_of (id),
3058 "%qE in %q#T does not name a type",
3059 id, parser->scope);
3061 else
3062 gcc_unreachable ();
3066 /* Check for a common situation where a type-name should be present,
3067 but is not, and issue a sensible error message. Returns true if an
3068 invalid type-name was detected.
3070 The situation handled by this function are variable declarations of the
3071 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3072 Usually, `ID' should name a type, but if we got here it means that it
3073 does not. We try to emit the best possible error message depending on
3074 how exactly the id-expression looks like. */
3076 static bool
3077 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3079 tree id;
3080 cp_token *token = cp_lexer_peek_token (parser->lexer);
3082 /* Avoid duplicate error about ambiguous lookup. */
3083 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3085 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3086 if (next->type == CPP_NAME && next->error_reported)
3087 goto out;
3090 cp_parser_parse_tentatively (parser);
3091 id = cp_parser_id_expression (parser,
3092 /*template_keyword_p=*/false,
3093 /*check_dependency_p=*/true,
3094 /*template_p=*/NULL,
3095 /*declarator_p=*/true,
3096 /*optional_p=*/false);
3097 /* If the next token is a (, this is a function with no explicit return
3098 type, i.e. constructor, destructor or conversion op. */
3099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3100 || TREE_CODE (id) == TYPE_DECL)
3102 cp_parser_abort_tentative_parse (parser);
3103 return false;
3105 if (!cp_parser_parse_definitely (parser))
3106 return false;
3108 /* Emit a diagnostic for the invalid type. */
3109 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3110 out:
3111 /* If we aren't in the middle of a declarator (i.e. in a
3112 parameter-declaration-clause), skip to the end of the declaration;
3113 there's no point in trying to process it. */
3114 if (!parser->in_declarator_p)
3115 cp_parser_skip_to_end_of_block_or_statement (parser);
3116 return true;
3119 /* Consume tokens up to, and including, the next non-nested closing `)'.
3120 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3121 are doing error recovery. Returns -1 if OR_COMMA is true and we
3122 found an unnested comma. */
3124 static int
3125 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3126 bool recovering,
3127 bool or_comma,
3128 bool consume_paren)
3130 unsigned paren_depth = 0;
3131 unsigned brace_depth = 0;
3132 unsigned square_depth = 0;
3134 if (recovering && !or_comma
3135 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3136 return 0;
3138 while (true)
3140 cp_token * token = cp_lexer_peek_token (parser->lexer);
3142 switch (token->type)
3144 case CPP_EOF:
3145 case CPP_PRAGMA_EOL:
3146 /* If we've run out of tokens, then there is no closing `)'. */
3147 return 0;
3149 /* This is good for lambda expression capture-lists. */
3150 case CPP_OPEN_SQUARE:
3151 ++square_depth;
3152 break;
3153 case CPP_CLOSE_SQUARE:
3154 if (!square_depth--)
3155 return 0;
3156 break;
3158 case CPP_SEMICOLON:
3159 /* This matches the processing in skip_to_end_of_statement. */
3160 if (!brace_depth)
3161 return 0;
3162 break;
3164 case CPP_OPEN_BRACE:
3165 ++brace_depth;
3166 break;
3167 case CPP_CLOSE_BRACE:
3168 if (!brace_depth--)
3169 return 0;
3170 break;
3172 case CPP_COMMA:
3173 if (recovering && or_comma && !brace_depth && !paren_depth
3174 && !square_depth)
3175 return -1;
3176 break;
3178 case CPP_OPEN_PAREN:
3179 if (!brace_depth)
3180 ++paren_depth;
3181 break;
3183 case CPP_CLOSE_PAREN:
3184 if (!brace_depth && !paren_depth--)
3186 if (consume_paren)
3187 cp_lexer_consume_token (parser->lexer);
3188 return 1;
3190 break;
3192 default:
3193 break;
3196 /* Consume the token. */
3197 cp_lexer_consume_token (parser->lexer);
3201 /* Consume tokens until we reach the end of the current statement.
3202 Normally, that will be just before consuming a `;'. However, if a
3203 non-nested `}' comes first, then we stop before consuming that. */
3205 static void
3206 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3208 unsigned nesting_depth = 0;
3210 /* Unwind generic function template scope if necessary. */
3211 if (parser->fully_implicit_function_template_p)
3212 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3214 while (true)
3216 cp_token *token = cp_lexer_peek_token (parser->lexer);
3218 switch (token->type)
3220 case CPP_EOF:
3221 case CPP_PRAGMA_EOL:
3222 /* If we've run out of tokens, stop. */
3223 return;
3225 case CPP_SEMICOLON:
3226 /* If the next token is a `;', we have reached the end of the
3227 statement. */
3228 if (!nesting_depth)
3229 return;
3230 break;
3232 case CPP_CLOSE_BRACE:
3233 /* If this is a non-nested '}', stop before consuming it.
3234 That way, when confronted with something like:
3236 { 3 + }
3238 we stop before consuming the closing '}', even though we
3239 have not yet reached a `;'. */
3240 if (nesting_depth == 0)
3241 return;
3243 /* If it is the closing '}' for a block that we have
3244 scanned, stop -- but only after consuming the token.
3245 That way given:
3247 void f g () { ... }
3248 typedef int I;
3250 we will stop after the body of the erroneously declared
3251 function, but before consuming the following `typedef'
3252 declaration. */
3253 if (--nesting_depth == 0)
3255 cp_lexer_consume_token (parser->lexer);
3256 return;
3259 case CPP_OPEN_BRACE:
3260 ++nesting_depth;
3261 break;
3263 default:
3264 break;
3267 /* Consume the token. */
3268 cp_lexer_consume_token (parser->lexer);
3272 /* This function is called at the end of a statement or declaration.
3273 If the next token is a semicolon, it is consumed; otherwise, error
3274 recovery is attempted. */
3276 static void
3277 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3279 /* Look for the trailing `;'. */
3280 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3282 /* If there is additional (erroneous) input, skip to the end of
3283 the statement. */
3284 cp_parser_skip_to_end_of_statement (parser);
3285 /* If the next token is now a `;', consume it. */
3286 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3287 cp_lexer_consume_token (parser->lexer);
3291 /* Skip tokens until we have consumed an entire block, or until we
3292 have consumed a non-nested `;'. */
3294 static void
3295 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3297 int nesting_depth = 0;
3299 /* Unwind generic function template scope if necessary. */
3300 if (parser->fully_implicit_function_template_p)
3301 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3303 while (nesting_depth >= 0)
3305 cp_token *token = cp_lexer_peek_token (parser->lexer);
3307 switch (token->type)
3309 case CPP_EOF:
3310 case CPP_PRAGMA_EOL:
3311 /* If we've run out of tokens, stop. */
3312 return;
3314 case CPP_SEMICOLON:
3315 /* Stop if this is an unnested ';'. */
3316 if (!nesting_depth)
3317 nesting_depth = -1;
3318 break;
3320 case CPP_CLOSE_BRACE:
3321 /* Stop if this is an unnested '}', or closes the outermost
3322 nesting level. */
3323 nesting_depth--;
3324 if (nesting_depth < 0)
3325 return;
3326 if (!nesting_depth)
3327 nesting_depth = -1;
3328 break;
3330 case CPP_OPEN_BRACE:
3331 /* Nest. */
3332 nesting_depth++;
3333 break;
3335 default:
3336 break;
3339 /* Consume the token. */
3340 cp_lexer_consume_token (parser->lexer);
3344 /* Skip tokens until a non-nested closing curly brace is the next
3345 token, or there are no more tokens. Return true in the first case,
3346 false otherwise. */
3348 static bool
3349 cp_parser_skip_to_closing_brace (cp_parser *parser)
3351 unsigned nesting_depth = 0;
3353 while (true)
3355 cp_token *token = cp_lexer_peek_token (parser->lexer);
3357 switch (token->type)
3359 case CPP_EOF:
3360 case CPP_PRAGMA_EOL:
3361 /* If we've run out of tokens, stop. */
3362 return false;
3364 case CPP_CLOSE_BRACE:
3365 /* If the next token is a non-nested `}', then we have reached
3366 the end of the current block. */
3367 if (nesting_depth-- == 0)
3368 return true;
3369 break;
3371 case CPP_OPEN_BRACE:
3372 /* If it the next token is a `{', then we are entering a new
3373 block. Consume the entire block. */
3374 ++nesting_depth;
3375 break;
3377 default:
3378 break;
3381 /* Consume the token. */
3382 cp_lexer_consume_token (parser->lexer);
3386 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3387 parameter is the PRAGMA token, allowing us to purge the entire pragma
3388 sequence. */
3390 static void
3391 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3393 cp_token *token;
3395 parser->lexer->in_pragma = false;
3398 token = cp_lexer_consume_token (parser->lexer);
3399 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3401 /* Ensure that the pragma is not parsed again. */
3402 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3405 /* Require pragma end of line, resyncing with it as necessary. The
3406 arguments are as for cp_parser_skip_to_pragma_eol. */
3408 static void
3409 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3411 parser->lexer->in_pragma = false;
3412 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3413 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3416 /* This is a simple wrapper around make_typename_type. When the id is
3417 an unresolved identifier node, we can provide a superior diagnostic
3418 using cp_parser_diagnose_invalid_type_name. */
3420 static tree
3421 cp_parser_make_typename_type (cp_parser *parser, tree id,
3422 location_t id_location)
3424 tree result;
3425 if (identifier_p (id))
3427 result = make_typename_type (parser->scope, id, typename_type,
3428 /*complain=*/tf_none);
3429 if (result == error_mark_node)
3430 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3431 return result;
3433 return make_typename_type (parser->scope, id, typename_type, tf_error);
3436 /* This is a wrapper around the
3437 make_{pointer,ptrmem,reference}_declarator functions that decides
3438 which one to call based on the CODE and CLASS_TYPE arguments. The
3439 CODE argument should be one of the values returned by
3440 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3441 appertain to the pointer or reference. */
3443 static cp_declarator *
3444 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3445 cp_cv_quals cv_qualifiers,
3446 cp_declarator *target,
3447 tree attributes)
3449 if (code == ERROR_MARK)
3450 return cp_error_declarator;
3452 if (code == INDIRECT_REF)
3453 if (class_type == NULL_TREE)
3454 return make_pointer_declarator (cv_qualifiers, target, attributes);
3455 else
3456 return make_ptrmem_declarator (cv_qualifiers, class_type,
3457 target, attributes);
3458 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3459 return make_reference_declarator (cv_qualifiers, target,
3460 false, attributes);
3461 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3462 return make_reference_declarator (cv_qualifiers, target,
3463 true, attributes);
3464 gcc_unreachable ();
3467 /* Create a new C++ parser. */
3469 static cp_parser *
3470 cp_parser_new (void)
3472 cp_parser *parser;
3473 cp_lexer *lexer;
3474 unsigned i;
3476 /* cp_lexer_new_main is called before doing GC allocation because
3477 cp_lexer_new_main might load a PCH file. */
3478 lexer = cp_lexer_new_main ();
3480 /* Initialize the binops_by_token so that we can get the tree
3481 directly from the token. */
3482 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3483 binops_by_token[binops[i].token_type] = binops[i];
3485 parser = ggc_cleared_alloc<cp_parser> ();
3486 parser->lexer = lexer;
3487 parser->context = cp_parser_context_new (NULL);
3489 /* For now, we always accept GNU extensions. */
3490 parser->allow_gnu_extensions_p = 1;
3492 /* The `>' token is a greater-than operator, not the end of a
3493 template-id. */
3494 parser->greater_than_is_operator_p = true;
3496 parser->default_arg_ok_p = true;
3498 /* We are not parsing a constant-expression. */
3499 parser->integral_constant_expression_p = false;
3500 parser->allow_non_integral_constant_expression_p = false;
3501 parser->non_integral_constant_expression_p = false;
3503 /* Local variable names are not forbidden. */
3504 parser->local_variables_forbidden_p = false;
3506 /* We are not processing an `extern "C"' declaration. */
3507 parser->in_unbraced_linkage_specification_p = false;
3509 /* We are not processing a declarator. */
3510 parser->in_declarator_p = false;
3512 /* We are not processing a template-argument-list. */
3513 parser->in_template_argument_list_p = false;
3515 /* We are not in an iteration statement. */
3516 parser->in_statement = 0;
3518 /* We are not in a switch statement. */
3519 parser->in_switch_statement_p = false;
3521 /* We are not parsing a type-id inside an expression. */
3522 parser->in_type_id_in_expr_p = false;
3524 /* Declarations aren't implicitly extern "C". */
3525 parser->implicit_extern_c = false;
3527 /* String literals should be translated to the execution character set. */
3528 parser->translate_strings_p = true;
3530 /* We are not parsing a function body. */
3531 parser->in_function_body = false;
3533 /* We can correct until told otherwise. */
3534 parser->colon_corrects_to_scope_p = true;
3536 /* The unparsed function queue is empty. */
3537 push_unparsed_function_queues (parser);
3539 /* There are no classes being defined. */
3540 parser->num_classes_being_defined = 0;
3542 /* No template parameters apply. */
3543 parser->num_template_parameter_lists = 0;
3545 /* Not declaring an implicit function template. */
3546 parser->auto_is_implicit_function_template_parm_p = false;
3547 parser->fully_implicit_function_template_p = false;
3548 parser->implicit_template_parms = 0;
3549 parser->implicit_template_scope = 0;
3551 return parser;
3554 /* Create a cp_lexer structure which will emit the tokens in CACHE
3555 and push it onto the parser's lexer stack. This is used for delayed
3556 parsing of in-class method bodies and default arguments, and should
3557 not be confused with tentative parsing. */
3558 static void
3559 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3561 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3562 lexer->next = parser->lexer;
3563 parser->lexer = lexer;
3565 /* Move the current source position to that of the first token in the
3566 new lexer. */
3567 cp_lexer_set_source_position_from_token (lexer->next_token);
3570 /* Pop the top lexer off the parser stack. This is never used for the
3571 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3572 static void
3573 cp_parser_pop_lexer (cp_parser *parser)
3575 cp_lexer *lexer = parser->lexer;
3576 parser->lexer = lexer->next;
3577 cp_lexer_destroy (lexer);
3579 /* Put the current source position back where it was before this
3580 lexer was pushed. */
3581 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3584 /* Lexical conventions [gram.lex] */
3586 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3587 identifier. */
3589 static tree
3590 cp_parser_identifier (cp_parser* parser)
3592 cp_token *token;
3594 /* Look for the identifier. */
3595 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3596 /* Return the value. */
3597 return token ? token->u.value : error_mark_node;
3600 /* Parse a sequence of adjacent string constants. Returns a
3601 TREE_STRING representing the combined, nul-terminated string
3602 constant. If TRANSLATE is true, translate the string to the
3603 execution character set. If WIDE_OK is true, a wide string is
3604 invalid here.
3606 C++98 [lex.string] says that if a narrow string literal token is
3607 adjacent to a wide string literal token, the behavior is undefined.
3608 However, C99 6.4.5p4 says that this results in a wide string literal.
3609 We follow C99 here, for consistency with the C front end.
3611 This code is largely lifted from lex_string() in c-lex.c.
3613 FUTURE: ObjC++ will need to handle @-strings here. */
3614 static tree
3615 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3616 bool lookup_udlit = true)
3618 tree value;
3619 size_t count;
3620 struct obstack str_ob;
3621 cpp_string str, istr, *strs;
3622 cp_token *tok;
3623 enum cpp_ttype type, curr_type;
3624 int have_suffix_p = 0;
3625 tree string_tree;
3626 tree suffix_id = NULL_TREE;
3627 bool curr_tok_is_userdef_p = false;
3629 tok = cp_lexer_peek_token (parser->lexer);
3630 if (!cp_parser_is_string_literal (tok))
3632 cp_parser_error (parser, "expected string-literal");
3633 return error_mark_node;
3636 if (cpp_userdef_string_p (tok->type))
3638 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3639 curr_type = cpp_userdef_string_remove_type (tok->type);
3640 curr_tok_is_userdef_p = true;
3642 else
3644 string_tree = tok->u.value;
3645 curr_type = tok->type;
3647 type = curr_type;
3649 /* Try to avoid the overhead of creating and destroying an obstack
3650 for the common case of just one string. */
3651 if (!cp_parser_is_string_literal
3652 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3654 cp_lexer_consume_token (parser->lexer);
3656 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3657 str.len = TREE_STRING_LENGTH (string_tree);
3658 count = 1;
3660 if (curr_tok_is_userdef_p)
3662 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3663 have_suffix_p = 1;
3664 curr_type = cpp_userdef_string_remove_type (tok->type);
3666 else
3667 curr_type = tok->type;
3669 strs = &str;
3671 else
3673 gcc_obstack_init (&str_ob);
3674 count = 0;
3678 cp_lexer_consume_token (parser->lexer);
3679 count++;
3680 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3681 str.len = TREE_STRING_LENGTH (string_tree);
3683 if (curr_tok_is_userdef_p)
3685 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3686 if (have_suffix_p == 0)
3688 suffix_id = curr_suffix_id;
3689 have_suffix_p = 1;
3691 else if (have_suffix_p == 1
3692 && curr_suffix_id != suffix_id)
3694 error ("inconsistent user-defined literal suffixes"
3695 " %qD and %qD in string literal",
3696 suffix_id, curr_suffix_id);
3697 have_suffix_p = -1;
3699 curr_type = cpp_userdef_string_remove_type (tok->type);
3701 else
3702 curr_type = tok->type;
3704 if (type != curr_type)
3706 if (type == CPP_STRING)
3707 type = curr_type;
3708 else if (curr_type != CPP_STRING)
3709 error_at (tok->location,
3710 "unsupported non-standard concatenation "
3711 "of string literals");
3714 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3716 tok = cp_lexer_peek_token (parser->lexer);
3717 if (cpp_userdef_string_p (tok->type))
3719 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3720 curr_type = cpp_userdef_string_remove_type (tok->type);
3721 curr_tok_is_userdef_p = true;
3723 else
3725 string_tree = tok->u.value;
3726 curr_type = tok->type;
3727 curr_tok_is_userdef_p = false;
3730 while (cp_parser_is_string_literal (tok));
3732 strs = (cpp_string *) obstack_finish (&str_ob);
3735 if (type != CPP_STRING && !wide_ok)
3737 cp_parser_error (parser, "a wide string is invalid in this context");
3738 type = CPP_STRING;
3741 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3742 (parse_in, strs, count, &istr, type))
3744 value = build_string (istr.len, (const char *)istr.text);
3745 free (CONST_CAST (unsigned char *, istr.text));
3747 switch (type)
3749 default:
3750 case CPP_STRING:
3751 case CPP_UTF8STRING:
3752 TREE_TYPE (value) = char_array_type_node;
3753 break;
3754 case CPP_STRING16:
3755 TREE_TYPE (value) = char16_array_type_node;
3756 break;
3757 case CPP_STRING32:
3758 TREE_TYPE (value) = char32_array_type_node;
3759 break;
3760 case CPP_WSTRING:
3761 TREE_TYPE (value) = wchar_array_type_node;
3762 break;
3765 value = fix_string_type (value);
3767 if (have_suffix_p)
3769 tree literal = build_userdef_literal (suffix_id, value,
3770 OT_NONE, NULL_TREE);
3771 if (lookup_udlit)
3772 value = cp_parser_userdef_string_literal (literal);
3773 else
3774 value = literal;
3777 else
3778 /* cpp_interpret_string has issued an error. */
3779 value = error_mark_node;
3781 if (count > 1)
3782 obstack_free (&str_ob, 0);
3784 return value;
3787 /* Look up a literal operator with the name and the exact arguments. */
3789 static tree
3790 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3792 tree decl, fns;
3793 decl = lookup_name (name);
3794 if (!decl || !is_overloaded_fn (decl))
3795 return error_mark_node;
3797 for (fns = decl; fns; fns = OVL_NEXT (fns))
3799 unsigned int ix;
3800 bool found = true;
3801 tree fn = OVL_CURRENT (fns);
3802 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3803 if (parmtypes != NULL_TREE)
3805 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3806 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3808 tree tparm = TREE_VALUE (parmtypes);
3809 tree targ = TREE_TYPE ((*args)[ix]);
3810 bool ptr = TYPE_PTR_P (tparm);
3811 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3812 if ((ptr || arr || !same_type_p (tparm, targ))
3813 && (!ptr || !arr
3814 || !same_type_p (TREE_TYPE (tparm),
3815 TREE_TYPE (targ))))
3816 found = false;
3818 if (found
3819 && ix == vec_safe_length (args)
3820 /* May be this should be sufficient_parms_p instead,
3821 depending on how exactly should user-defined literals
3822 work in presence of default arguments on the literal
3823 operator parameters. */
3824 && parmtypes == void_list_node)
3825 return fn;
3829 return error_mark_node;
3832 /* Parse a user-defined char constant. Returns a call to a user-defined
3833 literal operator taking the character as an argument. */
3835 static tree
3836 cp_parser_userdef_char_literal (cp_parser *parser)
3838 cp_token *token = cp_lexer_consume_token (parser->lexer);
3839 tree literal = token->u.value;
3840 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3841 tree value = USERDEF_LITERAL_VALUE (literal);
3842 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3843 tree decl, result;
3845 /* Build up a call to the user-defined operator */
3846 /* Lookup the name we got back from the id-expression. */
3847 vec<tree, va_gc> *args = make_tree_vector ();
3848 vec_safe_push (args, value);
3849 decl = lookup_literal_operator (name, args);
3850 if (!decl || decl == error_mark_node)
3852 error ("unable to find character literal operator %qD with %qT argument",
3853 name, TREE_TYPE (value));
3854 release_tree_vector (args);
3855 return error_mark_node;
3857 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3858 release_tree_vector (args);
3859 if (result != error_mark_node)
3860 return result;
3862 error ("unable to find character literal operator %qD with %qT argument",
3863 name, TREE_TYPE (value));
3864 return error_mark_node;
3867 /* A subroutine of cp_parser_userdef_numeric_literal to
3868 create a char... template parameter pack from a string node. */
3870 static tree
3871 make_char_string_pack (tree value)
3873 tree charvec;
3874 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3875 const char *str = TREE_STRING_POINTER (value);
3876 int i, len = TREE_STRING_LENGTH (value) - 1;
3877 tree argvec = make_tree_vec (1);
3879 /* Fill in CHARVEC with all of the parameters. */
3880 charvec = make_tree_vec (len);
3881 for (i = 0; i < len; ++i)
3882 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3884 /* Build the argument packs. */
3885 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3886 TREE_TYPE (argpack) = char_type_node;
3888 TREE_VEC_ELT (argvec, 0) = argpack;
3890 return argvec;
3893 /* A subroutine of cp_parser_userdef_numeric_literal to
3894 create a char... template parameter pack from a string node. */
3896 static tree
3897 make_string_pack (tree value)
3899 tree charvec;
3900 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3901 const unsigned char *str
3902 = (const unsigned char *) TREE_STRING_POINTER (value);
3903 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3904 int len = TREE_STRING_LENGTH (value) / sz - 1;
3905 tree argvec = make_tree_vec (2);
3907 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3908 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3910 /* First template parm is character type. */
3911 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3913 /* Fill in CHARVEC with all of the parameters. */
3914 charvec = make_tree_vec (len);
3915 for (int i = 0; i < len; ++i)
3916 TREE_VEC_ELT (charvec, i)
3917 = double_int_to_tree (str_char_type_node,
3918 double_int::from_buffer (str + i * sz, sz));
3920 /* Build the argument packs. */
3921 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3922 TREE_TYPE (argpack) = str_char_type_node;
3924 TREE_VEC_ELT (argvec, 1) = argpack;
3926 return argvec;
3929 /* Parse a user-defined numeric constant. returns a call to a user-defined
3930 literal operator. */
3932 static tree
3933 cp_parser_userdef_numeric_literal (cp_parser *parser)
3935 cp_token *token = cp_lexer_consume_token (parser->lexer);
3936 tree literal = token->u.value;
3937 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3938 tree value = USERDEF_LITERAL_VALUE (literal);
3939 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3940 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3941 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3942 tree decl, result;
3943 vec<tree, va_gc> *args;
3945 /* Look for a literal operator taking the exact type of numeric argument
3946 as the literal value. */
3947 args = make_tree_vector ();
3948 vec_safe_push (args, value);
3949 decl = lookup_literal_operator (name, args);
3950 if (decl && decl != error_mark_node)
3952 result = finish_call_expr (decl, &args, false, true, tf_none);
3953 if (result != error_mark_node)
3955 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3956 warning_at (token->location, OPT_Woverflow,
3957 "integer literal exceeds range of %qT type",
3958 long_long_unsigned_type_node);
3959 else
3961 if (overflow > 0)
3962 warning_at (token->location, OPT_Woverflow,
3963 "floating literal exceeds range of %qT type",
3964 long_double_type_node);
3965 else if (overflow < 0)
3966 warning_at (token->location, OPT_Woverflow,
3967 "floating literal truncated to zero");
3969 release_tree_vector (args);
3970 return result;
3973 release_tree_vector (args);
3975 /* If the numeric argument didn't work, look for a raw literal
3976 operator taking a const char* argument consisting of the number
3977 in string format. */
3978 args = make_tree_vector ();
3979 vec_safe_push (args, num_string);
3980 decl = lookup_literal_operator (name, args);
3981 if (decl && decl != error_mark_node)
3983 result = finish_call_expr (decl, &args, false, true, tf_none);
3984 if (result != error_mark_node)
3986 release_tree_vector (args);
3987 return result;
3990 release_tree_vector (args);
3992 /* If the raw literal didn't work, look for a non-type template
3993 function with parameter pack char.... Call the function with
3994 template parameter characters representing the number. */
3995 args = make_tree_vector ();
3996 decl = lookup_literal_operator (name, args);
3997 if (decl && decl != error_mark_node)
3999 tree tmpl_args = make_char_string_pack (num_string);
4000 decl = lookup_template_function (decl, tmpl_args);
4001 result = finish_call_expr (decl, &args, false, true, tf_none);
4002 if (result != error_mark_node)
4004 release_tree_vector (args);
4005 return result;
4008 release_tree_vector (args);
4010 error ("unable to find numeric literal operator %qD", name);
4011 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4012 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4013 "to enable more built-in suffixes");
4014 return error_mark_node;
4017 /* Parse a user-defined string constant. Returns a call to a user-defined
4018 literal operator taking a character pointer and the length of the string
4019 as arguments. */
4021 static tree
4022 cp_parser_userdef_string_literal (tree literal)
4024 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4025 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4026 tree value = USERDEF_LITERAL_VALUE (literal);
4027 int len = TREE_STRING_LENGTH (value)
4028 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4029 tree decl, result;
4030 vec<tree, va_gc> *args;
4032 /* Look for a template function with typename parameter CharT
4033 and parameter pack CharT... Call the function with
4034 template parameter characters representing the string. */
4035 args = make_tree_vector ();
4036 decl = lookup_literal_operator (name, args);
4037 if (decl && decl != error_mark_node)
4039 tree tmpl_args = make_string_pack (value);
4040 decl = lookup_template_function (decl, tmpl_args);
4041 result = finish_call_expr (decl, &args, false, true, tf_none);
4042 if (result != error_mark_node)
4044 release_tree_vector (args);
4045 return result;
4048 release_tree_vector (args);
4050 /* Build up a call to the user-defined operator */
4051 /* Lookup the name we got back from the id-expression. */
4052 args = make_tree_vector ();
4053 vec_safe_push (args, value);
4054 vec_safe_push (args, build_int_cst (size_type_node, len));
4055 decl = lookup_name (name);
4056 if (!decl || decl == error_mark_node)
4058 error ("unable to find string literal operator %qD", name);
4059 release_tree_vector (args);
4060 return error_mark_node;
4062 result = finish_call_expr (decl, &args, false, true, tf_none);
4063 release_tree_vector (args);
4064 if (result != error_mark_node)
4065 return result;
4067 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4068 name, TREE_TYPE (value), size_type_node);
4069 return error_mark_node;
4073 /* Basic concepts [gram.basic] */
4075 /* Parse a translation-unit.
4077 translation-unit:
4078 declaration-seq [opt]
4080 Returns TRUE if all went well. */
4082 static bool
4083 cp_parser_translation_unit (cp_parser* parser)
4085 /* The address of the first non-permanent object on the declarator
4086 obstack. */
4087 static void *declarator_obstack_base;
4089 bool success;
4091 /* Create the declarator obstack, if necessary. */
4092 if (!cp_error_declarator)
4094 gcc_obstack_init (&declarator_obstack);
4095 /* Create the error declarator. */
4096 cp_error_declarator = make_declarator (cdk_error);
4097 /* Create the empty parameter list. */
4098 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4099 /* Remember where the base of the declarator obstack lies. */
4100 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4103 cp_parser_declaration_seq_opt (parser);
4105 /* If there are no tokens left then all went well. */
4106 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4108 /* Get rid of the token array; we don't need it any more. */
4109 cp_lexer_destroy (parser->lexer);
4110 parser->lexer = NULL;
4112 /* This file might have been a context that's implicitly extern
4113 "C". If so, pop the lang context. (Only relevant for PCH.) */
4114 if (parser->implicit_extern_c)
4116 pop_lang_context ();
4117 parser->implicit_extern_c = false;
4120 /* Finish up. */
4121 finish_translation_unit ();
4123 success = true;
4125 else
4127 cp_parser_error (parser, "expected declaration");
4128 success = false;
4131 /* Make sure the declarator obstack was fully cleaned up. */
4132 gcc_assert (obstack_next_free (&declarator_obstack)
4133 == declarator_obstack_base);
4135 /* All went well. */
4136 return success;
4139 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4140 decltype context. */
4142 static inline tsubst_flags_t
4143 complain_flags (bool decltype_p)
4145 tsubst_flags_t complain = tf_warning_or_error;
4146 if (decltype_p)
4147 complain |= tf_decltype;
4148 return complain;
4151 /* We're about to parse a collection of statements. If we're currently
4152 parsing tentatively, set up a firewall so that any nested
4153 cp_parser_commit_to_tentative_parse won't affect the current context. */
4155 static cp_token_position
4156 cp_parser_start_tentative_firewall (cp_parser *parser)
4158 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4159 return 0;
4161 cp_parser_parse_tentatively (parser);
4162 cp_parser_commit_to_topmost_tentative_parse (parser);
4163 return cp_lexer_token_position (parser->lexer, false);
4166 /* We've finished parsing the collection of statements. Wrap up the
4167 firewall and replace the relevant tokens with the parsed form. */
4169 static void
4170 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4171 tree expr)
4173 if (!start)
4174 return;
4176 /* Finish the firewall level. */
4177 cp_parser_parse_definitely (parser);
4178 /* And remember the result of the parse for when we try again. */
4179 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4180 token->type = CPP_PREPARSED_EXPR;
4181 token->u.value = expr;
4182 token->keyword = RID_MAX;
4183 cp_lexer_purge_tokens_after (parser->lexer, start);
4186 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4187 enclosing parentheses. */
4189 static tree
4190 cp_parser_statement_expr (cp_parser *parser)
4192 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4194 /* Consume the '('. */
4195 cp_lexer_consume_token (parser->lexer);
4196 /* Start the statement-expression. */
4197 tree expr = begin_stmt_expr ();
4198 /* Parse the compound-statement. */
4199 cp_parser_compound_statement (parser, expr, false, false);
4200 /* Finish up. */
4201 expr = finish_stmt_expr (expr, false);
4202 /* Consume the ')'. */
4203 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4204 cp_parser_skip_to_end_of_statement (parser);
4206 cp_parser_end_tentative_firewall (parser, start, expr);
4207 return expr;
4210 /* Expressions [gram.expr] */
4212 /* Parse a primary-expression.
4214 primary-expression:
4215 literal
4216 this
4217 ( expression )
4218 id-expression
4219 lambda-expression (C++11)
4221 GNU Extensions:
4223 primary-expression:
4224 ( compound-statement )
4225 __builtin_va_arg ( assignment-expression , type-id )
4226 __builtin_offsetof ( type-id , offsetof-expression )
4228 C++ Extensions:
4229 __has_nothrow_assign ( type-id )
4230 __has_nothrow_constructor ( type-id )
4231 __has_nothrow_copy ( type-id )
4232 __has_trivial_assign ( type-id )
4233 __has_trivial_constructor ( type-id )
4234 __has_trivial_copy ( type-id )
4235 __has_trivial_destructor ( type-id )
4236 __has_virtual_destructor ( type-id )
4237 __is_abstract ( type-id )
4238 __is_base_of ( type-id , type-id )
4239 __is_class ( type-id )
4240 __is_empty ( type-id )
4241 __is_enum ( type-id )
4242 __is_final ( type-id )
4243 __is_literal_type ( type-id )
4244 __is_pod ( type-id )
4245 __is_polymorphic ( type-id )
4246 __is_std_layout ( type-id )
4247 __is_trivial ( type-id )
4248 __is_union ( type-id )
4250 Objective-C++ Extension:
4252 primary-expression:
4253 objc-expression
4255 literal:
4256 __null
4258 ADDRESS_P is true iff this expression was immediately preceded by
4259 "&" and therefore might denote a pointer-to-member. CAST_P is true
4260 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4261 true iff this expression is a template argument.
4263 Returns a representation of the expression. Upon return, *IDK
4264 indicates what kind of id-expression (if any) was present. */
4266 static tree
4267 cp_parser_primary_expression (cp_parser *parser,
4268 bool address_p,
4269 bool cast_p,
4270 bool template_arg_p,
4271 bool decltype_p,
4272 cp_id_kind *idk)
4274 cp_token *token = NULL;
4276 /* Assume the primary expression is not an id-expression. */
4277 *idk = CP_ID_KIND_NONE;
4279 /* Peek at the next token. */
4280 token = cp_lexer_peek_token (parser->lexer);
4281 switch ((int) token->type)
4283 /* literal:
4284 integer-literal
4285 character-literal
4286 floating-literal
4287 string-literal
4288 boolean-literal
4289 pointer-literal
4290 user-defined-literal */
4291 case CPP_CHAR:
4292 case CPP_CHAR16:
4293 case CPP_CHAR32:
4294 case CPP_WCHAR:
4295 case CPP_NUMBER:
4296 case CPP_PREPARSED_EXPR:
4297 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4298 return cp_parser_userdef_numeric_literal (parser);
4299 token = cp_lexer_consume_token (parser->lexer);
4300 if (TREE_CODE (token->u.value) == FIXED_CST)
4302 error_at (token->location,
4303 "fixed-point types not supported in C++");
4304 return error_mark_node;
4306 /* Floating-point literals are only allowed in an integral
4307 constant expression if they are cast to an integral or
4308 enumeration type. */
4309 if (TREE_CODE (token->u.value) == REAL_CST
4310 && parser->integral_constant_expression_p
4311 && pedantic)
4313 /* CAST_P will be set even in invalid code like "int(2.7 +
4314 ...)". Therefore, we have to check that the next token
4315 is sure to end the cast. */
4316 if (cast_p)
4318 cp_token *next_token;
4320 next_token = cp_lexer_peek_token (parser->lexer);
4321 if (/* The comma at the end of an
4322 enumerator-definition. */
4323 next_token->type != CPP_COMMA
4324 /* The curly brace at the end of an enum-specifier. */
4325 && next_token->type != CPP_CLOSE_BRACE
4326 /* The end of a statement. */
4327 && next_token->type != CPP_SEMICOLON
4328 /* The end of the cast-expression. */
4329 && next_token->type != CPP_CLOSE_PAREN
4330 /* The end of an array bound. */
4331 && next_token->type != CPP_CLOSE_SQUARE
4332 /* The closing ">" in a template-argument-list. */
4333 && (next_token->type != CPP_GREATER
4334 || parser->greater_than_is_operator_p)
4335 /* C++0x only: A ">>" treated like two ">" tokens,
4336 in a template-argument-list. */
4337 && (next_token->type != CPP_RSHIFT
4338 || (cxx_dialect == cxx98)
4339 || parser->greater_than_is_operator_p))
4340 cast_p = false;
4343 /* If we are within a cast, then the constraint that the
4344 cast is to an integral or enumeration type will be
4345 checked at that point. If we are not within a cast, then
4346 this code is invalid. */
4347 if (!cast_p)
4348 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4350 return token->u.value;
4352 case CPP_CHAR_USERDEF:
4353 case CPP_CHAR16_USERDEF:
4354 case CPP_CHAR32_USERDEF:
4355 case CPP_WCHAR_USERDEF:
4356 return cp_parser_userdef_char_literal (parser);
4358 case CPP_STRING:
4359 case CPP_STRING16:
4360 case CPP_STRING32:
4361 case CPP_WSTRING:
4362 case CPP_UTF8STRING:
4363 case CPP_STRING_USERDEF:
4364 case CPP_STRING16_USERDEF:
4365 case CPP_STRING32_USERDEF:
4366 case CPP_WSTRING_USERDEF:
4367 case CPP_UTF8STRING_USERDEF:
4368 /* ??? Should wide strings be allowed when parser->translate_strings_p
4369 is false (i.e. in attributes)? If not, we can kill the third
4370 argument to cp_parser_string_literal. */
4371 return cp_parser_string_literal (parser,
4372 parser->translate_strings_p,
4373 true);
4375 case CPP_OPEN_PAREN:
4376 /* If we see `( { ' then we are looking at the beginning of
4377 a GNU statement-expression. */
4378 if (cp_parser_allow_gnu_extensions_p (parser)
4379 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4381 /* Statement-expressions are not allowed by the standard. */
4382 pedwarn (token->location, OPT_Wpedantic,
4383 "ISO C++ forbids braced-groups within expressions");
4385 /* And they're not allowed outside of a function-body; you
4386 cannot, for example, write:
4388 int i = ({ int j = 3; j + 1; });
4390 at class or namespace scope. */
4391 if (!parser->in_function_body
4392 || parser->in_template_argument_list_p)
4394 error_at (token->location,
4395 "statement-expressions are not allowed outside "
4396 "functions nor in template-argument lists");
4397 cp_parser_skip_to_end_of_block_or_statement (parser);
4398 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4399 cp_lexer_consume_token (parser->lexer);
4400 return error_mark_node;
4402 else
4403 return cp_parser_statement_expr (parser);
4405 /* Otherwise it's a normal parenthesized expression. */
4407 tree expr;
4408 bool saved_greater_than_is_operator_p;
4410 /* Consume the `('. */
4411 cp_lexer_consume_token (parser->lexer);
4412 /* Within a parenthesized expression, a `>' token is always
4413 the greater-than operator. */
4414 saved_greater_than_is_operator_p
4415 = parser->greater_than_is_operator_p;
4416 parser->greater_than_is_operator_p = true;
4418 /* Parse the parenthesized expression. */
4419 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4420 /* Let the front end know that this expression was
4421 enclosed in parentheses. This matters in case, for
4422 example, the expression is of the form `A::B', since
4423 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4424 not. */
4425 expr = finish_parenthesized_expr (expr);
4426 /* DR 705: Wrapping an unqualified name in parentheses
4427 suppresses arg-dependent lookup. We want to pass back
4428 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4429 (c++/37862), but none of the others. */
4430 if (*idk != CP_ID_KIND_QUALIFIED)
4431 *idk = CP_ID_KIND_NONE;
4433 /* The `>' token might be the end of a template-id or
4434 template-parameter-list now. */
4435 parser->greater_than_is_operator_p
4436 = saved_greater_than_is_operator_p;
4437 /* Consume the `)'. */
4438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4439 cp_parser_skip_to_end_of_statement (parser);
4441 return expr;
4444 case CPP_OPEN_SQUARE:
4446 if (c_dialect_objc ())
4448 /* We might have an Objective-C++ message. */
4449 cp_parser_parse_tentatively (parser);
4450 tree msg = cp_parser_objc_message_expression (parser);
4451 /* If that works out, we're done ... */
4452 if (cp_parser_parse_definitely (parser))
4453 return msg;
4454 /* ... else, fall though to see if it's a lambda. */
4456 tree lam = cp_parser_lambda_expression (parser);
4457 /* Don't warn about a failed tentative parse. */
4458 if (cp_parser_error_occurred (parser))
4459 return error_mark_node;
4460 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4461 return lam;
4464 case CPP_OBJC_STRING:
4465 if (c_dialect_objc ())
4466 /* We have an Objective-C++ string literal. */
4467 return cp_parser_objc_expression (parser);
4468 cp_parser_error (parser, "expected primary-expression");
4469 return error_mark_node;
4471 case CPP_KEYWORD:
4472 switch (token->keyword)
4474 /* These two are the boolean literals. */
4475 case RID_TRUE:
4476 cp_lexer_consume_token (parser->lexer);
4477 return boolean_true_node;
4478 case RID_FALSE:
4479 cp_lexer_consume_token (parser->lexer);
4480 return boolean_false_node;
4482 /* The `__null' literal. */
4483 case RID_NULL:
4484 cp_lexer_consume_token (parser->lexer);
4485 return null_node;
4487 /* The `nullptr' literal. */
4488 case RID_NULLPTR:
4489 cp_lexer_consume_token (parser->lexer);
4490 return nullptr_node;
4492 /* Recognize the `this' keyword. */
4493 case RID_THIS:
4494 cp_lexer_consume_token (parser->lexer);
4495 if (parser->local_variables_forbidden_p)
4497 error_at (token->location,
4498 "%<this%> may not be used in this context");
4499 return error_mark_node;
4501 /* Pointers cannot appear in constant-expressions. */
4502 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4503 return error_mark_node;
4504 return finish_this_expr ();
4506 /* The `operator' keyword can be the beginning of an
4507 id-expression. */
4508 case RID_OPERATOR:
4509 goto id_expression;
4511 case RID_FUNCTION_NAME:
4512 case RID_PRETTY_FUNCTION_NAME:
4513 case RID_C99_FUNCTION_NAME:
4515 non_integral_constant name;
4517 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4518 __func__ are the names of variables -- but they are
4519 treated specially. Therefore, they are handled here,
4520 rather than relying on the generic id-expression logic
4521 below. Grammatically, these names are id-expressions.
4523 Consume the token. */
4524 token = cp_lexer_consume_token (parser->lexer);
4526 switch (token->keyword)
4528 case RID_FUNCTION_NAME:
4529 name = NIC_FUNC_NAME;
4530 break;
4531 case RID_PRETTY_FUNCTION_NAME:
4532 name = NIC_PRETTY_FUNC;
4533 break;
4534 case RID_C99_FUNCTION_NAME:
4535 name = NIC_C99_FUNC;
4536 break;
4537 default:
4538 gcc_unreachable ();
4541 if (cp_parser_non_integral_constant_expression (parser, name))
4542 return error_mark_node;
4544 /* Look up the name. */
4545 return finish_fname (token->u.value);
4548 case RID_VA_ARG:
4550 tree expression;
4551 tree type;
4552 source_location type_location;
4554 /* The `__builtin_va_arg' construct is used to handle
4555 `va_arg'. Consume the `__builtin_va_arg' token. */
4556 cp_lexer_consume_token (parser->lexer);
4557 /* Look for the opening `('. */
4558 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4559 /* Now, parse the assignment-expression. */
4560 expression = cp_parser_assignment_expression (parser);
4561 /* Look for the `,'. */
4562 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4563 type_location = cp_lexer_peek_token (parser->lexer)->location;
4564 /* Parse the type-id. */
4565 type = cp_parser_type_id (parser);
4566 /* Look for the closing `)'. */
4567 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4568 /* Using `va_arg' in a constant-expression is not
4569 allowed. */
4570 if (cp_parser_non_integral_constant_expression (parser,
4571 NIC_VA_ARG))
4572 return error_mark_node;
4573 return build_x_va_arg (type_location, expression, type);
4576 case RID_OFFSETOF:
4577 return cp_parser_builtin_offsetof (parser);
4579 case RID_HAS_NOTHROW_ASSIGN:
4580 case RID_HAS_NOTHROW_CONSTRUCTOR:
4581 case RID_HAS_NOTHROW_COPY:
4582 case RID_HAS_TRIVIAL_ASSIGN:
4583 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4584 case RID_HAS_TRIVIAL_COPY:
4585 case RID_HAS_TRIVIAL_DESTRUCTOR:
4586 case RID_HAS_VIRTUAL_DESTRUCTOR:
4587 case RID_IS_ABSTRACT:
4588 case RID_IS_BASE_OF:
4589 case RID_IS_CLASS:
4590 case RID_IS_EMPTY:
4591 case RID_IS_ENUM:
4592 case RID_IS_FINAL:
4593 case RID_IS_LITERAL_TYPE:
4594 case RID_IS_POD:
4595 case RID_IS_POLYMORPHIC:
4596 case RID_IS_STD_LAYOUT:
4597 case RID_IS_TRIVIAL:
4598 case RID_IS_TRIVIALLY_ASSIGNABLE:
4599 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4600 case RID_IS_TRIVIALLY_COPYABLE:
4601 case RID_IS_UNION:
4602 return cp_parser_trait_expr (parser, token->keyword);
4604 /* Objective-C++ expressions. */
4605 case RID_AT_ENCODE:
4606 case RID_AT_PROTOCOL:
4607 case RID_AT_SELECTOR:
4608 return cp_parser_objc_expression (parser);
4610 case RID_TEMPLATE:
4611 if (parser->in_function_body
4612 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4613 == CPP_LESS))
4615 error_at (token->location,
4616 "a template declaration cannot appear at block scope");
4617 cp_parser_skip_to_end_of_block_or_statement (parser);
4618 return error_mark_node;
4620 default:
4621 cp_parser_error (parser, "expected primary-expression");
4622 return error_mark_node;
4625 /* An id-expression can start with either an identifier, a
4626 `::' as the beginning of a qualified-id, or the "operator"
4627 keyword. */
4628 case CPP_NAME:
4629 case CPP_SCOPE:
4630 case CPP_TEMPLATE_ID:
4631 case CPP_NESTED_NAME_SPECIFIER:
4633 tree id_expression;
4634 tree decl;
4635 const char *error_msg;
4636 bool template_p;
4637 bool done;
4638 cp_token *id_expr_token;
4640 id_expression:
4641 /* Parse the id-expression. */
4642 id_expression
4643 = cp_parser_id_expression (parser,
4644 /*template_keyword_p=*/false,
4645 /*check_dependency_p=*/true,
4646 &template_p,
4647 /*declarator_p=*/false,
4648 /*optional_p=*/false);
4649 if (id_expression == error_mark_node)
4650 return error_mark_node;
4651 id_expr_token = token;
4652 token = cp_lexer_peek_token (parser->lexer);
4653 done = (token->type != CPP_OPEN_SQUARE
4654 && token->type != CPP_OPEN_PAREN
4655 && token->type != CPP_DOT
4656 && token->type != CPP_DEREF
4657 && token->type != CPP_PLUS_PLUS
4658 && token->type != CPP_MINUS_MINUS);
4659 /* If we have a template-id, then no further lookup is
4660 required. If the template-id was for a template-class, we
4661 will sometimes have a TYPE_DECL at this point. */
4662 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4663 || TREE_CODE (id_expression) == TYPE_DECL)
4664 decl = id_expression;
4665 /* Look up the name. */
4666 else
4668 tree ambiguous_decls;
4670 /* If we already know that this lookup is ambiguous, then
4671 we've already issued an error message; there's no reason
4672 to check again. */
4673 if (id_expr_token->type == CPP_NAME
4674 && id_expr_token->error_reported)
4676 cp_parser_simulate_error (parser);
4677 return error_mark_node;
4680 decl = cp_parser_lookup_name (parser, id_expression,
4681 none_type,
4682 template_p,
4683 /*is_namespace=*/false,
4684 /*check_dependency=*/true,
4685 &ambiguous_decls,
4686 id_expr_token->location);
4687 /* If the lookup was ambiguous, an error will already have
4688 been issued. */
4689 if (ambiguous_decls)
4690 return error_mark_node;
4692 /* In Objective-C++, we may have an Objective-C 2.0
4693 dot-syntax for classes here. */
4694 if (c_dialect_objc ()
4695 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4696 && TREE_CODE (decl) == TYPE_DECL
4697 && objc_is_class_name (decl))
4699 tree component;
4700 cp_lexer_consume_token (parser->lexer);
4701 component = cp_parser_identifier (parser);
4702 if (component == error_mark_node)
4703 return error_mark_node;
4705 return objc_build_class_component_ref (id_expression, component);
4708 /* In Objective-C++, an instance variable (ivar) may be preferred
4709 to whatever cp_parser_lookup_name() found. */
4710 decl = objc_lookup_ivar (decl, id_expression);
4712 /* If name lookup gives us a SCOPE_REF, then the
4713 qualifying scope was dependent. */
4714 if (TREE_CODE (decl) == SCOPE_REF)
4716 /* At this point, we do not know if DECL is a valid
4717 integral constant expression. We assume that it is
4718 in fact such an expression, so that code like:
4720 template <int N> struct A {
4721 int a[B<N>::i];
4724 is accepted. At template-instantiation time, we
4725 will check that B<N>::i is actually a constant. */
4726 return decl;
4728 /* Check to see if DECL is a local variable in a context
4729 where that is forbidden. */
4730 if (parser->local_variables_forbidden_p
4731 && local_variable_p (decl))
4733 /* It might be that we only found DECL because we are
4734 trying to be generous with pre-ISO scoping rules.
4735 For example, consider:
4737 int i;
4738 void g() {
4739 for (int i = 0; i < 10; ++i) {}
4740 extern void f(int j = i);
4743 Here, name look up will originally find the out
4744 of scope `i'. We need to issue a warning message,
4745 but then use the global `i'. */
4746 decl = check_for_out_of_scope_variable (decl);
4747 if (local_variable_p (decl))
4749 error_at (id_expr_token->location,
4750 "local variable %qD may not appear in this context",
4751 decl);
4752 return error_mark_node;
4757 decl = (finish_id_expression
4758 (id_expression, decl, parser->scope,
4759 idk,
4760 parser->integral_constant_expression_p,
4761 parser->allow_non_integral_constant_expression_p,
4762 &parser->non_integral_constant_expression_p,
4763 template_p, done, address_p,
4764 template_arg_p,
4765 &error_msg,
4766 id_expr_token->location));
4767 if (error_msg)
4768 cp_parser_error (parser, error_msg);
4769 return decl;
4772 /* Anything else is an error. */
4773 default:
4774 cp_parser_error (parser, "expected primary-expression");
4775 return error_mark_node;
4779 static inline tree
4780 cp_parser_primary_expression (cp_parser *parser,
4781 bool address_p,
4782 bool cast_p,
4783 bool template_arg_p,
4784 cp_id_kind *idk)
4786 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4787 /*decltype*/false, idk);
4790 /* Parse an id-expression.
4792 id-expression:
4793 unqualified-id
4794 qualified-id
4796 qualified-id:
4797 :: [opt] nested-name-specifier template [opt] unqualified-id
4798 :: identifier
4799 :: operator-function-id
4800 :: template-id
4802 Return a representation of the unqualified portion of the
4803 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4804 a `::' or nested-name-specifier.
4806 Often, if the id-expression was a qualified-id, the caller will
4807 want to make a SCOPE_REF to represent the qualified-id. This
4808 function does not do this in order to avoid wastefully creating
4809 SCOPE_REFs when they are not required.
4811 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4812 `template' keyword.
4814 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4815 uninstantiated templates.
4817 If *TEMPLATE_P is non-NULL, it is set to true iff the
4818 `template' keyword is used to explicitly indicate that the entity
4819 named is a template.
4821 If DECLARATOR_P is true, the id-expression is appearing as part of
4822 a declarator, rather than as part of an expression. */
4824 static tree
4825 cp_parser_id_expression (cp_parser *parser,
4826 bool template_keyword_p,
4827 bool check_dependency_p,
4828 bool *template_p,
4829 bool declarator_p,
4830 bool optional_p)
4832 bool global_scope_p;
4833 bool nested_name_specifier_p;
4835 /* Assume the `template' keyword was not used. */
4836 if (template_p)
4837 *template_p = template_keyword_p;
4839 /* Look for the optional `::' operator. */
4840 global_scope_p
4841 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4842 != NULL_TREE);
4843 /* Look for the optional nested-name-specifier. */
4844 nested_name_specifier_p
4845 = (cp_parser_nested_name_specifier_opt (parser,
4846 /*typename_keyword_p=*/false,
4847 check_dependency_p,
4848 /*type_p=*/false,
4849 declarator_p)
4850 != NULL_TREE);
4851 /* If there is a nested-name-specifier, then we are looking at
4852 the first qualified-id production. */
4853 if (nested_name_specifier_p)
4855 tree saved_scope;
4856 tree saved_object_scope;
4857 tree saved_qualifying_scope;
4858 tree unqualified_id;
4859 bool is_template;
4861 /* See if the next token is the `template' keyword. */
4862 if (!template_p)
4863 template_p = &is_template;
4864 *template_p = cp_parser_optional_template_keyword (parser);
4865 /* Name lookup we do during the processing of the
4866 unqualified-id might obliterate SCOPE. */
4867 saved_scope = parser->scope;
4868 saved_object_scope = parser->object_scope;
4869 saved_qualifying_scope = parser->qualifying_scope;
4870 /* Process the final unqualified-id. */
4871 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4872 check_dependency_p,
4873 declarator_p,
4874 /*optional_p=*/false);
4875 /* Restore the SAVED_SCOPE for our caller. */
4876 parser->scope = saved_scope;
4877 parser->object_scope = saved_object_scope;
4878 parser->qualifying_scope = saved_qualifying_scope;
4880 return unqualified_id;
4882 /* Otherwise, if we are in global scope, then we are looking at one
4883 of the other qualified-id productions. */
4884 else if (global_scope_p)
4886 cp_token *token;
4887 tree id;
4889 /* Peek at the next token. */
4890 token = cp_lexer_peek_token (parser->lexer);
4892 /* If it's an identifier, and the next token is not a "<", then
4893 we can avoid the template-id case. This is an optimization
4894 for this common case. */
4895 if (token->type == CPP_NAME
4896 && !cp_parser_nth_token_starts_template_argument_list_p
4897 (parser, 2))
4898 return cp_parser_identifier (parser);
4900 cp_parser_parse_tentatively (parser);
4901 /* Try a template-id. */
4902 id = cp_parser_template_id (parser,
4903 /*template_keyword_p=*/false,
4904 /*check_dependency_p=*/true,
4905 none_type,
4906 declarator_p);
4907 /* If that worked, we're done. */
4908 if (cp_parser_parse_definitely (parser))
4909 return id;
4911 /* Peek at the next token. (Changes in the token buffer may
4912 have invalidated the pointer obtained above.) */
4913 token = cp_lexer_peek_token (parser->lexer);
4915 switch (token->type)
4917 case CPP_NAME:
4918 return cp_parser_identifier (parser);
4920 case CPP_KEYWORD:
4921 if (token->keyword == RID_OPERATOR)
4922 return cp_parser_operator_function_id (parser);
4923 /* Fall through. */
4925 default:
4926 cp_parser_error (parser, "expected id-expression");
4927 return error_mark_node;
4930 else
4931 return cp_parser_unqualified_id (parser, template_keyword_p,
4932 /*check_dependency_p=*/true,
4933 declarator_p,
4934 optional_p);
4937 /* Parse an unqualified-id.
4939 unqualified-id:
4940 identifier
4941 operator-function-id
4942 conversion-function-id
4943 ~ class-name
4944 template-id
4946 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4947 keyword, in a construct like `A::template ...'.
4949 Returns a representation of unqualified-id. For the `identifier'
4950 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4951 production a BIT_NOT_EXPR is returned; the operand of the
4952 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4953 other productions, see the documentation accompanying the
4954 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4955 names are looked up in uninstantiated templates. If DECLARATOR_P
4956 is true, the unqualified-id is appearing as part of a declarator,
4957 rather than as part of an expression. */
4959 static tree
4960 cp_parser_unqualified_id (cp_parser* parser,
4961 bool template_keyword_p,
4962 bool check_dependency_p,
4963 bool declarator_p,
4964 bool optional_p)
4966 cp_token *token;
4968 /* Peek at the next token. */
4969 token = cp_lexer_peek_token (parser->lexer);
4971 switch ((int) token->type)
4973 case CPP_NAME:
4975 tree id;
4977 /* We don't know yet whether or not this will be a
4978 template-id. */
4979 cp_parser_parse_tentatively (parser);
4980 /* Try a template-id. */
4981 id = cp_parser_template_id (parser, template_keyword_p,
4982 check_dependency_p,
4983 none_type,
4984 declarator_p);
4985 /* If it worked, we're done. */
4986 if (cp_parser_parse_definitely (parser))
4987 return id;
4988 /* Otherwise, it's an ordinary identifier. */
4989 return cp_parser_identifier (parser);
4992 case CPP_TEMPLATE_ID:
4993 return cp_parser_template_id (parser, template_keyword_p,
4994 check_dependency_p,
4995 none_type,
4996 declarator_p);
4998 case CPP_COMPL:
5000 tree type_decl;
5001 tree qualifying_scope;
5002 tree object_scope;
5003 tree scope;
5004 bool done;
5006 /* Consume the `~' token. */
5007 cp_lexer_consume_token (parser->lexer);
5008 /* Parse the class-name. The standard, as written, seems to
5009 say that:
5011 template <typename T> struct S { ~S (); };
5012 template <typename T> S<T>::~S() {}
5014 is invalid, since `~' must be followed by a class-name, but
5015 `S<T>' is dependent, and so not known to be a class.
5016 That's not right; we need to look in uninstantiated
5017 templates. A further complication arises from:
5019 template <typename T> void f(T t) {
5020 t.T::~T();
5023 Here, it is not possible to look up `T' in the scope of `T'
5024 itself. We must look in both the current scope, and the
5025 scope of the containing complete expression.
5027 Yet another issue is:
5029 struct S {
5030 int S;
5031 ~S();
5034 S::~S() {}
5036 The standard does not seem to say that the `S' in `~S'
5037 should refer to the type `S' and not the data member
5038 `S::S'. */
5040 /* DR 244 says that we look up the name after the "~" in the
5041 same scope as we looked up the qualifying name. That idea
5042 isn't fully worked out; it's more complicated than that. */
5043 scope = parser->scope;
5044 object_scope = parser->object_scope;
5045 qualifying_scope = parser->qualifying_scope;
5047 /* Check for invalid scopes. */
5048 if (scope == error_mark_node)
5050 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5051 cp_lexer_consume_token (parser->lexer);
5052 return error_mark_node;
5054 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5056 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5057 error_at (token->location,
5058 "scope %qT before %<~%> is not a class-name",
5059 scope);
5060 cp_parser_simulate_error (parser);
5061 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5062 cp_lexer_consume_token (parser->lexer);
5063 return error_mark_node;
5065 gcc_assert (!scope || TYPE_P (scope));
5067 /* If the name is of the form "X::~X" it's OK even if X is a
5068 typedef. */
5069 token = cp_lexer_peek_token (parser->lexer);
5070 if (scope
5071 && token->type == CPP_NAME
5072 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5073 != CPP_LESS)
5074 && (token->u.value == TYPE_IDENTIFIER (scope)
5075 || (CLASS_TYPE_P (scope)
5076 && constructor_name_p (token->u.value, scope))))
5078 cp_lexer_consume_token (parser->lexer);
5079 return build_nt (BIT_NOT_EXPR, scope);
5082 /* ~auto means the destructor of whatever the object is. */
5083 if (cp_parser_is_keyword (token, RID_AUTO))
5085 if (cxx_dialect < cxx14)
5086 pedwarn (input_location, 0,
5087 "%<~auto%> only available with "
5088 "-std=c++14 or -std=gnu++14");
5089 cp_lexer_consume_token (parser->lexer);
5090 return build_nt (BIT_NOT_EXPR, make_auto ());
5093 /* If there was an explicit qualification (S::~T), first look
5094 in the scope given by the qualification (i.e., S).
5096 Note: in the calls to cp_parser_class_name below we pass
5097 typename_type so that lookup finds the injected-class-name
5098 rather than the constructor. */
5099 done = false;
5100 type_decl = NULL_TREE;
5101 if (scope)
5103 cp_parser_parse_tentatively (parser);
5104 type_decl = cp_parser_class_name (parser,
5105 /*typename_keyword_p=*/false,
5106 /*template_keyword_p=*/false,
5107 typename_type,
5108 /*check_dependency=*/false,
5109 /*class_head_p=*/false,
5110 declarator_p);
5111 if (cp_parser_parse_definitely (parser))
5112 done = true;
5114 /* In "N::S::~S", look in "N" as well. */
5115 if (!done && scope && qualifying_scope)
5117 cp_parser_parse_tentatively (parser);
5118 parser->scope = qualifying_scope;
5119 parser->object_scope = NULL_TREE;
5120 parser->qualifying_scope = NULL_TREE;
5121 type_decl
5122 = cp_parser_class_name (parser,
5123 /*typename_keyword_p=*/false,
5124 /*template_keyword_p=*/false,
5125 typename_type,
5126 /*check_dependency=*/false,
5127 /*class_head_p=*/false,
5128 declarator_p);
5129 if (cp_parser_parse_definitely (parser))
5130 done = true;
5132 /* In "p->S::~T", look in the scope given by "*p" as well. */
5133 else if (!done && object_scope)
5135 cp_parser_parse_tentatively (parser);
5136 parser->scope = object_scope;
5137 parser->object_scope = NULL_TREE;
5138 parser->qualifying_scope = NULL_TREE;
5139 type_decl
5140 = cp_parser_class_name (parser,
5141 /*typename_keyword_p=*/false,
5142 /*template_keyword_p=*/false,
5143 typename_type,
5144 /*check_dependency=*/false,
5145 /*class_head_p=*/false,
5146 declarator_p);
5147 if (cp_parser_parse_definitely (parser))
5148 done = true;
5150 /* Look in the surrounding context. */
5151 if (!done)
5153 parser->scope = NULL_TREE;
5154 parser->object_scope = NULL_TREE;
5155 parser->qualifying_scope = NULL_TREE;
5156 if (processing_template_decl)
5157 cp_parser_parse_tentatively (parser);
5158 type_decl
5159 = cp_parser_class_name (parser,
5160 /*typename_keyword_p=*/false,
5161 /*template_keyword_p=*/false,
5162 typename_type,
5163 /*check_dependency=*/false,
5164 /*class_head_p=*/false,
5165 declarator_p);
5166 if (processing_template_decl
5167 && ! cp_parser_parse_definitely (parser))
5169 /* We couldn't find a type with this name, so just accept
5170 it and check for a match at instantiation time. */
5171 type_decl = cp_parser_identifier (parser);
5172 if (type_decl != error_mark_node)
5173 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5174 return type_decl;
5177 /* If an error occurred, assume that the name of the
5178 destructor is the same as the name of the qualifying
5179 class. That allows us to keep parsing after running
5180 into ill-formed destructor names. */
5181 if (type_decl == error_mark_node && scope)
5182 return build_nt (BIT_NOT_EXPR, scope);
5183 else if (type_decl == error_mark_node)
5184 return error_mark_node;
5186 /* Check that destructor name and scope match. */
5187 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5189 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5190 error_at (token->location,
5191 "declaration of %<~%T%> as member of %qT",
5192 type_decl, scope);
5193 cp_parser_simulate_error (parser);
5194 return error_mark_node;
5197 /* [class.dtor]
5199 A typedef-name that names a class shall not be used as the
5200 identifier in the declarator for a destructor declaration. */
5201 if (declarator_p
5202 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5203 && !DECL_SELF_REFERENCE_P (type_decl)
5204 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5205 error_at (token->location,
5206 "typedef-name %qD used as destructor declarator",
5207 type_decl);
5209 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5212 case CPP_KEYWORD:
5213 if (token->keyword == RID_OPERATOR)
5215 tree id;
5217 /* This could be a template-id, so we try that first. */
5218 cp_parser_parse_tentatively (parser);
5219 /* Try a template-id. */
5220 id = cp_parser_template_id (parser, template_keyword_p,
5221 /*check_dependency_p=*/true,
5222 none_type,
5223 declarator_p);
5224 /* If that worked, we're done. */
5225 if (cp_parser_parse_definitely (parser))
5226 return id;
5227 /* We still don't know whether we're looking at an
5228 operator-function-id or a conversion-function-id. */
5229 cp_parser_parse_tentatively (parser);
5230 /* Try an operator-function-id. */
5231 id = cp_parser_operator_function_id (parser);
5232 /* If that didn't work, try a conversion-function-id. */
5233 if (!cp_parser_parse_definitely (parser))
5234 id = cp_parser_conversion_function_id (parser);
5235 else if (UDLIT_OPER_P (id))
5237 /* 17.6.3.3.5 */
5238 const char *name = UDLIT_OP_SUFFIX (id);
5239 if (name[0] != '_' && !in_system_header_at (input_location)
5240 && declarator_p)
5241 warning (0, "literal operator suffixes not preceded by %<_%>"
5242 " are reserved for future standardization");
5245 return id;
5247 /* Fall through. */
5249 default:
5250 if (optional_p)
5251 return NULL_TREE;
5252 cp_parser_error (parser, "expected unqualified-id");
5253 return error_mark_node;
5257 /* Parse an (optional) nested-name-specifier.
5259 nested-name-specifier: [C++98]
5260 class-or-namespace-name :: nested-name-specifier [opt]
5261 class-or-namespace-name :: template nested-name-specifier [opt]
5263 nested-name-specifier: [C++0x]
5264 type-name ::
5265 namespace-name ::
5266 nested-name-specifier identifier ::
5267 nested-name-specifier template [opt] simple-template-id ::
5269 PARSER->SCOPE should be set appropriately before this function is
5270 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5271 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5272 in name lookups.
5274 Sets PARSER->SCOPE to the class (TYPE) or namespace
5275 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5276 it unchanged if there is no nested-name-specifier. Returns the new
5277 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5279 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5280 part of a declaration and/or decl-specifier. */
5282 static tree
5283 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5284 bool typename_keyword_p,
5285 bool check_dependency_p,
5286 bool type_p,
5287 bool is_declaration)
5289 bool success = false;
5290 cp_token_position start = 0;
5291 cp_token *token;
5293 /* Remember where the nested-name-specifier starts. */
5294 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5296 start = cp_lexer_token_position (parser->lexer, false);
5297 push_deferring_access_checks (dk_deferred);
5300 while (true)
5302 tree new_scope;
5303 tree old_scope;
5304 tree saved_qualifying_scope;
5305 bool template_keyword_p;
5307 /* Spot cases that cannot be the beginning of a
5308 nested-name-specifier. */
5309 token = cp_lexer_peek_token (parser->lexer);
5311 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5312 the already parsed nested-name-specifier. */
5313 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5315 /* Grab the nested-name-specifier and continue the loop. */
5316 cp_parser_pre_parsed_nested_name_specifier (parser);
5317 /* If we originally encountered this nested-name-specifier
5318 with IS_DECLARATION set to false, we will not have
5319 resolved TYPENAME_TYPEs, so we must do so here. */
5320 if (is_declaration
5321 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5323 new_scope = resolve_typename_type (parser->scope,
5324 /*only_current_p=*/false);
5325 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5326 parser->scope = new_scope;
5328 success = true;
5329 continue;
5332 /* Spot cases that cannot be the beginning of a
5333 nested-name-specifier. On the second and subsequent times
5334 through the loop, we look for the `template' keyword. */
5335 if (success && token->keyword == RID_TEMPLATE)
5337 /* A template-id can start a nested-name-specifier. */
5338 else if (token->type == CPP_TEMPLATE_ID)
5340 /* DR 743: decltype can be used in a nested-name-specifier. */
5341 else if (token_is_decltype (token))
5343 else
5345 /* If the next token is not an identifier, then it is
5346 definitely not a type-name or namespace-name. */
5347 if (token->type != CPP_NAME)
5348 break;
5349 /* If the following token is neither a `<' (to begin a
5350 template-id), nor a `::', then we are not looking at a
5351 nested-name-specifier. */
5352 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5354 if (token->type == CPP_COLON
5355 && parser->colon_corrects_to_scope_p
5356 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5358 error_at (token->location,
5359 "found %<:%> in nested-name-specifier, expected %<::%>");
5360 token->type = CPP_SCOPE;
5363 if (token->type != CPP_SCOPE
5364 && !cp_parser_nth_token_starts_template_argument_list_p
5365 (parser, 2))
5366 break;
5369 /* The nested-name-specifier is optional, so we parse
5370 tentatively. */
5371 cp_parser_parse_tentatively (parser);
5373 /* Look for the optional `template' keyword, if this isn't the
5374 first time through the loop. */
5375 if (success)
5376 template_keyword_p = cp_parser_optional_template_keyword (parser);
5377 else
5378 template_keyword_p = false;
5380 /* Save the old scope since the name lookup we are about to do
5381 might destroy it. */
5382 old_scope = parser->scope;
5383 saved_qualifying_scope = parser->qualifying_scope;
5384 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5385 look up names in "X<T>::I" in order to determine that "Y" is
5386 a template. So, if we have a typename at this point, we make
5387 an effort to look through it. */
5388 if (is_declaration
5389 && !typename_keyword_p
5390 && parser->scope
5391 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5392 parser->scope = resolve_typename_type (parser->scope,
5393 /*only_current_p=*/false);
5394 /* Parse the qualifying entity. */
5395 new_scope
5396 = cp_parser_qualifying_entity (parser,
5397 typename_keyword_p,
5398 template_keyword_p,
5399 check_dependency_p,
5400 type_p,
5401 is_declaration);
5402 /* Look for the `::' token. */
5403 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5405 /* If we found what we wanted, we keep going; otherwise, we're
5406 done. */
5407 if (!cp_parser_parse_definitely (parser))
5409 bool error_p = false;
5411 /* Restore the OLD_SCOPE since it was valid before the
5412 failed attempt at finding the last
5413 class-or-namespace-name. */
5414 parser->scope = old_scope;
5415 parser->qualifying_scope = saved_qualifying_scope;
5417 /* If the next token is a decltype, and the one after that is a
5418 `::', then the decltype has failed to resolve to a class or
5419 enumeration type. Give this error even when parsing
5420 tentatively since it can't possibly be valid--and we're going
5421 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5422 won't get another chance.*/
5423 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5424 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5425 == CPP_SCOPE))
5427 token = cp_lexer_consume_token (parser->lexer);
5428 error_at (token->location, "decltype evaluates to %qT, "
5429 "which is not a class or enumeration type",
5430 token->u.value);
5431 parser->scope = error_mark_node;
5432 error_p = true;
5433 /* As below. */
5434 success = true;
5435 cp_lexer_consume_token (parser->lexer);
5438 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5439 break;
5440 /* If the next token is an identifier, and the one after
5441 that is a `::', then any valid interpretation would have
5442 found a class-or-namespace-name. */
5443 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5444 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5445 == CPP_SCOPE)
5446 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5447 != CPP_COMPL))
5449 token = cp_lexer_consume_token (parser->lexer);
5450 if (!error_p)
5452 if (!token->error_reported)
5454 tree decl;
5455 tree ambiguous_decls;
5457 decl = cp_parser_lookup_name (parser, token->u.value,
5458 none_type,
5459 /*is_template=*/false,
5460 /*is_namespace=*/false,
5461 /*check_dependency=*/true,
5462 &ambiguous_decls,
5463 token->location);
5464 if (TREE_CODE (decl) == TEMPLATE_DECL)
5465 error_at (token->location,
5466 "%qD used without template parameters",
5467 decl);
5468 else if (ambiguous_decls)
5470 // cp_parser_lookup_name has the same diagnostic,
5471 // thus make sure to emit it at most once.
5472 if (cp_parser_uncommitted_to_tentative_parse_p
5473 (parser))
5475 error_at (token->location,
5476 "reference to %qD is ambiguous",
5477 token->u.value);
5478 print_candidates (ambiguous_decls);
5480 decl = error_mark_node;
5482 else
5484 if (cxx_dialect != cxx98)
5485 cp_parser_name_lookup_error
5486 (parser, token->u.value, decl, NLE_NOT_CXX98,
5487 token->location);
5488 else
5489 cp_parser_name_lookup_error
5490 (parser, token->u.value, decl, NLE_CXX98,
5491 token->location);
5494 parser->scope = error_mark_node;
5495 error_p = true;
5496 /* Treat this as a successful nested-name-specifier
5497 due to:
5499 [basic.lookup.qual]
5501 If the name found is not a class-name (clause
5502 _class_) or namespace-name (_namespace.def_), the
5503 program is ill-formed. */
5504 success = true;
5506 cp_lexer_consume_token (parser->lexer);
5508 break;
5510 /* We've found one valid nested-name-specifier. */
5511 success = true;
5512 /* Name lookup always gives us a DECL. */
5513 if (TREE_CODE (new_scope) == TYPE_DECL)
5514 new_scope = TREE_TYPE (new_scope);
5515 /* Uses of "template" must be followed by actual templates. */
5516 if (template_keyword_p
5517 && !(CLASS_TYPE_P (new_scope)
5518 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5519 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5520 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5521 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5522 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5523 == TEMPLATE_ID_EXPR)))
5524 permerror (input_location, TYPE_P (new_scope)
5525 ? G_("%qT is not a template")
5526 : G_("%qD is not a template"),
5527 new_scope);
5528 /* If it is a class scope, try to complete it; we are about to
5529 be looking up names inside the class. */
5530 if (TYPE_P (new_scope)
5531 /* Since checking types for dependency can be expensive,
5532 avoid doing it if the type is already complete. */
5533 && !COMPLETE_TYPE_P (new_scope)
5534 /* Do not try to complete dependent types. */
5535 && !dependent_type_p (new_scope))
5537 new_scope = complete_type (new_scope);
5538 /* If it is a typedef to current class, use the current
5539 class instead, as the typedef won't have any names inside
5540 it yet. */
5541 if (!COMPLETE_TYPE_P (new_scope)
5542 && currently_open_class (new_scope))
5543 new_scope = TYPE_MAIN_VARIANT (new_scope);
5545 /* Make sure we look in the right scope the next time through
5546 the loop. */
5547 parser->scope = new_scope;
5550 /* If parsing tentatively, replace the sequence of tokens that makes
5551 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5552 token. That way, should we re-parse the token stream, we will
5553 not have to repeat the effort required to do the parse, nor will
5554 we issue duplicate error messages. */
5555 if (success && start)
5557 cp_token *token;
5559 token = cp_lexer_token_at (parser->lexer, start);
5560 /* Reset the contents of the START token. */
5561 token->type = CPP_NESTED_NAME_SPECIFIER;
5562 /* Retrieve any deferred checks. Do not pop this access checks yet
5563 so the memory will not be reclaimed during token replacing below. */
5564 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5565 token->u.tree_check_value->value = parser->scope;
5566 token->u.tree_check_value->checks = get_deferred_access_checks ();
5567 token->u.tree_check_value->qualifying_scope =
5568 parser->qualifying_scope;
5569 token->keyword = RID_MAX;
5571 /* Purge all subsequent tokens. */
5572 cp_lexer_purge_tokens_after (parser->lexer, start);
5575 if (start)
5576 pop_to_parent_deferring_access_checks ();
5578 return success ? parser->scope : NULL_TREE;
5581 /* Parse a nested-name-specifier. See
5582 cp_parser_nested_name_specifier_opt for details. This function
5583 behaves identically, except that it will an issue an error if no
5584 nested-name-specifier is present. */
5586 static tree
5587 cp_parser_nested_name_specifier (cp_parser *parser,
5588 bool typename_keyword_p,
5589 bool check_dependency_p,
5590 bool type_p,
5591 bool is_declaration)
5593 tree scope;
5595 /* Look for the nested-name-specifier. */
5596 scope = cp_parser_nested_name_specifier_opt (parser,
5597 typename_keyword_p,
5598 check_dependency_p,
5599 type_p,
5600 is_declaration);
5601 /* If it was not present, issue an error message. */
5602 if (!scope)
5604 cp_parser_error (parser, "expected nested-name-specifier");
5605 parser->scope = NULL_TREE;
5608 return scope;
5611 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5612 this is either a class-name or a namespace-name (which corresponds
5613 to the class-or-namespace-name production in the grammar). For
5614 C++0x, it can also be a type-name that refers to an enumeration
5615 type or a simple-template-id.
5617 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5618 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5619 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5620 TYPE_P is TRUE iff the next name should be taken as a class-name,
5621 even the same name is declared to be another entity in the same
5622 scope.
5624 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5625 specified by the class-or-namespace-name. If neither is found the
5626 ERROR_MARK_NODE is returned. */
5628 static tree
5629 cp_parser_qualifying_entity (cp_parser *parser,
5630 bool typename_keyword_p,
5631 bool template_keyword_p,
5632 bool check_dependency_p,
5633 bool type_p,
5634 bool is_declaration)
5636 tree saved_scope;
5637 tree saved_qualifying_scope;
5638 tree saved_object_scope;
5639 tree scope;
5640 bool only_class_p;
5641 bool successful_parse_p;
5643 /* DR 743: decltype can appear in a nested-name-specifier. */
5644 if (cp_lexer_next_token_is_decltype (parser->lexer))
5646 scope = cp_parser_decltype (parser);
5647 if (TREE_CODE (scope) != ENUMERAL_TYPE
5648 && !MAYBE_CLASS_TYPE_P (scope))
5650 cp_parser_simulate_error (parser);
5651 return error_mark_node;
5653 if (TYPE_NAME (scope))
5654 scope = TYPE_NAME (scope);
5655 return scope;
5658 /* Before we try to parse the class-name, we must save away the
5659 current PARSER->SCOPE since cp_parser_class_name will destroy
5660 it. */
5661 saved_scope = parser->scope;
5662 saved_qualifying_scope = parser->qualifying_scope;
5663 saved_object_scope = parser->object_scope;
5664 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5665 there is no need to look for a namespace-name. */
5666 only_class_p = template_keyword_p
5667 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5668 if (!only_class_p)
5669 cp_parser_parse_tentatively (parser);
5670 scope = cp_parser_class_name (parser,
5671 typename_keyword_p,
5672 template_keyword_p,
5673 type_p ? class_type : none_type,
5674 check_dependency_p,
5675 /*class_head_p=*/false,
5676 is_declaration);
5677 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5678 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5679 if (!only_class_p
5680 && cxx_dialect != cxx98
5681 && !successful_parse_p)
5683 /* Restore the saved scope. */
5684 parser->scope = saved_scope;
5685 parser->qualifying_scope = saved_qualifying_scope;
5686 parser->object_scope = saved_object_scope;
5688 /* Parse tentatively. */
5689 cp_parser_parse_tentatively (parser);
5691 /* Parse a type-name */
5692 scope = cp_parser_type_name (parser);
5694 /* "If the name found does not designate a namespace or a class,
5695 enumeration, or dependent type, the program is ill-formed."
5697 We cover classes and dependent types above and namespaces below,
5698 so this code is only looking for enums. */
5699 if (!scope || TREE_CODE (scope) != TYPE_DECL
5700 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5701 cp_parser_simulate_error (parser);
5703 successful_parse_p = cp_parser_parse_definitely (parser);
5705 /* If that didn't work, try for a namespace-name. */
5706 if (!only_class_p && !successful_parse_p)
5708 /* Restore the saved scope. */
5709 parser->scope = saved_scope;
5710 parser->qualifying_scope = saved_qualifying_scope;
5711 parser->object_scope = saved_object_scope;
5712 /* If we are not looking at an identifier followed by the scope
5713 resolution operator, then this is not part of a
5714 nested-name-specifier. (Note that this function is only used
5715 to parse the components of a nested-name-specifier.) */
5716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5717 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5718 return error_mark_node;
5719 scope = cp_parser_namespace_name (parser);
5722 return scope;
5725 /* Return true if we are looking at a compound-literal, false otherwise. */
5727 static bool
5728 cp_parser_compound_literal_p (cp_parser *parser)
5730 /* Consume the `('. */
5731 cp_lexer_consume_token (parser->lexer);
5733 cp_lexer_save_tokens (parser->lexer);
5735 /* Skip tokens until the next token is a closing parenthesis.
5736 If we find the closing `)', and the next token is a `{', then
5737 we are looking at a compound-literal. */
5738 bool compound_literal_p
5739 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5740 /*consume_paren=*/true)
5741 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5743 /* Roll back the tokens we skipped. */
5744 cp_lexer_rollback_tokens (parser->lexer);
5746 return compound_literal_p;
5749 /* Parse a postfix-expression.
5751 postfix-expression:
5752 primary-expression
5753 postfix-expression [ expression ]
5754 postfix-expression ( expression-list [opt] )
5755 simple-type-specifier ( expression-list [opt] )
5756 typename :: [opt] nested-name-specifier identifier
5757 ( expression-list [opt] )
5758 typename :: [opt] nested-name-specifier template [opt] template-id
5759 ( expression-list [opt] )
5760 postfix-expression . template [opt] id-expression
5761 postfix-expression -> template [opt] id-expression
5762 postfix-expression . pseudo-destructor-name
5763 postfix-expression -> pseudo-destructor-name
5764 postfix-expression ++
5765 postfix-expression --
5766 dynamic_cast < type-id > ( expression )
5767 static_cast < type-id > ( expression )
5768 reinterpret_cast < type-id > ( expression )
5769 const_cast < type-id > ( expression )
5770 typeid ( expression )
5771 typeid ( type-id )
5773 GNU Extension:
5775 postfix-expression:
5776 ( type-id ) { initializer-list , [opt] }
5778 This extension is a GNU version of the C99 compound-literal
5779 construct. (The C99 grammar uses `type-name' instead of `type-id',
5780 but they are essentially the same concept.)
5782 If ADDRESS_P is true, the postfix expression is the operand of the
5783 `&' operator. CAST_P is true if this expression is the target of a
5784 cast.
5786 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5787 class member access expressions [expr.ref].
5789 Returns a representation of the expression. */
5791 static tree
5792 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5793 bool member_access_only_p, bool decltype_p,
5794 cp_id_kind * pidk_return)
5796 cp_token *token;
5797 location_t loc;
5798 enum rid keyword;
5799 cp_id_kind idk = CP_ID_KIND_NONE;
5800 tree postfix_expression = NULL_TREE;
5801 bool is_member_access = false;
5802 int saved_in_statement = -1;
5804 /* Peek at the next token. */
5805 token = cp_lexer_peek_token (parser->lexer);
5806 loc = token->location;
5807 /* Some of the productions are determined by keywords. */
5808 keyword = token->keyword;
5809 switch (keyword)
5811 case RID_DYNCAST:
5812 case RID_STATCAST:
5813 case RID_REINTCAST:
5814 case RID_CONSTCAST:
5816 tree type;
5817 tree expression;
5818 const char *saved_message;
5819 bool saved_in_type_id_in_expr_p;
5821 /* All of these can be handled in the same way from the point
5822 of view of parsing. Begin by consuming the token
5823 identifying the cast. */
5824 cp_lexer_consume_token (parser->lexer);
5826 /* New types cannot be defined in the cast. */
5827 saved_message = parser->type_definition_forbidden_message;
5828 parser->type_definition_forbidden_message
5829 = G_("types may not be defined in casts");
5831 /* Look for the opening `<'. */
5832 cp_parser_require (parser, CPP_LESS, RT_LESS);
5833 /* Parse the type to which we are casting. */
5834 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5835 parser->in_type_id_in_expr_p = true;
5836 type = cp_parser_type_id (parser);
5837 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5838 /* Look for the closing `>'. */
5839 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5840 /* Restore the old message. */
5841 parser->type_definition_forbidden_message = saved_message;
5843 bool saved_greater_than_is_operator_p
5844 = parser->greater_than_is_operator_p;
5845 parser->greater_than_is_operator_p = true;
5847 /* And the expression which is being cast. */
5848 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5849 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5850 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5852 parser->greater_than_is_operator_p
5853 = saved_greater_than_is_operator_p;
5855 /* Only type conversions to integral or enumeration types
5856 can be used in constant-expressions. */
5857 if (!cast_valid_in_integral_constant_expression_p (type)
5858 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5859 return error_mark_node;
5861 switch (keyword)
5863 case RID_DYNCAST:
5864 postfix_expression
5865 = build_dynamic_cast (type, expression, tf_warning_or_error);
5866 break;
5867 case RID_STATCAST:
5868 postfix_expression
5869 = build_static_cast (type, expression, tf_warning_or_error);
5870 break;
5871 case RID_REINTCAST:
5872 postfix_expression
5873 = build_reinterpret_cast (type, expression,
5874 tf_warning_or_error);
5875 break;
5876 case RID_CONSTCAST:
5877 postfix_expression
5878 = build_const_cast (type, expression, tf_warning_or_error);
5879 break;
5880 default:
5881 gcc_unreachable ();
5884 break;
5886 case RID_TYPEID:
5888 tree type;
5889 const char *saved_message;
5890 bool saved_in_type_id_in_expr_p;
5892 /* Consume the `typeid' token. */
5893 cp_lexer_consume_token (parser->lexer);
5894 /* Look for the `(' token. */
5895 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5896 /* Types cannot be defined in a `typeid' expression. */
5897 saved_message = parser->type_definition_forbidden_message;
5898 parser->type_definition_forbidden_message
5899 = G_("types may not be defined in a %<typeid%> expression");
5900 /* We can't be sure yet whether we're looking at a type-id or an
5901 expression. */
5902 cp_parser_parse_tentatively (parser);
5903 /* Try a type-id first. */
5904 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5905 parser->in_type_id_in_expr_p = true;
5906 type = cp_parser_type_id (parser);
5907 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5908 /* Look for the `)' token. Otherwise, we can't be sure that
5909 we're not looking at an expression: consider `typeid (int
5910 (3))', for example. */
5911 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5912 /* If all went well, simply lookup the type-id. */
5913 if (cp_parser_parse_definitely (parser))
5914 postfix_expression = get_typeid (type, tf_warning_or_error);
5915 /* Otherwise, fall back to the expression variant. */
5916 else
5918 tree expression;
5920 /* Look for an expression. */
5921 expression = cp_parser_expression (parser, & idk);
5922 /* Compute its typeid. */
5923 postfix_expression = build_typeid (expression, tf_warning_or_error);
5924 /* Look for the `)' token. */
5925 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5927 /* Restore the saved message. */
5928 parser->type_definition_forbidden_message = saved_message;
5929 /* `typeid' may not appear in an integral constant expression. */
5930 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5931 return error_mark_node;
5933 break;
5935 case RID_TYPENAME:
5937 tree type;
5938 /* The syntax permitted here is the same permitted for an
5939 elaborated-type-specifier. */
5940 type = cp_parser_elaborated_type_specifier (parser,
5941 /*is_friend=*/false,
5942 /*is_declaration=*/false);
5943 postfix_expression = cp_parser_functional_cast (parser, type);
5945 break;
5947 case RID_CILK_SPAWN:
5949 cp_lexer_consume_token (parser->lexer);
5950 token = cp_lexer_peek_token (parser->lexer);
5951 if (token->type == CPP_SEMICOLON)
5953 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5954 "an expression");
5955 postfix_expression = error_mark_node;
5956 break;
5958 else if (!current_function_decl)
5960 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5961 "inside a function");
5962 postfix_expression = error_mark_node;
5963 break;
5965 else
5967 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5968 saved_in_statement = parser->in_statement;
5969 parser->in_statement |= IN_CILK_SPAWN;
5971 cfun->calls_cilk_spawn = 1;
5972 postfix_expression =
5973 cp_parser_postfix_expression (parser, false, false,
5974 false, false, &idk);
5975 if (!flag_cilkplus)
5977 error_at (token->location, "-fcilkplus must be enabled to use"
5978 " %<_Cilk_spawn%>");
5979 cfun->calls_cilk_spawn = 0;
5981 else if (saved_in_statement & IN_CILK_SPAWN)
5983 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5984 "are not permitted");
5985 postfix_expression = error_mark_node;
5986 cfun->calls_cilk_spawn = 0;
5988 else
5990 postfix_expression = build_cilk_spawn (token->location,
5991 postfix_expression);
5992 if (postfix_expression != error_mark_node)
5993 SET_EXPR_LOCATION (postfix_expression, input_location);
5994 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5996 break;
5999 case RID_BUILTIN_SHUFFLE:
6001 vec<tree, va_gc> *vec;
6002 unsigned int i;
6003 tree p;
6005 cp_lexer_consume_token (parser->lexer);
6006 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6007 /*cast_p=*/false, /*allow_expansion_p=*/true,
6008 /*non_constant_p=*/NULL);
6009 if (vec == NULL)
6010 return error_mark_node;
6012 FOR_EACH_VEC_ELT (*vec, i, p)
6013 mark_exp_read (p);
6015 if (vec->length () == 2)
6016 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6017 tf_warning_or_error);
6018 else if (vec->length () == 3)
6019 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6020 tf_warning_or_error);
6021 else
6023 error_at (loc, "wrong number of arguments to "
6024 "%<__builtin_shuffle%>");
6025 return error_mark_node;
6027 break;
6030 default:
6032 tree type;
6034 /* If the next thing is a simple-type-specifier, we may be
6035 looking at a functional cast. We could also be looking at
6036 an id-expression. So, we try the functional cast, and if
6037 that doesn't work we fall back to the primary-expression. */
6038 cp_parser_parse_tentatively (parser);
6039 /* Look for the simple-type-specifier. */
6040 type = cp_parser_simple_type_specifier (parser,
6041 /*decl_specs=*/NULL,
6042 CP_PARSER_FLAGS_NONE);
6043 /* Parse the cast itself. */
6044 if (!cp_parser_error_occurred (parser))
6045 postfix_expression
6046 = cp_parser_functional_cast (parser, type);
6047 /* If that worked, we're done. */
6048 if (cp_parser_parse_definitely (parser))
6049 break;
6051 /* If the functional-cast didn't work out, try a
6052 compound-literal. */
6053 if (cp_parser_allow_gnu_extensions_p (parser)
6054 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6056 tree initializer = NULL_TREE;
6058 cp_parser_parse_tentatively (parser);
6060 /* Avoid calling cp_parser_type_id pointlessly, see comment
6061 in cp_parser_cast_expression about c++/29234. */
6062 if (!cp_parser_compound_literal_p (parser))
6063 cp_parser_simulate_error (parser);
6064 else
6066 /* Parse the type. */
6067 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6068 parser->in_type_id_in_expr_p = true;
6069 type = cp_parser_type_id (parser);
6070 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6071 /* Look for the `)'. */
6072 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6075 /* If things aren't going well, there's no need to
6076 keep going. */
6077 if (!cp_parser_error_occurred (parser))
6079 bool non_constant_p;
6080 /* Parse the brace-enclosed initializer list. */
6081 initializer = cp_parser_braced_list (parser,
6082 &non_constant_p);
6084 /* If that worked, we're definitely looking at a
6085 compound-literal expression. */
6086 if (cp_parser_parse_definitely (parser))
6088 /* Warn the user that a compound literal is not
6089 allowed in standard C++. */
6090 pedwarn (input_location, OPT_Wpedantic,
6091 "ISO C++ forbids compound-literals");
6092 /* For simplicity, we disallow compound literals in
6093 constant-expressions. We could
6094 allow compound literals of integer type, whose
6095 initializer was a constant, in constant
6096 expressions. Permitting that usage, as a further
6097 extension, would not change the meaning of any
6098 currently accepted programs. (Of course, as
6099 compound literals are not part of ISO C++, the
6100 standard has nothing to say.) */
6101 if (cp_parser_non_integral_constant_expression (parser,
6102 NIC_NCC))
6104 postfix_expression = error_mark_node;
6105 break;
6107 /* Form the representation of the compound-literal. */
6108 postfix_expression
6109 = finish_compound_literal (type, initializer,
6110 tf_warning_or_error);
6111 break;
6115 /* It must be a primary-expression. */
6116 postfix_expression
6117 = cp_parser_primary_expression (parser, address_p, cast_p,
6118 /*template_arg_p=*/false,
6119 decltype_p,
6120 &idk);
6122 break;
6125 /* Note that we don't need to worry about calling build_cplus_new on a
6126 class-valued CALL_EXPR in decltype when it isn't the end of the
6127 postfix-expression; unary_complex_lvalue will take care of that for
6128 all these cases. */
6130 /* Keep looping until the postfix-expression is complete. */
6131 while (true)
6133 if (idk == CP_ID_KIND_UNQUALIFIED
6134 && identifier_p (postfix_expression)
6135 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6136 /* It is not a Koenig lookup function call. */
6137 postfix_expression
6138 = unqualified_name_lookup_error (postfix_expression);
6140 /* Peek at the next token. */
6141 token = cp_lexer_peek_token (parser->lexer);
6143 switch (token->type)
6145 case CPP_OPEN_SQUARE:
6146 if (cp_next_tokens_can_be_std_attribute_p (parser))
6148 cp_parser_error (parser,
6149 "two consecutive %<[%> shall "
6150 "only introduce an attribute");
6151 return error_mark_node;
6153 postfix_expression
6154 = cp_parser_postfix_open_square_expression (parser,
6155 postfix_expression,
6156 false,
6157 decltype_p);
6158 idk = CP_ID_KIND_NONE;
6159 is_member_access = false;
6160 break;
6162 case CPP_OPEN_PAREN:
6163 /* postfix-expression ( expression-list [opt] ) */
6165 bool koenig_p;
6166 bool is_builtin_constant_p;
6167 bool saved_integral_constant_expression_p = false;
6168 bool saved_non_integral_constant_expression_p = false;
6169 tsubst_flags_t complain = complain_flags (decltype_p);
6170 vec<tree, va_gc> *args;
6172 is_member_access = false;
6174 is_builtin_constant_p
6175 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6176 if (is_builtin_constant_p)
6178 /* The whole point of __builtin_constant_p is to allow
6179 non-constant expressions to appear as arguments. */
6180 saved_integral_constant_expression_p
6181 = parser->integral_constant_expression_p;
6182 saved_non_integral_constant_expression_p
6183 = parser->non_integral_constant_expression_p;
6184 parser->integral_constant_expression_p = false;
6186 args = (cp_parser_parenthesized_expression_list
6187 (parser, non_attr,
6188 /*cast_p=*/false, /*allow_expansion_p=*/true,
6189 /*non_constant_p=*/NULL,
6190 /*want_literal_zero_p=*/warn_memset_transposed_args));
6191 if (is_builtin_constant_p)
6193 parser->integral_constant_expression_p
6194 = saved_integral_constant_expression_p;
6195 parser->non_integral_constant_expression_p
6196 = saved_non_integral_constant_expression_p;
6199 if (args == NULL)
6201 postfix_expression = error_mark_node;
6202 break;
6205 /* Function calls are not permitted in
6206 constant-expressions. */
6207 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6208 && cp_parser_non_integral_constant_expression (parser,
6209 NIC_FUNC_CALL))
6211 postfix_expression = error_mark_node;
6212 release_tree_vector (args);
6213 break;
6216 koenig_p = false;
6217 if (idk == CP_ID_KIND_UNQUALIFIED
6218 || idk == CP_ID_KIND_TEMPLATE_ID)
6220 if (identifier_p (postfix_expression))
6222 if (!args->is_empty ())
6224 koenig_p = true;
6225 if (!any_type_dependent_arguments_p (args))
6226 postfix_expression
6227 = perform_koenig_lookup (postfix_expression, args,
6228 complain);
6230 else
6231 postfix_expression
6232 = unqualified_fn_lookup_error (postfix_expression);
6234 /* We do not perform argument-dependent lookup if
6235 normal lookup finds a non-function, in accordance
6236 with the expected resolution of DR 218. */
6237 else if (!args->is_empty ()
6238 && is_overloaded_fn (postfix_expression))
6240 tree fn = get_first_fn (postfix_expression);
6241 fn = STRIP_TEMPLATE (fn);
6243 /* Do not do argument dependent lookup if regular
6244 lookup finds a member function or a block-scope
6245 function declaration. [basic.lookup.argdep]/3 */
6246 if (!DECL_FUNCTION_MEMBER_P (fn)
6247 && !DECL_LOCAL_FUNCTION_P (fn))
6249 koenig_p = true;
6250 if (!any_type_dependent_arguments_p (args))
6251 postfix_expression
6252 = perform_koenig_lookup (postfix_expression, args,
6253 complain);
6258 if (warn_memset_transposed_args)
6260 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6261 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6262 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6263 && vec_safe_length (args) == 3
6264 && integer_zerop ((*args)[2])
6265 && LITERAL_ZERO_P ((*args)[2])
6266 && !(integer_zerop ((*args)[1])
6267 && LITERAL_ZERO_P ((*args)[1])))
6268 warning (OPT_Wmemset_transposed_args,
6269 "%<memset%> used with constant zero length "
6270 "parameter; this could be due to transposed "
6271 "parameters");
6273 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6274 to avoid leaking those into folder and middle-end. */
6275 unsigned int i;
6276 tree arg;
6277 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6278 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6279 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6282 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6284 tree instance = TREE_OPERAND (postfix_expression, 0);
6285 tree fn = TREE_OPERAND (postfix_expression, 1);
6287 if (processing_template_decl
6288 && (type_dependent_expression_p (instance)
6289 || (!BASELINK_P (fn)
6290 && TREE_CODE (fn) != FIELD_DECL)
6291 || type_dependent_expression_p (fn)
6292 || any_type_dependent_arguments_p (args)))
6294 postfix_expression
6295 = build_nt_call_vec (postfix_expression, args);
6296 release_tree_vector (args);
6297 break;
6300 if (BASELINK_P (fn))
6302 postfix_expression
6303 = (build_new_method_call
6304 (instance, fn, &args, NULL_TREE,
6305 (idk == CP_ID_KIND_QUALIFIED
6306 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6307 : LOOKUP_NORMAL),
6308 /*fn_p=*/NULL,
6309 complain));
6311 else
6312 postfix_expression
6313 = finish_call_expr (postfix_expression, &args,
6314 /*disallow_virtual=*/false,
6315 /*koenig_p=*/false,
6316 complain);
6318 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6319 || TREE_CODE (postfix_expression) == MEMBER_REF
6320 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6321 postfix_expression = (build_offset_ref_call_from_tree
6322 (postfix_expression, &args,
6323 complain));
6324 else if (idk == CP_ID_KIND_QUALIFIED)
6325 /* A call to a static class member, or a namespace-scope
6326 function. */
6327 postfix_expression
6328 = finish_call_expr (postfix_expression, &args,
6329 /*disallow_virtual=*/true,
6330 koenig_p,
6331 complain);
6332 else
6333 /* All other function calls. */
6334 postfix_expression
6335 = finish_call_expr (postfix_expression, &args,
6336 /*disallow_virtual=*/false,
6337 koenig_p,
6338 complain);
6340 protected_set_expr_location (postfix_expression, token->location);
6342 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6343 idk = CP_ID_KIND_NONE;
6345 release_tree_vector (args);
6347 break;
6349 case CPP_DOT:
6350 case CPP_DEREF:
6351 /* postfix-expression . template [opt] id-expression
6352 postfix-expression . pseudo-destructor-name
6353 postfix-expression -> template [opt] id-expression
6354 postfix-expression -> pseudo-destructor-name */
6356 /* Consume the `.' or `->' operator. */
6357 cp_lexer_consume_token (parser->lexer);
6359 postfix_expression
6360 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6361 postfix_expression,
6362 false, &idk, loc);
6364 is_member_access = true;
6365 break;
6367 case CPP_PLUS_PLUS:
6368 /* postfix-expression ++ */
6369 /* Consume the `++' token. */
6370 cp_lexer_consume_token (parser->lexer);
6371 /* Generate a representation for the complete expression. */
6372 postfix_expression
6373 = finish_increment_expr (postfix_expression,
6374 POSTINCREMENT_EXPR);
6375 /* Increments may not appear in constant-expressions. */
6376 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6377 postfix_expression = error_mark_node;
6378 idk = CP_ID_KIND_NONE;
6379 is_member_access = false;
6380 break;
6382 case CPP_MINUS_MINUS:
6383 /* postfix-expression -- */
6384 /* Consume the `--' token. */
6385 cp_lexer_consume_token (parser->lexer);
6386 /* Generate a representation for the complete expression. */
6387 postfix_expression
6388 = finish_increment_expr (postfix_expression,
6389 POSTDECREMENT_EXPR);
6390 /* Decrements may not appear in constant-expressions. */
6391 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6392 postfix_expression = error_mark_node;
6393 idk = CP_ID_KIND_NONE;
6394 is_member_access = false;
6395 break;
6397 default:
6398 if (pidk_return != NULL)
6399 * pidk_return = idk;
6400 if (member_access_only_p)
6401 return is_member_access? postfix_expression : error_mark_node;
6402 else
6403 return postfix_expression;
6407 /* We should never get here. */
6408 gcc_unreachable ();
6409 return error_mark_node;
6412 /* This function parses Cilk Plus array notations. If a normal array expr. is
6413 parsed then the array index is passed back to the caller through *INIT_INDEX
6414 and the function returns a NULL_TREE. If array notation expr. is parsed,
6415 then *INIT_INDEX is ignored by the caller and the function returns
6416 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6417 error_mark_node. */
6419 static tree
6420 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6421 tree array_value)
6423 cp_token *token = NULL;
6424 tree length_index, stride = NULL_TREE, value_tree, array_type;
6425 if (!array_value || array_value == error_mark_node)
6427 cp_parser_skip_to_end_of_statement (parser);
6428 return error_mark_node;
6431 array_type = TREE_TYPE (array_value);
6433 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6434 parser->colon_corrects_to_scope_p = false;
6435 token = cp_lexer_peek_token (parser->lexer);
6437 if (!token)
6439 cp_parser_error (parser, "expected %<:%> or numeral");
6440 return error_mark_node;
6442 else if (token->type == CPP_COLON)
6444 /* Consume the ':'. */
6445 cp_lexer_consume_token (parser->lexer);
6447 /* If we are here, then we have a case like this A[:]. */
6448 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6450 cp_parser_error (parser, "expected %<]%>");
6451 cp_parser_skip_to_end_of_statement (parser);
6452 return error_mark_node;
6454 *init_index = NULL_TREE;
6455 stride = NULL_TREE;
6456 length_index = NULL_TREE;
6458 else
6460 /* If we are here, then there are three valid possibilities:
6461 1. ARRAY [ EXP ]
6462 2. ARRAY [ EXP : EXP ]
6463 3. ARRAY [ EXP : EXP : EXP ] */
6465 *init_index = cp_parser_expression (parser);
6466 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6468 /* This indicates that we have a normal array expression. */
6469 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6470 return NULL_TREE;
6473 /* Consume the ':'. */
6474 cp_lexer_consume_token (parser->lexer);
6475 length_index = cp_parser_expression (parser);
6476 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6478 cp_lexer_consume_token (parser->lexer);
6479 stride = cp_parser_expression (parser);
6482 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6484 if (*init_index == error_mark_node || length_index == error_mark_node
6485 || stride == error_mark_node || array_type == error_mark_node)
6487 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6488 cp_lexer_consume_token (parser->lexer);
6489 return error_mark_node;
6491 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6493 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6494 length_index, stride, array_type);
6495 return value_tree;
6498 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6499 by cp_parser_builtin_offsetof. We're looking for
6501 postfix-expression [ expression ]
6502 postfix-expression [ braced-init-list ] (C++11)
6504 FOR_OFFSETOF is set if we're being called in that context, which
6505 changes how we deal with integer constant expressions. */
6507 static tree
6508 cp_parser_postfix_open_square_expression (cp_parser *parser,
6509 tree postfix_expression,
6510 bool for_offsetof,
6511 bool decltype_p)
6513 tree index = NULL_TREE;
6514 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6515 bool saved_greater_than_is_operator_p;
6517 /* Consume the `[' token. */
6518 cp_lexer_consume_token (parser->lexer);
6520 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6521 parser->greater_than_is_operator_p = true;
6523 /* Parse the index expression. */
6524 /* ??? For offsetof, there is a question of what to allow here. If
6525 offsetof is not being used in an integral constant expression context,
6526 then we *could* get the right answer by computing the value at runtime.
6527 If we are in an integral constant expression context, then we might
6528 could accept any constant expression; hard to say without analysis.
6529 Rather than open the barn door too wide right away, allow only integer
6530 constant expressions here. */
6531 if (for_offsetof)
6532 index = cp_parser_constant_expression (parser);
6533 else
6535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6537 bool expr_nonconst_p;
6538 cp_lexer_set_source_position (parser->lexer);
6539 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6540 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6541 if (flag_cilkplus
6542 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6544 error_at (cp_lexer_peek_token (parser->lexer)->location,
6545 "braced list index is not allowed with array "
6546 "notation");
6547 cp_parser_skip_to_end_of_statement (parser);
6548 return error_mark_node;
6551 else if (flag_cilkplus)
6553 /* Here are have these two options:
6554 ARRAY[EXP : EXP] - Array notation expr with default
6555 stride of 1.
6556 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6557 stride. */
6558 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6559 postfix_expression);
6560 if (an_exp)
6561 return an_exp;
6563 else
6564 index = cp_parser_expression (parser);
6567 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6569 /* Look for the closing `]'. */
6570 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6572 /* Build the ARRAY_REF. */
6573 postfix_expression = grok_array_decl (loc, postfix_expression,
6574 index, decltype_p);
6576 /* When not doing offsetof, array references are not permitted in
6577 constant-expressions. */
6578 if (!for_offsetof
6579 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6580 postfix_expression = error_mark_node;
6582 return postfix_expression;
6585 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6586 by cp_parser_builtin_offsetof. We're looking for
6588 postfix-expression . template [opt] id-expression
6589 postfix-expression . pseudo-destructor-name
6590 postfix-expression -> template [opt] id-expression
6591 postfix-expression -> pseudo-destructor-name
6593 FOR_OFFSETOF is set if we're being called in that context. That sorta
6594 limits what of the above we'll actually accept, but nevermind.
6595 TOKEN_TYPE is the "." or "->" token, which will already have been
6596 removed from the stream. */
6598 static tree
6599 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6600 enum cpp_ttype token_type,
6601 tree postfix_expression,
6602 bool for_offsetof, cp_id_kind *idk,
6603 location_t location)
6605 tree name;
6606 bool dependent_p;
6607 bool pseudo_destructor_p;
6608 tree scope = NULL_TREE;
6610 /* If this is a `->' operator, dereference the pointer. */
6611 if (token_type == CPP_DEREF)
6612 postfix_expression = build_x_arrow (location, postfix_expression,
6613 tf_warning_or_error);
6614 /* Check to see whether or not the expression is type-dependent. */
6615 dependent_p = type_dependent_expression_p (postfix_expression);
6616 /* The identifier following the `->' or `.' is not qualified. */
6617 parser->scope = NULL_TREE;
6618 parser->qualifying_scope = NULL_TREE;
6619 parser->object_scope = NULL_TREE;
6620 *idk = CP_ID_KIND_NONE;
6622 /* Enter the scope corresponding to the type of the object
6623 given by the POSTFIX_EXPRESSION. */
6624 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6626 scope = TREE_TYPE (postfix_expression);
6627 /* According to the standard, no expression should ever have
6628 reference type. Unfortunately, we do not currently match
6629 the standard in this respect in that our internal representation
6630 of an expression may have reference type even when the standard
6631 says it does not. Therefore, we have to manually obtain the
6632 underlying type here. */
6633 scope = non_reference (scope);
6634 /* The type of the POSTFIX_EXPRESSION must be complete. */
6635 if (scope == unknown_type_node)
6637 error_at (location, "%qE does not have class type",
6638 postfix_expression);
6639 scope = NULL_TREE;
6641 /* Unlike the object expression in other contexts, *this is not
6642 required to be of complete type for purposes of class member
6643 access (5.2.5) outside the member function body. */
6644 else if (postfix_expression != current_class_ref
6645 && !(processing_template_decl && scope == current_class_type))
6646 scope = complete_type_or_else (scope, NULL_TREE);
6647 /* Let the name lookup machinery know that we are processing a
6648 class member access expression. */
6649 parser->context->object_type = scope;
6650 /* If something went wrong, we want to be able to discern that case,
6651 as opposed to the case where there was no SCOPE due to the type
6652 of expression being dependent. */
6653 if (!scope)
6654 scope = error_mark_node;
6655 /* If the SCOPE was erroneous, make the various semantic analysis
6656 functions exit quickly -- and without issuing additional error
6657 messages. */
6658 if (scope == error_mark_node)
6659 postfix_expression = error_mark_node;
6662 /* Assume this expression is not a pseudo-destructor access. */
6663 pseudo_destructor_p = false;
6665 /* If the SCOPE is a scalar type, then, if this is a valid program,
6666 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6667 is type dependent, it can be pseudo-destructor-name or something else.
6668 Try to parse it as pseudo-destructor-name first. */
6669 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6671 tree s;
6672 tree type;
6674 cp_parser_parse_tentatively (parser);
6675 /* Parse the pseudo-destructor-name. */
6676 s = NULL_TREE;
6677 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6678 &s, &type);
6679 if (dependent_p
6680 && (cp_parser_error_occurred (parser)
6681 || !SCALAR_TYPE_P (type)))
6682 cp_parser_abort_tentative_parse (parser);
6683 else if (cp_parser_parse_definitely (parser))
6685 pseudo_destructor_p = true;
6686 postfix_expression
6687 = finish_pseudo_destructor_expr (postfix_expression,
6688 s, type, location);
6692 if (!pseudo_destructor_p)
6694 /* If the SCOPE is not a scalar type, we are looking at an
6695 ordinary class member access expression, rather than a
6696 pseudo-destructor-name. */
6697 bool template_p;
6698 cp_token *token = cp_lexer_peek_token (parser->lexer);
6699 /* Parse the id-expression. */
6700 name = (cp_parser_id_expression
6701 (parser,
6702 cp_parser_optional_template_keyword (parser),
6703 /*check_dependency_p=*/true,
6704 &template_p,
6705 /*declarator_p=*/false,
6706 /*optional_p=*/false));
6707 /* In general, build a SCOPE_REF if the member name is qualified.
6708 However, if the name was not dependent and has already been
6709 resolved; there is no need to build the SCOPE_REF. For example;
6711 struct X { void f(); };
6712 template <typename T> void f(T* t) { t->X::f(); }
6714 Even though "t" is dependent, "X::f" is not and has been resolved
6715 to a BASELINK; there is no need to include scope information. */
6717 /* But we do need to remember that there was an explicit scope for
6718 virtual function calls. */
6719 if (parser->scope)
6720 *idk = CP_ID_KIND_QUALIFIED;
6722 /* If the name is a template-id that names a type, we will get a
6723 TYPE_DECL here. That is invalid code. */
6724 if (TREE_CODE (name) == TYPE_DECL)
6726 error_at (token->location, "invalid use of %qD", name);
6727 postfix_expression = error_mark_node;
6729 else
6731 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6733 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6735 error_at (token->location, "%<%D::%D%> is not a class member",
6736 parser->scope, name);
6737 postfix_expression = error_mark_node;
6739 else
6740 name = build_qualified_name (/*type=*/NULL_TREE,
6741 parser->scope,
6742 name,
6743 template_p);
6744 parser->scope = NULL_TREE;
6745 parser->qualifying_scope = NULL_TREE;
6746 parser->object_scope = NULL_TREE;
6748 if (parser->scope && name && BASELINK_P (name))
6749 adjust_result_of_qualified_name_lookup
6750 (name, parser->scope, scope);
6751 postfix_expression
6752 = finish_class_member_access_expr (postfix_expression, name,
6753 template_p,
6754 tf_warning_or_error);
6758 /* We no longer need to look up names in the scope of the object on
6759 the left-hand side of the `.' or `->' operator. */
6760 parser->context->object_type = NULL_TREE;
6762 /* Outside of offsetof, these operators may not appear in
6763 constant-expressions. */
6764 if (!for_offsetof
6765 && (cp_parser_non_integral_constant_expression
6766 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6767 postfix_expression = error_mark_node;
6769 return postfix_expression;
6772 /* Cache of LITERAL_ZERO_P constants. */
6774 static GTY(()) tree literal_zeros[itk_none];
6776 /* Parse a parenthesized expression-list.
6778 expression-list:
6779 assignment-expression
6780 expression-list, assignment-expression
6782 attribute-list:
6783 expression-list
6784 identifier
6785 identifier, expression-list
6787 CAST_P is true if this expression is the target of a cast.
6789 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6790 argument pack.
6792 Returns a vector of trees. Each element is a representation of an
6793 assignment-expression. NULL is returned if the ( and or ) are
6794 missing. An empty, but allocated, vector is returned on no
6795 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6796 if we are parsing an attribute list for an attribute that wants a
6797 plain identifier argument, normal_attr for an attribute that wants
6798 an expression, or non_attr if we aren't parsing an attribute list. If
6799 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6800 not all of the expressions in the list were constant.
6801 WANT_LITERAL_ZERO_P is true if the caller is interested in
6802 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6803 immediately, this can be removed. */
6805 static vec<tree, va_gc> *
6806 cp_parser_parenthesized_expression_list (cp_parser* parser,
6807 int is_attribute_list,
6808 bool cast_p,
6809 bool allow_expansion_p,
6810 bool *non_constant_p,
6811 bool want_literal_zero_p)
6813 vec<tree, va_gc> *expression_list;
6814 bool fold_expr_p = is_attribute_list != non_attr;
6815 tree identifier = NULL_TREE;
6816 bool saved_greater_than_is_operator_p;
6818 /* Assume all the expressions will be constant. */
6819 if (non_constant_p)
6820 *non_constant_p = false;
6822 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6823 return NULL;
6825 expression_list = make_tree_vector ();
6827 /* Within a parenthesized expression, a `>' token is always
6828 the greater-than operator. */
6829 saved_greater_than_is_operator_p
6830 = parser->greater_than_is_operator_p;
6831 parser->greater_than_is_operator_p = true;
6833 /* Consume expressions until there are no more. */
6834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6835 while (true)
6837 tree expr;
6839 /* At the beginning of attribute lists, check to see if the
6840 next token is an identifier. */
6841 if (is_attribute_list == id_attr
6842 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6844 cp_token *token;
6846 /* Consume the identifier. */
6847 token = cp_lexer_consume_token (parser->lexer);
6848 /* Save the identifier. */
6849 identifier = token->u.value;
6851 else
6853 bool expr_non_constant_p;
6855 /* Parse the next assignment-expression. */
6856 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6858 /* A braced-init-list. */
6859 cp_lexer_set_source_position (parser->lexer);
6860 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6861 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6862 if (non_constant_p && expr_non_constant_p)
6863 *non_constant_p = true;
6865 else if (non_constant_p)
6867 expr = (cp_parser_constant_expression
6868 (parser, /*allow_non_constant_p=*/true,
6869 &expr_non_constant_p));
6870 if (expr_non_constant_p)
6871 *non_constant_p = true;
6873 else
6875 expr = NULL_TREE;
6876 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6877 switch (tok->type)
6879 case CPP_NUMBER:
6880 case CPP_CHAR:
6881 case CPP_WCHAR:
6882 case CPP_CHAR16:
6883 case CPP_CHAR32:
6884 /* If a parameter is literal zero alone, remember it
6885 for -Wmemset-transposed-args warning. */
6886 if (integer_zerop (tok->u.value)
6887 && !TREE_OVERFLOW (tok->u.value)
6888 && want_literal_zero_p
6889 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6890 == CPP_COMMA
6891 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6892 == CPP_CLOSE_PAREN))
6894 unsigned int i;
6895 for (i = 0; i < itk_none; ++i)
6896 if (TREE_TYPE (tok->u.value) == integer_types[i])
6897 break;
6898 if (i < itk_none && literal_zeros[i])
6899 expr = literal_zeros[i];
6900 else
6902 expr = copy_node (tok->u.value);
6903 LITERAL_ZERO_P (expr) = 1;
6904 if (i < itk_none)
6905 literal_zeros[i] = expr;
6907 /* Consume the 0 token (or '\0', 0LL etc.). */
6908 cp_lexer_consume_token (parser->lexer);
6910 break;
6911 default:
6912 break;
6914 if (expr == NULL_TREE)
6915 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6916 cast_p);
6919 if (fold_expr_p)
6920 expr = instantiate_non_dependent_expr (expr);
6922 /* If we have an ellipsis, then this is an expression
6923 expansion. */
6924 if (allow_expansion_p
6925 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6927 /* Consume the `...'. */
6928 cp_lexer_consume_token (parser->lexer);
6930 /* Build the argument pack. */
6931 expr = make_pack_expansion (expr);
6934 /* Add it to the list. We add error_mark_node
6935 expressions to the list, so that we can still tell if
6936 the correct form for a parenthesized expression-list
6937 is found. That gives better errors. */
6938 vec_safe_push (expression_list, expr);
6940 if (expr == error_mark_node)
6941 goto skip_comma;
6944 /* After the first item, attribute lists look the same as
6945 expression lists. */
6946 is_attribute_list = non_attr;
6948 get_comma:;
6949 /* If the next token isn't a `,', then we are done. */
6950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6951 break;
6953 /* Otherwise, consume the `,' and keep going. */
6954 cp_lexer_consume_token (parser->lexer);
6957 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6959 int ending;
6961 skip_comma:;
6962 /* We try and resync to an unnested comma, as that will give the
6963 user better diagnostics. */
6964 ending = cp_parser_skip_to_closing_parenthesis (parser,
6965 /*recovering=*/true,
6966 /*or_comma=*/true,
6967 /*consume_paren=*/true);
6968 if (ending < 0)
6969 goto get_comma;
6970 if (!ending)
6972 parser->greater_than_is_operator_p
6973 = saved_greater_than_is_operator_p;
6974 return NULL;
6978 parser->greater_than_is_operator_p
6979 = saved_greater_than_is_operator_p;
6981 if (identifier)
6982 vec_safe_insert (expression_list, 0, identifier);
6984 return expression_list;
6987 /* Parse a pseudo-destructor-name.
6989 pseudo-destructor-name:
6990 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6991 :: [opt] nested-name-specifier template template-id :: ~ type-name
6992 :: [opt] nested-name-specifier [opt] ~ type-name
6994 If either of the first two productions is used, sets *SCOPE to the
6995 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6996 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6997 or ERROR_MARK_NODE if the parse fails. */
6999 static void
7000 cp_parser_pseudo_destructor_name (cp_parser* parser,
7001 tree object,
7002 tree* scope,
7003 tree* type)
7005 bool nested_name_specifier_p;
7007 /* Handle ~auto. */
7008 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7009 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7010 && !type_dependent_expression_p (object))
7012 if (cxx_dialect < cxx14)
7013 pedwarn (input_location, 0,
7014 "%<~auto%> only available with "
7015 "-std=c++14 or -std=gnu++14");
7016 cp_lexer_consume_token (parser->lexer);
7017 cp_lexer_consume_token (parser->lexer);
7018 *scope = NULL_TREE;
7019 *type = TREE_TYPE (object);
7020 return;
7023 /* Assume that things will not work out. */
7024 *type = error_mark_node;
7026 /* Look for the optional `::' operator. */
7027 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7028 /* Look for the optional nested-name-specifier. */
7029 nested_name_specifier_p
7030 = (cp_parser_nested_name_specifier_opt (parser,
7031 /*typename_keyword_p=*/false,
7032 /*check_dependency_p=*/true,
7033 /*type_p=*/false,
7034 /*is_declaration=*/false)
7035 != NULL_TREE);
7036 /* Now, if we saw a nested-name-specifier, we might be doing the
7037 second production. */
7038 if (nested_name_specifier_p
7039 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7041 /* Consume the `template' keyword. */
7042 cp_lexer_consume_token (parser->lexer);
7043 /* Parse the template-id. */
7044 cp_parser_template_id (parser,
7045 /*template_keyword_p=*/true,
7046 /*check_dependency_p=*/false,
7047 class_type,
7048 /*is_declaration=*/true);
7049 /* Look for the `::' token. */
7050 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7052 /* If the next token is not a `~', then there might be some
7053 additional qualification. */
7054 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7056 /* At this point, we're looking for "type-name :: ~". The type-name
7057 must not be a class-name, since this is a pseudo-destructor. So,
7058 it must be either an enum-name, or a typedef-name -- both of which
7059 are just identifiers. So, we peek ahead to check that the "::"
7060 and "~" tokens are present; if they are not, then we can avoid
7061 calling type_name. */
7062 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7063 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7064 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7066 cp_parser_error (parser, "non-scalar type");
7067 return;
7070 /* Look for the type-name. */
7071 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7072 if (*scope == error_mark_node)
7073 return;
7075 /* Look for the `::' token. */
7076 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7078 else
7079 *scope = NULL_TREE;
7081 /* Look for the `~'. */
7082 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7084 /* Once we see the ~, this has to be a pseudo-destructor. */
7085 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7086 cp_parser_commit_to_topmost_tentative_parse (parser);
7088 /* Look for the type-name again. We are not responsible for
7089 checking that it matches the first type-name. */
7090 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7093 /* Parse a unary-expression.
7095 unary-expression:
7096 postfix-expression
7097 ++ cast-expression
7098 -- cast-expression
7099 unary-operator cast-expression
7100 sizeof unary-expression
7101 sizeof ( type-id )
7102 alignof ( type-id ) [C++0x]
7103 new-expression
7104 delete-expression
7106 GNU Extensions:
7108 unary-expression:
7109 __extension__ cast-expression
7110 __alignof__ unary-expression
7111 __alignof__ ( type-id )
7112 alignof unary-expression [C++0x]
7113 __real__ cast-expression
7114 __imag__ cast-expression
7115 && identifier
7116 sizeof ( type-id ) { initializer-list , [opt] }
7117 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7118 __alignof__ ( type-id ) { initializer-list , [opt] }
7120 ADDRESS_P is true iff the unary-expression is appearing as the
7121 operand of the `&' operator. CAST_P is true if this expression is
7122 the target of a cast.
7124 Returns a representation of the expression. */
7126 static tree
7127 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7128 bool address_p, bool cast_p, bool decltype_p)
7130 cp_token *token;
7131 enum tree_code unary_operator;
7133 /* Peek at the next token. */
7134 token = cp_lexer_peek_token (parser->lexer);
7135 /* Some keywords give away the kind of expression. */
7136 if (token->type == CPP_KEYWORD)
7138 enum rid keyword = token->keyword;
7140 switch (keyword)
7142 case RID_ALIGNOF:
7143 case RID_SIZEOF:
7145 tree operand, ret;
7146 enum tree_code op;
7147 location_t first_loc;
7149 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7150 /* Consume the token. */
7151 cp_lexer_consume_token (parser->lexer);
7152 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7153 /* Parse the operand. */
7154 operand = cp_parser_sizeof_operand (parser, keyword);
7156 if (TYPE_P (operand))
7157 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7158 else
7160 /* ISO C++ defines alignof only with types, not with
7161 expressions. So pedwarn if alignof is used with a non-
7162 type expression. However, __alignof__ is ok. */
7163 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7164 pedwarn (token->location, OPT_Wpedantic,
7165 "ISO C++ does not allow %<alignof%> "
7166 "with a non-type");
7168 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7170 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7171 SIZEOF_EXPR with the original operand. */
7172 if (op == SIZEOF_EXPR && ret != error_mark_node)
7174 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7176 if (!processing_template_decl && TYPE_P (operand))
7178 ret = build_min (SIZEOF_EXPR, size_type_node,
7179 build1 (NOP_EXPR, operand,
7180 error_mark_node));
7181 SIZEOF_EXPR_TYPE_P (ret) = 1;
7183 else
7184 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7185 TREE_SIDE_EFFECTS (ret) = 0;
7186 TREE_READONLY (ret) = 1;
7188 SET_EXPR_LOCATION (ret, first_loc);
7190 return ret;
7193 case RID_NEW:
7194 return cp_parser_new_expression (parser);
7196 case RID_DELETE:
7197 return cp_parser_delete_expression (parser);
7199 case RID_EXTENSION:
7201 /* The saved value of the PEDANTIC flag. */
7202 int saved_pedantic;
7203 tree expr;
7205 /* Save away the PEDANTIC flag. */
7206 cp_parser_extension_opt (parser, &saved_pedantic);
7207 /* Parse the cast-expression. */
7208 expr = cp_parser_simple_cast_expression (parser);
7209 /* Restore the PEDANTIC flag. */
7210 pedantic = saved_pedantic;
7212 return expr;
7215 case RID_REALPART:
7216 case RID_IMAGPART:
7218 tree expression;
7220 /* Consume the `__real__' or `__imag__' token. */
7221 cp_lexer_consume_token (parser->lexer);
7222 /* Parse the cast-expression. */
7223 expression = cp_parser_simple_cast_expression (parser);
7224 /* Create the complete representation. */
7225 return build_x_unary_op (token->location,
7226 (keyword == RID_REALPART
7227 ? REALPART_EXPR : IMAGPART_EXPR),
7228 expression,
7229 tf_warning_or_error);
7231 break;
7233 case RID_TRANSACTION_ATOMIC:
7234 case RID_TRANSACTION_RELAXED:
7235 return cp_parser_transaction_expression (parser, keyword);
7237 case RID_NOEXCEPT:
7239 tree expr;
7240 const char *saved_message;
7241 bool saved_integral_constant_expression_p;
7242 bool saved_non_integral_constant_expression_p;
7243 bool saved_greater_than_is_operator_p;
7245 cp_lexer_consume_token (parser->lexer);
7246 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7248 saved_message = parser->type_definition_forbidden_message;
7249 parser->type_definition_forbidden_message
7250 = G_("types may not be defined in %<noexcept%> expressions");
7252 saved_integral_constant_expression_p
7253 = parser->integral_constant_expression_p;
7254 saved_non_integral_constant_expression_p
7255 = parser->non_integral_constant_expression_p;
7256 parser->integral_constant_expression_p = false;
7258 saved_greater_than_is_operator_p
7259 = parser->greater_than_is_operator_p;
7260 parser->greater_than_is_operator_p = true;
7262 ++cp_unevaluated_operand;
7263 ++c_inhibit_evaluation_warnings;
7264 ++cp_noexcept_operand;
7265 expr = cp_parser_expression (parser);
7266 --cp_noexcept_operand;
7267 --c_inhibit_evaluation_warnings;
7268 --cp_unevaluated_operand;
7270 parser->greater_than_is_operator_p
7271 = saved_greater_than_is_operator_p;
7273 parser->integral_constant_expression_p
7274 = saved_integral_constant_expression_p;
7275 parser->non_integral_constant_expression_p
7276 = saved_non_integral_constant_expression_p;
7278 parser->type_definition_forbidden_message = saved_message;
7280 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7281 return finish_noexcept_expr (expr, tf_warning_or_error);
7284 default:
7285 break;
7289 /* Look for the `:: new' and `:: delete', which also signal the
7290 beginning of a new-expression, or delete-expression,
7291 respectively. If the next token is `::', then it might be one of
7292 these. */
7293 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7295 enum rid keyword;
7297 /* See if the token after the `::' is one of the keywords in
7298 which we're interested. */
7299 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7300 /* If it's `new', we have a new-expression. */
7301 if (keyword == RID_NEW)
7302 return cp_parser_new_expression (parser);
7303 /* Similarly, for `delete'. */
7304 else if (keyword == RID_DELETE)
7305 return cp_parser_delete_expression (parser);
7308 /* Look for a unary operator. */
7309 unary_operator = cp_parser_unary_operator (token);
7310 /* The `++' and `--' operators can be handled similarly, even though
7311 they are not technically unary-operators in the grammar. */
7312 if (unary_operator == ERROR_MARK)
7314 if (token->type == CPP_PLUS_PLUS)
7315 unary_operator = PREINCREMENT_EXPR;
7316 else if (token->type == CPP_MINUS_MINUS)
7317 unary_operator = PREDECREMENT_EXPR;
7318 /* Handle the GNU address-of-label extension. */
7319 else if (cp_parser_allow_gnu_extensions_p (parser)
7320 && token->type == CPP_AND_AND)
7322 tree identifier;
7323 tree expression;
7324 location_t loc = token->location;
7326 /* Consume the '&&' token. */
7327 cp_lexer_consume_token (parser->lexer);
7328 /* Look for the identifier. */
7329 identifier = cp_parser_identifier (parser);
7330 /* Create an expression representing the address. */
7331 expression = finish_label_address_expr (identifier, loc);
7332 if (cp_parser_non_integral_constant_expression (parser,
7333 NIC_ADDR_LABEL))
7334 expression = error_mark_node;
7335 return expression;
7338 if (unary_operator != ERROR_MARK)
7340 tree cast_expression;
7341 tree expression = error_mark_node;
7342 non_integral_constant non_constant_p = NIC_NONE;
7343 location_t loc = token->location;
7344 tsubst_flags_t complain = complain_flags (decltype_p);
7346 /* Consume the operator token. */
7347 token = cp_lexer_consume_token (parser->lexer);
7348 /* Parse the cast-expression. */
7349 cast_expression
7350 = cp_parser_cast_expression (parser,
7351 unary_operator == ADDR_EXPR,
7352 /*cast_p=*/false,
7353 /*decltype*/false,
7354 pidk);
7355 /* Now, build an appropriate representation. */
7356 switch (unary_operator)
7358 case INDIRECT_REF:
7359 non_constant_p = NIC_STAR;
7360 expression = build_x_indirect_ref (loc, cast_expression,
7361 RO_UNARY_STAR,
7362 complain);
7363 break;
7365 case ADDR_EXPR:
7366 non_constant_p = NIC_ADDR;
7367 /* Fall through. */
7368 case BIT_NOT_EXPR:
7369 expression = build_x_unary_op (loc, unary_operator,
7370 cast_expression,
7371 complain);
7372 break;
7374 case PREINCREMENT_EXPR:
7375 case PREDECREMENT_EXPR:
7376 non_constant_p = unary_operator == PREINCREMENT_EXPR
7377 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7378 /* Fall through. */
7379 case UNARY_PLUS_EXPR:
7380 case NEGATE_EXPR:
7381 case TRUTH_NOT_EXPR:
7382 expression = finish_unary_op_expr (loc, unary_operator,
7383 cast_expression, complain);
7384 break;
7386 default:
7387 gcc_unreachable ();
7390 if (non_constant_p != NIC_NONE
7391 && cp_parser_non_integral_constant_expression (parser,
7392 non_constant_p))
7393 expression = error_mark_node;
7395 return expression;
7398 return cp_parser_postfix_expression (parser, address_p, cast_p,
7399 /*member_access_only_p=*/false,
7400 decltype_p,
7401 pidk);
7404 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7405 unary-operator, the corresponding tree code is returned. */
7407 static enum tree_code
7408 cp_parser_unary_operator (cp_token* token)
7410 switch (token->type)
7412 case CPP_MULT:
7413 return INDIRECT_REF;
7415 case CPP_AND:
7416 return ADDR_EXPR;
7418 case CPP_PLUS:
7419 return UNARY_PLUS_EXPR;
7421 case CPP_MINUS:
7422 return NEGATE_EXPR;
7424 case CPP_NOT:
7425 return TRUTH_NOT_EXPR;
7427 case CPP_COMPL:
7428 return BIT_NOT_EXPR;
7430 default:
7431 return ERROR_MARK;
7435 /* Parse a new-expression.
7437 new-expression:
7438 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7439 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7441 Returns a representation of the expression. */
7443 static tree
7444 cp_parser_new_expression (cp_parser* parser)
7446 bool global_scope_p;
7447 vec<tree, va_gc> *placement;
7448 tree type;
7449 vec<tree, va_gc> *initializer;
7450 tree nelts = NULL_TREE;
7451 tree ret;
7453 /* Look for the optional `::' operator. */
7454 global_scope_p
7455 = (cp_parser_global_scope_opt (parser,
7456 /*current_scope_valid_p=*/false)
7457 != NULL_TREE);
7458 /* Look for the `new' operator. */
7459 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7460 /* There's no easy way to tell a new-placement from the
7461 `( type-id )' construct. */
7462 cp_parser_parse_tentatively (parser);
7463 /* Look for a new-placement. */
7464 placement = cp_parser_new_placement (parser);
7465 /* If that didn't work out, there's no new-placement. */
7466 if (!cp_parser_parse_definitely (parser))
7468 if (placement != NULL)
7469 release_tree_vector (placement);
7470 placement = NULL;
7473 /* If the next token is a `(', then we have a parenthesized
7474 type-id. */
7475 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7477 cp_token *token;
7478 const char *saved_message = parser->type_definition_forbidden_message;
7480 /* Consume the `('. */
7481 cp_lexer_consume_token (parser->lexer);
7483 /* Parse the type-id. */
7484 parser->type_definition_forbidden_message
7485 = G_("types may not be defined in a new-expression");
7486 type = cp_parser_type_id (parser);
7487 parser->type_definition_forbidden_message = saved_message;
7489 /* Look for the closing `)'. */
7490 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7491 token = cp_lexer_peek_token (parser->lexer);
7492 /* There should not be a direct-new-declarator in this production,
7493 but GCC used to allowed this, so we check and emit a sensible error
7494 message for this case. */
7495 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7497 error_at (token->location,
7498 "array bound forbidden after parenthesized type-id");
7499 inform (token->location,
7500 "try removing the parentheses around the type-id");
7501 cp_parser_direct_new_declarator (parser);
7504 /* Otherwise, there must be a new-type-id. */
7505 else
7506 type = cp_parser_new_type_id (parser, &nelts);
7508 /* If the next token is a `(' or '{', then we have a new-initializer. */
7509 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7510 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7511 initializer = cp_parser_new_initializer (parser);
7512 else
7513 initializer = NULL;
7515 /* A new-expression may not appear in an integral constant
7516 expression. */
7517 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7518 ret = error_mark_node;
7519 else
7521 /* Create a representation of the new-expression. */
7522 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7523 tf_warning_or_error);
7526 if (placement != NULL)
7527 release_tree_vector (placement);
7528 if (initializer != NULL)
7529 release_tree_vector (initializer);
7531 return ret;
7534 /* Parse a new-placement.
7536 new-placement:
7537 ( expression-list )
7539 Returns the same representation as for an expression-list. */
7541 static vec<tree, va_gc> *
7542 cp_parser_new_placement (cp_parser* parser)
7544 vec<tree, va_gc> *expression_list;
7546 /* Parse the expression-list. */
7547 expression_list = (cp_parser_parenthesized_expression_list
7548 (parser, non_attr, /*cast_p=*/false,
7549 /*allow_expansion_p=*/true,
7550 /*non_constant_p=*/NULL));
7552 return expression_list;
7555 /* Parse a new-type-id.
7557 new-type-id:
7558 type-specifier-seq new-declarator [opt]
7560 Returns the TYPE allocated. If the new-type-id indicates an array
7561 type, *NELTS is set to the number of elements in the last array
7562 bound; the TYPE will not include the last array bound. */
7564 static tree
7565 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7567 cp_decl_specifier_seq type_specifier_seq;
7568 cp_declarator *new_declarator;
7569 cp_declarator *declarator;
7570 cp_declarator *outer_declarator;
7571 const char *saved_message;
7573 /* The type-specifier sequence must not contain type definitions.
7574 (It cannot contain declarations of new types either, but if they
7575 are not definitions we will catch that because they are not
7576 complete.) */
7577 saved_message = parser->type_definition_forbidden_message;
7578 parser->type_definition_forbidden_message
7579 = G_("types may not be defined in a new-type-id");
7580 /* Parse the type-specifier-seq. */
7581 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7582 /*is_trailing_return=*/false,
7583 &type_specifier_seq);
7584 /* Restore the old message. */
7585 parser->type_definition_forbidden_message = saved_message;
7587 if (type_specifier_seq.type == error_mark_node)
7588 return error_mark_node;
7590 /* Parse the new-declarator. */
7591 new_declarator = cp_parser_new_declarator_opt (parser);
7593 /* Determine the number of elements in the last array dimension, if
7594 any. */
7595 *nelts = NULL_TREE;
7596 /* Skip down to the last array dimension. */
7597 declarator = new_declarator;
7598 outer_declarator = NULL;
7599 while (declarator && (declarator->kind == cdk_pointer
7600 || declarator->kind == cdk_ptrmem))
7602 outer_declarator = declarator;
7603 declarator = declarator->declarator;
7605 while (declarator
7606 && declarator->kind == cdk_array
7607 && declarator->declarator
7608 && declarator->declarator->kind == cdk_array)
7610 outer_declarator = declarator;
7611 declarator = declarator->declarator;
7614 if (declarator && declarator->kind == cdk_array)
7616 *nelts = declarator->u.array.bounds;
7617 if (*nelts == error_mark_node)
7618 *nelts = integer_one_node;
7620 if (outer_declarator)
7621 outer_declarator->declarator = declarator->declarator;
7622 else
7623 new_declarator = NULL;
7626 return groktypename (&type_specifier_seq, new_declarator, false);
7629 /* Parse an (optional) new-declarator.
7631 new-declarator:
7632 ptr-operator new-declarator [opt]
7633 direct-new-declarator
7635 Returns the declarator. */
7637 static cp_declarator *
7638 cp_parser_new_declarator_opt (cp_parser* parser)
7640 enum tree_code code;
7641 tree type, std_attributes = NULL_TREE;
7642 cp_cv_quals cv_quals;
7644 /* We don't know if there's a ptr-operator next, or not. */
7645 cp_parser_parse_tentatively (parser);
7646 /* Look for a ptr-operator. */
7647 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7648 /* If that worked, look for more new-declarators. */
7649 if (cp_parser_parse_definitely (parser))
7651 cp_declarator *declarator;
7653 /* Parse another optional declarator. */
7654 declarator = cp_parser_new_declarator_opt (parser);
7656 declarator = cp_parser_make_indirect_declarator
7657 (code, type, cv_quals, declarator, std_attributes);
7659 return declarator;
7662 /* If the next token is a `[', there is a direct-new-declarator. */
7663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7664 return cp_parser_direct_new_declarator (parser);
7666 return NULL;
7669 /* Parse a direct-new-declarator.
7671 direct-new-declarator:
7672 [ expression ]
7673 direct-new-declarator [constant-expression]
7677 static cp_declarator *
7678 cp_parser_direct_new_declarator (cp_parser* parser)
7680 cp_declarator *declarator = NULL;
7682 while (true)
7684 tree expression;
7685 cp_token *token;
7687 /* Look for the opening `['. */
7688 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7690 token = cp_lexer_peek_token (parser->lexer);
7691 expression = cp_parser_expression (parser);
7692 /* The standard requires that the expression have integral
7693 type. DR 74 adds enumeration types. We believe that the
7694 real intent is that these expressions be handled like the
7695 expression in a `switch' condition, which also allows
7696 classes with a single conversion to integral or
7697 enumeration type. */
7698 if (!processing_template_decl)
7700 expression
7701 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7702 expression,
7703 /*complain=*/true);
7704 if (!expression)
7706 error_at (token->location,
7707 "expression in new-declarator must have integral "
7708 "or enumeration type");
7709 expression = error_mark_node;
7713 /* Look for the closing `]'. */
7714 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7716 /* Add this bound to the declarator. */
7717 declarator = make_array_declarator (declarator, expression);
7719 /* If the next token is not a `[', then there are no more
7720 bounds. */
7721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7722 break;
7725 return declarator;
7728 /* Parse a new-initializer.
7730 new-initializer:
7731 ( expression-list [opt] )
7732 braced-init-list
7734 Returns a representation of the expression-list. */
7736 static vec<tree, va_gc> *
7737 cp_parser_new_initializer (cp_parser* parser)
7739 vec<tree, va_gc> *expression_list;
7741 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7743 tree t;
7744 bool expr_non_constant_p;
7745 cp_lexer_set_source_position (parser->lexer);
7746 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7747 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7748 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7749 expression_list = make_tree_vector_single (t);
7751 else
7752 expression_list = (cp_parser_parenthesized_expression_list
7753 (parser, non_attr, /*cast_p=*/false,
7754 /*allow_expansion_p=*/true,
7755 /*non_constant_p=*/NULL));
7757 return expression_list;
7760 /* Parse a delete-expression.
7762 delete-expression:
7763 :: [opt] delete cast-expression
7764 :: [opt] delete [ ] cast-expression
7766 Returns a representation of the expression. */
7768 static tree
7769 cp_parser_delete_expression (cp_parser* parser)
7771 bool global_scope_p;
7772 bool array_p;
7773 tree expression;
7775 /* Look for the optional `::' operator. */
7776 global_scope_p
7777 = (cp_parser_global_scope_opt (parser,
7778 /*current_scope_valid_p=*/false)
7779 != NULL_TREE);
7780 /* Look for the `delete' keyword. */
7781 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7782 /* See if the array syntax is in use. */
7783 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7785 /* Consume the `[' token. */
7786 cp_lexer_consume_token (parser->lexer);
7787 /* Look for the `]' token. */
7788 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7789 /* Remember that this is the `[]' construct. */
7790 array_p = true;
7792 else
7793 array_p = false;
7795 /* Parse the cast-expression. */
7796 expression = cp_parser_simple_cast_expression (parser);
7798 /* A delete-expression may not appear in an integral constant
7799 expression. */
7800 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7801 return error_mark_node;
7803 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7804 tf_warning_or_error);
7807 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7808 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7809 0 otherwise. */
7811 static int
7812 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7814 cp_token *token = cp_lexer_peek_token (parser->lexer);
7815 switch (token->type)
7817 case CPP_COMMA:
7818 case CPP_SEMICOLON:
7819 case CPP_QUERY:
7820 case CPP_COLON:
7821 case CPP_CLOSE_SQUARE:
7822 case CPP_CLOSE_PAREN:
7823 case CPP_CLOSE_BRACE:
7824 case CPP_OPEN_BRACE:
7825 case CPP_DOT:
7826 case CPP_DOT_STAR:
7827 case CPP_DEREF:
7828 case CPP_DEREF_STAR:
7829 case CPP_DIV:
7830 case CPP_MOD:
7831 case CPP_LSHIFT:
7832 case CPP_RSHIFT:
7833 case CPP_LESS:
7834 case CPP_GREATER:
7835 case CPP_LESS_EQ:
7836 case CPP_GREATER_EQ:
7837 case CPP_EQ_EQ:
7838 case CPP_NOT_EQ:
7839 case CPP_EQ:
7840 case CPP_MULT_EQ:
7841 case CPP_DIV_EQ:
7842 case CPP_MOD_EQ:
7843 case CPP_PLUS_EQ:
7844 case CPP_MINUS_EQ:
7845 case CPP_RSHIFT_EQ:
7846 case CPP_LSHIFT_EQ:
7847 case CPP_AND_EQ:
7848 case CPP_XOR_EQ:
7849 case CPP_OR_EQ:
7850 case CPP_XOR:
7851 case CPP_OR:
7852 case CPP_OR_OR:
7853 case CPP_EOF:
7854 case CPP_ELLIPSIS:
7855 return 0;
7857 case CPP_OPEN_PAREN:
7858 /* In ((type ()) () the last () isn't a valid cast-expression,
7859 so the whole must be parsed as postfix-expression. */
7860 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7861 != CPP_CLOSE_PAREN;
7863 case CPP_OPEN_SQUARE:
7864 /* '[' may start a primary-expression in obj-c++ and in C++11,
7865 as a lambda-expression, eg, '(void)[]{}'. */
7866 if (cxx_dialect >= cxx11)
7867 return -1;
7868 return c_dialect_objc ();
7870 case CPP_PLUS_PLUS:
7871 case CPP_MINUS_MINUS:
7872 /* '++' and '--' may or may not start a cast-expression:
7874 struct T { void operator++(int); };
7875 void f() { (T())++; }
7879 int a;
7880 (int)++a; */
7881 return -1;
7883 default:
7884 return 1;
7888 /* Parse a cast-expression.
7890 cast-expression:
7891 unary-expression
7892 ( type-id ) cast-expression
7894 ADDRESS_P is true iff the unary-expression is appearing as the
7895 operand of the `&' operator. CAST_P is true if this expression is
7896 the target of a cast.
7898 Returns a representation of the expression. */
7900 static tree
7901 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7902 bool decltype_p, cp_id_kind * pidk)
7904 /* If it's a `(', then we might be looking at a cast. */
7905 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7907 tree type = NULL_TREE;
7908 tree expr = NULL_TREE;
7909 int cast_expression = 0;
7910 const char *saved_message;
7912 /* There's no way to know yet whether or not this is a cast.
7913 For example, `(int (3))' is a unary-expression, while `(int)
7914 3' is a cast. So, we resort to parsing tentatively. */
7915 cp_parser_parse_tentatively (parser);
7916 /* Types may not be defined in a cast. */
7917 saved_message = parser->type_definition_forbidden_message;
7918 parser->type_definition_forbidden_message
7919 = G_("types may not be defined in casts");
7920 /* Consume the `('. */
7921 cp_lexer_consume_token (parser->lexer);
7922 /* A very tricky bit is that `(struct S) { 3 }' is a
7923 compound-literal (which we permit in C++ as an extension).
7924 But, that construct is not a cast-expression -- it is a
7925 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7926 is legal; if the compound-literal were a cast-expression,
7927 you'd need an extra set of parentheses.) But, if we parse
7928 the type-id, and it happens to be a class-specifier, then we
7929 will commit to the parse at that point, because we cannot
7930 undo the action that is done when creating a new class. So,
7931 then we cannot back up and do a postfix-expression.
7933 Another tricky case is the following (c++/29234):
7935 struct S { void operator () (); };
7937 void foo ()
7939 ( S()() );
7942 As a type-id we parse the parenthesized S()() as a function
7943 returning a function, groktypename complains and we cannot
7944 back up in this case either.
7946 Therefore, we scan ahead to the closing `)', and check to see
7947 if the tokens after the `)' can start a cast-expression. Otherwise
7948 we are dealing with an unary-expression, a postfix-expression
7949 or something else.
7951 Yet another tricky case, in C++11, is the following (c++/54891):
7953 (void)[]{};
7955 The issue is that usually, besides the case of lambda-expressions,
7956 the parenthesized type-id cannot be followed by '[', and, eg, we
7957 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7958 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7959 we don't commit, we try a cast-expression, then an unary-expression.
7961 Save tokens so that we can put them back. */
7962 cp_lexer_save_tokens (parser->lexer);
7964 /* We may be looking at a cast-expression. */
7965 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7966 /*consume_paren=*/true))
7967 cast_expression
7968 = cp_parser_tokens_start_cast_expression (parser);
7970 /* Roll back the tokens we skipped. */
7971 cp_lexer_rollback_tokens (parser->lexer);
7972 /* If we aren't looking at a cast-expression, simulate an error so
7973 that the call to cp_parser_error_occurred below returns true. */
7974 if (!cast_expression)
7975 cp_parser_simulate_error (parser);
7976 else
7978 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7979 parser->in_type_id_in_expr_p = true;
7980 /* Look for the type-id. */
7981 type = cp_parser_type_id (parser);
7982 /* Look for the closing `)'. */
7983 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7984 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7987 /* Restore the saved message. */
7988 parser->type_definition_forbidden_message = saved_message;
7990 /* At this point this can only be either a cast or a
7991 parenthesized ctor such as `(T ())' that looks like a cast to
7992 function returning T. */
7993 if (!cp_parser_error_occurred (parser))
7995 /* Only commit if the cast-expression doesn't start with
7996 '++', '--', or '[' in C++11. */
7997 if (cast_expression > 0)
7998 cp_parser_commit_to_topmost_tentative_parse (parser);
8000 expr = cp_parser_cast_expression (parser,
8001 /*address_p=*/false,
8002 /*cast_p=*/true,
8003 /*decltype_p=*/false,
8004 pidk);
8006 if (cp_parser_parse_definitely (parser))
8008 /* Warn about old-style casts, if so requested. */
8009 if (warn_old_style_cast
8010 && !in_system_header_at (input_location)
8011 && !VOID_TYPE_P (type)
8012 && current_lang_name != lang_name_c)
8013 warning (OPT_Wold_style_cast, "use of old-style cast");
8015 /* Only type conversions to integral or enumeration types
8016 can be used in constant-expressions. */
8017 if (!cast_valid_in_integral_constant_expression_p (type)
8018 && cp_parser_non_integral_constant_expression (parser,
8019 NIC_CAST))
8020 return error_mark_node;
8022 /* Perform the cast. */
8023 expr = build_c_cast (input_location, type, expr);
8024 return expr;
8027 else
8028 cp_parser_abort_tentative_parse (parser);
8031 /* If we get here, then it's not a cast, so it must be a
8032 unary-expression. */
8033 return cp_parser_unary_expression (parser, pidk, address_p,
8034 cast_p, decltype_p);
8037 /* Parse a binary expression of the general form:
8039 pm-expression:
8040 cast-expression
8041 pm-expression .* cast-expression
8042 pm-expression ->* cast-expression
8044 multiplicative-expression:
8045 pm-expression
8046 multiplicative-expression * pm-expression
8047 multiplicative-expression / pm-expression
8048 multiplicative-expression % pm-expression
8050 additive-expression:
8051 multiplicative-expression
8052 additive-expression + multiplicative-expression
8053 additive-expression - multiplicative-expression
8055 shift-expression:
8056 additive-expression
8057 shift-expression << additive-expression
8058 shift-expression >> additive-expression
8060 relational-expression:
8061 shift-expression
8062 relational-expression < shift-expression
8063 relational-expression > shift-expression
8064 relational-expression <= shift-expression
8065 relational-expression >= shift-expression
8067 GNU Extension:
8069 relational-expression:
8070 relational-expression <? shift-expression
8071 relational-expression >? shift-expression
8073 equality-expression:
8074 relational-expression
8075 equality-expression == relational-expression
8076 equality-expression != relational-expression
8078 and-expression:
8079 equality-expression
8080 and-expression & equality-expression
8082 exclusive-or-expression:
8083 and-expression
8084 exclusive-or-expression ^ and-expression
8086 inclusive-or-expression:
8087 exclusive-or-expression
8088 inclusive-or-expression | exclusive-or-expression
8090 logical-and-expression:
8091 inclusive-or-expression
8092 logical-and-expression && inclusive-or-expression
8094 logical-or-expression:
8095 logical-and-expression
8096 logical-or-expression || logical-and-expression
8098 All these are implemented with a single function like:
8100 binary-expression:
8101 simple-cast-expression
8102 binary-expression <token> binary-expression
8104 CAST_P is true if this expression is the target of a cast.
8106 The binops_by_token map is used to get the tree codes for each <token> type.
8107 binary-expressions are associated according to a precedence table. */
8109 #define TOKEN_PRECEDENCE(token) \
8110 (((token->type == CPP_GREATER \
8111 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8112 && !parser->greater_than_is_operator_p) \
8113 ? PREC_NOT_OPERATOR \
8114 : binops_by_token[token->type].prec)
8116 static tree
8117 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8118 bool no_toplevel_fold_p,
8119 bool decltype_p,
8120 enum cp_parser_prec prec,
8121 cp_id_kind * pidk)
8123 cp_parser_expression_stack stack;
8124 cp_parser_expression_stack_entry *sp = &stack[0];
8125 cp_parser_expression_stack_entry current;
8126 tree rhs;
8127 cp_token *token;
8128 enum tree_code rhs_type;
8129 enum cp_parser_prec new_prec, lookahead_prec;
8130 tree overload;
8132 /* Parse the first expression. */
8133 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8134 ? TRUTH_NOT_EXPR : ERROR_MARK);
8135 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8136 cast_p, decltype_p, pidk);
8137 current.prec = prec;
8139 if (cp_parser_error_occurred (parser))
8140 return error_mark_node;
8142 for (;;)
8144 /* Get an operator token. */
8145 token = cp_lexer_peek_token (parser->lexer);
8147 if (warn_cxx0x_compat
8148 && token->type == CPP_RSHIFT
8149 && !parser->greater_than_is_operator_p)
8151 if (warning_at (token->location, OPT_Wc__0x_compat,
8152 "%<>>%> operator is treated"
8153 " as two right angle brackets in C++11"))
8154 inform (token->location,
8155 "suggest parentheses around %<>>%> expression");
8158 new_prec = TOKEN_PRECEDENCE (token);
8160 /* Popping an entry off the stack means we completed a subexpression:
8161 - either we found a token which is not an operator (`>' where it is not
8162 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8163 will happen repeatedly;
8164 - or, we found an operator which has lower priority. This is the case
8165 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8166 parsing `3 * 4'. */
8167 if (new_prec <= current.prec)
8169 if (sp == stack)
8170 break;
8171 else
8172 goto pop;
8175 get_rhs:
8176 current.tree_type = binops_by_token[token->type].tree_type;
8177 current.loc = token->location;
8179 /* We used the operator token. */
8180 cp_lexer_consume_token (parser->lexer);
8182 /* For "false && x" or "true || x", x will never be executed;
8183 disable warnings while evaluating it. */
8184 if (current.tree_type == TRUTH_ANDIF_EXPR)
8185 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8186 else if (current.tree_type == TRUTH_ORIF_EXPR)
8187 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8189 /* Extract another operand. It may be the RHS of this expression
8190 or the LHS of a new, higher priority expression. */
8191 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8192 ? TRUTH_NOT_EXPR : ERROR_MARK);
8193 rhs = cp_parser_simple_cast_expression (parser);
8195 /* Get another operator token. Look up its precedence to avoid
8196 building a useless (immediately popped) stack entry for common
8197 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8198 token = cp_lexer_peek_token (parser->lexer);
8199 lookahead_prec = TOKEN_PRECEDENCE (token);
8200 if (lookahead_prec > new_prec)
8202 /* ... and prepare to parse the RHS of the new, higher priority
8203 expression. Since precedence levels on the stack are
8204 monotonically increasing, we do not have to care about
8205 stack overflows. */
8206 *sp = current;
8207 ++sp;
8208 current.lhs = rhs;
8209 current.lhs_type = rhs_type;
8210 current.prec = new_prec;
8211 new_prec = lookahead_prec;
8212 goto get_rhs;
8214 pop:
8215 lookahead_prec = new_prec;
8216 /* If the stack is not empty, we have parsed into LHS the right side
8217 (`4' in the example above) of an expression we had suspended.
8218 We can use the information on the stack to recover the LHS (`3')
8219 from the stack together with the tree code (`MULT_EXPR'), and
8220 the precedence of the higher level subexpression
8221 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8222 which will be used to actually build the additive expression. */
8223 rhs = current.lhs;
8224 rhs_type = current.lhs_type;
8225 --sp;
8226 current = *sp;
8229 /* Undo the disabling of warnings done above. */
8230 if (current.tree_type == TRUTH_ANDIF_EXPR)
8231 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8232 else if (current.tree_type == TRUTH_ORIF_EXPR)
8233 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8235 if (warn_logical_not_paren
8236 && current.lhs_type == TRUTH_NOT_EXPR)
8237 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8239 overload = NULL;
8240 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8241 ERROR_MARK for everything that is not a binary expression.
8242 This makes warn_about_parentheses miss some warnings that
8243 involve unary operators. For unary expressions we should
8244 pass the correct tree_code unless the unary expression was
8245 surrounded by parentheses.
8247 if (no_toplevel_fold_p
8248 && lookahead_prec <= current.prec
8249 && sp == stack)
8250 current.lhs = build2 (current.tree_type,
8251 TREE_CODE_CLASS (current.tree_type)
8252 == tcc_comparison
8253 ? boolean_type_node : TREE_TYPE (current.lhs),
8254 current.lhs, rhs);
8255 else
8256 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8257 current.lhs, current.lhs_type,
8258 rhs, rhs_type, &overload,
8259 complain_flags (decltype_p));
8260 current.lhs_type = current.tree_type;
8261 if (EXPR_P (current.lhs))
8262 SET_EXPR_LOCATION (current.lhs, current.loc);
8264 /* If the binary operator required the use of an overloaded operator,
8265 then this expression cannot be an integral constant-expression.
8266 An overloaded operator can be used even if both operands are
8267 otherwise permissible in an integral constant-expression if at
8268 least one of the operands is of enumeration type. */
8270 if (overload
8271 && cp_parser_non_integral_constant_expression (parser,
8272 NIC_OVERLOADED))
8273 return error_mark_node;
8276 return current.lhs;
8279 static tree
8280 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8281 bool no_toplevel_fold_p,
8282 enum cp_parser_prec prec,
8283 cp_id_kind * pidk)
8285 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8286 /*decltype*/false, prec, pidk);
8289 /* Parse the `? expression : assignment-expression' part of a
8290 conditional-expression. The LOGICAL_OR_EXPR is the
8291 logical-or-expression that started the conditional-expression.
8292 Returns a representation of the entire conditional-expression.
8294 This routine is used by cp_parser_assignment_expression.
8296 ? expression : assignment-expression
8298 GNU Extensions:
8300 ? : assignment-expression */
8302 static tree
8303 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8305 tree expr;
8306 tree assignment_expr;
8307 struct cp_token *token;
8308 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8310 /* Consume the `?' token. */
8311 cp_lexer_consume_token (parser->lexer);
8312 token = cp_lexer_peek_token (parser->lexer);
8313 if (cp_parser_allow_gnu_extensions_p (parser)
8314 && token->type == CPP_COLON)
8316 pedwarn (token->location, OPT_Wpedantic,
8317 "ISO C++ does not allow ?: with omitted middle operand");
8318 /* Implicit true clause. */
8319 expr = NULL_TREE;
8320 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8321 warn_for_omitted_condop (token->location, logical_or_expr);
8323 else
8325 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8326 parser->colon_corrects_to_scope_p = false;
8327 /* Parse the expression. */
8328 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8329 expr = cp_parser_expression (parser);
8330 c_inhibit_evaluation_warnings +=
8331 ((logical_or_expr == truthvalue_true_node)
8332 - (logical_or_expr == truthvalue_false_node));
8333 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8336 /* The next token should be a `:'. */
8337 cp_parser_require (parser, CPP_COLON, RT_COLON);
8338 /* Parse the assignment-expression. */
8339 assignment_expr = cp_parser_assignment_expression (parser);
8340 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8342 /* Build the conditional-expression. */
8343 return build_x_conditional_expr (loc, logical_or_expr,
8344 expr,
8345 assignment_expr,
8346 tf_warning_or_error);
8349 /* Parse an assignment-expression.
8351 assignment-expression:
8352 conditional-expression
8353 logical-or-expression assignment-operator assignment_expression
8354 throw-expression
8356 CAST_P is true if this expression is the target of a cast.
8357 DECLTYPE_P is true if this expression is the operand of decltype.
8359 Returns a representation for the expression. */
8361 static tree
8362 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8363 bool cast_p, bool decltype_p)
8365 tree expr;
8367 /* If the next token is the `throw' keyword, then we're looking at
8368 a throw-expression. */
8369 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8370 expr = cp_parser_throw_expression (parser);
8371 /* Otherwise, it must be that we are looking at a
8372 logical-or-expression. */
8373 else
8375 /* Parse the binary expressions (logical-or-expression). */
8376 expr = cp_parser_binary_expression (parser, cast_p, false,
8377 decltype_p,
8378 PREC_NOT_OPERATOR, pidk);
8379 /* If the next token is a `?' then we're actually looking at a
8380 conditional-expression. */
8381 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8382 return cp_parser_question_colon_clause (parser, expr);
8383 else
8385 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8387 /* If it's an assignment-operator, we're using the second
8388 production. */
8389 enum tree_code assignment_operator
8390 = cp_parser_assignment_operator_opt (parser);
8391 if (assignment_operator != ERROR_MARK)
8393 bool non_constant_p;
8394 location_t saved_input_location;
8396 /* Parse the right-hand side of the assignment. */
8397 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8399 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8400 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8402 /* An assignment may not appear in a
8403 constant-expression. */
8404 if (cp_parser_non_integral_constant_expression (parser,
8405 NIC_ASSIGNMENT))
8406 return error_mark_node;
8407 /* Build the assignment expression. Its default
8408 location is the location of the '=' token. */
8409 saved_input_location = input_location;
8410 input_location = loc;
8411 expr = build_x_modify_expr (loc, expr,
8412 assignment_operator,
8413 rhs,
8414 complain_flags (decltype_p));
8415 input_location = saved_input_location;
8420 return expr;
8423 /* Parse an (optional) assignment-operator.
8425 assignment-operator: one of
8426 = *= /= %= += -= >>= <<= &= ^= |=
8428 GNU Extension:
8430 assignment-operator: one of
8431 <?= >?=
8433 If the next token is an assignment operator, the corresponding tree
8434 code is returned, and the token is consumed. For example, for
8435 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8436 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8437 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8438 operator, ERROR_MARK is returned. */
8440 static enum tree_code
8441 cp_parser_assignment_operator_opt (cp_parser* parser)
8443 enum tree_code op;
8444 cp_token *token;
8446 /* Peek at the next token. */
8447 token = cp_lexer_peek_token (parser->lexer);
8449 switch (token->type)
8451 case CPP_EQ:
8452 op = NOP_EXPR;
8453 break;
8455 case CPP_MULT_EQ:
8456 op = MULT_EXPR;
8457 break;
8459 case CPP_DIV_EQ:
8460 op = TRUNC_DIV_EXPR;
8461 break;
8463 case CPP_MOD_EQ:
8464 op = TRUNC_MOD_EXPR;
8465 break;
8467 case CPP_PLUS_EQ:
8468 op = PLUS_EXPR;
8469 break;
8471 case CPP_MINUS_EQ:
8472 op = MINUS_EXPR;
8473 break;
8475 case CPP_RSHIFT_EQ:
8476 op = RSHIFT_EXPR;
8477 break;
8479 case CPP_LSHIFT_EQ:
8480 op = LSHIFT_EXPR;
8481 break;
8483 case CPP_AND_EQ:
8484 op = BIT_AND_EXPR;
8485 break;
8487 case CPP_XOR_EQ:
8488 op = BIT_XOR_EXPR;
8489 break;
8491 case CPP_OR_EQ:
8492 op = BIT_IOR_EXPR;
8493 break;
8495 default:
8496 /* Nothing else is an assignment operator. */
8497 op = ERROR_MARK;
8500 /* If it was an assignment operator, consume it. */
8501 if (op != ERROR_MARK)
8502 cp_lexer_consume_token (parser->lexer);
8504 return op;
8507 /* Parse an expression.
8509 expression:
8510 assignment-expression
8511 expression , assignment-expression
8513 CAST_P is true if this expression is the target of a cast.
8514 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8515 except possibly parenthesized or on the RHS of a comma (N3276).
8517 Returns a representation of the expression. */
8519 static tree
8520 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8521 bool cast_p, bool decltype_p)
8523 tree expression = NULL_TREE;
8524 location_t loc = UNKNOWN_LOCATION;
8526 while (true)
8528 tree assignment_expression;
8530 /* Parse the next assignment-expression. */
8531 assignment_expression
8532 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8534 /* We don't create a temporary for a call that is the immediate operand
8535 of decltype or on the RHS of a comma. But when we see a comma, we
8536 need to create a temporary for a call on the LHS. */
8537 if (decltype_p && !processing_template_decl
8538 && TREE_CODE (assignment_expression) == CALL_EXPR
8539 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8540 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8541 assignment_expression
8542 = build_cplus_new (TREE_TYPE (assignment_expression),
8543 assignment_expression, tf_warning_or_error);
8545 /* If this is the first assignment-expression, we can just
8546 save it away. */
8547 if (!expression)
8548 expression = assignment_expression;
8549 else
8550 expression = build_x_compound_expr (loc, expression,
8551 assignment_expression,
8552 complain_flags (decltype_p));
8553 /* If the next token is not a comma, then we are done with the
8554 expression. */
8555 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8556 break;
8557 /* Consume the `,'. */
8558 loc = cp_lexer_peek_token (parser->lexer)->location;
8559 cp_lexer_consume_token (parser->lexer);
8560 /* A comma operator cannot appear in a constant-expression. */
8561 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8562 expression = error_mark_node;
8565 return expression;
8568 /* Parse a constant-expression.
8570 constant-expression:
8571 conditional-expression
8573 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8574 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8575 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8576 is false, NON_CONSTANT_P should be NULL. */
8578 static tree
8579 cp_parser_constant_expression (cp_parser* parser,
8580 bool allow_non_constant_p,
8581 bool *non_constant_p)
8583 bool saved_integral_constant_expression_p;
8584 bool saved_allow_non_integral_constant_expression_p;
8585 bool saved_non_integral_constant_expression_p;
8586 tree expression;
8588 /* It might seem that we could simply parse the
8589 conditional-expression, and then check to see if it were
8590 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8591 one that the compiler can figure out is constant, possibly after
8592 doing some simplifications or optimizations. The standard has a
8593 precise definition of constant-expression, and we must honor
8594 that, even though it is somewhat more restrictive.
8596 For example:
8598 int i[(2, 3)];
8600 is not a legal declaration, because `(2, 3)' is not a
8601 constant-expression. The `,' operator is forbidden in a
8602 constant-expression. However, GCC's constant-folding machinery
8603 will fold this operation to an INTEGER_CST for `3'. */
8605 /* Save the old settings. */
8606 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8607 saved_allow_non_integral_constant_expression_p
8608 = parser->allow_non_integral_constant_expression_p;
8609 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8610 /* We are now parsing a constant-expression. */
8611 parser->integral_constant_expression_p = true;
8612 parser->allow_non_integral_constant_expression_p
8613 = (allow_non_constant_p || cxx_dialect >= cxx11);
8614 parser->non_integral_constant_expression_p = false;
8615 /* Although the grammar says "conditional-expression", we parse an
8616 "assignment-expression", which also permits "throw-expression"
8617 and the use of assignment operators. In the case that
8618 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8619 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8620 actually essential that we look for an assignment-expression.
8621 For example, cp_parser_initializer_clauses uses this function to
8622 determine whether a particular assignment-expression is in fact
8623 constant. */
8624 expression = cp_parser_assignment_expression (parser);
8625 /* Restore the old settings. */
8626 parser->integral_constant_expression_p
8627 = saved_integral_constant_expression_p;
8628 parser->allow_non_integral_constant_expression_p
8629 = saved_allow_non_integral_constant_expression_p;
8630 if (cxx_dialect >= cxx11)
8632 /* Require an rvalue constant expression here; that's what our
8633 callers expect. Reference constant expressions are handled
8634 separately in e.g. cp_parser_template_argument. */
8635 bool is_const = potential_rvalue_constant_expression (expression);
8636 parser->non_integral_constant_expression_p = !is_const;
8637 if (!is_const && !allow_non_constant_p)
8638 require_potential_rvalue_constant_expression (expression);
8640 if (allow_non_constant_p)
8641 *non_constant_p = parser->non_integral_constant_expression_p;
8642 parser->non_integral_constant_expression_p
8643 = saved_non_integral_constant_expression_p;
8645 return expression;
8648 /* Parse __builtin_offsetof.
8650 offsetof-expression:
8651 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8653 offsetof-member-designator:
8654 id-expression
8655 | offsetof-member-designator "." id-expression
8656 | offsetof-member-designator "[" expression "]"
8657 | offsetof-member-designator "->" id-expression */
8659 static tree
8660 cp_parser_builtin_offsetof (cp_parser *parser)
8662 int save_ice_p, save_non_ice_p;
8663 tree type, expr;
8664 cp_id_kind dummy;
8665 cp_token *token;
8667 /* We're about to accept non-integral-constant things, but will
8668 definitely yield an integral constant expression. Save and
8669 restore these values around our local parsing. */
8670 save_ice_p = parser->integral_constant_expression_p;
8671 save_non_ice_p = parser->non_integral_constant_expression_p;
8673 /* Consume the "__builtin_offsetof" token. */
8674 cp_lexer_consume_token (parser->lexer);
8675 /* Consume the opening `('. */
8676 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8677 /* Parse the type-id. */
8678 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8679 type = cp_parser_type_id (parser);
8680 /* Look for the `,'. */
8681 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8682 token = cp_lexer_peek_token (parser->lexer);
8684 /* Build the (type *)null that begins the traditional offsetof macro. */
8685 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8686 tf_warning_or_error);
8688 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8689 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8690 true, &dummy, token->location);
8691 while (true)
8693 token = cp_lexer_peek_token (parser->lexer);
8694 switch (token->type)
8696 case CPP_OPEN_SQUARE:
8697 /* offsetof-member-designator "[" expression "]" */
8698 expr = cp_parser_postfix_open_square_expression (parser, expr,
8699 true, false);
8700 break;
8702 case CPP_DEREF:
8703 /* offsetof-member-designator "->" identifier */
8704 expr = grok_array_decl (token->location, expr,
8705 integer_zero_node, false);
8706 /* FALLTHRU */
8708 case CPP_DOT:
8709 /* offsetof-member-designator "." identifier */
8710 cp_lexer_consume_token (parser->lexer);
8711 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8712 expr, true, &dummy,
8713 token->location);
8714 break;
8716 case CPP_CLOSE_PAREN:
8717 /* Consume the ")" token. */
8718 cp_lexer_consume_token (parser->lexer);
8719 goto success;
8721 default:
8722 /* Error. We know the following require will fail, but
8723 that gives the proper error message. */
8724 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8725 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8726 expr = error_mark_node;
8727 goto failure;
8731 success:
8732 expr = finish_offsetof (expr, loc);
8734 failure:
8735 parser->integral_constant_expression_p = save_ice_p;
8736 parser->non_integral_constant_expression_p = save_non_ice_p;
8738 return expr;
8741 /* Parse a trait expression.
8743 Returns a representation of the expression, the underlying type
8744 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8746 static tree
8747 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8749 cp_trait_kind kind;
8750 tree type1, type2 = NULL_TREE;
8751 bool binary = false;
8752 bool variadic = false;
8754 switch (keyword)
8756 case RID_HAS_NOTHROW_ASSIGN:
8757 kind = CPTK_HAS_NOTHROW_ASSIGN;
8758 break;
8759 case RID_HAS_NOTHROW_CONSTRUCTOR:
8760 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8761 break;
8762 case RID_HAS_NOTHROW_COPY:
8763 kind = CPTK_HAS_NOTHROW_COPY;
8764 break;
8765 case RID_HAS_TRIVIAL_ASSIGN:
8766 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8767 break;
8768 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8769 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8770 break;
8771 case RID_HAS_TRIVIAL_COPY:
8772 kind = CPTK_HAS_TRIVIAL_COPY;
8773 break;
8774 case RID_HAS_TRIVIAL_DESTRUCTOR:
8775 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8776 break;
8777 case RID_HAS_VIRTUAL_DESTRUCTOR:
8778 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8779 break;
8780 case RID_IS_ABSTRACT:
8781 kind = CPTK_IS_ABSTRACT;
8782 break;
8783 case RID_IS_BASE_OF:
8784 kind = CPTK_IS_BASE_OF;
8785 binary = true;
8786 break;
8787 case RID_IS_CLASS:
8788 kind = CPTK_IS_CLASS;
8789 break;
8790 case RID_IS_EMPTY:
8791 kind = CPTK_IS_EMPTY;
8792 break;
8793 case RID_IS_ENUM:
8794 kind = CPTK_IS_ENUM;
8795 break;
8796 case RID_IS_FINAL:
8797 kind = CPTK_IS_FINAL;
8798 break;
8799 case RID_IS_LITERAL_TYPE:
8800 kind = CPTK_IS_LITERAL_TYPE;
8801 break;
8802 case RID_IS_POD:
8803 kind = CPTK_IS_POD;
8804 break;
8805 case RID_IS_POLYMORPHIC:
8806 kind = CPTK_IS_POLYMORPHIC;
8807 break;
8808 case RID_IS_STD_LAYOUT:
8809 kind = CPTK_IS_STD_LAYOUT;
8810 break;
8811 case RID_IS_TRIVIAL:
8812 kind = CPTK_IS_TRIVIAL;
8813 break;
8814 case RID_IS_TRIVIALLY_ASSIGNABLE:
8815 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8816 binary = true;
8817 break;
8818 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8819 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8820 variadic = true;
8821 break;
8822 case RID_IS_TRIVIALLY_COPYABLE:
8823 kind = CPTK_IS_TRIVIALLY_COPYABLE;
8824 break;
8825 case RID_IS_UNION:
8826 kind = CPTK_IS_UNION;
8827 break;
8828 case RID_UNDERLYING_TYPE:
8829 kind = CPTK_UNDERLYING_TYPE;
8830 break;
8831 case RID_BASES:
8832 kind = CPTK_BASES;
8833 break;
8834 case RID_DIRECT_BASES:
8835 kind = CPTK_DIRECT_BASES;
8836 break;
8837 default:
8838 gcc_unreachable ();
8841 /* Consume the token. */
8842 cp_lexer_consume_token (parser->lexer);
8844 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8846 type1 = cp_parser_type_id (parser);
8848 if (type1 == error_mark_node)
8849 return error_mark_node;
8851 if (binary)
8853 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8855 type2 = cp_parser_type_id (parser);
8857 if (type2 == error_mark_node)
8858 return error_mark_node;
8860 else if (variadic)
8862 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8864 cp_lexer_consume_token (parser->lexer);
8865 tree elt = cp_parser_type_id (parser);
8866 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8868 cp_lexer_consume_token (parser->lexer);
8869 elt = make_pack_expansion (elt);
8871 if (elt == error_mark_node)
8872 return error_mark_node;
8873 type2 = tree_cons (NULL_TREE, elt, type2);
8877 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8879 /* Complete the trait expression, which may mean either processing
8880 the trait expr now or saving it for template instantiation. */
8881 switch(kind)
8883 case CPTK_UNDERLYING_TYPE:
8884 return finish_underlying_type (type1);
8885 case CPTK_BASES:
8886 return finish_bases (type1, false);
8887 case CPTK_DIRECT_BASES:
8888 return finish_bases (type1, true);
8889 default:
8890 return finish_trait_expr (kind, type1, type2);
8894 /* Lambdas that appear in variable initializer or default argument scope
8895 get that in their mangling, so we need to record it. We might as well
8896 use the count for function and namespace scopes as well. */
8897 static GTY(()) tree lambda_scope;
8898 static GTY(()) int lambda_count;
8899 typedef struct GTY(()) tree_int
8901 tree t;
8902 int i;
8903 } tree_int;
8904 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8906 static void
8907 start_lambda_scope (tree decl)
8909 tree_int ti;
8910 gcc_assert (decl);
8911 /* Once we're inside a function, we ignore other scopes and just push
8912 the function again so that popping works properly. */
8913 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8914 decl = current_function_decl;
8915 ti.t = lambda_scope;
8916 ti.i = lambda_count;
8917 vec_safe_push (lambda_scope_stack, ti);
8918 if (lambda_scope != decl)
8920 /* Don't reset the count if we're still in the same function. */
8921 lambda_scope = decl;
8922 lambda_count = 0;
8926 static void
8927 record_lambda_scope (tree lambda)
8929 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8930 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8933 static void
8934 finish_lambda_scope (void)
8936 tree_int *p = &lambda_scope_stack->last ();
8937 if (lambda_scope != p->t)
8939 lambda_scope = p->t;
8940 lambda_count = p->i;
8942 lambda_scope_stack->pop ();
8945 /* Parse a lambda expression.
8947 lambda-expression:
8948 lambda-introducer lambda-declarator [opt] compound-statement
8950 Returns a representation of the expression. */
8952 static tree
8953 cp_parser_lambda_expression (cp_parser* parser)
8955 tree lambda_expr = build_lambda_expr ();
8956 tree type;
8957 bool ok = true;
8958 cp_token *token = cp_lexer_peek_token (parser->lexer);
8959 cp_token_position start = 0;
8961 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8963 if (cp_unevaluated_operand)
8965 if (!token->error_reported)
8967 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8968 "lambda-expression in unevaluated context");
8969 token->error_reported = true;
8971 ok = false;
8973 else if (parser->in_template_argument_list_p)
8975 if (!token->error_reported)
8977 error_at (token->location, "lambda-expression in template-argument");
8978 token->error_reported = true;
8980 ok = false;
8983 /* We may be in the middle of deferred access check. Disable
8984 it now. */
8985 push_deferring_access_checks (dk_no_deferred);
8987 cp_parser_lambda_introducer (parser, lambda_expr);
8989 type = begin_lambda_type (lambda_expr);
8990 if (type == error_mark_node)
8991 return error_mark_node;
8993 record_lambda_scope (lambda_expr);
8995 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8996 determine_visibility (TYPE_NAME (type));
8998 /* Now that we've started the type, add the capture fields for any
8999 explicit captures. */
9000 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9003 /* Inside the class, surrounding template-parameter-lists do not apply. */
9004 unsigned int saved_num_template_parameter_lists
9005 = parser->num_template_parameter_lists;
9006 unsigned char in_statement = parser->in_statement;
9007 bool in_switch_statement_p = parser->in_switch_statement_p;
9008 bool fully_implicit_function_template_p
9009 = parser->fully_implicit_function_template_p;
9010 tree implicit_template_parms = parser->implicit_template_parms;
9011 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9012 bool auto_is_implicit_function_template_parm_p
9013 = parser->auto_is_implicit_function_template_parm_p;
9015 parser->num_template_parameter_lists = 0;
9016 parser->in_statement = 0;
9017 parser->in_switch_statement_p = false;
9018 parser->fully_implicit_function_template_p = false;
9019 parser->implicit_template_parms = 0;
9020 parser->implicit_template_scope = 0;
9021 parser->auto_is_implicit_function_template_parm_p = false;
9023 /* By virtue of defining a local class, a lambda expression has access to
9024 the private variables of enclosing classes. */
9026 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9028 if (ok)
9030 if (!cp_parser_error_occurred (parser)
9031 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9032 && cp_parser_start_tentative_firewall (parser))
9033 start = token;
9034 cp_parser_lambda_body (parser, lambda_expr);
9036 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9038 if (cp_parser_skip_to_closing_brace (parser))
9039 cp_lexer_consume_token (parser->lexer);
9042 /* The capture list was built up in reverse order; fix that now. */
9043 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9044 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9046 if (ok)
9047 maybe_add_lambda_conv_op (type);
9049 type = finish_struct (type, /*attributes=*/NULL_TREE);
9051 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9052 parser->in_statement = in_statement;
9053 parser->in_switch_statement_p = in_switch_statement_p;
9054 parser->fully_implicit_function_template_p
9055 = fully_implicit_function_template_p;
9056 parser->implicit_template_parms = implicit_template_parms;
9057 parser->implicit_template_scope = implicit_template_scope;
9058 parser->auto_is_implicit_function_template_parm_p
9059 = auto_is_implicit_function_template_parm_p;
9062 pop_deferring_access_checks ();
9064 /* This field is only used during parsing of the lambda. */
9065 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9067 /* This lambda shouldn't have any proxies left at this point. */
9068 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9069 /* And now that we're done, push proxies for an enclosing lambda. */
9070 insert_pending_capture_proxies ();
9072 if (ok)
9073 lambda_expr = build_lambda_object (lambda_expr);
9074 else
9075 lambda_expr = error_mark_node;
9077 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9079 return lambda_expr;
9082 /* Parse the beginning of a lambda expression.
9084 lambda-introducer:
9085 [ lambda-capture [opt] ]
9087 LAMBDA_EXPR is the current representation of the lambda expression. */
9089 static void
9090 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9092 /* Need commas after the first capture. */
9093 bool first = true;
9095 /* Eat the leading `['. */
9096 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9098 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9099 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9100 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9101 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9102 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9103 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9105 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9107 cp_lexer_consume_token (parser->lexer);
9108 first = false;
9111 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9113 cp_token* capture_token;
9114 tree capture_id;
9115 tree capture_init_expr;
9116 cp_id_kind idk = CP_ID_KIND_NONE;
9117 bool explicit_init_p = false;
9119 enum capture_kind_type
9121 BY_COPY,
9122 BY_REFERENCE
9124 enum capture_kind_type capture_kind = BY_COPY;
9126 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9128 error ("expected end of capture-list");
9129 return;
9132 if (first)
9133 first = false;
9134 else
9135 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9137 /* Possibly capture `this'. */
9138 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9140 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9141 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9142 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9143 "with by-copy capture default");
9144 cp_lexer_consume_token (parser->lexer);
9145 add_capture (lambda_expr,
9146 /*id=*/this_identifier,
9147 /*initializer=*/finish_this_expr(),
9148 /*by_reference_p=*/false,
9149 explicit_init_p);
9150 continue;
9153 /* Remember whether we want to capture as a reference or not. */
9154 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9156 capture_kind = BY_REFERENCE;
9157 cp_lexer_consume_token (parser->lexer);
9160 /* Get the identifier. */
9161 capture_token = cp_lexer_peek_token (parser->lexer);
9162 capture_id = cp_parser_identifier (parser);
9164 if (capture_id == error_mark_node)
9165 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9166 delimiters, but I modified this to stop on unnested ']' as well. It
9167 was already changed to stop on unnested '}', so the
9168 "closing_parenthesis" name is no more misleading with my change. */
9170 cp_parser_skip_to_closing_parenthesis (parser,
9171 /*recovering=*/true,
9172 /*or_comma=*/true,
9173 /*consume_paren=*/true);
9174 break;
9177 /* Find the initializer for this capture. */
9178 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9179 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9180 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9182 bool direct, non_constant;
9183 /* An explicit initializer exists. */
9184 if (cxx_dialect < cxx14)
9185 pedwarn (input_location, 0,
9186 "lambda capture initializers "
9187 "only available with -std=c++14 or -std=gnu++14");
9188 capture_init_expr = cp_parser_initializer (parser, &direct,
9189 &non_constant);
9190 explicit_init_p = true;
9191 if (capture_init_expr == NULL_TREE)
9193 error ("empty initializer for lambda init-capture");
9194 capture_init_expr = error_mark_node;
9197 else
9199 const char* error_msg;
9201 /* Turn the identifier into an id-expression. */
9202 capture_init_expr
9203 = cp_parser_lookup_name_simple (parser, capture_id,
9204 capture_token->location);
9206 if (capture_init_expr == error_mark_node)
9208 unqualified_name_lookup_error (capture_id);
9209 continue;
9211 else if (DECL_P (capture_init_expr)
9212 && (!VAR_P (capture_init_expr)
9213 && TREE_CODE (capture_init_expr) != PARM_DECL))
9215 error_at (capture_token->location,
9216 "capture of non-variable %qD ",
9217 capture_init_expr);
9218 inform (0, "%q+#D declared here", capture_init_expr);
9219 continue;
9221 if (VAR_P (capture_init_expr)
9222 && decl_storage_duration (capture_init_expr) != dk_auto)
9224 if (pedwarn (capture_token->location, 0, "capture of variable "
9225 "%qD with non-automatic storage duration",
9226 capture_init_expr))
9227 inform (0, "%q+#D declared here", capture_init_expr);
9228 continue;
9231 capture_init_expr
9232 = finish_id_expression
9233 (capture_id,
9234 capture_init_expr,
9235 parser->scope,
9236 &idk,
9237 /*integral_constant_expression_p=*/false,
9238 /*allow_non_integral_constant_expression_p=*/false,
9239 /*non_integral_constant_expression_p=*/NULL,
9240 /*template_p=*/false,
9241 /*done=*/true,
9242 /*address_p=*/false,
9243 /*template_arg_p=*/false,
9244 &error_msg,
9245 capture_token->location);
9247 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9249 cp_lexer_consume_token (parser->lexer);
9250 capture_init_expr = make_pack_expansion (capture_init_expr);
9252 else
9253 check_for_bare_parameter_packs (capture_init_expr);
9256 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9257 && !explicit_init_p)
9259 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9260 && capture_kind == BY_COPY)
9261 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9262 "of %qD redundant with by-copy capture default",
9263 capture_id);
9264 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9265 && capture_kind == BY_REFERENCE)
9266 pedwarn (capture_token->location, 0, "explicit by-reference "
9267 "capture of %qD redundant with by-reference capture "
9268 "default", capture_id);
9271 add_capture (lambda_expr,
9272 capture_id,
9273 capture_init_expr,
9274 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9275 explicit_init_p);
9278 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9281 /* Parse the (optional) middle of a lambda expression.
9283 lambda-declarator:
9284 < template-parameter-list [opt] >
9285 ( parameter-declaration-clause [opt] )
9286 attribute-specifier [opt]
9287 mutable [opt]
9288 exception-specification [opt]
9289 lambda-return-type-clause [opt]
9291 LAMBDA_EXPR is the current representation of the lambda expression. */
9293 static bool
9294 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9296 /* 5.1.1.4 of the standard says:
9297 If a lambda-expression does not include a lambda-declarator, it is as if
9298 the lambda-declarator were ().
9299 This means an empty parameter list, no attributes, and no exception
9300 specification. */
9301 tree param_list = void_list_node;
9302 tree attributes = NULL_TREE;
9303 tree exception_spec = NULL_TREE;
9304 tree template_param_list = NULL_TREE;
9306 /* The template-parameter-list is optional, but must begin with
9307 an opening angle if present. */
9308 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9310 if (cxx_dialect < cxx14)
9311 pedwarn (parser->lexer->next_token->location, 0,
9312 "lambda templates are only available with "
9313 "-std=c++14 or -std=gnu++14");
9315 cp_lexer_consume_token (parser->lexer);
9317 template_param_list = cp_parser_template_parameter_list (parser);
9319 cp_parser_skip_to_end_of_template_parameter_list (parser);
9321 /* We just processed one more parameter list. */
9322 ++parser->num_template_parameter_lists;
9325 /* The parameter-declaration-clause is optional (unless
9326 template-parameter-list was given), but must begin with an
9327 opening parenthesis if present. */
9328 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9330 cp_lexer_consume_token (parser->lexer);
9332 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9334 /* Parse parameters. */
9335 param_list = cp_parser_parameter_declaration_clause (parser);
9337 /* Default arguments shall not be specified in the
9338 parameter-declaration-clause of a lambda-declarator. */
9339 for (tree t = param_list; t; t = TREE_CHAIN (t))
9340 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9341 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9342 "default argument specified for lambda parameter");
9344 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9346 attributes = cp_parser_attributes_opt (parser);
9348 /* Parse optional `mutable' keyword. */
9349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9351 cp_lexer_consume_token (parser->lexer);
9352 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9355 /* Parse optional exception specification. */
9356 exception_spec = cp_parser_exception_specification_opt (parser);
9358 /* Parse optional trailing return type. */
9359 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9361 cp_lexer_consume_token (parser->lexer);
9362 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9363 = cp_parser_trailing_type_id (parser);
9366 /* The function parameters must be in scope all the way until after the
9367 trailing-return-type in case of decltype. */
9368 pop_bindings_and_leave_scope ();
9370 else if (template_param_list != NULL_TREE) // generate diagnostic
9371 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9373 /* Create the function call operator.
9375 Messing with declarators like this is no uglier than building up the
9376 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9377 other code. */
9379 cp_decl_specifier_seq return_type_specs;
9380 cp_declarator* declarator;
9381 tree fco;
9382 int quals;
9383 void *p;
9385 clear_decl_specs (&return_type_specs);
9386 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9387 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9388 else
9389 /* Maybe we will deduce the return type later. */
9390 return_type_specs.type = make_auto ();
9392 p = obstack_alloc (&declarator_obstack, 0);
9394 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9395 sfk_none);
9397 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9398 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9399 declarator = make_call_declarator (declarator, param_list, quals,
9400 VIRT_SPEC_UNSPECIFIED,
9401 REF_QUAL_NONE,
9402 exception_spec,
9403 /*late_return_type=*/NULL_TREE);
9404 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9406 fco = grokmethod (&return_type_specs,
9407 declarator,
9408 attributes);
9409 if (fco != error_mark_node)
9411 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9412 DECL_ARTIFICIAL (fco) = 1;
9413 /* Give the object parameter a different name. */
9414 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9415 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9416 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9418 if (template_param_list)
9420 fco = finish_member_template_decl (fco);
9421 finish_template_decl (template_param_list);
9422 --parser->num_template_parameter_lists;
9424 else if (parser->fully_implicit_function_template_p)
9425 fco = finish_fully_implicit_template (parser, fco);
9427 finish_member_declaration (fco);
9429 obstack_free (&declarator_obstack, p);
9431 return (fco != error_mark_node);
9435 /* Parse the body of a lambda expression, which is simply
9437 compound-statement
9439 but which requires special handling.
9440 LAMBDA_EXPR is the current representation of the lambda expression. */
9442 static void
9443 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9445 bool nested = (current_function_decl != NULL_TREE);
9446 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9447 if (nested)
9448 push_function_context ();
9449 else
9450 /* Still increment function_depth so that we don't GC in the
9451 middle of an expression. */
9452 ++function_depth;
9453 /* Clear this in case we're in the middle of a default argument. */
9454 parser->local_variables_forbidden_p = false;
9456 /* Finish the function call operator
9457 - class_specifier
9458 + late_parsing_for_member
9459 + function_definition_after_declarator
9460 + ctor_initializer_opt_and_function_body */
9462 tree fco = lambda_function (lambda_expr);
9463 tree body;
9464 bool done = false;
9465 tree compound_stmt;
9466 tree cap;
9468 /* Let the front end know that we are going to be defining this
9469 function. */
9470 start_preparsed_function (fco,
9471 NULL_TREE,
9472 SF_PRE_PARSED | SF_INCLASS_INLINE);
9474 start_lambda_scope (fco);
9475 body = begin_function_body ();
9477 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9478 goto out;
9480 /* Push the proxies for any explicit captures. */
9481 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9482 cap = TREE_CHAIN (cap))
9483 build_capture_proxy (TREE_PURPOSE (cap));
9485 compound_stmt = begin_compound_stmt (0);
9487 /* 5.1.1.4 of the standard says:
9488 If a lambda-expression does not include a trailing-return-type, it
9489 is as if the trailing-return-type denotes the following type:
9490 * if the compound-statement is of the form
9491 { return attribute-specifier [opt] expression ; }
9492 the type of the returned expression after lvalue-to-rvalue
9493 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9494 (_conv.array_ 4.2), and function-to-pointer conversion
9495 (_conv.func_ 4.3);
9496 * otherwise, void. */
9498 /* In a lambda that has neither a lambda-return-type-clause
9499 nor a deducible form, errors should be reported for return statements
9500 in the body. Since we used void as the placeholder return type, parsing
9501 the body as usual will give such desired behavior. */
9502 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9503 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9504 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9506 tree expr = NULL_TREE;
9507 cp_id_kind idk = CP_ID_KIND_NONE;
9509 /* Parse tentatively in case there's more after the initial return
9510 statement. */
9511 cp_parser_parse_tentatively (parser);
9513 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9515 expr = cp_parser_expression (parser, &idk);
9517 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9518 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9520 if (cp_parser_parse_definitely (parser))
9522 if (!processing_template_decl)
9523 apply_deduced_return_type (fco, lambda_return_type (expr));
9525 /* Will get error here if type not deduced yet. */
9526 finish_return_stmt (expr);
9528 done = true;
9532 if (!done)
9534 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9535 cp_parser_label_declaration (parser);
9536 cp_parser_statement_seq_opt (parser, NULL_TREE);
9537 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9540 finish_compound_stmt (compound_stmt);
9542 out:
9543 finish_function_body (body);
9544 finish_lambda_scope ();
9546 /* Finish the function and generate code for it if necessary. */
9547 tree fn = finish_function (/*inline*/2);
9549 /* Only expand if the call op is not a template. */
9550 if (!DECL_TEMPLATE_INFO (fco))
9551 expand_or_defer_fn (fn);
9554 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9555 if (nested)
9556 pop_function_context();
9557 else
9558 --function_depth;
9561 /* Statements [gram.stmt.stmt] */
9563 /* Parse a statement.
9565 statement:
9566 labeled-statement
9567 expression-statement
9568 compound-statement
9569 selection-statement
9570 iteration-statement
9571 jump-statement
9572 declaration-statement
9573 try-block
9575 C++11:
9577 statement:
9578 labeled-statement
9579 attribute-specifier-seq (opt) expression-statement
9580 attribute-specifier-seq (opt) compound-statement
9581 attribute-specifier-seq (opt) selection-statement
9582 attribute-specifier-seq (opt) iteration-statement
9583 attribute-specifier-seq (opt) jump-statement
9584 declaration-statement
9585 attribute-specifier-seq (opt) try-block
9587 TM Extension:
9589 statement:
9590 atomic-statement
9592 IN_COMPOUND is true when the statement is nested inside a
9593 cp_parser_compound_statement; this matters for certain pragmas.
9595 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9596 is a (possibly labeled) if statement which is not enclosed in braces
9597 and has an else clause. This is used to implement -Wparentheses. */
9599 static void
9600 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9601 bool in_compound, bool *if_p)
9603 tree statement, std_attrs = NULL_TREE;
9604 cp_token *token;
9605 location_t statement_location, attrs_location;
9607 restart:
9608 if (if_p != NULL)
9609 *if_p = false;
9610 /* There is no statement yet. */
9611 statement = NULL_TREE;
9613 saved_token_sentinel saved_tokens (parser->lexer);
9614 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9615 if (c_dialect_objc ())
9616 /* In obj-c++, seeing '[[' might be the either the beginning of
9617 c++11 attributes, or a nested objc-message-expression. So
9618 let's parse the c++11 attributes tentatively. */
9619 cp_parser_parse_tentatively (parser);
9620 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9621 if (c_dialect_objc ())
9623 if (!cp_parser_parse_definitely (parser))
9624 std_attrs = NULL_TREE;
9627 /* Peek at the next token. */
9628 token = cp_lexer_peek_token (parser->lexer);
9629 /* Remember the location of the first token in the statement. */
9630 statement_location = token->location;
9631 /* If this is a keyword, then that will often determine what kind of
9632 statement we have. */
9633 if (token->type == CPP_KEYWORD)
9635 enum rid keyword = token->keyword;
9637 switch (keyword)
9639 case RID_CASE:
9640 case RID_DEFAULT:
9641 /* Looks like a labeled-statement with a case label.
9642 Parse the label, and then use tail recursion to parse
9643 the statement. */
9644 cp_parser_label_for_labeled_statement (parser, std_attrs);
9645 goto restart;
9647 case RID_IF:
9648 case RID_SWITCH:
9649 statement = cp_parser_selection_statement (parser, if_p);
9650 break;
9652 case RID_WHILE:
9653 case RID_DO:
9654 case RID_FOR:
9655 statement = cp_parser_iteration_statement (parser, false);
9656 break;
9658 case RID_CILK_FOR:
9659 if (!flag_cilkplus)
9661 error_at (cp_lexer_peek_token (parser->lexer)->location,
9662 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9663 cp_lexer_consume_token (parser->lexer);
9664 statement = error_mark_node;
9666 else
9667 statement = cp_parser_cilk_for (parser, integer_zero_node);
9668 break;
9670 case RID_BREAK:
9671 case RID_CONTINUE:
9672 case RID_RETURN:
9673 case RID_GOTO:
9674 statement = cp_parser_jump_statement (parser);
9675 break;
9677 case RID_CILK_SYNC:
9678 cp_lexer_consume_token (parser->lexer);
9679 if (flag_cilkplus)
9681 tree sync_expr = build_cilk_sync ();
9682 SET_EXPR_LOCATION (sync_expr,
9683 token->location);
9684 statement = finish_expr_stmt (sync_expr);
9686 else
9688 error_at (token->location, "-fcilkplus must be enabled to use"
9689 " %<_Cilk_sync%>");
9690 statement = error_mark_node;
9692 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9693 break;
9695 /* Objective-C++ exception-handling constructs. */
9696 case RID_AT_TRY:
9697 case RID_AT_CATCH:
9698 case RID_AT_FINALLY:
9699 case RID_AT_SYNCHRONIZED:
9700 case RID_AT_THROW:
9701 statement = cp_parser_objc_statement (parser);
9702 break;
9704 case RID_TRY:
9705 statement = cp_parser_try_block (parser);
9706 break;
9708 case RID_NAMESPACE:
9709 /* This must be a namespace alias definition. */
9710 cp_parser_declaration_statement (parser);
9711 return;
9713 case RID_TRANSACTION_ATOMIC:
9714 case RID_TRANSACTION_RELAXED:
9715 statement = cp_parser_transaction (parser, keyword);
9716 break;
9717 case RID_TRANSACTION_CANCEL:
9718 statement = cp_parser_transaction_cancel (parser);
9719 break;
9721 default:
9722 /* It might be a keyword like `int' that can start a
9723 declaration-statement. */
9724 break;
9727 else if (token->type == CPP_NAME)
9729 /* If the next token is a `:', then we are looking at a
9730 labeled-statement. */
9731 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9732 if (token->type == CPP_COLON)
9734 /* Looks like a labeled-statement with an ordinary label.
9735 Parse the label, and then use tail recursion to parse
9736 the statement. */
9738 cp_parser_label_for_labeled_statement (parser, std_attrs);
9739 goto restart;
9742 /* Anything that starts with a `{' must be a compound-statement. */
9743 else if (token->type == CPP_OPEN_BRACE)
9744 statement = cp_parser_compound_statement (parser, NULL, false, false);
9745 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9746 a statement all its own. */
9747 else if (token->type == CPP_PRAGMA)
9749 /* Only certain OpenMP pragmas are attached to statements, and thus
9750 are considered statements themselves. All others are not. In
9751 the context of a compound, accept the pragma as a "statement" and
9752 return so that we can check for a close brace. Otherwise we
9753 require a real statement and must go back and read one. */
9754 if (in_compound)
9755 cp_parser_pragma (parser, pragma_compound);
9756 else if (!cp_parser_pragma (parser, pragma_stmt))
9757 goto restart;
9758 return;
9760 else if (token->type == CPP_EOF)
9762 cp_parser_error (parser, "expected statement");
9763 return;
9766 /* Everything else must be a declaration-statement or an
9767 expression-statement. Try for the declaration-statement
9768 first, unless we are looking at a `;', in which case we know that
9769 we have an expression-statement. */
9770 if (!statement)
9772 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9774 if (std_attrs != NULL_TREE)
9776 /* Attributes should be parsed as part of the the
9777 declaration, so let's un-parse them. */
9778 saved_tokens.rollback();
9779 std_attrs = NULL_TREE;
9782 cp_parser_parse_tentatively (parser);
9783 /* Try to parse the declaration-statement. */
9784 cp_parser_declaration_statement (parser);
9785 /* If that worked, we're done. */
9786 if (cp_parser_parse_definitely (parser))
9787 return;
9789 /* Look for an expression-statement instead. */
9790 statement = cp_parser_expression_statement (parser, in_statement_expr);
9793 /* Set the line number for the statement. */
9794 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9795 SET_EXPR_LOCATION (statement, statement_location);
9797 /* Note that for now, we don't do anything with c++11 statements
9798 parsed at this level. */
9799 if (std_attrs != NULL_TREE)
9800 warning_at (attrs_location,
9801 OPT_Wattributes,
9802 "attributes at the beginning of statement are ignored");
9805 /* Parse the label for a labeled-statement, i.e.
9807 identifier :
9808 case constant-expression :
9809 default :
9811 GNU Extension:
9812 case constant-expression ... constant-expression : statement
9814 When a label is parsed without errors, the label is added to the
9815 parse tree by the finish_* functions, so this function doesn't
9816 have to return the label. */
9818 static void
9819 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9821 cp_token *token;
9822 tree label = NULL_TREE;
9823 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9825 /* The next token should be an identifier. */
9826 token = cp_lexer_peek_token (parser->lexer);
9827 if (token->type != CPP_NAME
9828 && token->type != CPP_KEYWORD)
9830 cp_parser_error (parser, "expected labeled-statement");
9831 return;
9834 parser->colon_corrects_to_scope_p = false;
9835 switch (token->keyword)
9837 case RID_CASE:
9839 tree expr, expr_hi;
9840 cp_token *ellipsis;
9842 /* Consume the `case' token. */
9843 cp_lexer_consume_token (parser->lexer);
9844 /* Parse the constant-expression. */
9845 expr = cp_parser_constant_expression (parser);
9846 if (check_for_bare_parameter_packs (expr))
9847 expr = error_mark_node;
9849 ellipsis = cp_lexer_peek_token (parser->lexer);
9850 if (ellipsis->type == CPP_ELLIPSIS)
9852 /* Consume the `...' token. */
9853 cp_lexer_consume_token (parser->lexer);
9854 expr_hi = cp_parser_constant_expression (parser);
9855 if (check_for_bare_parameter_packs (expr_hi))
9856 expr_hi = error_mark_node;
9858 /* We don't need to emit warnings here, as the common code
9859 will do this for us. */
9861 else
9862 expr_hi = NULL_TREE;
9864 if (parser->in_switch_statement_p)
9865 finish_case_label (token->location, expr, expr_hi);
9866 else
9867 error_at (token->location,
9868 "case label %qE not within a switch statement",
9869 expr);
9871 break;
9873 case RID_DEFAULT:
9874 /* Consume the `default' token. */
9875 cp_lexer_consume_token (parser->lexer);
9877 if (parser->in_switch_statement_p)
9878 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9879 else
9880 error_at (token->location, "case label not within a switch statement");
9881 break;
9883 default:
9884 /* Anything else must be an ordinary label. */
9885 label = finish_label_stmt (cp_parser_identifier (parser));
9886 break;
9889 /* Require the `:' token. */
9890 cp_parser_require (parser, CPP_COLON, RT_COLON);
9892 /* An ordinary label may optionally be followed by attributes.
9893 However, this is only permitted if the attributes are then
9894 followed by a semicolon. This is because, for backward
9895 compatibility, when parsing
9896 lab: __attribute__ ((unused)) int i;
9897 we want the attribute to attach to "i", not "lab". */
9898 if (label != NULL_TREE
9899 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9901 tree attrs;
9902 cp_parser_parse_tentatively (parser);
9903 attrs = cp_parser_gnu_attributes_opt (parser);
9904 if (attrs == NULL_TREE
9905 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9906 cp_parser_abort_tentative_parse (parser);
9907 else if (!cp_parser_parse_definitely (parser))
9909 else
9910 attributes = chainon (attributes, attrs);
9913 if (attributes != NULL_TREE)
9914 cplus_decl_attributes (&label, attributes, 0);
9916 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9919 /* Parse an expression-statement.
9921 expression-statement:
9922 expression [opt] ;
9924 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9925 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9926 indicates whether this expression-statement is part of an
9927 expression statement. */
9929 static tree
9930 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9932 tree statement = NULL_TREE;
9933 cp_token *token = cp_lexer_peek_token (parser->lexer);
9935 /* If the next token is a ';', then there is no expression
9936 statement. */
9937 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9939 statement = cp_parser_expression (parser);
9940 if (statement == error_mark_node
9941 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9943 cp_parser_skip_to_end_of_block_or_statement (parser);
9944 return error_mark_node;
9948 /* Give a helpful message for "A<T>::type t;" and the like. */
9949 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9950 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9952 if (TREE_CODE (statement) == SCOPE_REF)
9953 error_at (token->location, "need %<typename%> before %qE because "
9954 "%qT is a dependent scope",
9955 statement, TREE_OPERAND (statement, 0));
9956 else if (is_overloaded_fn (statement)
9957 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9959 /* A::A a; */
9960 tree fn = get_first_fn (statement);
9961 error_at (token->location,
9962 "%<%T::%D%> names the constructor, not the type",
9963 DECL_CONTEXT (fn), DECL_NAME (fn));
9967 /* Consume the final `;'. */
9968 cp_parser_consume_semicolon_at_end_of_statement (parser);
9970 if (in_statement_expr
9971 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9972 /* This is the final expression statement of a statement
9973 expression. */
9974 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9975 else if (statement)
9976 statement = finish_expr_stmt (statement);
9978 return statement;
9981 /* Parse a compound-statement.
9983 compound-statement:
9984 { statement-seq [opt] }
9986 GNU extension:
9988 compound-statement:
9989 { label-declaration-seq [opt] statement-seq [opt] }
9991 label-declaration-seq:
9992 label-declaration
9993 label-declaration-seq label-declaration
9995 Returns a tree representing the statement. */
9997 static tree
9998 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9999 bool in_try, bool function_body)
10001 tree compound_stmt;
10003 /* Consume the `{'. */
10004 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10005 return error_mark_node;
10006 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10007 && !function_body && cxx_dialect < cxx14)
10008 pedwarn (input_location, OPT_Wpedantic,
10009 "compound-statement in constexpr function");
10010 /* Begin the compound-statement. */
10011 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10012 /* If the next keyword is `__label__' we have a label declaration. */
10013 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10014 cp_parser_label_declaration (parser);
10015 /* Parse an (optional) statement-seq. */
10016 cp_parser_statement_seq_opt (parser, in_statement_expr);
10017 /* Finish the compound-statement. */
10018 finish_compound_stmt (compound_stmt);
10019 /* Consume the `}'. */
10020 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10022 return compound_stmt;
10025 /* Parse an (optional) statement-seq.
10027 statement-seq:
10028 statement
10029 statement-seq [opt] statement */
10031 static void
10032 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10034 /* Scan statements until there aren't any more. */
10035 while (true)
10037 cp_token *token = cp_lexer_peek_token (parser->lexer);
10039 /* If we are looking at a `}', then we have run out of
10040 statements; the same is true if we have reached the end
10041 of file, or have stumbled upon a stray '@end'. */
10042 if (token->type == CPP_CLOSE_BRACE
10043 || token->type == CPP_EOF
10044 || token->type == CPP_PRAGMA_EOL
10045 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10046 break;
10048 /* If we are in a compound statement and find 'else' then
10049 something went wrong. */
10050 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10052 if (parser->in_statement & IN_IF_STMT)
10053 break;
10054 else
10056 token = cp_lexer_consume_token (parser->lexer);
10057 error_at (token->location, "%<else%> without a previous %<if%>");
10061 /* Parse the statement. */
10062 cp_parser_statement (parser, in_statement_expr, true, NULL);
10066 /* Parse a selection-statement.
10068 selection-statement:
10069 if ( condition ) statement
10070 if ( condition ) statement else statement
10071 switch ( condition ) statement
10073 Returns the new IF_STMT or SWITCH_STMT.
10075 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10076 is a (possibly labeled) if statement which is not enclosed in
10077 braces and has an else clause. This is used to implement
10078 -Wparentheses. */
10080 static tree
10081 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10083 cp_token *token;
10084 enum rid keyword;
10086 if (if_p != NULL)
10087 *if_p = false;
10089 /* Peek at the next token. */
10090 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10092 /* See what kind of keyword it is. */
10093 keyword = token->keyword;
10094 switch (keyword)
10096 case RID_IF:
10097 case RID_SWITCH:
10099 tree statement;
10100 tree condition;
10102 /* Look for the `('. */
10103 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10105 cp_parser_skip_to_end_of_statement (parser);
10106 return error_mark_node;
10109 /* Begin the selection-statement. */
10110 if (keyword == RID_IF)
10111 statement = begin_if_stmt ();
10112 else
10113 statement = begin_switch_stmt ();
10115 /* Parse the condition. */
10116 condition = cp_parser_condition (parser);
10117 /* Look for the `)'. */
10118 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10119 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10120 /*consume_paren=*/true);
10122 if (keyword == RID_IF)
10124 bool nested_if;
10125 unsigned char in_statement;
10127 /* Add the condition. */
10128 finish_if_stmt_cond (condition, statement);
10130 /* Parse the then-clause. */
10131 in_statement = parser->in_statement;
10132 parser->in_statement |= IN_IF_STMT;
10133 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10135 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10136 add_stmt (build_empty_stmt (loc));
10137 cp_lexer_consume_token (parser->lexer);
10138 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10139 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10140 "empty body in an %<if%> statement");
10141 nested_if = false;
10143 else
10144 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10145 parser->in_statement = in_statement;
10147 finish_then_clause (statement);
10149 /* If the next token is `else', parse the else-clause. */
10150 if (cp_lexer_next_token_is_keyword (parser->lexer,
10151 RID_ELSE))
10153 /* Consume the `else' keyword. */
10154 cp_lexer_consume_token (parser->lexer);
10155 begin_else_clause (statement);
10156 /* Parse the else-clause. */
10157 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10159 location_t loc;
10160 loc = cp_lexer_peek_token (parser->lexer)->location;
10161 warning_at (loc,
10162 OPT_Wempty_body, "suggest braces around "
10163 "empty body in an %<else%> statement");
10164 add_stmt (build_empty_stmt (loc));
10165 cp_lexer_consume_token (parser->lexer);
10167 else
10168 cp_parser_implicitly_scoped_statement (parser, NULL);
10170 finish_else_clause (statement);
10172 /* If we are currently parsing a then-clause, then
10173 IF_P will not be NULL. We set it to true to
10174 indicate that this if statement has an else clause.
10175 This may trigger the Wparentheses warning below
10176 when we get back up to the parent if statement. */
10177 if (if_p != NULL)
10178 *if_p = true;
10180 else
10182 /* This if statement does not have an else clause. If
10183 NESTED_IF is true, then the then-clause is an if
10184 statement which does have an else clause. We warn
10185 about the potential ambiguity. */
10186 if (nested_if)
10187 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10188 "suggest explicit braces to avoid ambiguous"
10189 " %<else%>");
10192 /* Now we're all done with the if-statement. */
10193 finish_if_stmt (statement);
10195 else
10197 bool in_switch_statement_p;
10198 unsigned char in_statement;
10200 /* Add the condition. */
10201 finish_switch_cond (condition, statement);
10203 /* Parse the body of the switch-statement. */
10204 in_switch_statement_p = parser->in_switch_statement_p;
10205 in_statement = parser->in_statement;
10206 parser->in_switch_statement_p = true;
10207 parser->in_statement |= IN_SWITCH_STMT;
10208 cp_parser_implicitly_scoped_statement (parser, NULL);
10209 parser->in_switch_statement_p = in_switch_statement_p;
10210 parser->in_statement = in_statement;
10212 /* Now we're all done with the switch-statement. */
10213 finish_switch_stmt (statement);
10216 return statement;
10218 break;
10220 default:
10221 cp_parser_error (parser, "expected selection-statement");
10222 return error_mark_node;
10226 /* Parse a condition.
10228 condition:
10229 expression
10230 type-specifier-seq declarator = initializer-clause
10231 type-specifier-seq declarator braced-init-list
10233 GNU Extension:
10235 condition:
10236 type-specifier-seq declarator asm-specification [opt]
10237 attributes [opt] = assignment-expression
10239 Returns the expression that should be tested. */
10241 static tree
10242 cp_parser_condition (cp_parser* parser)
10244 cp_decl_specifier_seq type_specifiers;
10245 const char *saved_message;
10246 int declares_class_or_enum;
10248 /* Try the declaration first. */
10249 cp_parser_parse_tentatively (parser);
10250 /* New types are not allowed in the type-specifier-seq for a
10251 condition. */
10252 saved_message = parser->type_definition_forbidden_message;
10253 parser->type_definition_forbidden_message
10254 = G_("types may not be defined in conditions");
10255 /* Parse the type-specifier-seq. */
10256 cp_parser_decl_specifier_seq (parser,
10257 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10258 &type_specifiers,
10259 &declares_class_or_enum);
10260 /* Restore the saved message. */
10261 parser->type_definition_forbidden_message = saved_message;
10262 /* If all is well, we might be looking at a declaration. */
10263 if (!cp_parser_error_occurred (parser))
10265 tree decl;
10266 tree asm_specification;
10267 tree attributes;
10268 cp_declarator *declarator;
10269 tree initializer = NULL_TREE;
10271 /* Parse the declarator. */
10272 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10273 /*ctor_dtor_or_conv_p=*/NULL,
10274 /*parenthesized_p=*/NULL,
10275 /*member_p=*/false,
10276 /*friend_p=*/false);
10277 /* Parse the attributes. */
10278 attributes = cp_parser_attributes_opt (parser);
10279 /* Parse the asm-specification. */
10280 asm_specification = cp_parser_asm_specification_opt (parser);
10281 /* If the next token is not an `=' or '{', then we might still be
10282 looking at an expression. For example:
10284 if (A(a).x)
10286 looks like a decl-specifier-seq and a declarator -- but then
10287 there is no `=', so this is an expression. */
10288 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10289 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10290 cp_parser_simulate_error (parser);
10292 /* If we did see an `=' or '{', then we are looking at a declaration
10293 for sure. */
10294 if (cp_parser_parse_definitely (parser))
10296 tree pushed_scope;
10297 bool non_constant_p;
10298 bool flags = LOOKUP_ONLYCONVERTING;
10300 /* Create the declaration. */
10301 decl = start_decl (declarator, &type_specifiers,
10302 /*initialized_p=*/true,
10303 attributes, /*prefix_attributes=*/NULL_TREE,
10304 &pushed_scope);
10306 /* Parse the initializer. */
10307 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10309 initializer = cp_parser_braced_list (parser, &non_constant_p);
10310 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10311 flags = 0;
10313 else
10315 /* Consume the `='. */
10316 cp_parser_require (parser, CPP_EQ, RT_EQ);
10317 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10319 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10320 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10322 /* Process the initializer. */
10323 cp_finish_decl (decl,
10324 initializer, !non_constant_p,
10325 asm_specification,
10326 flags);
10328 if (pushed_scope)
10329 pop_scope (pushed_scope);
10331 return convert_from_reference (decl);
10334 /* If we didn't even get past the declarator successfully, we are
10335 definitely not looking at a declaration. */
10336 else
10337 cp_parser_abort_tentative_parse (parser);
10339 /* Otherwise, we are looking at an expression. */
10340 return cp_parser_expression (parser);
10343 /* Parses a for-statement or range-for-statement until the closing ')',
10344 not included. */
10346 static tree
10347 cp_parser_for (cp_parser *parser, bool ivdep)
10349 tree init, scope, decl;
10350 bool is_range_for;
10352 /* Begin the for-statement. */
10353 scope = begin_for_scope (&init);
10355 /* Parse the initialization. */
10356 is_range_for = cp_parser_for_init_statement (parser, &decl);
10358 if (is_range_for)
10359 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10360 else
10361 return cp_parser_c_for (parser, scope, init, ivdep);
10364 static tree
10365 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10367 /* Normal for loop */
10368 tree condition = NULL_TREE;
10369 tree expression = NULL_TREE;
10370 tree stmt;
10372 stmt = begin_for_stmt (scope, init);
10373 /* The for-init-statement has already been parsed in
10374 cp_parser_for_init_statement, so no work is needed here. */
10375 finish_for_init_stmt (stmt);
10377 /* If there's a condition, process it. */
10378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10379 condition = cp_parser_condition (parser);
10380 else if (ivdep)
10382 cp_parser_error (parser, "missing loop condition in loop with "
10383 "%<GCC ivdep%> pragma");
10384 condition = error_mark_node;
10386 finish_for_cond (condition, stmt, ivdep);
10387 /* Look for the `;'. */
10388 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10390 /* If there's an expression, process it. */
10391 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10392 expression = cp_parser_expression (parser);
10393 finish_for_expr (expression, stmt);
10395 return stmt;
10398 /* Tries to parse a range-based for-statement:
10400 range-based-for:
10401 decl-specifier-seq declarator : expression
10403 The decl-specifier-seq declarator and the `:' are already parsed by
10404 cp_parser_for_init_statement. If processing_template_decl it returns a
10405 newly created RANGE_FOR_STMT; if not, it is converted to a
10406 regular FOR_STMT. */
10408 static tree
10409 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10410 bool ivdep)
10412 tree stmt, range_expr;
10414 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10416 bool expr_non_constant_p;
10417 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10419 else
10420 range_expr = cp_parser_expression (parser);
10422 /* If in template, STMT is converted to a normal for-statement
10423 at instantiation. If not, it is done just ahead. */
10424 if (processing_template_decl)
10426 if (check_for_bare_parameter_packs (range_expr))
10427 range_expr = error_mark_node;
10428 stmt = begin_range_for_stmt (scope, init);
10429 if (ivdep)
10430 RANGE_FOR_IVDEP (stmt) = 1;
10431 finish_range_for_decl (stmt, range_decl, range_expr);
10432 if (!type_dependent_expression_p (range_expr)
10433 /* do_auto_deduction doesn't mess with template init-lists. */
10434 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10435 do_range_for_auto_deduction (range_decl, range_expr);
10437 else
10439 stmt = begin_for_stmt (scope, init);
10440 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10442 return stmt;
10445 /* Subroutine of cp_convert_range_for: given the initializer expression,
10446 builds up the range temporary. */
10448 static tree
10449 build_range_temp (tree range_expr)
10451 tree range_type, range_temp;
10453 /* Find out the type deduced by the declaration
10454 `auto &&__range = range_expr'. */
10455 range_type = cp_build_reference_type (make_auto (), true);
10456 range_type = do_auto_deduction (range_type, range_expr,
10457 type_uses_auto (range_type));
10459 /* Create the __range variable. */
10460 range_temp = build_decl (input_location, VAR_DECL,
10461 get_identifier ("__for_range"), range_type);
10462 TREE_USED (range_temp) = 1;
10463 DECL_ARTIFICIAL (range_temp) = 1;
10465 return range_temp;
10468 /* Used by cp_parser_range_for in template context: we aren't going to
10469 do a full conversion yet, but we still need to resolve auto in the
10470 type of the for-range-declaration if present. This is basically
10471 a shortcut version of cp_convert_range_for. */
10473 static void
10474 do_range_for_auto_deduction (tree decl, tree range_expr)
10476 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10477 if (auto_node)
10479 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10480 range_temp = convert_from_reference (build_range_temp (range_expr));
10481 iter_type = (cp_parser_perform_range_for_lookup
10482 (range_temp, &begin_dummy, &end_dummy));
10483 if (iter_type)
10485 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10486 iter_type);
10487 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10488 tf_warning_or_error);
10489 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10490 iter_decl, auto_node);
10495 /* Converts a range-based for-statement into a normal
10496 for-statement, as per the definition.
10498 for (RANGE_DECL : RANGE_EXPR)
10499 BLOCK
10501 should be equivalent to:
10504 auto &&__range = RANGE_EXPR;
10505 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10506 __begin != __end;
10507 ++__begin)
10509 RANGE_DECL = *__begin;
10510 BLOCK
10514 If RANGE_EXPR is an array:
10515 BEGIN_EXPR = __range
10516 END_EXPR = __range + ARRAY_SIZE(__range)
10517 Else if RANGE_EXPR has a member 'begin' or 'end':
10518 BEGIN_EXPR = __range.begin()
10519 END_EXPR = __range.end()
10520 Else:
10521 BEGIN_EXPR = begin(__range)
10522 END_EXPR = end(__range);
10524 If __range has a member 'begin' but not 'end', or vice versa, we must
10525 still use the second alternative (it will surely fail, however).
10526 When calling begin()/end() in the third alternative we must use
10527 argument dependent lookup, but always considering 'std' as an associated
10528 namespace. */
10530 tree
10531 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10532 bool ivdep)
10534 tree begin, end;
10535 tree iter_type, begin_expr, end_expr;
10536 tree condition, expression;
10538 if (range_decl == error_mark_node || range_expr == error_mark_node)
10539 /* If an error happened previously do nothing or else a lot of
10540 unhelpful errors would be issued. */
10541 begin_expr = end_expr = iter_type = error_mark_node;
10542 else
10544 tree range_temp;
10546 if (TREE_CODE (range_expr) == VAR_DECL
10547 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10548 /* Can't bind a reference to an array of runtime bound. */
10549 range_temp = range_expr;
10550 else
10552 range_temp = build_range_temp (range_expr);
10553 pushdecl (range_temp);
10554 cp_finish_decl (range_temp, range_expr,
10555 /*is_constant_init*/false, NULL_TREE,
10556 LOOKUP_ONLYCONVERTING);
10557 range_temp = convert_from_reference (range_temp);
10559 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10560 &begin_expr, &end_expr);
10563 /* The new for initialization statement. */
10564 begin = build_decl (input_location, VAR_DECL,
10565 get_identifier ("__for_begin"), iter_type);
10566 TREE_USED (begin) = 1;
10567 DECL_ARTIFICIAL (begin) = 1;
10568 pushdecl (begin);
10569 cp_finish_decl (begin, begin_expr,
10570 /*is_constant_init*/false, NULL_TREE,
10571 LOOKUP_ONLYCONVERTING);
10573 end = build_decl (input_location, VAR_DECL,
10574 get_identifier ("__for_end"), iter_type);
10575 TREE_USED (end) = 1;
10576 DECL_ARTIFICIAL (end) = 1;
10577 pushdecl (end);
10578 cp_finish_decl (end, end_expr,
10579 /*is_constant_init*/false, NULL_TREE,
10580 LOOKUP_ONLYCONVERTING);
10582 finish_for_init_stmt (statement);
10584 /* The new for condition. */
10585 condition = build_x_binary_op (input_location, NE_EXPR,
10586 begin, ERROR_MARK,
10587 end, ERROR_MARK,
10588 NULL, tf_warning_or_error);
10589 finish_for_cond (condition, statement, ivdep);
10591 /* The new increment expression. */
10592 expression = finish_unary_op_expr (input_location,
10593 PREINCREMENT_EXPR, begin,
10594 tf_warning_or_error);
10595 finish_for_expr (expression, statement);
10597 /* The declaration is initialized with *__begin inside the loop body. */
10598 cp_finish_decl (range_decl,
10599 build_x_indirect_ref (input_location, begin, RO_NULL,
10600 tf_warning_or_error),
10601 /*is_constant_init*/false, NULL_TREE,
10602 LOOKUP_ONLYCONVERTING);
10604 return statement;
10607 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10608 We need to solve both at the same time because the method used
10609 depends on the existence of members begin or end.
10610 Returns the type deduced for the iterator expression. */
10612 static tree
10613 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10615 if (error_operand_p (range))
10617 *begin = *end = error_mark_node;
10618 return error_mark_node;
10621 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10623 error ("range-based %<for%> expression of type %qT "
10624 "has incomplete type", TREE_TYPE (range));
10625 *begin = *end = error_mark_node;
10626 return error_mark_node;
10628 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10630 /* If RANGE is an array, we will use pointer arithmetic. */
10631 *begin = range;
10632 *end = build_binary_op (input_location, PLUS_EXPR,
10633 range,
10634 array_type_nelts_top (TREE_TYPE (range)),
10636 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10638 else
10640 /* If it is not an array, we must do a bit of magic. */
10641 tree id_begin, id_end;
10642 tree member_begin, member_end;
10644 *begin = *end = error_mark_node;
10646 id_begin = get_identifier ("begin");
10647 id_end = get_identifier ("end");
10648 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10649 /*protect=*/2, /*want_type=*/false,
10650 tf_warning_or_error);
10651 member_end = lookup_member (TREE_TYPE (range), id_end,
10652 /*protect=*/2, /*want_type=*/false,
10653 tf_warning_or_error);
10655 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10657 /* Use the member functions. */
10658 if (member_begin != NULL_TREE)
10659 *begin = cp_parser_range_for_member_function (range, id_begin);
10660 else
10661 error ("range-based %<for%> expression of type %qT has an "
10662 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10664 if (member_end != NULL_TREE)
10665 *end = cp_parser_range_for_member_function (range, id_end);
10666 else
10667 error ("range-based %<for%> expression of type %qT has a "
10668 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10670 else
10672 /* Use global functions with ADL. */
10673 vec<tree, va_gc> *vec;
10674 vec = make_tree_vector ();
10676 vec_safe_push (vec, range);
10678 member_begin = perform_koenig_lookup (id_begin, vec,
10679 tf_warning_or_error);
10680 *begin = finish_call_expr (member_begin, &vec, false, true,
10681 tf_warning_or_error);
10682 member_end = perform_koenig_lookup (id_end, vec,
10683 tf_warning_or_error);
10684 *end = finish_call_expr (member_end, &vec, false, true,
10685 tf_warning_or_error);
10687 release_tree_vector (vec);
10690 /* Last common checks. */
10691 if (*begin == error_mark_node || *end == error_mark_node)
10693 /* If one of the expressions is an error do no more checks. */
10694 *begin = *end = error_mark_node;
10695 return error_mark_node;
10697 else if (type_dependent_expression_p (*begin)
10698 || type_dependent_expression_p (*end))
10699 /* Can happen, when, eg, in a template context, Koenig lookup
10700 can't resolve begin/end (c++/58503). */
10701 return NULL_TREE;
10702 else
10704 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10705 /* The unqualified type of the __begin and __end temporaries should
10706 be the same, as required by the multiple auto declaration. */
10707 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10708 error ("inconsistent begin/end types in range-based %<for%> "
10709 "statement: %qT and %qT",
10710 TREE_TYPE (*begin), TREE_TYPE (*end));
10711 return iter_type;
10716 /* Helper function for cp_parser_perform_range_for_lookup.
10717 Builds a tree for RANGE.IDENTIFIER(). */
10719 static tree
10720 cp_parser_range_for_member_function (tree range, tree identifier)
10722 tree member, res;
10723 vec<tree, va_gc> *vec;
10725 member = finish_class_member_access_expr (range, identifier,
10726 false, tf_warning_or_error);
10727 if (member == error_mark_node)
10728 return error_mark_node;
10730 vec = make_tree_vector ();
10731 res = finish_call_expr (member, &vec,
10732 /*disallow_virtual=*/false,
10733 /*koenig_p=*/false,
10734 tf_warning_or_error);
10735 release_tree_vector (vec);
10736 return res;
10739 /* Parse an iteration-statement.
10741 iteration-statement:
10742 while ( condition ) statement
10743 do statement while ( expression ) ;
10744 for ( for-init-statement condition [opt] ; expression [opt] )
10745 statement
10747 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10749 static tree
10750 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10752 cp_token *token;
10753 enum rid keyword;
10754 tree statement;
10755 unsigned char in_statement;
10757 /* Peek at the next token. */
10758 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10759 if (!token)
10760 return error_mark_node;
10762 /* Remember whether or not we are already within an iteration
10763 statement. */
10764 in_statement = parser->in_statement;
10766 /* See what kind of keyword it is. */
10767 keyword = token->keyword;
10768 switch (keyword)
10770 case RID_WHILE:
10772 tree condition;
10774 /* Begin the while-statement. */
10775 statement = begin_while_stmt ();
10776 /* Look for the `('. */
10777 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10778 /* Parse the condition. */
10779 condition = cp_parser_condition (parser);
10780 finish_while_stmt_cond (condition, statement, ivdep);
10781 /* Look for the `)'. */
10782 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10783 /* Parse the dependent statement. */
10784 parser->in_statement = IN_ITERATION_STMT;
10785 cp_parser_already_scoped_statement (parser);
10786 parser->in_statement = in_statement;
10787 /* We're done with the while-statement. */
10788 finish_while_stmt (statement);
10790 break;
10792 case RID_DO:
10794 tree expression;
10796 /* Begin the do-statement. */
10797 statement = begin_do_stmt ();
10798 /* Parse the body of the do-statement. */
10799 parser->in_statement = IN_ITERATION_STMT;
10800 cp_parser_implicitly_scoped_statement (parser, NULL);
10801 parser->in_statement = in_statement;
10802 finish_do_body (statement);
10803 /* Look for the `while' keyword. */
10804 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10805 /* Look for the `('. */
10806 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10807 /* Parse the expression. */
10808 expression = cp_parser_expression (parser);
10809 /* We're done with the do-statement. */
10810 finish_do_stmt (expression, statement, ivdep);
10811 /* Look for the `)'. */
10812 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10813 /* Look for the `;'. */
10814 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10816 break;
10818 case RID_FOR:
10820 /* Look for the `('. */
10821 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10823 statement = cp_parser_for (parser, ivdep);
10825 /* Look for the `)'. */
10826 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10828 /* Parse the body of the for-statement. */
10829 parser->in_statement = IN_ITERATION_STMT;
10830 cp_parser_already_scoped_statement (parser);
10831 parser->in_statement = in_statement;
10833 /* We're done with the for-statement. */
10834 finish_for_stmt (statement);
10836 break;
10838 default:
10839 cp_parser_error (parser, "expected iteration-statement");
10840 statement = error_mark_node;
10841 break;
10844 return statement;
10847 /* Parse a for-init-statement or the declarator of a range-based-for.
10848 Returns true if a range-based-for declaration is seen.
10850 for-init-statement:
10851 expression-statement
10852 simple-declaration */
10854 static bool
10855 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10857 /* If the next token is a `;', then we have an empty
10858 expression-statement. Grammatically, this is also a
10859 simple-declaration, but an invalid one, because it does not
10860 declare anything. Therefore, if we did not handle this case
10861 specially, we would issue an error message about an invalid
10862 declaration. */
10863 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10865 bool is_range_for = false;
10866 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10868 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10869 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10871 /* N3994 -- for (id : init) ... */
10872 if (cxx_dialect < cxx1z)
10873 pedwarn (input_location, 0, "range-based for loop without a "
10874 "type-specifier only available with "
10875 "-std=c++1z or -std=gnu++1z");
10876 tree name = cp_parser_identifier (parser);
10877 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10878 *decl = build_decl (input_location, VAR_DECL, name, type);
10879 pushdecl (*decl);
10880 cp_lexer_consume_token (parser->lexer);
10881 return true;
10884 /* A colon is used in range-based for. */
10885 parser->colon_corrects_to_scope_p = false;
10887 /* We're going to speculatively look for a declaration, falling back
10888 to an expression, if necessary. */
10889 cp_parser_parse_tentatively (parser);
10890 /* Parse the declaration. */
10891 cp_parser_simple_declaration (parser,
10892 /*function_definition_allowed_p=*/false,
10893 decl);
10894 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10895 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10897 /* It is a range-for, consume the ':' */
10898 cp_lexer_consume_token (parser->lexer);
10899 is_range_for = true;
10900 if (cxx_dialect < cxx11)
10902 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10903 "range-based %<for%> loops only available with "
10904 "-std=c++11 or -std=gnu++11");
10905 *decl = error_mark_node;
10908 else
10909 /* The ';' is not consumed yet because we told
10910 cp_parser_simple_declaration not to. */
10911 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10913 if (cp_parser_parse_definitely (parser))
10914 return is_range_for;
10915 /* If the tentative parse failed, then we shall need to look for an
10916 expression-statement. */
10918 /* If we are here, it is an expression-statement. */
10919 cp_parser_expression_statement (parser, NULL_TREE);
10920 return false;
10923 /* Parse a jump-statement.
10925 jump-statement:
10926 break ;
10927 continue ;
10928 return expression [opt] ;
10929 return braced-init-list ;
10930 goto identifier ;
10932 GNU extension:
10934 jump-statement:
10935 goto * expression ;
10937 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10939 static tree
10940 cp_parser_jump_statement (cp_parser* parser)
10942 tree statement = error_mark_node;
10943 cp_token *token;
10944 enum rid keyword;
10945 unsigned char in_statement;
10947 /* Peek at the next token. */
10948 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10949 if (!token)
10950 return error_mark_node;
10952 /* See what kind of keyword it is. */
10953 keyword = token->keyword;
10954 switch (keyword)
10956 case RID_BREAK:
10957 in_statement = parser->in_statement & ~IN_IF_STMT;
10958 switch (in_statement)
10960 case 0:
10961 error_at (token->location, "break statement not within loop or switch");
10962 break;
10963 default:
10964 gcc_assert ((in_statement & IN_SWITCH_STMT)
10965 || in_statement == IN_ITERATION_STMT);
10966 statement = finish_break_stmt ();
10967 if (in_statement == IN_ITERATION_STMT)
10968 break_maybe_infinite_loop ();
10969 break;
10970 case IN_OMP_BLOCK:
10971 error_at (token->location, "invalid exit from OpenMP structured block");
10972 break;
10973 case IN_OMP_FOR:
10974 error_at (token->location, "break statement used with OpenMP for loop");
10975 break;
10976 case IN_CILK_SIMD_FOR:
10977 error_at (token->location, "break statement used with Cilk Plus for loop");
10978 break;
10980 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10981 break;
10983 case RID_CONTINUE:
10984 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10986 case 0:
10987 error_at (token->location, "continue statement not within a loop");
10988 break;
10989 case IN_CILK_SIMD_FOR:
10990 error_at (token->location,
10991 "continue statement within %<#pragma simd%> loop body");
10992 /* Fall through. */
10993 case IN_ITERATION_STMT:
10994 case IN_OMP_FOR:
10995 statement = finish_continue_stmt ();
10996 break;
10997 case IN_OMP_BLOCK:
10998 error_at (token->location, "invalid exit from OpenMP structured block");
10999 break;
11000 default:
11001 gcc_unreachable ();
11003 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11004 break;
11006 case RID_RETURN:
11008 tree expr;
11009 bool expr_non_constant_p;
11011 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11013 cp_lexer_set_source_position (parser->lexer);
11014 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11015 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11017 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11018 expr = cp_parser_expression (parser);
11019 else
11020 /* If the next token is a `;', then there is no
11021 expression. */
11022 expr = NULL_TREE;
11023 /* Build the return-statement. */
11024 statement = finish_return_stmt (expr);
11025 /* Look for the final `;'. */
11026 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11028 break;
11030 case RID_GOTO:
11031 if (parser->in_function_body
11032 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11034 error ("%<goto%> in %<constexpr%> function");
11035 cp_function_chain->invalid_constexpr = true;
11038 /* Create the goto-statement. */
11039 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11041 /* Issue a warning about this use of a GNU extension. */
11042 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11043 /* Consume the '*' token. */
11044 cp_lexer_consume_token (parser->lexer);
11045 /* Parse the dependent expression. */
11046 finish_goto_stmt (cp_parser_expression (parser));
11048 else
11049 finish_goto_stmt (cp_parser_identifier (parser));
11050 /* Look for the final `;'. */
11051 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11052 break;
11054 default:
11055 cp_parser_error (parser, "expected jump-statement");
11056 break;
11059 return statement;
11062 /* Parse a declaration-statement.
11064 declaration-statement:
11065 block-declaration */
11067 static void
11068 cp_parser_declaration_statement (cp_parser* parser)
11070 void *p;
11072 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11073 p = obstack_alloc (&declarator_obstack, 0);
11075 /* Parse the block-declaration. */
11076 cp_parser_block_declaration (parser, /*statement_p=*/true);
11078 /* Free any declarators allocated. */
11079 obstack_free (&declarator_obstack, p);
11082 /* Some dependent statements (like `if (cond) statement'), are
11083 implicitly in their own scope. In other words, if the statement is
11084 a single statement (as opposed to a compound-statement), it is
11085 none-the-less treated as if it were enclosed in braces. Any
11086 declarations appearing in the dependent statement are out of scope
11087 after control passes that point. This function parses a statement,
11088 but ensures that is in its own scope, even if it is not a
11089 compound-statement.
11091 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11092 is a (possibly labeled) if statement which is not enclosed in
11093 braces and has an else clause. This is used to implement
11094 -Wparentheses.
11096 Returns the new statement. */
11098 static tree
11099 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11101 tree statement;
11103 if (if_p != NULL)
11104 *if_p = false;
11106 /* Mark if () ; with a special NOP_EXPR. */
11107 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11109 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11110 cp_lexer_consume_token (parser->lexer);
11111 statement = add_stmt (build_empty_stmt (loc));
11113 /* if a compound is opened, we simply parse the statement directly. */
11114 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11115 statement = cp_parser_compound_statement (parser, NULL, false, false);
11116 /* If the token is not a `{', then we must take special action. */
11117 else
11119 /* Create a compound-statement. */
11120 statement = begin_compound_stmt (0);
11121 /* Parse the dependent-statement. */
11122 cp_parser_statement (parser, NULL_TREE, false, if_p);
11123 /* Finish the dummy compound-statement. */
11124 finish_compound_stmt (statement);
11127 /* Return the statement. */
11128 return statement;
11131 /* For some dependent statements (like `while (cond) statement'), we
11132 have already created a scope. Therefore, even if the dependent
11133 statement is a compound-statement, we do not want to create another
11134 scope. */
11136 static void
11137 cp_parser_already_scoped_statement (cp_parser* parser)
11139 /* If the token is a `{', then we must take special action. */
11140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11141 cp_parser_statement (parser, NULL_TREE, false, NULL);
11142 else
11144 /* Avoid calling cp_parser_compound_statement, so that we
11145 don't create a new scope. Do everything else by hand. */
11146 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11147 /* If the next keyword is `__label__' we have a label declaration. */
11148 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11149 cp_parser_label_declaration (parser);
11150 /* Parse an (optional) statement-seq. */
11151 cp_parser_statement_seq_opt (parser, NULL_TREE);
11152 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11156 /* Declarations [gram.dcl.dcl] */
11158 /* Parse an optional declaration-sequence.
11160 declaration-seq:
11161 declaration
11162 declaration-seq declaration */
11164 static void
11165 cp_parser_declaration_seq_opt (cp_parser* parser)
11167 while (true)
11169 cp_token *token;
11171 token = cp_lexer_peek_token (parser->lexer);
11173 if (token->type == CPP_CLOSE_BRACE
11174 || token->type == CPP_EOF
11175 || token->type == CPP_PRAGMA_EOL)
11176 break;
11178 if (token->type == CPP_SEMICOLON)
11180 /* A declaration consisting of a single semicolon is
11181 invalid. Allow it unless we're being pedantic. */
11182 cp_lexer_consume_token (parser->lexer);
11183 if (!in_system_header_at (input_location))
11184 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11185 continue;
11188 /* If we're entering or exiting a region that's implicitly
11189 extern "C", modify the lang context appropriately. */
11190 if (!parser->implicit_extern_c && token->implicit_extern_c)
11192 push_lang_context (lang_name_c);
11193 parser->implicit_extern_c = true;
11195 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11197 pop_lang_context ();
11198 parser->implicit_extern_c = false;
11201 if (token->type == CPP_PRAGMA)
11203 /* A top-level declaration can consist solely of a #pragma.
11204 A nested declaration cannot, so this is done here and not
11205 in cp_parser_declaration. (A #pragma at block scope is
11206 handled in cp_parser_statement.) */
11207 cp_parser_pragma (parser, pragma_external);
11208 continue;
11211 /* Parse the declaration itself. */
11212 cp_parser_declaration (parser);
11216 /* Parse a declaration.
11218 declaration:
11219 block-declaration
11220 function-definition
11221 template-declaration
11222 explicit-instantiation
11223 explicit-specialization
11224 linkage-specification
11225 namespace-definition
11227 GNU extension:
11229 declaration:
11230 __extension__ declaration */
11232 static void
11233 cp_parser_declaration (cp_parser* parser)
11235 cp_token token1;
11236 cp_token token2;
11237 int saved_pedantic;
11238 void *p;
11239 tree attributes = NULL_TREE;
11241 /* Check for the `__extension__' keyword. */
11242 if (cp_parser_extension_opt (parser, &saved_pedantic))
11244 /* Parse the qualified declaration. */
11245 cp_parser_declaration (parser);
11246 /* Restore the PEDANTIC flag. */
11247 pedantic = saved_pedantic;
11249 return;
11252 /* Try to figure out what kind of declaration is present. */
11253 token1 = *cp_lexer_peek_token (parser->lexer);
11255 if (token1.type != CPP_EOF)
11256 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11257 else
11259 token2.type = CPP_EOF;
11260 token2.keyword = RID_MAX;
11263 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11264 p = obstack_alloc (&declarator_obstack, 0);
11266 /* If the next token is `extern' and the following token is a string
11267 literal, then we have a linkage specification. */
11268 if (token1.keyword == RID_EXTERN
11269 && cp_parser_is_pure_string_literal (&token2))
11270 cp_parser_linkage_specification (parser);
11271 /* If the next token is `template', then we have either a template
11272 declaration, an explicit instantiation, or an explicit
11273 specialization. */
11274 else if (token1.keyword == RID_TEMPLATE)
11276 /* `template <>' indicates a template specialization. */
11277 if (token2.type == CPP_LESS
11278 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11279 cp_parser_explicit_specialization (parser);
11280 /* `template <' indicates a template declaration. */
11281 else if (token2.type == CPP_LESS)
11282 cp_parser_template_declaration (parser, /*member_p=*/false);
11283 /* Anything else must be an explicit instantiation. */
11284 else
11285 cp_parser_explicit_instantiation (parser);
11287 /* If the next token is `export', then we have a template
11288 declaration. */
11289 else if (token1.keyword == RID_EXPORT)
11290 cp_parser_template_declaration (parser, /*member_p=*/false);
11291 /* If the next token is `extern', 'static' or 'inline' and the one
11292 after that is `template', we have a GNU extended explicit
11293 instantiation directive. */
11294 else if (cp_parser_allow_gnu_extensions_p (parser)
11295 && (token1.keyword == RID_EXTERN
11296 || token1.keyword == RID_STATIC
11297 || token1.keyword == RID_INLINE)
11298 && token2.keyword == RID_TEMPLATE)
11299 cp_parser_explicit_instantiation (parser);
11300 /* If the next token is `namespace', check for a named or unnamed
11301 namespace definition. */
11302 else if (token1.keyword == RID_NAMESPACE
11303 && (/* A named namespace definition. */
11304 (token2.type == CPP_NAME
11305 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11306 != CPP_EQ))
11307 /* An unnamed namespace definition. */
11308 || token2.type == CPP_OPEN_BRACE
11309 || token2.keyword == RID_ATTRIBUTE))
11310 cp_parser_namespace_definition (parser);
11311 /* An inline (associated) namespace definition. */
11312 else if (token1.keyword == RID_INLINE
11313 && token2.keyword == RID_NAMESPACE)
11314 cp_parser_namespace_definition (parser);
11315 /* Objective-C++ declaration/definition. */
11316 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11317 cp_parser_objc_declaration (parser, NULL_TREE);
11318 else if (c_dialect_objc ()
11319 && token1.keyword == RID_ATTRIBUTE
11320 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11321 cp_parser_objc_declaration (parser, attributes);
11322 /* We must have either a block declaration or a function
11323 definition. */
11324 else
11325 /* Try to parse a block-declaration, or a function-definition. */
11326 cp_parser_block_declaration (parser, /*statement_p=*/false);
11328 /* Free any declarators allocated. */
11329 obstack_free (&declarator_obstack, p);
11332 /* Parse a block-declaration.
11334 block-declaration:
11335 simple-declaration
11336 asm-definition
11337 namespace-alias-definition
11338 using-declaration
11339 using-directive
11341 GNU Extension:
11343 block-declaration:
11344 __extension__ block-declaration
11346 C++0x Extension:
11348 block-declaration:
11349 static_assert-declaration
11351 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11352 part of a declaration-statement. */
11354 static void
11355 cp_parser_block_declaration (cp_parser *parser,
11356 bool statement_p)
11358 cp_token *token1;
11359 int saved_pedantic;
11361 /* Check for the `__extension__' keyword. */
11362 if (cp_parser_extension_opt (parser, &saved_pedantic))
11364 /* Parse the qualified declaration. */
11365 cp_parser_block_declaration (parser, statement_p);
11366 /* Restore the PEDANTIC flag. */
11367 pedantic = saved_pedantic;
11369 return;
11372 /* Peek at the next token to figure out which kind of declaration is
11373 present. */
11374 token1 = cp_lexer_peek_token (parser->lexer);
11376 /* If the next keyword is `asm', we have an asm-definition. */
11377 if (token1->keyword == RID_ASM)
11379 if (statement_p)
11380 cp_parser_commit_to_tentative_parse (parser);
11381 cp_parser_asm_definition (parser);
11383 /* If the next keyword is `namespace', we have a
11384 namespace-alias-definition. */
11385 else if (token1->keyword == RID_NAMESPACE)
11386 cp_parser_namespace_alias_definition (parser);
11387 /* If the next keyword is `using', we have a
11388 using-declaration, a using-directive, or an alias-declaration. */
11389 else if (token1->keyword == RID_USING)
11391 cp_token *token2;
11393 if (statement_p)
11394 cp_parser_commit_to_tentative_parse (parser);
11395 /* If the token after `using' is `namespace', then we have a
11396 using-directive. */
11397 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11398 if (token2->keyword == RID_NAMESPACE)
11399 cp_parser_using_directive (parser);
11400 /* If the second token after 'using' is '=', then we have an
11401 alias-declaration. */
11402 else if (cxx_dialect >= cxx11
11403 && token2->type == CPP_NAME
11404 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11405 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11406 cp_parser_alias_declaration (parser);
11407 /* Otherwise, it's a using-declaration. */
11408 else
11409 cp_parser_using_declaration (parser,
11410 /*access_declaration_p=*/false);
11412 /* If the next keyword is `__label__' we have a misplaced label
11413 declaration. */
11414 else if (token1->keyword == RID_LABEL)
11416 cp_lexer_consume_token (parser->lexer);
11417 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11418 cp_parser_skip_to_end_of_statement (parser);
11419 /* If the next token is now a `;', consume it. */
11420 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11421 cp_lexer_consume_token (parser->lexer);
11423 /* If the next token is `static_assert' we have a static assertion. */
11424 else if (token1->keyword == RID_STATIC_ASSERT)
11425 cp_parser_static_assert (parser, /*member_p=*/false);
11426 /* Anything else must be a simple-declaration. */
11427 else
11428 cp_parser_simple_declaration (parser, !statement_p,
11429 /*maybe_range_for_decl*/NULL);
11432 /* Parse a simple-declaration.
11434 simple-declaration:
11435 decl-specifier-seq [opt] init-declarator-list [opt] ;
11437 init-declarator-list:
11438 init-declarator
11439 init-declarator-list , init-declarator
11441 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11442 function-definition as a simple-declaration.
11444 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11445 parsed declaration if it is an uninitialized single declarator not followed
11446 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11447 if present, will not be consumed. */
11449 static void
11450 cp_parser_simple_declaration (cp_parser* parser,
11451 bool function_definition_allowed_p,
11452 tree *maybe_range_for_decl)
11454 cp_decl_specifier_seq decl_specifiers;
11455 int declares_class_or_enum;
11456 bool saw_declarator;
11457 location_t comma_loc = UNKNOWN_LOCATION;
11458 location_t init_loc = UNKNOWN_LOCATION;
11460 if (maybe_range_for_decl)
11461 *maybe_range_for_decl = NULL_TREE;
11463 /* Defer access checks until we know what is being declared; the
11464 checks for names appearing in the decl-specifier-seq should be
11465 done as if we were in the scope of the thing being declared. */
11466 push_deferring_access_checks (dk_deferred);
11468 /* Parse the decl-specifier-seq. We have to keep track of whether
11469 or not the decl-specifier-seq declares a named class or
11470 enumeration type, since that is the only case in which the
11471 init-declarator-list is allowed to be empty.
11473 [dcl.dcl]
11475 In a simple-declaration, the optional init-declarator-list can be
11476 omitted only when declaring a class or enumeration, that is when
11477 the decl-specifier-seq contains either a class-specifier, an
11478 elaborated-type-specifier, or an enum-specifier. */
11479 cp_parser_decl_specifier_seq (parser,
11480 CP_PARSER_FLAGS_OPTIONAL,
11481 &decl_specifiers,
11482 &declares_class_or_enum);
11483 /* We no longer need to defer access checks. */
11484 stop_deferring_access_checks ();
11486 /* In a block scope, a valid declaration must always have a
11487 decl-specifier-seq. By not trying to parse declarators, we can
11488 resolve the declaration/expression ambiguity more quickly. */
11489 if (!function_definition_allowed_p
11490 && !decl_specifiers.any_specifiers_p)
11492 cp_parser_error (parser, "expected declaration");
11493 goto done;
11496 /* If the next two tokens are both identifiers, the code is
11497 erroneous. The usual cause of this situation is code like:
11499 T t;
11501 where "T" should name a type -- but does not. */
11502 if (!decl_specifiers.any_type_specifiers_p
11503 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11505 /* If parsing tentatively, we should commit; we really are
11506 looking at a declaration. */
11507 cp_parser_commit_to_tentative_parse (parser);
11508 /* Give up. */
11509 goto done;
11512 /* If we have seen at least one decl-specifier, and the next token
11513 is not a parenthesis, then we must be looking at a declaration.
11514 (After "int (" we might be looking at a functional cast.) */
11515 if (decl_specifiers.any_specifiers_p
11516 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11517 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11518 && !cp_parser_error_occurred (parser))
11519 cp_parser_commit_to_tentative_parse (parser);
11521 /* Keep going until we hit the `;' at the end of the simple
11522 declaration. */
11523 saw_declarator = false;
11524 while (cp_lexer_next_token_is_not (parser->lexer,
11525 CPP_SEMICOLON))
11527 cp_token *token;
11528 bool function_definition_p;
11529 tree decl;
11531 if (saw_declarator)
11533 /* If we are processing next declarator, comma is expected */
11534 token = cp_lexer_peek_token (parser->lexer);
11535 gcc_assert (token->type == CPP_COMMA);
11536 cp_lexer_consume_token (parser->lexer);
11537 if (maybe_range_for_decl)
11539 *maybe_range_for_decl = error_mark_node;
11540 if (comma_loc == UNKNOWN_LOCATION)
11541 comma_loc = token->location;
11544 else
11545 saw_declarator = true;
11547 /* Parse the init-declarator. */
11548 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11549 /*checks=*/NULL,
11550 function_definition_allowed_p,
11551 /*member_p=*/false,
11552 declares_class_or_enum,
11553 &function_definition_p,
11554 maybe_range_for_decl,
11555 &init_loc);
11556 /* If an error occurred while parsing tentatively, exit quickly.
11557 (That usually happens when in the body of a function; each
11558 statement is treated as a declaration-statement until proven
11559 otherwise.) */
11560 if (cp_parser_error_occurred (parser))
11561 goto done;
11562 /* Handle function definitions specially. */
11563 if (function_definition_p)
11565 /* If the next token is a `,', then we are probably
11566 processing something like:
11568 void f() {}, *p;
11570 which is erroneous. */
11571 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11573 cp_token *token = cp_lexer_peek_token (parser->lexer);
11574 error_at (token->location,
11575 "mixing"
11576 " declarations and function-definitions is forbidden");
11578 /* Otherwise, we're done with the list of declarators. */
11579 else
11581 pop_deferring_access_checks ();
11582 return;
11585 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11586 *maybe_range_for_decl = decl;
11587 /* The next token should be either a `,' or a `;'. */
11588 token = cp_lexer_peek_token (parser->lexer);
11589 /* If it's a `,', there are more declarators to come. */
11590 if (token->type == CPP_COMMA)
11591 /* will be consumed next time around */;
11592 /* If it's a `;', we are done. */
11593 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11594 break;
11595 /* Anything else is an error. */
11596 else
11598 /* If we have already issued an error message we don't need
11599 to issue another one. */
11600 if (decl != error_mark_node
11601 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11602 cp_parser_error (parser, "expected %<,%> or %<;%>");
11603 /* Skip tokens until we reach the end of the statement. */
11604 cp_parser_skip_to_end_of_statement (parser);
11605 /* If the next token is now a `;', consume it. */
11606 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11607 cp_lexer_consume_token (parser->lexer);
11608 goto done;
11610 /* After the first time around, a function-definition is not
11611 allowed -- even if it was OK at first. For example:
11613 int i, f() {}
11615 is not valid. */
11616 function_definition_allowed_p = false;
11619 /* Issue an error message if no declarators are present, and the
11620 decl-specifier-seq does not itself declare a class or
11621 enumeration: [dcl.dcl]/3. */
11622 if (!saw_declarator)
11624 if (cp_parser_declares_only_class_p (parser))
11626 if (!declares_class_or_enum
11627 && decl_specifiers.type
11628 && OVERLOAD_TYPE_P (decl_specifiers.type))
11629 /* Ensure an error is issued anyway when finish_decltype_type,
11630 called via cp_parser_decl_specifier_seq, returns a class or
11631 an enumeration (c++/51786). */
11632 decl_specifiers.type = NULL_TREE;
11633 shadow_tag (&decl_specifiers);
11635 /* Perform any deferred access checks. */
11636 perform_deferred_access_checks (tf_warning_or_error);
11639 /* Consume the `;'. */
11640 if (!maybe_range_for_decl)
11641 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11642 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11644 if (init_loc != UNKNOWN_LOCATION)
11645 error_at (init_loc, "initializer in range-based %<for%> loop");
11646 if (comma_loc != UNKNOWN_LOCATION)
11647 error_at (comma_loc,
11648 "multiple declarations in range-based %<for%> loop");
11651 done:
11652 pop_deferring_access_checks ();
11655 /* Parse a decl-specifier-seq.
11657 decl-specifier-seq:
11658 decl-specifier-seq [opt] decl-specifier
11659 decl-specifier attribute-specifier-seq [opt] (C++11)
11661 decl-specifier:
11662 storage-class-specifier
11663 type-specifier
11664 function-specifier
11665 friend
11666 typedef
11668 GNU Extension:
11670 decl-specifier:
11671 attributes
11673 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11675 The parser flags FLAGS is used to control type-specifier parsing.
11677 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11678 flags:
11680 1: one of the decl-specifiers is an elaborated-type-specifier
11681 (i.e., a type declaration)
11682 2: one of the decl-specifiers is an enum-specifier or a
11683 class-specifier (i.e., a type definition)
11687 static void
11688 cp_parser_decl_specifier_seq (cp_parser* parser,
11689 cp_parser_flags flags,
11690 cp_decl_specifier_seq *decl_specs,
11691 int* declares_class_or_enum)
11693 bool constructor_possible_p = !parser->in_declarator_p;
11694 bool found_decl_spec = false;
11695 cp_token *start_token = NULL;
11696 cp_decl_spec ds;
11698 /* Clear DECL_SPECS. */
11699 clear_decl_specs (decl_specs);
11701 /* Assume no class or enumeration type is declared. */
11702 *declares_class_or_enum = 0;
11704 /* Keep reading specifiers until there are no more to read. */
11705 while (true)
11707 bool constructor_p;
11708 cp_token *token;
11709 ds = ds_last;
11711 /* Peek at the next token. */
11712 token = cp_lexer_peek_token (parser->lexer);
11714 /* Save the first token of the decl spec list for error
11715 reporting. */
11716 if (!start_token)
11717 start_token = token;
11718 /* Handle attributes. */
11719 if (cp_next_tokens_can_be_attribute_p (parser))
11721 /* Parse the attributes. */
11722 tree attrs = cp_parser_attributes_opt (parser);
11724 /* In a sequence of declaration specifiers, c++11 attributes
11725 appertain to the type that precede them. In that case
11726 [dcl.spec]/1 says:
11728 The attribute-specifier-seq affects the type only for
11729 the declaration it appears in, not other declarations
11730 involving the same type.
11732 But for now let's force the user to position the
11733 attribute either at the beginning of the declaration or
11734 after the declarator-id, which would clearly mean that it
11735 applies to the declarator. */
11736 if (cxx11_attribute_p (attrs))
11738 if (!found_decl_spec)
11739 /* The c++11 attribute is at the beginning of the
11740 declaration. It appertains to the entity being
11741 declared. */;
11742 else
11744 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11746 /* This is an attribute following a
11747 class-specifier. */
11748 if (decl_specs->type_definition_p)
11749 warn_misplaced_attr_for_class_type (token->location,
11750 decl_specs->type);
11751 attrs = NULL_TREE;
11753 else
11755 decl_specs->std_attributes
11756 = chainon (decl_specs->std_attributes,
11757 attrs);
11758 if (decl_specs->locations[ds_std_attribute] == 0)
11759 decl_specs->locations[ds_std_attribute] = token->location;
11761 continue;
11765 decl_specs->attributes
11766 = chainon (decl_specs->attributes,
11767 attrs);
11768 if (decl_specs->locations[ds_attribute] == 0)
11769 decl_specs->locations[ds_attribute] = token->location;
11770 continue;
11772 /* Assume we will find a decl-specifier keyword. */
11773 found_decl_spec = true;
11774 /* If the next token is an appropriate keyword, we can simply
11775 add it to the list. */
11776 switch (token->keyword)
11778 /* decl-specifier:
11779 friend
11780 constexpr */
11781 case RID_FRIEND:
11782 if (!at_class_scope_p ())
11784 error_at (token->location, "%<friend%> used outside of class");
11785 cp_lexer_purge_token (parser->lexer);
11787 else
11789 ds = ds_friend;
11790 /* Consume the token. */
11791 cp_lexer_consume_token (parser->lexer);
11793 break;
11795 case RID_CONSTEXPR:
11796 ds = ds_constexpr;
11797 cp_lexer_consume_token (parser->lexer);
11798 break;
11800 /* function-specifier:
11801 inline
11802 virtual
11803 explicit */
11804 case RID_INLINE:
11805 case RID_VIRTUAL:
11806 case RID_EXPLICIT:
11807 cp_parser_function_specifier_opt (parser, decl_specs);
11808 break;
11810 /* decl-specifier:
11811 typedef */
11812 case RID_TYPEDEF:
11813 ds = ds_typedef;
11814 /* Consume the token. */
11815 cp_lexer_consume_token (parser->lexer);
11816 /* A constructor declarator cannot appear in a typedef. */
11817 constructor_possible_p = false;
11818 /* The "typedef" keyword can only occur in a declaration; we
11819 may as well commit at this point. */
11820 cp_parser_commit_to_tentative_parse (parser);
11822 if (decl_specs->storage_class != sc_none)
11823 decl_specs->conflicting_specifiers_p = true;
11824 break;
11826 /* storage-class-specifier:
11827 auto
11828 register
11829 static
11830 extern
11831 mutable
11833 GNU Extension:
11834 thread */
11835 case RID_AUTO:
11836 if (cxx_dialect == cxx98)
11838 /* Consume the token. */
11839 cp_lexer_consume_token (parser->lexer);
11841 /* Complain about `auto' as a storage specifier, if
11842 we're complaining about C++0x compatibility. */
11843 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11844 " changes meaning in C++11; please remove it");
11846 /* Set the storage class anyway. */
11847 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11848 token);
11850 else
11851 /* C++0x auto type-specifier. */
11852 found_decl_spec = false;
11853 break;
11855 case RID_REGISTER:
11856 case RID_STATIC:
11857 case RID_EXTERN:
11858 case RID_MUTABLE:
11859 /* Consume the token. */
11860 cp_lexer_consume_token (parser->lexer);
11861 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11862 token);
11863 break;
11864 case RID_THREAD:
11865 /* Consume the token. */
11866 ds = ds_thread;
11867 cp_lexer_consume_token (parser->lexer);
11868 break;
11870 default:
11871 /* We did not yet find a decl-specifier yet. */
11872 found_decl_spec = false;
11873 break;
11876 if (found_decl_spec
11877 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11878 && token->keyword != RID_CONSTEXPR)
11879 error ("decl-specifier invalid in condition");
11881 if (ds != ds_last)
11882 set_and_check_decl_spec_loc (decl_specs, ds, token);
11884 /* Constructors are a special case. The `S' in `S()' is not a
11885 decl-specifier; it is the beginning of the declarator. */
11886 constructor_p
11887 = (!found_decl_spec
11888 && constructor_possible_p
11889 && (cp_parser_constructor_declarator_p
11890 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11892 /* If we don't have a DECL_SPEC yet, then we must be looking at
11893 a type-specifier. */
11894 if (!found_decl_spec && !constructor_p)
11896 int decl_spec_declares_class_or_enum;
11897 bool is_cv_qualifier;
11898 tree type_spec;
11900 type_spec
11901 = cp_parser_type_specifier (parser, flags,
11902 decl_specs,
11903 /*is_declaration=*/true,
11904 &decl_spec_declares_class_or_enum,
11905 &is_cv_qualifier);
11906 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11908 /* If this type-specifier referenced a user-defined type
11909 (a typedef, class-name, etc.), then we can't allow any
11910 more such type-specifiers henceforth.
11912 [dcl.spec]
11914 The longest sequence of decl-specifiers that could
11915 possibly be a type name is taken as the
11916 decl-specifier-seq of a declaration. The sequence shall
11917 be self-consistent as described below.
11919 [dcl.type]
11921 As a general rule, at most one type-specifier is allowed
11922 in the complete decl-specifier-seq of a declaration. The
11923 only exceptions are the following:
11925 -- const or volatile can be combined with any other
11926 type-specifier.
11928 -- signed or unsigned can be combined with char, long,
11929 short, or int.
11931 -- ..
11933 Example:
11935 typedef char* Pc;
11936 void g (const int Pc);
11938 Here, Pc is *not* part of the decl-specifier seq; it's
11939 the declarator. Therefore, once we see a type-specifier
11940 (other than a cv-qualifier), we forbid any additional
11941 user-defined types. We *do* still allow things like `int
11942 int' to be considered a decl-specifier-seq, and issue the
11943 error message later. */
11944 if (type_spec && !is_cv_qualifier)
11945 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11946 /* A constructor declarator cannot follow a type-specifier. */
11947 if (type_spec)
11949 constructor_possible_p = false;
11950 found_decl_spec = true;
11951 if (!is_cv_qualifier)
11952 decl_specs->any_type_specifiers_p = true;
11956 /* If we still do not have a DECL_SPEC, then there are no more
11957 decl-specifiers. */
11958 if (!found_decl_spec)
11959 break;
11961 decl_specs->any_specifiers_p = true;
11962 /* After we see one decl-specifier, further decl-specifiers are
11963 always optional. */
11964 flags |= CP_PARSER_FLAGS_OPTIONAL;
11967 /* Don't allow a friend specifier with a class definition. */
11968 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11969 && (*declares_class_or_enum & 2))
11970 error_at (decl_specs->locations[ds_friend],
11971 "class definition may not be declared a friend");
11974 /* Parse an (optional) storage-class-specifier.
11976 storage-class-specifier:
11977 auto
11978 register
11979 static
11980 extern
11981 mutable
11983 GNU Extension:
11985 storage-class-specifier:
11986 thread
11988 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11990 static tree
11991 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11993 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11995 case RID_AUTO:
11996 if (cxx_dialect != cxx98)
11997 return NULL_TREE;
11998 /* Fall through for C++98. */
12000 case RID_REGISTER:
12001 case RID_STATIC:
12002 case RID_EXTERN:
12003 case RID_MUTABLE:
12004 case RID_THREAD:
12005 /* Consume the token. */
12006 return cp_lexer_consume_token (parser->lexer)->u.value;
12008 default:
12009 return NULL_TREE;
12013 /* Parse an (optional) function-specifier.
12015 function-specifier:
12016 inline
12017 virtual
12018 explicit
12020 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12021 Updates DECL_SPECS, if it is non-NULL. */
12023 static tree
12024 cp_parser_function_specifier_opt (cp_parser* parser,
12025 cp_decl_specifier_seq *decl_specs)
12027 cp_token *token = cp_lexer_peek_token (parser->lexer);
12028 switch (token->keyword)
12030 case RID_INLINE:
12031 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12032 break;
12034 case RID_VIRTUAL:
12035 /* 14.5.2.3 [temp.mem]
12037 A member function template shall not be virtual. */
12038 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12039 error_at (token->location, "templates may not be %<virtual%>");
12040 else
12041 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12042 break;
12044 case RID_EXPLICIT:
12045 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12046 break;
12048 default:
12049 return NULL_TREE;
12052 /* Consume the token. */
12053 return cp_lexer_consume_token (parser->lexer)->u.value;
12056 /* Parse a linkage-specification.
12058 linkage-specification:
12059 extern string-literal { declaration-seq [opt] }
12060 extern string-literal declaration */
12062 static void
12063 cp_parser_linkage_specification (cp_parser* parser)
12065 tree linkage;
12067 /* Look for the `extern' keyword. */
12068 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12070 /* Look for the string-literal. */
12071 linkage = cp_parser_string_literal (parser, false, false);
12073 /* Transform the literal into an identifier. If the literal is a
12074 wide-character string, or contains embedded NULs, then we can't
12075 handle it as the user wants. */
12076 if (strlen (TREE_STRING_POINTER (linkage))
12077 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12079 cp_parser_error (parser, "invalid linkage-specification");
12080 /* Assume C++ linkage. */
12081 linkage = lang_name_cplusplus;
12083 else
12084 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12086 /* We're now using the new linkage. */
12087 push_lang_context (linkage);
12089 /* If the next token is a `{', then we're using the first
12090 production. */
12091 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12093 cp_ensure_no_omp_declare_simd (parser);
12095 /* Consume the `{' token. */
12096 cp_lexer_consume_token (parser->lexer);
12097 /* Parse the declarations. */
12098 cp_parser_declaration_seq_opt (parser);
12099 /* Look for the closing `}'. */
12100 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12102 /* Otherwise, there's just one declaration. */
12103 else
12105 bool saved_in_unbraced_linkage_specification_p;
12107 saved_in_unbraced_linkage_specification_p
12108 = parser->in_unbraced_linkage_specification_p;
12109 parser->in_unbraced_linkage_specification_p = true;
12110 cp_parser_declaration (parser);
12111 parser->in_unbraced_linkage_specification_p
12112 = saved_in_unbraced_linkage_specification_p;
12115 /* We're done with the linkage-specification. */
12116 pop_lang_context ();
12119 /* Parse a static_assert-declaration.
12121 static_assert-declaration:
12122 static_assert ( constant-expression , string-literal ) ;
12124 If MEMBER_P, this static_assert is a class member. */
12126 static void
12127 cp_parser_static_assert(cp_parser *parser, bool member_p)
12129 tree condition;
12130 tree message;
12131 cp_token *token;
12132 location_t saved_loc;
12133 bool dummy;
12135 /* Peek at the `static_assert' token so we can keep track of exactly
12136 where the static assertion started. */
12137 token = cp_lexer_peek_token (parser->lexer);
12138 saved_loc = token->location;
12140 /* Look for the `static_assert' keyword. */
12141 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12142 RT_STATIC_ASSERT))
12143 return;
12145 /* We know we are in a static assertion; commit to any tentative
12146 parse. */
12147 if (cp_parser_parsing_tentatively (parser))
12148 cp_parser_commit_to_tentative_parse (parser);
12150 /* Parse the `(' starting the static assertion condition. */
12151 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12153 /* Parse the constant-expression. Allow a non-constant expression
12154 here in order to give better diagnostics in finish_static_assert. */
12155 condition =
12156 cp_parser_constant_expression (parser,
12157 /*allow_non_constant_p=*/true,
12158 /*non_constant_p=*/&dummy);
12160 /* Parse the separating `,'. */
12161 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12163 /* Parse the string-literal message. */
12164 message = cp_parser_string_literal (parser,
12165 /*translate=*/false,
12166 /*wide_ok=*/true);
12168 /* A `)' completes the static assertion. */
12169 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12170 cp_parser_skip_to_closing_parenthesis (parser,
12171 /*recovering=*/true,
12172 /*or_comma=*/false,
12173 /*consume_paren=*/true);
12175 /* A semicolon terminates the declaration. */
12176 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12178 /* Complete the static assertion, which may mean either processing
12179 the static assert now or saving it for template instantiation. */
12180 finish_static_assert (condition, message, saved_loc, member_p);
12183 /* Parse the expression in decltype ( expression ). */
12185 static tree
12186 cp_parser_decltype_expr (cp_parser *parser,
12187 bool &id_expression_or_member_access_p)
12189 cp_token *id_expr_start_token;
12190 tree expr;
12192 /* First, try parsing an id-expression. */
12193 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12194 cp_parser_parse_tentatively (parser);
12195 expr = cp_parser_id_expression (parser,
12196 /*template_keyword_p=*/false,
12197 /*check_dependency_p=*/true,
12198 /*template_p=*/NULL,
12199 /*declarator_p=*/false,
12200 /*optional_p=*/false);
12202 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12204 bool non_integral_constant_expression_p = false;
12205 tree id_expression = expr;
12206 cp_id_kind idk;
12207 const char *error_msg;
12209 if (identifier_p (expr))
12210 /* Lookup the name we got back from the id-expression. */
12211 expr = cp_parser_lookup_name_simple (parser, expr,
12212 id_expr_start_token->location);
12214 if (expr
12215 && expr != error_mark_node
12216 && TREE_CODE (expr) != TYPE_DECL
12217 && (TREE_CODE (expr) != BIT_NOT_EXPR
12218 || !TYPE_P (TREE_OPERAND (expr, 0)))
12219 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12221 /* Complete lookup of the id-expression. */
12222 expr = (finish_id_expression
12223 (id_expression, expr, parser->scope, &idk,
12224 /*integral_constant_expression_p=*/false,
12225 /*allow_non_integral_constant_expression_p=*/true,
12226 &non_integral_constant_expression_p,
12227 /*template_p=*/false,
12228 /*done=*/true,
12229 /*address_p=*/false,
12230 /*template_arg_p=*/false,
12231 &error_msg,
12232 id_expr_start_token->location));
12234 if (expr == error_mark_node)
12235 /* We found an id-expression, but it was something that we
12236 should not have found. This is an error, not something
12237 we can recover from, so note that we found an
12238 id-expression and we'll recover as gracefully as
12239 possible. */
12240 id_expression_or_member_access_p = true;
12243 if (expr
12244 && expr != error_mark_node
12245 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12246 /* We have an id-expression. */
12247 id_expression_or_member_access_p = true;
12250 if (!id_expression_or_member_access_p)
12252 /* Abort the id-expression parse. */
12253 cp_parser_abort_tentative_parse (parser);
12255 /* Parsing tentatively, again. */
12256 cp_parser_parse_tentatively (parser);
12258 /* Parse a class member access. */
12259 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12260 /*cast_p=*/false, /*decltype*/true,
12261 /*member_access_only_p=*/true, NULL);
12263 if (expr
12264 && expr != error_mark_node
12265 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12266 /* We have an id-expression. */
12267 id_expression_or_member_access_p = true;
12270 if (id_expression_or_member_access_p)
12271 /* We have parsed the complete id-expression or member access. */
12272 cp_parser_parse_definitely (parser);
12273 else
12275 /* Abort our attempt to parse an id-expression or member access
12276 expression. */
12277 cp_parser_abort_tentative_parse (parser);
12279 /* Parse a full expression. */
12280 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12281 /*decltype_p=*/true);
12284 return expr;
12287 /* Parse a `decltype' type. Returns the type.
12289 simple-type-specifier:
12290 decltype ( expression )
12291 C++14 proposal:
12292 decltype ( auto ) */
12294 static tree
12295 cp_parser_decltype (cp_parser *parser)
12297 tree expr;
12298 bool id_expression_or_member_access_p = false;
12299 const char *saved_message;
12300 bool saved_integral_constant_expression_p;
12301 bool saved_non_integral_constant_expression_p;
12302 bool saved_greater_than_is_operator_p;
12303 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12305 if (start_token->type == CPP_DECLTYPE)
12307 /* Already parsed. */
12308 cp_lexer_consume_token (parser->lexer);
12309 return start_token->u.value;
12312 /* Look for the `decltype' token. */
12313 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12314 return error_mark_node;
12316 /* Parse the opening `('. */
12317 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12318 return error_mark_node;
12320 /* decltype (auto) */
12321 if (cxx_dialect >= cxx14
12322 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12324 cp_lexer_consume_token (parser->lexer);
12325 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12326 return error_mark_node;
12327 expr = make_decltype_auto ();
12328 AUTO_IS_DECLTYPE (expr) = true;
12329 goto rewrite;
12332 /* Types cannot be defined in a `decltype' expression. Save away the
12333 old message. */
12334 saved_message = parser->type_definition_forbidden_message;
12336 /* And create the new one. */
12337 parser->type_definition_forbidden_message
12338 = G_("types may not be defined in %<decltype%> expressions");
12340 /* The restrictions on constant-expressions do not apply inside
12341 decltype expressions. */
12342 saved_integral_constant_expression_p
12343 = parser->integral_constant_expression_p;
12344 saved_non_integral_constant_expression_p
12345 = parser->non_integral_constant_expression_p;
12346 parser->integral_constant_expression_p = false;
12348 /* Within a parenthesized expression, a `>' token is always
12349 the greater-than operator. */
12350 saved_greater_than_is_operator_p
12351 = parser->greater_than_is_operator_p;
12352 parser->greater_than_is_operator_p = true;
12354 /* Do not actually evaluate the expression. */
12355 ++cp_unevaluated_operand;
12357 /* Do not warn about problems with the expression. */
12358 ++c_inhibit_evaluation_warnings;
12360 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12362 /* Go back to evaluating expressions. */
12363 --cp_unevaluated_operand;
12364 --c_inhibit_evaluation_warnings;
12366 /* The `>' token might be the end of a template-id or
12367 template-parameter-list now. */
12368 parser->greater_than_is_operator_p
12369 = saved_greater_than_is_operator_p;
12371 /* Restore the old message and the integral constant expression
12372 flags. */
12373 parser->type_definition_forbidden_message = saved_message;
12374 parser->integral_constant_expression_p
12375 = saved_integral_constant_expression_p;
12376 parser->non_integral_constant_expression_p
12377 = saved_non_integral_constant_expression_p;
12379 /* Parse to the closing `)'. */
12380 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12382 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12383 /*consume_paren=*/true);
12384 return error_mark_node;
12387 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12388 tf_warning_or_error);
12390 rewrite:
12391 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12392 it again. */
12393 start_token->type = CPP_DECLTYPE;
12394 start_token->u.value = expr;
12395 start_token->keyword = RID_MAX;
12396 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12398 return expr;
12401 /* Special member functions [gram.special] */
12403 /* Parse a conversion-function-id.
12405 conversion-function-id:
12406 operator conversion-type-id
12408 Returns an IDENTIFIER_NODE representing the operator. */
12410 static tree
12411 cp_parser_conversion_function_id (cp_parser* parser)
12413 tree type;
12414 tree saved_scope;
12415 tree saved_qualifying_scope;
12416 tree saved_object_scope;
12417 tree pushed_scope = NULL_TREE;
12419 /* Look for the `operator' token. */
12420 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12421 return error_mark_node;
12422 /* When we parse the conversion-type-id, the current scope will be
12423 reset. However, we need that information in able to look up the
12424 conversion function later, so we save it here. */
12425 saved_scope = parser->scope;
12426 saved_qualifying_scope = parser->qualifying_scope;
12427 saved_object_scope = parser->object_scope;
12428 /* We must enter the scope of the class so that the names of
12429 entities declared within the class are available in the
12430 conversion-type-id. For example, consider:
12432 struct S {
12433 typedef int I;
12434 operator I();
12437 S::operator I() { ... }
12439 In order to see that `I' is a type-name in the definition, we
12440 must be in the scope of `S'. */
12441 if (saved_scope)
12442 pushed_scope = push_scope (saved_scope);
12443 /* Parse the conversion-type-id. */
12444 type = cp_parser_conversion_type_id (parser);
12445 /* Leave the scope of the class, if any. */
12446 if (pushed_scope)
12447 pop_scope (pushed_scope);
12448 /* Restore the saved scope. */
12449 parser->scope = saved_scope;
12450 parser->qualifying_scope = saved_qualifying_scope;
12451 parser->object_scope = saved_object_scope;
12452 /* If the TYPE is invalid, indicate failure. */
12453 if (type == error_mark_node)
12454 return error_mark_node;
12455 return mangle_conv_op_name_for_type (type);
12458 /* Parse a conversion-type-id:
12460 conversion-type-id:
12461 type-specifier-seq conversion-declarator [opt]
12463 Returns the TYPE specified. */
12465 static tree
12466 cp_parser_conversion_type_id (cp_parser* parser)
12468 tree attributes;
12469 cp_decl_specifier_seq type_specifiers;
12470 cp_declarator *declarator;
12471 tree type_specified;
12472 const char *saved_message;
12474 /* Parse the attributes. */
12475 attributes = cp_parser_attributes_opt (parser);
12477 saved_message = parser->type_definition_forbidden_message;
12478 parser->type_definition_forbidden_message
12479 = G_("types may not be defined in a conversion-type-id");
12481 /* Parse the type-specifiers. */
12482 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12483 /*is_trailing_return=*/false,
12484 &type_specifiers);
12486 parser->type_definition_forbidden_message = saved_message;
12488 /* If that didn't work, stop. */
12489 if (type_specifiers.type == error_mark_node)
12490 return error_mark_node;
12491 /* Parse the conversion-declarator. */
12492 declarator = cp_parser_conversion_declarator_opt (parser);
12494 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12495 /*initialized=*/0, &attributes);
12496 if (attributes)
12497 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12499 /* Don't give this error when parsing tentatively. This happens to
12500 work because we always parse this definitively once. */
12501 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12502 && type_uses_auto (type_specified))
12504 if (cxx_dialect < cxx14)
12506 error ("invalid use of %<auto%> in conversion operator");
12507 return error_mark_node;
12509 else if (template_parm_scope_p ())
12510 warning (0, "use of %<auto%> in member template "
12511 "conversion operator can never be deduced");
12514 return type_specified;
12517 /* Parse an (optional) conversion-declarator.
12519 conversion-declarator:
12520 ptr-operator conversion-declarator [opt]
12524 static cp_declarator *
12525 cp_parser_conversion_declarator_opt (cp_parser* parser)
12527 enum tree_code code;
12528 tree class_type, std_attributes = NULL_TREE;
12529 cp_cv_quals cv_quals;
12531 /* We don't know if there's a ptr-operator next, or not. */
12532 cp_parser_parse_tentatively (parser);
12533 /* Try the ptr-operator. */
12534 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12535 &std_attributes);
12536 /* If it worked, look for more conversion-declarators. */
12537 if (cp_parser_parse_definitely (parser))
12539 cp_declarator *declarator;
12541 /* Parse another optional declarator. */
12542 declarator = cp_parser_conversion_declarator_opt (parser);
12544 declarator = cp_parser_make_indirect_declarator
12545 (code, class_type, cv_quals, declarator, std_attributes);
12547 return declarator;
12550 return NULL;
12553 /* Parse an (optional) ctor-initializer.
12555 ctor-initializer:
12556 : mem-initializer-list
12558 Returns TRUE iff the ctor-initializer was actually present. */
12560 static bool
12561 cp_parser_ctor_initializer_opt (cp_parser* parser)
12563 /* If the next token is not a `:', then there is no
12564 ctor-initializer. */
12565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12567 /* Do default initialization of any bases and members. */
12568 if (DECL_CONSTRUCTOR_P (current_function_decl))
12569 finish_mem_initializers (NULL_TREE);
12571 return false;
12574 /* Consume the `:' token. */
12575 cp_lexer_consume_token (parser->lexer);
12576 /* And the mem-initializer-list. */
12577 cp_parser_mem_initializer_list (parser);
12579 return true;
12582 /* Parse a mem-initializer-list.
12584 mem-initializer-list:
12585 mem-initializer ... [opt]
12586 mem-initializer ... [opt] , mem-initializer-list */
12588 static void
12589 cp_parser_mem_initializer_list (cp_parser* parser)
12591 tree mem_initializer_list = NULL_TREE;
12592 tree target_ctor = error_mark_node;
12593 cp_token *token = cp_lexer_peek_token (parser->lexer);
12595 /* Let the semantic analysis code know that we are starting the
12596 mem-initializer-list. */
12597 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12598 error_at (token->location,
12599 "only constructors take member initializers");
12601 /* Loop through the list. */
12602 while (true)
12604 tree mem_initializer;
12606 token = cp_lexer_peek_token (parser->lexer);
12607 /* Parse the mem-initializer. */
12608 mem_initializer = cp_parser_mem_initializer (parser);
12609 /* If the next token is a `...', we're expanding member initializers. */
12610 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12612 /* Consume the `...'. */
12613 cp_lexer_consume_token (parser->lexer);
12615 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12616 can be expanded but members cannot. */
12617 if (mem_initializer != error_mark_node
12618 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12620 error_at (token->location,
12621 "cannot expand initializer for member %<%D%>",
12622 TREE_PURPOSE (mem_initializer));
12623 mem_initializer = error_mark_node;
12626 /* Construct the pack expansion type. */
12627 if (mem_initializer != error_mark_node)
12628 mem_initializer = make_pack_expansion (mem_initializer);
12630 if (target_ctor != error_mark_node
12631 && mem_initializer != error_mark_node)
12633 error ("mem-initializer for %qD follows constructor delegation",
12634 TREE_PURPOSE (mem_initializer));
12635 mem_initializer = error_mark_node;
12637 /* Look for a target constructor. */
12638 if (mem_initializer != error_mark_node
12639 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12640 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12642 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12643 if (mem_initializer_list)
12645 error ("constructor delegation follows mem-initializer for %qD",
12646 TREE_PURPOSE (mem_initializer_list));
12647 mem_initializer = error_mark_node;
12649 target_ctor = mem_initializer;
12651 /* Add it to the list, unless it was erroneous. */
12652 if (mem_initializer != error_mark_node)
12654 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12655 mem_initializer_list = mem_initializer;
12657 /* If the next token is not a `,', we're done. */
12658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12659 break;
12660 /* Consume the `,' token. */
12661 cp_lexer_consume_token (parser->lexer);
12664 /* Perform semantic analysis. */
12665 if (DECL_CONSTRUCTOR_P (current_function_decl))
12666 finish_mem_initializers (mem_initializer_list);
12669 /* Parse a mem-initializer.
12671 mem-initializer:
12672 mem-initializer-id ( expression-list [opt] )
12673 mem-initializer-id braced-init-list
12675 GNU extension:
12677 mem-initializer:
12678 ( expression-list [opt] )
12680 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12681 class) or FIELD_DECL (for a non-static data member) to initialize;
12682 the TREE_VALUE is the expression-list. An empty initialization
12683 list is represented by void_list_node. */
12685 static tree
12686 cp_parser_mem_initializer (cp_parser* parser)
12688 tree mem_initializer_id;
12689 tree expression_list;
12690 tree member;
12691 cp_token *token = cp_lexer_peek_token (parser->lexer);
12693 /* Find out what is being initialized. */
12694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12696 permerror (token->location,
12697 "anachronistic old-style base class initializer");
12698 mem_initializer_id = NULL_TREE;
12700 else
12702 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12703 if (mem_initializer_id == error_mark_node)
12704 return mem_initializer_id;
12706 member = expand_member_init (mem_initializer_id);
12707 if (member && !DECL_P (member))
12708 in_base_initializer = 1;
12710 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12712 bool expr_non_constant_p;
12713 cp_lexer_set_source_position (parser->lexer);
12714 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12715 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12716 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12717 expression_list = build_tree_list (NULL_TREE, expression_list);
12719 else
12721 vec<tree, va_gc> *vec;
12722 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12723 /*cast_p=*/false,
12724 /*allow_expansion_p=*/true,
12725 /*non_constant_p=*/NULL);
12726 if (vec == NULL)
12727 return error_mark_node;
12728 expression_list = build_tree_list_vec (vec);
12729 release_tree_vector (vec);
12732 if (expression_list == error_mark_node)
12733 return error_mark_node;
12734 if (!expression_list)
12735 expression_list = void_type_node;
12737 in_base_initializer = 0;
12739 return member ? build_tree_list (member, expression_list) : error_mark_node;
12742 /* Parse a mem-initializer-id.
12744 mem-initializer-id:
12745 :: [opt] nested-name-specifier [opt] class-name
12746 identifier
12748 Returns a TYPE indicating the class to be initializer for the first
12749 production. Returns an IDENTIFIER_NODE indicating the data member
12750 to be initialized for the second production. */
12752 static tree
12753 cp_parser_mem_initializer_id (cp_parser* parser)
12755 bool global_scope_p;
12756 bool nested_name_specifier_p;
12757 bool template_p = false;
12758 tree id;
12760 cp_token *token = cp_lexer_peek_token (parser->lexer);
12762 /* `typename' is not allowed in this context ([temp.res]). */
12763 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12765 error_at (token->location,
12766 "keyword %<typename%> not allowed in this context (a qualified "
12767 "member initializer is implicitly a type)");
12768 cp_lexer_consume_token (parser->lexer);
12770 /* Look for the optional `::' operator. */
12771 global_scope_p
12772 = (cp_parser_global_scope_opt (parser,
12773 /*current_scope_valid_p=*/false)
12774 != NULL_TREE);
12775 /* Look for the optional nested-name-specifier. The simplest way to
12776 implement:
12778 [temp.res]
12780 The keyword `typename' is not permitted in a base-specifier or
12781 mem-initializer; in these contexts a qualified name that
12782 depends on a template-parameter is implicitly assumed to be a
12783 type name.
12785 is to assume that we have seen the `typename' keyword at this
12786 point. */
12787 nested_name_specifier_p
12788 = (cp_parser_nested_name_specifier_opt (parser,
12789 /*typename_keyword_p=*/true,
12790 /*check_dependency_p=*/true,
12791 /*type_p=*/true,
12792 /*is_declaration=*/true)
12793 != NULL_TREE);
12794 if (nested_name_specifier_p)
12795 template_p = cp_parser_optional_template_keyword (parser);
12796 /* If there is a `::' operator or a nested-name-specifier, then we
12797 are definitely looking for a class-name. */
12798 if (global_scope_p || nested_name_specifier_p)
12799 return cp_parser_class_name (parser,
12800 /*typename_keyword_p=*/true,
12801 /*template_keyword_p=*/template_p,
12802 typename_type,
12803 /*check_dependency_p=*/true,
12804 /*class_head_p=*/false,
12805 /*is_declaration=*/true);
12806 /* Otherwise, we could also be looking for an ordinary identifier. */
12807 cp_parser_parse_tentatively (parser);
12808 /* Try a class-name. */
12809 id = cp_parser_class_name (parser,
12810 /*typename_keyword_p=*/true,
12811 /*template_keyword_p=*/false,
12812 none_type,
12813 /*check_dependency_p=*/true,
12814 /*class_head_p=*/false,
12815 /*is_declaration=*/true);
12816 /* If we found one, we're done. */
12817 if (cp_parser_parse_definitely (parser))
12818 return id;
12819 /* Otherwise, look for an ordinary identifier. */
12820 return cp_parser_identifier (parser);
12823 /* Overloading [gram.over] */
12825 /* Parse an operator-function-id.
12827 operator-function-id:
12828 operator operator
12830 Returns an IDENTIFIER_NODE for the operator which is a
12831 human-readable spelling of the identifier, e.g., `operator +'. */
12833 static tree
12834 cp_parser_operator_function_id (cp_parser* parser)
12836 /* Look for the `operator' keyword. */
12837 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12838 return error_mark_node;
12839 /* And then the name of the operator itself. */
12840 return cp_parser_operator (parser);
12843 /* Return an identifier node for a user-defined literal operator.
12844 The suffix identifier is chained to the operator name identifier. */
12846 static tree
12847 cp_literal_operator_id (const char* name)
12849 tree identifier;
12850 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12851 + strlen (name) + 10);
12852 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12853 identifier = get_identifier (buffer);
12855 return identifier;
12858 /* Parse an operator.
12860 operator:
12861 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12862 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12863 || ++ -- , ->* -> () []
12865 GNU Extensions:
12867 operator:
12868 <? >? <?= >?=
12870 Returns an IDENTIFIER_NODE for the operator which is a
12871 human-readable spelling of the identifier, e.g., `operator +'. */
12873 static tree
12874 cp_parser_operator (cp_parser* parser)
12876 tree id = NULL_TREE;
12877 cp_token *token;
12878 bool utf8 = false;
12880 /* Peek at the next token. */
12881 token = cp_lexer_peek_token (parser->lexer);
12882 /* Figure out which operator we have. */
12883 switch (token->type)
12885 case CPP_KEYWORD:
12887 enum tree_code op;
12889 /* The keyword should be either `new' or `delete'. */
12890 if (token->keyword == RID_NEW)
12891 op = NEW_EXPR;
12892 else if (token->keyword == RID_DELETE)
12893 op = DELETE_EXPR;
12894 else
12895 break;
12897 /* Consume the `new' or `delete' token. */
12898 cp_lexer_consume_token (parser->lexer);
12900 /* Peek at the next token. */
12901 token = cp_lexer_peek_token (parser->lexer);
12902 /* If it's a `[' token then this is the array variant of the
12903 operator. */
12904 if (token->type == CPP_OPEN_SQUARE)
12906 /* Consume the `[' token. */
12907 cp_lexer_consume_token (parser->lexer);
12908 /* Look for the `]' token. */
12909 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12910 id = ansi_opname (op == NEW_EXPR
12911 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12913 /* Otherwise, we have the non-array variant. */
12914 else
12915 id = ansi_opname (op);
12917 return id;
12920 case CPP_PLUS:
12921 id = ansi_opname (PLUS_EXPR);
12922 break;
12924 case CPP_MINUS:
12925 id = ansi_opname (MINUS_EXPR);
12926 break;
12928 case CPP_MULT:
12929 id = ansi_opname (MULT_EXPR);
12930 break;
12932 case CPP_DIV:
12933 id = ansi_opname (TRUNC_DIV_EXPR);
12934 break;
12936 case CPP_MOD:
12937 id = ansi_opname (TRUNC_MOD_EXPR);
12938 break;
12940 case CPP_XOR:
12941 id = ansi_opname (BIT_XOR_EXPR);
12942 break;
12944 case CPP_AND:
12945 id = ansi_opname (BIT_AND_EXPR);
12946 break;
12948 case CPP_OR:
12949 id = ansi_opname (BIT_IOR_EXPR);
12950 break;
12952 case CPP_COMPL:
12953 id = ansi_opname (BIT_NOT_EXPR);
12954 break;
12956 case CPP_NOT:
12957 id = ansi_opname (TRUTH_NOT_EXPR);
12958 break;
12960 case CPP_EQ:
12961 id = ansi_assopname (NOP_EXPR);
12962 break;
12964 case CPP_LESS:
12965 id = ansi_opname (LT_EXPR);
12966 break;
12968 case CPP_GREATER:
12969 id = ansi_opname (GT_EXPR);
12970 break;
12972 case CPP_PLUS_EQ:
12973 id = ansi_assopname (PLUS_EXPR);
12974 break;
12976 case CPP_MINUS_EQ:
12977 id = ansi_assopname (MINUS_EXPR);
12978 break;
12980 case CPP_MULT_EQ:
12981 id = ansi_assopname (MULT_EXPR);
12982 break;
12984 case CPP_DIV_EQ:
12985 id = ansi_assopname (TRUNC_DIV_EXPR);
12986 break;
12988 case CPP_MOD_EQ:
12989 id = ansi_assopname (TRUNC_MOD_EXPR);
12990 break;
12992 case CPP_XOR_EQ:
12993 id = ansi_assopname (BIT_XOR_EXPR);
12994 break;
12996 case CPP_AND_EQ:
12997 id = ansi_assopname (BIT_AND_EXPR);
12998 break;
13000 case CPP_OR_EQ:
13001 id = ansi_assopname (BIT_IOR_EXPR);
13002 break;
13004 case CPP_LSHIFT:
13005 id = ansi_opname (LSHIFT_EXPR);
13006 break;
13008 case CPP_RSHIFT:
13009 id = ansi_opname (RSHIFT_EXPR);
13010 break;
13012 case CPP_LSHIFT_EQ:
13013 id = ansi_assopname (LSHIFT_EXPR);
13014 break;
13016 case CPP_RSHIFT_EQ:
13017 id = ansi_assopname (RSHIFT_EXPR);
13018 break;
13020 case CPP_EQ_EQ:
13021 id = ansi_opname (EQ_EXPR);
13022 break;
13024 case CPP_NOT_EQ:
13025 id = ansi_opname (NE_EXPR);
13026 break;
13028 case CPP_LESS_EQ:
13029 id = ansi_opname (LE_EXPR);
13030 break;
13032 case CPP_GREATER_EQ:
13033 id = ansi_opname (GE_EXPR);
13034 break;
13036 case CPP_AND_AND:
13037 id = ansi_opname (TRUTH_ANDIF_EXPR);
13038 break;
13040 case CPP_OR_OR:
13041 id = ansi_opname (TRUTH_ORIF_EXPR);
13042 break;
13044 case CPP_PLUS_PLUS:
13045 id = ansi_opname (POSTINCREMENT_EXPR);
13046 break;
13048 case CPP_MINUS_MINUS:
13049 id = ansi_opname (PREDECREMENT_EXPR);
13050 break;
13052 case CPP_COMMA:
13053 id = ansi_opname (COMPOUND_EXPR);
13054 break;
13056 case CPP_DEREF_STAR:
13057 id = ansi_opname (MEMBER_REF);
13058 break;
13060 case CPP_DEREF:
13061 id = ansi_opname (COMPONENT_REF);
13062 break;
13064 case CPP_OPEN_PAREN:
13065 /* Consume the `('. */
13066 cp_lexer_consume_token (parser->lexer);
13067 /* Look for the matching `)'. */
13068 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13069 return ansi_opname (CALL_EXPR);
13071 case CPP_OPEN_SQUARE:
13072 /* Consume the `['. */
13073 cp_lexer_consume_token (parser->lexer);
13074 /* Look for the matching `]'. */
13075 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13076 return ansi_opname (ARRAY_REF);
13078 case CPP_UTF8STRING:
13079 case CPP_UTF8STRING_USERDEF:
13080 utf8 = true;
13081 case CPP_STRING:
13082 case CPP_WSTRING:
13083 case CPP_STRING16:
13084 case CPP_STRING32:
13085 case CPP_STRING_USERDEF:
13086 case CPP_WSTRING_USERDEF:
13087 case CPP_STRING16_USERDEF:
13088 case CPP_STRING32_USERDEF:
13090 tree str, string_tree;
13091 int sz, len;
13093 if (cxx_dialect == cxx98)
13094 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13096 /* Consume the string. */
13097 str = cp_parser_string_literal (parser, /*translate=*/true,
13098 /*wide_ok=*/true, /*lookup_udlit=*/false);
13099 if (str == error_mark_node)
13100 return error_mark_node;
13101 else if (TREE_CODE (str) == USERDEF_LITERAL)
13103 string_tree = USERDEF_LITERAL_VALUE (str);
13104 id = USERDEF_LITERAL_SUFFIX_ID (str);
13106 else
13108 string_tree = str;
13109 /* Look for the suffix identifier. */
13110 token = cp_lexer_peek_token (parser->lexer);
13111 if (token->type == CPP_NAME)
13112 id = cp_parser_identifier (parser);
13113 else if (token->type == CPP_KEYWORD)
13115 error ("unexpected keyword;"
13116 " remove space between quotes and suffix identifier");
13117 return error_mark_node;
13119 else
13121 error ("expected suffix identifier");
13122 return error_mark_node;
13125 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13126 (TREE_TYPE (TREE_TYPE (string_tree))));
13127 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13128 if (len != 0)
13130 error ("expected empty string after %<operator%> keyword");
13131 return error_mark_node;
13133 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13134 != char_type_node)
13136 error ("invalid encoding prefix in literal operator");
13137 return error_mark_node;
13139 if (id != error_mark_node)
13141 const char *name = IDENTIFIER_POINTER (id);
13142 id = cp_literal_operator_id (name);
13144 return id;
13147 default:
13148 /* Anything else is an error. */
13149 break;
13152 /* If we have selected an identifier, we need to consume the
13153 operator token. */
13154 if (id)
13155 cp_lexer_consume_token (parser->lexer);
13156 /* Otherwise, no valid operator name was present. */
13157 else
13159 cp_parser_error (parser, "expected operator");
13160 id = error_mark_node;
13163 return id;
13166 /* Parse a template-declaration.
13168 template-declaration:
13169 export [opt] template < template-parameter-list > declaration
13171 If MEMBER_P is TRUE, this template-declaration occurs within a
13172 class-specifier.
13174 The grammar rule given by the standard isn't correct. What
13175 is really meant is:
13177 template-declaration:
13178 export [opt] template-parameter-list-seq
13179 decl-specifier-seq [opt] init-declarator [opt] ;
13180 export [opt] template-parameter-list-seq
13181 function-definition
13183 template-parameter-list-seq:
13184 template-parameter-list-seq [opt]
13185 template < template-parameter-list > */
13187 static void
13188 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13190 /* Check for `export'. */
13191 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13193 /* Consume the `export' token. */
13194 cp_lexer_consume_token (parser->lexer);
13195 /* Warn that we do not support `export'. */
13196 warning (0, "keyword %<export%> not implemented, and will be ignored");
13199 cp_parser_template_declaration_after_export (parser, member_p);
13202 /* Parse a template-parameter-list.
13204 template-parameter-list:
13205 template-parameter
13206 template-parameter-list , template-parameter
13208 Returns a TREE_LIST. Each node represents a template parameter.
13209 The nodes are connected via their TREE_CHAINs. */
13211 static tree
13212 cp_parser_template_parameter_list (cp_parser* parser)
13214 tree parameter_list = NULL_TREE;
13216 begin_template_parm_list ();
13218 /* The loop below parses the template parms. We first need to know
13219 the total number of template parms to be able to compute proper
13220 canonical types of each dependent type. So after the loop, when
13221 we know the total number of template parms,
13222 end_template_parm_list computes the proper canonical types and
13223 fixes up the dependent types accordingly. */
13224 while (true)
13226 tree parameter;
13227 bool is_non_type;
13228 bool is_parameter_pack;
13229 location_t parm_loc;
13231 /* Parse the template-parameter. */
13232 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13233 parameter = cp_parser_template_parameter (parser,
13234 &is_non_type,
13235 &is_parameter_pack);
13236 /* Add it to the list. */
13237 if (parameter != error_mark_node)
13238 parameter_list = process_template_parm (parameter_list,
13239 parm_loc,
13240 parameter,
13241 is_non_type,
13242 is_parameter_pack);
13243 else
13245 tree err_parm = build_tree_list (parameter, parameter);
13246 parameter_list = chainon (parameter_list, err_parm);
13249 /* If the next token is not a `,', we're done. */
13250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13251 break;
13252 /* Otherwise, consume the `,' token. */
13253 cp_lexer_consume_token (parser->lexer);
13256 return end_template_parm_list (parameter_list);
13259 /* Parse a template-parameter.
13261 template-parameter:
13262 type-parameter
13263 parameter-declaration
13265 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13266 the parameter. The TREE_PURPOSE is the default value, if any.
13267 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13268 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13269 set to true iff this parameter is a parameter pack. */
13271 static tree
13272 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13273 bool *is_parameter_pack)
13275 cp_token *token;
13276 cp_parameter_declarator *parameter_declarator;
13277 cp_declarator *id_declarator;
13278 tree parm;
13280 /* Assume it is a type parameter or a template parameter. */
13281 *is_non_type = false;
13282 /* Assume it not a parameter pack. */
13283 *is_parameter_pack = false;
13284 /* Peek at the next token. */
13285 token = cp_lexer_peek_token (parser->lexer);
13286 /* If it is `class' or `template', we have a type-parameter. */
13287 if (token->keyword == RID_TEMPLATE)
13288 return cp_parser_type_parameter (parser, is_parameter_pack);
13289 /* If it is `class' or `typename' we do not know yet whether it is a
13290 type parameter or a non-type parameter. Consider:
13292 template <typename T, typename T::X X> ...
13296 template <class C, class D*> ...
13298 Here, the first parameter is a type parameter, and the second is
13299 a non-type parameter. We can tell by looking at the token after
13300 the identifier -- if it is a `,', `=', or `>' then we have a type
13301 parameter. */
13302 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13304 /* Peek at the token after `class' or `typename'. */
13305 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13306 /* If it's an ellipsis, we have a template type parameter
13307 pack. */
13308 if (token->type == CPP_ELLIPSIS)
13309 return cp_parser_type_parameter (parser, is_parameter_pack);
13310 /* If it's an identifier, skip it. */
13311 if (token->type == CPP_NAME)
13312 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13313 /* Now, see if the token looks like the end of a template
13314 parameter. */
13315 if (token->type == CPP_COMMA
13316 || token->type == CPP_EQ
13317 || token->type == CPP_GREATER)
13318 return cp_parser_type_parameter (parser, is_parameter_pack);
13321 /* Otherwise, it is a non-type parameter.
13323 [temp.param]
13325 When parsing a default template-argument for a non-type
13326 template-parameter, the first non-nested `>' is taken as the end
13327 of the template parameter-list rather than a greater-than
13328 operator. */
13329 *is_non_type = true;
13330 parameter_declarator
13331 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13332 /*parenthesized_p=*/NULL);
13334 if (!parameter_declarator)
13335 return error_mark_node;
13337 /* If the parameter declaration is marked as a parameter pack, set
13338 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13339 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13340 grokdeclarator. */
13341 if (parameter_declarator->declarator
13342 && parameter_declarator->declarator->parameter_pack_p)
13344 *is_parameter_pack = true;
13345 parameter_declarator->declarator->parameter_pack_p = false;
13348 if (parameter_declarator->default_argument)
13350 /* Can happen in some cases of erroneous input (c++/34892). */
13351 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13352 /* Consume the `...' for better error recovery. */
13353 cp_lexer_consume_token (parser->lexer);
13355 /* If the next token is an ellipsis, and we don't already have it
13356 marked as a parameter pack, then we have a parameter pack (that
13357 has no declarator). */
13358 else if (!*is_parameter_pack
13359 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13360 && (declarator_can_be_parameter_pack
13361 (parameter_declarator->declarator)))
13363 /* Consume the `...'. */
13364 cp_lexer_consume_token (parser->lexer);
13365 maybe_warn_variadic_templates ();
13367 *is_parameter_pack = true;
13369 /* We might end up with a pack expansion as the type of the non-type
13370 template parameter, in which case this is a non-type template
13371 parameter pack. */
13372 else if (parameter_declarator->decl_specifiers.type
13373 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13375 *is_parameter_pack = true;
13376 parameter_declarator->decl_specifiers.type =
13377 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13380 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13382 /* Parameter packs cannot have default arguments. However, a
13383 user may try to do so, so we'll parse them and give an
13384 appropriate diagnostic here. */
13386 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13388 /* Find the name of the parameter pack. */
13389 id_declarator = parameter_declarator->declarator;
13390 while (id_declarator && id_declarator->kind != cdk_id)
13391 id_declarator = id_declarator->declarator;
13393 if (id_declarator && id_declarator->kind == cdk_id)
13394 error_at (start_token->location,
13395 "template parameter pack %qD cannot have a default argument",
13396 id_declarator->u.id.unqualified_name);
13397 else
13398 error_at (start_token->location,
13399 "template parameter pack cannot have a default argument");
13401 /* Parse the default argument, but throw away the result. */
13402 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13405 parm = grokdeclarator (parameter_declarator->declarator,
13406 &parameter_declarator->decl_specifiers,
13407 TPARM, /*initialized=*/0,
13408 /*attrlist=*/NULL);
13409 if (parm == error_mark_node)
13410 return error_mark_node;
13412 return build_tree_list (parameter_declarator->default_argument, parm);
13415 /* Parse a type-parameter.
13417 type-parameter:
13418 class identifier [opt]
13419 class identifier [opt] = type-id
13420 typename identifier [opt]
13421 typename identifier [opt] = type-id
13422 template < template-parameter-list > class identifier [opt]
13423 template < template-parameter-list > class identifier [opt]
13424 = id-expression
13426 GNU Extension (variadic templates):
13428 type-parameter:
13429 class ... identifier [opt]
13430 typename ... identifier [opt]
13432 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13433 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13434 the declaration of the parameter.
13436 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13438 static tree
13439 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13441 cp_token *token;
13442 tree parameter;
13444 /* Look for a keyword to tell us what kind of parameter this is. */
13445 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13446 if (!token)
13447 return error_mark_node;
13449 switch (token->keyword)
13451 case RID_CLASS:
13452 case RID_TYPENAME:
13454 tree identifier;
13455 tree default_argument;
13457 /* If the next token is an ellipsis, we have a template
13458 argument pack. */
13459 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13461 /* Consume the `...' token. */
13462 cp_lexer_consume_token (parser->lexer);
13463 maybe_warn_variadic_templates ();
13465 *is_parameter_pack = true;
13468 /* If the next token is an identifier, then it names the
13469 parameter. */
13470 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13471 identifier = cp_parser_identifier (parser);
13472 else
13473 identifier = NULL_TREE;
13475 /* Create the parameter. */
13476 parameter = finish_template_type_parm (class_type_node, identifier);
13478 /* If the next token is an `=', we have a default argument. */
13479 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13481 /* Consume the `=' token. */
13482 cp_lexer_consume_token (parser->lexer);
13483 /* Parse the default-argument. */
13484 push_deferring_access_checks (dk_no_deferred);
13485 default_argument = cp_parser_type_id (parser);
13487 /* Template parameter packs cannot have default
13488 arguments. */
13489 if (*is_parameter_pack)
13491 if (identifier)
13492 error_at (token->location,
13493 "template parameter pack %qD cannot have a "
13494 "default argument", identifier);
13495 else
13496 error_at (token->location,
13497 "template parameter packs cannot have "
13498 "default arguments");
13499 default_argument = NULL_TREE;
13501 else if (check_for_bare_parameter_packs (default_argument))
13502 default_argument = error_mark_node;
13503 pop_deferring_access_checks ();
13505 else
13506 default_argument = NULL_TREE;
13508 /* Create the combined representation of the parameter and the
13509 default argument. */
13510 parameter = build_tree_list (default_argument, parameter);
13512 break;
13514 case RID_TEMPLATE:
13516 tree identifier;
13517 tree default_argument;
13519 /* Look for the `<'. */
13520 cp_parser_require (parser, CPP_LESS, RT_LESS);
13521 /* Parse the template-parameter-list. */
13522 cp_parser_template_parameter_list (parser);
13523 /* Look for the `>'. */
13524 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13525 /* Look for the `class' or 'typename' keywords. */
13526 cp_parser_type_parameter_key (parser);
13527 /* If the next token is an ellipsis, we have a template
13528 argument pack. */
13529 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13531 /* Consume the `...' token. */
13532 cp_lexer_consume_token (parser->lexer);
13533 maybe_warn_variadic_templates ();
13535 *is_parameter_pack = true;
13537 /* If the next token is an `=', then there is a
13538 default-argument. If the next token is a `>', we are at
13539 the end of the parameter-list. If the next token is a `,',
13540 then we are at the end of this parameter. */
13541 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13542 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13543 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13545 identifier = cp_parser_identifier (parser);
13546 /* Treat invalid names as if the parameter were nameless. */
13547 if (identifier == error_mark_node)
13548 identifier = NULL_TREE;
13550 else
13551 identifier = NULL_TREE;
13553 /* Create the template parameter. */
13554 parameter = finish_template_template_parm (class_type_node,
13555 identifier);
13557 /* If the next token is an `=', then there is a
13558 default-argument. */
13559 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13561 bool is_template;
13563 /* Consume the `='. */
13564 cp_lexer_consume_token (parser->lexer);
13565 /* Parse the id-expression. */
13566 push_deferring_access_checks (dk_no_deferred);
13567 /* save token before parsing the id-expression, for error
13568 reporting */
13569 token = cp_lexer_peek_token (parser->lexer);
13570 default_argument
13571 = cp_parser_id_expression (parser,
13572 /*template_keyword_p=*/false,
13573 /*check_dependency_p=*/true,
13574 /*template_p=*/&is_template,
13575 /*declarator_p=*/false,
13576 /*optional_p=*/false);
13577 if (TREE_CODE (default_argument) == TYPE_DECL)
13578 /* If the id-expression was a template-id that refers to
13579 a template-class, we already have the declaration here,
13580 so no further lookup is needed. */
13582 else
13583 /* Look up the name. */
13584 default_argument
13585 = cp_parser_lookup_name (parser, default_argument,
13586 none_type,
13587 /*is_template=*/is_template,
13588 /*is_namespace=*/false,
13589 /*check_dependency=*/true,
13590 /*ambiguous_decls=*/NULL,
13591 token->location);
13592 /* See if the default argument is valid. */
13593 default_argument
13594 = check_template_template_default_arg (default_argument);
13596 /* Template parameter packs cannot have default
13597 arguments. */
13598 if (*is_parameter_pack)
13600 if (identifier)
13601 error_at (token->location,
13602 "template parameter pack %qD cannot "
13603 "have a default argument",
13604 identifier);
13605 else
13606 error_at (token->location, "template parameter packs cannot "
13607 "have default arguments");
13608 default_argument = NULL_TREE;
13610 pop_deferring_access_checks ();
13612 else
13613 default_argument = NULL_TREE;
13615 /* Create the combined representation of the parameter and the
13616 default argument. */
13617 parameter = build_tree_list (default_argument, parameter);
13619 break;
13621 default:
13622 gcc_unreachable ();
13623 break;
13626 return parameter;
13629 /* Parse a template-id.
13631 template-id:
13632 template-name < template-argument-list [opt] >
13634 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13635 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13636 returned. Otherwise, if the template-name names a function, or set
13637 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13638 names a class, returns a TYPE_DECL for the specialization.
13640 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13641 uninstantiated templates. */
13643 static tree
13644 cp_parser_template_id (cp_parser *parser,
13645 bool template_keyword_p,
13646 bool check_dependency_p,
13647 enum tag_types tag_type,
13648 bool is_declaration)
13650 int i;
13651 tree templ;
13652 tree arguments;
13653 tree template_id;
13654 cp_token_position start_of_id = 0;
13655 deferred_access_check *chk;
13656 vec<deferred_access_check, va_gc> *access_check;
13657 cp_token *next_token = NULL, *next_token_2 = NULL;
13658 bool is_identifier;
13660 /* If the next token corresponds to a template-id, there is no need
13661 to reparse it. */
13662 next_token = cp_lexer_peek_token (parser->lexer);
13663 if (next_token->type == CPP_TEMPLATE_ID)
13665 struct tree_check *check_value;
13667 /* Get the stored value. */
13668 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13669 /* Perform any access checks that were deferred. */
13670 access_check = check_value->checks;
13671 if (access_check)
13673 FOR_EACH_VEC_ELT (*access_check, i, chk)
13674 perform_or_defer_access_check (chk->binfo,
13675 chk->decl,
13676 chk->diag_decl,
13677 tf_warning_or_error);
13679 /* Return the stored value. */
13680 return check_value->value;
13683 /* Avoid performing name lookup if there is no possibility of
13684 finding a template-id. */
13685 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13686 || (next_token->type == CPP_NAME
13687 && !cp_parser_nth_token_starts_template_argument_list_p
13688 (parser, 2)))
13690 cp_parser_error (parser, "expected template-id");
13691 return error_mark_node;
13694 /* Remember where the template-id starts. */
13695 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13696 start_of_id = cp_lexer_token_position (parser->lexer, false);
13698 push_deferring_access_checks (dk_deferred);
13700 /* Parse the template-name. */
13701 is_identifier = false;
13702 templ = cp_parser_template_name (parser, template_keyword_p,
13703 check_dependency_p,
13704 is_declaration,
13705 tag_type,
13706 &is_identifier);
13707 if (templ == error_mark_node || is_identifier)
13709 pop_deferring_access_checks ();
13710 return templ;
13713 /* If we find the sequence `[:' after a template-name, it's probably
13714 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13715 parse correctly the argument list. */
13716 next_token = cp_lexer_peek_token (parser->lexer);
13717 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13718 if (next_token->type == CPP_OPEN_SQUARE
13719 && next_token->flags & DIGRAPH
13720 && next_token_2->type == CPP_COLON
13721 && !(next_token_2->flags & PREV_WHITE))
13723 cp_parser_parse_tentatively (parser);
13724 /* Change `:' into `::'. */
13725 next_token_2->type = CPP_SCOPE;
13726 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13727 CPP_LESS. */
13728 cp_lexer_consume_token (parser->lexer);
13730 /* Parse the arguments. */
13731 arguments = cp_parser_enclosed_template_argument_list (parser);
13732 if (!cp_parser_parse_definitely (parser))
13734 /* If we couldn't parse an argument list, then we revert our changes
13735 and return simply an error. Maybe this is not a template-id
13736 after all. */
13737 next_token_2->type = CPP_COLON;
13738 cp_parser_error (parser, "expected %<<%>");
13739 pop_deferring_access_checks ();
13740 return error_mark_node;
13742 /* Otherwise, emit an error about the invalid digraph, but continue
13743 parsing because we got our argument list. */
13744 if (permerror (next_token->location,
13745 "%<<::%> cannot begin a template-argument list"))
13747 static bool hint = false;
13748 inform (next_token->location,
13749 "%<<:%> is an alternate spelling for %<[%>."
13750 " Insert whitespace between %<<%> and %<::%>");
13751 if (!hint && !flag_permissive)
13753 inform (next_token->location, "(if you use %<-fpermissive%> "
13754 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13755 "accept your code)");
13756 hint = true;
13760 else
13762 /* Look for the `<' that starts the template-argument-list. */
13763 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13765 pop_deferring_access_checks ();
13766 return error_mark_node;
13768 /* Parse the arguments. */
13769 arguments = cp_parser_enclosed_template_argument_list (parser);
13772 /* Build a representation of the specialization. */
13773 if (identifier_p (templ))
13774 template_id = build_min_nt_loc (next_token->location,
13775 TEMPLATE_ID_EXPR,
13776 templ, arguments);
13777 else if (DECL_TYPE_TEMPLATE_P (templ)
13778 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13780 bool entering_scope;
13781 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13782 template (rather than some instantiation thereof) only if
13783 is not nested within some other construct. For example, in
13784 "template <typename T> void f(T) { A<T>::", A<T> is just an
13785 instantiation of A. */
13786 entering_scope = (template_parm_scope_p ()
13787 && cp_lexer_next_token_is (parser->lexer,
13788 CPP_SCOPE));
13789 template_id
13790 = finish_template_type (templ, arguments, entering_scope);
13792 else if (variable_template_p (templ))
13794 template_id = lookup_template_variable (templ, arguments);
13796 else
13798 /* If it's not a class-template or a template-template, it should be
13799 a function-template. */
13800 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13801 || TREE_CODE (templ) == OVERLOAD
13802 || BASELINK_P (templ)));
13804 template_id = lookup_template_function (templ, arguments);
13807 /* If parsing tentatively, replace the sequence of tokens that makes
13808 up the template-id with a CPP_TEMPLATE_ID token. That way,
13809 should we re-parse the token stream, we will not have to repeat
13810 the effort required to do the parse, nor will we issue duplicate
13811 error messages about problems during instantiation of the
13812 template. */
13813 if (start_of_id
13814 /* Don't do this if we had a parse error in a declarator; re-parsing
13815 might succeed if a name changes meaning (60361). */
13816 && !(cp_parser_error_occurred (parser)
13817 && cp_parser_parsing_tentatively (parser)
13818 && parser->in_declarator_p))
13820 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13822 /* Reset the contents of the START_OF_ID token. */
13823 token->type = CPP_TEMPLATE_ID;
13824 /* Retrieve any deferred checks. Do not pop this access checks yet
13825 so the memory will not be reclaimed during token replacing below. */
13826 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13827 token->u.tree_check_value->value = template_id;
13828 token->u.tree_check_value->checks = get_deferred_access_checks ();
13829 token->keyword = RID_MAX;
13831 /* Purge all subsequent tokens. */
13832 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13834 /* ??? Can we actually assume that, if template_id ==
13835 error_mark_node, we will have issued a diagnostic to the
13836 user, as opposed to simply marking the tentative parse as
13837 failed? */
13838 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13839 error_at (token->location, "parse error in template argument list");
13842 pop_to_parent_deferring_access_checks ();
13843 return template_id;
13846 /* Parse a template-name.
13848 template-name:
13849 identifier
13851 The standard should actually say:
13853 template-name:
13854 identifier
13855 operator-function-id
13857 A defect report has been filed about this issue.
13859 A conversion-function-id cannot be a template name because they cannot
13860 be part of a template-id. In fact, looking at this code:
13862 a.operator K<int>()
13864 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13865 It is impossible to call a templated conversion-function-id with an
13866 explicit argument list, since the only allowed template parameter is
13867 the type to which it is converting.
13869 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13870 `template' keyword, in a construction like:
13872 T::template f<3>()
13874 In that case `f' is taken to be a template-name, even though there
13875 is no way of knowing for sure.
13877 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13878 name refers to a set of overloaded functions, at least one of which
13879 is a template, or an IDENTIFIER_NODE with the name of the template,
13880 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13881 names are looked up inside uninstantiated templates. */
13883 static tree
13884 cp_parser_template_name (cp_parser* parser,
13885 bool template_keyword_p,
13886 bool check_dependency_p,
13887 bool is_declaration,
13888 enum tag_types tag_type,
13889 bool *is_identifier)
13891 tree identifier;
13892 tree decl;
13893 tree fns;
13894 cp_token *token = cp_lexer_peek_token (parser->lexer);
13896 /* If the next token is `operator', then we have either an
13897 operator-function-id or a conversion-function-id. */
13898 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13900 /* We don't know whether we're looking at an
13901 operator-function-id or a conversion-function-id. */
13902 cp_parser_parse_tentatively (parser);
13903 /* Try an operator-function-id. */
13904 identifier = cp_parser_operator_function_id (parser);
13905 /* If that didn't work, try a conversion-function-id. */
13906 if (!cp_parser_parse_definitely (parser))
13908 cp_parser_error (parser, "expected template-name");
13909 return error_mark_node;
13912 /* Look for the identifier. */
13913 else
13914 identifier = cp_parser_identifier (parser);
13916 /* If we didn't find an identifier, we don't have a template-id. */
13917 if (identifier == error_mark_node)
13918 return error_mark_node;
13920 /* If the name immediately followed the `template' keyword, then it
13921 is a template-name. However, if the next token is not `<', then
13922 we do not treat it as a template-name, since it is not being used
13923 as part of a template-id. This enables us to handle constructs
13924 like:
13926 template <typename T> struct S { S(); };
13927 template <typename T> S<T>::S();
13929 correctly. We would treat `S' as a template -- if it were `S<T>'
13930 -- but we do not if there is no `<'. */
13932 if (processing_template_decl
13933 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13935 /* In a declaration, in a dependent context, we pretend that the
13936 "template" keyword was present in order to improve error
13937 recovery. For example, given:
13939 template <typename T> void f(T::X<int>);
13941 we want to treat "X<int>" as a template-id. */
13942 if (is_declaration
13943 && !template_keyword_p
13944 && parser->scope && TYPE_P (parser->scope)
13945 && check_dependency_p
13946 && dependent_scope_p (parser->scope)
13947 /* Do not do this for dtors (or ctors), since they never
13948 need the template keyword before their name. */
13949 && !constructor_name_p (identifier, parser->scope))
13951 cp_token_position start = 0;
13953 /* Explain what went wrong. */
13954 error_at (token->location, "non-template %qD used as template",
13955 identifier);
13956 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13957 parser->scope, identifier);
13958 /* If parsing tentatively, find the location of the "<" token. */
13959 if (cp_parser_simulate_error (parser))
13960 start = cp_lexer_token_position (parser->lexer, true);
13961 /* Parse the template arguments so that we can issue error
13962 messages about them. */
13963 cp_lexer_consume_token (parser->lexer);
13964 cp_parser_enclosed_template_argument_list (parser);
13965 /* Skip tokens until we find a good place from which to
13966 continue parsing. */
13967 cp_parser_skip_to_closing_parenthesis (parser,
13968 /*recovering=*/true,
13969 /*or_comma=*/true,
13970 /*consume_paren=*/false);
13971 /* If parsing tentatively, permanently remove the
13972 template argument list. That will prevent duplicate
13973 error messages from being issued about the missing
13974 "template" keyword. */
13975 if (start)
13976 cp_lexer_purge_tokens_after (parser->lexer, start);
13977 if (is_identifier)
13978 *is_identifier = true;
13979 return identifier;
13982 /* If the "template" keyword is present, then there is generally
13983 no point in doing name-lookup, so we just return IDENTIFIER.
13984 But, if the qualifying scope is non-dependent then we can
13985 (and must) do name-lookup normally. */
13986 if (template_keyword_p
13987 && (!parser->scope
13988 || (TYPE_P (parser->scope)
13989 && dependent_type_p (parser->scope))))
13990 return identifier;
13993 /* Look up the name. */
13994 decl = cp_parser_lookup_name (parser, identifier,
13995 tag_type,
13996 /*is_template=*/true,
13997 /*is_namespace=*/false,
13998 check_dependency_p,
13999 /*ambiguous_decls=*/NULL,
14000 token->location);
14002 /* If DECL is a template, then the name was a template-name. */
14003 if (TREE_CODE (decl) == TEMPLATE_DECL)
14005 if (TREE_DEPRECATED (decl)
14006 && deprecated_state != DEPRECATED_SUPPRESS)
14007 warn_deprecated_use (decl, NULL_TREE);
14009 else
14011 tree fn = NULL_TREE;
14013 /* The standard does not explicitly indicate whether a name that
14014 names a set of overloaded declarations, some of which are
14015 templates, is a template-name. However, such a name should
14016 be a template-name; otherwise, there is no way to form a
14017 template-id for the overloaded templates. */
14018 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14019 if (TREE_CODE (fns) == OVERLOAD)
14020 for (fn = fns; fn; fn = OVL_NEXT (fn))
14021 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14022 break;
14024 if (!fn)
14026 /* The name does not name a template. */
14027 cp_parser_error (parser, "expected template-name");
14028 return error_mark_node;
14032 /* If DECL is dependent, and refers to a function, then just return
14033 its name; we will look it up again during template instantiation. */
14034 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14036 tree scope = ovl_scope (decl);
14037 if (TYPE_P (scope) && dependent_type_p (scope))
14038 return identifier;
14041 return decl;
14044 /* Parse a template-argument-list.
14046 template-argument-list:
14047 template-argument ... [opt]
14048 template-argument-list , template-argument ... [opt]
14050 Returns a TREE_VEC containing the arguments. */
14052 static tree
14053 cp_parser_template_argument_list (cp_parser* parser)
14055 tree fixed_args[10];
14056 unsigned n_args = 0;
14057 unsigned alloced = 10;
14058 tree *arg_ary = fixed_args;
14059 tree vec;
14060 bool saved_in_template_argument_list_p;
14061 bool saved_ice_p;
14062 bool saved_non_ice_p;
14064 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14065 parser->in_template_argument_list_p = true;
14066 /* Even if the template-id appears in an integral
14067 constant-expression, the contents of the argument list do
14068 not. */
14069 saved_ice_p = parser->integral_constant_expression_p;
14070 parser->integral_constant_expression_p = false;
14071 saved_non_ice_p = parser->non_integral_constant_expression_p;
14072 parser->non_integral_constant_expression_p = false;
14074 /* Parse the arguments. */
14077 tree argument;
14079 if (n_args)
14080 /* Consume the comma. */
14081 cp_lexer_consume_token (parser->lexer);
14083 /* Parse the template-argument. */
14084 argument = cp_parser_template_argument (parser);
14086 /* If the next token is an ellipsis, we're expanding a template
14087 argument pack. */
14088 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14090 if (argument == error_mark_node)
14092 cp_token *token = cp_lexer_peek_token (parser->lexer);
14093 error_at (token->location,
14094 "expected parameter pack before %<...%>");
14096 /* Consume the `...' token. */
14097 cp_lexer_consume_token (parser->lexer);
14099 /* Make the argument into a TYPE_PACK_EXPANSION or
14100 EXPR_PACK_EXPANSION. */
14101 argument = make_pack_expansion (argument);
14104 if (n_args == alloced)
14106 alloced *= 2;
14108 if (arg_ary == fixed_args)
14110 arg_ary = XNEWVEC (tree, alloced);
14111 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14113 else
14114 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14116 arg_ary[n_args++] = argument;
14118 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14120 vec = make_tree_vec (n_args);
14122 while (n_args--)
14123 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14125 if (arg_ary != fixed_args)
14126 free (arg_ary);
14127 parser->non_integral_constant_expression_p = saved_non_ice_p;
14128 parser->integral_constant_expression_p = saved_ice_p;
14129 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14130 #ifdef ENABLE_CHECKING
14131 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14132 #endif
14133 return vec;
14136 /* Parse a template-argument.
14138 template-argument:
14139 assignment-expression
14140 type-id
14141 id-expression
14143 The representation is that of an assignment-expression, type-id, or
14144 id-expression -- except that the qualified id-expression is
14145 evaluated, so that the value returned is either a DECL or an
14146 OVERLOAD.
14148 Although the standard says "assignment-expression", it forbids
14149 throw-expressions or assignments in the template argument.
14150 Therefore, we use "conditional-expression" instead. */
14152 static tree
14153 cp_parser_template_argument (cp_parser* parser)
14155 tree argument;
14156 bool template_p;
14157 bool address_p;
14158 bool maybe_type_id = false;
14159 cp_token *token = NULL, *argument_start_token = NULL;
14160 location_t loc = 0;
14161 cp_id_kind idk;
14163 /* There's really no way to know what we're looking at, so we just
14164 try each alternative in order.
14166 [temp.arg]
14168 In a template-argument, an ambiguity between a type-id and an
14169 expression is resolved to a type-id, regardless of the form of
14170 the corresponding template-parameter.
14172 Therefore, we try a type-id first. */
14173 cp_parser_parse_tentatively (parser);
14174 argument = cp_parser_template_type_arg (parser);
14175 /* If there was no error parsing the type-id but the next token is a
14176 '>>', our behavior depends on which dialect of C++ we're
14177 parsing. In C++98, we probably found a typo for '> >'. But there
14178 are type-id which are also valid expressions. For instance:
14180 struct X { int operator >> (int); };
14181 template <int V> struct Foo {};
14182 Foo<X () >> 5> r;
14184 Here 'X()' is a valid type-id of a function type, but the user just
14185 wanted to write the expression "X() >> 5". Thus, we remember that we
14186 found a valid type-id, but we still try to parse the argument as an
14187 expression to see what happens.
14189 In C++0x, the '>>' will be considered two separate '>'
14190 tokens. */
14191 if (!cp_parser_error_occurred (parser)
14192 && cxx_dialect == cxx98
14193 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14195 maybe_type_id = true;
14196 cp_parser_abort_tentative_parse (parser);
14198 else
14200 /* If the next token isn't a `,' or a `>', then this argument wasn't
14201 really finished. This means that the argument is not a valid
14202 type-id. */
14203 if (!cp_parser_next_token_ends_template_argument_p (parser))
14204 cp_parser_error (parser, "expected template-argument");
14205 /* If that worked, we're done. */
14206 if (cp_parser_parse_definitely (parser))
14207 return argument;
14209 /* We're still not sure what the argument will be. */
14210 cp_parser_parse_tentatively (parser);
14211 /* Try a template. */
14212 argument_start_token = cp_lexer_peek_token (parser->lexer);
14213 argument = cp_parser_id_expression (parser,
14214 /*template_keyword_p=*/false,
14215 /*check_dependency_p=*/true,
14216 &template_p,
14217 /*declarator_p=*/false,
14218 /*optional_p=*/false);
14219 /* If the next token isn't a `,' or a `>', then this argument wasn't
14220 really finished. */
14221 if (!cp_parser_next_token_ends_template_argument_p (parser))
14222 cp_parser_error (parser, "expected template-argument");
14223 if (!cp_parser_error_occurred (parser))
14225 /* Figure out what is being referred to. If the id-expression
14226 was for a class template specialization, then we will have a
14227 TYPE_DECL at this point. There is no need to do name lookup
14228 at this point in that case. */
14229 if (TREE_CODE (argument) != TYPE_DECL)
14230 argument = cp_parser_lookup_name (parser, argument,
14231 none_type,
14232 /*is_template=*/template_p,
14233 /*is_namespace=*/false,
14234 /*check_dependency=*/true,
14235 /*ambiguous_decls=*/NULL,
14236 argument_start_token->location);
14237 if (TREE_CODE (argument) != TEMPLATE_DECL
14238 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14239 cp_parser_error (parser, "expected template-name");
14241 if (cp_parser_parse_definitely (parser))
14243 if (TREE_DEPRECATED (argument))
14244 warn_deprecated_use (argument, NULL_TREE);
14245 return argument;
14247 /* It must be a non-type argument. There permitted cases are given
14248 in [temp.arg.nontype]:
14250 -- an integral constant-expression of integral or enumeration
14251 type; or
14253 -- the name of a non-type template-parameter; or
14255 -- the name of an object or function with external linkage...
14257 -- the address of an object or function with external linkage...
14259 -- a pointer to member... */
14260 /* Look for a non-type template parameter. */
14261 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14263 cp_parser_parse_tentatively (parser);
14264 argument = cp_parser_primary_expression (parser,
14265 /*address_p=*/false,
14266 /*cast_p=*/false,
14267 /*template_arg_p=*/true,
14268 &idk);
14269 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14270 || !cp_parser_next_token_ends_template_argument_p (parser))
14271 cp_parser_simulate_error (parser);
14272 if (cp_parser_parse_definitely (parser))
14273 return argument;
14276 /* If the next token is "&", the argument must be the address of an
14277 object or function with external linkage. */
14278 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14279 if (address_p)
14281 loc = cp_lexer_peek_token (parser->lexer)->location;
14282 cp_lexer_consume_token (parser->lexer);
14284 /* See if we might have an id-expression. */
14285 token = cp_lexer_peek_token (parser->lexer);
14286 if (token->type == CPP_NAME
14287 || token->keyword == RID_OPERATOR
14288 || token->type == CPP_SCOPE
14289 || token->type == CPP_TEMPLATE_ID
14290 || token->type == CPP_NESTED_NAME_SPECIFIER)
14292 cp_parser_parse_tentatively (parser);
14293 argument = cp_parser_primary_expression (parser,
14294 address_p,
14295 /*cast_p=*/false,
14296 /*template_arg_p=*/true,
14297 &idk);
14298 if (cp_parser_error_occurred (parser)
14299 || !cp_parser_next_token_ends_template_argument_p (parser))
14300 cp_parser_abort_tentative_parse (parser);
14301 else
14303 tree probe;
14305 if (INDIRECT_REF_P (argument))
14307 /* Strip the dereference temporarily. */
14308 gcc_assert (REFERENCE_REF_P (argument));
14309 argument = TREE_OPERAND (argument, 0);
14312 /* If we're in a template, we represent a qualified-id referring
14313 to a static data member as a SCOPE_REF even if the scope isn't
14314 dependent so that we can check access control later. */
14315 probe = argument;
14316 if (TREE_CODE (probe) == SCOPE_REF)
14317 probe = TREE_OPERAND (probe, 1);
14318 if (VAR_P (probe))
14320 /* A variable without external linkage might still be a
14321 valid constant-expression, so no error is issued here
14322 if the external-linkage check fails. */
14323 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14324 cp_parser_simulate_error (parser);
14326 else if (is_overloaded_fn (argument))
14327 /* All overloaded functions are allowed; if the external
14328 linkage test does not pass, an error will be issued
14329 later. */
14331 else if (address_p
14332 && (TREE_CODE (argument) == OFFSET_REF
14333 || TREE_CODE (argument) == SCOPE_REF))
14334 /* A pointer-to-member. */
14336 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14338 else
14339 cp_parser_simulate_error (parser);
14341 if (cp_parser_parse_definitely (parser))
14343 if (address_p)
14344 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14345 tf_warning_or_error);
14346 else
14347 argument = convert_from_reference (argument);
14348 return argument;
14352 /* If the argument started with "&", there are no other valid
14353 alternatives at this point. */
14354 if (address_p)
14356 cp_parser_error (parser, "invalid non-type template argument");
14357 return error_mark_node;
14360 /* If the argument wasn't successfully parsed as a type-id followed
14361 by '>>', the argument can only be a constant expression now.
14362 Otherwise, we try parsing the constant-expression tentatively,
14363 because the argument could really be a type-id. */
14364 if (maybe_type_id)
14365 cp_parser_parse_tentatively (parser);
14366 argument = cp_parser_constant_expression (parser);
14368 if (!maybe_type_id)
14369 return argument;
14370 if (!cp_parser_next_token_ends_template_argument_p (parser))
14371 cp_parser_error (parser, "expected template-argument");
14372 if (cp_parser_parse_definitely (parser))
14373 return argument;
14374 /* We did our best to parse the argument as a non type-id, but that
14375 was the only alternative that matched (albeit with a '>' after
14376 it). We can assume it's just a typo from the user, and a
14377 diagnostic will then be issued. */
14378 return cp_parser_template_type_arg (parser);
14381 /* Parse an explicit-instantiation.
14383 explicit-instantiation:
14384 template declaration
14386 Although the standard says `declaration', what it really means is:
14388 explicit-instantiation:
14389 template decl-specifier-seq [opt] declarator [opt] ;
14391 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14392 supposed to be allowed. A defect report has been filed about this
14393 issue.
14395 GNU Extension:
14397 explicit-instantiation:
14398 storage-class-specifier template
14399 decl-specifier-seq [opt] declarator [opt] ;
14400 function-specifier template
14401 decl-specifier-seq [opt] declarator [opt] ; */
14403 static void
14404 cp_parser_explicit_instantiation (cp_parser* parser)
14406 int declares_class_or_enum;
14407 cp_decl_specifier_seq decl_specifiers;
14408 tree extension_specifier = NULL_TREE;
14410 timevar_push (TV_TEMPLATE_INST);
14412 /* Look for an (optional) storage-class-specifier or
14413 function-specifier. */
14414 if (cp_parser_allow_gnu_extensions_p (parser))
14416 extension_specifier
14417 = cp_parser_storage_class_specifier_opt (parser);
14418 if (!extension_specifier)
14419 extension_specifier
14420 = cp_parser_function_specifier_opt (parser,
14421 /*decl_specs=*/NULL);
14424 /* Look for the `template' keyword. */
14425 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14426 /* Let the front end know that we are processing an explicit
14427 instantiation. */
14428 begin_explicit_instantiation ();
14429 /* [temp.explicit] says that we are supposed to ignore access
14430 control while processing explicit instantiation directives. */
14431 push_deferring_access_checks (dk_no_check);
14432 /* Parse a decl-specifier-seq. */
14433 cp_parser_decl_specifier_seq (parser,
14434 CP_PARSER_FLAGS_OPTIONAL,
14435 &decl_specifiers,
14436 &declares_class_or_enum);
14437 /* If there was exactly one decl-specifier, and it declared a class,
14438 and there's no declarator, then we have an explicit type
14439 instantiation. */
14440 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14442 tree type;
14444 type = check_tag_decl (&decl_specifiers,
14445 /*explicit_type_instantiation_p=*/true);
14446 /* Turn access control back on for names used during
14447 template instantiation. */
14448 pop_deferring_access_checks ();
14449 if (type)
14450 do_type_instantiation (type, extension_specifier,
14451 /*complain=*/tf_error);
14453 else
14455 cp_declarator *declarator;
14456 tree decl;
14458 /* Parse the declarator. */
14459 declarator
14460 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14461 /*ctor_dtor_or_conv_p=*/NULL,
14462 /*parenthesized_p=*/NULL,
14463 /*member_p=*/false,
14464 /*friend_p=*/false);
14465 if (declares_class_or_enum & 2)
14466 cp_parser_check_for_definition_in_return_type (declarator,
14467 decl_specifiers.type,
14468 decl_specifiers.locations[ds_type_spec]);
14469 if (declarator != cp_error_declarator)
14471 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14472 permerror (decl_specifiers.locations[ds_inline],
14473 "explicit instantiation shall not use"
14474 " %<inline%> specifier");
14475 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14476 permerror (decl_specifiers.locations[ds_constexpr],
14477 "explicit instantiation shall not use"
14478 " %<constexpr%> specifier");
14480 decl = grokdeclarator (declarator, &decl_specifiers,
14481 NORMAL, 0, &decl_specifiers.attributes);
14482 /* Turn access control back on for names used during
14483 template instantiation. */
14484 pop_deferring_access_checks ();
14485 /* Do the explicit instantiation. */
14486 do_decl_instantiation (decl, extension_specifier);
14488 else
14490 pop_deferring_access_checks ();
14491 /* Skip the body of the explicit instantiation. */
14492 cp_parser_skip_to_end_of_statement (parser);
14495 /* We're done with the instantiation. */
14496 end_explicit_instantiation ();
14498 cp_parser_consume_semicolon_at_end_of_statement (parser);
14500 timevar_pop (TV_TEMPLATE_INST);
14503 /* Parse an explicit-specialization.
14505 explicit-specialization:
14506 template < > declaration
14508 Although the standard says `declaration', what it really means is:
14510 explicit-specialization:
14511 template <> decl-specifier [opt] init-declarator [opt] ;
14512 template <> function-definition
14513 template <> explicit-specialization
14514 template <> template-declaration */
14516 static void
14517 cp_parser_explicit_specialization (cp_parser* parser)
14519 bool need_lang_pop;
14520 cp_token *token = cp_lexer_peek_token (parser->lexer);
14522 /* Look for the `template' keyword. */
14523 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14524 /* Look for the `<'. */
14525 cp_parser_require (parser, CPP_LESS, RT_LESS);
14526 /* Look for the `>'. */
14527 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14528 /* We have processed another parameter list. */
14529 ++parser->num_template_parameter_lists;
14530 /* [temp]
14532 A template ... explicit specialization ... shall not have C
14533 linkage. */
14534 if (current_lang_name == lang_name_c)
14536 error_at (token->location, "template specialization with C linkage");
14537 /* Give it C++ linkage to avoid confusing other parts of the
14538 front end. */
14539 push_lang_context (lang_name_cplusplus);
14540 need_lang_pop = true;
14542 else
14543 need_lang_pop = false;
14544 /* Let the front end know that we are beginning a specialization. */
14545 if (!begin_specialization ())
14547 end_specialization ();
14548 return;
14551 /* If the next keyword is `template', we need to figure out whether
14552 or not we're looking a template-declaration. */
14553 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14555 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14556 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14557 cp_parser_template_declaration_after_export (parser,
14558 /*member_p=*/false);
14559 else
14560 cp_parser_explicit_specialization (parser);
14562 else
14563 /* Parse the dependent declaration. */
14564 cp_parser_single_declaration (parser,
14565 /*checks=*/NULL,
14566 /*member_p=*/false,
14567 /*explicit_specialization_p=*/true,
14568 /*friend_p=*/NULL);
14569 /* We're done with the specialization. */
14570 end_specialization ();
14571 /* For the erroneous case of a template with C linkage, we pushed an
14572 implicit C++ linkage scope; exit that scope now. */
14573 if (need_lang_pop)
14574 pop_lang_context ();
14575 /* We're done with this parameter list. */
14576 --parser->num_template_parameter_lists;
14579 /* Parse a type-specifier.
14581 type-specifier:
14582 simple-type-specifier
14583 class-specifier
14584 enum-specifier
14585 elaborated-type-specifier
14586 cv-qualifier
14588 GNU Extension:
14590 type-specifier:
14591 __complex__
14593 Returns a representation of the type-specifier. For a
14594 class-specifier, enum-specifier, or elaborated-type-specifier, a
14595 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14597 The parser flags FLAGS is used to control type-specifier parsing.
14599 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14600 in a decl-specifier-seq.
14602 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14603 class-specifier, enum-specifier, or elaborated-type-specifier, then
14604 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14605 if a type is declared; 2 if it is defined. Otherwise, it is set to
14606 zero.
14608 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14609 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14610 is set to FALSE. */
14612 static tree
14613 cp_parser_type_specifier (cp_parser* parser,
14614 cp_parser_flags flags,
14615 cp_decl_specifier_seq *decl_specs,
14616 bool is_declaration,
14617 int* declares_class_or_enum,
14618 bool* is_cv_qualifier)
14620 tree type_spec = NULL_TREE;
14621 cp_token *token;
14622 enum rid keyword;
14623 cp_decl_spec ds = ds_last;
14625 /* Assume this type-specifier does not declare a new type. */
14626 if (declares_class_or_enum)
14627 *declares_class_or_enum = 0;
14628 /* And that it does not specify a cv-qualifier. */
14629 if (is_cv_qualifier)
14630 *is_cv_qualifier = false;
14631 /* Peek at the next token. */
14632 token = cp_lexer_peek_token (parser->lexer);
14634 /* If we're looking at a keyword, we can use that to guide the
14635 production we choose. */
14636 keyword = token->keyword;
14637 switch (keyword)
14639 case RID_ENUM:
14640 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14641 goto elaborated_type_specifier;
14643 /* Look for the enum-specifier. */
14644 type_spec = cp_parser_enum_specifier (parser);
14645 /* If that worked, we're done. */
14646 if (type_spec)
14648 if (declares_class_or_enum)
14649 *declares_class_or_enum = 2;
14650 if (decl_specs)
14651 cp_parser_set_decl_spec_type (decl_specs,
14652 type_spec,
14653 token,
14654 /*type_definition_p=*/true);
14655 return type_spec;
14657 else
14658 goto elaborated_type_specifier;
14660 /* Any of these indicate either a class-specifier, or an
14661 elaborated-type-specifier. */
14662 case RID_CLASS:
14663 case RID_STRUCT:
14664 case RID_UNION:
14665 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14666 goto elaborated_type_specifier;
14668 /* Parse tentatively so that we can back up if we don't find a
14669 class-specifier. */
14670 cp_parser_parse_tentatively (parser);
14671 /* Look for the class-specifier. */
14672 type_spec = cp_parser_class_specifier (parser);
14673 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14674 /* If that worked, we're done. */
14675 if (cp_parser_parse_definitely (parser))
14677 if (declares_class_or_enum)
14678 *declares_class_or_enum = 2;
14679 if (decl_specs)
14680 cp_parser_set_decl_spec_type (decl_specs,
14681 type_spec,
14682 token,
14683 /*type_definition_p=*/true);
14684 return type_spec;
14687 /* Fall through. */
14688 elaborated_type_specifier:
14689 /* We're declaring (not defining) a class or enum. */
14690 if (declares_class_or_enum)
14691 *declares_class_or_enum = 1;
14693 /* Fall through. */
14694 case RID_TYPENAME:
14695 /* Look for an elaborated-type-specifier. */
14696 type_spec
14697 = (cp_parser_elaborated_type_specifier
14698 (parser,
14699 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14700 is_declaration));
14701 if (decl_specs)
14702 cp_parser_set_decl_spec_type (decl_specs,
14703 type_spec,
14704 token,
14705 /*type_definition_p=*/false);
14706 return type_spec;
14708 case RID_CONST:
14709 ds = ds_const;
14710 if (is_cv_qualifier)
14711 *is_cv_qualifier = true;
14712 break;
14714 case RID_VOLATILE:
14715 ds = ds_volatile;
14716 if (is_cv_qualifier)
14717 *is_cv_qualifier = true;
14718 break;
14720 case RID_RESTRICT:
14721 ds = ds_restrict;
14722 if (is_cv_qualifier)
14723 *is_cv_qualifier = true;
14724 break;
14726 case RID_COMPLEX:
14727 /* The `__complex__' keyword is a GNU extension. */
14728 ds = ds_complex;
14729 break;
14731 default:
14732 break;
14735 /* Handle simple keywords. */
14736 if (ds != ds_last)
14738 if (decl_specs)
14740 set_and_check_decl_spec_loc (decl_specs, ds, token);
14741 decl_specs->any_specifiers_p = true;
14743 return cp_lexer_consume_token (parser->lexer)->u.value;
14746 /* If we do not already have a type-specifier, assume we are looking
14747 at a simple-type-specifier. */
14748 type_spec = cp_parser_simple_type_specifier (parser,
14749 decl_specs,
14750 flags);
14752 /* If we didn't find a type-specifier, and a type-specifier was not
14753 optional in this context, issue an error message. */
14754 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14756 cp_parser_error (parser, "expected type specifier");
14757 return error_mark_node;
14760 return type_spec;
14763 /* Parse a simple-type-specifier.
14765 simple-type-specifier:
14766 :: [opt] nested-name-specifier [opt] type-name
14767 :: [opt] nested-name-specifier template template-id
14768 char
14769 wchar_t
14770 bool
14771 short
14773 long
14774 signed
14775 unsigned
14776 float
14777 double
14778 void
14780 C++0x Extension:
14782 simple-type-specifier:
14783 auto
14784 decltype ( expression )
14785 char16_t
14786 char32_t
14787 __underlying_type ( type-id )
14789 GNU Extension:
14791 simple-type-specifier:
14792 __int128
14793 __typeof__ unary-expression
14794 __typeof__ ( type-id )
14795 __typeof__ ( type-id ) { initializer-list , [opt] }
14797 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14798 appropriately updated. */
14800 static tree
14801 cp_parser_simple_type_specifier (cp_parser* parser,
14802 cp_decl_specifier_seq *decl_specs,
14803 cp_parser_flags flags)
14805 tree type = NULL_TREE;
14806 cp_token *token;
14807 int idx;
14809 /* Peek at the next token. */
14810 token = cp_lexer_peek_token (parser->lexer);
14812 /* If we're looking at a keyword, things are easy. */
14813 switch (token->keyword)
14815 case RID_CHAR:
14816 if (decl_specs)
14817 decl_specs->explicit_char_p = true;
14818 type = char_type_node;
14819 break;
14820 case RID_CHAR16:
14821 type = char16_type_node;
14822 break;
14823 case RID_CHAR32:
14824 type = char32_type_node;
14825 break;
14826 case RID_WCHAR:
14827 type = wchar_type_node;
14828 break;
14829 case RID_BOOL:
14830 type = boolean_type_node;
14831 break;
14832 case RID_SHORT:
14833 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14834 type = short_integer_type_node;
14835 break;
14836 case RID_INT:
14837 if (decl_specs)
14838 decl_specs->explicit_int_p = true;
14839 type = integer_type_node;
14840 break;
14841 case RID_INT_N_0:
14842 case RID_INT_N_1:
14843 case RID_INT_N_2:
14844 case RID_INT_N_3:
14845 idx = token->keyword - RID_INT_N_0;
14846 if (! int_n_enabled_p [idx])
14847 break;
14848 if (decl_specs)
14850 decl_specs->explicit_intN_p = true;
14851 decl_specs->int_n_idx = idx;
14853 type = int_n_trees [idx].signed_type;
14854 break;
14855 case RID_LONG:
14856 if (decl_specs)
14857 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14858 type = long_integer_type_node;
14859 break;
14860 case RID_SIGNED:
14861 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14862 type = integer_type_node;
14863 break;
14864 case RID_UNSIGNED:
14865 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14866 type = unsigned_type_node;
14867 break;
14868 case RID_FLOAT:
14869 type = float_type_node;
14870 break;
14871 case RID_DOUBLE:
14872 type = double_type_node;
14873 break;
14874 case RID_VOID:
14875 type = void_type_node;
14876 break;
14878 case RID_AUTO:
14879 maybe_warn_cpp0x (CPP0X_AUTO);
14880 if (parser->auto_is_implicit_function_template_parm_p)
14882 if (cxx_dialect >= cxx14)
14883 type = synthesize_implicit_template_parm (parser);
14884 else
14885 type = error_mark_node;
14887 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14889 if (cxx_dialect < cxx14)
14890 error_at (token->location,
14891 "use of %<auto%> in lambda parameter declaration "
14892 "only available with "
14893 "-std=c++14 or -std=gnu++14");
14895 else if (cxx_dialect < cxx14)
14896 error_at (token->location,
14897 "use of %<auto%> in parameter declaration "
14898 "only available with "
14899 "-std=c++14 or -std=gnu++14");
14900 else
14901 pedwarn (token->location, OPT_Wpedantic,
14902 "ISO C++ forbids use of %<auto%> in parameter "
14903 "declaration");
14905 else
14906 type = make_auto ();
14907 break;
14909 case RID_DECLTYPE:
14910 /* Since DR 743, decltype can either be a simple-type-specifier by
14911 itself or begin a nested-name-specifier. Parsing it will replace
14912 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14913 handling below decide what to do. */
14914 cp_parser_decltype (parser);
14915 cp_lexer_set_token_position (parser->lexer, token);
14916 break;
14918 case RID_TYPEOF:
14919 /* Consume the `typeof' token. */
14920 cp_lexer_consume_token (parser->lexer);
14921 /* Parse the operand to `typeof'. */
14922 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14923 /* If it is not already a TYPE, take its type. */
14924 if (!TYPE_P (type))
14925 type = finish_typeof (type);
14927 if (decl_specs)
14928 cp_parser_set_decl_spec_type (decl_specs, type,
14929 token,
14930 /*type_definition_p=*/false);
14932 return type;
14934 case RID_UNDERLYING_TYPE:
14935 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14936 if (decl_specs)
14937 cp_parser_set_decl_spec_type (decl_specs, type,
14938 token,
14939 /*type_definition_p=*/false);
14941 return type;
14943 case RID_BASES:
14944 case RID_DIRECT_BASES:
14945 type = cp_parser_trait_expr (parser, token->keyword);
14946 if (decl_specs)
14947 cp_parser_set_decl_spec_type (decl_specs, type,
14948 token,
14949 /*type_definition_p=*/false);
14950 return type;
14951 default:
14952 break;
14955 /* If token is an already-parsed decltype not followed by ::,
14956 it's a simple-type-specifier. */
14957 if (token->type == CPP_DECLTYPE
14958 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14960 type = token->u.value;
14961 if (decl_specs)
14963 cp_parser_set_decl_spec_type (decl_specs, type,
14964 token,
14965 /*type_definition_p=*/false);
14966 /* Remember that we are handling a decltype in order to
14967 implement the resolution of DR 1510 when the argument
14968 isn't instantiation dependent. */
14969 decl_specs->decltype_p = true;
14971 cp_lexer_consume_token (parser->lexer);
14972 return type;
14975 /* If the type-specifier was for a built-in type, we're done. */
14976 if (type)
14978 /* Record the type. */
14979 if (decl_specs
14980 && (token->keyword != RID_SIGNED
14981 && token->keyword != RID_UNSIGNED
14982 && token->keyword != RID_SHORT
14983 && token->keyword != RID_LONG))
14984 cp_parser_set_decl_spec_type (decl_specs,
14985 type,
14986 token,
14987 /*type_definition_p=*/false);
14988 if (decl_specs)
14989 decl_specs->any_specifiers_p = true;
14991 /* Consume the token. */
14992 cp_lexer_consume_token (parser->lexer);
14994 if (type == error_mark_node)
14995 return error_mark_node;
14997 /* There is no valid C++ program where a non-template type is
14998 followed by a "<". That usually indicates that the user thought
14999 that the type was a template. */
15000 cp_parser_check_for_invalid_template_id (parser, type, none_type,
15001 token->location);
15003 return TYPE_NAME (type);
15006 /* The type-specifier must be a user-defined type. */
15007 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15009 bool qualified_p;
15010 bool global_p;
15012 /* Don't gobble tokens or issue error messages if this is an
15013 optional type-specifier. */
15014 if (flags & CP_PARSER_FLAGS_OPTIONAL)
15015 cp_parser_parse_tentatively (parser);
15017 /* Look for the optional `::' operator. */
15018 global_p
15019 = (cp_parser_global_scope_opt (parser,
15020 /*current_scope_valid_p=*/false)
15021 != NULL_TREE);
15022 /* Look for the nested-name specifier. */
15023 qualified_p
15024 = (cp_parser_nested_name_specifier_opt (parser,
15025 /*typename_keyword_p=*/false,
15026 /*check_dependency_p=*/true,
15027 /*type_p=*/false,
15028 /*is_declaration=*/false)
15029 != NULL_TREE);
15030 token = cp_lexer_peek_token (parser->lexer);
15031 /* If we have seen a nested-name-specifier, and the next token
15032 is `template', then we are using the template-id production. */
15033 if (parser->scope
15034 && cp_parser_optional_template_keyword (parser))
15036 /* Look for the template-id. */
15037 type = cp_parser_template_id (parser,
15038 /*template_keyword_p=*/true,
15039 /*check_dependency_p=*/true,
15040 none_type,
15041 /*is_declaration=*/false);
15042 /* If the template-id did not name a type, we are out of
15043 luck. */
15044 if (TREE_CODE (type) != TYPE_DECL)
15046 cp_parser_error (parser, "expected template-id for type");
15047 type = NULL_TREE;
15050 /* Otherwise, look for a type-name. */
15051 else
15052 type = cp_parser_type_name (parser);
15053 /* Keep track of all name-lookups performed in class scopes. */
15054 if (type
15055 && !global_p
15056 && !qualified_p
15057 && TREE_CODE (type) == TYPE_DECL
15058 && identifier_p (DECL_NAME (type)))
15059 maybe_note_name_used_in_class (DECL_NAME (type), type);
15060 /* If it didn't work out, we don't have a TYPE. */
15061 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15062 && !cp_parser_parse_definitely (parser))
15063 type = NULL_TREE;
15064 if (type && decl_specs)
15065 cp_parser_set_decl_spec_type (decl_specs, type,
15066 token,
15067 /*type_definition_p=*/false);
15070 /* If we didn't get a type-name, issue an error message. */
15071 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15073 cp_parser_error (parser, "expected type-name");
15074 return error_mark_node;
15077 if (type && type != error_mark_node)
15079 /* See if TYPE is an Objective-C type, and if so, parse and
15080 accept any protocol references following it. Do this before
15081 the cp_parser_check_for_invalid_template_id() call, because
15082 Objective-C types can be followed by '<...>' which would
15083 enclose protocol names rather than template arguments, and so
15084 everything is fine. */
15085 if (c_dialect_objc () && !parser->scope
15086 && (objc_is_id (type) || objc_is_class_name (type)))
15088 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15089 tree qual_type = objc_get_protocol_qualified_type (type, protos);
15091 /* Clobber the "unqualified" type previously entered into
15092 DECL_SPECS with the new, improved protocol-qualified version. */
15093 if (decl_specs)
15094 decl_specs->type = qual_type;
15096 return qual_type;
15099 /* There is no valid C++ program where a non-template type is
15100 followed by a "<". That usually indicates that the user
15101 thought that the type was a template. */
15102 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15103 none_type,
15104 token->location);
15107 return type;
15110 /* Parse a type-name.
15112 type-name:
15113 class-name
15114 enum-name
15115 typedef-name
15116 simple-template-id [in c++0x]
15118 enum-name:
15119 identifier
15121 typedef-name:
15122 identifier
15124 Returns a TYPE_DECL for the type. */
15126 static tree
15127 cp_parser_type_name (cp_parser* parser)
15129 tree type_decl;
15131 /* We can't know yet whether it is a class-name or not. */
15132 cp_parser_parse_tentatively (parser);
15133 /* Try a class-name. */
15134 type_decl = cp_parser_class_name (parser,
15135 /*typename_keyword_p=*/false,
15136 /*template_keyword_p=*/false,
15137 none_type,
15138 /*check_dependency_p=*/true,
15139 /*class_head_p=*/false,
15140 /*is_declaration=*/false);
15141 /* If it's not a class-name, keep looking. */
15142 if (!cp_parser_parse_definitely (parser))
15144 if (cxx_dialect < cxx11)
15145 /* It must be a typedef-name or an enum-name. */
15146 return cp_parser_nonclass_name (parser);
15148 cp_parser_parse_tentatively (parser);
15149 /* It is either a simple-template-id representing an
15150 instantiation of an alias template... */
15151 type_decl = cp_parser_template_id (parser,
15152 /*template_keyword_p=*/false,
15153 /*check_dependency_p=*/true,
15154 none_type,
15155 /*is_declaration=*/false);
15156 /* Note that this must be an instantiation of an alias template
15157 because [temp.names]/6 says:
15159 A template-id that names an alias template specialization
15160 is a type-name.
15162 Whereas [temp.names]/7 says:
15164 A simple-template-id that names a class template
15165 specialization is a class-name. */
15166 if (type_decl != NULL_TREE
15167 && TREE_CODE (type_decl) == TYPE_DECL
15168 && TYPE_DECL_ALIAS_P (type_decl))
15169 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15170 else
15171 cp_parser_simulate_error (parser);
15173 if (!cp_parser_parse_definitely (parser))
15174 /* ... Or a typedef-name or an enum-name. */
15175 return cp_parser_nonclass_name (parser);
15178 return type_decl;
15181 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15183 enum-name:
15184 identifier
15186 typedef-name:
15187 identifier
15189 Returns a TYPE_DECL for the type. */
15191 static tree
15192 cp_parser_nonclass_name (cp_parser* parser)
15194 tree type_decl;
15195 tree identifier;
15197 cp_token *token = cp_lexer_peek_token (parser->lexer);
15198 identifier = cp_parser_identifier (parser);
15199 if (identifier == error_mark_node)
15200 return error_mark_node;
15202 /* Look up the type-name. */
15203 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15205 type_decl = strip_using_decl (type_decl);
15207 if (TREE_CODE (type_decl) != TYPE_DECL
15208 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15210 /* See if this is an Objective-C type. */
15211 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15212 tree type = objc_get_protocol_qualified_type (identifier, protos);
15213 if (type)
15214 type_decl = TYPE_NAME (type);
15217 /* Issue an error if we did not find a type-name. */
15218 if (TREE_CODE (type_decl) != TYPE_DECL
15219 /* In Objective-C, we have the complication that class names are
15220 normally type names and start declarations (eg, the
15221 "NSObject" in "NSObject *object;"), but can be used in an
15222 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15223 is an expression. So, a classname followed by a dot is not a
15224 valid type-name. */
15225 || (objc_is_class_name (TREE_TYPE (type_decl))
15226 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15228 if (!cp_parser_simulate_error (parser))
15229 cp_parser_name_lookup_error (parser, identifier, type_decl,
15230 NLE_TYPE, token->location);
15231 return error_mark_node;
15233 /* Remember that the name was used in the definition of the
15234 current class so that we can check later to see if the
15235 meaning would have been different after the class was
15236 entirely defined. */
15237 else if (type_decl != error_mark_node
15238 && !parser->scope)
15239 maybe_note_name_used_in_class (identifier, type_decl);
15241 return type_decl;
15244 /* Parse an elaborated-type-specifier. Note that the grammar given
15245 here incorporates the resolution to DR68.
15247 elaborated-type-specifier:
15248 class-key :: [opt] nested-name-specifier [opt] identifier
15249 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15250 enum-key :: [opt] nested-name-specifier [opt] identifier
15251 typename :: [opt] nested-name-specifier identifier
15252 typename :: [opt] nested-name-specifier template [opt]
15253 template-id
15255 GNU extension:
15257 elaborated-type-specifier:
15258 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15259 class-key attributes :: [opt] nested-name-specifier [opt]
15260 template [opt] template-id
15261 enum attributes :: [opt] nested-name-specifier [opt] identifier
15263 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15264 declared `friend'. If IS_DECLARATION is TRUE, then this
15265 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15266 something is being declared.
15268 Returns the TYPE specified. */
15270 static tree
15271 cp_parser_elaborated_type_specifier (cp_parser* parser,
15272 bool is_friend,
15273 bool is_declaration)
15275 enum tag_types tag_type;
15276 tree identifier;
15277 tree type = NULL_TREE;
15278 tree attributes = NULL_TREE;
15279 tree globalscope;
15280 cp_token *token = NULL;
15282 /* See if we're looking at the `enum' keyword. */
15283 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15285 /* Consume the `enum' token. */
15286 cp_lexer_consume_token (parser->lexer);
15287 /* Remember that it's an enumeration type. */
15288 tag_type = enum_type;
15289 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15290 enums) is used here. */
15291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15292 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15294 pedwarn (input_location, 0, "elaborated-type-specifier "
15295 "for a scoped enum must not use the %<%D%> keyword",
15296 cp_lexer_peek_token (parser->lexer)->u.value);
15297 /* Consume the `struct' or `class' and parse it anyway. */
15298 cp_lexer_consume_token (parser->lexer);
15300 /* Parse the attributes. */
15301 attributes = cp_parser_attributes_opt (parser);
15303 /* Or, it might be `typename'. */
15304 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15305 RID_TYPENAME))
15307 /* Consume the `typename' token. */
15308 cp_lexer_consume_token (parser->lexer);
15309 /* Remember that it's a `typename' type. */
15310 tag_type = typename_type;
15312 /* Otherwise it must be a class-key. */
15313 else
15315 tag_type = cp_parser_class_key (parser);
15316 if (tag_type == none_type)
15317 return error_mark_node;
15318 /* Parse the attributes. */
15319 attributes = cp_parser_attributes_opt (parser);
15322 /* Look for the `::' operator. */
15323 globalscope = cp_parser_global_scope_opt (parser,
15324 /*current_scope_valid_p=*/false);
15325 /* Look for the nested-name-specifier. */
15326 if (tag_type == typename_type && !globalscope)
15328 if (!cp_parser_nested_name_specifier (parser,
15329 /*typename_keyword_p=*/true,
15330 /*check_dependency_p=*/true,
15331 /*type_p=*/true,
15332 is_declaration))
15333 return error_mark_node;
15335 else
15336 /* Even though `typename' is not present, the proposed resolution
15337 to Core Issue 180 says that in `class A<T>::B', `B' should be
15338 considered a type-name, even if `A<T>' is dependent. */
15339 cp_parser_nested_name_specifier_opt (parser,
15340 /*typename_keyword_p=*/true,
15341 /*check_dependency_p=*/true,
15342 /*type_p=*/true,
15343 is_declaration);
15344 /* For everything but enumeration types, consider a template-id.
15345 For an enumeration type, consider only a plain identifier. */
15346 if (tag_type != enum_type)
15348 bool template_p = false;
15349 tree decl;
15351 /* Allow the `template' keyword. */
15352 template_p = cp_parser_optional_template_keyword (parser);
15353 /* If we didn't see `template', we don't know if there's a
15354 template-id or not. */
15355 if (!template_p)
15356 cp_parser_parse_tentatively (parser);
15357 /* Parse the template-id. */
15358 token = cp_lexer_peek_token (parser->lexer);
15359 decl = cp_parser_template_id (parser, template_p,
15360 /*check_dependency_p=*/true,
15361 tag_type,
15362 is_declaration);
15363 /* If we didn't find a template-id, look for an ordinary
15364 identifier. */
15365 if (!template_p && !cp_parser_parse_definitely (parser))
15367 /* We can get here when cp_parser_template_id, called by
15368 cp_parser_class_name with tag_type == none_type, succeeds
15369 and caches a BASELINK. Then, when called again here,
15370 instead of failing and returning an error_mark_node
15371 returns it (see template/typename17.C in C++11).
15372 ??? Could we diagnose this earlier? */
15373 else if (tag_type == typename_type && BASELINK_P (decl))
15375 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15376 type = error_mark_node;
15378 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15379 in effect, then we must assume that, upon instantiation, the
15380 template will correspond to a class. */
15381 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15382 && tag_type == typename_type)
15383 type = make_typename_type (parser->scope, decl,
15384 typename_type,
15385 /*complain=*/tf_error);
15386 /* If the `typename' keyword is in effect and DECL is not a type
15387 decl, then type is non existent. */
15388 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15390 else if (TREE_CODE (decl) == TYPE_DECL)
15391 type = check_elaborated_type_specifier (tag_type, decl,
15392 /*allow_template_p=*/true);
15393 else if (decl == error_mark_node)
15394 type = error_mark_node;
15397 if (!type)
15399 token = cp_lexer_peek_token (parser->lexer);
15400 identifier = cp_parser_identifier (parser);
15402 if (identifier == error_mark_node)
15404 parser->scope = NULL_TREE;
15405 return error_mark_node;
15408 /* For a `typename', we needn't call xref_tag. */
15409 if (tag_type == typename_type
15410 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15411 return cp_parser_make_typename_type (parser, identifier,
15412 token->location);
15414 /* Template parameter lists apply only if we are not within a
15415 function parameter list. */
15416 bool template_parm_lists_apply
15417 = parser->num_template_parameter_lists;
15418 if (template_parm_lists_apply)
15419 for (cp_binding_level *s = current_binding_level;
15420 s && s->kind != sk_template_parms;
15421 s = s->level_chain)
15422 if (s->kind == sk_function_parms)
15423 template_parm_lists_apply = false;
15425 /* Look up a qualified name in the usual way. */
15426 if (parser->scope)
15428 tree decl;
15429 tree ambiguous_decls;
15431 decl = cp_parser_lookup_name (parser, identifier,
15432 tag_type,
15433 /*is_template=*/false,
15434 /*is_namespace=*/false,
15435 /*check_dependency=*/true,
15436 &ambiguous_decls,
15437 token->location);
15439 /* If the lookup was ambiguous, an error will already have been
15440 issued. */
15441 if (ambiguous_decls)
15442 return error_mark_node;
15444 /* If we are parsing friend declaration, DECL may be a
15445 TEMPLATE_DECL tree node here. However, we need to check
15446 whether this TEMPLATE_DECL results in valid code. Consider
15447 the following example:
15449 namespace N {
15450 template <class T> class C {};
15452 class X {
15453 template <class T> friend class N::C; // #1, valid code
15455 template <class T> class Y {
15456 friend class N::C; // #2, invalid code
15459 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15460 name lookup of `N::C'. We see that friend declaration must
15461 be template for the code to be valid. Note that
15462 processing_template_decl does not work here since it is
15463 always 1 for the above two cases. */
15465 decl = (cp_parser_maybe_treat_template_as_class
15466 (decl, /*tag_name_p=*/is_friend
15467 && template_parm_lists_apply));
15469 if (TREE_CODE (decl) != TYPE_DECL)
15471 cp_parser_diagnose_invalid_type_name (parser,
15472 identifier,
15473 token->location);
15474 return error_mark_node;
15477 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15479 bool allow_template = (template_parm_lists_apply
15480 || DECL_SELF_REFERENCE_P (decl));
15481 type = check_elaborated_type_specifier (tag_type, decl,
15482 allow_template);
15484 if (type == error_mark_node)
15485 return error_mark_node;
15488 /* Forward declarations of nested types, such as
15490 class C1::C2;
15491 class C1::C2::C3;
15493 are invalid unless all components preceding the final '::'
15494 are complete. If all enclosing types are complete, these
15495 declarations become merely pointless.
15497 Invalid forward declarations of nested types are errors
15498 caught elsewhere in parsing. Those that are pointless arrive
15499 here. */
15501 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15502 && !is_friend && !processing_explicit_instantiation)
15503 warning (0, "declaration %qD does not declare anything", decl);
15505 type = TREE_TYPE (decl);
15507 else
15509 /* An elaborated-type-specifier sometimes introduces a new type and
15510 sometimes names an existing type. Normally, the rule is that it
15511 introduces a new type only if there is not an existing type of
15512 the same name already in scope. For example, given:
15514 struct S {};
15515 void f() { struct S s; }
15517 the `struct S' in the body of `f' is the same `struct S' as in
15518 the global scope; the existing definition is used. However, if
15519 there were no global declaration, this would introduce a new
15520 local class named `S'.
15522 An exception to this rule applies to the following code:
15524 namespace N { struct S; }
15526 Here, the elaborated-type-specifier names a new type
15527 unconditionally; even if there is already an `S' in the
15528 containing scope this declaration names a new type.
15529 This exception only applies if the elaborated-type-specifier
15530 forms the complete declaration:
15532 [class.name]
15534 A declaration consisting solely of `class-key identifier ;' is
15535 either a redeclaration of the name in the current scope or a
15536 forward declaration of the identifier as a class name. It
15537 introduces the name into the current scope.
15539 We are in this situation precisely when the next token is a `;'.
15541 An exception to the exception is that a `friend' declaration does
15542 *not* name a new type; i.e., given:
15544 struct S { friend struct T; };
15546 `T' is not a new type in the scope of `S'.
15548 Also, `new struct S' or `sizeof (struct S)' never results in the
15549 definition of a new type; a new type can only be declared in a
15550 declaration context. */
15552 tag_scope ts;
15553 bool template_p;
15555 if (is_friend)
15556 /* Friends have special name lookup rules. */
15557 ts = ts_within_enclosing_non_class;
15558 else if (is_declaration
15559 && cp_lexer_next_token_is (parser->lexer,
15560 CPP_SEMICOLON))
15561 /* This is a `class-key identifier ;' */
15562 ts = ts_current;
15563 else
15564 ts = ts_global;
15566 template_p =
15567 (template_parm_lists_apply
15568 && (cp_parser_next_token_starts_class_definition_p (parser)
15569 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15570 /* An unqualified name was used to reference this type, so
15571 there were no qualifying templates. */
15572 if (template_parm_lists_apply
15573 && !cp_parser_check_template_parameters (parser,
15574 /*num_templates=*/0,
15575 token->location,
15576 /*declarator=*/NULL))
15577 return error_mark_node;
15578 type = xref_tag (tag_type, identifier, ts, template_p);
15582 if (type == error_mark_node)
15583 return error_mark_node;
15585 /* Allow attributes on forward declarations of classes. */
15586 if (attributes)
15588 if (TREE_CODE (type) == TYPENAME_TYPE)
15589 warning (OPT_Wattributes,
15590 "attributes ignored on uninstantiated type");
15591 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15592 && ! processing_explicit_instantiation)
15593 warning (OPT_Wattributes,
15594 "attributes ignored on template instantiation");
15595 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15596 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15597 else
15598 warning (OPT_Wattributes,
15599 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15602 if (tag_type != enum_type)
15604 /* Indicate whether this class was declared as a `class' or as a
15605 `struct'. */
15606 if (TREE_CODE (type) == RECORD_TYPE)
15607 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15608 cp_parser_check_class_key (tag_type, type);
15611 /* A "<" cannot follow an elaborated type specifier. If that
15612 happens, the user was probably trying to form a template-id. */
15613 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15614 token->location);
15616 return type;
15619 /* Parse an enum-specifier.
15621 enum-specifier:
15622 enum-head { enumerator-list [opt] }
15623 enum-head { enumerator-list , } [C++0x]
15625 enum-head:
15626 enum-key identifier [opt] enum-base [opt]
15627 enum-key nested-name-specifier identifier enum-base [opt]
15629 enum-key:
15630 enum
15631 enum class [C++0x]
15632 enum struct [C++0x]
15634 enum-base: [C++0x]
15635 : type-specifier-seq
15637 opaque-enum-specifier:
15638 enum-key identifier enum-base [opt] ;
15640 GNU Extensions:
15641 enum-key attributes[opt] identifier [opt] enum-base [opt]
15642 { enumerator-list [opt] }attributes[opt]
15643 enum-key attributes[opt] identifier [opt] enum-base [opt]
15644 { enumerator-list, }attributes[opt] [C++0x]
15646 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15647 if the token stream isn't an enum-specifier after all. */
15649 static tree
15650 cp_parser_enum_specifier (cp_parser* parser)
15652 tree identifier;
15653 tree type = NULL_TREE;
15654 tree prev_scope;
15655 tree nested_name_specifier = NULL_TREE;
15656 tree attributes;
15657 bool scoped_enum_p = false;
15658 bool has_underlying_type = false;
15659 bool nested_being_defined = false;
15660 bool new_value_list = false;
15661 bool is_new_type = false;
15662 bool is_anonymous = false;
15663 tree underlying_type = NULL_TREE;
15664 cp_token *type_start_token = NULL;
15665 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15667 parser->colon_corrects_to_scope_p = false;
15669 /* Parse tentatively so that we can back up if we don't find a
15670 enum-specifier. */
15671 cp_parser_parse_tentatively (parser);
15673 /* Caller guarantees that the current token is 'enum', an identifier
15674 possibly follows, and the token after that is an opening brace.
15675 If we don't have an identifier, fabricate an anonymous name for
15676 the enumeration being defined. */
15677 cp_lexer_consume_token (parser->lexer);
15679 /* Parse the "class" or "struct", which indicates a scoped
15680 enumeration type in C++0x. */
15681 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15682 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15684 if (cxx_dialect < cxx11)
15685 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15687 /* Consume the `struct' or `class' token. */
15688 cp_lexer_consume_token (parser->lexer);
15690 scoped_enum_p = true;
15693 attributes = cp_parser_attributes_opt (parser);
15695 /* Clear the qualification. */
15696 parser->scope = NULL_TREE;
15697 parser->qualifying_scope = NULL_TREE;
15698 parser->object_scope = NULL_TREE;
15700 /* Figure out in what scope the declaration is being placed. */
15701 prev_scope = current_scope ();
15703 type_start_token = cp_lexer_peek_token (parser->lexer);
15705 push_deferring_access_checks (dk_no_check);
15706 nested_name_specifier
15707 = cp_parser_nested_name_specifier_opt (parser,
15708 /*typename_keyword_p=*/true,
15709 /*check_dependency_p=*/false,
15710 /*type_p=*/false,
15711 /*is_declaration=*/false);
15713 if (nested_name_specifier)
15715 tree name;
15717 identifier = cp_parser_identifier (parser);
15718 name = cp_parser_lookup_name (parser, identifier,
15719 enum_type,
15720 /*is_template=*/false,
15721 /*is_namespace=*/false,
15722 /*check_dependency=*/true,
15723 /*ambiguous_decls=*/NULL,
15724 input_location);
15725 if (name && name != error_mark_node)
15727 type = TREE_TYPE (name);
15728 if (TREE_CODE (type) == TYPENAME_TYPE)
15730 /* Are template enums allowed in ISO? */
15731 if (template_parm_scope_p ())
15732 pedwarn (type_start_token->location, OPT_Wpedantic,
15733 "%qD is an enumeration template", name);
15734 /* ignore a typename reference, for it will be solved by name
15735 in start_enum. */
15736 type = NULL_TREE;
15739 else if (nested_name_specifier == error_mark_node)
15740 /* We already issued an error. */;
15741 else
15742 error_at (type_start_token->location,
15743 "%qD is not an enumerator-name", identifier);
15745 else
15747 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15748 identifier = cp_parser_identifier (parser);
15749 else
15751 identifier = make_anon_name ();
15752 is_anonymous = true;
15753 if (scoped_enum_p)
15754 error_at (type_start_token->location,
15755 "anonymous scoped enum is not allowed");
15758 pop_deferring_access_checks ();
15760 /* Check for the `:' that denotes a specified underlying type in C++0x.
15761 Note that a ':' could also indicate a bitfield width, however. */
15762 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15764 cp_decl_specifier_seq type_specifiers;
15766 /* Consume the `:'. */
15767 cp_lexer_consume_token (parser->lexer);
15769 /* Parse the type-specifier-seq. */
15770 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15771 /*is_trailing_return=*/false,
15772 &type_specifiers);
15774 /* At this point this is surely not elaborated type specifier. */
15775 if (!cp_parser_parse_definitely (parser))
15776 return NULL_TREE;
15778 if (cxx_dialect < cxx11)
15779 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15781 has_underlying_type = true;
15783 /* If that didn't work, stop. */
15784 if (type_specifiers.type != error_mark_node)
15786 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15787 /*initialized=*/0, NULL);
15788 if (underlying_type == error_mark_node
15789 || check_for_bare_parameter_packs (underlying_type))
15790 underlying_type = NULL_TREE;
15794 /* Look for the `{' but don't consume it yet. */
15795 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15797 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15799 cp_parser_error (parser, "expected %<{%>");
15800 if (has_underlying_type)
15802 type = NULL_TREE;
15803 goto out;
15806 /* An opaque-enum-specifier must have a ';' here. */
15807 if ((scoped_enum_p || underlying_type)
15808 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15810 cp_parser_error (parser, "expected %<;%> or %<{%>");
15811 if (has_underlying_type)
15813 type = NULL_TREE;
15814 goto out;
15819 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15820 return NULL_TREE;
15822 if (nested_name_specifier)
15824 if (CLASS_TYPE_P (nested_name_specifier))
15826 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15827 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15828 push_scope (nested_name_specifier);
15830 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15832 push_nested_namespace (nested_name_specifier);
15836 /* Issue an error message if type-definitions are forbidden here. */
15837 if (!cp_parser_check_type_definition (parser))
15838 type = error_mark_node;
15839 else
15840 /* Create the new type. We do this before consuming the opening
15841 brace so the enum will be recorded as being on the line of its
15842 tag (or the 'enum' keyword, if there is no tag). */
15843 type = start_enum (identifier, type, underlying_type,
15844 scoped_enum_p, &is_new_type);
15846 /* If the next token is not '{' it is an opaque-enum-specifier or an
15847 elaborated-type-specifier. */
15848 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15850 timevar_push (TV_PARSE_ENUM);
15851 if (nested_name_specifier
15852 && nested_name_specifier != error_mark_node)
15854 /* The following catches invalid code such as:
15855 enum class S<int>::E { A, B, C }; */
15856 if (!processing_specialization
15857 && CLASS_TYPE_P (nested_name_specifier)
15858 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15859 error_at (type_start_token->location, "cannot add an enumerator "
15860 "list to a template instantiation");
15862 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15864 error_at (type_start_token->location,
15865 "%<%T::%E%> has not been declared",
15866 TYPE_CONTEXT (nested_name_specifier),
15867 nested_name_specifier);
15868 type = error_mark_node;
15870 /* If that scope does not contain the scope in which the
15871 class was originally declared, the program is invalid. */
15872 else if (prev_scope && !is_ancestor (prev_scope,
15873 nested_name_specifier))
15875 if (at_namespace_scope_p ())
15876 error_at (type_start_token->location,
15877 "declaration of %qD in namespace %qD which does not "
15878 "enclose %qD",
15879 type, prev_scope, nested_name_specifier);
15880 else
15881 error_at (type_start_token->location,
15882 "declaration of %qD in %qD which does not "
15883 "enclose %qD",
15884 type, prev_scope, nested_name_specifier);
15885 type = error_mark_node;
15889 if (scoped_enum_p)
15890 begin_scope (sk_scoped_enum, type);
15892 /* Consume the opening brace. */
15893 cp_lexer_consume_token (parser->lexer);
15895 if (type == error_mark_node)
15896 ; /* Nothing to add */
15897 else if (OPAQUE_ENUM_P (type)
15898 || (cxx_dialect > cxx98 && processing_specialization))
15900 new_value_list = true;
15901 SET_OPAQUE_ENUM_P (type, false);
15902 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15904 else
15906 error_at (type_start_token->location,
15907 "multiple definition of %q#T", type);
15908 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15909 "previous definition here");
15910 type = error_mark_node;
15913 if (type == error_mark_node)
15914 cp_parser_skip_to_end_of_block_or_statement (parser);
15915 /* If the next token is not '}', then there are some enumerators. */
15916 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15918 if (is_anonymous && !scoped_enum_p)
15919 pedwarn (type_start_token->location, OPT_Wpedantic,
15920 "ISO C++ forbids empty anonymous enum");
15922 else
15923 cp_parser_enumerator_list (parser, type);
15925 /* Consume the final '}'. */
15926 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15928 if (scoped_enum_p)
15929 finish_scope ();
15930 timevar_pop (TV_PARSE_ENUM);
15932 else
15934 /* If a ';' follows, then it is an opaque-enum-specifier
15935 and additional restrictions apply. */
15936 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15938 if (is_anonymous)
15939 error_at (type_start_token->location,
15940 "opaque-enum-specifier without name");
15941 else if (nested_name_specifier)
15942 error_at (type_start_token->location,
15943 "opaque-enum-specifier must use a simple identifier");
15947 /* Look for trailing attributes to apply to this enumeration, and
15948 apply them if appropriate. */
15949 if (cp_parser_allow_gnu_extensions_p (parser))
15951 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15952 trailing_attr = chainon (trailing_attr, attributes);
15953 cplus_decl_attributes (&type,
15954 trailing_attr,
15955 (int) ATTR_FLAG_TYPE_IN_PLACE);
15958 /* Finish up the enumeration. */
15959 if (type != error_mark_node)
15961 if (new_value_list)
15962 finish_enum_value_list (type);
15963 if (is_new_type)
15964 finish_enum (type);
15967 if (nested_name_specifier)
15969 if (CLASS_TYPE_P (nested_name_specifier))
15971 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15972 pop_scope (nested_name_specifier);
15974 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15976 pop_nested_namespace (nested_name_specifier);
15979 out:
15980 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15981 return type;
15984 /* Parse an enumerator-list. The enumerators all have the indicated
15985 TYPE.
15987 enumerator-list:
15988 enumerator-definition
15989 enumerator-list , enumerator-definition */
15991 static void
15992 cp_parser_enumerator_list (cp_parser* parser, tree type)
15994 while (true)
15996 /* Parse an enumerator-definition. */
15997 cp_parser_enumerator_definition (parser, type);
15999 /* If the next token is not a ',', we've reached the end of
16000 the list. */
16001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16002 break;
16003 /* Otherwise, consume the `,' and keep going. */
16004 cp_lexer_consume_token (parser->lexer);
16005 /* If the next token is a `}', there is a trailing comma. */
16006 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16008 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16009 pedwarn (input_location, OPT_Wpedantic,
16010 "comma at end of enumerator list");
16011 break;
16016 /* Parse an enumerator-definition. The enumerator has the indicated
16017 TYPE.
16019 enumerator-definition:
16020 enumerator
16021 enumerator = constant-expression
16023 enumerator:
16024 identifier */
16026 static void
16027 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16029 tree identifier;
16030 tree value;
16031 location_t loc;
16033 /* Save the input location because we are interested in the location
16034 of the identifier and not the location of the explicit value. */
16035 loc = cp_lexer_peek_token (parser->lexer)->location;
16037 /* Look for the identifier. */
16038 identifier = cp_parser_identifier (parser);
16039 if (identifier == error_mark_node)
16040 return;
16042 /* If the next token is an '=', then there is an explicit value. */
16043 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16045 /* Consume the `=' token. */
16046 cp_lexer_consume_token (parser->lexer);
16047 /* Parse the value. */
16048 value = cp_parser_constant_expression (parser);
16050 else
16051 value = NULL_TREE;
16053 /* If we are processing a template, make sure the initializer of the
16054 enumerator doesn't contain any bare template parameter pack. */
16055 if (check_for_bare_parameter_packs (value))
16056 value = error_mark_node;
16058 /* Create the enumerator. */
16059 build_enumerator (identifier, value, type, loc);
16062 /* Parse a namespace-name.
16064 namespace-name:
16065 original-namespace-name
16066 namespace-alias
16068 Returns the NAMESPACE_DECL for the namespace. */
16070 static tree
16071 cp_parser_namespace_name (cp_parser* parser)
16073 tree identifier;
16074 tree namespace_decl;
16076 cp_token *token = cp_lexer_peek_token (parser->lexer);
16078 /* Get the name of the namespace. */
16079 identifier = cp_parser_identifier (parser);
16080 if (identifier == error_mark_node)
16081 return error_mark_node;
16083 /* Look up the identifier in the currently active scope. Look only
16084 for namespaces, due to:
16086 [basic.lookup.udir]
16088 When looking up a namespace-name in a using-directive or alias
16089 definition, only namespace names are considered.
16091 And:
16093 [basic.lookup.qual]
16095 During the lookup of a name preceding the :: scope resolution
16096 operator, object, function, and enumerator names are ignored.
16098 (Note that cp_parser_qualifying_entity only calls this
16099 function if the token after the name is the scope resolution
16100 operator.) */
16101 namespace_decl = cp_parser_lookup_name (parser, identifier,
16102 none_type,
16103 /*is_template=*/false,
16104 /*is_namespace=*/true,
16105 /*check_dependency=*/true,
16106 /*ambiguous_decls=*/NULL,
16107 token->location);
16108 /* If it's not a namespace, issue an error. */
16109 if (namespace_decl == error_mark_node
16110 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16112 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16113 error_at (token->location, "%qD is not a namespace-name", identifier);
16114 cp_parser_error (parser, "expected namespace-name");
16115 namespace_decl = error_mark_node;
16118 return namespace_decl;
16121 /* Parse a namespace-definition.
16123 namespace-definition:
16124 named-namespace-definition
16125 unnamed-namespace-definition
16127 named-namespace-definition:
16128 original-namespace-definition
16129 extension-namespace-definition
16131 original-namespace-definition:
16132 namespace identifier { namespace-body }
16134 extension-namespace-definition:
16135 namespace original-namespace-name { namespace-body }
16137 unnamed-namespace-definition:
16138 namespace { namespace-body } */
16140 static void
16141 cp_parser_namespace_definition (cp_parser* parser)
16143 tree identifier, attribs;
16144 bool has_visibility;
16145 bool is_inline;
16147 cp_ensure_no_omp_declare_simd (parser);
16148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16150 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16151 is_inline = true;
16152 cp_lexer_consume_token (parser->lexer);
16154 else
16155 is_inline = false;
16157 /* Look for the `namespace' keyword. */
16158 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16160 /* Get the name of the namespace. We do not attempt to distinguish
16161 between an original-namespace-definition and an
16162 extension-namespace-definition at this point. The semantic
16163 analysis routines are responsible for that. */
16164 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16165 identifier = cp_parser_identifier (parser);
16166 else
16167 identifier = NULL_TREE;
16169 /* Parse any specified attributes. */
16170 attribs = cp_parser_attributes_opt (parser);
16172 /* Look for the `{' to start the namespace. */
16173 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16174 /* Start the namespace. */
16175 push_namespace (identifier);
16177 /* "inline namespace" is equivalent to a stub namespace definition
16178 followed by a strong using directive. */
16179 if (is_inline)
16181 tree name_space = current_namespace;
16182 /* Set up namespace association. */
16183 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16184 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16185 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16186 /* Import the contents of the inline namespace. */
16187 pop_namespace ();
16188 do_using_directive (name_space);
16189 push_namespace (identifier);
16192 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16194 /* Parse the body of the namespace. */
16195 cp_parser_namespace_body (parser);
16197 if (has_visibility)
16198 pop_visibility (1);
16200 /* Finish the namespace. */
16201 pop_namespace ();
16202 /* Look for the final `}'. */
16203 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16206 /* Parse a namespace-body.
16208 namespace-body:
16209 declaration-seq [opt] */
16211 static void
16212 cp_parser_namespace_body (cp_parser* parser)
16214 cp_parser_declaration_seq_opt (parser);
16217 /* Parse a namespace-alias-definition.
16219 namespace-alias-definition:
16220 namespace identifier = qualified-namespace-specifier ; */
16222 static void
16223 cp_parser_namespace_alias_definition (cp_parser* parser)
16225 tree identifier;
16226 tree namespace_specifier;
16228 cp_token *token = cp_lexer_peek_token (parser->lexer);
16230 /* Look for the `namespace' keyword. */
16231 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16232 /* Look for the identifier. */
16233 identifier = cp_parser_identifier (parser);
16234 if (identifier == error_mark_node)
16235 return;
16236 /* Look for the `=' token. */
16237 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16238 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16240 error_at (token->location, "%<namespace%> definition is not allowed here");
16241 /* Skip the definition. */
16242 cp_lexer_consume_token (parser->lexer);
16243 if (cp_parser_skip_to_closing_brace (parser))
16244 cp_lexer_consume_token (parser->lexer);
16245 return;
16247 cp_parser_require (parser, CPP_EQ, RT_EQ);
16248 /* Look for the qualified-namespace-specifier. */
16249 namespace_specifier
16250 = cp_parser_qualified_namespace_specifier (parser);
16251 /* Look for the `;' token. */
16252 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16254 /* Register the alias in the symbol table. */
16255 do_namespace_alias (identifier, namespace_specifier);
16258 /* Parse a qualified-namespace-specifier.
16260 qualified-namespace-specifier:
16261 :: [opt] nested-name-specifier [opt] namespace-name
16263 Returns a NAMESPACE_DECL corresponding to the specified
16264 namespace. */
16266 static tree
16267 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16269 /* Look for the optional `::'. */
16270 cp_parser_global_scope_opt (parser,
16271 /*current_scope_valid_p=*/false);
16273 /* Look for the optional nested-name-specifier. */
16274 cp_parser_nested_name_specifier_opt (parser,
16275 /*typename_keyword_p=*/false,
16276 /*check_dependency_p=*/true,
16277 /*type_p=*/false,
16278 /*is_declaration=*/true);
16280 return cp_parser_namespace_name (parser);
16283 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16284 access declaration.
16286 using-declaration:
16287 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16288 using :: unqualified-id ;
16290 access-declaration:
16291 qualified-id ;
16295 static bool
16296 cp_parser_using_declaration (cp_parser* parser,
16297 bool access_declaration_p)
16299 cp_token *token;
16300 bool typename_p = false;
16301 bool global_scope_p;
16302 tree decl;
16303 tree identifier;
16304 tree qscope;
16305 int oldcount = errorcount;
16306 cp_token *diag_token = NULL;
16308 if (access_declaration_p)
16310 diag_token = cp_lexer_peek_token (parser->lexer);
16311 cp_parser_parse_tentatively (parser);
16313 else
16315 /* Look for the `using' keyword. */
16316 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16318 /* Peek at the next token. */
16319 token = cp_lexer_peek_token (parser->lexer);
16320 /* See if it's `typename'. */
16321 if (token->keyword == RID_TYPENAME)
16323 /* Remember that we've seen it. */
16324 typename_p = true;
16325 /* Consume the `typename' token. */
16326 cp_lexer_consume_token (parser->lexer);
16330 /* Look for the optional global scope qualification. */
16331 global_scope_p
16332 = (cp_parser_global_scope_opt (parser,
16333 /*current_scope_valid_p=*/false)
16334 != NULL_TREE);
16336 /* If we saw `typename', or didn't see `::', then there must be a
16337 nested-name-specifier present. */
16338 if (typename_p || !global_scope_p)
16340 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16341 /*check_dependency_p=*/true,
16342 /*type_p=*/false,
16343 /*is_declaration=*/true);
16344 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16346 cp_parser_skip_to_end_of_block_or_statement (parser);
16347 return false;
16350 /* Otherwise, we could be in either of the two productions. In that
16351 case, treat the nested-name-specifier as optional. */
16352 else
16353 qscope = cp_parser_nested_name_specifier_opt (parser,
16354 /*typename_keyword_p=*/false,
16355 /*check_dependency_p=*/true,
16356 /*type_p=*/false,
16357 /*is_declaration=*/true);
16358 if (!qscope)
16359 qscope = global_namespace;
16360 else if (UNSCOPED_ENUM_P (qscope))
16361 qscope = CP_TYPE_CONTEXT (qscope);
16363 if (access_declaration_p && cp_parser_error_occurred (parser))
16364 /* Something has already gone wrong; there's no need to parse
16365 further. Since an error has occurred, the return value of
16366 cp_parser_parse_definitely will be false, as required. */
16367 return cp_parser_parse_definitely (parser);
16369 token = cp_lexer_peek_token (parser->lexer);
16370 /* Parse the unqualified-id. */
16371 identifier = cp_parser_unqualified_id (parser,
16372 /*template_keyword_p=*/false,
16373 /*check_dependency_p=*/true,
16374 /*declarator_p=*/true,
16375 /*optional_p=*/false);
16377 if (access_declaration_p)
16379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16380 cp_parser_simulate_error (parser);
16381 if (!cp_parser_parse_definitely (parser))
16382 return false;
16385 /* The function we call to handle a using-declaration is different
16386 depending on what scope we are in. */
16387 if (qscope == error_mark_node || identifier == error_mark_node)
16389 else if (!identifier_p (identifier)
16390 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16391 /* [namespace.udecl]
16393 A using declaration shall not name a template-id. */
16394 error_at (token->location,
16395 "a template-id may not appear in a using-declaration");
16396 else
16398 if (at_class_scope_p ())
16400 /* Create the USING_DECL. */
16401 decl = do_class_using_decl (parser->scope, identifier);
16403 if (decl && typename_p)
16404 USING_DECL_TYPENAME_P (decl) = 1;
16406 if (check_for_bare_parameter_packs (decl))
16408 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16409 return false;
16411 else
16412 /* Add it to the list of members in this class. */
16413 finish_member_declaration (decl);
16415 else
16417 decl = cp_parser_lookup_name_simple (parser,
16418 identifier,
16419 token->location);
16420 if (decl == error_mark_node)
16421 cp_parser_name_lookup_error (parser, identifier,
16422 decl, NLE_NULL,
16423 token->location);
16424 else if (check_for_bare_parameter_packs (decl))
16426 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16427 return false;
16429 else if (!at_namespace_scope_p ())
16430 do_local_using_decl (decl, qscope, identifier);
16431 else
16432 do_toplevel_using_decl (decl, qscope, identifier);
16436 /* Look for the final `;'. */
16437 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16439 if (access_declaration_p && errorcount == oldcount)
16440 warning_at (diag_token->location, OPT_Wdeprecated,
16441 "access declarations are deprecated "
16442 "in favour of using-declarations; "
16443 "suggestion: add the %<using%> keyword");
16445 return true;
16448 /* Parse an alias-declaration.
16450 alias-declaration:
16451 using identifier attribute-specifier-seq [opt] = type-id */
16453 static tree
16454 cp_parser_alias_declaration (cp_parser* parser)
16456 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16457 location_t id_location;
16458 cp_declarator *declarator;
16459 cp_decl_specifier_seq decl_specs;
16460 bool member_p;
16461 const char *saved_message = NULL;
16463 /* Look for the `using' keyword. */
16464 cp_token *using_token
16465 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16466 if (using_token == NULL)
16467 return error_mark_node;
16469 id_location = cp_lexer_peek_token (parser->lexer)->location;
16470 id = cp_parser_identifier (parser);
16471 if (id == error_mark_node)
16472 return error_mark_node;
16474 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16475 attributes = cp_parser_attributes_opt (parser);
16476 if (attributes == error_mark_node)
16477 return error_mark_node;
16479 cp_parser_require (parser, CPP_EQ, RT_EQ);
16481 if (cp_parser_error_occurred (parser))
16482 return error_mark_node;
16484 cp_parser_commit_to_tentative_parse (parser);
16486 /* Now we are going to parse the type-id of the declaration. */
16489 [dcl.type]/3 says:
16491 "A type-specifier-seq shall not define a class or enumeration
16492 unless it appears in the type-id of an alias-declaration (7.1.3) that
16493 is not the declaration of a template-declaration."
16495 In other words, if we currently are in an alias template, the
16496 type-id should not define a type.
16498 So let's set parser->type_definition_forbidden_message in that
16499 case; cp_parser_check_type_definition (called by
16500 cp_parser_class_specifier) will then emit an error if a type is
16501 defined in the type-id. */
16502 if (parser->num_template_parameter_lists)
16504 saved_message = parser->type_definition_forbidden_message;
16505 parser->type_definition_forbidden_message =
16506 G_("types may not be defined in alias template declarations");
16509 type = cp_parser_type_id (parser);
16511 /* Restore the error message if need be. */
16512 if (parser->num_template_parameter_lists)
16513 parser->type_definition_forbidden_message = saved_message;
16515 if (type == error_mark_node
16516 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16518 cp_parser_skip_to_end_of_block_or_statement (parser);
16519 return error_mark_node;
16522 /* A typedef-name can also be introduced by an alias-declaration. The
16523 identifier following the using keyword becomes a typedef-name. It has
16524 the same semantics as if it were introduced by the typedef
16525 specifier. In particular, it does not define a new type and it shall
16526 not appear in the type-id. */
16528 clear_decl_specs (&decl_specs);
16529 decl_specs.type = type;
16530 if (attributes != NULL_TREE)
16532 decl_specs.attributes = attributes;
16533 set_and_check_decl_spec_loc (&decl_specs,
16534 ds_attribute,
16535 attrs_token);
16537 set_and_check_decl_spec_loc (&decl_specs,
16538 ds_typedef,
16539 using_token);
16540 set_and_check_decl_spec_loc (&decl_specs,
16541 ds_alias,
16542 using_token);
16544 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16545 declarator->id_loc = id_location;
16547 member_p = at_class_scope_p ();
16548 if (member_p)
16549 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16550 NULL_TREE, attributes);
16551 else
16552 decl = start_decl (declarator, &decl_specs, 0,
16553 attributes, NULL_TREE, &pushed_scope);
16554 if (decl == error_mark_node)
16555 return decl;
16557 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16559 if (pushed_scope)
16560 pop_scope (pushed_scope);
16562 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16563 added into the symbol table; otherwise, return the TYPE_DECL. */
16564 if (DECL_LANG_SPECIFIC (decl)
16565 && DECL_TEMPLATE_INFO (decl)
16566 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16568 decl = DECL_TI_TEMPLATE (decl);
16569 if (member_p)
16570 check_member_template (decl);
16573 return decl;
16576 /* Parse a using-directive.
16578 using-directive:
16579 using namespace :: [opt] nested-name-specifier [opt]
16580 namespace-name ; */
16582 static void
16583 cp_parser_using_directive (cp_parser* parser)
16585 tree namespace_decl;
16586 tree attribs;
16588 /* Look for the `using' keyword. */
16589 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16590 /* And the `namespace' keyword. */
16591 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16592 /* Look for the optional `::' operator. */
16593 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16594 /* And the optional nested-name-specifier. */
16595 cp_parser_nested_name_specifier_opt (parser,
16596 /*typename_keyword_p=*/false,
16597 /*check_dependency_p=*/true,
16598 /*type_p=*/false,
16599 /*is_declaration=*/true);
16600 /* Get the namespace being used. */
16601 namespace_decl = cp_parser_namespace_name (parser);
16602 /* And any specified attributes. */
16603 attribs = cp_parser_attributes_opt (parser);
16604 /* Update the symbol table. */
16605 parse_using_directive (namespace_decl, attribs);
16606 /* Look for the final `;'. */
16607 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16610 /* Parse an asm-definition.
16612 asm-definition:
16613 asm ( string-literal ) ;
16615 GNU Extension:
16617 asm-definition:
16618 asm volatile [opt] ( string-literal ) ;
16619 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16620 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16621 : asm-operand-list [opt] ) ;
16622 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16623 : asm-operand-list [opt]
16624 : asm-clobber-list [opt] ) ;
16625 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16626 : asm-clobber-list [opt]
16627 : asm-goto-list ) ; */
16629 static void
16630 cp_parser_asm_definition (cp_parser* parser)
16632 tree string;
16633 tree outputs = NULL_TREE;
16634 tree inputs = NULL_TREE;
16635 tree clobbers = NULL_TREE;
16636 tree labels = NULL_TREE;
16637 tree asm_stmt;
16638 bool volatile_p = false;
16639 bool extended_p = false;
16640 bool invalid_inputs_p = false;
16641 bool invalid_outputs_p = false;
16642 bool goto_p = false;
16643 required_token missing = RT_NONE;
16645 /* Look for the `asm' keyword. */
16646 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16648 if (parser->in_function_body
16649 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16651 error ("%<asm%> in %<constexpr%> function");
16652 cp_function_chain->invalid_constexpr = true;
16655 /* See if the next token is `volatile'. */
16656 if (cp_parser_allow_gnu_extensions_p (parser)
16657 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16659 /* Remember that we saw the `volatile' keyword. */
16660 volatile_p = true;
16661 /* Consume the token. */
16662 cp_lexer_consume_token (parser->lexer);
16664 if (cp_parser_allow_gnu_extensions_p (parser)
16665 && parser->in_function_body
16666 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16668 /* Remember that we saw the `goto' keyword. */
16669 goto_p = true;
16670 /* Consume the token. */
16671 cp_lexer_consume_token (parser->lexer);
16673 /* Look for the opening `('. */
16674 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16675 return;
16676 /* Look for the string. */
16677 string = cp_parser_string_literal (parser, false, false);
16678 if (string == error_mark_node)
16680 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16681 /*consume_paren=*/true);
16682 return;
16685 /* If we're allowing GNU extensions, check for the extended assembly
16686 syntax. Unfortunately, the `:' tokens need not be separated by
16687 a space in C, and so, for compatibility, we tolerate that here
16688 too. Doing that means that we have to treat the `::' operator as
16689 two `:' tokens. */
16690 if (cp_parser_allow_gnu_extensions_p (parser)
16691 && parser->in_function_body
16692 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16693 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16695 bool inputs_p = false;
16696 bool clobbers_p = false;
16697 bool labels_p = false;
16699 /* The extended syntax was used. */
16700 extended_p = true;
16702 /* Look for outputs. */
16703 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16705 /* Consume the `:'. */
16706 cp_lexer_consume_token (parser->lexer);
16707 /* Parse the output-operands. */
16708 if (cp_lexer_next_token_is_not (parser->lexer,
16709 CPP_COLON)
16710 && cp_lexer_next_token_is_not (parser->lexer,
16711 CPP_SCOPE)
16712 && cp_lexer_next_token_is_not (parser->lexer,
16713 CPP_CLOSE_PAREN)
16714 && !goto_p)
16715 outputs = cp_parser_asm_operand_list (parser);
16717 if (outputs == error_mark_node)
16718 invalid_outputs_p = true;
16720 /* If the next token is `::', there are no outputs, and the
16721 next token is the beginning of the inputs. */
16722 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16723 /* The inputs are coming next. */
16724 inputs_p = true;
16726 /* Look for inputs. */
16727 if (inputs_p
16728 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16730 /* Consume the `:' or `::'. */
16731 cp_lexer_consume_token (parser->lexer);
16732 /* Parse the output-operands. */
16733 if (cp_lexer_next_token_is_not (parser->lexer,
16734 CPP_COLON)
16735 && cp_lexer_next_token_is_not (parser->lexer,
16736 CPP_SCOPE)
16737 && cp_lexer_next_token_is_not (parser->lexer,
16738 CPP_CLOSE_PAREN))
16739 inputs = cp_parser_asm_operand_list (parser);
16741 if (inputs == error_mark_node)
16742 invalid_inputs_p = true;
16744 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16745 /* The clobbers are coming next. */
16746 clobbers_p = true;
16748 /* Look for clobbers. */
16749 if (clobbers_p
16750 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16752 clobbers_p = true;
16753 /* Consume the `:' or `::'. */
16754 cp_lexer_consume_token (parser->lexer);
16755 /* Parse the clobbers. */
16756 if (cp_lexer_next_token_is_not (parser->lexer,
16757 CPP_COLON)
16758 && cp_lexer_next_token_is_not (parser->lexer,
16759 CPP_CLOSE_PAREN))
16760 clobbers = cp_parser_asm_clobber_list (parser);
16762 else if (goto_p
16763 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16764 /* The labels are coming next. */
16765 labels_p = true;
16767 /* Look for labels. */
16768 if (labels_p
16769 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16771 labels_p = true;
16772 /* Consume the `:' or `::'. */
16773 cp_lexer_consume_token (parser->lexer);
16774 /* Parse the labels. */
16775 labels = cp_parser_asm_label_list (parser);
16778 if (goto_p && !labels_p)
16779 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16781 else if (goto_p)
16782 missing = RT_COLON_SCOPE;
16784 /* Look for the closing `)'. */
16785 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16786 missing ? missing : RT_CLOSE_PAREN))
16787 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16788 /*consume_paren=*/true);
16789 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16791 if (!invalid_inputs_p && !invalid_outputs_p)
16793 /* Create the ASM_EXPR. */
16794 if (parser->in_function_body)
16796 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16797 inputs, clobbers, labels);
16798 /* If the extended syntax was not used, mark the ASM_EXPR. */
16799 if (!extended_p)
16801 tree temp = asm_stmt;
16802 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16803 temp = TREE_OPERAND (temp, 0);
16805 ASM_INPUT_P (temp) = 1;
16808 else
16809 symtab->finalize_toplevel_asm (string);
16813 /* Declarators [gram.dcl.decl] */
16815 /* Parse an init-declarator.
16817 init-declarator:
16818 declarator initializer [opt]
16820 GNU Extension:
16822 init-declarator:
16823 declarator asm-specification [opt] attributes [opt] initializer [opt]
16825 function-definition:
16826 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16827 function-body
16828 decl-specifier-seq [opt] declarator function-try-block
16830 GNU Extension:
16832 function-definition:
16833 __extension__ function-definition
16835 TM Extension:
16837 function-definition:
16838 decl-specifier-seq [opt] declarator function-transaction-block
16840 The DECL_SPECIFIERS apply to this declarator. Returns a
16841 representation of the entity declared. If MEMBER_P is TRUE, then
16842 this declarator appears in a class scope. The new DECL created by
16843 this declarator is returned.
16845 The CHECKS are access checks that should be performed once we know
16846 what entity is being declared (and, therefore, what classes have
16847 befriended it).
16849 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16850 for a function-definition here as well. If the declarator is a
16851 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16852 be TRUE upon return. By that point, the function-definition will
16853 have been completely parsed.
16855 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16856 is FALSE.
16858 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16859 parsed declaration if it is an uninitialized single declarator not followed
16860 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16861 if present, will not be consumed. If returned, this declarator will be
16862 created with SD_INITIALIZED but will not call cp_finish_decl.
16864 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16865 and there is an initializer, the pointed location_t is set to the
16866 location of the '=' or `(', or '{' in C++11 token introducing the
16867 initializer. */
16869 static tree
16870 cp_parser_init_declarator (cp_parser* parser,
16871 cp_decl_specifier_seq *decl_specifiers,
16872 vec<deferred_access_check, va_gc> *checks,
16873 bool function_definition_allowed_p,
16874 bool member_p,
16875 int declares_class_or_enum,
16876 bool* function_definition_p,
16877 tree* maybe_range_for_decl,
16878 location_t* init_loc)
16880 cp_token *token = NULL, *asm_spec_start_token = NULL,
16881 *attributes_start_token = NULL;
16882 cp_declarator *declarator;
16883 tree prefix_attributes;
16884 tree attributes = NULL;
16885 tree asm_specification;
16886 tree initializer;
16887 tree decl = NULL_TREE;
16888 tree scope;
16889 int is_initialized;
16890 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16891 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16892 "(...)". */
16893 enum cpp_ttype initialization_kind;
16894 bool is_direct_init = false;
16895 bool is_non_constant_init;
16896 int ctor_dtor_or_conv_p;
16897 bool friend_p = cp_parser_friend_p (decl_specifiers);
16898 tree pushed_scope = NULL_TREE;
16899 bool range_for_decl_p = false;
16900 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16901 location_t tmp_init_loc = UNKNOWN_LOCATION;
16903 /* Gather the attributes that were provided with the
16904 decl-specifiers. */
16905 prefix_attributes = decl_specifiers->attributes;
16907 /* Assume that this is not the declarator for a function
16908 definition. */
16909 if (function_definition_p)
16910 *function_definition_p = false;
16912 /* Default arguments are only permitted for function parameters. */
16913 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16914 parser->default_arg_ok_p = false;
16916 /* Defer access checks while parsing the declarator; we cannot know
16917 what names are accessible until we know what is being
16918 declared. */
16919 resume_deferring_access_checks ();
16921 /* Parse the declarator. */
16922 token = cp_lexer_peek_token (parser->lexer);
16923 declarator
16924 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16925 &ctor_dtor_or_conv_p,
16926 /*parenthesized_p=*/NULL,
16927 member_p, friend_p);
16928 /* Gather up the deferred checks. */
16929 stop_deferring_access_checks ();
16931 parser->default_arg_ok_p = saved_default_arg_ok_p;
16933 /* If the DECLARATOR was erroneous, there's no need to go
16934 further. */
16935 if (declarator == cp_error_declarator)
16936 return error_mark_node;
16938 /* Check that the number of template-parameter-lists is OK. */
16939 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16940 token->location))
16941 return error_mark_node;
16943 if (declares_class_or_enum & 2)
16944 cp_parser_check_for_definition_in_return_type (declarator,
16945 decl_specifiers->type,
16946 decl_specifiers->locations[ds_type_spec]);
16948 /* Figure out what scope the entity declared by the DECLARATOR is
16949 located in. `grokdeclarator' sometimes changes the scope, so
16950 we compute it now. */
16951 scope = get_scope_of_declarator (declarator);
16953 /* Perform any lookups in the declared type which were thought to be
16954 dependent, but are not in the scope of the declarator. */
16955 decl_specifiers->type
16956 = maybe_update_decl_type (decl_specifiers->type, scope);
16958 /* If we're allowing GNU extensions, look for an
16959 asm-specification. */
16960 if (cp_parser_allow_gnu_extensions_p (parser))
16962 /* Look for an asm-specification. */
16963 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16964 asm_specification = cp_parser_asm_specification_opt (parser);
16966 else
16967 asm_specification = NULL_TREE;
16969 /* Look for attributes. */
16970 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16971 attributes = cp_parser_attributes_opt (parser);
16973 /* Peek at the next token. */
16974 token = cp_lexer_peek_token (parser->lexer);
16976 bool bogus_implicit_tmpl = false;
16978 if (function_declarator_p (declarator))
16980 /* Check to see if the token indicates the start of a
16981 function-definition. */
16982 if (cp_parser_token_starts_function_definition_p (token))
16984 if (!function_definition_allowed_p)
16986 /* If a function-definition should not appear here, issue an
16987 error message. */
16988 cp_parser_error (parser,
16989 "a function-definition is not allowed here");
16990 return error_mark_node;
16993 location_t func_brace_location
16994 = cp_lexer_peek_token (parser->lexer)->location;
16996 /* Neither attributes nor an asm-specification are allowed
16997 on a function-definition. */
16998 if (asm_specification)
16999 error_at (asm_spec_start_token->location,
17000 "an asm-specification is not allowed "
17001 "on a function-definition");
17002 if (attributes)
17003 error_at (attributes_start_token->location,
17004 "attributes are not allowed "
17005 "on a function-definition");
17006 /* This is a function-definition. */
17007 *function_definition_p = true;
17009 /* Parse the function definition. */
17010 if (member_p)
17011 decl = cp_parser_save_member_function_body (parser,
17012 decl_specifiers,
17013 declarator,
17014 prefix_attributes);
17015 else
17016 decl =
17017 (cp_parser_function_definition_from_specifiers_and_declarator
17018 (parser, decl_specifiers, prefix_attributes, declarator));
17020 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17022 /* This is where the prologue starts... */
17023 DECL_STRUCT_FUNCTION (decl)->function_start_locus
17024 = func_brace_location;
17027 return decl;
17030 else if (parser->fully_implicit_function_template_p)
17032 /* A non-template declaration involving a function parameter list
17033 containing an implicit template parameter will be made into a
17034 template. If the resulting declaration is not going to be an
17035 actual function then finish the template scope here to prevent it.
17036 An error message will be issued once we have a decl to talk about.
17038 FIXME probably we should do type deduction rather than create an
17039 implicit template, but the standard currently doesn't allow it. */
17040 bogus_implicit_tmpl = true;
17041 finish_fully_implicit_template (parser, NULL_TREE);
17044 /* [dcl.dcl]
17046 Only in function declarations for constructors, destructors, and
17047 type conversions can the decl-specifier-seq be omitted.
17049 We explicitly postpone this check past the point where we handle
17050 function-definitions because we tolerate function-definitions
17051 that are missing their return types in some modes. */
17052 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17054 cp_parser_error (parser,
17055 "expected constructor, destructor, or type conversion");
17056 return error_mark_node;
17059 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
17060 if (token->type == CPP_EQ
17061 || token->type == CPP_OPEN_PAREN
17062 || token->type == CPP_OPEN_BRACE)
17064 is_initialized = SD_INITIALIZED;
17065 initialization_kind = token->type;
17066 if (maybe_range_for_decl)
17067 *maybe_range_for_decl = error_mark_node;
17068 tmp_init_loc = token->location;
17069 if (init_loc && *init_loc == UNKNOWN_LOCATION)
17070 *init_loc = tmp_init_loc;
17072 if (token->type == CPP_EQ
17073 && function_declarator_p (declarator))
17075 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17076 if (t2->keyword == RID_DEFAULT)
17077 is_initialized = SD_DEFAULTED;
17078 else if (t2->keyword == RID_DELETE)
17079 is_initialized = SD_DELETED;
17082 else
17084 /* If the init-declarator isn't initialized and isn't followed by a
17085 `,' or `;', it's not a valid init-declarator. */
17086 if (token->type != CPP_COMMA
17087 && token->type != CPP_SEMICOLON)
17089 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17090 range_for_decl_p = true;
17091 else
17093 if (!maybe_range_for_decl)
17094 cp_parser_error (parser, "expected initializer");
17095 return error_mark_node;
17098 is_initialized = SD_UNINITIALIZED;
17099 initialization_kind = CPP_EOF;
17102 /* Because start_decl has side-effects, we should only call it if we
17103 know we're going ahead. By this point, we know that we cannot
17104 possibly be looking at any other construct. */
17105 cp_parser_commit_to_tentative_parse (parser);
17107 /* Enter the newly declared entry in the symbol table. If we're
17108 processing a declaration in a class-specifier, we wait until
17109 after processing the initializer. */
17110 if (!member_p)
17112 if (parser->in_unbraced_linkage_specification_p)
17113 decl_specifiers->storage_class = sc_extern;
17114 decl = start_decl (declarator, decl_specifiers,
17115 range_for_decl_p? SD_INITIALIZED : is_initialized,
17116 attributes, prefix_attributes, &pushed_scope);
17117 cp_finalize_omp_declare_simd (parser, decl);
17118 /* Adjust location of decl if declarator->id_loc is more appropriate:
17119 set, and decl wasn't merged with another decl, in which case its
17120 location would be different from input_location, and more accurate. */
17121 if (DECL_P (decl)
17122 && declarator->id_loc != UNKNOWN_LOCATION
17123 && DECL_SOURCE_LOCATION (decl) == input_location)
17124 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17126 else if (scope)
17127 /* Enter the SCOPE. That way unqualified names appearing in the
17128 initializer will be looked up in SCOPE. */
17129 pushed_scope = push_scope (scope);
17131 /* Perform deferred access control checks, now that we know in which
17132 SCOPE the declared entity resides. */
17133 if (!member_p && decl)
17135 tree saved_current_function_decl = NULL_TREE;
17137 /* If the entity being declared is a function, pretend that we
17138 are in its scope. If it is a `friend', it may have access to
17139 things that would not otherwise be accessible. */
17140 if (TREE_CODE (decl) == FUNCTION_DECL)
17142 saved_current_function_decl = current_function_decl;
17143 current_function_decl = decl;
17146 /* Perform access checks for template parameters. */
17147 cp_parser_perform_template_parameter_access_checks (checks);
17149 /* Perform the access control checks for the declarator and the
17150 decl-specifiers. */
17151 perform_deferred_access_checks (tf_warning_or_error);
17153 /* Restore the saved value. */
17154 if (TREE_CODE (decl) == FUNCTION_DECL)
17155 current_function_decl = saved_current_function_decl;
17158 /* Parse the initializer. */
17159 initializer = NULL_TREE;
17160 is_direct_init = false;
17161 is_non_constant_init = true;
17162 if (is_initialized)
17164 if (function_declarator_p (declarator))
17166 if (initialization_kind == CPP_EQ)
17167 initializer = cp_parser_pure_specifier (parser);
17168 else
17170 /* If the declaration was erroneous, we don't really
17171 know what the user intended, so just silently
17172 consume the initializer. */
17173 if (decl != error_mark_node)
17174 error_at (tmp_init_loc, "initializer provided for function");
17175 cp_parser_skip_to_closing_parenthesis (parser,
17176 /*recovering=*/true,
17177 /*or_comma=*/false,
17178 /*consume_paren=*/true);
17181 else
17183 /* We want to record the extra mangling scope for in-class
17184 initializers of class members and initializers of static data
17185 member templates. The former involves deferring
17186 parsing of the initializer until end of class as with default
17187 arguments. So right here we only handle the latter. */
17188 if (!member_p && processing_template_decl)
17189 start_lambda_scope (decl);
17190 initializer = cp_parser_initializer (parser,
17191 &is_direct_init,
17192 &is_non_constant_init);
17193 if (!member_p && processing_template_decl)
17194 finish_lambda_scope ();
17195 if (initializer == error_mark_node)
17196 cp_parser_skip_to_end_of_statement (parser);
17200 /* The old parser allows attributes to appear after a parenthesized
17201 initializer. Mark Mitchell proposed removing this functionality
17202 on the GCC mailing lists on 2002-08-13. This parser accepts the
17203 attributes -- but ignores them. */
17204 if (cp_parser_allow_gnu_extensions_p (parser)
17205 && initialization_kind == CPP_OPEN_PAREN)
17206 if (cp_parser_attributes_opt (parser))
17207 warning (OPT_Wattributes,
17208 "attributes after parenthesized initializer ignored");
17210 /* And now complain about a non-function implicit template. */
17211 if (bogus_implicit_tmpl)
17212 error_at (DECL_SOURCE_LOCATION (decl),
17213 "non-function %qD declared as implicit template", decl);
17215 /* For an in-class declaration, use `grokfield' to create the
17216 declaration. */
17217 if (member_p)
17219 if (pushed_scope)
17221 pop_scope (pushed_scope);
17222 pushed_scope = NULL_TREE;
17224 decl = grokfield (declarator, decl_specifiers,
17225 initializer, !is_non_constant_init,
17226 /*asmspec=*/NULL_TREE,
17227 chainon (attributes, prefix_attributes));
17228 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17229 cp_parser_save_default_args (parser, decl);
17230 cp_finalize_omp_declare_simd (parser, decl);
17233 /* Finish processing the declaration. But, skip member
17234 declarations. */
17235 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17237 cp_finish_decl (decl,
17238 initializer, !is_non_constant_init,
17239 asm_specification,
17240 /* If the initializer is in parentheses, then this is
17241 a direct-initialization, which means that an
17242 `explicit' constructor is OK. Otherwise, an
17243 `explicit' constructor cannot be used. */
17244 ((is_direct_init || !is_initialized)
17245 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17247 else if ((cxx_dialect != cxx98) && friend_p
17248 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17249 /* Core issue #226 (C++0x only): A default template-argument
17250 shall not be specified in a friend class template
17251 declaration. */
17252 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17253 /*is_partial=*/false, /*is_friend_decl=*/1);
17255 if (!friend_p && pushed_scope)
17256 pop_scope (pushed_scope);
17258 if (function_declarator_p (declarator)
17259 && parser->fully_implicit_function_template_p)
17261 if (member_p)
17262 decl = finish_fully_implicit_template (parser, decl);
17263 else
17264 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17267 return decl;
17270 /* Parse a declarator.
17272 declarator:
17273 direct-declarator
17274 ptr-operator declarator
17276 abstract-declarator:
17277 ptr-operator abstract-declarator [opt]
17278 direct-abstract-declarator
17280 GNU Extensions:
17282 declarator:
17283 attributes [opt] direct-declarator
17284 attributes [opt] ptr-operator declarator
17286 abstract-declarator:
17287 attributes [opt] ptr-operator abstract-declarator [opt]
17288 attributes [opt] direct-abstract-declarator
17290 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17291 detect constructor, destructor or conversion operators. It is set
17292 to -1 if the declarator is a name, and +1 if it is a
17293 function. Otherwise it is set to zero. Usually you just want to
17294 test for >0, but internally the negative value is used.
17296 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17297 a decl-specifier-seq unless it declares a constructor, destructor,
17298 or conversion. It might seem that we could check this condition in
17299 semantic analysis, rather than parsing, but that makes it difficult
17300 to handle something like `f()'. We want to notice that there are
17301 no decl-specifiers, and therefore realize that this is an
17302 expression, not a declaration.)
17304 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17305 the declarator is a direct-declarator of the form "(...)".
17307 MEMBER_P is true iff this declarator is a member-declarator.
17309 FRIEND_P is true iff this declarator is a friend. */
17311 static cp_declarator *
17312 cp_parser_declarator (cp_parser* parser,
17313 cp_parser_declarator_kind dcl_kind,
17314 int* ctor_dtor_or_conv_p,
17315 bool* parenthesized_p,
17316 bool member_p, bool friend_p)
17318 cp_declarator *declarator;
17319 enum tree_code code;
17320 cp_cv_quals cv_quals;
17321 tree class_type;
17322 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17324 /* Assume this is not a constructor, destructor, or type-conversion
17325 operator. */
17326 if (ctor_dtor_or_conv_p)
17327 *ctor_dtor_or_conv_p = 0;
17329 if (cp_parser_allow_gnu_extensions_p (parser))
17330 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17332 /* Check for the ptr-operator production. */
17333 cp_parser_parse_tentatively (parser);
17334 /* Parse the ptr-operator. */
17335 code = cp_parser_ptr_operator (parser,
17336 &class_type,
17337 &cv_quals,
17338 &std_attributes);
17340 /* If that worked, then we have a ptr-operator. */
17341 if (cp_parser_parse_definitely (parser))
17343 /* If a ptr-operator was found, then this declarator was not
17344 parenthesized. */
17345 if (parenthesized_p)
17346 *parenthesized_p = true;
17347 /* The dependent declarator is optional if we are parsing an
17348 abstract-declarator. */
17349 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17350 cp_parser_parse_tentatively (parser);
17352 /* Parse the dependent declarator. */
17353 declarator = cp_parser_declarator (parser, dcl_kind,
17354 /*ctor_dtor_or_conv_p=*/NULL,
17355 /*parenthesized_p=*/NULL,
17356 /*member_p=*/false,
17357 friend_p);
17359 /* If we are parsing an abstract-declarator, we must handle the
17360 case where the dependent declarator is absent. */
17361 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17362 && !cp_parser_parse_definitely (parser))
17363 declarator = NULL;
17365 declarator = cp_parser_make_indirect_declarator
17366 (code, class_type, cv_quals, declarator, std_attributes);
17368 /* Everything else is a direct-declarator. */
17369 else
17371 if (parenthesized_p)
17372 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17373 CPP_OPEN_PAREN);
17374 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17375 ctor_dtor_or_conv_p,
17376 member_p, friend_p);
17379 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17380 declarator->attributes = gnu_attributes;
17381 return declarator;
17384 /* Parse a direct-declarator or direct-abstract-declarator.
17386 direct-declarator:
17387 declarator-id
17388 direct-declarator ( parameter-declaration-clause )
17389 cv-qualifier-seq [opt]
17390 ref-qualifier [opt]
17391 exception-specification [opt]
17392 direct-declarator [ constant-expression [opt] ]
17393 ( declarator )
17395 direct-abstract-declarator:
17396 direct-abstract-declarator [opt]
17397 ( parameter-declaration-clause )
17398 cv-qualifier-seq [opt]
17399 ref-qualifier [opt]
17400 exception-specification [opt]
17401 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17402 ( abstract-declarator )
17404 Returns a representation of the declarator. DCL_KIND is
17405 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17406 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17407 we are parsing a direct-declarator. It is
17408 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17409 of ambiguity we prefer an abstract declarator, as per
17410 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17411 as for cp_parser_declarator. */
17413 static cp_declarator *
17414 cp_parser_direct_declarator (cp_parser* parser,
17415 cp_parser_declarator_kind dcl_kind,
17416 int* ctor_dtor_or_conv_p,
17417 bool member_p, bool friend_p)
17419 cp_token *token;
17420 cp_declarator *declarator = NULL;
17421 tree scope = NULL_TREE;
17422 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17423 bool saved_in_declarator_p = parser->in_declarator_p;
17424 bool first = true;
17425 tree pushed_scope = NULL_TREE;
17427 while (true)
17429 /* Peek at the next token. */
17430 token = cp_lexer_peek_token (parser->lexer);
17431 if (token->type == CPP_OPEN_PAREN)
17433 /* This is either a parameter-declaration-clause, or a
17434 parenthesized declarator. When we know we are parsing a
17435 named declarator, it must be a parenthesized declarator
17436 if FIRST is true. For instance, `(int)' is a
17437 parameter-declaration-clause, with an omitted
17438 direct-abstract-declarator. But `((*))', is a
17439 parenthesized abstract declarator. Finally, when T is a
17440 template parameter `(T)' is a
17441 parameter-declaration-clause, and not a parenthesized
17442 named declarator.
17444 We first try and parse a parameter-declaration-clause,
17445 and then try a nested declarator (if FIRST is true).
17447 It is not an error for it not to be a
17448 parameter-declaration-clause, even when FIRST is
17449 false. Consider,
17451 int i (int);
17452 int i (3);
17454 The first is the declaration of a function while the
17455 second is the definition of a variable, including its
17456 initializer.
17458 Having seen only the parenthesis, we cannot know which of
17459 these two alternatives should be selected. Even more
17460 complex are examples like:
17462 int i (int (a));
17463 int i (int (3));
17465 The former is a function-declaration; the latter is a
17466 variable initialization.
17468 Thus again, we try a parameter-declaration-clause, and if
17469 that fails, we back out and return. */
17471 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17473 tree params;
17474 bool is_declarator = false;
17476 /* In a member-declarator, the only valid interpretation
17477 of a parenthesis is the start of a
17478 parameter-declaration-clause. (It is invalid to
17479 initialize a static data member with a parenthesized
17480 initializer; only the "=" form of initialization is
17481 permitted.) */
17482 if (!member_p)
17483 cp_parser_parse_tentatively (parser);
17485 /* Consume the `('. */
17486 cp_lexer_consume_token (parser->lexer);
17487 if (first)
17489 /* If this is going to be an abstract declarator, we're
17490 in a declarator and we can't have default args. */
17491 parser->default_arg_ok_p = false;
17492 parser->in_declarator_p = true;
17495 begin_scope (sk_function_parms, NULL_TREE);
17497 /* Parse the parameter-declaration-clause. */
17498 params = cp_parser_parameter_declaration_clause (parser);
17500 /* Consume the `)'. */
17501 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17503 /* If all went well, parse the cv-qualifier-seq,
17504 ref-qualifier and the exception-specification. */
17505 if (member_p || cp_parser_parse_definitely (parser))
17507 cp_cv_quals cv_quals;
17508 cp_virt_specifiers virt_specifiers;
17509 cp_ref_qualifier ref_qual;
17510 tree exception_specification;
17511 tree late_return;
17512 tree attrs;
17513 bool memfn = (member_p || (pushed_scope
17514 && CLASS_TYPE_P (pushed_scope)));
17516 is_declarator = true;
17518 if (ctor_dtor_or_conv_p)
17519 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17520 first = false;
17522 /* Parse the cv-qualifier-seq. */
17523 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17524 /* Parse the ref-qualifier. */
17525 ref_qual = cp_parser_ref_qualifier_opt (parser);
17526 /* And the exception-specification. */
17527 exception_specification
17528 = cp_parser_exception_specification_opt (parser);
17530 attrs = cp_parser_std_attribute_spec_seq (parser);
17532 /* In here, we handle cases where attribute is used after
17533 the function declaration. For example:
17534 void func (int x) __attribute__((vector(..))); */
17535 if (flag_cilkplus
17536 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17538 cp_parser_parse_tentatively (parser);
17539 tree attr = cp_parser_gnu_attributes_opt (parser);
17540 if (cp_lexer_next_token_is_not (parser->lexer,
17541 CPP_SEMICOLON)
17542 && cp_lexer_next_token_is_not (parser->lexer,
17543 CPP_OPEN_BRACE))
17544 cp_parser_abort_tentative_parse (parser);
17545 else if (!cp_parser_parse_definitely (parser))
17547 else
17548 attrs = chainon (attr, attrs);
17550 late_return = (cp_parser_late_return_type_opt
17551 (parser, declarator,
17552 memfn ? cv_quals : -1));
17555 /* Parse the virt-specifier-seq. */
17556 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17558 /* Create the function-declarator. */
17559 declarator = make_call_declarator (declarator,
17560 params,
17561 cv_quals,
17562 virt_specifiers,
17563 ref_qual,
17564 exception_specification,
17565 late_return);
17566 declarator->std_attributes = attrs;
17567 /* Any subsequent parameter lists are to do with
17568 return type, so are not those of the declared
17569 function. */
17570 parser->default_arg_ok_p = false;
17573 /* Remove the function parms from scope. */
17574 pop_bindings_and_leave_scope ();
17576 if (is_declarator)
17577 /* Repeat the main loop. */
17578 continue;
17581 /* If this is the first, we can try a parenthesized
17582 declarator. */
17583 if (first)
17585 bool saved_in_type_id_in_expr_p;
17587 parser->default_arg_ok_p = saved_default_arg_ok_p;
17588 parser->in_declarator_p = saved_in_declarator_p;
17590 /* Consume the `('. */
17591 cp_lexer_consume_token (parser->lexer);
17592 /* Parse the nested declarator. */
17593 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17594 parser->in_type_id_in_expr_p = true;
17595 declarator
17596 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17597 /*parenthesized_p=*/NULL,
17598 member_p, friend_p);
17599 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17600 first = false;
17601 /* Expect a `)'. */
17602 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17603 declarator = cp_error_declarator;
17604 if (declarator == cp_error_declarator)
17605 break;
17607 goto handle_declarator;
17609 /* Otherwise, we must be done. */
17610 else
17611 break;
17613 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17614 && token->type == CPP_OPEN_SQUARE
17615 && !cp_next_tokens_can_be_attribute_p (parser))
17617 /* Parse an array-declarator. */
17618 tree bounds, attrs;
17620 if (ctor_dtor_or_conv_p)
17621 *ctor_dtor_or_conv_p = 0;
17623 first = false;
17624 parser->default_arg_ok_p = false;
17625 parser->in_declarator_p = true;
17626 /* Consume the `['. */
17627 cp_lexer_consume_token (parser->lexer);
17628 /* Peek at the next token. */
17629 token = cp_lexer_peek_token (parser->lexer);
17630 /* If the next token is `]', then there is no
17631 constant-expression. */
17632 if (token->type != CPP_CLOSE_SQUARE)
17634 bool non_constant_p;
17635 bounds
17636 = cp_parser_constant_expression (parser,
17637 /*allow_non_constant=*/true,
17638 &non_constant_p);
17639 if (!non_constant_p)
17640 /* OK */;
17641 else if (error_operand_p (bounds))
17642 /* Already gave an error. */;
17643 else if (!parser->in_function_body
17644 || current_binding_level->kind == sk_function_parms)
17646 /* Normally, the array bound must be an integral constant
17647 expression. However, as an extension, we allow VLAs
17648 in function scopes as long as they aren't part of a
17649 parameter declaration. */
17650 cp_parser_error (parser,
17651 "array bound is not an integer constant");
17652 bounds = error_mark_node;
17654 else if (processing_template_decl
17655 && !type_dependent_expression_p (bounds))
17657 /* Remember this wasn't a constant-expression. */
17658 bounds = build_nop (TREE_TYPE (bounds), bounds);
17659 TREE_SIDE_EFFECTS (bounds) = 1;
17662 else
17663 bounds = NULL_TREE;
17664 /* Look for the closing `]'. */
17665 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17667 declarator = cp_error_declarator;
17668 break;
17671 attrs = cp_parser_std_attribute_spec_seq (parser);
17672 declarator = make_array_declarator (declarator, bounds);
17673 declarator->std_attributes = attrs;
17675 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17678 tree qualifying_scope;
17679 tree unqualified_name;
17680 tree attrs;
17681 special_function_kind sfk;
17682 bool abstract_ok;
17683 bool pack_expansion_p = false;
17684 cp_token *declarator_id_start_token;
17686 /* Parse a declarator-id */
17687 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17688 if (abstract_ok)
17690 cp_parser_parse_tentatively (parser);
17692 /* If we see an ellipsis, we should be looking at a
17693 parameter pack. */
17694 if (token->type == CPP_ELLIPSIS)
17696 /* Consume the `...' */
17697 cp_lexer_consume_token (parser->lexer);
17699 pack_expansion_p = true;
17703 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17704 unqualified_name
17705 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17706 qualifying_scope = parser->scope;
17707 if (abstract_ok)
17709 bool okay = false;
17711 if (!unqualified_name && pack_expansion_p)
17713 /* Check whether an error occurred. */
17714 okay = !cp_parser_error_occurred (parser);
17716 /* We already consumed the ellipsis to mark a
17717 parameter pack, but we have no way to report it,
17718 so abort the tentative parse. We will be exiting
17719 immediately anyway. */
17720 cp_parser_abort_tentative_parse (parser);
17722 else
17723 okay = cp_parser_parse_definitely (parser);
17725 if (!okay)
17726 unqualified_name = error_mark_node;
17727 else if (unqualified_name
17728 && (qualifying_scope
17729 || (!identifier_p (unqualified_name))))
17731 cp_parser_error (parser, "expected unqualified-id");
17732 unqualified_name = error_mark_node;
17736 if (!unqualified_name)
17737 return NULL;
17738 if (unqualified_name == error_mark_node)
17740 declarator = cp_error_declarator;
17741 pack_expansion_p = false;
17742 declarator->parameter_pack_p = false;
17743 break;
17746 attrs = cp_parser_std_attribute_spec_seq (parser);
17748 if (qualifying_scope && at_namespace_scope_p ()
17749 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17751 /* In the declaration of a member of a template class
17752 outside of the class itself, the SCOPE will sometimes
17753 be a TYPENAME_TYPE. For example, given:
17755 template <typename T>
17756 int S<T>::R::i = 3;
17758 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17759 this context, we must resolve S<T>::R to an ordinary
17760 type, rather than a typename type.
17762 The reason we normally avoid resolving TYPENAME_TYPEs
17763 is that a specialization of `S' might render
17764 `S<T>::R' not a type. However, if `S' is
17765 specialized, then this `i' will not be used, so there
17766 is no harm in resolving the types here. */
17767 tree type;
17769 /* Resolve the TYPENAME_TYPE. */
17770 type = resolve_typename_type (qualifying_scope,
17771 /*only_current_p=*/false);
17772 /* If that failed, the declarator is invalid. */
17773 if (TREE_CODE (type) == TYPENAME_TYPE)
17775 if (typedef_variant_p (type))
17776 error_at (declarator_id_start_token->location,
17777 "cannot define member of dependent typedef "
17778 "%qT", type);
17779 else
17780 error_at (declarator_id_start_token->location,
17781 "%<%T::%E%> is not a type",
17782 TYPE_CONTEXT (qualifying_scope),
17783 TYPE_IDENTIFIER (qualifying_scope));
17785 qualifying_scope = type;
17788 sfk = sfk_none;
17790 if (unqualified_name)
17792 tree class_type;
17794 if (qualifying_scope
17795 && CLASS_TYPE_P (qualifying_scope))
17796 class_type = qualifying_scope;
17797 else
17798 class_type = current_class_type;
17800 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17802 tree name_type = TREE_TYPE (unqualified_name);
17803 if (class_type && same_type_p (name_type, class_type))
17805 if (qualifying_scope
17806 && CLASSTYPE_USE_TEMPLATE (name_type))
17808 error_at (declarator_id_start_token->location,
17809 "invalid use of constructor as a template");
17810 inform (declarator_id_start_token->location,
17811 "use %<%T::%D%> instead of %<%T::%D%> to "
17812 "name the constructor in a qualified name",
17813 class_type,
17814 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17815 class_type, name_type);
17816 declarator = cp_error_declarator;
17817 break;
17819 else
17820 unqualified_name = constructor_name (class_type);
17822 else
17824 /* We do not attempt to print the declarator
17825 here because we do not have enough
17826 information about its original syntactic
17827 form. */
17828 cp_parser_error (parser, "invalid declarator");
17829 declarator = cp_error_declarator;
17830 break;
17834 if (class_type)
17836 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17837 sfk = sfk_destructor;
17838 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17839 sfk = sfk_conversion;
17840 else if (/* There's no way to declare a constructor
17841 for an anonymous type, even if the type
17842 got a name for linkage purposes. */
17843 !TYPE_WAS_ANONYMOUS (class_type)
17844 /* Handle correctly (c++/19200):
17846 struct S {
17847 struct T{};
17848 friend void S(T);
17851 and also:
17853 namespace N {
17854 void S();
17857 struct S {
17858 friend void N::S();
17859 }; */
17860 && !(friend_p
17861 && class_type != qualifying_scope)
17862 && constructor_name_p (unqualified_name,
17863 class_type))
17865 unqualified_name = constructor_name (class_type);
17866 sfk = sfk_constructor;
17868 else if (is_overloaded_fn (unqualified_name)
17869 && DECL_CONSTRUCTOR_P (get_first_fn
17870 (unqualified_name)))
17871 sfk = sfk_constructor;
17873 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17874 *ctor_dtor_or_conv_p = -1;
17877 declarator = make_id_declarator (qualifying_scope,
17878 unqualified_name,
17879 sfk);
17880 declarator->std_attributes = attrs;
17881 declarator->id_loc = token->location;
17882 declarator->parameter_pack_p = pack_expansion_p;
17884 if (pack_expansion_p)
17885 maybe_warn_variadic_templates ();
17888 handle_declarator:;
17889 scope = get_scope_of_declarator (declarator);
17890 if (scope)
17892 /* Any names that appear after the declarator-id for a
17893 member are looked up in the containing scope. */
17894 if (at_function_scope_p ())
17896 /* But declarations with qualified-ids can't appear in a
17897 function. */
17898 cp_parser_error (parser, "qualified-id in declaration");
17899 declarator = cp_error_declarator;
17900 break;
17902 pushed_scope = push_scope (scope);
17904 parser->in_declarator_p = true;
17905 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17906 || (declarator && declarator->kind == cdk_id))
17907 /* Default args are only allowed on function
17908 declarations. */
17909 parser->default_arg_ok_p = saved_default_arg_ok_p;
17910 else
17911 parser->default_arg_ok_p = false;
17913 first = false;
17915 /* We're done. */
17916 else
17917 break;
17920 /* For an abstract declarator, we might wind up with nothing at this
17921 point. That's an error; the declarator is not optional. */
17922 if (!declarator)
17923 cp_parser_error (parser, "expected declarator");
17925 /* If we entered a scope, we must exit it now. */
17926 if (pushed_scope)
17927 pop_scope (pushed_scope);
17929 parser->default_arg_ok_p = saved_default_arg_ok_p;
17930 parser->in_declarator_p = saved_in_declarator_p;
17932 return declarator;
17935 /* Parse a ptr-operator.
17937 ptr-operator:
17938 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17939 * cv-qualifier-seq [opt]
17941 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17942 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17944 GNU Extension:
17946 ptr-operator:
17947 & cv-qualifier-seq [opt]
17949 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17950 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17951 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17952 filled in with the TYPE containing the member. *CV_QUALS is
17953 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17954 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17955 Note that the tree codes returned by this function have nothing
17956 to do with the types of trees that will be eventually be created
17957 to represent the pointer or reference type being parsed. They are
17958 just constants with suggestive names. */
17959 static enum tree_code
17960 cp_parser_ptr_operator (cp_parser* parser,
17961 tree* type,
17962 cp_cv_quals *cv_quals,
17963 tree *attributes)
17965 enum tree_code code = ERROR_MARK;
17966 cp_token *token;
17967 tree attrs = NULL_TREE;
17969 /* Assume that it's not a pointer-to-member. */
17970 *type = NULL_TREE;
17971 /* And that there are no cv-qualifiers. */
17972 *cv_quals = TYPE_UNQUALIFIED;
17974 /* Peek at the next token. */
17975 token = cp_lexer_peek_token (parser->lexer);
17977 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17978 if (token->type == CPP_MULT)
17979 code = INDIRECT_REF;
17980 else if (token->type == CPP_AND)
17981 code = ADDR_EXPR;
17982 else if ((cxx_dialect != cxx98) &&
17983 token->type == CPP_AND_AND) /* C++0x only */
17984 code = NON_LVALUE_EXPR;
17986 if (code != ERROR_MARK)
17988 /* Consume the `*', `&' or `&&'. */
17989 cp_lexer_consume_token (parser->lexer);
17991 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17992 `&', if we are allowing GNU extensions. (The only qualifier
17993 that can legally appear after `&' is `restrict', but that is
17994 enforced during semantic analysis. */
17995 if (code == INDIRECT_REF
17996 || cp_parser_allow_gnu_extensions_p (parser))
17997 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17999 attrs = cp_parser_std_attribute_spec_seq (parser);
18000 if (attributes != NULL)
18001 *attributes = attrs;
18003 else
18005 /* Try the pointer-to-member case. */
18006 cp_parser_parse_tentatively (parser);
18007 /* Look for the optional `::' operator. */
18008 cp_parser_global_scope_opt (parser,
18009 /*current_scope_valid_p=*/false);
18010 /* Look for the nested-name specifier. */
18011 token = cp_lexer_peek_token (parser->lexer);
18012 cp_parser_nested_name_specifier (parser,
18013 /*typename_keyword_p=*/false,
18014 /*check_dependency_p=*/true,
18015 /*type_p=*/false,
18016 /*is_declaration=*/false);
18017 /* If we found it, and the next token is a `*', then we are
18018 indeed looking at a pointer-to-member operator. */
18019 if (!cp_parser_error_occurred (parser)
18020 && cp_parser_require (parser, CPP_MULT, RT_MULT))
18022 /* Indicate that the `*' operator was used. */
18023 code = INDIRECT_REF;
18025 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18026 error_at (token->location, "%qD is a namespace", parser->scope);
18027 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18028 error_at (token->location, "cannot form pointer to member of "
18029 "non-class %q#T", parser->scope);
18030 else
18032 /* The type of which the member is a member is given by the
18033 current SCOPE. */
18034 *type = parser->scope;
18035 /* The next name will not be qualified. */
18036 parser->scope = NULL_TREE;
18037 parser->qualifying_scope = NULL_TREE;
18038 parser->object_scope = NULL_TREE;
18039 /* Look for optional c++11 attributes. */
18040 attrs = cp_parser_std_attribute_spec_seq (parser);
18041 if (attributes != NULL)
18042 *attributes = attrs;
18043 /* Look for the optional cv-qualifier-seq. */
18044 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18047 /* If that didn't work we don't have a ptr-operator. */
18048 if (!cp_parser_parse_definitely (parser))
18049 cp_parser_error (parser, "expected ptr-operator");
18052 return code;
18055 /* Parse an (optional) cv-qualifier-seq.
18057 cv-qualifier-seq:
18058 cv-qualifier cv-qualifier-seq [opt]
18060 cv-qualifier:
18061 const
18062 volatile
18064 GNU Extension:
18066 cv-qualifier:
18067 __restrict__
18069 Returns a bitmask representing the cv-qualifiers. */
18071 static cp_cv_quals
18072 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18074 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18076 while (true)
18078 cp_token *token;
18079 cp_cv_quals cv_qualifier;
18081 /* Peek at the next token. */
18082 token = cp_lexer_peek_token (parser->lexer);
18083 /* See if it's a cv-qualifier. */
18084 switch (token->keyword)
18086 case RID_CONST:
18087 cv_qualifier = TYPE_QUAL_CONST;
18088 break;
18090 case RID_VOLATILE:
18091 cv_qualifier = TYPE_QUAL_VOLATILE;
18092 break;
18094 case RID_RESTRICT:
18095 cv_qualifier = TYPE_QUAL_RESTRICT;
18096 break;
18098 default:
18099 cv_qualifier = TYPE_UNQUALIFIED;
18100 break;
18103 if (!cv_qualifier)
18104 break;
18106 if (cv_quals & cv_qualifier)
18108 error_at (token->location, "duplicate cv-qualifier");
18109 cp_lexer_purge_token (parser->lexer);
18111 else
18113 cp_lexer_consume_token (parser->lexer);
18114 cv_quals |= cv_qualifier;
18118 return cv_quals;
18121 /* Parse an (optional) ref-qualifier
18123 ref-qualifier:
18127 Returns cp_ref_qualifier representing ref-qualifier. */
18129 static cp_ref_qualifier
18130 cp_parser_ref_qualifier_opt (cp_parser* parser)
18132 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18134 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
18135 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18136 return ref_qual;
18138 while (true)
18140 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18141 cp_token *token = cp_lexer_peek_token (parser->lexer);
18143 switch (token->type)
18145 case CPP_AND:
18146 curr_ref_qual = REF_QUAL_LVALUE;
18147 break;
18149 case CPP_AND_AND:
18150 curr_ref_qual = REF_QUAL_RVALUE;
18151 break;
18153 default:
18154 curr_ref_qual = REF_QUAL_NONE;
18155 break;
18158 if (!curr_ref_qual)
18159 break;
18160 else if (ref_qual)
18162 error_at (token->location, "multiple ref-qualifiers");
18163 cp_lexer_purge_token (parser->lexer);
18165 else
18167 ref_qual = curr_ref_qual;
18168 cp_lexer_consume_token (parser->lexer);
18172 return ref_qual;
18175 /* Parse an (optional) virt-specifier-seq.
18177 virt-specifier-seq:
18178 virt-specifier virt-specifier-seq [opt]
18180 virt-specifier:
18181 override
18182 final
18184 Returns a bitmask representing the virt-specifiers. */
18186 static cp_virt_specifiers
18187 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18189 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18191 while (true)
18193 cp_token *token;
18194 cp_virt_specifiers virt_specifier;
18196 /* Peek at the next token. */
18197 token = cp_lexer_peek_token (parser->lexer);
18198 /* See if it's a virt-specifier-qualifier. */
18199 if (token->type != CPP_NAME)
18200 break;
18201 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18203 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18204 virt_specifier = VIRT_SPEC_OVERRIDE;
18206 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18208 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18209 virt_specifier = VIRT_SPEC_FINAL;
18211 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18213 virt_specifier = VIRT_SPEC_FINAL;
18215 else
18216 break;
18218 if (virt_specifiers & virt_specifier)
18220 error_at (token->location, "duplicate virt-specifier");
18221 cp_lexer_purge_token (parser->lexer);
18223 else
18225 cp_lexer_consume_token (parser->lexer);
18226 virt_specifiers |= virt_specifier;
18229 return virt_specifiers;
18232 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18233 is in scope even though it isn't real. */
18235 void
18236 inject_this_parameter (tree ctype, cp_cv_quals quals)
18238 tree this_parm;
18240 if (current_class_ptr)
18242 /* We don't clear this between NSDMIs. Is it already what we want? */
18243 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18244 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18245 && cp_type_quals (type) == quals)
18246 return;
18249 this_parm = build_this_parm (ctype, quals);
18250 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18251 current_class_ptr = NULL_TREE;
18252 current_class_ref
18253 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18254 current_class_ptr = this_parm;
18257 /* Return true iff our current scope is a non-static data member
18258 initializer. */
18260 bool
18261 parsing_nsdmi (void)
18263 /* We recognize NSDMI context by the context-less 'this' pointer set up
18264 by the function above. */
18265 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18266 return true;
18267 return false;
18270 /* Parse a late-specified return type, if any. This is not a separate
18271 non-terminal, but part of a function declarator, which looks like
18273 -> trailing-type-specifier-seq abstract-declarator(opt)
18275 Returns the type indicated by the type-id.
18277 In addition to this this parses any queued up omp declare simd
18278 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18280 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18281 function. */
18283 static tree
18284 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18285 cp_cv_quals quals)
18287 cp_token *token;
18288 tree type = NULL_TREE;
18289 bool declare_simd_p = (parser->omp_declare_simd
18290 && declarator
18291 && declarator->kind == cdk_id);
18293 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18294 && declarator && declarator->kind == cdk_id);
18296 /* Peek at the next token. */
18297 token = cp_lexer_peek_token (parser->lexer);
18298 /* A late-specified return type is indicated by an initial '->'. */
18299 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18300 return NULL_TREE;
18302 tree save_ccp = current_class_ptr;
18303 tree save_ccr = current_class_ref;
18304 if (quals >= 0)
18306 /* DR 1207: 'this' is in scope in the trailing return type. */
18307 inject_this_parameter (current_class_type, quals);
18310 if (token->type == CPP_DEREF)
18312 /* Consume the ->. */
18313 cp_lexer_consume_token (parser->lexer);
18315 type = cp_parser_trailing_type_id (parser);
18318 if (cilk_simd_fn_vector_p)
18319 declarator->std_attributes
18320 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18321 declarator->std_attributes);
18322 if (declare_simd_p)
18323 declarator->std_attributes
18324 = cp_parser_late_parsing_omp_declare_simd (parser,
18325 declarator->std_attributes);
18327 if (quals >= 0)
18329 current_class_ptr = save_ccp;
18330 current_class_ref = save_ccr;
18333 return type;
18336 /* Parse a declarator-id.
18338 declarator-id:
18339 id-expression
18340 :: [opt] nested-name-specifier [opt] type-name
18342 In the `id-expression' case, the value returned is as for
18343 cp_parser_id_expression if the id-expression was an unqualified-id.
18344 If the id-expression was a qualified-id, then a SCOPE_REF is
18345 returned. The first operand is the scope (either a NAMESPACE_DECL
18346 or TREE_TYPE), but the second is still just a representation of an
18347 unqualified-id. */
18349 static tree
18350 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18352 tree id;
18353 /* The expression must be an id-expression. Assume that qualified
18354 names are the names of types so that:
18356 template <class T>
18357 int S<T>::R::i = 3;
18359 will work; we must treat `S<T>::R' as the name of a type.
18360 Similarly, assume that qualified names are templates, where
18361 required, so that:
18363 template <class T>
18364 int S<T>::R<T>::i = 3;
18366 will work, too. */
18367 id = cp_parser_id_expression (parser,
18368 /*template_keyword_p=*/false,
18369 /*check_dependency_p=*/false,
18370 /*template_p=*/NULL,
18371 /*declarator_p=*/true,
18372 optional_p);
18373 if (id && BASELINK_P (id))
18374 id = BASELINK_FUNCTIONS (id);
18375 return id;
18378 /* Parse a type-id.
18380 type-id:
18381 type-specifier-seq abstract-declarator [opt]
18383 Returns the TYPE specified. */
18385 static tree
18386 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18387 bool is_trailing_return)
18389 cp_decl_specifier_seq type_specifier_seq;
18390 cp_declarator *abstract_declarator;
18392 /* Parse the type-specifier-seq. */
18393 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18394 is_trailing_return,
18395 &type_specifier_seq);
18396 if (type_specifier_seq.type == error_mark_node)
18397 return error_mark_node;
18399 /* There might or might not be an abstract declarator. */
18400 cp_parser_parse_tentatively (parser);
18401 /* Look for the declarator. */
18402 abstract_declarator
18403 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18404 /*parenthesized_p=*/NULL,
18405 /*member_p=*/false,
18406 /*friend_p=*/false);
18407 /* Check to see if there really was a declarator. */
18408 if (!cp_parser_parse_definitely (parser))
18409 abstract_declarator = NULL;
18411 if (type_specifier_seq.type
18412 /* None of the valid uses of 'auto' in C++14 involve the type-id
18413 nonterminal, but it is valid in a trailing-return-type. */
18414 && !(cxx_dialect >= cxx14 && is_trailing_return)
18415 && type_uses_auto (type_specifier_seq.type))
18417 /* A type-id with type 'auto' is only ok if the abstract declarator
18418 is a function declarator with a late-specified return type. */
18419 if (abstract_declarator
18420 && abstract_declarator->kind == cdk_function
18421 && abstract_declarator->u.function.late_return_type)
18422 /* OK */;
18423 else
18425 error ("invalid use of %<auto%>");
18426 return error_mark_node;
18430 return groktypename (&type_specifier_seq, abstract_declarator,
18431 is_template_arg);
18434 static tree cp_parser_type_id (cp_parser *parser)
18436 return cp_parser_type_id_1 (parser, false, false);
18439 static tree cp_parser_template_type_arg (cp_parser *parser)
18441 tree r;
18442 const char *saved_message = parser->type_definition_forbidden_message;
18443 parser->type_definition_forbidden_message
18444 = G_("types may not be defined in template arguments");
18445 r = cp_parser_type_id_1 (parser, true, false);
18446 parser->type_definition_forbidden_message = saved_message;
18447 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18449 error ("invalid use of %<auto%> in template argument");
18450 r = error_mark_node;
18452 return r;
18455 static tree cp_parser_trailing_type_id (cp_parser *parser)
18457 return cp_parser_type_id_1 (parser, false, true);
18460 /* Parse a type-specifier-seq.
18462 type-specifier-seq:
18463 type-specifier type-specifier-seq [opt]
18465 GNU extension:
18467 type-specifier-seq:
18468 attributes type-specifier-seq [opt]
18470 If IS_DECLARATION is true, we are at the start of a "condition" or
18471 exception-declaration, so we might be followed by a declarator-id.
18473 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18474 i.e. we've just seen "->".
18476 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18478 static void
18479 cp_parser_type_specifier_seq (cp_parser* parser,
18480 bool is_declaration,
18481 bool is_trailing_return,
18482 cp_decl_specifier_seq *type_specifier_seq)
18484 bool seen_type_specifier = false;
18485 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18486 cp_token *start_token = NULL;
18488 /* Clear the TYPE_SPECIFIER_SEQ. */
18489 clear_decl_specs (type_specifier_seq);
18491 /* In the context of a trailing return type, enum E { } is an
18492 elaborated-type-specifier followed by a function-body, not an
18493 enum-specifier. */
18494 if (is_trailing_return)
18495 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18497 /* Parse the type-specifiers and attributes. */
18498 while (true)
18500 tree type_specifier;
18501 bool is_cv_qualifier;
18503 /* Check for attributes first. */
18504 if (cp_next_tokens_can_be_attribute_p (parser))
18506 type_specifier_seq->attributes =
18507 chainon (type_specifier_seq->attributes,
18508 cp_parser_attributes_opt (parser));
18509 continue;
18512 /* record the token of the beginning of the type specifier seq,
18513 for error reporting purposes*/
18514 if (!start_token)
18515 start_token = cp_lexer_peek_token (parser->lexer);
18517 /* Look for the type-specifier. */
18518 type_specifier = cp_parser_type_specifier (parser,
18519 flags,
18520 type_specifier_seq,
18521 /*is_declaration=*/false,
18522 NULL,
18523 &is_cv_qualifier);
18524 if (!type_specifier)
18526 /* If the first type-specifier could not be found, this is not a
18527 type-specifier-seq at all. */
18528 if (!seen_type_specifier)
18530 /* Set in_declarator_p to avoid skipping to the semicolon. */
18531 int in_decl = parser->in_declarator_p;
18532 parser->in_declarator_p = true;
18534 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18535 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18536 cp_parser_error (parser, "expected type-specifier");
18538 parser->in_declarator_p = in_decl;
18540 type_specifier_seq->type = error_mark_node;
18541 return;
18543 /* If subsequent type-specifiers could not be found, the
18544 type-specifier-seq is complete. */
18545 break;
18548 seen_type_specifier = true;
18549 /* The standard says that a condition can be:
18551 type-specifier-seq declarator = assignment-expression
18553 However, given:
18555 struct S {};
18556 if (int S = ...)
18558 we should treat the "S" as a declarator, not as a
18559 type-specifier. The standard doesn't say that explicitly for
18560 type-specifier-seq, but it does say that for
18561 decl-specifier-seq in an ordinary declaration. Perhaps it
18562 would be clearer just to allow a decl-specifier-seq here, and
18563 then add a semantic restriction that if any decl-specifiers
18564 that are not type-specifiers appear, the program is invalid. */
18565 if (is_declaration && !is_cv_qualifier)
18566 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18570 /* Return whether the function currently being declared has an associated
18571 template parameter list. */
18573 static bool
18574 function_being_declared_is_template_p (cp_parser* parser)
18576 if (!current_template_parms || processing_template_parmlist)
18577 return false;
18579 if (parser->implicit_template_scope)
18580 return true;
18582 if (at_class_scope_p ()
18583 && TYPE_BEING_DEFINED (current_class_type))
18584 return parser->num_template_parameter_lists != 0;
18586 return ((int) parser->num_template_parameter_lists > template_class_depth
18587 (current_class_type));
18590 /* Parse a parameter-declaration-clause.
18592 parameter-declaration-clause:
18593 parameter-declaration-list [opt] ... [opt]
18594 parameter-declaration-list , ...
18596 Returns a representation for the parameter declarations. A return
18597 value of NULL indicates a parameter-declaration-clause consisting
18598 only of an ellipsis. */
18600 static tree
18601 cp_parser_parameter_declaration_clause (cp_parser* parser)
18603 tree parameters;
18604 cp_token *token;
18605 bool ellipsis_p;
18606 bool is_error;
18608 struct cleanup {
18609 cp_parser* parser;
18610 int auto_is_implicit_function_template_parm_p;
18611 ~cleanup() {
18612 parser->auto_is_implicit_function_template_parm_p
18613 = auto_is_implicit_function_template_parm_p;
18615 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18617 (void) cleanup;
18619 if (!processing_specialization
18620 && !processing_template_parmlist
18621 && !processing_explicit_instantiation)
18622 if (!current_function_decl
18623 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18624 parser->auto_is_implicit_function_template_parm_p = true;
18626 /* Peek at the next token. */
18627 token = cp_lexer_peek_token (parser->lexer);
18628 /* Check for trivial parameter-declaration-clauses. */
18629 if (token->type == CPP_ELLIPSIS)
18631 /* Consume the `...' token. */
18632 cp_lexer_consume_token (parser->lexer);
18633 return NULL_TREE;
18635 else if (token->type == CPP_CLOSE_PAREN)
18636 /* There are no parameters. */
18638 #ifndef NO_IMPLICIT_EXTERN_C
18639 if (in_system_header_at (input_location)
18640 && current_class_type == NULL
18641 && current_lang_name == lang_name_c)
18642 return NULL_TREE;
18643 else
18644 #endif
18645 return void_list_node;
18647 /* Check for `(void)', too, which is a special case. */
18648 else if (token->keyword == RID_VOID
18649 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18650 == CPP_CLOSE_PAREN))
18652 /* Consume the `void' token. */
18653 cp_lexer_consume_token (parser->lexer);
18654 /* There are no parameters. */
18655 return void_list_node;
18658 /* Parse the parameter-declaration-list. */
18659 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18660 /* If a parse error occurred while parsing the
18661 parameter-declaration-list, then the entire
18662 parameter-declaration-clause is erroneous. */
18663 if (is_error)
18664 return NULL;
18666 /* Peek at the next token. */
18667 token = cp_lexer_peek_token (parser->lexer);
18668 /* If it's a `,', the clause should terminate with an ellipsis. */
18669 if (token->type == CPP_COMMA)
18671 /* Consume the `,'. */
18672 cp_lexer_consume_token (parser->lexer);
18673 /* Expect an ellipsis. */
18674 ellipsis_p
18675 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18677 /* It might also be `...' if the optional trailing `,' was
18678 omitted. */
18679 else if (token->type == CPP_ELLIPSIS)
18681 /* Consume the `...' token. */
18682 cp_lexer_consume_token (parser->lexer);
18683 /* And remember that we saw it. */
18684 ellipsis_p = true;
18686 else
18687 ellipsis_p = false;
18689 /* Finish the parameter list. */
18690 if (!ellipsis_p)
18691 parameters = chainon (parameters, void_list_node);
18693 return parameters;
18696 /* Parse a parameter-declaration-list.
18698 parameter-declaration-list:
18699 parameter-declaration
18700 parameter-declaration-list , parameter-declaration
18702 Returns a representation of the parameter-declaration-list, as for
18703 cp_parser_parameter_declaration_clause. However, the
18704 `void_list_node' is never appended to the list. Upon return,
18705 *IS_ERROR will be true iff an error occurred. */
18707 static tree
18708 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18710 tree parameters = NULL_TREE;
18711 tree *tail = &parameters;
18712 bool saved_in_unbraced_linkage_specification_p;
18713 int index = 0;
18715 /* Assume all will go well. */
18716 *is_error = false;
18717 /* The special considerations that apply to a function within an
18718 unbraced linkage specifications do not apply to the parameters
18719 to the function. */
18720 saved_in_unbraced_linkage_specification_p
18721 = parser->in_unbraced_linkage_specification_p;
18722 parser->in_unbraced_linkage_specification_p = false;
18724 /* Look for more parameters. */
18725 while (true)
18727 cp_parameter_declarator *parameter;
18728 tree decl = error_mark_node;
18729 bool parenthesized_p = false;
18730 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18731 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18732 (current_template_parms)) : 0);
18734 /* Parse the parameter. */
18735 parameter
18736 = cp_parser_parameter_declaration (parser,
18737 /*template_parm_p=*/false,
18738 &parenthesized_p);
18740 /* We don't know yet if the enclosing context is deprecated, so wait
18741 and warn in grokparms if appropriate. */
18742 deprecated_state = DEPRECATED_SUPPRESS;
18744 if (parameter)
18746 /* If a function parameter pack was specified and an implicit template
18747 parameter was introduced during cp_parser_parameter_declaration,
18748 change any implicit parameters introduced into packs. */
18749 if (parser->implicit_template_parms
18750 && parameter->declarator
18751 && parameter->declarator->parameter_pack_p)
18753 int latest_template_parm_idx = TREE_VEC_LENGTH
18754 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18756 if (latest_template_parm_idx != template_parm_idx)
18757 parameter->decl_specifiers.type = convert_generic_types_to_packs
18758 (parameter->decl_specifiers.type,
18759 template_parm_idx, latest_template_parm_idx);
18762 decl = grokdeclarator (parameter->declarator,
18763 &parameter->decl_specifiers,
18764 PARM,
18765 parameter->default_argument != NULL_TREE,
18766 &parameter->decl_specifiers.attributes);
18769 deprecated_state = DEPRECATED_NORMAL;
18771 /* If a parse error occurred parsing the parameter declaration,
18772 then the entire parameter-declaration-list is erroneous. */
18773 if (decl == error_mark_node)
18775 *is_error = true;
18776 parameters = error_mark_node;
18777 break;
18780 if (parameter->decl_specifiers.attributes)
18781 cplus_decl_attributes (&decl,
18782 parameter->decl_specifiers.attributes,
18784 if (DECL_NAME (decl))
18785 decl = pushdecl (decl);
18787 if (decl != error_mark_node)
18789 retrofit_lang_decl (decl);
18790 DECL_PARM_INDEX (decl) = ++index;
18791 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18794 /* Add the new parameter to the list. */
18795 *tail = build_tree_list (parameter->default_argument, decl);
18796 tail = &TREE_CHAIN (*tail);
18798 /* Peek at the next token. */
18799 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18800 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18801 /* These are for Objective-C++ */
18802 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18803 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18804 /* The parameter-declaration-list is complete. */
18805 break;
18806 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18808 cp_token *token;
18810 /* Peek at the next token. */
18811 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18812 /* If it's an ellipsis, then the list is complete. */
18813 if (token->type == CPP_ELLIPSIS)
18814 break;
18815 /* Otherwise, there must be more parameters. Consume the
18816 `,'. */
18817 cp_lexer_consume_token (parser->lexer);
18818 /* When parsing something like:
18820 int i(float f, double d)
18822 we can tell after seeing the declaration for "f" that we
18823 are not looking at an initialization of a variable "i",
18824 but rather at the declaration of a function "i".
18826 Due to the fact that the parsing of template arguments
18827 (as specified to a template-id) requires backtracking we
18828 cannot use this technique when inside a template argument
18829 list. */
18830 if (!parser->in_template_argument_list_p
18831 && !parser->in_type_id_in_expr_p
18832 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18833 /* However, a parameter-declaration of the form
18834 "float(f)" (which is a valid declaration of a
18835 parameter "f") can also be interpreted as an
18836 expression (the conversion of "f" to "float"). */
18837 && !parenthesized_p)
18838 cp_parser_commit_to_tentative_parse (parser);
18840 else
18842 cp_parser_error (parser, "expected %<,%> or %<...%>");
18843 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18844 cp_parser_skip_to_closing_parenthesis (parser,
18845 /*recovering=*/true,
18846 /*or_comma=*/false,
18847 /*consume_paren=*/false);
18848 break;
18852 parser->in_unbraced_linkage_specification_p
18853 = saved_in_unbraced_linkage_specification_p;
18855 /* Reset implicit_template_scope if we are about to leave the function
18856 parameter list that introduced it. Note that for out-of-line member
18857 definitions, there will be one or more class scopes before we get to
18858 the template parameter scope. */
18860 if (cp_binding_level *its = parser->implicit_template_scope)
18861 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18863 while (maybe_its->kind == sk_class)
18864 maybe_its = maybe_its->level_chain;
18865 if (maybe_its == its)
18867 parser->implicit_template_parms = 0;
18868 parser->implicit_template_scope = 0;
18872 return parameters;
18875 /* Parse a parameter declaration.
18877 parameter-declaration:
18878 decl-specifier-seq ... [opt] declarator
18879 decl-specifier-seq declarator = assignment-expression
18880 decl-specifier-seq ... [opt] abstract-declarator [opt]
18881 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18883 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18884 declares a template parameter. (In that case, a non-nested `>'
18885 token encountered during the parsing of the assignment-expression
18886 is not interpreted as a greater-than operator.)
18888 Returns a representation of the parameter, or NULL if an error
18889 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18890 true iff the declarator is of the form "(p)". */
18892 static cp_parameter_declarator *
18893 cp_parser_parameter_declaration (cp_parser *parser,
18894 bool template_parm_p,
18895 bool *parenthesized_p)
18897 int declares_class_or_enum;
18898 cp_decl_specifier_seq decl_specifiers;
18899 cp_declarator *declarator;
18900 tree default_argument;
18901 cp_token *token = NULL, *declarator_token_start = NULL;
18902 const char *saved_message;
18904 /* In a template parameter, `>' is not an operator.
18906 [temp.param]
18908 When parsing a default template-argument for a non-type
18909 template-parameter, the first non-nested `>' is taken as the end
18910 of the template parameter-list rather than a greater-than
18911 operator. */
18913 /* Type definitions may not appear in parameter types. */
18914 saved_message = parser->type_definition_forbidden_message;
18915 parser->type_definition_forbidden_message
18916 = G_("types may not be defined in parameter types");
18918 /* Parse the declaration-specifiers. */
18919 cp_parser_decl_specifier_seq (parser,
18920 CP_PARSER_FLAGS_NONE,
18921 &decl_specifiers,
18922 &declares_class_or_enum);
18924 /* Complain about missing 'typename' or other invalid type names. */
18925 if (!decl_specifiers.any_type_specifiers_p
18926 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18927 decl_specifiers.type = error_mark_node;
18929 /* If an error occurred, there's no reason to attempt to parse the
18930 rest of the declaration. */
18931 if (cp_parser_error_occurred (parser))
18933 parser->type_definition_forbidden_message = saved_message;
18934 return NULL;
18937 /* Peek at the next token. */
18938 token = cp_lexer_peek_token (parser->lexer);
18940 /* If the next token is a `)', `,', `=', `>', or `...', then there
18941 is no declarator. However, when variadic templates are enabled,
18942 there may be a declarator following `...'. */
18943 if (token->type == CPP_CLOSE_PAREN
18944 || token->type == CPP_COMMA
18945 || token->type == CPP_EQ
18946 || token->type == CPP_GREATER)
18948 declarator = NULL;
18949 if (parenthesized_p)
18950 *parenthesized_p = false;
18952 /* Otherwise, there should be a declarator. */
18953 else
18955 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18956 parser->default_arg_ok_p = false;
18958 /* After seeing a decl-specifier-seq, if the next token is not a
18959 "(", there is no possibility that the code is a valid
18960 expression. Therefore, if parsing tentatively, we commit at
18961 this point. */
18962 if (!parser->in_template_argument_list_p
18963 /* In an expression context, having seen:
18965 (int((char ...
18967 we cannot be sure whether we are looking at a
18968 function-type (taking a "char" as a parameter) or a cast
18969 of some object of type "char" to "int". */
18970 && !parser->in_type_id_in_expr_p
18971 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18972 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18973 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18974 cp_parser_commit_to_tentative_parse (parser);
18975 /* Parse the declarator. */
18976 declarator_token_start = token;
18977 declarator = cp_parser_declarator (parser,
18978 CP_PARSER_DECLARATOR_EITHER,
18979 /*ctor_dtor_or_conv_p=*/NULL,
18980 parenthesized_p,
18981 /*member_p=*/false,
18982 /*friend_p=*/false);
18983 parser->default_arg_ok_p = saved_default_arg_ok_p;
18984 /* After the declarator, allow more attributes. */
18985 decl_specifiers.attributes
18986 = chainon (decl_specifiers.attributes,
18987 cp_parser_attributes_opt (parser));
18990 /* If the next token is an ellipsis, and we have not seen a
18991 declarator name, and the type of the declarator contains parameter
18992 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18993 a parameter pack expansion expression. Otherwise, leave the
18994 ellipsis for a C-style variadic function. */
18995 token = cp_lexer_peek_token (parser->lexer);
18996 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18998 tree type = decl_specifiers.type;
19000 if (type && DECL_P (type))
19001 type = TREE_TYPE (type);
19003 if (type
19004 && TREE_CODE (type) != TYPE_PACK_EXPANSION
19005 && declarator_can_be_parameter_pack (declarator)
19006 && (!declarator || !declarator->parameter_pack_p)
19007 && uses_parameter_packs (type))
19009 /* Consume the `...'. */
19010 cp_lexer_consume_token (parser->lexer);
19011 maybe_warn_variadic_templates ();
19013 /* Build a pack expansion type */
19014 if (declarator)
19015 declarator->parameter_pack_p = true;
19016 else
19017 decl_specifiers.type = make_pack_expansion (type);
19021 /* The restriction on defining new types applies only to the type
19022 of the parameter, not to the default argument. */
19023 parser->type_definition_forbidden_message = saved_message;
19025 /* If the next token is `=', then process a default argument. */
19026 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19028 token = cp_lexer_peek_token (parser->lexer);
19029 /* If we are defining a class, then the tokens that make up the
19030 default argument must be saved and processed later. */
19031 if (!template_parm_p && at_class_scope_p ()
19032 && TYPE_BEING_DEFINED (current_class_type)
19033 && !LAMBDA_TYPE_P (current_class_type))
19034 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19035 /* Outside of a class definition, we can just parse the
19036 assignment-expression. */
19037 else
19038 default_argument
19039 = cp_parser_default_argument (parser, template_parm_p);
19041 if (!parser->default_arg_ok_p)
19043 if (flag_permissive)
19044 warning (0, "deprecated use of default argument for parameter of non-function");
19045 else
19047 error_at (token->location,
19048 "default arguments are only "
19049 "permitted for function parameters");
19050 default_argument = NULL_TREE;
19053 else if ((declarator && declarator->parameter_pack_p)
19054 || (decl_specifiers.type
19055 && PACK_EXPANSION_P (decl_specifiers.type)))
19057 /* Find the name of the parameter pack. */
19058 cp_declarator *id_declarator = declarator;
19059 while (id_declarator && id_declarator->kind != cdk_id)
19060 id_declarator = id_declarator->declarator;
19062 if (id_declarator && id_declarator->kind == cdk_id)
19063 error_at (declarator_token_start->location,
19064 template_parm_p
19065 ? G_("template parameter pack %qD "
19066 "cannot have a default argument")
19067 : G_("parameter pack %qD cannot have "
19068 "a default argument"),
19069 id_declarator->u.id.unqualified_name);
19070 else
19071 error_at (declarator_token_start->location,
19072 template_parm_p
19073 ? G_("template parameter pack cannot have "
19074 "a default argument")
19075 : G_("parameter pack cannot have a "
19076 "default argument"));
19078 default_argument = NULL_TREE;
19081 else
19082 default_argument = NULL_TREE;
19084 return make_parameter_declarator (&decl_specifiers,
19085 declarator,
19086 default_argument);
19089 /* Parse a default argument and return it.
19091 TEMPLATE_PARM_P is true if this is a default argument for a
19092 non-type template parameter. */
19093 static tree
19094 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19096 tree default_argument = NULL_TREE;
19097 bool saved_greater_than_is_operator_p;
19098 bool saved_local_variables_forbidden_p;
19099 bool non_constant_p, is_direct_init;
19101 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19102 set correctly. */
19103 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19104 parser->greater_than_is_operator_p = !template_parm_p;
19105 /* Local variable names (and the `this' keyword) may not
19106 appear in a default argument. */
19107 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19108 parser->local_variables_forbidden_p = true;
19109 /* Parse the assignment-expression. */
19110 if (template_parm_p)
19111 push_deferring_access_checks (dk_no_deferred);
19112 tree saved_class_ptr = NULL_TREE;
19113 tree saved_class_ref = NULL_TREE;
19114 /* The "this" pointer is not valid in a default argument. */
19115 if (cfun)
19117 saved_class_ptr = current_class_ptr;
19118 cp_function_chain->x_current_class_ptr = NULL_TREE;
19119 saved_class_ref = current_class_ref;
19120 cp_function_chain->x_current_class_ref = NULL_TREE;
19122 default_argument
19123 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19124 /* Restore the "this" pointer. */
19125 if (cfun)
19127 cp_function_chain->x_current_class_ptr = saved_class_ptr;
19128 cp_function_chain->x_current_class_ref = saved_class_ref;
19130 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19131 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19132 if (template_parm_p)
19133 pop_deferring_access_checks ();
19134 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19135 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19137 return default_argument;
19140 /* Parse a function-body.
19142 function-body:
19143 compound_statement */
19145 static void
19146 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19148 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19151 /* Parse a ctor-initializer-opt followed by a function-body. Return
19152 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
19153 is true we are parsing a function-try-block. */
19155 static bool
19156 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19157 bool in_function_try_block)
19159 tree body, list;
19160 bool ctor_initializer_p;
19161 const bool check_body_p =
19162 DECL_CONSTRUCTOR_P (current_function_decl)
19163 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19164 tree last = NULL;
19166 /* Begin the function body. */
19167 body = begin_function_body ();
19168 /* Parse the optional ctor-initializer. */
19169 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19171 /* If we're parsing a constexpr constructor definition, we need
19172 to check that the constructor body is indeed empty. However,
19173 before we get to cp_parser_function_body lot of junk has been
19174 generated, so we can't just check that we have an empty block.
19175 Rather we take a snapshot of the outermost block, and check whether
19176 cp_parser_function_body changed its state. */
19177 if (check_body_p)
19179 list = cur_stmt_list;
19180 if (STATEMENT_LIST_TAIL (list))
19181 last = STATEMENT_LIST_TAIL (list)->stmt;
19183 /* Parse the function-body. */
19184 cp_parser_function_body (parser, in_function_try_block);
19185 if (check_body_p)
19186 check_constexpr_ctor_body (last, list, /*complain=*/true);
19187 /* Finish the function body. */
19188 finish_function_body (body);
19190 return ctor_initializer_p;
19193 /* Parse an initializer.
19195 initializer:
19196 = initializer-clause
19197 ( expression-list )
19199 Returns an expression representing the initializer. If no
19200 initializer is present, NULL_TREE is returned.
19202 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19203 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19204 set to TRUE if there is no initializer present. If there is an
19205 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19206 is set to true; otherwise it is set to false. */
19208 static tree
19209 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19210 bool* non_constant_p)
19212 cp_token *token;
19213 tree init;
19215 /* Peek at the next token. */
19216 token = cp_lexer_peek_token (parser->lexer);
19218 /* Let our caller know whether or not this initializer was
19219 parenthesized. */
19220 *is_direct_init = (token->type != CPP_EQ);
19221 /* Assume that the initializer is constant. */
19222 *non_constant_p = false;
19224 if (token->type == CPP_EQ)
19226 /* Consume the `='. */
19227 cp_lexer_consume_token (parser->lexer);
19228 /* Parse the initializer-clause. */
19229 init = cp_parser_initializer_clause (parser, non_constant_p);
19231 else if (token->type == CPP_OPEN_PAREN)
19233 vec<tree, va_gc> *vec;
19234 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19235 /*cast_p=*/false,
19236 /*allow_expansion_p=*/true,
19237 non_constant_p);
19238 if (vec == NULL)
19239 return error_mark_node;
19240 init = build_tree_list_vec (vec);
19241 release_tree_vector (vec);
19243 else if (token->type == CPP_OPEN_BRACE)
19245 cp_lexer_set_source_position (parser->lexer);
19246 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19247 init = cp_parser_braced_list (parser, non_constant_p);
19248 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19250 else
19252 /* Anything else is an error. */
19253 cp_parser_error (parser, "expected initializer");
19254 init = error_mark_node;
19257 return init;
19260 /* Parse an initializer-clause.
19262 initializer-clause:
19263 assignment-expression
19264 braced-init-list
19266 Returns an expression representing the initializer.
19268 If the `assignment-expression' production is used the value
19269 returned is simply a representation for the expression.
19271 Otherwise, calls cp_parser_braced_list. */
19273 static tree
19274 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19276 tree initializer;
19278 /* Assume the expression is constant. */
19279 *non_constant_p = false;
19281 /* If it is not a `{', then we are looking at an
19282 assignment-expression. */
19283 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19285 initializer
19286 = cp_parser_constant_expression (parser,
19287 /*allow_non_constant_p=*/true,
19288 non_constant_p);
19290 else
19291 initializer = cp_parser_braced_list (parser, non_constant_p);
19293 return initializer;
19296 /* Parse a brace-enclosed initializer list.
19298 braced-init-list:
19299 { initializer-list , [opt] }
19302 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19303 the elements of the initializer-list (or NULL, if the last
19304 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19305 NULL_TREE. There is no way to detect whether or not the optional
19306 trailing `,' was provided. NON_CONSTANT_P is as for
19307 cp_parser_initializer. */
19309 static tree
19310 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19312 tree initializer;
19314 /* Consume the `{' token. */
19315 cp_lexer_consume_token (parser->lexer);
19316 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19317 initializer = make_node (CONSTRUCTOR);
19318 /* If it's not a `}', then there is a non-trivial initializer. */
19319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19321 /* Parse the initializer list. */
19322 CONSTRUCTOR_ELTS (initializer)
19323 = cp_parser_initializer_list (parser, non_constant_p);
19324 /* A trailing `,' token is allowed. */
19325 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19326 cp_lexer_consume_token (parser->lexer);
19328 else
19329 *non_constant_p = false;
19330 /* Now, there should be a trailing `}'. */
19331 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19332 TREE_TYPE (initializer) = init_list_type_node;
19333 return initializer;
19336 /* Consume tokens up to, and including, the next non-nested closing `]'.
19337 Returns true iff we found a closing `]'. */
19339 static bool
19340 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19342 unsigned square_depth = 0;
19344 while (true)
19346 cp_token * token = cp_lexer_peek_token (parser->lexer);
19348 switch (token->type)
19350 case CPP_EOF:
19351 case CPP_PRAGMA_EOL:
19352 /* If we've run out of tokens, then there is no closing `]'. */
19353 return false;
19355 case CPP_OPEN_SQUARE:
19356 ++square_depth;
19357 break;
19359 case CPP_CLOSE_SQUARE:
19360 if (!square_depth--)
19362 cp_lexer_consume_token (parser->lexer);
19363 return true;
19365 break;
19367 default:
19368 break;
19371 /* Consume the token. */
19372 cp_lexer_consume_token (parser->lexer);
19376 /* Return true if we are looking at an array-designator, false otherwise. */
19378 static bool
19379 cp_parser_array_designator_p (cp_parser *parser)
19381 /* Consume the `['. */
19382 cp_lexer_consume_token (parser->lexer);
19384 cp_lexer_save_tokens (parser->lexer);
19386 /* Skip tokens until the next token is a closing square bracket.
19387 If we find the closing `]', and the next token is a `=', then
19388 we are looking at an array designator. */
19389 bool array_designator_p
19390 = (cp_parser_skip_to_closing_square_bracket (parser)
19391 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19393 /* Roll back the tokens we skipped. */
19394 cp_lexer_rollback_tokens (parser->lexer);
19396 return array_designator_p;
19399 /* Parse an initializer-list.
19401 initializer-list:
19402 initializer-clause ... [opt]
19403 initializer-list , initializer-clause ... [opt]
19405 GNU Extension:
19407 initializer-list:
19408 designation initializer-clause ...[opt]
19409 initializer-list , designation initializer-clause ...[opt]
19411 designation:
19412 . identifier =
19413 identifier :
19414 [ constant-expression ] =
19416 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19417 for the initializer. If the INDEX of the elt is non-NULL, it is the
19418 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19419 as for cp_parser_initializer. */
19421 static vec<constructor_elt, va_gc> *
19422 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19424 vec<constructor_elt, va_gc> *v = NULL;
19426 /* Assume all of the expressions are constant. */
19427 *non_constant_p = false;
19429 /* Parse the rest of the list. */
19430 while (true)
19432 cp_token *token;
19433 tree designator;
19434 tree initializer;
19435 bool clause_non_constant_p;
19437 /* If the next token is an identifier and the following one is a
19438 colon, we are looking at the GNU designated-initializer
19439 syntax. */
19440 if (cp_parser_allow_gnu_extensions_p (parser)
19441 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19442 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19444 /* Warn the user that they are using an extension. */
19445 pedwarn (input_location, OPT_Wpedantic,
19446 "ISO C++ does not allow designated initializers");
19447 /* Consume the identifier. */
19448 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19449 /* Consume the `:'. */
19450 cp_lexer_consume_token (parser->lexer);
19452 /* Also handle the C99 syntax, '. id ='. */
19453 else if (cp_parser_allow_gnu_extensions_p (parser)
19454 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19455 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19456 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19458 /* Warn the user that they are using an extension. */
19459 pedwarn (input_location, OPT_Wpedantic,
19460 "ISO C++ does not allow C99 designated initializers");
19461 /* Consume the `.'. */
19462 cp_lexer_consume_token (parser->lexer);
19463 /* Consume the identifier. */
19464 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19465 /* Consume the `='. */
19466 cp_lexer_consume_token (parser->lexer);
19468 /* Also handle C99 array designators, '[ const ] ='. */
19469 else if (cp_parser_allow_gnu_extensions_p (parser)
19470 && !c_dialect_objc ()
19471 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19473 /* In C++11, [ could start a lambda-introducer. */
19474 bool non_const = false;
19476 cp_parser_parse_tentatively (parser);
19478 if (!cp_parser_array_designator_p (parser))
19480 cp_parser_simulate_error (parser);
19481 designator = NULL_TREE;
19483 else
19485 designator = cp_parser_constant_expression (parser, true,
19486 &non_const);
19487 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19488 cp_parser_require (parser, CPP_EQ, RT_EQ);
19491 if (!cp_parser_parse_definitely (parser))
19492 designator = NULL_TREE;
19493 else if (non_const)
19494 require_potential_rvalue_constant_expression (designator);
19496 else
19497 designator = NULL_TREE;
19499 /* Parse the initializer. */
19500 initializer = cp_parser_initializer_clause (parser,
19501 &clause_non_constant_p);
19502 /* If any clause is non-constant, so is the entire initializer. */
19503 if (clause_non_constant_p)
19504 *non_constant_p = true;
19506 /* If we have an ellipsis, this is an initializer pack
19507 expansion. */
19508 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19510 /* Consume the `...'. */
19511 cp_lexer_consume_token (parser->lexer);
19513 /* Turn the initializer into an initializer expansion. */
19514 initializer = make_pack_expansion (initializer);
19517 /* Add it to the vector. */
19518 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19520 /* If the next token is not a comma, we have reached the end of
19521 the list. */
19522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19523 break;
19525 /* Peek at the next token. */
19526 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19527 /* If the next token is a `}', then we're still done. An
19528 initializer-clause can have a trailing `,' after the
19529 initializer-list and before the closing `}'. */
19530 if (token->type == CPP_CLOSE_BRACE)
19531 break;
19533 /* Consume the `,' token. */
19534 cp_lexer_consume_token (parser->lexer);
19537 return v;
19540 /* Classes [gram.class] */
19542 /* Parse a class-name.
19544 class-name:
19545 identifier
19546 template-id
19548 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19549 to indicate that names looked up in dependent types should be
19550 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19551 keyword has been used to indicate that the name that appears next
19552 is a template. TAG_TYPE indicates the explicit tag given before
19553 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19554 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19555 is the class being defined in a class-head.
19557 Returns the TYPE_DECL representing the class. */
19559 static tree
19560 cp_parser_class_name (cp_parser *parser,
19561 bool typename_keyword_p,
19562 bool template_keyword_p,
19563 enum tag_types tag_type,
19564 bool check_dependency_p,
19565 bool class_head_p,
19566 bool is_declaration)
19568 tree decl;
19569 tree scope;
19570 bool typename_p;
19571 cp_token *token;
19572 tree identifier = NULL_TREE;
19574 /* All class-names start with an identifier. */
19575 token = cp_lexer_peek_token (parser->lexer);
19576 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19578 cp_parser_error (parser, "expected class-name");
19579 return error_mark_node;
19582 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19583 to a template-id, so we save it here. */
19584 scope = parser->scope;
19585 if (scope == error_mark_node)
19586 return error_mark_node;
19588 /* Any name names a type if we're following the `typename' keyword
19589 in a qualified name where the enclosing scope is type-dependent. */
19590 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19591 && dependent_type_p (scope));
19592 /* Handle the common case (an identifier, but not a template-id)
19593 efficiently. */
19594 if (token->type == CPP_NAME
19595 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19597 cp_token *identifier_token;
19598 bool ambiguous_p;
19600 /* Look for the identifier. */
19601 identifier_token = cp_lexer_peek_token (parser->lexer);
19602 ambiguous_p = identifier_token->error_reported;
19603 identifier = cp_parser_identifier (parser);
19604 /* If the next token isn't an identifier, we are certainly not
19605 looking at a class-name. */
19606 if (identifier == error_mark_node)
19607 decl = error_mark_node;
19608 /* If we know this is a type-name, there's no need to look it
19609 up. */
19610 else if (typename_p)
19611 decl = identifier;
19612 else
19614 tree ambiguous_decls;
19615 /* If we already know that this lookup is ambiguous, then
19616 we've already issued an error message; there's no reason
19617 to check again. */
19618 if (ambiguous_p)
19620 cp_parser_simulate_error (parser);
19621 return error_mark_node;
19623 /* If the next token is a `::', then the name must be a type
19624 name.
19626 [basic.lookup.qual]
19628 During the lookup for a name preceding the :: scope
19629 resolution operator, object, function, and enumerator
19630 names are ignored. */
19631 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19632 tag_type = typename_type;
19633 /* Look up the name. */
19634 decl = cp_parser_lookup_name (parser, identifier,
19635 tag_type,
19636 /*is_template=*/false,
19637 /*is_namespace=*/false,
19638 check_dependency_p,
19639 &ambiguous_decls,
19640 identifier_token->location);
19641 if (ambiguous_decls)
19643 if (cp_parser_parsing_tentatively (parser))
19644 cp_parser_simulate_error (parser);
19645 return error_mark_node;
19649 else
19651 /* Try a template-id. */
19652 decl = cp_parser_template_id (parser, template_keyword_p,
19653 check_dependency_p,
19654 tag_type,
19655 is_declaration);
19656 if (decl == error_mark_node)
19657 return error_mark_node;
19660 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19662 /* If this is a typename, create a TYPENAME_TYPE. */
19663 if (typename_p && decl != error_mark_node)
19665 decl = make_typename_type (scope, decl, typename_type,
19666 /*complain=*/tf_error);
19667 if (decl != error_mark_node)
19668 decl = TYPE_NAME (decl);
19671 decl = strip_using_decl (decl);
19673 /* Check to see that it is really the name of a class. */
19674 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19675 && identifier_p (TREE_OPERAND (decl, 0))
19676 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19677 /* Situations like this:
19679 template <typename T> struct A {
19680 typename T::template X<int>::I i;
19683 are problematic. Is `T::template X<int>' a class-name? The
19684 standard does not seem to be definitive, but there is no other
19685 valid interpretation of the following `::'. Therefore, those
19686 names are considered class-names. */
19688 decl = make_typename_type (scope, decl, tag_type, tf_error);
19689 if (decl != error_mark_node)
19690 decl = TYPE_NAME (decl);
19692 else if (TREE_CODE (decl) != TYPE_DECL
19693 || TREE_TYPE (decl) == error_mark_node
19694 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19695 /* In Objective-C 2.0, a classname followed by '.' starts a
19696 dot-syntax expression, and it's not a type-name. */
19697 || (c_dialect_objc ()
19698 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19699 && objc_is_class_name (decl)))
19700 decl = error_mark_node;
19702 if (decl == error_mark_node)
19703 cp_parser_error (parser, "expected class-name");
19704 else if (identifier && !parser->scope)
19705 maybe_note_name_used_in_class (identifier, decl);
19707 return decl;
19710 /* Parse a class-specifier.
19712 class-specifier:
19713 class-head { member-specification [opt] }
19715 Returns the TREE_TYPE representing the class. */
19717 static tree
19718 cp_parser_class_specifier_1 (cp_parser* parser)
19720 tree type;
19721 tree attributes = NULL_TREE;
19722 bool nested_name_specifier_p;
19723 unsigned saved_num_template_parameter_lists;
19724 bool saved_in_function_body;
19725 unsigned char in_statement;
19726 bool in_switch_statement_p;
19727 bool saved_in_unbraced_linkage_specification_p;
19728 tree old_scope = NULL_TREE;
19729 tree scope = NULL_TREE;
19730 cp_token *closing_brace;
19732 push_deferring_access_checks (dk_no_deferred);
19734 /* Parse the class-head. */
19735 type = cp_parser_class_head (parser,
19736 &nested_name_specifier_p);
19737 /* If the class-head was a semantic disaster, skip the entire body
19738 of the class. */
19739 if (!type)
19741 cp_parser_skip_to_end_of_block_or_statement (parser);
19742 pop_deferring_access_checks ();
19743 return error_mark_node;
19746 /* Look for the `{'. */
19747 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19749 pop_deferring_access_checks ();
19750 return error_mark_node;
19753 cp_ensure_no_omp_declare_simd (parser);
19755 /* Issue an error message if type-definitions are forbidden here. */
19756 cp_parser_check_type_definition (parser);
19757 /* Remember that we are defining one more class. */
19758 ++parser->num_classes_being_defined;
19759 /* Inside the class, surrounding template-parameter-lists do not
19760 apply. */
19761 saved_num_template_parameter_lists
19762 = parser->num_template_parameter_lists;
19763 parser->num_template_parameter_lists = 0;
19764 /* We are not in a function body. */
19765 saved_in_function_body = parser->in_function_body;
19766 parser->in_function_body = false;
19767 /* Or in a loop. */
19768 in_statement = parser->in_statement;
19769 parser->in_statement = 0;
19770 /* Or in a switch. */
19771 in_switch_statement_p = parser->in_switch_statement_p;
19772 parser->in_switch_statement_p = false;
19773 /* We are not immediately inside an extern "lang" block. */
19774 saved_in_unbraced_linkage_specification_p
19775 = parser->in_unbraced_linkage_specification_p;
19776 parser->in_unbraced_linkage_specification_p = false;
19778 /* Start the class. */
19779 if (nested_name_specifier_p)
19781 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19782 old_scope = push_inner_scope (scope);
19784 type = begin_class_definition (type);
19786 if (type == error_mark_node)
19787 /* If the type is erroneous, skip the entire body of the class. */
19788 cp_parser_skip_to_closing_brace (parser);
19789 else
19790 /* Parse the member-specification. */
19791 cp_parser_member_specification_opt (parser);
19793 /* Look for the trailing `}'. */
19794 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19795 /* Look for trailing attributes to apply to this class. */
19796 if (cp_parser_allow_gnu_extensions_p (parser))
19797 attributes = cp_parser_gnu_attributes_opt (parser);
19798 if (type != error_mark_node)
19799 type = finish_struct (type, attributes);
19800 if (nested_name_specifier_p)
19801 pop_inner_scope (old_scope, scope);
19803 /* We've finished a type definition. Check for the common syntax
19804 error of forgetting a semicolon after the definition. We need to
19805 be careful, as we can't just check for not-a-semicolon and be done
19806 with it; the user might have typed:
19808 class X { } c = ...;
19809 class X { } *p = ...;
19811 and so forth. Instead, enumerate all the possible tokens that
19812 might follow this production; if we don't see one of them, then
19813 complain and silently insert the semicolon. */
19815 cp_token *token = cp_lexer_peek_token (parser->lexer);
19816 bool want_semicolon = true;
19818 if (cp_next_tokens_can_be_std_attribute_p (parser))
19819 /* Don't try to parse c++11 attributes here. As per the
19820 grammar, that should be a task for
19821 cp_parser_decl_specifier_seq. */
19822 want_semicolon = false;
19824 switch (token->type)
19826 case CPP_NAME:
19827 case CPP_SEMICOLON:
19828 case CPP_MULT:
19829 case CPP_AND:
19830 case CPP_OPEN_PAREN:
19831 case CPP_CLOSE_PAREN:
19832 case CPP_COMMA:
19833 want_semicolon = false;
19834 break;
19836 /* While it's legal for type qualifiers and storage class
19837 specifiers to follow type definitions in the grammar, only
19838 compiler testsuites contain code like that. Assume that if
19839 we see such code, then what we're really seeing is a case
19840 like:
19842 class X { }
19843 const <type> var = ...;
19847 class Y { }
19848 static <type> func (...) ...
19850 i.e. the qualifier or specifier applies to the next
19851 declaration. To do so, however, we need to look ahead one
19852 more token to see if *that* token is a type specifier.
19854 This code could be improved to handle:
19856 class Z { }
19857 static const <type> var = ...; */
19858 case CPP_KEYWORD:
19859 if (keyword_is_decl_specifier (token->keyword))
19861 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19863 /* Handling user-defined types here would be nice, but very
19864 tricky. */
19865 want_semicolon
19866 = (lookahead->type == CPP_KEYWORD
19867 && keyword_begins_type_specifier (lookahead->keyword));
19869 break;
19870 default:
19871 break;
19874 /* If we don't have a type, then something is very wrong and we
19875 shouldn't try to do anything clever. Likewise for not seeing the
19876 closing brace. */
19877 if (closing_brace && TYPE_P (type) && want_semicolon)
19879 cp_token_position prev
19880 = cp_lexer_previous_token_position (parser->lexer);
19881 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19882 location_t loc = prev_token->location;
19884 if (CLASSTYPE_DECLARED_CLASS (type))
19885 error_at (loc, "expected %<;%> after class definition");
19886 else if (TREE_CODE (type) == RECORD_TYPE)
19887 error_at (loc, "expected %<;%> after struct definition");
19888 else if (TREE_CODE (type) == UNION_TYPE)
19889 error_at (loc, "expected %<;%> after union definition");
19890 else
19891 gcc_unreachable ();
19893 /* Unget one token and smash it to look as though we encountered
19894 a semicolon in the input stream. */
19895 cp_lexer_set_token_position (parser->lexer, prev);
19896 token = cp_lexer_peek_token (parser->lexer);
19897 token->type = CPP_SEMICOLON;
19898 token->keyword = RID_MAX;
19902 /* If this class is not itself within the scope of another class,
19903 then we need to parse the bodies of all of the queued function
19904 definitions. Note that the queued functions defined in a class
19905 are not always processed immediately following the
19906 class-specifier for that class. Consider:
19908 struct A {
19909 struct B { void f() { sizeof (A); } };
19912 If `f' were processed before the processing of `A' were
19913 completed, there would be no way to compute the size of `A'.
19914 Note that the nesting we are interested in here is lexical --
19915 not the semantic nesting given by TYPE_CONTEXT. In particular,
19916 for:
19918 struct A { struct B; };
19919 struct A::B { void f() { } };
19921 there is no need to delay the parsing of `A::B::f'. */
19922 if (--parser->num_classes_being_defined == 0)
19924 tree decl;
19925 tree class_type = NULL_TREE;
19926 tree pushed_scope = NULL_TREE;
19927 unsigned ix;
19928 cp_default_arg_entry *e;
19929 tree save_ccp, save_ccr;
19931 /* In a first pass, parse default arguments to the functions.
19932 Then, in a second pass, parse the bodies of the functions.
19933 This two-phased approach handles cases like:
19935 struct S {
19936 void f() { g(); }
19937 void g(int i = 3);
19941 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19943 decl = e->decl;
19944 /* If there are default arguments that have not yet been processed,
19945 take care of them now. */
19946 if (class_type != e->class_type)
19948 if (pushed_scope)
19949 pop_scope (pushed_scope);
19950 class_type = e->class_type;
19951 pushed_scope = push_scope (class_type);
19953 /* Make sure that any template parameters are in scope. */
19954 maybe_begin_member_template_processing (decl);
19955 /* Parse the default argument expressions. */
19956 cp_parser_late_parsing_default_args (parser, decl);
19957 /* Remove any template parameters from the symbol table. */
19958 maybe_end_member_template_processing ();
19960 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19961 /* Now parse any NSDMIs. */
19962 save_ccp = current_class_ptr;
19963 save_ccr = current_class_ref;
19964 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19966 if (class_type != DECL_CONTEXT (decl))
19968 if (pushed_scope)
19969 pop_scope (pushed_scope);
19970 class_type = DECL_CONTEXT (decl);
19971 pushed_scope = push_scope (class_type);
19973 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19974 cp_parser_late_parsing_nsdmi (parser, decl);
19976 vec_safe_truncate (unparsed_nsdmis, 0);
19977 current_class_ptr = save_ccp;
19978 current_class_ref = save_ccr;
19979 if (pushed_scope)
19980 pop_scope (pushed_scope);
19982 /* Now do some post-NSDMI bookkeeping. */
19983 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19984 after_nsdmi_defaulted_late_checks (class_type);
19985 vec_safe_truncate (unparsed_classes, 0);
19986 after_nsdmi_defaulted_late_checks (type);
19988 /* Now parse the body of the functions. */
19989 if (flag_openmp)
19991 /* OpenMP UDRs need to be parsed before all other functions. */
19992 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19993 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19994 cp_parser_late_parsing_for_member (parser, decl);
19995 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19996 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19997 cp_parser_late_parsing_for_member (parser, decl);
19999 else
20000 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20001 cp_parser_late_parsing_for_member (parser, decl);
20002 vec_safe_truncate (unparsed_funs_with_definitions, 0);
20004 else
20005 vec_safe_push (unparsed_classes, type);
20007 /* Put back any saved access checks. */
20008 pop_deferring_access_checks ();
20010 /* Restore saved state. */
20011 parser->in_switch_statement_p = in_switch_statement_p;
20012 parser->in_statement = in_statement;
20013 parser->in_function_body = saved_in_function_body;
20014 parser->num_template_parameter_lists
20015 = saved_num_template_parameter_lists;
20016 parser->in_unbraced_linkage_specification_p
20017 = saved_in_unbraced_linkage_specification_p;
20019 return type;
20022 static tree
20023 cp_parser_class_specifier (cp_parser* parser)
20025 tree ret;
20026 timevar_push (TV_PARSE_STRUCT);
20027 ret = cp_parser_class_specifier_1 (parser);
20028 timevar_pop (TV_PARSE_STRUCT);
20029 return ret;
20032 /* Parse a class-head.
20034 class-head:
20035 class-key identifier [opt] base-clause [opt]
20036 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20037 class-key nested-name-specifier [opt] template-id
20038 base-clause [opt]
20040 class-virt-specifier:
20041 final
20043 GNU Extensions:
20044 class-key attributes identifier [opt] base-clause [opt]
20045 class-key attributes nested-name-specifier identifier base-clause [opt]
20046 class-key attributes nested-name-specifier [opt] template-id
20047 base-clause [opt]
20049 Upon return BASES is initialized to the list of base classes (or
20050 NULL, if there are none) in the same form returned by
20051 cp_parser_base_clause.
20053 Returns the TYPE of the indicated class. Sets
20054 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20055 involving a nested-name-specifier was used, and FALSE otherwise.
20057 Returns error_mark_node if this is not a class-head.
20059 Returns NULL_TREE if the class-head is syntactically valid, but
20060 semantically invalid in a way that means we should skip the entire
20061 body of the class. */
20063 static tree
20064 cp_parser_class_head (cp_parser* parser,
20065 bool* nested_name_specifier_p)
20067 tree nested_name_specifier;
20068 enum tag_types class_key;
20069 tree id = NULL_TREE;
20070 tree type = NULL_TREE;
20071 tree attributes;
20072 tree bases;
20073 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20074 bool template_id_p = false;
20075 bool qualified_p = false;
20076 bool invalid_nested_name_p = false;
20077 bool invalid_explicit_specialization_p = false;
20078 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20079 tree pushed_scope = NULL_TREE;
20080 unsigned num_templates;
20081 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20082 /* Assume no nested-name-specifier will be present. */
20083 *nested_name_specifier_p = false;
20084 /* Assume no template parameter lists will be used in defining the
20085 type. */
20086 num_templates = 0;
20087 parser->colon_corrects_to_scope_p = false;
20089 /* Look for the class-key. */
20090 class_key = cp_parser_class_key (parser);
20091 if (class_key == none_type)
20092 return error_mark_node;
20094 /* Parse the attributes. */
20095 attributes = cp_parser_attributes_opt (parser);
20097 /* If the next token is `::', that is invalid -- but sometimes
20098 people do try to write:
20100 struct ::S {};
20102 Handle this gracefully by accepting the extra qualifier, and then
20103 issuing an error about it later if this really is a
20104 class-head. If it turns out just to be an elaborated type
20105 specifier, remain silent. */
20106 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20107 qualified_p = true;
20109 push_deferring_access_checks (dk_no_check);
20111 /* Determine the name of the class. Begin by looking for an
20112 optional nested-name-specifier. */
20113 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20114 nested_name_specifier
20115 = cp_parser_nested_name_specifier_opt (parser,
20116 /*typename_keyword_p=*/false,
20117 /*check_dependency_p=*/false,
20118 /*type_p=*/true,
20119 /*is_declaration=*/false);
20120 /* If there was a nested-name-specifier, then there *must* be an
20121 identifier. */
20122 if (nested_name_specifier)
20124 type_start_token = cp_lexer_peek_token (parser->lexer);
20125 /* Although the grammar says `identifier', it really means
20126 `class-name' or `template-name'. You are only allowed to
20127 define a class that has already been declared with this
20128 syntax.
20130 The proposed resolution for Core Issue 180 says that wherever
20131 you see `class T::X' you should treat `X' as a type-name.
20133 It is OK to define an inaccessible class; for example:
20135 class A { class B; };
20136 class A::B {};
20138 We do not know if we will see a class-name, or a
20139 template-name. We look for a class-name first, in case the
20140 class-name is a template-id; if we looked for the
20141 template-name first we would stop after the template-name. */
20142 cp_parser_parse_tentatively (parser);
20143 type = cp_parser_class_name (parser,
20144 /*typename_keyword_p=*/false,
20145 /*template_keyword_p=*/false,
20146 class_type,
20147 /*check_dependency_p=*/false,
20148 /*class_head_p=*/true,
20149 /*is_declaration=*/false);
20150 /* If that didn't work, ignore the nested-name-specifier. */
20151 if (!cp_parser_parse_definitely (parser))
20153 invalid_nested_name_p = true;
20154 type_start_token = cp_lexer_peek_token (parser->lexer);
20155 id = cp_parser_identifier (parser);
20156 if (id == error_mark_node)
20157 id = NULL_TREE;
20159 /* If we could not find a corresponding TYPE, treat this
20160 declaration like an unqualified declaration. */
20161 if (type == error_mark_node)
20162 nested_name_specifier = NULL_TREE;
20163 /* Otherwise, count the number of templates used in TYPE and its
20164 containing scopes. */
20165 else
20167 tree scope;
20169 for (scope = TREE_TYPE (type);
20170 scope && TREE_CODE (scope) != NAMESPACE_DECL;
20171 scope = get_containing_scope (scope))
20172 if (TYPE_P (scope)
20173 && CLASS_TYPE_P (scope)
20174 && CLASSTYPE_TEMPLATE_INFO (scope)
20175 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20176 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20177 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20178 ++num_templates;
20181 /* Otherwise, the identifier is optional. */
20182 else
20184 /* We don't know whether what comes next is a template-id,
20185 an identifier, or nothing at all. */
20186 cp_parser_parse_tentatively (parser);
20187 /* Check for a template-id. */
20188 type_start_token = cp_lexer_peek_token (parser->lexer);
20189 id = cp_parser_template_id (parser,
20190 /*template_keyword_p=*/false,
20191 /*check_dependency_p=*/true,
20192 class_key,
20193 /*is_declaration=*/true);
20194 /* If that didn't work, it could still be an identifier. */
20195 if (!cp_parser_parse_definitely (parser))
20197 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20199 type_start_token = cp_lexer_peek_token (parser->lexer);
20200 id = cp_parser_identifier (parser);
20202 else
20203 id = NULL_TREE;
20205 else
20207 template_id_p = true;
20208 ++num_templates;
20212 pop_deferring_access_checks ();
20214 if (id)
20216 cp_parser_check_for_invalid_template_id (parser, id,
20217 class_key,
20218 type_start_token->location);
20220 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20222 /* If it's not a `:' or a `{' then we can't really be looking at a
20223 class-head, since a class-head only appears as part of a
20224 class-specifier. We have to detect this situation before calling
20225 xref_tag, since that has irreversible side-effects. */
20226 if (!cp_parser_next_token_starts_class_definition_p (parser))
20228 cp_parser_error (parser, "expected %<{%> or %<:%>");
20229 type = error_mark_node;
20230 goto out;
20233 /* At this point, we're going ahead with the class-specifier, even
20234 if some other problem occurs. */
20235 cp_parser_commit_to_tentative_parse (parser);
20236 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20238 cp_parser_error (parser,
20239 "cannot specify %<override%> for a class");
20240 type = error_mark_node;
20241 goto out;
20243 /* Issue the error about the overly-qualified name now. */
20244 if (qualified_p)
20246 cp_parser_error (parser,
20247 "global qualification of class name is invalid");
20248 type = error_mark_node;
20249 goto out;
20251 else if (invalid_nested_name_p)
20253 cp_parser_error (parser,
20254 "qualified name does not name a class");
20255 type = error_mark_node;
20256 goto out;
20258 else if (nested_name_specifier)
20260 tree scope;
20262 /* Reject typedef-names in class heads. */
20263 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20265 error_at (type_start_token->location,
20266 "invalid class name in declaration of %qD",
20267 type);
20268 type = NULL_TREE;
20269 goto done;
20272 /* Figure out in what scope the declaration is being placed. */
20273 scope = current_scope ();
20274 /* If that scope does not contain the scope in which the
20275 class was originally declared, the program is invalid. */
20276 if (scope && !is_ancestor (scope, nested_name_specifier))
20278 if (at_namespace_scope_p ())
20279 error_at (type_start_token->location,
20280 "declaration of %qD in namespace %qD which does not "
20281 "enclose %qD",
20282 type, scope, nested_name_specifier);
20283 else
20284 error_at (type_start_token->location,
20285 "declaration of %qD in %qD which does not enclose %qD",
20286 type, scope, nested_name_specifier);
20287 type = NULL_TREE;
20288 goto done;
20290 /* [dcl.meaning]
20292 A declarator-id shall not be qualified except for the
20293 definition of a ... nested class outside of its class
20294 ... [or] the definition or explicit instantiation of a
20295 class member of a namespace outside of its namespace. */
20296 if (scope == nested_name_specifier)
20298 permerror (nested_name_specifier_token_start->location,
20299 "extra qualification not allowed");
20300 nested_name_specifier = NULL_TREE;
20301 num_templates = 0;
20304 /* An explicit-specialization must be preceded by "template <>". If
20305 it is not, try to recover gracefully. */
20306 if (at_namespace_scope_p ()
20307 && parser->num_template_parameter_lists == 0
20308 && template_id_p)
20310 error_at (type_start_token->location,
20311 "an explicit specialization must be preceded by %<template <>%>");
20312 invalid_explicit_specialization_p = true;
20313 /* Take the same action that would have been taken by
20314 cp_parser_explicit_specialization. */
20315 ++parser->num_template_parameter_lists;
20316 begin_specialization ();
20318 /* There must be no "return" statements between this point and the
20319 end of this function; set "type "to the correct return value and
20320 use "goto done;" to return. */
20321 /* Make sure that the right number of template parameters were
20322 present. */
20323 if (!cp_parser_check_template_parameters (parser, num_templates,
20324 type_start_token->location,
20325 /*declarator=*/NULL))
20327 /* If something went wrong, there is no point in even trying to
20328 process the class-definition. */
20329 type = NULL_TREE;
20330 goto done;
20333 /* Look up the type. */
20334 if (template_id_p)
20336 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20337 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20338 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20340 error_at (type_start_token->location,
20341 "function template %qD redeclared as a class template", id);
20342 type = error_mark_node;
20344 else
20346 type = TREE_TYPE (id);
20347 type = maybe_process_partial_specialization (type);
20349 if (nested_name_specifier)
20350 pushed_scope = push_scope (nested_name_specifier);
20352 else if (nested_name_specifier)
20354 tree class_type;
20356 /* Given:
20358 template <typename T> struct S { struct T };
20359 template <typename T> struct S<T>::T { };
20361 we will get a TYPENAME_TYPE when processing the definition of
20362 `S::T'. We need to resolve it to the actual type before we
20363 try to define it. */
20364 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20366 class_type = resolve_typename_type (TREE_TYPE (type),
20367 /*only_current_p=*/false);
20368 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20369 type = TYPE_NAME (class_type);
20370 else
20372 cp_parser_error (parser, "could not resolve typename type");
20373 type = error_mark_node;
20377 if (maybe_process_partial_specialization (TREE_TYPE (type))
20378 == error_mark_node)
20380 type = NULL_TREE;
20381 goto done;
20384 class_type = current_class_type;
20385 /* Enter the scope indicated by the nested-name-specifier. */
20386 pushed_scope = push_scope (nested_name_specifier);
20387 /* Get the canonical version of this type. */
20388 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20389 /* Call push_template_decl if it seems like we should be defining a
20390 template either from the template headers or the type we're
20391 defining, so that we diagnose both extra and missing headers. */
20392 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20393 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20394 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20396 type = push_template_decl (type);
20397 if (type == error_mark_node)
20399 type = NULL_TREE;
20400 goto done;
20404 type = TREE_TYPE (type);
20405 *nested_name_specifier_p = true;
20407 else /* The name is not a nested name. */
20409 /* If the class was unnamed, create a dummy name. */
20410 if (!id)
20411 id = make_anon_name ();
20412 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20413 parser->num_template_parameter_lists);
20416 /* Indicate whether this class was declared as a `class' or as a
20417 `struct'. */
20418 if (TREE_CODE (type) == RECORD_TYPE)
20419 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20420 cp_parser_check_class_key (class_key, type);
20422 /* If this type was already complete, and we see another definition,
20423 that's an error. */
20424 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20426 error_at (type_start_token->location, "redefinition of %q#T",
20427 type);
20428 error_at (type_start_token->location, "previous definition of %q+#T",
20429 type);
20430 type = NULL_TREE;
20431 goto done;
20433 else if (type == error_mark_node)
20434 type = NULL_TREE;
20436 if (type)
20438 /* Apply attributes now, before any use of the class as a template
20439 argument in its base list. */
20440 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20441 fixup_attribute_variants (type);
20444 /* We will have entered the scope containing the class; the names of
20445 base classes should be looked up in that context. For example:
20447 struct A { struct B {}; struct C; };
20448 struct A::C : B {};
20450 is valid. */
20452 /* Get the list of base-classes, if there is one. */
20453 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20455 /* PR59482: enter the class scope so that base-specifiers are looked
20456 up correctly. */
20457 if (type)
20458 pushclass (type);
20459 bases = cp_parser_base_clause (parser);
20460 /* PR59482: get out of the previously pushed class scope so that the
20461 subsequent pops pop the right thing. */
20462 if (type)
20463 popclass ();
20465 else
20466 bases = NULL_TREE;
20468 /* If we're really defining a class, process the base classes.
20469 If they're invalid, fail. */
20470 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20471 && !xref_basetypes (type, bases))
20472 type = NULL_TREE;
20474 done:
20475 /* Leave the scope given by the nested-name-specifier. We will
20476 enter the class scope itself while processing the members. */
20477 if (pushed_scope)
20478 pop_scope (pushed_scope);
20480 if (invalid_explicit_specialization_p)
20482 end_specialization ();
20483 --parser->num_template_parameter_lists;
20486 if (type)
20487 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20488 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20489 CLASSTYPE_FINAL (type) = 1;
20490 out:
20491 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20492 return type;
20495 /* Parse a class-key.
20497 class-key:
20498 class
20499 struct
20500 union
20502 Returns the kind of class-key specified, or none_type to indicate
20503 error. */
20505 static enum tag_types
20506 cp_parser_class_key (cp_parser* parser)
20508 cp_token *token;
20509 enum tag_types tag_type;
20511 /* Look for the class-key. */
20512 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20513 if (!token)
20514 return none_type;
20516 /* Check to see if the TOKEN is a class-key. */
20517 tag_type = cp_parser_token_is_class_key (token);
20518 if (!tag_type)
20519 cp_parser_error (parser, "expected class-key");
20520 return tag_type;
20523 /* Parse a type-parameter-key.
20525 type-parameter-key:
20526 class
20527 typename
20530 static void
20531 cp_parser_type_parameter_key (cp_parser* parser)
20533 /* Look for the type-parameter-key. */
20534 enum tag_types tag_type = none_type;
20535 cp_token *token = cp_lexer_peek_token (parser->lexer);
20536 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20538 cp_lexer_consume_token (parser->lexer);
20539 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20540 /* typename is not allowed in a template template parameter
20541 by the standard until C++1Z. */
20542 pedwarn (token->location, OPT_Wpedantic,
20543 "ISO C++ forbids typename key in template template parameter;"
20544 " use -std=c++1z or -std=gnu++1z");
20546 else
20547 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20549 return;
20552 /* Parse an (optional) member-specification.
20554 member-specification:
20555 member-declaration member-specification [opt]
20556 access-specifier : member-specification [opt] */
20558 static void
20559 cp_parser_member_specification_opt (cp_parser* parser)
20561 while (true)
20563 cp_token *token;
20564 enum rid keyword;
20566 /* Peek at the next token. */
20567 token = cp_lexer_peek_token (parser->lexer);
20568 /* If it's a `}', or EOF then we've seen all the members. */
20569 if (token->type == CPP_CLOSE_BRACE
20570 || token->type == CPP_EOF
20571 || token->type == CPP_PRAGMA_EOL)
20572 break;
20574 /* See if this token is a keyword. */
20575 keyword = token->keyword;
20576 switch (keyword)
20578 case RID_PUBLIC:
20579 case RID_PROTECTED:
20580 case RID_PRIVATE:
20581 /* Consume the access-specifier. */
20582 cp_lexer_consume_token (parser->lexer);
20583 /* Remember which access-specifier is active. */
20584 current_access_specifier = token->u.value;
20585 /* Look for the `:'. */
20586 cp_parser_require (parser, CPP_COLON, RT_COLON);
20587 break;
20589 default:
20590 /* Accept #pragmas at class scope. */
20591 if (token->type == CPP_PRAGMA)
20593 cp_parser_pragma (parser, pragma_member);
20594 break;
20597 /* Otherwise, the next construction must be a
20598 member-declaration. */
20599 cp_parser_member_declaration (parser);
20604 /* Parse a member-declaration.
20606 member-declaration:
20607 decl-specifier-seq [opt] member-declarator-list [opt] ;
20608 function-definition ; [opt]
20609 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20610 using-declaration
20611 template-declaration
20612 alias-declaration
20614 member-declarator-list:
20615 member-declarator
20616 member-declarator-list , member-declarator
20618 member-declarator:
20619 declarator pure-specifier [opt]
20620 declarator constant-initializer [opt]
20621 identifier [opt] : constant-expression
20623 GNU Extensions:
20625 member-declaration:
20626 __extension__ member-declaration
20628 member-declarator:
20629 declarator attributes [opt] pure-specifier [opt]
20630 declarator attributes [opt] constant-initializer [opt]
20631 identifier [opt] attributes [opt] : constant-expression
20633 C++0x Extensions:
20635 member-declaration:
20636 static_assert-declaration */
20638 static void
20639 cp_parser_member_declaration (cp_parser* parser)
20641 cp_decl_specifier_seq decl_specifiers;
20642 tree prefix_attributes;
20643 tree decl;
20644 int declares_class_or_enum;
20645 bool friend_p;
20646 cp_token *token = NULL;
20647 cp_token *decl_spec_token_start = NULL;
20648 cp_token *initializer_token_start = NULL;
20649 int saved_pedantic;
20650 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20652 /* Check for the `__extension__' keyword. */
20653 if (cp_parser_extension_opt (parser, &saved_pedantic))
20655 /* Recurse. */
20656 cp_parser_member_declaration (parser);
20657 /* Restore the old value of the PEDANTIC flag. */
20658 pedantic = saved_pedantic;
20660 return;
20663 /* Check for a template-declaration. */
20664 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20666 /* An explicit specialization here is an error condition, and we
20667 expect the specialization handler to detect and report this. */
20668 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20669 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20670 cp_parser_explicit_specialization (parser);
20671 else
20672 cp_parser_template_declaration (parser, /*member_p=*/true);
20674 return;
20677 /* Check for a using-declaration. */
20678 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20680 if (cxx_dialect < cxx11)
20682 /* Parse the using-declaration. */
20683 cp_parser_using_declaration (parser,
20684 /*access_declaration_p=*/false);
20685 return;
20687 else
20689 tree decl;
20690 bool alias_decl_expected;
20691 cp_parser_parse_tentatively (parser);
20692 decl = cp_parser_alias_declaration (parser);
20693 /* Note that if we actually see the '=' token after the
20694 identifier, cp_parser_alias_declaration commits the
20695 tentative parse. In that case, we really expects an
20696 alias-declaration. Otherwise, we expect a using
20697 declaration. */
20698 alias_decl_expected =
20699 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20700 cp_parser_parse_definitely (parser);
20702 if (alias_decl_expected)
20703 finish_member_declaration (decl);
20704 else
20705 cp_parser_using_declaration (parser,
20706 /*access_declaration_p=*/false);
20707 return;
20711 /* Check for @defs. */
20712 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20714 tree ivar, member;
20715 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20716 ivar = ivar_chains;
20717 while (ivar)
20719 member = ivar;
20720 ivar = TREE_CHAIN (member);
20721 TREE_CHAIN (member) = NULL_TREE;
20722 finish_member_declaration (member);
20724 return;
20727 /* If the next token is `static_assert' we have a static assertion. */
20728 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20730 cp_parser_static_assert (parser, /*member_p=*/true);
20731 return;
20734 parser->colon_corrects_to_scope_p = false;
20736 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20737 goto out;
20739 /* Parse the decl-specifier-seq. */
20740 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20741 cp_parser_decl_specifier_seq (parser,
20742 CP_PARSER_FLAGS_OPTIONAL,
20743 &decl_specifiers,
20744 &declares_class_or_enum);
20745 /* Check for an invalid type-name. */
20746 if (!decl_specifiers.any_type_specifiers_p
20747 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20748 goto out;
20749 /* If there is no declarator, then the decl-specifier-seq should
20750 specify a type. */
20751 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20753 /* If there was no decl-specifier-seq, and the next token is a
20754 `;', then we have something like:
20756 struct S { ; };
20758 [class.mem]
20760 Each member-declaration shall declare at least one member
20761 name of the class. */
20762 if (!decl_specifiers.any_specifiers_p)
20764 cp_token *token = cp_lexer_peek_token (parser->lexer);
20765 if (!in_system_header_at (token->location))
20766 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20768 else
20770 tree type;
20772 /* See if this declaration is a friend. */
20773 friend_p = cp_parser_friend_p (&decl_specifiers);
20774 /* If there were decl-specifiers, check to see if there was
20775 a class-declaration. */
20776 type = check_tag_decl (&decl_specifiers,
20777 /*explicit_type_instantiation_p=*/false);
20778 /* Nested classes have already been added to the class, but
20779 a `friend' needs to be explicitly registered. */
20780 if (friend_p)
20782 /* If the `friend' keyword was present, the friend must
20783 be introduced with a class-key. */
20784 if (!declares_class_or_enum && cxx_dialect < cxx11)
20785 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20786 "in C++03 a class-key must be used "
20787 "when declaring a friend");
20788 /* In this case:
20790 template <typename T> struct A {
20791 friend struct A<T>::B;
20794 A<T>::B will be represented by a TYPENAME_TYPE, and
20795 therefore not recognized by check_tag_decl. */
20796 if (!type)
20798 type = decl_specifiers.type;
20799 if (type && TREE_CODE (type) == TYPE_DECL)
20800 type = TREE_TYPE (type);
20802 if (!type || !TYPE_P (type))
20803 error_at (decl_spec_token_start->location,
20804 "friend declaration does not name a class or "
20805 "function");
20806 else
20807 make_friend_class (current_class_type, type,
20808 /*complain=*/true);
20810 /* If there is no TYPE, an error message will already have
20811 been issued. */
20812 else if (!type || type == error_mark_node)
20814 /* An anonymous aggregate has to be handled specially; such
20815 a declaration really declares a data member (with a
20816 particular type), as opposed to a nested class. */
20817 else if (ANON_AGGR_TYPE_P (type))
20819 /* C++11 9.5/6. */
20820 if (decl_specifiers.storage_class != sc_none)
20821 error_at (decl_spec_token_start->location,
20822 "a storage class on an anonymous aggregate "
20823 "in class scope is not allowed");
20825 /* Remove constructors and such from TYPE, now that we
20826 know it is an anonymous aggregate. */
20827 fixup_anonymous_aggr (type);
20828 /* And make the corresponding data member. */
20829 decl = build_decl (decl_spec_token_start->location,
20830 FIELD_DECL, NULL_TREE, type);
20831 /* Add it to the class. */
20832 finish_member_declaration (decl);
20834 else
20835 cp_parser_check_access_in_redeclaration
20836 (TYPE_NAME (type),
20837 decl_spec_token_start->location);
20840 else
20842 bool assume_semicolon = false;
20844 /* Clear attributes from the decl_specifiers but keep them
20845 around as prefix attributes that apply them to the entity
20846 being declared. */
20847 prefix_attributes = decl_specifiers.attributes;
20848 decl_specifiers.attributes = NULL_TREE;
20850 /* See if these declarations will be friends. */
20851 friend_p = cp_parser_friend_p (&decl_specifiers);
20853 /* Keep going until we hit the `;' at the end of the
20854 declaration. */
20855 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20857 tree attributes = NULL_TREE;
20858 tree first_attribute;
20860 /* Peek at the next token. */
20861 token = cp_lexer_peek_token (parser->lexer);
20863 /* Check for a bitfield declaration. */
20864 if (token->type == CPP_COLON
20865 || (token->type == CPP_NAME
20866 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20867 == CPP_COLON))
20869 tree identifier;
20870 tree width;
20872 /* Get the name of the bitfield. Note that we cannot just
20873 check TOKEN here because it may have been invalidated by
20874 the call to cp_lexer_peek_nth_token above. */
20875 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20876 identifier = cp_parser_identifier (parser);
20877 else
20878 identifier = NULL_TREE;
20880 /* Consume the `:' token. */
20881 cp_lexer_consume_token (parser->lexer);
20882 /* Get the width of the bitfield. */
20883 width
20884 = cp_parser_constant_expression (parser);
20886 /* Look for attributes that apply to the bitfield. */
20887 attributes = cp_parser_attributes_opt (parser);
20888 /* Remember which attributes are prefix attributes and
20889 which are not. */
20890 first_attribute = attributes;
20891 /* Combine the attributes. */
20892 attributes = chainon (prefix_attributes, attributes);
20894 /* Create the bitfield declaration. */
20895 decl = grokbitfield (identifier
20896 ? make_id_declarator (NULL_TREE,
20897 identifier,
20898 sfk_none)
20899 : NULL,
20900 &decl_specifiers,
20901 width,
20902 attributes);
20904 else
20906 cp_declarator *declarator;
20907 tree initializer;
20908 tree asm_specification;
20909 int ctor_dtor_or_conv_p;
20911 /* Parse the declarator. */
20912 declarator
20913 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20914 &ctor_dtor_or_conv_p,
20915 /*parenthesized_p=*/NULL,
20916 /*member_p=*/true,
20917 friend_p);
20919 /* If something went wrong parsing the declarator, make sure
20920 that we at least consume some tokens. */
20921 if (declarator == cp_error_declarator)
20923 /* Skip to the end of the statement. */
20924 cp_parser_skip_to_end_of_statement (parser);
20925 /* If the next token is not a semicolon, that is
20926 probably because we just skipped over the body of
20927 a function. So, we consume a semicolon if
20928 present, but do not issue an error message if it
20929 is not present. */
20930 if (cp_lexer_next_token_is (parser->lexer,
20931 CPP_SEMICOLON))
20932 cp_lexer_consume_token (parser->lexer);
20933 goto out;
20936 if (declares_class_or_enum & 2)
20937 cp_parser_check_for_definition_in_return_type
20938 (declarator, decl_specifiers.type,
20939 decl_specifiers.locations[ds_type_spec]);
20941 /* Look for an asm-specification. */
20942 asm_specification = cp_parser_asm_specification_opt (parser);
20943 /* Look for attributes that apply to the declaration. */
20944 attributes = cp_parser_attributes_opt (parser);
20945 /* Remember which attributes are prefix attributes and
20946 which are not. */
20947 first_attribute = attributes;
20948 /* Combine the attributes. */
20949 attributes = chainon (prefix_attributes, attributes);
20951 /* If it's an `=', then we have a constant-initializer or a
20952 pure-specifier. It is not correct to parse the
20953 initializer before registering the member declaration
20954 since the member declaration should be in scope while
20955 its initializer is processed. However, the rest of the
20956 front end does not yet provide an interface that allows
20957 us to handle this correctly. */
20958 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20960 /* In [class.mem]:
20962 A pure-specifier shall be used only in the declaration of
20963 a virtual function.
20965 A member-declarator can contain a constant-initializer
20966 only if it declares a static member of integral or
20967 enumeration type.
20969 Therefore, if the DECLARATOR is for a function, we look
20970 for a pure-specifier; otherwise, we look for a
20971 constant-initializer. When we call `grokfield', it will
20972 perform more stringent semantics checks. */
20973 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20974 if (function_declarator_p (declarator)
20975 || (decl_specifiers.type
20976 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20977 && declarator->kind == cdk_id
20978 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20979 == FUNCTION_TYPE)))
20980 initializer = cp_parser_pure_specifier (parser);
20981 else if (decl_specifiers.storage_class != sc_static)
20982 initializer = cp_parser_save_nsdmi (parser);
20983 else if (cxx_dialect >= cxx11)
20985 bool nonconst;
20986 /* Don't require a constant rvalue in C++11, since we
20987 might want a reference constant. We'll enforce
20988 constancy later. */
20989 cp_lexer_consume_token (parser->lexer);
20990 /* Parse the initializer. */
20991 initializer = cp_parser_initializer_clause (parser,
20992 &nonconst);
20994 else
20995 /* Parse the initializer. */
20996 initializer = cp_parser_constant_initializer (parser);
20998 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20999 && !function_declarator_p (declarator))
21001 bool x;
21002 if (decl_specifiers.storage_class != sc_static)
21003 initializer = cp_parser_save_nsdmi (parser);
21004 else
21005 initializer = cp_parser_initializer (parser, &x, &x);
21007 /* Otherwise, there is no initializer. */
21008 else
21009 initializer = NULL_TREE;
21011 /* See if we are probably looking at a function
21012 definition. We are certainly not looking at a
21013 member-declarator. Calling `grokfield' has
21014 side-effects, so we must not do it unless we are sure
21015 that we are looking at a member-declarator. */
21016 if (cp_parser_token_starts_function_definition_p
21017 (cp_lexer_peek_token (parser->lexer)))
21019 /* The grammar does not allow a pure-specifier to be
21020 used when a member function is defined. (It is
21021 possible that this fact is an oversight in the
21022 standard, since a pure function may be defined
21023 outside of the class-specifier. */
21024 if (initializer && initializer_token_start)
21025 error_at (initializer_token_start->location,
21026 "pure-specifier on function-definition");
21027 decl = cp_parser_save_member_function_body (parser,
21028 &decl_specifiers,
21029 declarator,
21030 attributes);
21031 if (parser->fully_implicit_function_template_p)
21032 decl = finish_fully_implicit_template (parser, decl);
21033 /* If the member was not a friend, declare it here. */
21034 if (!friend_p)
21035 finish_member_declaration (decl);
21036 /* Peek at the next token. */
21037 token = cp_lexer_peek_token (parser->lexer);
21038 /* If the next token is a semicolon, consume it. */
21039 if (token->type == CPP_SEMICOLON)
21040 cp_lexer_consume_token (parser->lexer);
21041 goto out;
21043 else
21044 if (declarator->kind == cdk_function)
21045 declarator->id_loc = token->location;
21046 /* Create the declaration. */
21047 decl = grokfield (declarator, &decl_specifiers,
21048 initializer, /*init_const_expr_p=*/true,
21049 asm_specification, attributes);
21050 if (parser->fully_implicit_function_template_p)
21052 if (friend_p)
21053 finish_fully_implicit_template (parser, 0);
21054 else
21055 decl = finish_fully_implicit_template (parser, decl);
21059 cp_finalize_omp_declare_simd (parser, decl);
21061 /* Reset PREFIX_ATTRIBUTES. */
21062 while (attributes && TREE_CHAIN (attributes) != first_attribute)
21063 attributes = TREE_CHAIN (attributes);
21064 if (attributes)
21065 TREE_CHAIN (attributes) = NULL_TREE;
21067 /* If there is any qualification still in effect, clear it
21068 now; we will be starting fresh with the next declarator. */
21069 parser->scope = NULL_TREE;
21070 parser->qualifying_scope = NULL_TREE;
21071 parser->object_scope = NULL_TREE;
21072 /* If it's a `,', then there are more declarators. */
21073 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21075 cp_lexer_consume_token (parser->lexer);
21076 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21078 cp_token *token = cp_lexer_previous_token (parser->lexer);
21079 error_at (token->location,
21080 "stray %<,%> at end of member declaration");
21083 /* If the next token isn't a `;', then we have a parse error. */
21084 else if (cp_lexer_next_token_is_not (parser->lexer,
21085 CPP_SEMICOLON))
21087 /* The next token might be a ways away from where the
21088 actual semicolon is missing. Find the previous token
21089 and use that for our error position. */
21090 cp_token *token = cp_lexer_previous_token (parser->lexer);
21091 error_at (token->location,
21092 "expected %<;%> at end of member declaration");
21094 /* Assume that the user meant to provide a semicolon. If
21095 we were to cp_parser_skip_to_end_of_statement, we might
21096 skip to a semicolon inside a member function definition
21097 and issue nonsensical error messages. */
21098 assume_semicolon = true;
21101 if (decl)
21103 /* Add DECL to the list of members. */
21104 if (!friend_p
21105 /* Explicitly include, eg, NSDMIs, for better error
21106 recovery (c++/58650). */
21107 || !DECL_DECLARES_FUNCTION_P (decl))
21108 finish_member_declaration (decl);
21110 if (TREE_CODE (decl) == FUNCTION_DECL)
21111 cp_parser_save_default_args (parser, decl);
21112 else if (TREE_CODE (decl) == FIELD_DECL
21113 && !DECL_C_BIT_FIELD (decl)
21114 && DECL_INITIAL (decl))
21115 /* Add DECL to the queue of NSDMI to be parsed later. */
21116 vec_safe_push (unparsed_nsdmis, decl);
21119 if (assume_semicolon)
21120 goto out;
21124 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21125 out:
21126 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21129 /* Parse a pure-specifier.
21131 pure-specifier:
21134 Returns INTEGER_ZERO_NODE if a pure specifier is found.
21135 Otherwise, ERROR_MARK_NODE is returned. */
21137 static tree
21138 cp_parser_pure_specifier (cp_parser* parser)
21140 cp_token *token;
21142 /* Look for the `=' token. */
21143 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21144 return error_mark_node;
21145 /* Look for the `0' token. */
21146 token = cp_lexer_peek_token (parser->lexer);
21148 if (token->type == CPP_EOF
21149 || token->type == CPP_PRAGMA_EOL)
21150 return error_mark_node;
21152 cp_lexer_consume_token (parser->lexer);
21154 /* Accept = default or = delete in c++0x mode. */
21155 if (token->keyword == RID_DEFAULT
21156 || token->keyword == RID_DELETE)
21158 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21159 return token->u.value;
21162 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
21163 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21165 cp_parser_error (parser,
21166 "invalid pure specifier (only %<= 0%> is allowed)");
21167 cp_parser_skip_to_end_of_statement (parser);
21168 return error_mark_node;
21170 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21172 error_at (token->location, "templates may not be %<virtual%>");
21173 return error_mark_node;
21176 return integer_zero_node;
21179 /* Parse a constant-initializer.
21181 constant-initializer:
21182 = constant-expression
21184 Returns a representation of the constant-expression. */
21186 static tree
21187 cp_parser_constant_initializer (cp_parser* parser)
21189 /* Look for the `=' token. */
21190 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21191 return error_mark_node;
21193 /* It is invalid to write:
21195 struct S { static const int i = { 7 }; };
21198 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21200 cp_parser_error (parser,
21201 "a brace-enclosed initializer is not allowed here");
21202 /* Consume the opening brace. */
21203 cp_lexer_consume_token (parser->lexer);
21204 /* Skip the initializer. */
21205 cp_parser_skip_to_closing_brace (parser);
21206 /* Look for the trailing `}'. */
21207 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21209 return error_mark_node;
21212 return cp_parser_constant_expression (parser);
21215 /* Derived classes [gram.class.derived] */
21217 /* Parse a base-clause.
21219 base-clause:
21220 : base-specifier-list
21222 base-specifier-list:
21223 base-specifier ... [opt]
21224 base-specifier-list , base-specifier ... [opt]
21226 Returns a TREE_LIST representing the base-classes, in the order in
21227 which they were declared. The representation of each node is as
21228 described by cp_parser_base_specifier.
21230 In the case that no bases are specified, this function will return
21231 NULL_TREE, not ERROR_MARK_NODE. */
21233 static tree
21234 cp_parser_base_clause (cp_parser* parser)
21236 tree bases = NULL_TREE;
21238 /* Look for the `:' that begins the list. */
21239 cp_parser_require (parser, CPP_COLON, RT_COLON);
21241 /* Scan the base-specifier-list. */
21242 while (true)
21244 cp_token *token;
21245 tree base;
21246 bool pack_expansion_p = false;
21248 /* Look for the base-specifier. */
21249 base = cp_parser_base_specifier (parser);
21250 /* Look for the (optional) ellipsis. */
21251 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21253 /* Consume the `...'. */
21254 cp_lexer_consume_token (parser->lexer);
21256 pack_expansion_p = true;
21259 /* Add BASE to the front of the list. */
21260 if (base && base != error_mark_node)
21262 if (pack_expansion_p)
21263 /* Make this a pack expansion type. */
21264 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21266 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21268 TREE_CHAIN (base) = bases;
21269 bases = base;
21272 /* Peek at the next token. */
21273 token = cp_lexer_peek_token (parser->lexer);
21274 /* If it's not a comma, then the list is complete. */
21275 if (token->type != CPP_COMMA)
21276 break;
21277 /* Consume the `,'. */
21278 cp_lexer_consume_token (parser->lexer);
21281 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21282 base class had a qualified name. However, the next name that
21283 appears is certainly not qualified. */
21284 parser->scope = NULL_TREE;
21285 parser->qualifying_scope = NULL_TREE;
21286 parser->object_scope = NULL_TREE;
21288 return nreverse (bases);
21291 /* Parse a base-specifier.
21293 base-specifier:
21294 :: [opt] nested-name-specifier [opt] class-name
21295 virtual access-specifier [opt] :: [opt] nested-name-specifier
21296 [opt] class-name
21297 access-specifier virtual [opt] :: [opt] nested-name-specifier
21298 [opt] class-name
21300 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21301 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21302 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21303 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21305 static tree
21306 cp_parser_base_specifier (cp_parser* parser)
21308 cp_token *token;
21309 bool done = false;
21310 bool virtual_p = false;
21311 bool duplicate_virtual_error_issued_p = false;
21312 bool duplicate_access_error_issued_p = false;
21313 bool class_scope_p, template_p;
21314 tree access = access_default_node;
21315 tree type;
21317 /* Process the optional `virtual' and `access-specifier'. */
21318 while (!done)
21320 /* Peek at the next token. */
21321 token = cp_lexer_peek_token (parser->lexer);
21322 /* Process `virtual'. */
21323 switch (token->keyword)
21325 case RID_VIRTUAL:
21326 /* If `virtual' appears more than once, issue an error. */
21327 if (virtual_p && !duplicate_virtual_error_issued_p)
21329 cp_parser_error (parser,
21330 "%<virtual%> specified more than once in base-specified");
21331 duplicate_virtual_error_issued_p = true;
21334 virtual_p = true;
21336 /* Consume the `virtual' token. */
21337 cp_lexer_consume_token (parser->lexer);
21339 break;
21341 case RID_PUBLIC:
21342 case RID_PROTECTED:
21343 case RID_PRIVATE:
21344 /* If more than one access specifier appears, issue an
21345 error. */
21346 if (access != access_default_node
21347 && !duplicate_access_error_issued_p)
21349 cp_parser_error (parser,
21350 "more than one access specifier in base-specified");
21351 duplicate_access_error_issued_p = true;
21354 access = ridpointers[(int) token->keyword];
21356 /* Consume the access-specifier. */
21357 cp_lexer_consume_token (parser->lexer);
21359 break;
21361 default:
21362 done = true;
21363 break;
21366 /* It is not uncommon to see programs mechanically, erroneously, use
21367 the 'typename' keyword to denote (dependent) qualified types
21368 as base classes. */
21369 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21371 token = cp_lexer_peek_token (parser->lexer);
21372 if (!processing_template_decl)
21373 error_at (token->location,
21374 "keyword %<typename%> not allowed outside of templates");
21375 else
21376 error_at (token->location,
21377 "keyword %<typename%> not allowed in this context "
21378 "(the base class is implicitly a type)");
21379 cp_lexer_consume_token (parser->lexer);
21382 /* Look for the optional `::' operator. */
21383 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21384 /* Look for the nested-name-specifier. The simplest way to
21385 implement:
21387 [temp.res]
21389 The keyword `typename' is not permitted in a base-specifier or
21390 mem-initializer; in these contexts a qualified name that
21391 depends on a template-parameter is implicitly assumed to be a
21392 type name.
21394 is to pretend that we have seen the `typename' keyword at this
21395 point. */
21396 cp_parser_nested_name_specifier_opt (parser,
21397 /*typename_keyword_p=*/true,
21398 /*check_dependency_p=*/true,
21399 typename_type,
21400 /*is_declaration=*/true);
21401 /* If the base class is given by a qualified name, assume that names
21402 we see are type names or templates, as appropriate. */
21403 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21404 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21406 if (!parser->scope
21407 && cp_lexer_next_token_is_decltype (parser->lexer))
21408 /* DR 950 allows decltype as a base-specifier. */
21409 type = cp_parser_decltype (parser);
21410 else
21412 /* Otherwise, look for the class-name. */
21413 type = cp_parser_class_name (parser,
21414 class_scope_p,
21415 template_p,
21416 typename_type,
21417 /*check_dependency_p=*/true,
21418 /*class_head_p=*/false,
21419 /*is_declaration=*/true);
21420 type = TREE_TYPE (type);
21423 if (type == error_mark_node)
21424 return error_mark_node;
21426 return finish_base_specifier (type, access, virtual_p);
21429 /* Exception handling [gram.exception] */
21431 /* Parse an (optional) noexcept-specification.
21433 noexcept-specification:
21434 noexcept ( constant-expression ) [opt]
21436 If no noexcept-specification is present, returns NULL_TREE.
21437 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21438 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21439 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21440 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21441 in which case a boolean condition is returned instead. */
21443 static tree
21444 cp_parser_noexcept_specification_opt (cp_parser* parser,
21445 bool require_constexpr,
21446 bool* consumed_expr,
21447 bool return_cond)
21449 cp_token *token;
21450 const char *saved_message;
21452 /* Peek at the next token. */
21453 token = cp_lexer_peek_token (parser->lexer);
21455 /* Is it a noexcept-specification? */
21456 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21458 tree expr;
21459 cp_lexer_consume_token (parser->lexer);
21461 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21463 cp_lexer_consume_token (parser->lexer);
21465 if (require_constexpr)
21467 /* Types may not be defined in an exception-specification. */
21468 saved_message = parser->type_definition_forbidden_message;
21469 parser->type_definition_forbidden_message
21470 = G_("types may not be defined in an exception-specification");
21472 expr = cp_parser_constant_expression (parser);
21474 /* Restore the saved message. */
21475 parser->type_definition_forbidden_message = saved_message;
21477 else
21479 expr = cp_parser_expression (parser);
21480 *consumed_expr = true;
21483 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21485 else
21487 expr = boolean_true_node;
21488 if (!require_constexpr)
21489 *consumed_expr = false;
21492 /* We cannot build a noexcept-spec right away because this will check
21493 that expr is a constexpr. */
21494 if (!return_cond)
21495 return build_noexcept_spec (expr, tf_warning_or_error);
21496 else
21497 return expr;
21499 else
21500 return NULL_TREE;
21503 /* Parse an (optional) exception-specification.
21505 exception-specification:
21506 throw ( type-id-list [opt] )
21508 Returns a TREE_LIST representing the exception-specification. The
21509 TREE_VALUE of each node is a type. */
21511 static tree
21512 cp_parser_exception_specification_opt (cp_parser* parser)
21514 cp_token *token;
21515 tree type_id_list;
21516 const char *saved_message;
21518 /* Peek at the next token. */
21519 token = cp_lexer_peek_token (parser->lexer);
21521 /* Is it a noexcept-specification? */
21522 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21523 false);
21524 if (type_id_list != NULL_TREE)
21525 return type_id_list;
21527 /* If it's not `throw', then there's no exception-specification. */
21528 if (!cp_parser_is_keyword (token, RID_THROW))
21529 return NULL_TREE;
21531 #if 0
21532 /* Enable this once a lot of code has transitioned to noexcept? */
21533 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21534 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21535 "deprecated in C++0x; use %<noexcept%> instead");
21536 #endif
21538 /* Consume the `throw'. */
21539 cp_lexer_consume_token (parser->lexer);
21541 /* Look for the `('. */
21542 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21544 /* Peek at the next token. */
21545 token = cp_lexer_peek_token (parser->lexer);
21546 /* If it's not a `)', then there is a type-id-list. */
21547 if (token->type != CPP_CLOSE_PAREN)
21549 /* Types may not be defined in an exception-specification. */
21550 saved_message = parser->type_definition_forbidden_message;
21551 parser->type_definition_forbidden_message
21552 = G_("types may not be defined in an exception-specification");
21553 /* Parse the type-id-list. */
21554 type_id_list = cp_parser_type_id_list (parser);
21555 /* Restore the saved message. */
21556 parser->type_definition_forbidden_message = saved_message;
21558 else
21559 type_id_list = empty_except_spec;
21561 /* Look for the `)'. */
21562 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21564 return type_id_list;
21567 /* Parse an (optional) type-id-list.
21569 type-id-list:
21570 type-id ... [opt]
21571 type-id-list , type-id ... [opt]
21573 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21574 in the order that the types were presented. */
21576 static tree
21577 cp_parser_type_id_list (cp_parser* parser)
21579 tree types = NULL_TREE;
21581 while (true)
21583 cp_token *token;
21584 tree type;
21586 /* Get the next type-id. */
21587 type = cp_parser_type_id (parser);
21588 /* Parse the optional ellipsis. */
21589 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21591 /* Consume the `...'. */
21592 cp_lexer_consume_token (parser->lexer);
21594 /* Turn the type into a pack expansion expression. */
21595 type = make_pack_expansion (type);
21597 /* Add it to the list. */
21598 types = add_exception_specifier (types, type, /*complain=*/1);
21599 /* Peek at the next token. */
21600 token = cp_lexer_peek_token (parser->lexer);
21601 /* If it is not a `,', we are done. */
21602 if (token->type != CPP_COMMA)
21603 break;
21604 /* Consume the `,'. */
21605 cp_lexer_consume_token (parser->lexer);
21608 return nreverse (types);
21611 /* Parse a try-block.
21613 try-block:
21614 try compound-statement handler-seq */
21616 static tree
21617 cp_parser_try_block (cp_parser* parser)
21619 tree try_block;
21621 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21622 if (parser->in_function_body
21623 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21624 error ("%<try%> in %<constexpr%> function");
21626 try_block = begin_try_block ();
21627 cp_parser_compound_statement (parser, NULL, true, false);
21628 finish_try_block (try_block);
21629 cp_parser_handler_seq (parser);
21630 finish_handler_sequence (try_block);
21632 return try_block;
21635 /* Parse a function-try-block.
21637 function-try-block:
21638 try ctor-initializer [opt] function-body handler-seq */
21640 static bool
21641 cp_parser_function_try_block (cp_parser* parser)
21643 tree compound_stmt;
21644 tree try_block;
21645 bool ctor_initializer_p;
21647 /* Look for the `try' keyword. */
21648 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21649 return false;
21650 /* Let the rest of the front end know where we are. */
21651 try_block = begin_function_try_block (&compound_stmt);
21652 /* Parse the function-body. */
21653 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21654 (parser, /*in_function_try_block=*/true);
21655 /* We're done with the `try' part. */
21656 finish_function_try_block (try_block);
21657 /* Parse the handlers. */
21658 cp_parser_handler_seq (parser);
21659 /* We're done with the handlers. */
21660 finish_function_handler_sequence (try_block, compound_stmt);
21662 return ctor_initializer_p;
21665 /* Parse a handler-seq.
21667 handler-seq:
21668 handler handler-seq [opt] */
21670 static void
21671 cp_parser_handler_seq (cp_parser* parser)
21673 while (true)
21675 cp_token *token;
21677 /* Parse the handler. */
21678 cp_parser_handler (parser);
21679 /* Peek at the next token. */
21680 token = cp_lexer_peek_token (parser->lexer);
21681 /* If it's not `catch' then there are no more handlers. */
21682 if (!cp_parser_is_keyword (token, RID_CATCH))
21683 break;
21687 /* Parse a handler.
21689 handler:
21690 catch ( exception-declaration ) compound-statement */
21692 static void
21693 cp_parser_handler (cp_parser* parser)
21695 tree handler;
21696 tree declaration;
21698 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21699 handler = begin_handler ();
21700 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21701 declaration = cp_parser_exception_declaration (parser);
21702 finish_handler_parms (declaration, handler);
21703 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21704 cp_parser_compound_statement (parser, NULL, false, false);
21705 finish_handler (handler);
21708 /* Parse an exception-declaration.
21710 exception-declaration:
21711 type-specifier-seq declarator
21712 type-specifier-seq abstract-declarator
21713 type-specifier-seq
21716 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21717 ellipsis variant is used. */
21719 static tree
21720 cp_parser_exception_declaration (cp_parser* parser)
21722 cp_decl_specifier_seq type_specifiers;
21723 cp_declarator *declarator;
21724 const char *saved_message;
21726 /* If it's an ellipsis, it's easy to handle. */
21727 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21729 /* Consume the `...' token. */
21730 cp_lexer_consume_token (parser->lexer);
21731 return NULL_TREE;
21734 /* Types may not be defined in exception-declarations. */
21735 saved_message = parser->type_definition_forbidden_message;
21736 parser->type_definition_forbidden_message
21737 = G_("types may not be defined in exception-declarations");
21739 /* Parse the type-specifier-seq. */
21740 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21741 /*is_trailing_return=*/false,
21742 &type_specifiers);
21743 /* If it's a `)', then there is no declarator. */
21744 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21745 declarator = NULL;
21746 else
21747 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21748 /*ctor_dtor_or_conv_p=*/NULL,
21749 /*parenthesized_p=*/NULL,
21750 /*member_p=*/false,
21751 /*friend_p=*/false);
21753 /* Restore the saved message. */
21754 parser->type_definition_forbidden_message = saved_message;
21756 if (!type_specifiers.any_specifiers_p)
21757 return error_mark_node;
21759 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21762 /* Parse a throw-expression.
21764 throw-expression:
21765 throw assignment-expression [opt]
21767 Returns a THROW_EXPR representing the throw-expression. */
21769 static tree
21770 cp_parser_throw_expression (cp_parser* parser)
21772 tree expression;
21773 cp_token* token;
21775 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21776 token = cp_lexer_peek_token (parser->lexer);
21777 /* Figure out whether or not there is an assignment-expression
21778 following the "throw" keyword. */
21779 if (token->type == CPP_COMMA
21780 || token->type == CPP_SEMICOLON
21781 || token->type == CPP_CLOSE_PAREN
21782 || token->type == CPP_CLOSE_SQUARE
21783 || token->type == CPP_CLOSE_BRACE
21784 || token->type == CPP_COLON)
21785 expression = NULL_TREE;
21786 else
21787 expression = cp_parser_assignment_expression (parser);
21789 return build_throw (expression);
21792 /* GNU Extensions */
21794 /* Parse an (optional) asm-specification.
21796 asm-specification:
21797 asm ( string-literal )
21799 If the asm-specification is present, returns a STRING_CST
21800 corresponding to the string-literal. Otherwise, returns
21801 NULL_TREE. */
21803 static tree
21804 cp_parser_asm_specification_opt (cp_parser* parser)
21806 cp_token *token;
21807 tree asm_specification;
21809 /* Peek at the next token. */
21810 token = cp_lexer_peek_token (parser->lexer);
21811 /* If the next token isn't the `asm' keyword, then there's no
21812 asm-specification. */
21813 if (!cp_parser_is_keyword (token, RID_ASM))
21814 return NULL_TREE;
21816 /* Consume the `asm' token. */
21817 cp_lexer_consume_token (parser->lexer);
21818 /* Look for the `('. */
21819 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21821 /* Look for the string-literal. */
21822 asm_specification = cp_parser_string_literal (parser, false, false);
21824 /* Look for the `)'. */
21825 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21827 return asm_specification;
21830 /* Parse an asm-operand-list.
21832 asm-operand-list:
21833 asm-operand
21834 asm-operand-list , asm-operand
21836 asm-operand:
21837 string-literal ( expression )
21838 [ string-literal ] string-literal ( expression )
21840 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21841 each node is the expression. The TREE_PURPOSE is itself a
21842 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21843 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21844 is a STRING_CST for the string literal before the parenthesis. Returns
21845 ERROR_MARK_NODE if any of the operands are invalid. */
21847 static tree
21848 cp_parser_asm_operand_list (cp_parser* parser)
21850 tree asm_operands = NULL_TREE;
21851 bool invalid_operands = false;
21853 while (true)
21855 tree string_literal;
21856 tree expression;
21857 tree name;
21859 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21861 /* Consume the `[' token. */
21862 cp_lexer_consume_token (parser->lexer);
21863 /* Read the operand name. */
21864 name = cp_parser_identifier (parser);
21865 if (name != error_mark_node)
21866 name = build_string (IDENTIFIER_LENGTH (name),
21867 IDENTIFIER_POINTER (name));
21868 /* Look for the closing `]'. */
21869 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21871 else
21872 name = NULL_TREE;
21873 /* Look for the string-literal. */
21874 string_literal = cp_parser_string_literal (parser, false, false);
21876 /* Look for the `('. */
21877 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21878 /* Parse the expression. */
21879 expression = cp_parser_expression (parser);
21880 /* Look for the `)'. */
21881 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21883 if (name == error_mark_node
21884 || string_literal == error_mark_node
21885 || expression == error_mark_node)
21886 invalid_operands = true;
21888 /* Add this operand to the list. */
21889 asm_operands = tree_cons (build_tree_list (name, string_literal),
21890 expression,
21891 asm_operands);
21892 /* If the next token is not a `,', there are no more
21893 operands. */
21894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21895 break;
21896 /* Consume the `,'. */
21897 cp_lexer_consume_token (parser->lexer);
21900 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21903 /* Parse an asm-clobber-list.
21905 asm-clobber-list:
21906 string-literal
21907 asm-clobber-list , string-literal
21909 Returns a TREE_LIST, indicating the clobbers in the order that they
21910 appeared. The TREE_VALUE of each node is a STRING_CST. */
21912 static tree
21913 cp_parser_asm_clobber_list (cp_parser* parser)
21915 tree clobbers = NULL_TREE;
21917 while (true)
21919 tree string_literal;
21921 /* Look for the string literal. */
21922 string_literal = cp_parser_string_literal (parser, false, false);
21923 /* Add it to the list. */
21924 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21925 /* If the next token is not a `,', then the list is
21926 complete. */
21927 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21928 break;
21929 /* Consume the `,' token. */
21930 cp_lexer_consume_token (parser->lexer);
21933 return clobbers;
21936 /* Parse an asm-label-list.
21938 asm-label-list:
21939 identifier
21940 asm-label-list , identifier
21942 Returns a TREE_LIST, indicating the labels in the order that they
21943 appeared. The TREE_VALUE of each node is a label. */
21945 static tree
21946 cp_parser_asm_label_list (cp_parser* parser)
21948 tree labels = NULL_TREE;
21950 while (true)
21952 tree identifier, label, name;
21954 /* Look for the identifier. */
21955 identifier = cp_parser_identifier (parser);
21956 if (!error_operand_p (identifier))
21958 label = lookup_label (identifier);
21959 if (TREE_CODE (label) == LABEL_DECL)
21961 TREE_USED (label) = 1;
21962 check_goto (label);
21963 name = build_string (IDENTIFIER_LENGTH (identifier),
21964 IDENTIFIER_POINTER (identifier));
21965 labels = tree_cons (name, label, labels);
21968 /* If the next token is not a `,', then the list is
21969 complete. */
21970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21971 break;
21972 /* Consume the `,' token. */
21973 cp_lexer_consume_token (parser->lexer);
21976 return nreverse (labels);
21979 /* Return TRUE iff the next tokens in the stream are possibly the
21980 beginning of a GNU extension attribute. */
21982 static bool
21983 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21985 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21988 /* Return TRUE iff the next tokens in the stream are possibly the
21989 beginning of a standard C++-11 attribute specifier. */
21991 static bool
21992 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21994 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21997 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21998 beginning of a standard C++-11 attribute specifier. */
22000 static bool
22001 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22003 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22005 return (cxx_dialect >= cxx11
22006 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22007 || (token->type == CPP_OPEN_SQUARE
22008 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22009 && token->type == CPP_OPEN_SQUARE)));
22012 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22013 beginning of a GNU extension attribute. */
22015 static bool
22016 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22018 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22020 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22023 /* Return true iff the next tokens can be the beginning of either a
22024 GNU attribute list, or a standard C++11 attribute sequence. */
22026 static bool
22027 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22029 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22030 || cp_next_tokens_can_be_std_attribute_p (parser));
22033 /* Return true iff the next Nth tokens can be the beginning of either
22034 a GNU attribute list, or a standard C++11 attribute sequence. */
22036 static bool
22037 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22039 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22040 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22043 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22044 of GNU attributes, or return NULL. */
22046 static tree
22047 cp_parser_attributes_opt (cp_parser *parser)
22049 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22050 return cp_parser_gnu_attributes_opt (parser);
22051 return cp_parser_std_attribute_spec_seq (parser);
22054 #define CILK_SIMD_FN_CLAUSE_MASK \
22055 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
22056 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
22057 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
22058 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
22059 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22061 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
22062 vector [(<clauses>)] */
22064 static void
22065 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22067 bool first_p = parser->cilk_simd_fn_info == NULL;
22068 cp_token *token = v_token;
22069 if (first_p)
22071 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22072 parser->cilk_simd_fn_info->error_seen = false;
22073 parser->cilk_simd_fn_info->fndecl_seen = false;
22074 parser->cilk_simd_fn_info->tokens = vNULL;
22076 int paren_scope = 0;
22077 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22079 cp_lexer_consume_token (parser->lexer);
22080 v_token = cp_lexer_peek_token (parser->lexer);
22081 paren_scope++;
22083 while (paren_scope > 0)
22085 token = cp_lexer_peek_token (parser->lexer);
22086 if (token->type == CPP_OPEN_PAREN)
22087 paren_scope++;
22088 else if (token->type == CPP_CLOSE_PAREN)
22089 paren_scope--;
22090 /* Do not push the last ')' */
22091 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22092 cp_lexer_consume_token (parser->lexer);
22095 token->type = CPP_PRAGMA_EOL;
22096 parser->lexer->next_token = token;
22097 cp_lexer_consume_token (parser->lexer);
22099 struct cp_token_cache *cp
22100 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22101 parser->cilk_simd_fn_info->tokens.safe_push (cp);
22104 /* Parse an (optional) series of attributes.
22106 attributes:
22107 attributes attribute
22109 attribute:
22110 __attribute__ (( attribute-list [opt] ))
22112 The return value is as for cp_parser_gnu_attribute_list. */
22114 static tree
22115 cp_parser_gnu_attributes_opt (cp_parser* parser)
22117 tree attributes = NULL_TREE;
22119 while (true)
22121 cp_token *token;
22122 tree attribute_list;
22123 bool ok = true;
22125 /* Peek at the next token. */
22126 token = cp_lexer_peek_token (parser->lexer);
22127 /* If it's not `__attribute__', then we're done. */
22128 if (token->keyword != RID_ATTRIBUTE)
22129 break;
22131 /* Consume the `__attribute__' keyword. */
22132 cp_lexer_consume_token (parser->lexer);
22133 /* Look for the two `(' tokens. */
22134 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22135 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22137 /* Peek at the next token. */
22138 token = cp_lexer_peek_token (parser->lexer);
22139 if (token->type != CPP_CLOSE_PAREN)
22140 /* Parse the attribute-list. */
22141 attribute_list = cp_parser_gnu_attribute_list (parser);
22142 else
22143 /* If the next token is a `)', then there is no attribute
22144 list. */
22145 attribute_list = NULL;
22147 /* Look for the two `)' tokens. */
22148 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22149 ok = false;
22150 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22151 ok = false;
22152 if (!ok)
22153 cp_parser_skip_to_end_of_statement (parser);
22155 /* Add these new attributes to the list. */
22156 attributes = chainon (attributes, attribute_list);
22159 return attributes;
22162 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22163 "__vector" or "__vector__." */
22165 static inline bool
22166 is_cilkplus_vector_p (tree name)
22168 if (flag_cilkplus && is_attribute_p ("vector", name))
22169 return true;
22170 return false;
22173 /* Parse a GNU attribute-list.
22175 attribute-list:
22176 attribute
22177 attribute-list , attribute
22179 attribute:
22180 identifier
22181 identifier ( identifier )
22182 identifier ( identifier , expression-list )
22183 identifier ( expression-list )
22185 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
22186 to an attribute. The TREE_PURPOSE of each node is the identifier
22187 indicating which attribute is in use. The TREE_VALUE represents
22188 the arguments, if any. */
22190 static tree
22191 cp_parser_gnu_attribute_list (cp_parser* parser)
22193 tree attribute_list = NULL_TREE;
22194 bool save_translate_strings_p = parser->translate_strings_p;
22196 parser->translate_strings_p = false;
22197 while (true)
22199 cp_token *token;
22200 tree identifier;
22201 tree attribute;
22203 /* Look for the identifier. We also allow keywords here; for
22204 example `__attribute__ ((const))' is legal. */
22205 token = cp_lexer_peek_token (parser->lexer);
22206 if (token->type == CPP_NAME
22207 || token->type == CPP_KEYWORD)
22209 tree arguments = NULL_TREE;
22211 /* Consume the token, but save it since we need it for the
22212 SIMD enabled function parsing. */
22213 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22215 /* Save away the identifier that indicates which attribute
22216 this is. */
22217 identifier = (token->type == CPP_KEYWORD)
22218 /* For keywords, use the canonical spelling, not the
22219 parsed identifier. */
22220 ? ridpointers[(int) token->keyword]
22221 : id_token->u.value;
22223 attribute = build_tree_list (identifier, NULL_TREE);
22225 /* Peek at the next token. */
22226 token = cp_lexer_peek_token (parser->lexer);
22227 /* If it's an `(', then parse the attribute arguments. */
22228 if (token->type == CPP_OPEN_PAREN)
22230 vec<tree, va_gc> *vec;
22231 int attr_flag = (attribute_takes_identifier_p (identifier)
22232 ? id_attr : normal_attr);
22233 if (is_cilkplus_vector_p (identifier))
22235 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22236 continue;
22238 else
22239 vec = cp_parser_parenthesized_expression_list
22240 (parser, attr_flag, /*cast_p=*/false,
22241 /*allow_expansion_p=*/false,
22242 /*non_constant_p=*/NULL);
22243 if (vec == NULL)
22244 arguments = error_mark_node;
22245 else
22247 arguments = build_tree_list_vec (vec);
22248 release_tree_vector (vec);
22250 /* Save the arguments away. */
22251 TREE_VALUE (attribute) = arguments;
22253 else if (is_cilkplus_vector_p (identifier))
22255 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22256 continue;
22259 if (arguments != error_mark_node)
22261 /* Add this attribute to the list. */
22262 TREE_CHAIN (attribute) = attribute_list;
22263 attribute_list = attribute;
22266 token = cp_lexer_peek_token (parser->lexer);
22268 /* Now, look for more attributes. If the next token isn't a
22269 `,', we're done. */
22270 if (token->type != CPP_COMMA)
22271 break;
22273 /* Consume the comma and keep going. */
22274 cp_lexer_consume_token (parser->lexer);
22276 parser->translate_strings_p = save_translate_strings_p;
22278 /* We built up the list in reverse order. */
22279 return nreverse (attribute_list);
22282 /* Parse a standard C++11 attribute.
22284 The returned representation is a TREE_LIST which TREE_PURPOSE is
22285 the scoped name of the attribute, and the TREE_VALUE is its
22286 arguments list.
22288 Note that the scoped name of the attribute is itself a TREE_LIST
22289 which TREE_PURPOSE is the namespace of the attribute, and
22290 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22291 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22292 and which TREE_PURPOSE is directly the attribute name.
22294 Clients of the attribute code should use get_attribute_namespace
22295 and get_attribute_name to get the actual namespace and name of
22296 attributes, regardless of their being GNU or C++11 attributes.
22298 attribute:
22299 attribute-token attribute-argument-clause [opt]
22301 attribute-token:
22302 identifier
22303 attribute-scoped-token
22305 attribute-scoped-token:
22306 attribute-namespace :: identifier
22308 attribute-namespace:
22309 identifier
22311 attribute-argument-clause:
22312 ( balanced-token-seq )
22314 balanced-token-seq:
22315 balanced-token [opt]
22316 balanced-token-seq balanced-token
22318 balanced-token:
22319 ( balanced-token-seq )
22320 [ balanced-token-seq ]
22321 { balanced-token-seq }. */
22323 static tree
22324 cp_parser_std_attribute (cp_parser *parser)
22326 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22327 cp_token *token;
22329 /* First, parse name of the the attribute, a.k.a
22330 attribute-token. */
22332 token = cp_lexer_peek_token (parser->lexer);
22333 if (token->type == CPP_NAME)
22334 attr_id = token->u.value;
22335 else if (token->type == CPP_KEYWORD)
22336 attr_id = ridpointers[(int) token->keyword];
22337 else if (token->flags & NAMED_OP)
22338 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22340 if (attr_id == NULL_TREE)
22341 return NULL_TREE;
22343 cp_lexer_consume_token (parser->lexer);
22345 token = cp_lexer_peek_token (parser->lexer);
22346 if (token->type == CPP_SCOPE)
22348 /* We are seeing a scoped attribute token. */
22350 cp_lexer_consume_token (parser->lexer);
22351 attr_ns = attr_id;
22353 token = cp_lexer_consume_token (parser->lexer);
22354 if (token->type == CPP_NAME)
22355 attr_id = token->u.value;
22356 else if (token->type == CPP_KEYWORD)
22357 attr_id = ridpointers[(int) token->keyword];
22358 else
22360 error_at (token->location,
22361 "expected an identifier for the attribute name");
22362 return error_mark_node;
22364 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22365 NULL_TREE);
22366 token = cp_lexer_peek_token (parser->lexer);
22368 else
22370 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22371 NULL_TREE);
22372 /* C++11 noreturn attribute is equivalent to GNU's. */
22373 if (is_attribute_p ("noreturn", attr_id))
22374 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22375 /* C++14 deprecated attribute is equivalent to GNU's. */
22376 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22378 if (cxx_dialect == cxx11)
22379 pedwarn (token->location, OPT_Wpedantic,
22380 "%<deprecated%> is a C++14 feature;"
22381 " use %<gnu::deprecated%>");
22382 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22386 /* Now parse the optional argument clause of the attribute. */
22388 if (token->type != CPP_OPEN_PAREN)
22389 return attribute;
22392 vec<tree, va_gc> *vec;
22393 int attr_flag = normal_attr;
22395 if (attr_ns == get_identifier ("gnu")
22396 && attribute_takes_identifier_p (attr_id))
22397 /* A GNU attribute that takes an identifier in parameter. */
22398 attr_flag = id_attr;
22400 vec = cp_parser_parenthesized_expression_list
22401 (parser, attr_flag, /*cast_p=*/false,
22402 /*allow_expansion_p=*/true,
22403 /*non_constant_p=*/NULL);
22404 if (vec == NULL)
22405 arguments = error_mark_node;
22406 else
22408 arguments = build_tree_list_vec (vec);
22409 release_tree_vector (vec);
22412 if (arguments == error_mark_node)
22413 attribute = error_mark_node;
22414 else
22415 TREE_VALUE (attribute) = arguments;
22418 return attribute;
22421 /* Parse a list of standard C++-11 attributes.
22423 attribute-list:
22424 attribute [opt]
22425 attribute-list , attribute[opt]
22426 attribute ...
22427 attribute-list , attribute ...
22430 static tree
22431 cp_parser_std_attribute_list (cp_parser *parser)
22433 tree attributes = NULL_TREE, attribute = NULL_TREE;
22434 cp_token *token = NULL;
22436 while (true)
22438 attribute = cp_parser_std_attribute (parser);
22439 if (attribute == error_mark_node)
22440 break;
22441 if (attribute != NULL_TREE)
22443 TREE_CHAIN (attribute) = attributes;
22444 attributes = attribute;
22446 token = cp_lexer_peek_token (parser->lexer);
22447 if (token->type != CPP_COMMA)
22448 break;
22449 cp_lexer_consume_token (parser->lexer);
22451 attributes = nreverse (attributes);
22452 return attributes;
22455 /* Parse a standard C++-11 attribute specifier.
22457 attribute-specifier:
22458 [ [ attribute-list ] ]
22459 alignment-specifier
22461 alignment-specifier:
22462 alignas ( type-id ... [opt] )
22463 alignas ( alignment-expression ... [opt] ). */
22465 static tree
22466 cp_parser_std_attribute_spec (cp_parser *parser)
22468 tree attributes = NULL_TREE;
22469 cp_token *token = cp_lexer_peek_token (parser->lexer);
22471 if (token->type == CPP_OPEN_SQUARE
22472 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22474 cp_lexer_consume_token (parser->lexer);
22475 cp_lexer_consume_token (parser->lexer);
22477 attributes = cp_parser_std_attribute_list (parser);
22479 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22480 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22481 cp_parser_skip_to_end_of_statement (parser);
22482 else
22483 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22484 when we are sure that we have actually parsed them. */
22485 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22487 else
22489 tree alignas_expr;
22491 /* Look for an alignment-specifier. */
22493 token = cp_lexer_peek_token (parser->lexer);
22495 if (token->type != CPP_KEYWORD
22496 || token->keyword != RID_ALIGNAS)
22497 return NULL_TREE;
22499 cp_lexer_consume_token (parser->lexer);
22500 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22502 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22504 cp_parser_error (parser, "expected %<(%>");
22505 return error_mark_node;
22508 cp_parser_parse_tentatively (parser);
22509 alignas_expr = cp_parser_type_id (parser);
22511 if (!cp_parser_parse_definitely (parser))
22513 gcc_assert (alignas_expr == error_mark_node
22514 || alignas_expr == NULL_TREE);
22516 alignas_expr =
22517 cp_parser_assignment_expression (parser);
22518 if (alignas_expr == error_mark_node)
22519 cp_parser_skip_to_end_of_statement (parser);
22520 if (alignas_expr == NULL_TREE
22521 || alignas_expr == error_mark_node)
22522 return alignas_expr;
22525 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22527 cp_parser_error (parser, "expected %<)%>");
22528 return error_mark_node;
22531 alignas_expr = cxx_alignas_expr (alignas_expr);
22533 /* Build the C++-11 representation of an 'aligned'
22534 attribute. */
22535 attributes =
22536 build_tree_list (build_tree_list (get_identifier ("gnu"),
22537 get_identifier ("aligned")),
22538 build_tree_list (NULL_TREE, alignas_expr));
22541 return attributes;
22544 /* Parse a standard C++-11 attribute-specifier-seq.
22546 attribute-specifier-seq:
22547 attribute-specifier-seq [opt] attribute-specifier
22550 static tree
22551 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22553 tree attr_specs = NULL;
22555 while (true)
22557 tree attr_spec = cp_parser_std_attribute_spec (parser);
22558 if (attr_spec == NULL_TREE)
22559 break;
22560 if (attr_spec == error_mark_node)
22561 return error_mark_node;
22563 TREE_CHAIN (attr_spec) = attr_specs;
22564 attr_specs = attr_spec;
22567 attr_specs = nreverse (attr_specs);
22568 return attr_specs;
22571 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22572 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22573 current value of the PEDANTIC flag, regardless of whether or not
22574 the `__extension__' keyword is present. The caller is responsible
22575 for restoring the value of the PEDANTIC flag. */
22577 static bool
22578 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22580 /* Save the old value of the PEDANTIC flag. */
22581 *saved_pedantic = pedantic;
22583 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22585 /* Consume the `__extension__' token. */
22586 cp_lexer_consume_token (parser->lexer);
22587 /* We're not being pedantic while the `__extension__' keyword is
22588 in effect. */
22589 pedantic = 0;
22591 return true;
22594 return false;
22597 /* Parse a label declaration.
22599 label-declaration:
22600 __label__ label-declarator-seq ;
22602 label-declarator-seq:
22603 identifier , label-declarator-seq
22604 identifier */
22606 static void
22607 cp_parser_label_declaration (cp_parser* parser)
22609 /* Look for the `__label__' keyword. */
22610 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22612 while (true)
22614 tree identifier;
22616 /* Look for an identifier. */
22617 identifier = cp_parser_identifier (parser);
22618 /* If we failed, stop. */
22619 if (identifier == error_mark_node)
22620 break;
22621 /* Declare it as a label. */
22622 finish_label_decl (identifier);
22623 /* If the next token is a `;', stop. */
22624 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22625 break;
22626 /* Look for the `,' separating the label declarations. */
22627 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22630 /* Look for the final `;'. */
22631 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22634 /* Support Functions */
22636 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22637 NAME should have one of the representations used for an
22638 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22639 is returned. If PARSER->SCOPE is a dependent type, then a
22640 SCOPE_REF is returned.
22642 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22643 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22644 was formed. Abstractly, such entities should not be passed to this
22645 function, because they do not need to be looked up, but it is
22646 simpler to check for this special case here, rather than at the
22647 call-sites.
22649 In cases not explicitly covered above, this function returns a
22650 DECL, OVERLOAD, or baselink representing the result of the lookup.
22651 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22652 is returned.
22654 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22655 (e.g., "struct") that was used. In that case bindings that do not
22656 refer to types are ignored.
22658 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22659 ignored.
22661 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22662 are ignored.
22664 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22665 types.
22667 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22668 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22669 NULL_TREE otherwise. */
22671 static tree
22672 cp_parser_lookup_name (cp_parser *parser, tree name,
22673 enum tag_types tag_type,
22674 bool is_template,
22675 bool is_namespace,
22676 bool check_dependency,
22677 tree *ambiguous_decls,
22678 location_t name_location)
22680 tree decl;
22681 tree object_type = parser->context->object_type;
22683 /* Assume that the lookup will be unambiguous. */
22684 if (ambiguous_decls)
22685 *ambiguous_decls = NULL_TREE;
22687 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22688 no longer valid. Note that if we are parsing tentatively, and
22689 the parse fails, OBJECT_TYPE will be automatically restored. */
22690 parser->context->object_type = NULL_TREE;
22692 if (name == error_mark_node)
22693 return error_mark_node;
22695 /* A template-id has already been resolved; there is no lookup to
22696 do. */
22697 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22698 return name;
22699 if (BASELINK_P (name))
22701 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22702 == TEMPLATE_ID_EXPR);
22703 return name;
22706 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22707 it should already have been checked to make sure that the name
22708 used matches the type being destroyed. */
22709 if (TREE_CODE (name) == BIT_NOT_EXPR)
22711 tree type;
22713 /* Figure out to which type this destructor applies. */
22714 if (parser->scope)
22715 type = parser->scope;
22716 else if (object_type)
22717 type = object_type;
22718 else
22719 type = current_class_type;
22720 /* If that's not a class type, there is no destructor. */
22721 if (!type || !CLASS_TYPE_P (type))
22722 return error_mark_node;
22723 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22724 lazily_declare_fn (sfk_destructor, type);
22725 if (!CLASSTYPE_DESTRUCTORS (type))
22726 return error_mark_node;
22727 /* If it was a class type, return the destructor. */
22728 return CLASSTYPE_DESTRUCTORS (type);
22731 /* By this point, the NAME should be an ordinary identifier. If
22732 the id-expression was a qualified name, the qualifying scope is
22733 stored in PARSER->SCOPE at this point. */
22734 gcc_assert (identifier_p (name));
22736 /* Perform the lookup. */
22737 if (parser->scope)
22739 bool dependent_p;
22741 if (parser->scope == error_mark_node)
22742 return error_mark_node;
22744 /* If the SCOPE is dependent, the lookup must be deferred until
22745 the template is instantiated -- unless we are explicitly
22746 looking up names in uninstantiated templates. Even then, we
22747 cannot look up the name if the scope is not a class type; it
22748 might, for example, be a template type parameter. */
22749 dependent_p = (TYPE_P (parser->scope)
22750 && dependent_scope_p (parser->scope));
22751 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22752 && dependent_p)
22753 /* Defer lookup. */
22754 decl = error_mark_node;
22755 else
22757 tree pushed_scope = NULL_TREE;
22759 /* If PARSER->SCOPE is a dependent type, then it must be a
22760 class type, and we must not be checking dependencies;
22761 otherwise, we would have processed this lookup above. So
22762 that PARSER->SCOPE is not considered a dependent base by
22763 lookup_member, we must enter the scope here. */
22764 if (dependent_p)
22765 pushed_scope = push_scope (parser->scope);
22767 /* If the PARSER->SCOPE is a template specialization, it
22768 may be instantiated during name lookup. In that case,
22769 errors may be issued. Even if we rollback the current
22770 tentative parse, those errors are valid. */
22771 decl = lookup_qualified_name (parser->scope, name,
22772 tag_type != none_type,
22773 /*complain=*/true);
22775 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22776 lookup result and the nested-name-specifier nominates a class C:
22777 * if the name specified after the nested-name-specifier, when
22778 looked up in C, is the injected-class-name of C (Clause 9), or
22779 * if the name specified after the nested-name-specifier is the
22780 same as the identifier or the simple-template-id's template-
22781 name in the last component of the nested-name-specifier,
22782 the name is instead considered to name the constructor of
22783 class C. [ Note: for example, the constructor is not an
22784 acceptable lookup result in an elaborated-type-specifier so
22785 the constructor would not be used in place of the
22786 injected-class-name. --end note ] Such a constructor name
22787 shall be used only in the declarator-id of a declaration that
22788 names a constructor or in a using-declaration. */
22789 if (tag_type == none_type
22790 && DECL_SELF_REFERENCE_P (decl)
22791 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22792 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22793 tag_type != none_type,
22794 /*complain=*/true);
22796 /* If we have a single function from a using decl, pull it out. */
22797 if (TREE_CODE (decl) == OVERLOAD
22798 && !really_overloaded_fn (decl))
22799 decl = OVL_FUNCTION (decl);
22801 if (pushed_scope)
22802 pop_scope (pushed_scope);
22805 /* If the scope is a dependent type and either we deferred lookup or
22806 we did lookup but didn't find the name, rememeber the name. */
22807 if (decl == error_mark_node && TYPE_P (parser->scope)
22808 && dependent_type_p (parser->scope))
22810 if (tag_type)
22812 tree type;
22814 /* The resolution to Core Issue 180 says that `struct
22815 A::B' should be considered a type-name, even if `A'
22816 is dependent. */
22817 type = make_typename_type (parser->scope, name, tag_type,
22818 /*complain=*/tf_error);
22819 if (type != error_mark_node)
22820 decl = TYPE_NAME (type);
22822 else if (is_template
22823 && (cp_parser_next_token_ends_template_argument_p (parser)
22824 || cp_lexer_next_token_is (parser->lexer,
22825 CPP_CLOSE_PAREN)))
22826 decl = make_unbound_class_template (parser->scope,
22827 name, NULL_TREE,
22828 /*complain=*/tf_error);
22829 else
22830 decl = build_qualified_name (/*type=*/NULL_TREE,
22831 parser->scope, name,
22832 is_template);
22834 parser->qualifying_scope = parser->scope;
22835 parser->object_scope = NULL_TREE;
22837 else if (object_type)
22839 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22840 OBJECT_TYPE is not a class. */
22841 if (CLASS_TYPE_P (object_type))
22842 /* If the OBJECT_TYPE is a template specialization, it may
22843 be instantiated during name lookup. In that case, errors
22844 may be issued. Even if we rollback the current tentative
22845 parse, those errors are valid. */
22846 decl = lookup_member (object_type,
22847 name,
22848 /*protect=*/0,
22849 tag_type != none_type,
22850 tf_warning_or_error);
22851 else
22852 decl = NULL_TREE;
22854 if (!decl)
22855 /* Look it up in the enclosing context. */
22856 decl = lookup_name_real (name, tag_type != none_type,
22857 /*nonclass=*/0,
22858 /*block_p=*/true, is_namespace, 0);
22859 parser->object_scope = object_type;
22860 parser->qualifying_scope = NULL_TREE;
22862 else
22864 decl = lookup_name_real (name, tag_type != none_type,
22865 /*nonclass=*/0,
22866 /*block_p=*/true, is_namespace, 0);
22867 parser->qualifying_scope = NULL_TREE;
22868 parser->object_scope = NULL_TREE;
22871 /* If the lookup failed, let our caller know. */
22872 if (!decl || decl == error_mark_node)
22873 return error_mark_node;
22875 /* Pull out the template from an injected-class-name (or multiple). */
22876 if (is_template)
22877 decl = maybe_get_template_decl_from_type_decl (decl);
22879 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22880 if (TREE_CODE (decl) == TREE_LIST)
22882 if (ambiguous_decls)
22883 *ambiguous_decls = decl;
22884 /* The error message we have to print is too complicated for
22885 cp_parser_error, so we incorporate its actions directly. */
22886 if (!cp_parser_simulate_error (parser))
22888 error_at (name_location, "reference to %qD is ambiguous",
22889 name);
22890 print_candidates (decl);
22892 return error_mark_node;
22895 gcc_assert (DECL_P (decl)
22896 || TREE_CODE (decl) == OVERLOAD
22897 || TREE_CODE (decl) == SCOPE_REF
22898 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22899 || BASELINK_P (decl));
22901 /* If we have resolved the name of a member declaration, check to
22902 see if the declaration is accessible. When the name resolves to
22903 set of overloaded functions, accessibility is checked when
22904 overload resolution is done.
22906 During an explicit instantiation, access is not checked at all,
22907 as per [temp.explicit]. */
22908 if (DECL_P (decl))
22909 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22911 maybe_record_typedef_use (decl);
22913 return decl;
22916 /* Like cp_parser_lookup_name, but for use in the typical case where
22917 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22918 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22920 static tree
22921 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22923 return cp_parser_lookup_name (parser, name,
22924 none_type,
22925 /*is_template=*/false,
22926 /*is_namespace=*/false,
22927 /*check_dependency=*/true,
22928 /*ambiguous_decls=*/NULL,
22929 location);
22932 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22933 the current context, return the TYPE_DECL. If TAG_NAME_P is
22934 true, the DECL indicates the class being defined in a class-head,
22935 or declared in an elaborated-type-specifier.
22937 Otherwise, return DECL. */
22939 static tree
22940 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22942 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22943 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22945 struct A {
22946 template <typename T> struct B;
22949 template <typename T> struct A::B {};
22951 Similarly, in an elaborated-type-specifier:
22953 namespace N { struct X{}; }
22955 struct A {
22956 template <typename T> friend struct N::X;
22959 However, if the DECL refers to a class type, and we are in
22960 the scope of the class, then the name lookup automatically
22961 finds the TYPE_DECL created by build_self_reference rather
22962 than a TEMPLATE_DECL. For example, in:
22964 template <class T> struct S {
22965 S s;
22968 there is no need to handle such case. */
22970 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22971 return DECL_TEMPLATE_RESULT (decl);
22973 return decl;
22976 /* If too many, or too few, template-parameter lists apply to the
22977 declarator, issue an error message. Returns TRUE if all went well,
22978 and FALSE otherwise. */
22980 static bool
22981 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22982 cp_declarator *declarator,
22983 location_t declarator_location)
22985 switch (declarator->kind)
22987 case cdk_id:
22989 unsigned num_templates = 0;
22990 tree scope = declarator->u.id.qualifying_scope;
22992 if (scope)
22993 num_templates = num_template_headers_for_class (scope);
22994 else if (TREE_CODE (declarator->u.id.unqualified_name)
22995 == TEMPLATE_ID_EXPR)
22996 /* If the DECLARATOR has the form `X<y>' then it uses one
22997 additional level of template parameters. */
22998 ++num_templates;
23000 return cp_parser_check_template_parameters
23001 (parser, num_templates, declarator_location, declarator);
23004 case cdk_function:
23005 case cdk_array:
23006 case cdk_pointer:
23007 case cdk_reference:
23008 case cdk_ptrmem:
23009 return (cp_parser_check_declarator_template_parameters
23010 (parser, declarator->declarator, declarator_location));
23012 case cdk_error:
23013 return true;
23015 default:
23016 gcc_unreachable ();
23018 return false;
23021 /* NUM_TEMPLATES were used in the current declaration. If that is
23022 invalid, return FALSE and issue an error messages. Otherwise,
23023 return TRUE. If DECLARATOR is non-NULL, then we are checking a
23024 declarator and we can print more accurate diagnostics. */
23026 static bool
23027 cp_parser_check_template_parameters (cp_parser* parser,
23028 unsigned num_templates,
23029 location_t location,
23030 cp_declarator *declarator)
23032 /* If there are the same number of template classes and parameter
23033 lists, that's OK. */
23034 if (parser->num_template_parameter_lists == num_templates)
23035 return true;
23036 /* If there are more, but only one more, then we are referring to a
23037 member template. That's OK too. */
23038 if (parser->num_template_parameter_lists == num_templates + 1)
23039 return true;
23040 /* If there are more template classes than parameter lists, we have
23041 something like:
23043 template <class T> void S<T>::R<T>::f (); */
23044 if (parser->num_template_parameter_lists < num_templates)
23046 if (declarator && !current_function_decl)
23047 error_at (location, "specializing member %<%T::%E%> "
23048 "requires %<template<>%> syntax",
23049 declarator->u.id.qualifying_scope,
23050 declarator->u.id.unqualified_name);
23051 else if (declarator)
23052 error_at (location, "invalid declaration of %<%T::%E%>",
23053 declarator->u.id.qualifying_scope,
23054 declarator->u.id.unqualified_name);
23055 else
23056 error_at (location, "too few template-parameter-lists");
23057 return false;
23059 /* Otherwise, there are too many template parameter lists. We have
23060 something like:
23062 template <class T> template <class U> void S::f(); */
23063 error_at (location, "too many template-parameter-lists");
23064 return false;
23067 /* Parse an optional `::' token indicating that the following name is
23068 from the global namespace. If so, PARSER->SCOPE is set to the
23069 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23070 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23071 Returns the new value of PARSER->SCOPE, if the `::' token is
23072 present, and NULL_TREE otherwise. */
23074 static tree
23075 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23077 cp_token *token;
23079 /* Peek at the next token. */
23080 token = cp_lexer_peek_token (parser->lexer);
23081 /* If we're looking at a `::' token then we're starting from the
23082 global namespace, not our current location. */
23083 if (token->type == CPP_SCOPE)
23085 /* Consume the `::' token. */
23086 cp_lexer_consume_token (parser->lexer);
23087 /* Set the SCOPE so that we know where to start the lookup. */
23088 parser->scope = global_namespace;
23089 parser->qualifying_scope = global_namespace;
23090 parser->object_scope = NULL_TREE;
23092 return parser->scope;
23094 else if (!current_scope_valid_p)
23096 parser->scope = NULL_TREE;
23097 parser->qualifying_scope = NULL_TREE;
23098 parser->object_scope = NULL_TREE;
23101 return NULL_TREE;
23104 /* Returns TRUE if the upcoming token sequence is the start of a
23105 constructor declarator. If FRIEND_P is true, the declarator is
23106 preceded by the `friend' specifier. */
23108 static bool
23109 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23111 bool constructor_p;
23112 bool outside_class_specifier_p;
23113 tree nested_name_specifier;
23114 cp_token *next_token;
23116 /* The common case is that this is not a constructor declarator, so
23117 try to avoid doing lots of work if at all possible. It's not
23118 valid declare a constructor at function scope. */
23119 if (parser->in_function_body)
23120 return false;
23121 /* And only certain tokens can begin a constructor declarator. */
23122 next_token = cp_lexer_peek_token (parser->lexer);
23123 if (next_token->type != CPP_NAME
23124 && next_token->type != CPP_SCOPE
23125 && next_token->type != CPP_NESTED_NAME_SPECIFIER
23126 && next_token->type != CPP_TEMPLATE_ID)
23127 return false;
23129 /* Parse tentatively; we are going to roll back all of the tokens
23130 consumed here. */
23131 cp_parser_parse_tentatively (parser);
23132 /* Assume that we are looking at a constructor declarator. */
23133 constructor_p = true;
23135 /* Look for the optional `::' operator. */
23136 cp_parser_global_scope_opt (parser,
23137 /*current_scope_valid_p=*/false);
23138 /* Look for the nested-name-specifier. */
23139 nested_name_specifier
23140 = (cp_parser_nested_name_specifier_opt (parser,
23141 /*typename_keyword_p=*/false,
23142 /*check_dependency_p=*/false,
23143 /*type_p=*/false,
23144 /*is_declaration=*/false));
23146 outside_class_specifier_p = (!at_class_scope_p ()
23147 || !TYPE_BEING_DEFINED (current_class_type)
23148 || friend_p);
23150 /* Outside of a class-specifier, there must be a
23151 nested-name-specifier. */
23152 if (!nested_name_specifier && outside_class_specifier_p)
23153 constructor_p = false;
23154 else if (nested_name_specifier == error_mark_node)
23155 constructor_p = false;
23157 /* If we have a class scope, this is easy; DR 147 says that S::S always
23158 names the constructor, and no other qualified name could. */
23159 if (constructor_p && nested_name_specifier
23160 && CLASS_TYPE_P (nested_name_specifier))
23162 tree id = cp_parser_unqualified_id (parser,
23163 /*template_keyword_p=*/false,
23164 /*check_dependency_p=*/false,
23165 /*declarator_p=*/true,
23166 /*optional_p=*/false);
23167 if (is_overloaded_fn (id))
23168 id = DECL_NAME (get_first_fn (id));
23169 if (!constructor_name_p (id, nested_name_specifier))
23170 constructor_p = false;
23172 /* If we still think that this might be a constructor-declarator,
23173 look for a class-name. */
23174 else if (constructor_p)
23176 /* If we have:
23178 template <typename T> struct S {
23179 S();
23182 we must recognize that the nested `S' names a class. */
23183 tree type_decl;
23184 type_decl = cp_parser_class_name (parser,
23185 /*typename_keyword_p=*/false,
23186 /*template_keyword_p=*/false,
23187 none_type,
23188 /*check_dependency_p=*/false,
23189 /*class_head_p=*/false,
23190 /*is_declaration=*/false);
23191 /* If there was no class-name, then this is not a constructor.
23192 Otherwise, if we are in a class-specifier and we aren't
23193 handling a friend declaration, check that its type matches
23194 current_class_type (c++/38313). Note: error_mark_node
23195 is left alone for error recovery purposes. */
23196 constructor_p = (!cp_parser_error_occurred (parser)
23197 && (outside_class_specifier_p
23198 || type_decl == error_mark_node
23199 || same_type_p (current_class_type,
23200 TREE_TYPE (type_decl))));
23202 /* If we're still considering a constructor, we have to see a `(',
23203 to begin the parameter-declaration-clause, followed by either a
23204 `)', an `...', or a decl-specifier. We need to check for a
23205 type-specifier to avoid being fooled into thinking that:
23207 S (f) (int);
23209 is a constructor. (It is actually a function named `f' that
23210 takes one parameter (of type `int') and returns a value of type
23211 `S'. */
23212 if (constructor_p
23213 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23214 constructor_p = false;
23216 if (constructor_p
23217 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23218 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23219 /* A parameter declaration begins with a decl-specifier,
23220 which is either the "attribute" keyword, a storage class
23221 specifier, or (usually) a type-specifier. */
23222 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23224 tree type;
23225 tree pushed_scope = NULL_TREE;
23226 unsigned saved_num_template_parameter_lists;
23228 /* Names appearing in the type-specifier should be looked up
23229 in the scope of the class. */
23230 if (current_class_type)
23231 type = NULL_TREE;
23232 else
23234 type = TREE_TYPE (type_decl);
23235 if (TREE_CODE (type) == TYPENAME_TYPE)
23237 type = resolve_typename_type (type,
23238 /*only_current_p=*/false);
23239 if (TREE_CODE (type) == TYPENAME_TYPE)
23241 cp_parser_abort_tentative_parse (parser);
23242 return false;
23245 pushed_scope = push_scope (type);
23248 /* Inside the constructor parameter list, surrounding
23249 template-parameter-lists do not apply. */
23250 saved_num_template_parameter_lists
23251 = parser->num_template_parameter_lists;
23252 parser->num_template_parameter_lists = 0;
23254 /* Look for the type-specifier. */
23255 cp_parser_type_specifier (parser,
23256 CP_PARSER_FLAGS_NONE,
23257 /*decl_specs=*/NULL,
23258 /*is_declarator=*/true,
23259 /*declares_class_or_enum=*/NULL,
23260 /*is_cv_qualifier=*/NULL);
23262 parser->num_template_parameter_lists
23263 = saved_num_template_parameter_lists;
23265 /* Leave the scope of the class. */
23266 if (pushed_scope)
23267 pop_scope (pushed_scope);
23269 constructor_p = !cp_parser_error_occurred (parser);
23273 /* We did not really want to consume any tokens. */
23274 cp_parser_abort_tentative_parse (parser);
23276 return constructor_p;
23279 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23280 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23281 they must be performed once we are in the scope of the function.
23283 Returns the function defined. */
23285 static tree
23286 cp_parser_function_definition_from_specifiers_and_declarator
23287 (cp_parser* parser,
23288 cp_decl_specifier_seq *decl_specifiers,
23289 tree attributes,
23290 const cp_declarator *declarator)
23292 tree fn;
23293 bool success_p;
23295 /* Begin the function-definition. */
23296 success_p = start_function (decl_specifiers, declarator, attributes);
23298 /* The things we're about to see are not directly qualified by any
23299 template headers we've seen thus far. */
23300 reset_specialization ();
23302 /* If there were names looked up in the decl-specifier-seq that we
23303 did not check, check them now. We must wait until we are in the
23304 scope of the function to perform the checks, since the function
23305 might be a friend. */
23306 perform_deferred_access_checks (tf_warning_or_error);
23308 if (success_p)
23310 cp_finalize_omp_declare_simd (parser, current_function_decl);
23311 parser->omp_declare_simd = NULL;
23314 if (!success_p)
23316 /* Skip the entire function. */
23317 cp_parser_skip_to_end_of_block_or_statement (parser);
23318 fn = error_mark_node;
23320 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23322 /* Seen already, skip it. An error message has already been output. */
23323 cp_parser_skip_to_end_of_block_or_statement (parser);
23324 fn = current_function_decl;
23325 current_function_decl = NULL_TREE;
23326 /* If this is a function from a class, pop the nested class. */
23327 if (current_class_name)
23328 pop_nested_class ();
23330 else
23332 timevar_id_t tv;
23333 if (DECL_DECLARED_INLINE_P (current_function_decl))
23334 tv = TV_PARSE_INLINE;
23335 else
23336 tv = TV_PARSE_FUNC;
23337 timevar_push (tv);
23338 fn = cp_parser_function_definition_after_declarator (parser,
23339 /*inline_p=*/false);
23340 timevar_pop (tv);
23343 return fn;
23346 /* Parse the part of a function-definition that follows the
23347 declarator. INLINE_P is TRUE iff this function is an inline
23348 function defined within a class-specifier.
23350 Returns the function defined. */
23352 static tree
23353 cp_parser_function_definition_after_declarator (cp_parser* parser,
23354 bool inline_p)
23356 tree fn;
23357 bool ctor_initializer_p = false;
23358 bool saved_in_unbraced_linkage_specification_p;
23359 bool saved_in_function_body;
23360 unsigned saved_num_template_parameter_lists;
23361 cp_token *token;
23362 bool fully_implicit_function_template_p
23363 = parser->fully_implicit_function_template_p;
23364 parser->fully_implicit_function_template_p = false;
23365 tree implicit_template_parms
23366 = parser->implicit_template_parms;
23367 parser->implicit_template_parms = 0;
23368 cp_binding_level* implicit_template_scope
23369 = parser->implicit_template_scope;
23370 parser->implicit_template_scope = 0;
23372 saved_in_function_body = parser->in_function_body;
23373 parser->in_function_body = true;
23374 /* If the next token is `return', then the code may be trying to
23375 make use of the "named return value" extension that G++ used to
23376 support. */
23377 token = cp_lexer_peek_token (parser->lexer);
23378 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23380 /* Consume the `return' keyword. */
23381 cp_lexer_consume_token (parser->lexer);
23382 /* Look for the identifier that indicates what value is to be
23383 returned. */
23384 cp_parser_identifier (parser);
23385 /* Issue an error message. */
23386 error_at (token->location,
23387 "named return values are no longer supported");
23388 /* Skip tokens until we reach the start of the function body. */
23389 while (true)
23391 cp_token *token = cp_lexer_peek_token (parser->lexer);
23392 if (token->type == CPP_OPEN_BRACE
23393 || token->type == CPP_EOF
23394 || token->type == CPP_PRAGMA_EOL)
23395 break;
23396 cp_lexer_consume_token (parser->lexer);
23399 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23400 anything declared inside `f'. */
23401 saved_in_unbraced_linkage_specification_p
23402 = parser->in_unbraced_linkage_specification_p;
23403 parser->in_unbraced_linkage_specification_p = false;
23404 /* Inside the function, surrounding template-parameter-lists do not
23405 apply. */
23406 saved_num_template_parameter_lists
23407 = parser->num_template_parameter_lists;
23408 parser->num_template_parameter_lists = 0;
23410 start_lambda_scope (current_function_decl);
23412 /* If the next token is `try', `__transaction_atomic', or
23413 `__transaction_relaxed`, then we are looking at either function-try-block
23414 or function-transaction-block. Note that all of these include the
23415 function-body. */
23416 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23417 ctor_initializer_p = cp_parser_function_transaction (parser,
23418 RID_TRANSACTION_ATOMIC);
23419 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23420 RID_TRANSACTION_RELAXED))
23421 ctor_initializer_p = cp_parser_function_transaction (parser,
23422 RID_TRANSACTION_RELAXED);
23423 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23424 ctor_initializer_p = cp_parser_function_try_block (parser);
23425 else
23426 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23427 (parser, /*in_function_try_block=*/false);
23429 finish_lambda_scope ();
23431 /* Finish the function. */
23432 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23433 (inline_p ? 2 : 0));
23434 /* Generate code for it, if necessary. */
23435 expand_or_defer_fn (fn);
23436 /* Restore the saved values. */
23437 parser->in_unbraced_linkage_specification_p
23438 = saved_in_unbraced_linkage_specification_p;
23439 parser->num_template_parameter_lists
23440 = saved_num_template_parameter_lists;
23441 parser->in_function_body = saved_in_function_body;
23443 parser->fully_implicit_function_template_p
23444 = fully_implicit_function_template_p;
23445 parser->implicit_template_parms
23446 = implicit_template_parms;
23447 parser->implicit_template_scope
23448 = implicit_template_scope;
23450 if (parser->fully_implicit_function_template_p)
23451 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23453 return fn;
23456 /* Parse a template-declaration, assuming that the `export' (and
23457 `extern') keywords, if present, has already been scanned. MEMBER_P
23458 is as for cp_parser_template_declaration. */
23460 static void
23461 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23463 tree decl = NULL_TREE;
23464 vec<deferred_access_check, va_gc> *checks;
23465 tree parameter_list;
23466 bool friend_p = false;
23467 bool need_lang_pop;
23468 cp_token *token;
23470 /* Look for the `template' keyword. */
23471 token = cp_lexer_peek_token (parser->lexer);
23472 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23473 return;
23475 /* And the `<'. */
23476 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23477 return;
23478 if (at_class_scope_p () && current_function_decl)
23480 /* 14.5.2.2 [temp.mem]
23482 A local class shall not have member templates. */
23483 error_at (token->location,
23484 "invalid declaration of member template in local class");
23485 cp_parser_skip_to_end_of_block_or_statement (parser);
23486 return;
23488 /* [temp]
23490 A template ... shall not have C linkage. */
23491 if (current_lang_name == lang_name_c)
23493 error_at (token->location, "template with C linkage");
23494 /* Give it C++ linkage to avoid confusing other parts of the
23495 front end. */
23496 push_lang_context (lang_name_cplusplus);
23497 need_lang_pop = true;
23499 else
23500 need_lang_pop = false;
23502 /* We cannot perform access checks on the template parameter
23503 declarations until we know what is being declared, just as we
23504 cannot check the decl-specifier list. */
23505 push_deferring_access_checks (dk_deferred);
23507 /* If the next token is `>', then we have an invalid
23508 specialization. Rather than complain about an invalid template
23509 parameter, issue an error message here. */
23510 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23512 cp_parser_error (parser, "invalid explicit specialization");
23513 begin_specialization ();
23514 parameter_list = NULL_TREE;
23516 else
23518 /* Parse the template parameters. */
23519 parameter_list = cp_parser_template_parameter_list (parser);
23522 /* Get the deferred access checks from the parameter list. These
23523 will be checked once we know what is being declared, as for a
23524 member template the checks must be performed in the scope of the
23525 class containing the member. */
23526 checks = get_deferred_access_checks ();
23528 /* Look for the `>'. */
23529 cp_parser_skip_to_end_of_template_parameter_list (parser);
23530 /* We just processed one more parameter list. */
23531 ++parser->num_template_parameter_lists;
23532 /* If the next token is `template', there are more template
23533 parameters. */
23534 if (cp_lexer_next_token_is_keyword (parser->lexer,
23535 RID_TEMPLATE))
23536 cp_parser_template_declaration_after_export (parser, member_p);
23537 else if (cxx_dialect >= cxx11
23538 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23539 decl = cp_parser_alias_declaration (parser);
23540 else
23542 /* There are no access checks when parsing a template, as we do not
23543 know if a specialization will be a friend. */
23544 push_deferring_access_checks (dk_no_check);
23545 token = cp_lexer_peek_token (parser->lexer);
23546 decl = cp_parser_single_declaration (parser,
23547 checks,
23548 member_p,
23549 /*explicit_specialization_p=*/false,
23550 &friend_p);
23551 pop_deferring_access_checks ();
23553 /* If this is a member template declaration, let the front
23554 end know. */
23555 if (member_p && !friend_p && decl)
23557 if (TREE_CODE (decl) == TYPE_DECL)
23558 cp_parser_check_access_in_redeclaration (decl, token->location);
23560 decl = finish_member_template_decl (decl);
23562 else if (friend_p && decl
23563 && DECL_DECLARES_TYPE_P (decl))
23564 make_friend_class (current_class_type, TREE_TYPE (decl),
23565 /*complain=*/true);
23567 /* We are done with the current parameter list. */
23568 --parser->num_template_parameter_lists;
23570 pop_deferring_access_checks ();
23572 /* Finish up. */
23573 finish_template_decl (parameter_list);
23575 /* Check the template arguments for a literal operator template. */
23576 if (decl
23577 && DECL_DECLARES_FUNCTION_P (decl)
23578 && UDLIT_OPER_P (DECL_NAME (decl)))
23580 bool ok = true;
23581 if (parameter_list == NULL_TREE)
23582 ok = false;
23583 else
23585 int num_parms = TREE_VEC_LENGTH (parameter_list);
23586 if (num_parms == 1)
23588 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23589 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23590 if (TREE_TYPE (parm) != char_type_node
23591 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23592 ok = false;
23594 else if (num_parms == 2 && cxx_dialect >= cxx14)
23596 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23597 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23598 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23599 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23600 if (TREE_TYPE (parm) != TREE_TYPE (type)
23601 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23602 ok = false;
23604 else
23605 ok = false;
23607 if (!ok)
23609 if (cxx_dialect >= cxx14)
23610 error ("literal operator template %qD has invalid parameter list."
23611 " Expected non-type template argument pack <char...>"
23612 " or <typename CharT, CharT...>",
23613 decl);
23614 else
23615 error ("literal operator template %qD has invalid parameter list."
23616 " Expected non-type template argument pack <char...>",
23617 decl);
23620 /* Register member declarations. */
23621 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23622 finish_member_declaration (decl);
23623 /* For the erroneous case of a template with C linkage, we pushed an
23624 implicit C++ linkage scope; exit that scope now. */
23625 if (need_lang_pop)
23626 pop_lang_context ();
23627 /* If DECL is a function template, we must return to parse it later.
23628 (Even though there is no definition, there might be default
23629 arguments that need handling.) */
23630 if (member_p && decl
23631 && DECL_DECLARES_FUNCTION_P (decl))
23632 vec_safe_push (unparsed_funs_with_definitions, decl);
23635 /* Perform the deferred access checks from a template-parameter-list.
23636 CHECKS is a TREE_LIST of access checks, as returned by
23637 get_deferred_access_checks. */
23639 static void
23640 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23642 ++processing_template_parmlist;
23643 perform_access_checks (checks, tf_warning_or_error);
23644 --processing_template_parmlist;
23647 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23648 `function-definition' sequence that follows a template header.
23649 If MEMBER_P is true, this declaration appears in a class scope.
23651 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23652 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23654 static tree
23655 cp_parser_single_declaration (cp_parser* parser,
23656 vec<deferred_access_check, va_gc> *checks,
23657 bool member_p,
23658 bool explicit_specialization_p,
23659 bool* friend_p)
23661 int declares_class_or_enum;
23662 tree decl = NULL_TREE;
23663 cp_decl_specifier_seq decl_specifiers;
23664 bool function_definition_p = false;
23665 cp_token *decl_spec_token_start;
23667 /* This function is only used when processing a template
23668 declaration. */
23669 gcc_assert (innermost_scope_kind () == sk_template_parms
23670 || innermost_scope_kind () == sk_template_spec);
23672 /* Defer access checks until we know what is being declared. */
23673 push_deferring_access_checks (dk_deferred);
23675 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23676 alternative. */
23677 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23678 cp_parser_decl_specifier_seq (parser,
23679 CP_PARSER_FLAGS_OPTIONAL,
23680 &decl_specifiers,
23681 &declares_class_or_enum);
23682 if (friend_p)
23683 *friend_p = cp_parser_friend_p (&decl_specifiers);
23685 /* There are no template typedefs. */
23686 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23688 error_at (decl_spec_token_start->location,
23689 "template declaration of %<typedef%>");
23690 decl = error_mark_node;
23693 /* Gather up the access checks that occurred the
23694 decl-specifier-seq. */
23695 stop_deferring_access_checks ();
23697 /* Check for the declaration of a template class. */
23698 if (declares_class_or_enum)
23700 if (cp_parser_declares_only_class_p (parser))
23702 decl = shadow_tag (&decl_specifiers);
23704 /* In this case:
23706 struct C {
23707 friend template <typename T> struct A<T>::B;
23710 A<T>::B will be represented by a TYPENAME_TYPE, and
23711 therefore not recognized by shadow_tag. */
23712 if (friend_p && *friend_p
23713 && !decl
23714 && decl_specifiers.type
23715 && TYPE_P (decl_specifiers.type))
23716 decl = decl_specifiers.type;
23718 if (decl && decl != error_mark_node)
23719 decl = TYPE_NAME (decl);
23720 else
23721 decl = error_mark_node;
23723 /* Perform access checks for template parameters. */
23724 cp_parser_perform_template_parameter_access_checks (checks);
23728 /* Complain about missing 'typename' or other invalid type names. */
23729 if (!decl_specifiers.any_type_specifiers_p
23730 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23732 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23733 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23734 the rest of this declaration. */
23735 decl = error_mark_node;
23736 goto out;
23739 /* If it's not a template class, try for a template function. If
23740 the next token is a `;', then this declaration does not declare
23741 anything. But, if there were errors in the decl-specifiers, then
23742 the error might well have come from an attempted class-specifier.
23743 In that case, there's no need to warn about a missing declarator. */
23744 if (!decl
23745 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23746 || decl_specifiers.type != error_mark_node))
23748 decl = cp_parser_init_declarator (parser,
23749 &decl_specifiers,
23750 checks,
23751 /*function_definition_allowed_p=*/true,
23752 member_p,
23753 declares_class_or_enum,
23754 &function_definition_p,
23755 NULL, NULL);
23757 /* 7.1.1-1 [dcl.stc]
23759 A storage-class-specifier shall not be specified in an explicit
23760 specialization... */
23761 if (decl
23762 && explicit_specialization_p
23763 && decl_specifiers.storage_class != sc_none)
23765 error_at (decl_spec_token_start->location,
23766 "explicit template specialization cannot have a storage class");
23767 decl = error_mark_node;
23770 if (decl && VAR_P (decl))
23771 check_template_variable (decl);
23774 /* Look for a trailing `;' after the declaration. */
23775 if (!function_definition_p
23776 && (decl == error_mark_node
23777 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23778 cp_parser_skip_to_end_of_block_or_statement (parser);
23780 out:
23781 pop_deferring_access_checks ();
23783 /* Clear any current qualification; whatever comes next is the start
23784 of something new. */
23785 parser->scope = NULL_TREE;
23786 parser->qualifying_scope = NULL_TREE;
23787 parser->object_scope = NULL_TREE;
23789 return decl;
23792 /* Parse a cast-expression that is not the operand of a unary "&". */
23794 static tree
23795 cp_parser_simple_cast_expression (cp_parser *parser)
23797 return cp_parser_cast_expression (parser, /*address_p=*/false,
23798 /*cast_p=*/false, /*decltype*/false, NULL);
23801 /* Parse a functional cast to TYPE. Returns an expression
23802 representing the cast. */
23804 static tree
23805 cp_parser_functional_cast (cp_parser* parser, tree type)
23807 vec<tree, va_gc> *vec;
23808 tree expression_list;
23809 tree cast;
23810 bool nonconst_p;
23812 if (!type)
23813 type = error_mark_node;
23815 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23817 cp_lexer_set_source_position (parser->lexer);
23818 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23819 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23820 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23821 if (TREE_CODE (type) == TYPE_DECL)
23822 type = TREE_TYPE (type);
23823 return finish_compound_literal (type, expression_list,
23824 tf_warning_or_error);
23828 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23829 /*cast_p=*/true,
23830 /*allow_expansion_p=*/true,
23831 /*non_constant_p=*/NULL);
23832 if (vec == NULL)
23833 expression_list = error_mark_node;
23834 else
23836 expression_list = build_tree_list_vec (vec);
23837 release_tree_vector (vec);
23840 cast = build_functional_cast (type, expression_list,
23841 tf_warning_or_error);
23842 /* [expr.const]/1: In an integral constant expression "only type
23843 conversions to integral or enumeration type can be used". */
23844 if (TREE_CODE (type) == TYPE_DECL)
23845 type = TREE_TYPE (type);
23846 if (cast != error_mark_node
23847 && !cast_valid_in_integral_constant_expression_p (type)
23848 && cp_parser_non_integral_constant_expression (parser,
23849 NIC_CONSTRUCTOR))
23850 return error_mark_node;
23851 return cast;
23854 /* Save the tokens that make up the body of a member function defined
23855 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23856 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23857 specifiers applied to the declaration. Returns the FUNCTION_DECL
23858 for the member function. */
23860 static tree
23861 cp_parser_save_member_function_body (cp_parser* parser,
23862 cp_decl_specifier_seq *decl_specifiers,
23863 cp_declarator *declarator,
23864 tree attributes)
23866 cp_token *first;
23867 cp_token *last;
23868 tree fn;
23870 /* Create the FUNCTION_DECL. */
23871 fn = grokmethod (decl_specifiers, declarator, attributes);
23872 cp_finalize_omp_declare_simd (parser, fn);
23873 /* If something went badly wrong, bail out now. */
23874 if (fn == error_mark_node)
23876 /* If there's a function-body, skip it. */
23877 if (cp_parser_token_starts_function_definition_p
23878 (cp_lexer_peek_token (parser->lexer)))
23879 cp_parser_skip_to_end_of_block_or_statement (parser);
23880 return error_mark_node;
23883 /* Remember it, if there default args to post process. */
23884 cp_parser_save_default_args (parser, fn);
23886 /* Save away the tokens that make up the body of the
23887 function. */
23888 first = parser->lexer->next_token;
23889 /* Handle function try blocks. */
23890 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23891 cp_lexer_consume_token (parser->lexer);
23892 /* We can have braced-init-list mem-initializers before the fn body. */
23893 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23895 cp_lexer_consume_token (parser->lexer);
23896 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23898 /* cache_group will stop after an un-nested { } pair, too. */
23899 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23900 break;
23902 /* variadic mem-inits have ... after the ')'. */
23903 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23904 cp_lexer_consume_token (parser->lexer);
23907 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23908 /* Handle function try blocks. */
23909 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23910 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23911 last = parser->lexer->next_token;
23913 /* Save away the inline definition; we will process it when the
23914 class is complete. */
23915 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23916 DECL_PENDING_INLINE_P (fn) = 1;
23918 /* We need to know that this was defined in the class, so that
23919 friend templates are handled correctly. */
23920 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23922 /* Add FN to the queue of functions to be parsed later. */
23923 vec_safe_push (unparsed_funs_with_definitions, fn);
23925 return fn;
23928 /* Save the tokens that make up the in-class initializer for a non-static
23929 data member. Returns a DEFAULT_ARG. */
23931 static tree
23932 cp_parser_save_nsdmi (cp_parser* parser)
23934 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23937 /* Parse a template-argument-list, as well as the trailing ">" (but
23938 not the opening "<"). See cp_parser_template_argument_list for the
23939 return value. */
23941 static tree
23942 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23944 tree arguments;
23945 tree saved_scope;
23946 tree saved_qualifying_scope;
23947 tree saved_object_scope;
23948 bool saved_greater_than_is_operator_p;
23949 int saved_unevaluated_operand;
23950 int saved_inhibit_evaluation_warnings;
23952 /* [temp.names]
23954 When parsing a template-id, the first non-nested `>' is taken as
23955 the end of the template-argument-list rather than a greater-than
23956 operator. */
23957 saved_greater_than_is_operator_p
23958 = parser->greater_than_is_operator_p;
23959 parser->greater_than_is_operator_p = false;
23960 /* Parsing the argument list may modify SCOPE, so we save it
23961 here. */
23962 saved_scope = parser->scope;
23963 saved_qualifying_scope = parser->qualifying_scope;
23964 saved_object_scope = parser->object_scope;
23965 /* We need to evaluate the template arguments, even though this
23966 template-id may be nested within a "sizeof". */
23967 saved_unevaluated_operand = cp_unevaluated_operand;
23968 cp_unevaluated_operand = 0;
23969 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23970 c_inhibit_evaluation_warnings = 0;
23971 /* Parse the template-argument-list itself. */
23972 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23973 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23974 arguments = NULL_TREE;
23975 else
23976 arguments = cp_parser_template_argument_list (parser);
23977 /* Look for the `>' that ends the template-argument-list. If we find
23978 a '>>' instead, it's probably just a typo. */
23979 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23981 if (cxx_dialect != cxx98)
23983 /* In C++0x, a `>>' in a template argument list or cast
23984 expression is considered to be two separate `>'
23985 tokens. So, change the current token to a `>', but don't
23986 consume it: it will be consumed later when the outer
23987 template argument list (or cast expression) is parsed.
23988 Note that this replacement of `>' for `>>' is necessary
23989 even if we are parsing tentatively: in the tentative
23990 case, after calling
23991 cp_parser_enclosed_template_argument_list we will always
23992 throw away all of the template arguments and the first
23993 closing `>', either because the template argument list
23994 was erroneous or because we are replacing those tokens
23995 with a CPP_TEMPLATE_ID token. The second `>' (which will
23996 not have been thrown away) is needed either to close an
23997 outer template argument list or to complete a new-style
23998 cast. */
23999 cp_token *token = cp_lexer_peek_token (parser->lexer);
24000 token->type = CPP_GREATER;
24002 else if (!saved_greater_than_is_operator_p)
24004 /* If we're in a nested template argument list, the '>>' has
24005 to be a typo for '> >'. We emit the error message, but we
24006 continue parsing and we push a '>' as next token, so that
24007 the argument list will be parsed correctly. Note that the
24008 global source location is still on the token before the
24009 '>>', so we need to say explicitly where we want it. */
24010 cp_token *token = cp_lexer_peek_token (parser->lexer);
24011 error_at (token->location, "%<>>%> should be %<> >%> "
24012 "within a nested template argument list");
24014 token->type = CPP_GREATER;
24016 else
24018 /* If this is not a nested template argument list, the '>>'
24019 is a typo for '>'. Emit an error message and continue.
24020 Same deal about the token location, but here we can get it
24021 right by consuming the '>>' before issuing the diagnostic. */
24022 cp_token *token = cp_lexer_consume_token (parser->lexer);
24023 error_at (token->location,
24024 "spurious %<>>%>, use %<>%> to terminate "
24025 "a template argument list");
24028 else
24029 cp_parser_skip_to_end_of_template_parameter_list (parser);
24030 /* The `>' token might be a greater-than operator again now. */
24031 parser->greater_than_is_operator_p
24032 = saved_greater_than_is_operator_p;
24033 /* Restore the SAVED_SCOPE. */
24034 parser->scope = saved_scope;
24035 parser->qualifying_scope = saved_qualifying_scope;
24036 parser->object_scope = saved_object_scope;
24037 cp_unevaluated_operand = saved_unevaluated_operand;
24038 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24040 return arguments;
24043 /* MEMBER_FUNCTION is a member function, or a friend. If default
24044 arguments, or the body of the function have not yet been parsed,
24045 parse them now. */
24047 static void
24048 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24050 timevar_push (TV_PARSE_INMETH);
24051 /* If this member is a template, get the underlying
24052 FUNCTION_DECL. */
24053 if (DECL_FUNCTION_TEMPLATE_P (member_function))
24054 member_function = DECL_TEMPLATE_RESULT (member_function);
24056 /* There should not be any class definitions in progress at this
24057 point; the bodies of members are only parsed outside of all class
24058 definitions. */
24059 gcc_assert (parser->num_classes_being_defined == 0);
24060 /* While we're parsing the member functions we might encounter more
24061 classes. We want to handle them right away, but we don't want
24062 them getting mixed up with functions that are currently in the
24063 queue. */
24064 push_unparsed_function_queues (parser);
24066 /* Make sure that any template parameters are in scope. */
24067 maybe_begin_member_template_processing (member_function);
24069 /* If the body of the function has not yet been parsed, parse it
24070 now. */
24071 if (DECL_PENDING_INLINE_P (member_function))
24073 tree function_scope;
24074 cp_token_cache *tokens;
24076 /* The function is no longer pending; we are processing it. */
24077 tokens = DECL_PENDING_INLINE_INFO (member_function);
24078 DECL_PENDING_INLINE_INFO (member_function) = NULL;
24079 DECL_PENDING_INLINE_P (member_function) = 0;
24081 /* If this is a local class, enter the scope of the containing
24082 function. */
24083 function_scope = current_function_decl;
24084 if (function_scope)
24085 push_function_context ();
24087 /* Push the body of the function onto the lexer stack. */
24088 cp_parser_push_lexer_for_tokens (parser, tokens);
24090 /* Let the front end know that we going to be defining this
24091 function. */
24092 start_preparsed_function (member_function, NULL_TREE,
24093 SF_PRE_PARSED | SF_INCLASS_INLINE);
24095 /* Don't do access checking if it is a templated function. */
24096 if (processing_template_decl)
24097 push_deferring_access_checks (dk_no_check);
24099 /* #pragma omp declare reduction needs special parsing. */
24100 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24102 parser->lexer->in_pragma = true;
24103 cp_parser_omp_declare_reduction_exprs (member_function, parser);
24104 finish_function (/*inline*/2);
24105 cp_check_omp_declare_reduction (member_function);
24107 else
24108 /* Now, parse the body of the function. */
24109 cp_parser_function_definition_after_declarator (parser,
24110 /*inline_p=*/true);
24112 if (processing_template_decl)
24113 pop_deferring_access_checks ();
24115 /* Leave the scope of the containing function. */
24116 if (function_scope)
24117 pop_function_context ();
24118 cp_parser_pop_lexer (parser);
24121 /* Remove any template parameters from the symbol table. */
24122 maybe_end_member_template_processing ();
24124 /* Restore the queue. */
24125 pop_unparsed_function_queues (parser);
24126 timevar_pop (TV_PARSE_INMETH);
24129 /* If DECL contains any default args, remember it on the unparsed
24130 functions queue. */
24132 static void
24133 cp_parser_save_default_args (cp_parser* parser, tree decl)
24135 tree probe;
24137 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24138 probe;
24139 probe = TREE_CHAIN (probe))
24140 if (TREE_PURPOSE (probe))
24142 cp_default_arg_entry entry = {current_class_type, decl};
24143 vec_safe_push (unparsed_funs_with_default_args, entry);
24144 break;
24148 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24149 which is either a FIELD_DECL or PARM_DECL. Parse it and return
24150 the result. For a PARM_DECL, PARMTYPE is the corresponding type
24151 from the parameter-type-list. */
24153 static tree
24154 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24155 tree default_arg, tree parmtype)
24157 cp_token_cache *tokens;
24158 tree parsed_arg;
24159 bool dummy;
24161 if (default_arg == error_mark_node)
24162 return error_mark_node;
24164 /* Push the saved tokens for the default argument onto the parser's
24165 lexer stack. */
24166 tokens = DEFARG_TOKENS (default_arg);
24167 cp_parser_push_lexer_for_tokens (parser, tokens);
24169 start_lambda_scope (decl);
24171 /* Parse the default argument. */
24172 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24173 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24174 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24176 finish_lambda_scope ();
24178 if (parsed_arg == error_mark_node)
24179 cp_parser_skip_to_end_of_statement (parser);
24181 if (!processing_template_decl)
24183 /* In a non-template class, check conversions now. In a template,
24184 we'll wait and instantiate these as needed. */
24185 if (TREE_CODE (decl) == PARM_DECL)
24186 parsed_arg = check_default_argument (parmtype, parsed_arg,
24187 tf_warning_or_error);
24188 else
24189 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24192 /* If the token stream has not been completely used up, then
24193 there was extra junk after the end of the default
24194 argument. */
24195 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24197 if (TREE_CODE (decl) == PARM_DECL)
24198 cp_parser_error (parser, "expected %<,%>");
24199 else
24200 cp_parser_error (parser, "expected %<;%>");
24203 /* Revert to the main lexer. */
24204 cp_parser_pop_lexer (parser);
24206 return parsed_arg;
24209 /* FIELD is a non-static data member with an initializer which we saved for
24210 later; parse it now. */
24212 static void
24213 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24215 tree def;
24217 maybe_begin_member_template_processing (field);
24219 push_unparsed_function_queues (parser);
24220 def = cp_parser_late_parse_one_default_arg (parser, field,
24221 DECL_INITIAL (field),
24222 NULL_TREE);
24223 pop_unparsed_function_queues (parser);
24225 maybe_end_member_template_processing ();
24227 DECL_INITIAL (field) = def;
24230 /* FN is a FUNCTION_DECL which may contains a parameter with an
24231 unparsed DEFAULT_ARG. Parse the default args now. This function
24232 assumes that the current scope is the scope in which the default
24233 argument should be processed. */
24235 static void
24236 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24238 bool saved_local_variables_forbidden_p;
24239 tree parm, parmdecl;
24241 /* While we're parsing the default args, we might (due to the
24242 statement expression extension) encounter more classes. We want
24243 to handle them right away, but we don't want them getting mixed
24244 up with default args that are currently in the queue. */
24245 push_unparsed_function_queues (parser);
24247 /* Local variable names (and the `this' keyword) may not appear
24248 in a default argument. */
24249 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24250 parser->local_variables_forbidden_p = true;
24252 push_defarg_context (fn);
24254 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24255 parmdecl = DECL_ARGUMENTS (fn);
24256 parm && parm != void_list_node;
24257 parm = TREE_CHAIN (parm),
24258 parmdecl = DECL_CHAIN (parmdecl))
24260 tree default_arg = TREE_PURPOSE (parm);
24261 tree parsed_arg;
24262 vec<tree, va_gc> *insts;
24263 tree copy;
24264 unsigned ix;
24266 if (!default_arg)
24267 continue;
24269 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24270 /* This can happen for a friend declaration for a function
24271 already declared with default arguments. */
24272 continue;
24274 parsed_arg
24275 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24276 default_arg,
24277 TREE_VALUE (parm));
24278 if (parsed_arg == error_mark_node)
24280 continue;
24283 TREE_PURPOSE (parm) = parsed_arg;
24285 /* Update any instantiations we've already created. */
24286 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24287 vec_safe_iterate (insts, ix, &copy); ix++)
24288 TREE_PURPOSE (copy) = parsed_arg;
24291 pop_defarg_context ();
24293 /* Make sure no default arg is missing. */
24294 check_default_args (fn);
24296 /* Restore the state of local_variables_forbidden_p. */
24297 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24299 /* Restore the queue. */
24300 pop_unparsed_function_queues (parser);
24303 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24305 sizeof ... ( identifier )
24307 where the 'sizeof' token has already been consumed. */
24309 static tree
24310 cp_parser_sizeof_pack (cp_parser *parser)
24312 /* Consume the `...'. */
24313 cp_lexer_consume_token (parser->lexer);
24314 maybe_warn_variadic_templates ();
24316 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24317 if (paren)
24318 cp_lexer_consume_token (parser->lexer);
24319 else
24320 permerror (cp_lexer_peek_token (parser->lexer)->location,
24321 "%<sizeof...%> argument must be surrounded by parentheses");
24323 cp_token *token = cp_lexer_peek_token (parser->lexer);
24324 tree name = cp_parser_identifier (parser);
24325 if (name == error_mark_node)
24326 return error_mark_node;
24327 /* The name is not qualified. */
24328 parser->scope = NULL_TREE;
24329 parser->qualifying_scope = NULL_TREE;
24330 parser->object_scope = NULL_TREE;
24331 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24332 if (expr == error_mark_node)
24333 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24334 token->location);
24335 if (TREE_CODE (expr) == TYPE_DECL)
24336 expr = TREE_TYPE (expr);
24337 else if (TREE_CODE (expr) == CONST_DECL)
24338 expr = DECL_INITIAL (expr);
24339 expr = make_pack_expansion (expr);
24341 if (paren)
24342 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24344 return expr;
24347 /* Parse the operand of `sizeof' (or a similar operator). Returns
24348 either a TYPE or an expression, depending on the form of the
24349 input. The KEYWORD indicates which kind of expression we have
24350 encountered. */
24352 static tree
24353 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24355 tree expr = NULL_TREE;
24356 const char *saved_message;
24357 char *tmp;
24358 bool saved_integral_constant_expression_p;
24359 bool saved_non_integral_constant_expression_p;
24361 /* If it's a `...', then we are computing the length of a parameter
24362 pack. */
24363 if (keyword == RID_SIZEOF
24364 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24365 return cp_parser_sizeof_pack (parser);
24367 /* Types cannot be defined in a `sizeof' expression. Save away the
24368 old message. */
24369 saved_message = parser->type_definition_forbidden_message;
24370 /* And create the new one. */
24371 tmp = concat ("types may not be defined in %<",
24372 IDENTIFIER_POINTER (ridpointers[keyword]),
24373 "%> expressions", NULL);
24374 parser->type_definition_forbidden_message = tmp;
24376 /* The restrictions on constant-expressions do not apply inside
24377 sizeof expressions. */
24378 saved_integral_constant_expression_p
24379 = parser->integral_constant_expression_p;
24380 saved_non_integral_constant_expression_p
24381 = parser->non_integral_constant_expression_p;
24382 parser->integral_constant_expression_p = false;
24384 /* Do not actually evaluate the expression. */
24385 ++cp_unevaluated_operand;
24386 ++c_inhibit_evaluation_warnings;
24387 /* If it's a `(', then we might be looking at the type-id
24388 construction. */
24389 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24391 tree type = NULL_TREE;
24393 /* We can't be sure yet whether we're looking at a type-id or an
24394 expression. */
24395 cp_parser_parse_tentatively (parser);
24396 /* Note: as a GNU Extension, compound literals are considered
24397 postfix-expressions as they are in C99, so they are valid
24398 arguments to sizeof. See comment in cp_parser_cast_expression
24399 for details. */
24400 if (cp_parser_compound_literal_p (parser))
24401 cp_parser_simulate_error (parser);
24402 else
24404 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24405 parser->in_type_id_in_expr_p = true;
24406 /* Look for the type-id. */
24407 type = cp_parser_type_id (parser);
24408 /* Look for the closing `)'. */
24409 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24410 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24413 /* If all went well, then we're done. */
24414 if (cp_parser_parse_definitely (parser))
24416 cp_decl_specifier_seq decl_specs;
24418 /* Build a trivial decl-specifier-seq. */
24419 clear_decl_specs (&decl_specs);
24420 decl_specs.type = type;
24422 /* Call grokdeclarator to figure out what type this is. */
24423 expr = grokdeclarator (NULL,
24424 &decl_specs,
24425 TYPENAME,
24426 /*initialized=*/0,
24427 /*attrlist=*/NULL);
24431 /* If the type-id production did not work out, then we must be
24432 looking at the unary-expression production. */
24433 if (!expr)
24434 expr = cp_parser_unary_expression (parser);
24436 /* Go back to evaluating expressions. */
24437 --cp_unevaluated_operand;
24438 --c_inhibit_evaluation_warnings;
24440 /* Free the message we created. */
24441 free (tmp);
24442 /* And restore the old one. */
24443 parser->type_definition_forbidden_message = saved_message;
24444 parser->integral_constant_expression_p
24445 = saved_integral_constant_expression_p;
24446 parser->non_integral_constant_expression_p
24447 = saved_non_integral_constant_expression_p;
24449 return expr;
24452 /* If the current declaration has no declarator, return true. */
24454 static bool
24455 cp_parser_declares_only_class_p (cp_parser *parser)
24457 /* If the next token is a `;' or a `,' then there is no
24458 declarator. */
24459 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24460 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24463 /* Update the DECL_SPECS to reflect the storage class indicated by
24464 KEYWORD. */
24466 static void
24467 cp_parser_set_storage_class (cp_parser *parser,
24468 cp_decl_specifier_seq *decl_specs,
24469 enum rid keyword,
24470 cp_token *token)
24472 cp_storage_class storage_class;
24474 if (parser->in_unbraced_linkage_specification_p)
24476 error_at (token->location, "invalid use of %qD in linkage specification",
24477 ridpointers[keyword]);
24478 return;
24480 else if (decl_specs->storage_class != sc_none)
24482 decl_specs->conflicting_specifiers_p = true;
24483 return;
24486 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24487 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24488 && decl_specs->gnu_thread_keyword_p)
24490 pedwarn (decl_specs->locations[ds_thread], 0,
24491 "%<__thread%> before %qD", ridpointers[keyword]);
24494 switch (keyword)
24496 case RID_AUTO:
24497 storage_class = sc_auto;
24498 break;
24499 case RID_REGISTER:
24500 storage_class = sc_register;
24501 break;
24502 case RID_STATIC:
24503 storage_class = sc_static;
24504 break;
24505 case RID_EXTERN:
24506 storage_class = sc_extern;
24507 break;
24508 case RID_MUTABLE:
24509 storage_class = sc_mutable;
24510 break;
24511 default:
24512 gcc_unreachable ();
24514 decl_specs->storage_class = storage_class;
24515 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24517 /* A storage class specifier cannot be applied alongside a typedef
24518 specifier. If there is a typedef specifier present then set
24519 conflicting_specifiers_p which will trigger an error later
24520 on in grokdeclarator. */
24521 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24522 decl_specs->conflicting_specifiers_p = true;
24525 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24526 is true, the type is a class or enum definition. */
24528 static void
24529 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24530 tree type_spec,
24531 cp_token *token,
24532 bool type_definition_p)
24534 decl_specs->any_specifiers_p = true;
24536 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24537 (with, for example, in "typedef int wchar_t;") we remember that
24538 this is what happened. In system headers, we ignore these
24539 declarations so that G++ can work with system headers that are not
24540 C++-safe. */
24541 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24542 && !type_definition_p
24543 && (type_spec == boolean_type_node
24544 || type_spec == char16_type_node
24545 || type_spec == char32_type_node
24546 || type_spec == wchar_type_node)
24547 && (decl_specs->type
24548 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24549 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24550 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24551 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24553 decl_specs->redefined_builtin_type = type_spec;
24554 set_and_check_decl_spec_loc (decl_specs,
24555 ds_redefined_builtin_type_spec,
24556 token);
24557 if (!decl_specs->type)
24559 decl_specs->type = type_spec;
24560 decl_specs->type_definition_p = false;
24561 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24564 else if (decl_specs->type)
24565 decl_specs->multiple_types_p = true;
24566 else
24568 decl_specs->type = type_spec;
24569 decl_specs->type_definition_p = type_definition_p;
24570 decl_specs->redefined_builtin_type = NULL_TREE;
24571 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24575 /* True iff TOKEN is the GNU keyword __thread. */
24577 static bool
24578 token_is__thread (cp_token *token)
24580 gcc_assert (token->keyword == RID_THREAD);
24581 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24584 /* Set the location for a declarator specifier and check if it is
24585 duplicated.
24587 DECL_SPECS is the sequence of declarator specifiers onto which to
24588 set the location.
24590 DS is the single declarator specifier to set which location is to
24591 be set onto the existing sequence of declarators.
24593 LOCATION is the location for the declarator specifier to
24594 consider. */
24596 static void
24597 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24598 cp_decl_spec ds, cp_token *token)
24600 gcc_assert (ds < ds_last);
24602 if (decl_specs == NULL)
24603 return;
24605 source_location location = token->location;
24607 if (decl_specs->locations[ds] == 0)
24609 decl_specs->locations[ds] = location;
24610 if (ds == ds_thread)
24611 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24613 else
24615 if (ds == ds_long)
24617 if (decl_specs->locations[ds_long_long] != 0)
24618 error_at (location,
24619 "%<long long long%> is too long for GCC");
24620 else
24622 decl_specs->locations[ds_long_long] = location;
24623 pedwarn_cxx98 (location,
24624 OPT_Wlong_long,
24625 "ISO C++ 1998 does not support %<long long%>");
24628 else if (ds == ds_thread)
24630 bool gnu = token_is__thread (token);
24631 if (gnu != decl_specs->gnu_thread_keyword_p)
24632 error_at (location,
24633 "both %<__thread%> and %<thread_local%> specified");
24634 else
24635 error_at (location, "duplicate %qD", token->u.value);
24637 else
24639 static const char *const decl_spec_names[] = {
24640 "signed",
24641 "unsigned",
24642 "short",
24643 "long",
24644 "const",
24645 "volatile",
24646 "restrict",
24647 "inline",
24648 "virtual",
24649 "explicit",
24650 "friend",
24651 "typedef",
24652 "using",
24653 "constexpr",
24654 "__complex"
24656 error_at (location,
24657 "duplicate %qs", decl_spec_names[ds]);
24662 /* Return true iff the declarator specifier DS is present in the
24663 sequence of declarator specifiers DECL_SPECS. */
24665 bool
24666 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24667 cp_decl_spec ds)
24669 gcc_assert (ds < ds_last);
24671 if (decl_specs == NULL)
24672 return false;
24674 return decl_specs->locations[ds] != 0;
24677 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24678 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24680 static bool
24681 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24683 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24686 /* Issue an error message indicating that TOKEN_DESC was expected.
24687 If KEYWORD is true, it indicated this function is called by
24688 cp_parser_require_keword and the required token can only be
24689 a indicated keyword. */
24691 static void
24692 cp_parser_required_error (cp_parser *parser,
24693 required_token token_desc,
24694 bool keyword)
24696 switch (token_desc)
24698 case RT_NEW:
24699 cp_parser_error (parser, "expected %<new%>");
24700 return;
24701 case RT_DELETE:
24702 cp_parser_error (parser, "expected %<delete%>");
24703 return;
24704 case RT_RETURN:
24705 cp_parser_error (parser, "expected %<return%>");
24706 return;
24707 case RT_WHILE:
24708 cp_parser_error (parser, "expected %<while%>");
24709 return;
24710 case RT_EXTERN:
24711 cp_parser_error (parser, "expected %<extern%>");
24712 return;
24713 case RT_STATIC_ASSERT:
24714 cp_parser_error (parser, "expected %<static_assert%>");
24715 return;
24716 case RT_DECLTYPE:
24717 cp_parser_error (parser, "expected %<decltype%>");
24718 return;
24719 case RT_OPERATOR:
24720 cp_parser_error (parser, "expected %<operator%>");
24721 return;
24722 case RT_CLASS:
24723 cp_parser_error (parser, "expected %<class%>");
24724 return;
24725 case RT_TEMPLATE:
24726 cp_parser_error (parser, "expected %<template%>");
24727 return;
24728 case RT_NAMESPACE:
24729 cp_parser_error (parser, "expected %<namespace%>");
24730 return;
24731 case RT_USING:
24732 cp_parser_error (parser, "expected %<using%>");
24733 return;
24734 case RT_ASM:
24735 cp_parser_error (parser, "expected %<asm%>");
24736 return;
24737 case RT_TRY:
24738 cp_parser_error (parser, "expected %<try%>");
24739 return;
24740 case RT_CATCH:
24741 cp_parser_error (parser, "expected %<catch%>");
24742 return;
24743 case RT_THROW:
24744 cp_parser_error (parser, "expected %<throw%>");
24745 return;
24746 case RT_LABEL:
24747 cp_parser_error (parser, "expected %<__label__%>");
24748 return;
24749 case RT_AT_TRY:
24750 cp_parser_error (parser, "expected %<@try%>");
24751 return;
24752 case RT_AT_SYNCHRONIZED:
24753 cp_parser_error (parser, "expected %<@synchronized%>");
24754 return;
24755 case RT_AT_THROW:
24756 cp_parser_error (parser, "expected %<@throw%>");
24757 return;
24758 case RT_TRANSACTION_ATOMIC:
24759 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24760 return;
24761 case RT_TRANSACTION_RELAXED:
24762 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24763 return;
24764 default:
24765 break;
24767 if (!keyword)
24769 switch (token_desc)
24771 case RT_SEMICOLON:
24772 cp_parser_error (parser, "expected %<;%>");
24773 return;
24774 case RT_OPEN_PAREN:
24775 cp_parser_error (parser, "expected %<(%>");
24776 return;
24777 case RT_CLOSE_BRACE:
24778 cp_parser_error (parser, "expected %<}%>");
24779 return;
24780 case RT_OPEN_BRACE:
24781 cp_parser_error (parser, "expected %<{%>");
24782 return;
24783 case RT_CLOSE_SQUARE:
24784 cp_parser_error (parser, "expected %<]%>");
24785 return;
24786 case RT_OPEN_SQUARE:
24787 cp_parser_error (parser, "expected %<[%>");
24788 return;
24789 case RT_COMMA:
24790 cp_parser_error (parser, "expected %<,%>");
24791 return;
24792 case RT_SCOPE:
24793 cp_parser_error (parser, "expected %<::%>");
24794 return;
24795 case RT_LESS:
24796 cp_parser_error (parser, "expected %<<%>");
24797 return;
24798 case RT_GREATER:
24799 cp_parser_error (parser, "expected %<>%>");
24800 return;
24801 case RT_EQ:
24802 cp_parser_error (parser, "expected %<=%>");
24803 return;
24804 case RT_ELLIPSIS:
24805 cp_parser_error (parser, "expected %<...%>");
24806 return;
24807 case RT_MULT:
24808 cp_parser_error (parser, "expected %<*%>");
24809 return;
24810 case RT_COMPL:
24811 cp_parser_error (parser, "expected %<~%>");
24812 return;
24813 case RT_COLON:
24814 cp_parser_error (parser, "expected %<:%>");
24815 return;
24816 case RT_COLON_SCOPE:
24817 cp_parser_error (parser, "expected %<:%> or %<::%>");
24818 return;
24819 case RT_CLOSE_PAREN:
24820 cp_parser_error (parser, "expected %<)%>");
24821 return;
24822 case RT_COMMA_CLOSE_PAREN:
24823 cp_parser_error (parser, "expected %<,%> or %<)%>");
24824 return;
24825 case RT_PRAGMA_EOL:
24826 cp_parser_error (parser, "expected end of line");
24827 return;
24828 case RT_NAME:
24829 cp_parser_error (parser, "expected identifier");
24830 return;
24831 case RT_SELECT:
24832 cp_parser_error (parser, "expected selection-statement");
24833 return;
24834 case RT_INTERATION:
24835 cp_parser_error (parser, "expected iteration-statement");
24836 return;
24837 case RT_JUMP:
24838 cp_parser_error (parser, "expected jump-statement");
24839 return;
24840 case RT_CLASS_KEY:
24841 cp_parser_error (parser, "expected class-key");
24842 return;
24843 case RT_CLASS_TYPENAME_TEMPLATE:
24844 cp_parser_error (parser,
24845 "expected %<class%>, %<typename%>, or %<template%>");
24846 return;
24847 default:
24848 gcc_unreachable ();
24851 else
24852 gcc_unreachable ();
24857 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24858 issue an error message indicating that TOKEN_DESC was expected.
24860 Returns the token consumed, if the token had the appropriate type.
24861 Otherwise, returns NULL. */
24863 static cp_token *
24864 cp_parser_require (cp_parser* parser,
24865 enum cpp_ttype type,
24866 required_token token_desc)
24868 if (cp_lexer_next_token_is (parser->lexer, type))
24869 return cp_lexer_consume_token (parser->lexer);
24870 else
24872 /* Output the MESSAGE -- unless we're parsing tentatively. */
24873 if (!cp_parser_simulate_error (parser))
24874 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24875 return NULL;
24879 /* An error message is produced if the next token is not '>'.
24880 All further tokens are skipped until the desired token is
24881 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24883 static void
24884 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24886 /* Current level of '< ... >'. */
24887 unsigned level = 0;
24888 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24889 unsigned nesting_depth = 0;
24891 /* Are we ready, yet? If not, issue error message. */
24892 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24893 return;
24895 /* Skip tokens until the desired token is found. */
24896 while (true)
24898 /* Peek at the next token. */
24899 switch (cp_lexer_peek_token (parser->lexer)->type)
24901 case CPP_LESS:
24902 if (!nesting_depth)
24903 ++level;
24904 break;
24906 case CPP_RSHIFT:
24907 if (cxx_dialect == cxx98)
24908 /* C++0x views the `>>' operator as two `>' tokens, but
24909 C++98 does not. */
24910 break;
24911 else if (!nesting_depth && level-- == 0)
24913 /* We've hit a `>>' where the first `>' closes the
24914 template argument list, and the second `>' is
24915 spurious. Just consume the `>>' and stop; we've
24916 already produced at least one error. */
24917 cp_lexer_consume_token (parser->lexer);
24918 return;
24920 /* Fall through for C++0x, so we handle the second `>' in
24921 the `>>'. */
24923 case CPP_GREATER:
24924 if (!nesting_depth && level-- == 0)
24926 /* We've reached the token we want, consume it and stop. */
24927 cp_lexer_consume_token (parser->lexer);
24928 return;
24930 break;
24932 case CPP_OPEN_PAREN:
24933 case CPP_OPEN_SQUARE:
24934 ++nesting_depth;
24935 break;
24937 case CPP_CLOSE_PAREN:
24938 case CPP_CLOSE_SQUARE:
24939 if (nesting_depth-- == 0)
24940 return;
24941 break;
24943 case CPP_EOF:
24944 case CPP_PRAGMA_EOL:
24945 case CPP_SEMICOLON:
24946 case CPP_OPEN_BRACE:
24947 case CPP_CLOSE_BRACE:
24948 /* The '>' was probably forgotten, don't look further. */
24949 return;
24951 default:
24952 break;
24955 /* Consume this token. */
24956 cp_lexer_consume_token (parser->lexer);
24960 /* If the next token is the indicated keyword, consume it. Otherwise,
24961 issue an error message indicating that TOKEN_DESC was expected.
24963 Returns the token consumed, if the token had the appropriate type.
24964 Otherwise, returns NULL. */
24966 static cp_token *
24967 cp_parser_require_keyword (cp_parser* parser,
24968 enum rid keyword,
24969 required_token token_desc)
24971 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24973 if (token && token->keyword != keyword)
24975 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24976 return NULL;
24979 return token;
24982 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24983 function-definition. */
24985 static bool
24986 cp_parser_token_starts_function_definition_p (cp_token* token)
24988 return (/* An ordinary function-body begins with an `{'. */
24989 token->type == CPP_OPEN_BRACE
24990 /* A ctor-initializer begins with a `:'. */
24991 || token->type == CPP_COLON
24992 /* A function-try-block begins with `try'. */
24993 || token->keyword == RID_TRY
24994 /* A function-transaction-block begins with `__transaction_atomic'
24995 or `__transaction_relaxed'. */
24996 || token->keyword == RID_TRANSACTION_ATOMIC
24997 || token->keyword == RID_TRANSACTION_RELAXED
24998 /* The named return value extension begins with `return'. */
24999 || token->keyword == RID_RETURN);
25002 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25003 definition. */
25005 static bool
25006 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25008 cp_token *token;
25010 token = cp_lexer_peek_token (parser->lexer);
25011 return (token->type == CPP_OPEN_BRACE
25012 || (token->type == CPP_COLON
25013 && !parser->colon_doesnt_start_class_def_p));
25016 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25017 C++0x) ending a template-argument. */
25019 static bool
25020 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25022 cp_token *token;
25024 token = cp_lexer_peek_token (parser->lexer);
25025 return (token->type == CPP_COMMA
25026 || token->type == CPP_GREATER
25027 || token->type == CPP_ELLIPSIS
25028 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25031 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25032 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
25034 static bool
25035 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25036 size_t n)
25038 cp_token *token;
25040 token = cp_lexer_peek_nth_token (parser->lexer, n);
25041 if (token->type == CPP_LESS)
25042 return true;
25043 /* Check for the sequence `<::' in the original code. It would be lexed as
25044 `[:', where `[' is a digraph, and there is no whitespace before
25045 `:'. */
25046 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25048 cp_token *token2;
25049 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25050 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25051 return true;
25053 return false;
25056 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25057 or none_type otherwise. */
25059 static enum tag_types
25060 cp_parser_token_is_class_key (cp_token* token)
25062 switch (token->keyword)
25064 case RID_CLASS:
25065 return class_type;
25066 case RID_STRUCT:
25067 return record_type;
25068 case RID_UNION:
25069 return union_type;
25071 default:
25072 return none_type;
25076 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25077 or none_type otherwise or if the token is null. */
25079 static enum tag_types
25080 cp_parser_token_is_type_parameter_key (cp_token* token)
25082 if (!token)
25083 return none_type;
25085 switch (token->keyword)
25087 case RID_CLASS:
25088 return class_type;
25089 case RID_TYPENAME:
25090 return typename_type;
25092 default:
25093 return none_type;
25097 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
25099 static void
25100 cp_parser_check_class_key (enum tag_types class_key, tree type)
25102 if (type == error_mark_node)
25103 return;
25104 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25106 if (permerror (input_location, "%qs tag used in naming %q#T",
25107 class_key == union_type ? "union"
25108 : class_key == record_type ? "struct" : "class",
25109 type))
25110 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25111 "%q#T was previously declared here", type);
25115 /* Issue an error message if DECL is redeclared with different
25116 access than its original declaration [class.access.spec/3].
25117 This applies to nested classes and nested class templates.
25118 [class.mem/1]. */
25120 static void
25121 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25123 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25124 return;
25126 if ((TREE_PRIVATE (decl)
25127 != (current_access_specifier == access_private_node))
25128 || (TREE_PROTECTED (decl)
25129 != (current_access_specifier == access_protected_node)))
25130 error_at (location, "%qD redeclared with different access", decl);
25133 /* Look for the `template' keyword, as a syntactic disambiguator.
25134 Return TRUE iff it is present, in which case it will be
25135 consumed. */
25137 static bool
25138 cp_parser_optional_template_keyword (cp_parser *parser)
25140 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25142 /* In C++98 the `template' keyword can only be used within templates;
25143 outside templates the parser can always figure out what is a
25144 template and what is not. In C++11, per the resolution of DR 468,
25145 `template' is allowed in cases where it is not strictly necessary. */
25146 if (!processing_template_decl
25147 && pedantic && cxx_dialect == cxx98)
25149 cp_token *token = cp_lexer_peek_token (parser->lexer);
25150 pedwarn (token->location, OPT_Wpedantic,
25151 "in C++98 %<template%> (as a disambiguator) is only "
25152 "allowed within templates");
25153 /* If this part of the token stream is rescanned, the same
25154 error message would be generated. So, we purge the token
25155 from the stream. */
25156 cp_lexer_purge_token (parser->lexer);
25157 return false;
25159 else
25161 /* Consume the `template' keyword. */
25162 cp_lexer_consume_token (parser->lexer);
25163 return true;
25166 return false;
25169 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
25170 set PARSER->SCOPE, and perform other related actions. */
25172 static void
25173 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25175 int i;
25176 struct tree_check *check_value;
25177 deferred_access_check *chk;
25178 vec<deferred_access_check, va_gc> *checks;
25180 /* Get the stored value. */
25181 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25182 /* Perform any access checks that were deferred. */
25183 checks = check_value->checks;
25184 if (checks)
25186 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25187 perform_or_defer_access_check (chk->binfo,
25188 chk->decl,
25189 chk->diag_decl, tf_warning_or_error);
25191 /* Set the scope from the stored value. */
25192 parser->scope = check_value->value;
25193 parser->qualifying_scope = check_value->qualifying_scope;
25194 parser->object_scope = NULL_TREE;
25197 /* Consume tokens up through a non-nested END token. Returns TRUE if we
25198 encounter the end of a block before what we were looking for. */
25200 static bool
25201 cp_parser_cache_group (cp_parser *parser,
25202 enum cpp_ttype end,
25203 unsigned depth)
25205 while (true)
25207 cp_token *token = cp_lexer_peek_token (parser->lexer);
25209 /* Abort a parenthesized expression if we encounter a semicolon. */
25210 if ((end == CPP_CLOSE_PAREN || depth == 0)
25211 && token->type == CPP_SEMICOLON)
25212 return true;
25213 /* If we've reached the end of the file, stop. */
25214 if (token->type == CPP_EOF
25215 || (end != CPP_PRAGMA_EOL
25216 && token->type == CPP_PRAGMA_EOL))
25217 return true;
25218 if (token->type == CPP_CLOSE_BRACE && depth == 0)
25219 /* We've hit the end of an enclosing block, so there's been some
25220 kind of syntax error. */
25221 return true;
25223 /* Consume the token. */
25224 cp_lexer_consume_token (parser->lexer);
25225 /* See if it starts a new group. */
25226 if (token->type == CPP_OPEN_BRACE)
25228 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25229 /* In theory this should probably check end == '}', but
25230 cp_parser_save_member_function_body needs it to exit
25231 after either '}' or ')' when called with ')'. */
25232 if (depth == 0)
25233 return false;
25235 else if (token->type == CPP_OPEN_PAREN)
25237 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25238 if (depth == 0 && end == CPP_CLOSE_PAREN)
25239 return false;
25241 else if (token->type == CPP_PRAGMA)
25242 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25243 else if (token->type == end)
25244 return false;
25248 /* Like above, for caching a default argument or NSDMI. Both of these are
25249 terminated by a non-nested comma, but it can be unclear whether or not a
25250 comma is nested in a template argument list unless we do more parsing.
25251 In order to handle this ambiguity, when we encounter a ',' after a '<'
25252 we try to parse what follows as a parameter-declaration-list (in the
25253 case of a default argument) or a member-declarator (in the case of an
25254 NSDMI). If that succeeds, then we stop caching. */
25256 static tree
25257 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25259 unsigned depth = 0;
25260 int maybe_template_id = 0;
25261 cp_token *first_token;
25262 cp_token *token;
25263 tree default_argument;
25265 /* Add tokens until we have processed the entire default
25266 argument. We add the range [first_token, token). */
25267 first_token = cp_lexer_peek_token (parser->lexer);
25268 if (first_token->type == CPP_OPEN_BRACE)
25270 /* For list-initialization, this is straightforward. */
25271 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25272 token = cp_lexer_peek_token (parser->lexer);
25274 else while (true)
25276 bool done = false;
25278 /* Peek at the next token. */
25279 token = cp_lexer_peek_token (parser->lexer);
25280 /* What we do depends on what token we have. */
25281 switch (token->type)
25283 /* In valid code, a default argument must be
25284 immediately followed by a `,' `)', or `...'. */
25285 case CPP_COMMA:
25286 if (depth == 0 && maybe_template_id)
25288 /* If we've seen a '<', we might be in a
25289 template-argument-list. Until Core issue 325 is
25290 resolved, we don't know how this situation ought
25291 to be handled, so try to DTRT. We check whether
25292 what comes after the comma is a valid parameter
25293 declaration list. If it is, then the comma ends
25294 the default argument; otherwise the default
25295 argument continues. */
25296 bool error = false;
25298 /* Set ITALP so cp_parser_parameter_declaration_list
25299 doesn't decide to commit to this parse. */
25300 bool saved_italp = parser->in_template_argument_list_p;
25301 parser->in_template_argument_list_p = true;
25303 cp_parser_parse_tentatively (parser);
25304 cp_lexer_consume_token (parser->lexer);
25306 if (nsdmi)
25308 int ctor_dtor_or_conv_p;
25309 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25310 &ctor_dtor_or_conv_p,
25311 /*parenthesized_p=*/NULL,
25312 /*member_p=*/true,
25313 /*friend_p=*/false);
25315 else
25317 begin_scope (sk_function_parms, NULL_TREE);
25318 cp_parser_parameter_declaration_list (parser, &error);
25319 pop_bindings_and_leave_scope ();
25321 if (!cp_parser_error_occurred (parser) && !error)
25322 done = true;
25323 cp_parser_abort_tentative_parse (parser);
25325 parser->in_template_argument_list_p = saved_italp;
25326 break;
25328 case CPP_CLOSE_PAREN:
25329 case CPP_ELLIPSIS:
25330 /* If we run into a non-nested `;', `}', or `]',
25331 then the code is invalid -- but the default
25332 argument is certainly over. */
25333 case CPP_SEMICOLON:
25334 case CPP_CLOSE_BRACE:
25335 case CPP_CLOSE_SQUARE:
25336 if (depth == 0
25337 /* Handle correctly int n = sizeof ... ( p ); */
25338 && token->type != CPP_ELLIPSIS)
25339 done = true;
25340 /* Update DEPTH, if necessary. */
25341 else if (token->type == CPP_CLOSE_PAREN
25342 || token->type == CPP_CLOSE_BRACE
25343 || token->type == CPP_CLOSE_SQUARE)
25344 --depth;
25345 break;
25347 case CPP_OPEN_PAREN:
25348 case CPP_OPEN_SQUARE:
25349 case CPP_OPEN_BRACE:
25350 ++depth;
25351 break;
25353 case CPP_LESS:
25354 if (depth == 0)
25355 /* This might be the comparison operator, or it might
25356 start a template argument list. */
25357 ++maybe_template_id;
25358 break;
25360 case CPP_RSHIFT:
25361 if (cxx_dialect == cxx98)
25362 break;
25363 /* Fall through for C++0x, which treats the `>>'
25364 operator like two `>' tokens in certain
25365 cases. */
25367 case CPP_GREATER:
25368 if (depth == 0)
25370 /* This might be an operator, or it might close a
25371 template argument list. But if a previous '<'
25372 started a template argument list, this will have
25373 closed it, so we can't be in one anymore. */
25374 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25375 if (maybe_template_id < 0)
25376 maybe_template_id = 0;
25378 break;
25380 /* If we run out of tokens, issue an error message. */
25381 case CPP_EOF:
25382 case CPP_PRAGMA_EOL:
25383 error_at (token->location, "file ends in default argument");
25384 done = true;
25385 break;
25387 case CPP_NAME:
25388 case CPP_SCOPE:
25389 /* In these cases, we should look for template-ids.
25390 For example, if the default argument is
25391 `X<int, double>()', we need to do name lookup to
25392 figure out whether or not `X' is a template; if
25393 so, the `,' does not end the default argument.
25395 That is not yet done. */
25396 break;
25398 default:
25399 break;
25402 /* If we've reached the end, stop. */
25403 if (done)
25404 break;
25406 /* Add the token to the token block. */
25407 token = cp_lexer_consume_token (parser->lexer);
25410 /* Create a DEFAULT_ARG to represent the unparsed default
25411 argument. */
25412 default_argument = make_node (DEFAULT_ARG);
25413 DEFARG_TOKENS (default_argument)
25414 = cp_token_cache_new (first_token, token);
25415 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25417 return default_argument;
25420 /* Begin parsing tentatively. We always save tokens while parsing
25421 tentatively so that if the tentative parsing fails we can restore the
25422 tokens. */
25424 static void
25425 cp_parser_parse_tentatively (cp_parser* parser)
25427 /* Enter a new parsing context. */
25428 parser->context = cp_parser_context_new (parser->context);
25429 /* Begin saving tokens. */
25430 cp_lexer_save_tokens (parser->lexer);
25431 /* In order to avoid repetitive access control error messages,
25432 access checks are queued up until we are no longer parsing
25433 tentatively. */
25434 push_deferring_access_checks (dk_deferred);
25437 /* Commit to the currently active tentative parse. */
25439 static void
25440 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25442 cp_parser_context *context;
25443 cp_lexer *lexer;
25445 /* Mark all of the levels as committed. */
25446 lexer = parser->lexer;
25447 for (context = parser->context; context->next; context = context->next)
25449 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25450 break;
25451 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25452 while (!cp_lexer_saving_tokens (lexer))
25453 lexer = lexer->next;
25454 cp_lexer_commit_tokens (lexer);
25458 /* Commit to the topmost currently active tentative parse.
25460 Note that this function shouldn't be called when there are
25461 irreversible side-effects while in a tentative state. For
25462 example, we shouldn't create a permanent entry in the symbol
25463 table, or issue an error message that might not apply if the
25464 tentative parse is aborted. */
25466 static void
25467 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25469 cp_parser_context *context = parser->context;
25470 cp_lexer *lexer = parser->lexer;
25472 if (context)
25474 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25475 return;
25476 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25478 while (!cp_lexer_saving_tokens (lexer))
25479 lexer = lexer->next;
25480 cp_lexer_commit_tokens (lexer);
25484 /* Abort the currently active tentative parse. All consumed tokens
25485 will be rolled back, and no diagnostics will be issued. */
25487 static void
25488 cp_parser_abort_tentative_parse (cp_parser* parser)
25490 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25491 || errorcount > 0);
25492 cp_parser_simulate_error (parser);
25493 /* Now, pretend that we want to see if the construct was
25494 successfully parsed. */
25495 cp_parser_parse_definitely (parser);
25498 /* Stop parsing tentatively. If a parse error has occurred, restore the
25499 token stream. Otherwise, commit to the tokens we have consumed.
25500 Returns true if no error occurred; false otherwise. */
25502 static bool
25503 cp_parser_parse_definitely (cp_parser* parser)
25505 bool error_occurred;
25506 cp_parser_context *context;
25508 /* Remember whether or not an error occurred, since we are about to
25509 destroy that information. */
25510 error_occurred = cp_parser_error_occurred (parser);
25511 /* Remove the topmost context from the stack. */
25512 context = parser->context;
25513 parser->context = context->next;
25514 /* If no parse errors occurred, commit to the tentative parse. */
25515 if (!error_occurred)
25517 /* Commit to the tokens read tentatively, unless that was
25518 already done. */
25519 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25520 cp_lexer_commit_tokens (parser->lexer);
25522 pop_to_parent_deferring_access_checks ();
25524 /* Otherwise, if errors occurred, roll back our state so that things
25525 are just as they were before we began the tentative parse. */
25526 else
25528 cp_lexer_rollback_tokens (parser->lexer);
25529 pop_deferring_access_checks ();
25531 /* Add the context to the front of the free list. */
25532 context->next = cp_parser_context_free_list;
25533 cp_parser_context_free_list = context;
25535 return !error_occurred;
25538 /* Returns true if we are parsing tentatively and are not committed to
25539 this tentative parse. */
25541 static bool
25542 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25544 return (cp_parser_parsing_tentatively (parser)
25545 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25548 /* Returns nonzero iff an error has occurred during the most recent
25549 tentative parse. */
25551 static bool
25552 cp_parser_error_occurred (cp_parser* parser)
25554 return (cp_parser_parsing_tentatively (parser)
25555 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25558 /* Returns nonzero if GNU extensions are allowed. */
25560 static bool
25561 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25563 return parser->allow_gnu_extensions_p;
25566 /* Objective-C++ Productions */
25569 /* Parse an Objective-C expression, which feeds into a primary-expression
25570 above.
25572 objc-expression:
25573 objc-message-expression
25574 objc-string-literal
25575 objc-encode-expression
25576 objc-protocol-expression
25577 objc-selector-expression
25579 Returns a tree representation of the expression. */
25581 static tree
25582 cp_parser_objc_expression (cp_parser* parser)
25584 /* Try to figure out what kind of declaration is present. */
25585 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25587 switch (kwd->type)
25589 case CPP_OPEN_SQUARE:
25590 return cp_parser_objc_message_expression (parser);
25592 case CPP_OBJC_STRING:
25593 kwd = cp_lexer_consume_token (parser->lexer);
25594 return objc_build_string_object (kwd->u.value);
25596 case CPP_KEYWORD:
25597 switch (kwd->keyword)
25599 case RID_AT_ENCODE:
25600 return cp_parser_objc_encode_expression (parser);
25602 case RID_AT_PROTOCOL:
25603 return cp_parser_objc_protocol_expression (parser);
25605 case RID_AT_SELECTOR:
25606 return cp_parser_objc_selector_expression (parser);
25608 default:
25609 break;
25611 default:
25612 error_at (kwd->location,
25613 "misplaced %<@%D%> Objective-C++ construct",
25614 kwd->u.value);
25615 cp_parser_skip_to_end_of_block_or_statement (parser);
25618 return error_mark_node;
25621 /* Parse an Objective-C message expression.
25623 objc-message-expression:
25624 [ objc-message-receiver objc-message-args ]
25626 Returns a representation of an Objective-C message. */
25628 static tree
25629 cp_parser_objc_message_expression (cp_parser* parser)
25631 tree receiver, messageargs;
25633 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25634 receiver = cp_parser_objc_message_receiver (parser);
25635 messageargs = cp_parser_objc_message_args (parser);
25636 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25638 return objc_build_message_expr (receiver, messageargs);
25641 /* Parse an objc-message-receiver.
25643 objc-message-receiver:
25644 expression
25645 simple-type-specifier
25647 Returns a representation of the type or expression. */
25649 static tree
25650 cp_parser_objc_message_receiver (cp_parser* parser)
25652 tree rcv;
25654 /* An Objective-C message receiver may be either (1) a type
25655 or (2) an expression. */
25656 cp_parser_parse_tentatively (parser);
25657 rcv = cp_parser_expression (parser);
25659 /* If that worked out, fine. */
25660 if (cp_parser_parse_definitely (parser))
25661 return rcv;
25663 cp_parser_parse_tentatively (parser);
25664 rcv = cp_parser_simple_type_specifier (parser,
25665 /*decl_specs=*/NULL,
25666 CP_PARSER_FLAGS_NONE);
25668 if (cp_parser_parse_definitely (parser))
25669 return objc_get_class_reference (rcv);
25671 cp_parser_error (parser, "objective-c++ message receiver expected");
25672 return error_mark_node;
25675 /* Parse the arguments and selectors comprising an Objective-C message.
25677 objc-message-args:
25678 objc-selector
25679 objc-selector-args
25680 objc-selector-args , objc-comma-args
25682 objc-selector-args:
25683 objc-selector [opt] : assignment-expression
25684 objc-selector-args objc-selector [opt] : assignment-expression
25686 objc-comma-args:
25687 assignment-expression
25688 objc-comma-args , assignment-expression
25690 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25691 selector arguments and TREE_VALUE containing a list of comma
25692 arguments. */
25694 static tree
25695 cp_parser_objc_message_args (cp_parser* parser)
25697 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25698 bool maybe_unary_selector_p = true;
25699 cp_token *token = cp_lexer_peek_token (parser->lexer);
25701 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25703 tree selector = NULL_TREE, arg;
25705 if (token->type != CPP_COLON)
25706 selector = cp_parser_objc_selector (parser);
25708 /* Detect if we have a unary selector. */
25709 if (maybe_unary_selector_p
25710 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25711 return build_tree_list (selector, NULL_TREE);
25713 maybe_unary_selector_p = false;
25714 cp_parser_require (parser, CPP_COLON, RT_COLON);
25715 arg = cp_parser_assignment_expression (parser);
25717 sel_args
25718 = chainon (sel_args,
25719 build_tree_list (selector, arg));
25721 token = cp_lexer_peek_token (parser->lexer);
25724 /* Handle non-selector arguments, if any. */
25725 while (token->type == CPP_COMMA)
25727 tree arg;
25729 cp_lexer_consume_token (parser->lexer);
25730 arg = cp_parser_assignment_expression (parser);
25732 addl_args
25733 = chainon (addl_args,
25734 build_tree_list (NULL_TREE, arg));
25736 token = cp_lexer_peek_token (parser->lexer);
25739 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25741 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25742 return build_tree_list (error_mark_node, error_mark_node);
25745 return build_tree_list (sel_args, addl_args);
25748 /* Parse an Objective-C encode expression.
25750 objc-encode-expression:
25751 @encode objc-typename
25753 Returns an encoded representation of the type argument. */
25755 static tree
25756 cp_parser_objc_encode_expression (cp_parser* parser)
25758 tree type;
25759 cp_token *token;
25761 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25762 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25763 token = cp_lexer_peek_token (parser->lexer);
25764 type = complete_type (cp_parser_type_id (parser));
25765 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25767 if (!type)
25769 error_at (token->location,
25770 "%<@encode%> must specify a type as an argument");
25771 return error_mark_node;
25774 /* This happens if we find @encode(T) (where T is a template
25775 typename or something dependent on a template typename) when
25776 parsing a template. In that case, we can't compile it
25777 immediately, but we rather create an AT_ENCODE_EXPR which will
25778 need to be instantiated when the template is used.
25780 if (dependent_type_p (type))
25782 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25783 TREE_READONLY (value) = 1;
25784 return value;
25787 return objc_build_encode_expr (type);
25790 /* Parse an Objective-C @defs expression. */
25792 static tree
25793 cp_parser_objc_defs_expression (cp_parser *parser)
25795 tree name;
25797 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25798 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25799 name = cp_parser_identifier (parser);
25800 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25802 return objc_get_class_ivars (name);
25805 /* Parse an Objective-C protocol expression.
25807 objc-protocol-expression:
25808 @protocol ( identifier )
25810 Returns a representation of the protocol expression. */
25812 static tree
25813 cp_parser_objc_protocol_expression (cp_parser* parser)
25815 tree proto;
25817 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25818 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25819 proto = cp_parser_identifier (parser);
25820 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25822 return objc_build_protocol_expr (proto);
25825 /* Parse an Objective-C selector expression.
25827 objc-selector-expression:
25828 @selector ( objc-method-signature )
25830 objc-method-signature:
25831 objc-selector
25832 objc-selector-seq
25834 objc-selector-seq:
25835 objc-selector :
25836 objc-selector-seq objc-selector :
25838 Returns a representation of the method selector. */
25840 static tree
25841 cp_parser_objc_selector_expression (cp_parser* parser)
25843 tree sel_seq = NULL_TREE;
25844 bool maybe_unary_selector_p = true;
25845 cp_token *token;
25846 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25848 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25849 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25850 token = cp_lexer_peek_token (parser->lexer);
25852 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25853 || token->type == CPP_SCOPE)
25855 tree selector = NULL_TREE;
25857 if (token->type != CPP_COLON
25858 || token->type == CPP_SCOPE)
25859 selector = cp_parser_objc_selector (parser);
25861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25862 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25864 /* Detect if we have a unary selector. */
25865 if (maybe_unary_selector_p)
25867 sel_seq = selector;
25868 goto finish_selector;
25870 else
25872 cp_parser_error (parser, "expected %<:%>");
25875 maybe_unary_selector_p = false;
25876 token = cp_lexer_consume_token (parser->lexer);
25878 if (token->type == CPP_SCOPE)
25880 sel_seq
25881 = chainon (sel_seq,
25882 build_tree_list (selector, NULL_TREE));
25883 sel_seq
25884 = chainon (sel_seq,
25885 build_tree_list (NULL_TREE, NULL_TREE));
25887 else
25888 sel_seq
25889 = chainon (sel_seq,
25890 build_tree_list (selector, NULL_TREE));
25892 token = cp_lexer_peek_token (parser->lexer);
25895 finish_selector:
25896 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25898 return objc_build_selector_expr (loc, sel_seq);
25901 /* Parse a list of identifiers.
25903 objc-identifier-list:
25904 identifier
25905 objc-identifier-list , identifier
25907 Returns a TREE_LIST of identifier nodes. */
25909 static tree
25910 cp_parser_objc_identifier_list (cp_parser* parser)
25912 tree identifier;
25913 tree list;
25914 cp_token *sep;
25916 identifier = cp_parser_identifier (parser);
25917 if (identifier == error_mark_node)
25918 return error_mark_node;
25920 list = build_tree_list (NULL_TREE, identifier);
25921 sep = cp_lexer_peek_token (parser->lexer);
25923 while (sep->type == CPP_COMMA)
25925 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25926 identifier = cp_parser_identifier (parser);
25927 if (identifier == error_mark_node)
25928 return list;
25930 list = chainon (list, build_tree_list (NULL_TREE,
25931 identifier));
25932 sep = cp_lexer_peek_token (parser->lexer);
25935 return list;
25938 /* Parse an Objective-C alias declaration.
25940 objc-alias-declaration:
25941 @compatibility_alias identifier identifier ;
25943 This function registers the alias mapping with the Objective-C front end.
25944 It returns nothing. */
25946 static void
25947 cp_parser_objc_alias_declaration (cp_parser* parser)
25949 tree alias, orig;
25951 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25952 alias = cp_parser_identifier (parser);
25953 orig = cp_parser_identifier (parser);
25954 objc_declare_alias (alias, orig);
25955 cp_parser_consume_semicolon_at_end_of_statement (parser);
25958 /* Parse an Objective-C class forward-declaration.
25960 objc-class-declaration:
25961 @class objc-identifier-list ;
25963 The function registers the forward declarations with the Objective-C
25964 front end. It returns nothing. */
25966 static void
25967 cp_parser_objc_class_declaration (cp_parser* parser)
25969 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25970 while (true)
25972 tree id;
25974 id = cp_parser_identifier (parser);
25975 if (id == error_mark_node)
25976 break;
25978 objc_declare_class (id);
25980 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25981 cp_lexer_consume_token (parser->lexer);
25982 else
25983 break;
25985 cp_parser_consume_semicolon_at_end_of_statement (parser);
25988 /* Parse a list of Objective-C protocol references.
25990 objc-protocol-refs-opt:
25991 objc-protocol-refs [opt]
25993 objc-protocol-refs:
25994 < objc-identifier-list >
25996 Returns a TREE_LIST of identifiers, if any. */
25998 static tree
25999 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26001 tree protorefs = NULL_TREE;
26003 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26005 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
26006 protorefs = cp_parser_objc_identifier_list (parser);
26007 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26010 return protorefs;
26013 /* Parse a Objective-C visibility specification. */
26015 static void
26016 cp_parser_objc_visibility_spec (cp_parser* parser)
26018 cp_token *vis = cp_lexer_peek_token (parser->lexer);
26020 switch (vis->keyword)
26022 case RID_AT_PRIVATE:
26023 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26024 break;
26025 case RID_AT_PROTECTED:
26026 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26027 break;
26028 case RID_AT_PUBLIC:
26029 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26030 break;
26031 case RID_AT_PACKAGE:
26032 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26033 break;
26034 default:
26035 return;
26038 /* Eat '@private'/'@protected'/'@public'. */
26039 cp_lexer_consume_token (parser->lexer);
26042 /* Parse an Objective-C method type. Return 'true' if it is a class
26043 (+) method, and 'false' if it is an instance (-) method. */
26045 static inline bool
26046 cp_parser_objc_method_type (cp_parser* parser)
26048 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26049 return true;
26050 else
26051 return false;
26054 /* Parse an Objective-C protocol qualifier. */
26056 static tree
26057 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26059 tree quals = NULL_TREE, node;
26060 cp_token *token = cp_lexer_peek_token (parser->lexer);
26062 node = token->u.value;
26064 while (node && identifier_p (node)
26065 && (node == ridpointers [(int) RID_IN]
26066 || node == ridpointers [(int) RID_OUT]
26067 || node == ridpointers [(int) RID_INOUT]
26068 || node == ridpointers [(int) RID_BYCOPY]
26069 || node == ridpointers [(int) RID_BYREF]
26070 || node == ridpointers [(int) RID_ONEWAY]))
26072 quals = tree_cons (NULL_TREE, node, quals);
26073 cp_lexer_consume_token (parser->lexer);
26074 token = cp_lexer_peek_token (parser->lexer);
26075 node = token->u.value;
26078 return quals;
26081 /* Parse an Objective-C typename. */
26083 static tree
26084 cp_parser_objc_typename (cp_parser* parser)
26086 tree type_name = NULL_TREE;
26088 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26090 tree proto_quals, cp_type = NULL_TREE;
26092 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26093 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26095 /* An ObjC type name may consist of just protocol qualifiers, in which
26096 case the type shall default to 'id'. */
26097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26099 cp_type = cp_parser_type_id (parser);
26101 /* If the type could not be parsed, an error has already
26102 been produced. For error recovery, behave as if it had
26103 not been specified, which will use the default type
26104 'id'. */
26105 if (cp_type == error_mark_node)
26107 cp_type = NULL_TREE;
26108 /* We need to skip to the closing parenthesis as
26109 cp_parser_type_id() does not seem to do it for
26110 us. */
26111 cp_parser_skip_to_closing_parenthesis (parser,
26112 /*recovering=*/true,
26113 /*or_comma=*/false,
26114 /*consume_paren=*/false);
26118 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26119 type_name = build_tree_list (proto_quals, cp_type);
26122 return type_name;
26125 /* Check to see if TYPE refers to an Objective-C selector name. */
26127 static bool
26128 cp_parser_objc_selector_p (enum cpp_ttype type)
26130 return (type == CPP_NAME || type == CPP_KEYWORD
26131 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26132 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26133 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26134 || type == CPP_XOR || type == CPP_XOR_EQ);
26137 /* Parse an Objective-C selector. */
26139 static tree
26140 cp_parser_objc_selector (cp_parser* parser)
26142 cp_token *token = cp_lexer_consume_token (parser->lexer);
26144 if (!cp_parser_objc_selector_p (token->type))
26146 error_at (token->location, "invalid Objective-C++ selector name");
26147 return error_mark_node;
26150 /* C++ operator names are allowed to appear in ObjC selectors. */
26151 switch (token->type)
26153 case CPP_AND_AND: return get_identifier ("and");
26154 case CPP_AND_EQ: return get_identifier ("and_eq");
26155 case CPP_AND: return get_identifier ("bitand");
26156 case CPP_OR: return get_identifier ("bitor");
26157 case CPP_COMPL: return get_identifier ("compl");
26158 case CPP_NOT: return get_identifier ("not");
26159 case CPP_NOT_EQ: return get_identifier ("not_eq");
26160 case CPP_OR_OR: return get_identifier ("or");
26161 case CPP_OR_EQ: return get_identifier ("or_eq");
26162 case CPP_XOR: return get_identifier ("xor");
26163 case CPP_XOR_EQ: return get_identifier ("xor_eq");
26164 default: return token->u.value;
26168 /* Parse an Objective-C params list. */
26170 static tree
26171 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26173 tree params = NULL_TREE;
26174 bool maybe_unary_selector_p = true;
26175 cp_token *token = cp_lexer_peek_token (parser->lexer);
26177 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26179 tree selector = NULL_TREE, type_name, identifier;
26180 tree parm_attr = NULL_TREE;
26182 if (token->keyword == RID_ATTRIBUTE)
26183 break;
26185 if (token->type != CPP_COLON)
26186 selector = cp_parser_objc_selector (parser);
26188 /* Detect if we have a unary selector. */
26189 if (maybe_unary_selector_p
26190 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26192 params = selector; /* Might be followed by attributes. */
26193 break;
26196 maybe_unary_selector_p = false;
26197 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26199 /* Something went quite wrong. There should be a colon
26200 here, but there is not. Stop parsing parameters. */
26201 break;
26203 type_name = cp_parser_objc_typename (parser);
26204 /* New ObjC allows attributes on parameters too. */
26205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26206 parm_attr = cp_parser_attributes_opt (parser);
26207 identifier = cp_parser_identifier (parser);
26209 params
26210 = chainon (params,
26211 objc_build_keyword_decl (selector,
26212 type_name,
26213 identifier,
26214 parm_attr));
26216 token = cp_lexer_peek_token (parser->lexer);
26219 if (params == NULL_TREE)
26221 cp_parser_error (parser, "objective-c++ method declaration is expected");
26222 return error_mark_node;
26225 /* We allow tail attributes for the method. */
26226 if (token->keyword == RID_ATTRIBUTE)
26228 *attributes = cp_parser_attributes_opt (parser);
26229 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26230 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26231 return params;
26232 cp_parser_error (parser,
26233 "method attributes must be specified at the end");
26234 return error_mark_node;
26237 if (params == NULL_TREE)
26239 cp_parser_error (parser, "objective-c++ method declaration is expected");
26240 return error_mark_node;
26242 return params;
26245 /* Parse the non-keyword Objective-C params. */
26247 static tree
26248 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
26249 tree* attributes)
26251 tree params = make_node (TREE_LIST);
26252 cp_token *token = cp_lexer_peek_token (parser->lexer);
26253 *ellipsisp = false; /* Initially, assume no ellipsis. */
26255 while (token->type == CPP_COMMA)
26257 cp_parameter_declarator *parmdecl;
26258 tree parm;
26260 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26261 token = cp_lexer_peek_token (parser->lexer);
26263 if (token->type == CPP_ELLIPSIS)
26265 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26266 *ellipsisp = true;
26267 token = cp_lexer_peek_token (parser->lexer);
26268 break;
26271 /* TODO: parse attributes for tail parameters. */
26272 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26273 parm = grokdeclarator (parmdecl->declarator,
26274 &parmdecl->decl_specifiers,
26275 PARM, /*initialized=*/0,
26276 /*attrlist=*/NULL);
26278 chainon (params, build_tree_list (NULL_TREE, parm));
26279 token = cp_lexer_peek_token (parser->lexer);
26282 /* We allow tail attributes for the method. */
26283 if (token->keyword == RID_ATTRIBUTE)
26285 if (*attributes == NULL_TREE)
26287 *attributes = cp_parser_attributes_opt (parser);
26288 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26289 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26290 return params;
26292 else
26293 /* We have an error, but parse the attributes, so that we can
26294 carry on. */
26295 *attributes = cp_parser_attributes_opt (parser);
26297 cp_parser_error (parser,
26298 "method attributes must be specified at the end");
26299 return error_mark_node;
26302 return params;
26305 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26307 static void
26308 cp_parser_objc_interstitial_code (cp_parser* parser)
26310 cp_token *token = cp_lexer_peek_token (parser->lexer);
26312 /* If the next token is `extern' and the following token is a string
26313 literal, then we have a linkage specification. */
26314 if (token->keyword == RID_EXTERN
26315 && cp_parser_is_pure_string_literal
26316 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26317 cp_parser_linkage_specification (parser);
26318 /* Handle #pragma, if any. */
26319 else if (token->type == CPP_PRAGMA)
26320 cp_parser_pragma (parser, pragma_objc_icode);
26321 /* Allow stray semicolons. */
26322 else if (token->type == CPP_SEMICOLON)
26323 cp_lexer_consume_token (parser->lexer);
26324 /* Mark methods as optional or required, when building protocols. */
26325 else if (token->keyword == RID_AT_OPTIONAL)
26327 cp_lexer_consume_token (parser->lexer);
26328 objc_set_method_opt (true);
26330 else if (token->keyword == RID_AT_REQUIRED)
26332 cp_lexer_consume_token (parser->lexer);
26333 objc_set_method_opt (false);
26335 else if (token->keyword == RID_NAMESPACE)
26336 cp_parser_namespace_definition (parser);
26337 /* Other stray characters must generate errors. */
26338 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26340 cp_lexer_consume_token (parser->lexer);
26341 error ("stray %qs between Objective-C++ methods",
26342 token->type == CPP_OPEN_BRACE ? "{" : "}");
26344 /* Finally, try to parse a block-declaration, or a function-definition. */
26345 else
26346 cp_parser_block_declaration (parser, /*statement_p=*/false);
26349 /* Parse a method signature. */
26351 static tree
26352 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26354 tree rettype, kwdparms, optparms;
26355 bool ellipsis = false;
26356 bool is_class_method;
26358 is_class_method = cp_parser_objc_method_type (parser);
26359 rettype = cp_parser_objc_typename (parser);
26360 *attributes = NULL_TREE;
26361 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26362 if (kwdparms == error_mark_node)
26363 return error_mark_node;
26364 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26365 if (optparms == error_mark_node)
26366 return error_mark_node;
26368 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26371 static bool
26372 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26374 tree tattr;
26375 cp_lexer_save_tokens (parser->lexer);
26376 tattr = cp_parser_attributes_opt (parser);
26377 gcc_assert (tattr) ;
26379 /* If the attributes are followed by a method introducer, this is not allowed.
26380 Dump the attributes and flag the situation. */
26381 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26382 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26383 return true;
26385 /* Otherwise, the attributes introduce some interstitial code, possibly so
26386 rewind to allow that check. */
26387 cp_lexer_rollback_tokens (parser->lexer);
26388 return false;
26391 /* Parse an Objective-C method prototype list. */
26393 static void
26394 cp_parser_objc_method_prototype_list (cp_parser* parser)
26396 cp_token *token = cp_lexer_peek_token (parser->lexer);
26398 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26400 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26402 tree attributes, sig;
26403 bool is_class_method;
26404 if (token->type == CPP_PLUS)
26405 is_class_method = true;
26406 else
26407 is_class_method = false;
26408 sig = cp_parser_objc_method_signature (parser, &attributes);
26409 if (sig == error_mark_node)
26411 cp_parser_skip_to_end_of_block_or_statement (parser);
26412 token = cp_lexer_peek_token (parser->lexer);
26413 continue;
26415 objc_add_method_declaration (is_class_method, sig, attributes);
26416 cp_parser_consume_semicolon_at_end_of_statement (parser);
26418 else if (token->keyword == RID_AT_PROPERTY)
26419 cp_parser_objc_at_property_declaration (parser);
26420 else if (token->keyword == RID_ATTRIBUTE
26421 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26422 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26423 OPT_Wattributes,
26424 "prefix attributes are ignored for methods");
26425 else
26426 /* Allow for interspersed non-ObjC++ code. */
26427 cp_parser_objc_interstitial_code (parser);
26429 token = cp_lexer_peek_token (parser->lexer);
26432 if (token->type != CPP_EOF)
26433 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26434 else
26435 cp_parser_error (parser, "expected %<@end%>");
26437 objc_finish_interface ();
26440 /* Parse an Objective-C method definition list. */
26442 static void
26443 cp_parser_objc_method_definition_list (cp_parser* parser)
26445 cp_token *token = cp_lexer_peek_token (parser->lexer);
26447 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26449 tree meth;
26451 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26453 cp_token *ptk;
26454 tree sig, attribute;
26455 bool is_class_method;
26456 if (token->type == CPP_PLUS)
26457 is_class_method = true;
26458 else
26459 is_class_method = false;
26460 push_deferring_access_checks (dk_deferred);
26461 sig = cp_parser_objc_method_signature (parser, &attribute);
26462 if (sig == error_mark_node)
26464 cp_parser_skip_to_end_of_block_or_statement (parser);
26465 token = cp_lexer_peek_token (parser->lexer);
26466 continue;
26468 objc_start_method_definition (is_class_method, sig, attribute,
26469 NULL_TREE);
26471 /* For historical reasons, we accept an optional semicolon. */
26472 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26473 cp_lexer_consume_token (parser->lexer);
26475 ptk = cp_lexer_peek_token (parser->lexer);
26476 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26477 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26479 perform_deferred_access_checks (tf_warning_or_error);
26480 stop_deferring_access_checks ();
26481 meth = cp_parser_function_definition_after_declarator (parser,
26482 false);
26483 pop_deferring_access_checks ();
26484 objc_finish_method_definition (meth);
26487 /* The following case will be removed once @synthesize is
26488 completely implemented. */
26489 else if (token->keyword == RID_AT_PROPERTY)
26490 cp_parser_objc_at_property_declaration (parser);
26491 else if (token->keyword == RID_AT_SYNTHESIZE)
26492 cp_parser_objc_at_synthesize_declaration (parser);
26493 else if (token->keyword == RID_AT_DYNAMIC)
26494 cp_parser_objc_at_dynamic_declaration (parser);
26495 else if (token->keyword == RID_ATTRIBUTE
26496 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26497 warning_at (token->location, OPT_Wattributes,
26498 "prefix attributes are ignored for methods");
26499 else
26500 /* Allow for interspersed non-ObjC++ code. */
26501 cp_parser_objc_interstitial_code (parser);
26503 token = cp_lexer_peek_token (parser->lexer);
26506 if (token->type != CPP_EOF)
26507 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26508 else
26509 cp_parser_error (parser, "expected %<@end%>");
26511 objc_finish_implementation ();
26514 /* Parse Objective-C ivars. */
26516 static void
26517 cp_parser_objc_class_ivars (cp_parser* parser)
26519 cp_token *token = cp_lexer_peek_token (parser->lexer);
26521 if (token->type != CPP_OPEN_BRACE)
26522 return; /* No ivars specified. */
26524 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26525 token = cp_lexer_peek_token (parser->lexer);
26527 while (token->type != CPP_CLOSE_BRACE
26528 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26530 cp_decl_specifier_seq declspecs;
26531 int decl_class_or_enum_p;
26532 tree prefix_attributes;
26534 cp_parser_objc_visibility_spec (parser);
26536 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26537 break;
26539 cp_parser_decl_specifier_seq (parser,
26540 CP_PARSER_FLAGS_OPTIONAL,
26541 &declspecs,
26542 &decl_class_or_enum_p);
26544 /* auto, register, static, extern, mutable. */
26545 if (declspecs.storage_class != sc_none)
26547 cp_parser_error (parser, "invalid type for instance variable");
26548 declspecs.storage_class = sc_none;
26551 /* thread_local. */
26552 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26554 cp_parser_error (parser, "invalid type for instance variable");
26555 declspecs.locations[ds_thread] = 0;
26558 /* typedef. */
26559 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26561 cp_parser_error (parser, "invalid type for instance variable");
26562 declspecs.locations[ds_typedef] = 0;
26565 prefix_attributes = declspecs.attributes;
26566 declspecs.attributes = NULL_TREE;
26568 /* Keep going until we hit the `;' at the end of the
26569 declaration. */
26570 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26572 tree width = NULL_TREE, attributes, first_attribute, decl;
26573 cp_declarator *declarator = NULL;
26574 int ctor_dtor_or_conv_p;
26576 /* Check for a (possibly unnamed) bitfield declaration. */
26577 token = cp_lexer_peek_token (parser->lexer);
26578 if (token->type == CPP_COLON)
26579 goto eat_colon;
26581 if (token->type == CPP_NAME
26582 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26583 == CPP_COLON))
26585 /* Get the name of the bitfield. */
26586 declarator = make_id_declarator (NULL_TREE,
26587 cp_parser_identifier (parser),
26588 sfk_none);
26590 eat_colon:
26591 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26592 /* Get the width of the bitfield. */
26593 width
26594 = cp_parser_constant_expression (parser);
26596 else
26598 /* Parse the declarator. */
26599 declarator
26600 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26601 &ctor_dtor_or_conv_p,
26602 /*parenthesized_p=*/NULL,
26603 /*member_p=*/false,
26604 /*friend_p=*/false);
26607 /* Look for attributes that apply to the ivar. */
26608 attributes = cp_parser_attributes_opt (parser);
26609 /* Remember which attributes are prefix attributes and
26610 which are not. */
26611 first_attribute = attributes;
26612 /* Combine the attributes. */
26613 attributes = chainon (prefix_attributes, attributes);
26615 if (width)
26616 /* Create the bitfield declaration. */
26617 decl = grokbitfield (declarator, &declspecs,
26618 width,
26619 attributes);
26620 else
26621 decl = grokfield (declarator, &declspecs,
26622 NULL_TREE, /*init_const_expr_p=*/false,
26623 NULL_TREE, attributes);
26625 /* Add the instance variable. */
26626 if (decl != error_mark_node && decl != NULL_TREE)
26627 objc_add_instance_variable (decl);
26629 /* Reset PREFIX_ATTRIBUTES. */
26630 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26631 attributes = TREE_CHAIN (attributes);
26632 if (attributes)
26633 TREE_CHAIN (attributes) = NULL_TREE;
26635 token = cp_lexer_peek_token (parser->lexer);
26637 if (token->type == CPP_COMMA)
26639 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26640 continue;
26642 break;
26645 cp_parser_consume_semicolon_at_end_of_statement (parser);
26646 token = cp_lexer_peek_token (parser->lexer);
26649 if (token->keyword == RID_AT_END)
26650 cp_parser_error (parser, "expected %<}%>");
26652 /* Do not consume the RID_AT_END, so it will be read again as terminating
26653 the @interface of @implementation. */
26654 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26655 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26657 /* For historical reasons, we accept an optional semicolon. */
26658 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26659 cp_lexer_consume_token (parser->lexer);
26662 /* Parse an Objective-C protocol declaration. */
26664 static void
26665 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26667 tree proto, protorefs;
26668 cp_token *tok;
26670 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26673 tok = cp_lexer_peek_token (parser->lexer);
26674 error_at (tok->location, "identifier expected after %<@protocol%>");
26675 cp_parser_consume_semicolon_at_end_of_statement (parser);
26676 return;
26679 /* See if we have a forward declaration or a definition. */
26680 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26682 /* Try a forward declaration first. */
26683 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26685 while (true)
26687 tree id;
26689 id = cp_parser_identifier (parser);
26690 if (id == error_mark_node)
26691 break;
26693 objc_declare_protocol (id, attributes);
26695 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26696 cp_lexer_consume_token (parser->lexer);
26697 else
26698 break;
26700 cp_parser_consume_semicolon_at_end_of_statement (parser);
26703 /* Ok, we got a full-fledged definition (or at least should). */
26704 else
26706 proto = cp_parser_identifier (parser);
26707 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26708 objc_start_protocol (proto, protorefs, attributes);
26709 cp_parser_objc_method_prototype_list (parser);
26713 /* Parse an Objective-C superclass or category. */
26715 static void
26716 cp_parser_objc_superclass_or_category (cp_parser *parser,
26717 bool iface_p,
26718 tree *super,
26719 tree *categ, bool *is_class_extension)
26721 cp_token *next = cp_lexer_peek_token (parser->lexer);
26723 *super = *categ = NULL_TREE;
26724 *is_class_extension = false;
26725 if (next->type == CPP_COLON)
26727 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26728 *super = cp_parser_identifier (parser);
26730 else if (next->type == CPP_OPEN_PAREN)
26732 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26734 /* If there is no category name, and this is an @interface, we
26735 have a class extension. */
26736 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26738 *categ = NULL_TREE;
26739 *is_class_extension = true;
26741 else
26742 *categ = cp_parser_identifier (parser);
26744 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26748 /* Parse an Objective-C class interface. */
26750 static void
26751 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26753 tree name, super, categ, protos;
26754 bool is_class_extension;
26756 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26757 name = cp_parser_identifier (parser);
26758 if (name == error_mark_node)
26760 /* It's hard to recover because even if valid @interface stuff
26761 is to follow, we can't compile it (or validate it) if we
26762 don't even know which class it refers to. Let's assume this
26763 was a stray '@interface' token in the stream and skip it.
26765 return;
26767 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26768 &is_class_extension);
26769 protos = cp_parser_objc_protocol_refs_opt (parser);
26771 /* We have either a class or a category on our hands. */
26772 if (categ || is_class_extension)
26773 objc_start_category_interface (name, categ, protos, attributes);
26774 else
26776 objc_start_class_interface (name, super, protos, attributes);
26777 /* Handle instance variable declarations, if any. */
26778 cp_parser_objc_class_ivars (parser);
26779 objc_continue_interface ();
26782 cp_parser_objc_method_prototype_list (parser);
26785 /* Parse an Objective-C class implementation. */
26787 static void
26788 cp_parser_objc_class_implementation (cp_parser* parser)
26790 tree name, super, categ;
26791 bool is_class_extension;
26793 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26794 name = cp_parser_identifier (parser);
26795 if (name == error_mark_node)
26797 /* It's hard to recover because even if valid @implementation
26798 stuff is to follow, we can't compile it (or validate it) if
26799 we don't even know which class it refers to. Let's assume
26800 this was a stray '@implementation' token in the stream and
26801 skip it.
26803 return;
26805 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26806 &is_class_extension);
26808 /* We have either a class or a category on our hands. */
26809 if (categ)
26810 objc_start_category_implementation (name, categ);
26811 else
26813 objc_start_class_implementation (name, super);
26814 /* Handle instance variable declarations, if any. */
26815 cp_parser_objc_class_ivars (parser);
26816 objc_continue_implementation ();
26819 cp_parser_objc_method_definition_list (parser);
26822 /* Consume the @end token and finish off the implementation. */
26824 static void
26825 cp_parser_objc_end_implementation (cp_parser* parser)
26827 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26828 objc_finish_implementation ();
26831 /* Parse an Objective-C declaration. */
26833 static void
26834 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26836 /* Try to figure out what kind of declaration is present. */
26837 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26839 if (attributes)
26840 switch (kwd->keyword)
26842 case RID_AT_ALIAS:
26843 case RID_AT_CLASS:
26844 case RID_AT_END:
26845 error_at (kwd->location, "attributes may not be specified before"
26846 " the %<@%D%> Objective-C++ keyword",
26847 kwd->u.value);
26848 attributes = NULL;
26849 break;
26850 case RID_AT_IMPLEMENTATION:
26851 warning_at (kwd->location, OPT_Wattributes,
26852 "prefix attributes are ignored before %<@%D%>",
26853 kwd->u.value);
26854 attributes = NULL;
26855 default:
26856 break;
26859 switch (kwd->keyword)
26861 case RID_AT_ALIAS:
26862 cp_parser_objc_alias_declaration (parser);
26863 break;
26864 case RID_AT_CLASS:
26865 cp_parser_objc_class_declaration (parser);
26866 break;
26867 case RID_AT_PROTOCOL:
26868 cp_parser_objc_protocol_declaration (parser, attributes);
26869 break;
26870 case RID_AT_INTERFACE:
26871 cp_parser_objc_class_interface (parser, attributes);
26872 break;
26873 case RID_AT_IMPLEMENTATION:
26874 cp_parser_objc_class_implementation (parser);
26875 break;
26876 case RID_AT_END:
26877 cp_parser_objc_end_implementation (parser);
26878 break;
26879 default:
26880 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26881 kwd->u.value);
26882 cp_parser_skip_to_end_of_block_or_statement (parser);
26886 /* Parse an Objective-C try-catch-finally statement.
26888 objc-try-catch-finally-stmt:
26889 @try compound-statement objc-catch-clause-seq [opt]
26890 objc-finally-clause [opt]
26892 objc-catch-clause-seq:
26893 objc-catch-clause objc-catch-clause-seq [opt]
26895 objc-catch-clause:
26896 @catch ( objc-exception-declaration ) compound-statement
26898 objc-finally-clause:
26899 @finally compound-statement
26901 objc-exception-declaration:
26902 parameter-declaration
26903 '...'
26905 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26907 Returns NULL_TREE.
26909 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26910 for C. Keep them in sync. */
26912 static tree
26913 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26915 location_t location;
26916 tree stmt;
26918 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26919 location = cp_lexer_peek_token (parser->lexer)->location;
26920 objc_maybe_warn_exceptions (location);
26921 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26922 node, lest it get absorbed into the surrounding block. */
26923 stmt = push_stmt_list ();
26924 cp_parser_compound_statement (parser, NULL, false, false);
26925 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26927 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26929 cp_parameter_declarator *parm;
26930 tree parameter_declaration = error_mark_node;
26931 bool seen_open_paren = false;
26933 cp_lexer_consume_token (parser->lexer);
26934 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26935 seen_open_paren = true;
26936 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26938 /* We have "@catch (...)" (where the '...' are literally
26939 what is in the code). Skip the '...'.
26940 parameter_declaration is set to NULL_TREE, and
26941 objc_being_catch_clauses() knows that that means
26942 '...'. */
26943 cp_lexer_consume_token (parser->lexer);
26944 parameter_declaration = NULL_TREE;
26946 else
26948 /* We have "@catch (NSException *exception)" or something
26949 like that. Parse the parameter declaration. */
26950 parm = cp_parser_parameter_declaration (parser, false, NULL);
26951 if (parm == NULL)
26952 parameter_declaration = error_mark_node;
26953 else
26954 parameter_declaration = grokdeclarator (parm->declarator,
26955 &parm->decl_specifiers,
26956 PARM, /*initialized=*/0,
26957 /*attrlist=*/NULL);
26959 if (seen_open_paren)
26960 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26961 else
26963 /* If there was no open parenthesis, we are recovering from
26964 an error, and we are trying to figure out what mistake
26965 the user has made. */
26967 /* If there is an immediate closing parenthesis, the user
26968 probably forgot the opening one (ie, they typed "@catch
26969 NSException *e)". Parse the closing parenthesis and keep
26970 going. */
26971 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26972 cp_lexer_consume_token (parser->lexer);
26974 /* If these is no immediate closing parenthesis, the user
26975 probably doesn't know that parenthesis are required at
26976 all (ie, they typed "@catch NSException *e"). So, just
26977 forget about the closing parenthesis and keep going. */
26979 objc_begin_catch_clause (parameter_declaration);
26980 cp_parser_compound_statement (parser, NULL, false, false);
26981 objc_finish_catch_clause ();
26983 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26985 cp_lexer_consume_token (parser->lexer);
26986 location = cp_lexer_peek_token (parser->lexer)->location;
26987 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26988 node, lest it get absorbed into the surrounding block. */
26989 stmt = push_stmt_list ();
26990 cp_parser_compound_statement (parser, NULL, false, false);
26991 objc_build_finally_clause (location, pop_stmt_list (stmt));
26994 return objc_finish_try_stmt ();
26997 /* Parse an Objective-C synchronized statement.
26999 objc-synchronized-stmt:
27000 @synchronized ( expression ) compound-statement
27002 Returns NULL_TREE. */
27004 static tree
27005 cp_parser_objc_synchronized_statement (cp_parser *parser)
27007 location_t location;
27008 tree lock, stmt;
27010 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27012 location = cp_lexer_peek_token (parser->lexer)->location;
27013 objc_maybe_warn_exceptions (location);
27014 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27015 lock = cp_parser_expression (parser);
27016 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27018 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27019 node, lest it get absorbed into the surrounding block. */
27020 stmt = push_stmt_list ();
27021 cp_parser_compound_statement (parser, NULL, false, false);
27023 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27026 /* Parse an Objective-C throw statement.
27028 objc-throw-stmt:
27029 @throw assignment-expression [opt] ;
27031 Returns a constructed '@throw' statement. */
27033 static tree
27034 cp_parser_objc_throw_statement (cp_parser *parser)
27036 tree expr = NULL_TREE;
27037 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27039 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27042 expr = cp_parser_expression (parser);
27044 cp_parser_consume_semicolon_at_end_of_statement (parser);
27046 return objc_build_throw_stmt (loc, expr);
27049 /* Parse an Objective-C statement. */
27051 static tree
27052 cp_parser_objc_statement (cp_parser * parser)
27054 /* Try to figure out what kind of declaration is present. */
27055 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27057 switch (kwd->keyword)
27059 case RID_AT_TRY:
27060 return cp_parser_objc_try_catch_finally_statement (parser);
27061 case RID_AT_SYNCHRONIZED:
27062 return cp_parser_objc_synchronized_statement (parser);
27063 case RID_AT_THROW:
27064 return cp_parser_objc_throw_statement (parser);
27065 default:
27066 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27067 kwd->u.value);
27068 cp_parser_skip_to_end_of_block_or_statement (parser);
27071 return error_mark_node;
27074 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
27075 look ahead to see if an objc keyword follows the attributes. This
27076 is to detect the use of prefix attributes on ObjC @interface and
27077 @protocol. */
27079 static bool
27080 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27082 cp_lexer_save_tokens (parser->lexer);
27083 *attrib = cp_parser_attributes_opt (parser);
27084 gcc_assert (*attrib);
27085 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27087 cp_lexer_commit_tokens (parser->lexer);
27088 return true;
27090 cp_lexer_rollback_tokens (parser->lexer);
27091 return false;
27094 /* This routine is a minimal replacement for
27095 c_parser_struct_declaration () used when parsing the list of
27096 types/names or ObjC++ properties. For example, when parsing the
27097 code
27099 @property (readonly) int a, b, c;
27101 this function is responsible for parsing "int a, int b, int c" and
27102 returning the declarations as CHAIN of DECLs.
27104 TODO: Share this code with cp_parser_objc_class_ivars. It's very
27105 similar parsing. */
27106 static tree
27107 cp_parser_objc_struct_declaration (cp_parser *parser)
27109 tree decls = NULL_TREE;
27110 cp_decl_specifier_seq declspecs;
27111 int decl_class_or_enum_p;
27112 tree prefix_attributes;
27114 cp_parser_decl_specifier_seq (parser,
27115 CP_PARSER_FLAGS_NONE,
27116 &declspecs,
27117 &decl_class_or_enum_p);
27119 if (declspecs.type == error_mark_node)
27120 return error_mark_node;
27122 /* auto, register, static, extern, mutable. */
27123 if (declspecs.storage_class != sc_none)
27125 cp_parser_error (parser, "invalid type for property");
27126 declspecs.storage_class = sc_none;
27129 /* thread_local. */
27130 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27132 cp_parser_error (parser, "invalid type for property");
27133 declspecs.locations[ds_thread] = 0;
27136 /* typedef. */
27137 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27139 cp_parser_error (parser, "invalid type for property");
27140 declspecs.locations[ds_typedef] = 0;
27143 prefix_attributes = declspecs.attributes;
27144 declspecs.attributes = NULL_TREE;
27146 /* Keep going until we hit the `;' at the end of the declaration. */
27147 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27149 tree attributes, first_attribute, decl;
27150 cp_declarator *declarator;
27151 cp_token *token;
27153 /* Parse the declarator. */
27154 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27155 NULL, NULL, false, false);
27157 /* Look for attributes that apply to the ivar. */
27158 attributes = cp_parser_attributes_opt (parser);
27159 /* Remember which attributes are prefix attributes and
27160 which are not. */
27161 first_attribute = attributes;
27162 /* Combine the attributes. */
27163 attributes = chainon (prefix_attributes, attributes);
27165 decl = grokfield (declarator, &declspecs,
27166 NULL_TREE, /*init_const_expr_p=*/false,
27167 NULL_TREE, attributes);
27169 if (decl == error_mark_node || decl == NULL_TREE)
27170 return error_mark_node;
27172 /* Reset PREFIX_ATTRIBUTES. */
27173 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27174 attributes = TREE_CHAIN (attributes);
27175 if (attributes)
27176 TREE_CHAIN (attributes) = NULL_TREE;
27178 DECL_CHAIN (decl) = decls;
27179 decls = decl;
27181 token = cp_lexer_peek_token (parser->lexer);
27182 if (token->type == CPP_COMMA)
27184 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
27185 continue;
27187 else
27188 break;
27190 return decls;
27193 /* Parse an Objective-C @property declaration. The syntax is:
27195 objc-property-declaration:
27196 '@property' objc-property-attributes[opt] struct-declaration ;
27198 objc-property-attributes:
27199 '(' objc-property-attribute-list ')'
27201 objc-property-attribute-list:
27202 objc-property-attribute
27203 objc-property-attribute-list, objc-property-attribute
27205 objc-property-attribute
27206 'getter' = identifier
27207 'setter' = identifier
27208 'readonly'
27209 'readwrite'
27210 'assign'
27211 'retain'
27212 'copy'
27213 'nonatomic'
27215 For example:
27216 @property NSString *name;
27217 @property (readonly) id object;
27218 @property (retain, nonatomic, getter=getTheName) id name;
27219 @property int a, b, c;
27221 PS: This function is identical to
27222 c_parser_objc_at_property_declaration for C. Keep them in sync. */
27223 static void
27224 cp_parser_objc_at_property_declaration (cp_parser *parser)
27226 /* The following variables hold the attributes of the properties as
27227 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
27228 seen. When we see an attribute, we set them to 'true' (if they
27229 are boolean properties) or to the identifier (if they have an
27230 argument, ie, for getter and setter). Note that here we only
27231 parse the list of attributes, check the syntax and accumulate the
27232 attributes that we find. objc_add_property_declaration() will
27233 then process the information. */
27234 bool property_assign = false;
27235 bool property_copy = false;
27236 tree property_getter_ident = NULL_TREE;
27237 bool property_nonatomic = false;
27238 bool property_readonly = false;
27239 bool property_readwrite = false;
27240 bool property_retain = false;
27241 tree property_setter_ident = NULL_TREE;
27243 /* 'properties' is the list of properties that we read. Usually a
27244 single one, but maybe more (eg, in "@property int a, b, c;" there
27245 are three). */
27246 tree properties;
27247 location_t loc;
27249 loc = cp_lexer_peek_token (parser->lexer)->location;
27251 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
27253 /* Parse the optional attribute list... */
27254 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27256 /* Eat the '('. */
27257 cp_lexer_consume_token (parser->lexer);
27259 while (true)
27261 bool syntax_error = false;
27262 cp_token *token = cp_lexer_peek_token (parser->lexer);
27263 enum rid keyword;
27265 if (token->type != CPP_NAME)
27267 cp_parser_error (parser, "expected identifier");
27268 break;
27270 keyword = C_RID_CODE (token->u.value);
27271 cp_lexer_consume_token (parser->lexer);
27272 switch (keyword)
27274 case RID_ASSIGN: property_assign = true; break;
27275 case RID_COPY: property_copy = true; break;
27276 case RID_NONATOMIC: property_nonatomic = true; break;
27277 case RID_READONLY: property_readonly = true; break;
27278 case RID_READWRITE: property_readwrite = true; break;
27279 case RID_RETAIN: property_retain = true; break;
27281 case RID_GETTER:
27282 case RID_SETTER:
27283 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27285 if (keyword == RID_GETTER)
27286 cp_parser_error (parser,
27287 "missing %<=%> (after %<getter%> attribute)");
27288 else
27289 cp_parser_error (parser,
27290 "missing %<=%> (after %<setter%> attribute)");
27291 syntax_error = true;
27292 break;
27294 cp_lexer_consume_token (parser->lexer); /* eat the = */
27295 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27297 cp_parser_error (parser, "expected identifier");
27298 syntax_error = true;
27299 break;
27301 if (keyword == RID_SETTER)
27303 if (property_setter_ident != NULL_TREE)
27305 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27306 cp_lexer_consume_token (parser->lexer);
27308 else
27309 property_setter_ident = cp_parser_objc_selector (parser);
27310 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27311 cp_parser_error (parser, "setter name must terminate with %<:%>");
27312 else
27313 cp_lexer_consume_token (parser->lexer);
27315 else
27317 if (property_getter_ident != NULL_TREE)
27319 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27320 cp_lexer_consume_token (parser->lexer);
27322 else
27323 property_getter_ident = cp_parser_objc_selector (parser);
27325 break;
27326 default:
27327 cp_parser_error (parser, "unknown property attribute");
27328 syntax_error = true;
27329 break;
27332 if (syntax_error)
27333 break;
27335 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27336 cp_lexer_consume_token (parser->lexer);
27337 else
27338 break;
27341 /* FIXME: "@property (setter, assign);" will generate a spurious
27342 "error: expected ‘)’ before ‘,’ token". This is because
27343 cp_parser_require, unlike the C counterpart, will produce an
27344 error even if we are in error recovery. */
27345 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27347 cp_parser_skip_to_closing_parenthesis (parser,
27348 /*recovering=*/true,
27349 /*or_comma=*/false,
27350 /*consume_paren=*/true);
27354 /* ... and the property declaration(s). */
27355 properties = cp_parser_objc_struct_declaration (parser);
27357 if (properties == error_mark_node)
27359 cp_parser_skip_to_end_of_statement (parser);
27360 /* If the next token is now a `;', consume it. */
27361 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27362 cp_lexer_consume_token (parser->lexer);
27363 return;
27366 if (properties == NULL_TREE)
27367 cp_parser_error (parser, "expected identifier");
27368 else
27370 /* Comma-separated properties are chained together in
27371 reverse order; add them one by one. */
27372 properties = nreverse (properties);
27374 for (; properties; properties = TREE_CHAIN (properties))
27375 objc_add_property_declaration (loc, copy_node (properties),
27376 property_readonly, property_readwrite,
27377 property_assign, property_retain,
27378 property_copy, property_nonatomic,
27379 property_getter_ident, property_setter_ident);
27382 cp_parser_consume_semicolon_at_end_of_statement (parser);
27385 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27387 objc-synthesize-declaration:
27388 @synthesize objc-synthesize-identifier-list ;
27390 objc-synthesize-identifier-list:
27391 objc-synthesize-identifier
27392 objc-synthesize-identifier-list, objc-synthesize-identifier
27394 objc-synthesize-identifier
27395 identifier
27396 identifier = identifier
27398 For example:
27399 @synthesize MyProperty;
27400 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27402 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27403 for C. Keep them in sync.
27405 static void
27406 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27408 tree list = NULL_TREE;
27409 location_t loc;
27410 loc = cp_lexer_peek_token (parser->lexer)->location;
27412 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27413 while (true)
27415 tree property, ivar;
27416 property = cp_parser_identifier (parser);
27417 if (property == error_mark_node)
27419 cp_parser_consume_semicolon_at_end_of_statement (parser);
27420 return;
27422 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27424 cp_lexer_consume_token (parser->lexer);
27425 ivar = cp_parser_identifier (parser);
27426 if (ivar == error_mark_node)
27428 cp_parser_consume_semicolon_at_end_of_statement (parser);
27429 return;
27432 else
27433 ivar = NULL_TREE;
27434 list = chainon (list, build_tree_list (ivar, property));
27435 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27436 cp_lexer_consume_token (parser->lexer);
27437 else
27438 break;
27440 cp_parser_consume_semicolon_at_end_of_statement (parser);
27441 objc_add_synthesize_declaration (loc, list);
27444 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27446 objc-dynamic-declaration:
27447 @dynamic identifier-list ;
27449 For example:
27450 @dynamic MyProperty;
27451 @dynamic MyProperty, AnotherProperty;
27453 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27454 for C. Keep them in sync.
27456 static void
27457 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27459 tree list = NULL_TREE;
27460 location_t loc;
27461 loc = cp_lexer_peek_token (parser->lexer)->location;
27463 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27464 while (true)
27466 tree property;
27467 property = cp_parser_identifier (parser);
27468 if (property == error_mark_node)
27470 cp_parser_consume_semicolon_at_end_of_statement (parser);
27471 return;
27473 list = chainon (list, build_tree_list (NULL, property));
27474 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27475 cp_lexer_consume_token (parser->lexer);
27476 else
27477 break;
27479 cp_parser_consume_semicolon_at_end_of_statement (parser);
27480 objc_add_dynamic_declaration (loc, list);
27484 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27486 /* Returns name of the next clause.
27487 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27488 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27489 returned and the token is consumed. */
27491 static pragma_omp_clause
27492 cp_parser_omp_clause_name (cp_parser *parser)
27494 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27496 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27497 result = PRAGMA_OMP_CLAUSE_IF;
27498 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27499 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27500 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27501 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27502 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27503 result = PRAGMA_OMP_CLAUSE_FOR;
27504 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27506 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27507 const char *p = IDENTIFIER_POINTER (id);
27509 switch (p[0])
27511 case 'a':
27512 if (!strcmp ("aligned", p))
27513 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27514 break;
27515 case 'c':
27516 if (!strcmp ("collapse", p))
27517 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27518 else if (!strcmp ("copyin", p))
27519 result = PRAGMA_OMP_CLAUSE_COPYIN;
27520 else if (!strcmp ("copyprivate", p))
27521 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27522 break;
27523 case 'd':
27524 if (!strcmp ("depend", p))
27525 result = PRAGMA_OMP_CLAUSE_DEPEND;
27526 else if (!strcmp ("device", p))
27527 result = PRAGMA_OMP_CLAUSE_DEVICE;
27528 else if (!strcmp ("dist_schedule", p))
27529 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27530 break;
27531 case 'f':
27532 if (!strcmp ("final", p))
27533 result = PRAGMA_OMP_CLAUSE_FINAL;
27534 else if (!strcmp ("firstprivate", p))
27535 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27536 else if (!strcmp ("from", p))
27537 result = PRAGMA_OMP_CLAUSE_FROM;
27538 break;
27539 case 'i':
27540 if (!strcmp ("inbranch", p))
27541 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27542 break;
27543 case 'l':
27544 if (!strcmp ("lastprivate", p))
27545 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27546 else if (!strcmp ("linear", p))
27547 result = PRAGMA_OMP_CLAUSE_LINEAR;
27548 break;
27549 case 'm':
27550 if (!strcmp ("map", p))
27551 result = PRAGMA_OMP_CLAUSE_MAP;
27552 else if (!strcmp ("mergeable", p))
27553 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27554 else if (flag_cilkplus && !strcmp ("mask", p))
27555 result = PRAGMA_CILK_CLAUSE_MASK;
27556 break;
27557 case 'n':
27558 if (!strcmp ("notinbranch", p))
27559 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27560 else if (!strcmp ("nowait", p))
27561 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27562 else if (flag_cilkplus && !strcmp ("nomask", p))
27563 result = PRAGMA_CILK_CLAUSE_NOMASK;
27564 else if (!strcmp ("num_teams", p))
27565 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27566 else if (!strcmp ("num_threads", p))
27567 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27568 break;
27569 case 'o':
27570 if (!strcmp ("ordered", p))
27571 result = PRAGMA_OMP_CLAUSE_ORDERED;
27572 break;
27573 case 'p':
27574 if (!strcmp ("parallel", p))
27575 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27576 else if (!strcmp ("proc_bind", p))
27577 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27578 break;
27579 case 'r':
27580 if (!strcmp ("reduction", p))
27581 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27582 break;
27583 case 's':
27584 if (!strcmp ("safelen", p))
27585 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27586 else if (!strcmp ("schedule", p))
27587 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27588 else if (!strcmp ("sections", p))
27589 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27590 else if (!strcmp ("shared", p))
27591 result = PRAGMA_OMP_CLAUSE_SHARED;
27592 else if (!strcmp ("simdlen", p))
27593 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27594 break;
27595 case 't':
27596 if (!strcmp ("taskgroup", p))
27597 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27598 else if (!strcmp ("thread_limit", p))
27599 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27600 else if (!strcmp ("to", p))
27601 result = PRAGMA_OMP_CLAUSE_TO;
27602 break;
27603 case 'u':
27604 if (!strcmp ("uniform", p))
27605 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27606 else if (!strcmp ("untied", p))
27607 result = PRAGMA_OMP_CLAUSE_UNTIED;
27608 break;
27609 case 'v':
27610 if (flag_cilkplus && !strcmp ("vectorlength", p))
27611 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27612 break;
27616 if (result != PRAGMA_OMP_CLAUSE_NONE)
27617 cp_lexer_consume_token (parser->lexer);
27619 return result;
27622 /* Validate that a clause of the given type does not already exist. */
27624 static void
27625 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27626 const char *name, location_t location)
27628 tree c;
27630 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27631 if (OMP_CLAUSE_CODE (c) == code)
27633 error_at (location, "too many %qs clauses", name);
27634 break;
27638 /* OpenMP 2.5:
27639 variable-list:
27640 identifier
27641 variable-list , identifier
27643 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27644 colon). An opening parenthesis will have been consumed by the caller.
27646 If KIND is nonzero, create the appropriate node and install the decl
27647 in OMP_CLAUSE_DECL and add the node to the head of the list.
27649 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27650 return the list created.
27652 COLON can be NULL if only closing parenthesis should end the list,
27653 or pointer to bool which will receive false if the list is terminated
27654 by closing parenthesis or true if the list is terminated by colon. */
27656 static tree
27657 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27658 tree list, bool *colon)
27660 cp_token *token;
27661 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27662 if (colon)
27664 parser->colon_corrects_to_scope_p = false;
27665 *colon = false;
27667 while (1)
27669 tree name, decl;
27671 token = cp_lexer_peek_token (parser->lexer);
27672 name = cp_parser_id_expression (parser, /*template_p=*/false,
27673 /*check_dependency_p=*/true,
27674 /*template_p=*/NULL,
27675 /*declarator_p=*/false,
27676 /*optional_p=*/false);
27677 if (name == error_mark_node)
27678 goto skip_comma;
27680 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27681 if (decl == error_mark_node)
27682 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27683 token->location);
27684 else if (kind != 0)
27686 switch (kind)
27688 case OMP_CLAUSE_MAP:
27689 case OMP_CLAUSE_FROM:
27690 case OMP_CLAUSE_TO:
27691 case OMP_CLAUSE_DEPEND:
27692 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27694 tree low_bound = NULL_TREE, length = NULL_TREE;
27696 parser->colon_corrects_to_scope_p = false;
27697 cp_lexer_consume_token (parser->lexer);
27698 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27699 low_bound = cp_parser_expression (parser);
27700 if (!colon)
27701 parser->colon_corrects_to_scope_p
27702 = saved_colon_corrects_to_scope_p;
27703 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27704 length = integer_one_node;
27705 else
27707 /* Look for `:'. */
27708 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27709 goto skip_comma;
27710 if (!cp_lexer_next_token_is (parser->lexer,
27711 CPP_CLOSE_SQUARE))
27712 length = cp_parser_expression (parser);
27714 /* Look for the closing `]'. */
27715 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27716 RT_CLOSE_SQUARE))
27717 goto skip_comma;
27718 decl = tree_cons (low_bound, length, decl);
27720 break;
27721 default:
27722 break;
27725 tree u = build_omp_clause (token->location, kind);
27726 OMP_CLAUSE_DECL (u) = decl;
27727 OMP_CLAUSE_CHAIN (u) = list;
27728 list = u;
27730 else
27731 list = tree_cons (decl, NULL_TREE, list);
27733 get_comma:
27734 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27735 break;
27736 cp_lexer_consume_token (parser->lexer);
27739 if (colon)
27740 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27742 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27744 *colon = true;
27745 cp_parser_require (parser, CPP_COLON, RT_COLON);
27746 return list;
27749 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27751 int ending;
27753 /* Try to resync to an unnested comma. Copied from
27754 cp_parser_parenthesized_expression_list. */
27755 skip_comma:
27756 if (colon)
27757 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27758 ending = cp_parser_skip_to_closing_parenthesis (parser,
27759 /*recovering=*/true,
27760 /*or_comma=*/true,
27761 /*consume_paren=*/true);
27762 if (ending < 0)
27763 goto get_comma;
27766 return list;
27769 /* Similarly, but expect leading and trailing parenthesis. This is a very
27770 common case for omp clauses. */
27772 static tree
27773 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27775 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27776 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27777 return list;
27780 /* OpenMP 3.0:
27781 collapse ( constant-expression ) */
27783 static tree
27784 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27786 tree c, num;
27787 location_t loc;
27788 HOST_WIDE_INT n;
27790 loc = cp_lexer_peek_token (parser->lexer)->location;
27791 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27792 return list;
27794 num = cp_parser_constant_expression (parser);
27796 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27797 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27798 /*or_comma=*/false,
27799 /*consume_paren=*/true);
27801 if (num == error_mark_node)
27802 return list;
27803 num = fold_non_dependent_expr (num);
27804 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27805 || !tree_fits_shwi_p (num)
27806 || (n = tree_to_shwi (num)) <= 0
27807 || (int) n != n)
27809 error_at (loc, "collapse argument needs positive constant integer expression");
27810 return list;
27813 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27814 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27815 OMP_CLAUSE_CHAIN (c) = list;
27816 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27818 return c;
27821 /* OpenMP 2.5:
27822 default ( shared | none ) */
27824 static tree
27825 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27827 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27828 tree c;
27830 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27831 return list;
27832 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27834 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27835 const char *p = IDENTIFIER_POINTER (id);
27837 switch (p[0])
27839 case 'n':
27840 if (strcmp ("none", p) != 0)
27841 goto invalid_kind;
27842 kind = OMP_CLAUSE_DEFAULT_NONE;
27843 break;
27845 case 's':
27846 if (strcmp ("shared", p) != 0)
27847 goto invalid_kind;
27848 kind = OMP_CLAUSE_DEFAULT_SHARED;
27849 break;
27851 default:
27852 goto invalid_kind;
27855 cp_lexer_consume_token (parser->lexer);
27857 else
27859 invalid_kind:
27860 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27863 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27864 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27865 /*or_comma=*/false,
27866 /*consume_paren=*/true);
27868 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27869 return list;
27871 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27872 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27873 OMP_CLAUSE_CHAIN (c) = list;
27874 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27876 return c;
27879 /* OpenMP 3.1:
27880 final ( expression ) */
27882 static tree
27883 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27885 tree t, c;
27887 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27888 return list;
27890 t = cp_parser_condition (parser);
27892 if (t == error_mark_node
27893 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27894 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27895 /*or_comma=*/false,
27896 /*consume_paren=*/true);
27898 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27900 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27901 OMP_CLAUSE_FINAL_EXPR (c) = t;
27902 OMP_CLAUSE_CHAIN (c) = list;
27904 return c;
27907 /* OpenMP 2.5:
27908 if ( expression ) */
27910 static tree
27911 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27913 tree t, c;
27915 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27916 return list;
27918 t = cp_parser_condition (parser);
27920 if (t == error_mark_node
27921 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27922 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27923 /*or_comma=*/false,
27924 /*consume_paren=*/true);
27926 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27928 c = build_omp_clause (location, OMP_CLAUSE_IF);
27929 OMP_CLAUSE_IF_EXPR (c) = t;
27930 OMP_CLAUSE_CHAIN (c) = list;
27932 return c;
27935 /* OpenMP 3.1:
27936 mergeable */
27938 static tree
27939 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27940 tree list, location_t location)
27942 tree c;
27944 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27945 location);
27947 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27948 OMP_CLAUSE_CHAIN (c) = list;
27949 return c;
27952 /* OpenMP 2.5:
27953 nowait */
27955 static tree
27956 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27957 tree list, location_t location)
27959 tree c;
27961 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27963 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27964 OMP_CLAUSE_CHAIN (c) = list;
27965 return c;
27968 /* OpenMP 2.5:
27969 num_threads ( expression ) */
27971 static tree
27972 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27973 location_t location)
27975 tree t, c;
27977 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27978 return list;
27980 t = cp_parser_expression (parser);
27982 if (t == error_mark_node
27983 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27984 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27985 /*or_comma=*/false,
27986 /*consume_paren=*/true);
27988 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27989 "num_threads", location);
27991 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27992 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27993 OMP_CLAUSE_CHAIN (c) = list;
27995 return c;
27998 /* OpenMP 2.5:
27999 ordered */
28001 static tree
28002 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28003 tree list, location_t location)
28005 tree c;
28007 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28008 "ordered", location);
28010 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28011 OMP_CLAUSE_CHAIN (c) = list;
28012 return c;
28015 /* OpenMP 2.5:
28016 reduction ( reduction-operator : variable-list )
28018 reduction-operator:
28019 One of: + * - & ^ | && ||
28021 OpenMP 3.1:
28023 reduction-operator:
28024 One of: + * - & ^ | && || min max
28026 OpenMP 4.0:
28028 reduction-operator:
28029 One of: + * - & ^ | && ||
28030 id-expression */
28032 static tree
28033 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28035 enum tree_code code = ERROR_MARK;
28036 tree nlist, c, id = NULL_TREE;
28038 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28039 return list;
28041 switch (cp_lexer_peek_token (parser->lexer)->type)
28043 case CPP_PLUS: code = PLUS_EXPR; break;
28044 case CPP_MULT: code = MULT_EXPR; break;
28045 case CPP_MINUS: code = MINUS_EXPR; break;
28046 case CPP_AND: code = BIT_AND_EXPR; break;
28047 case CPP_XOR: code = BIT_XOR_EXPR; break;
28048 case CPP_OR: code = BIT_IOR_EXPR; break;
28049 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28050 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28051 default: break;
28054 if (code != ERROR_MARK)
28055 cp_lexer_consume_token (parser->lexer);
28056 else
28058 bool saved_colon_corrects_to_scope_p;
28059 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28060 parser->colon_corrects_to_scope_p = false;
28061 id = cp_parser_id_expression (parser, /*template_p=*/false,
28062 /*check_dependency_p=*/true,
28063 /*template_p=*/NULL,
28064 /*declarator_p=*/false,
28065 /*optional_p=*/false);
28066 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28067 if (identifier_p (id))
28069 const char *p = IDENTIFIER_POINTER (id);
28071 if (strcmp (p, "min") == 0)
28072 code = MIN_EXPR;
28073 else if (strcmp (p, "max") == 0)
28074 code = MAX_EXPR;
28075 else if (id == ansi_opname (PLUS_EXPR))
28076 code = PLUS_EXPR;
28077 else if (id == ansi_opname (MULT_EXPR))
28078 code = MULT_EXPR;
28079 else if (id == ansi_opname (MINUS_EXPR))
28080 code = MINUS_EXPR;
28081 else if (id == ansi_opname (BIT_AND_EXPR))
28082 code = BIT_AND_EXPR;
28083 else if (id == ansi_opname (BIT_IOR_EXPR))
28084 code = BIT_IOR_EXPR;
28085 else if (id == ansi_opname (BIT_XOR_EXPR))
28086 code = BIT_XOR_EXPR;
28087 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28088 code = TRUTH_ANDIF_EXPR;
28089 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28090 code = TRUTH_ORIF_EXPR;
28091 id = omp_reduction_id (code, id, NULL_TREE);
28092 tree scope = parser->scope;
28093 if (scope)
28094 id = build_qualified_name (NULL_TREE, scope, id, false);
28095 parser->scope = NULL_TREE;
28096 parser->qualifying_scope = NULL_TREE;
28097 parser->object_scope = NULL_TREE;
28099 else
28101 error ("invalid reduction-identifier");
28102 resync_fail:
28103 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28104 /*or_comma=*/false,
28105 /*consume_paren=*/true);
28106 return list;
28110 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28111 goto resync_fail;
28113 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28114 NULL);
28115 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28117 OMP_CLAUSE_REDUCTION_CODE (c) = code;
28118 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28121 return nlist;
28124 /* OpenMP 2.5:
28125 schedule ( schedule-kind )
28126 schedule ( schedule-kind , expression )
28128 schedule-kind:
28129 static | dynamic | guided | runtime | auto */
28131 static tree
28132 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28134 tree c, t;
28136 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28137 return list;
28139 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28141 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28143 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28144 const char *p = IDENTIFIER_POINTER (id);
28146 switch (p[0])
28148 case 'd':
28149 if (strcmp ("dynamic", p) != 0)
28150 goto invalid_kind;
28151 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28152 break;
28154 case 'g':
28155 if (strcmp ("guided", p) != 0)
28156 goto invalid_kind;
28157 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28158 break;
28160 case 'r':
28161 if (strcmp ("runtime", p) != 0)
28162 goto invalid_kind;
28163 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28164 break;
28166 default:
28167 goto invalid_kind;
28170 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28171 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28172 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28173 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28174 else
28175 goto invalid_kind;
28176 cp_lexer_consume_token (parser->lexer);
28178 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28180 cp_token *token;
28181 cp_lexer_consume_token (parser->lexer);
28183 token = cp_lexer_peek_token (parser->lexer);
28184 t = cp_parser_assignment_expression (parser);
28186 if (t == error_mark_node)
28187 goto resync_fail;
28188 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28189 error_at (token->location, "schedule %<runtime%> does not take "
28190 "a %<chunk_size%> parameter");
28191 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28192 error_at (token->location, "schedule %<auto%> does not take "
28193 "a %<chunk_size%> parameter");
28194 else
28195 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28197 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28198 goto resync_fail;
28200 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28201 goto resync_fail;
28203 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28204 OMP_CLAUSE_CHAIN (c) = list;
28205 return c;
28207 invalid_kind:
28208 cp_parser_error (parser, "invalid schedule kind");
28209 resync_fail:
28210 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28211 /*or_comma=*/false,
28212 /*consume_paren=*/true);
28213 return list;
28216 /* OpenMP 3.0:
28217 untied */
28219 static tree
28220 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28221 tree list, location_t location)
28223 tree c;
28225 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28227 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28228 OMP_CLAUSE_CHAIN (c) = list;
28229 return c;
28232 /* OpenMP 4.0:
28233 inbranch
28234 notinbranch */
28236 static tree
28237 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28238 tree list, location_t location)
28240 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28241 tree c = build_omp_clause (location, code);
28242 OMP_CLAUSE_CHAIN (c) = list;
28243 return c;
28246 /* OpenMP 4.0:
28247 parallel
28249 sections
28250 taskgroup */
28252 static tree
28253 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28254 enum omp_clause_code code,
28255 tree list, location_t location)
28257 tree c = build_omp_clause (location, code);
28258 OMP_CLAUSE_CHAIN (c) = list;
28259 return c;
28262 /* OpenMP 4.0:
28263 num_teams ( expression ) */
28265 static tree
28266 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28267 location_t location)
28269 tree t, c;
28271 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28272 return list;
28274 t = cp_parser_expression (parser);
28276 if (t == error_mark_node
28277 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28278 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28279 /*or_comma=*/false,
28280 /*consume_paren=*/true);
28282 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28283 "num_teams", location);
28285 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28286 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28287 OMP_CLAUSE_CHAIN (c) = list;
28289 return c;
28292 /* OpenMP 4.0:
28293 thread_limit ( expression ) */
28295 static tree
28296 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28297 location_t location)
28299 tree t, c;
28301 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28302 return list;
28304 t = cp_parser_expression (parser);
28306 if (t == error_mark_node
28307 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28308 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28309 /*or_comma=*/false,
28310 /*consume_paren=*/true);
28312 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28313 "thread_limit", location);
28315 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28316 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28317 OMP_CLAUSE_CHAIN (c) = list;
28319 return c;
28322 /* OpenMP 4.0:
28323 aligned ( variable-list )
28324 aligned ( variable-list : constant-expression ) */
28326 static tree
28327 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28329 tree nlist, c, alignment = NULL_TREE;
28330 bool colon;
28332 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28333 return list;
28335 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28336 &colon);
28338 if (colon)
28340 alignment = cp_parser_constant_expression (parser);
28342 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28343 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28344 /*or_comma=*/false,
28345 /*consume_paren=*/true);
28347 if (alignment == error_mark_node)
28348 alignment = NULL_TREE;
28351 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28352 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28354 return nlist;
28357 /* OpenMP 4.0:
28358 linear ( variable-list )
28359 linear ( variable-list : expression ) */
28361 static tree
28362 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28363 bool is_cilk_simd_fn)
28365 tree nlist, c, step = integer_one_node;
28366 bool colon;
28368 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28369 return list;
28371 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28372 &colon);
28374 if (colon)
28376 step = cp_parser_expression (parser);
28378 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28380 sorry ("using parameters for %<linear%> step is not supported yet");
28381 step = integer_one_node;
28383 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28384 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28385 /*or_comma=*/false,
28386 /*consume_paren=*/true);
28388 if (step == error_mark_node)
28389 return list;
28392 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28393 OMP_CLAUSE_LINEAR_STEP (c) = step;
28395 return nlist;
28398 /* OpenMP 4.0:
28399 safelen ( constant-expression ) */
28401 static tree
28402 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28403 location_t location)
28405 tree t, c;
28407 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28408 return list;
28410 t = cp_parser_constant_expression (parser);
28412 if (t == error_mark_node
28413 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28414 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28415 /*or_comma=*/false,
28416 /*consume_paren=*/true);
28418 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28420 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28421 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28422 OMP_CLAUSE_CHAIN (c) = list;
28424 return c;
28427 /* OpenMP 4.0:
28428 simdlen ( constant-expression ) */
28430 static tree
28431 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28432 location_t location)
28434 tree t, c;
28436 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28437 return list;
28439 t = cp_parser_constant_expression (parser);
28441 if (t == error_mark_node
28442 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28443 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28444 /*or_comma=*/false,
28445 /*consume_paren=*/true);
28447 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28449 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28450 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28451 OMP_CLAUSE_CHAIN (c) = list;
28453 return c;
28456 /* OpenMP 4.0:
28457 depend ( depend-kind : variable-list )
28459 depend-kind:
28460 in | out | inout */
28462 static tree
28463 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28465 tree nlist, c;
28466 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28468 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28469 return list;
28471 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28473 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28474 const char *p = IDENTIFIER_POINTER (id);
28476 if (strcmp ("in", p) == 0)
28477 kind = OMP_CLAUSE_DEPEND_IN;
28478 else if (strcmp ("inout", p) == 0)
28479 kind = OMP_CLAUSE_DEPEND_INOUT;
28480 else if (strcmp ("out", p) == 0)
28481 kind = OMP_CLAUSE_DEPEND_OUT;
28482 else
28483 goto invalid_kind;
28485 else
28486 goto invalid_kind;
28488 cp_lexer_consume_token (parser->lexer);
28489 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28490 goto resync_fail;
28492 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28493 NULL);
28495 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28496 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28498 return nlist;
28500 invalid_kind:
28501 cp_parser_error (parser, "invalid depend kind");
28502 resync_fail:
28503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28504 /*or_comma=*/false,
28505 /*consume_paren=*/true);
28506 return list;
28509 /* OpenMP 4.0:
28510 map ( map-kind : variable-list )
28511 map ( variable-list )
28513 map-kind:
28514 alloc | to | from | tofrom */
28516 static tree
28517 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28519 tree nlist, c;
28520 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28522 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28523 return list;
28525 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28526 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28528 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28529 const char *p = IDENTIFIER_POINTER (id);
28531 if (strcmp ("alloc", p) == 0)
28532 kind = OMP_CLAUSE_MAP_ALLOC;
28533 else if (strcmp ("to", p) == 0)
28534 kind = OMP_CLAUSE_MAP_TO;
28535 else if (strcmp ("from", p) == 0)
28536 kind = OMP_CLAUSE_MAP_FROM;
28537 else if (strcmp ("tofrom", p) == 0)
28538 kind = OMP_CLAUSE_MAP_TOFROM;
28539 else
28541 cp_parser_error (parser, "invalid map kind");
28542 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28543 /*or_comma=*/false,
28544 /*consume_paren=*/true);
28545 return list;
28547 cp_lexer_consume_token (parser->lexer);
28548 cp_lexer_consume_token (parser->lexer);
28551 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28552 NULL);
28554 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28555 OMP_CLAUSE_MAP_KIND (c) = kind;
28557 return nlist;
28560 /* OpenMP 4.0:
28561 device ( expression ) */
28563 static tree
28564 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28565 location_t location)
28567 tree t, c;
28569 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28570 return list;
28572 t = cp_parser_expression (parser);
28574 if (t == error_mark_node
28575 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28577 /*or_comma=*/false,
28578 /*consume_paren=*/true);
28580 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28581 "device", location);
28583 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28584 OMP_CLAUSE_DEVICE_ID (c) = t;
28585 OMP_CLAUSE_CHAIN (c) = list;
28587 return c;
28590 /* OpenMP 4.0:
28591 dist_schedule ( static )
28592 dist_schedule ( static , expression ) */
28594 static tree
28595 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28596 location_t location)
28598 tree c, t;
28600 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28601 return list;
28603 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28605 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28606 goto invalid_kind;
28607 cp_lexer_consume_token (parser->lexer);
28609 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28611 cp_lexer_consume_token (parser->lexer);
28613 t = cp_parser_assignment_expression (parser);
28615 if (t == error_mark_node)
28616 goto resync_fail;
28617 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28619 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28620 goto resync_fail;
28622 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28623 goto resync_fail;
28625 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28626 location);
28627 OMP_CLAUSE_CHAIN (c) = list;
28628 return c;
28630 invalid_kind:
28631 cp_parser_error (parser, "invalid dist_schedule kind");
28632 resync_fail:
28633 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28634 /*or_comma=*/false,
28635 /*consume_paren=*/true);
28636 return list;
28639 /* OpenMP 4.0:
28640 proc_bind ( proc-bind-kind )
28642 proc-bind-kind:
28643 master | close | spread */
28645 static tree
28646 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28647 location_t location)
28649 tree c;
28650 enum omp_clause_proc_bind_kind kind;
28652 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28653 return list;
28655 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28657 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28658 const char *p = IDENTIFIER_POINTER (id);
28660 if (strcmp ("master", p) == 0)
28661 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28662 else if (strcmp ("close", p) == 0)
28663 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28664 else if (strcmp ("spread", p) == 0)
28665 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28666 else
28667 goto invalid_kind;
28669 else
28670 goto invalid_kind;
28672 cp_lexer_consume_token (parser->lexer);
28673 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28674 goto resync_fail;
28676 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28677 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28678 location);
28679 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28680 OMP_CLAUSE_CHAIN (c) = list;
28681 return c;
28683 invalid_kind:
28684 cp_parser_error (parser, "invalid depend kind");
28685 resync_fail:
28686 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28687 /*or_comma=*/false,
28688 /*consume_paren=*/true);
28689 return list;
28692 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28693 is a bitmask in MASK. Return the list of clauses found; the result
28694 of clause default goes in *pdefault. */
28696 static tree
28697 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28698 const char *where, cp_token *pragma_tok,
28699 bool finish_p = true)
28701 tree clauses = NULL;
28702 bool first = true;
28703 cp_token *token = NULL;
28704 bool cilk_simd_fn = false;
28706 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28708 pragma_omp_clause c_kind;
28709 const char *c_name;
28710 tree prev = clauses;
28712 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28713 cp_lexer_consume_token (parser->lexer);
28715 token = cp_lexer_peek_token (parser->lexer);
28716 c_kind = cp_parser_omp_clause_name (parser);
28718 switch (c_kind)
28720 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28721 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28722 token->location);
28723 c_name = "collapse";
28724 break;
28725 case PRAGMA_OMP_CLAUSE_COPYIN:
28726 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28727 c_name = "copyin";
28728 break;
28729 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28730 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28731 clauses);
28732 c_name = "copyprivate";
28733 break;
28734 case PRAGMA_OMP_CLAUSE_DEFAULT:
28735 clauses = cp_parser_omp_clause_default (parser, clauses,
28736 token->location);
28737 c_name = "default";
28738 break;
28739 case PRAGMA_OMP_CLAUSE_FINAL:
28740 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28741 c_name = "final";
28742 break;
28743 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28744 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28745 clauses);
28746 c_name = "firstprivate";
28747 break;
28748 case PRAGMA_OMP_CLAUSE_IF:
28749 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28750 c_name = "if";
28751 break;
28752 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28753 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28754 clauses);
28755 c_name = "lastprivate";
28756 break;
28757 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28758 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28759 token->location);
28760 c_name = "mergeable";
28761 break;
28762 case PRAGMA_OMP_CLAUSE_NOWAIT:
28763 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28764 c_name = "nowait";
28765 break;
28766 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28767 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28768 token->location);
28769 c_name = "num_threads";
28770 break;
28771 case PRAGMA_OMP_CLAUSE_ORDERED:
28772 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28773 token->location);
28774 c_name = "ordered";
28775 break;
28776 case PRAGMA_OMP_CLAUSE_PRIVATE:
28777 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28778 clauses);
28779 c_name = "private";
28780 break;
28781 case PRAGMA_OMP_CLAUSE_REDUCTION:
28782 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28783 c_name = "reduction";
28784 break;
28785 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28786 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28787 token->location);
28788 c_name = "schedule";
28789 break;
28790 case PRAGMA_OMP_CLAUSE_SHARED:
28791 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28792 clauses);
28793 c_name = "shared";
28794 break;
28795 case PRAGMA_OMP_CLAUSE_UNTIED:
28796 clauses = cp_parser_omp_clause_untied (parser, clauses,
28797 token->location);
28798 c_name = "untied";
28799 break;
28800 case PRAGMA_OMP_CLAUSE_INBRANCH:
28801 case PRAGMA_CILK_CLAUSE_MASK:
28802 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28803 clauses, token->location);
28804 c_name = "inbranch";
28805 break;
28806 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28807 case PRAGMA_CILK_CLAUSE_NOMASK:
28808 clauses = cp_parser_omp_clause_branch (parser,
28809 OMP_CLAUSE_NOTINBRANCH,
28810 clauses, token->location);
28811 c_name = "notinbranch";
28812 break;
28813 case PRAGMA_OMP_CLAUSE_PARALLEL:
28814 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28815 clauses, token->location);
28816 c_name = "parallel";
28817 if (!first)
28819 clause_not_first:
28820 error_at (token->location, "%qs must be the first clause of %qs",
28821 c_name, where);
28822 clauses = prev;
28824 break;
28825 case PRAGMA_OMP_CLAUSE_FOR:
28826 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28827 clauses, token->location);
28828 c_name = "for";
28829 if (!first)
28830 goto clause_not_first;
28831 break;
28832 case PRAGMA_OMP_CLAUSE_SECTIONS:
28833 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28834 clauses, token->location);
28835 c_name = "sections";
28836 if (!first)
28837 goto clause_not_first;
28838 break;
28839 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28840 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28841 clauses, token->location);
28842 c_name = "taskgroup";
28843 if (!first)
28844 goto clause_not_first;
28845 break;
28846 case PRAGMA_OMP_CLAUSE_TO:
28847 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28848 clauses);
28849 c_name = "to";
28850 break;
28851 case PRAGMA_OMP_CLAUSE_FROM:
28852 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28853 clauses);
28854 c_name = "from";
28855 break;
28856 case PRAGMA_OMP_CLAUSE_UNIFORM:
28857 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28858 clauses);
28859 c_name = "uniform";
28860 break;
28861 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28862 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28863 token->location);
28864 c_name = "num_teams";
28865 break;
28866 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28867 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28868 token->location);
28869 c_name = "thread_limit";
28870 break;
28871 case PRAGMA_OMP_CLAUSE_ALIGNED:
28872 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28873 c_name = "aligned";
28874 break;
28875 case PRAGMA_OMP_CLAUSE_LINEAR:
28876 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28877 cilk_simd_fn = true;
28878 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28879 c_name = "linear";
28880 break;
28881 case PRAGMA_OMP_CLAUSE_DEPEND:
28882 clauses = cp_parser_omp_clause_depend (parser, clauses);
28883 c_name = "depend";
28884 break;
28885 case PRAGMA_OMP_CLAUSE_MAP:
28886 clauses = cp_parser_omp_clause_map (parser, clauses);
28887 c_name = "map";
28888 break;
28889 case PRAGMA_OMP_CLAUSE_DEVICE:
28890 clauses = cp_parser_omp_clause_device (parser, clauses,
28891 token->location);
28892 c_name = "device";
28893 break;
28894 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28895 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28896 token->location);
28897 c_name = "dist_schedule";
28898 break;
28899 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28900 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28901 token->location);
28902 c_name = "proc_bind";
28903 break;
28904 case PRAGMA_OMP_CLAUSE_SAFELEN:
28905 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28906 token->location);
28907 c_name = "safelen";
28908 break;
28909 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28910 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28911 token->location);
28912 c_name = "simdlen";
28913 break;
28914 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28915 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28916 c_name = "simdlen";
28917 break;
28918 default:
28919 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28920 goto saw_error;
28923 first = false;
28925 if (((mask >> c_kind) & 1) == 0)
28927 /* Remove the invalid clause(s) from the list to avoid
28928 confusing the rest of the compiler. */
28929 clauses = prev;
28930 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28933 saw_error:
28934 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28935 no reason to skip to the end. */
28936 if (!(flag_cilkplus && pragma_tok == NULL))
28937 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28938 if (finish_p)
28939 return finish_omp_clauses (clauses);
28940 return clauses;
28943 /* OpenMP 2.5:
28944 structured-block:
28945 statement
28947 In practice, we're also interested in adding the statement to an
28948 outer node. So it is convenient if we work around the fact that
28949 cp_parser_statement calls add_stmt. */
28951 static unsigned
28952 cp_parser_begin_omp_structured_block (cp_parser *parser)
28954 unsigned save = parser->in_statement;
28956 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28957 This preserves the "not within loop or switch" style error messages
28958 for nonsense cases like
28959 void foo() {
28960 #pragma omp single
28961 break;
28964 if (parser->in_statement)
28965 parser->in_statement = IN_OMP_BLOCK;
28967 return save;
28970 static void
28971 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28973 parser->in_statement = save;
28976 static tree
28977 cp_parser_omp_structured_block (cp_parser *parser)
28979 tree stmt = begin_omp_structured_block ();
28980 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28982 cp_parser_statement (parser, NULL_TREE, false, NULL);
28984 cp_parser_end_omp_structured_block (parser, save);
28985 return finish_omp_structured_block (stmt);
28988 /* OpenMP 2.5:
28989 # pragma omp atomic new-line
28990 expression-stmt
28992 expression-stmt:
28993 x binop= expr | x++ | ++x | x-- | --x
28994 binop:
28995 +, *, -, /, &, ^, |, <<, >>
28997 where x is an lvalue expression with scalar type.
28999 OpenMP 3.1:
29000 # pragma omp atomic new-line
29001 update-stmt
29003 # pragma omp atomic read new-line
29004 read-stmt
29006 # pragma omp atomic write new-line
29007 write-stmt
29009 # pragma omp atomic update new-line
29010 update-stmt
29012 # pragma omp atomic capture new-line
29013 capture-stmt
29015 # pragma omp atomic capture new-line
29016 capture-block
29018 read-stmt:
29019 v = x
29020 write-stmt:
29021 x = expr
29022 update-stmt:
29023 expression-stmt | x = x binop expr
29024 capture-stmt:
29025 v = expression-stmt
29026 capture-block:
29027 { v = x; update-stmt; } | { update-stmt; v = x; }
29029 OpenMP 4.0:
29030 update-stmt:
29031 expression-stmt | x = x binop expr | x = expr binop x
29032 capture-stmt:
29033 v = update-stmt
29034 capture-block:
29035 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29037 where x and v are lvalue expressions with scalar type. */
29039 static void
29040 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29042 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29043 tree rhs1 = NULL_TREE, orig_lhs;
29044 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29045 bool structured_block = false;
29046 bool seq_cst = false;
29048 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29050 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29051 const char *p = IDENTIFIER_POINTER (id);
29053 if (!strcmp (p, "seq_cst"))
29055 seq_cst = true;
29056 cp_lexer_consume_token (parser->lexer);
29057 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29058 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29059 cp_lexer_consume_token (parser->lexer);
29062 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29064 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29065 const char *p = IDENTIFIER_POINTER (id);
29067 if (!strcmp (p, "read"))
29068 code = OMP_ATOMIC_READ;
29069 else if (!strcmp (p, "write"))
29070 code = NOP_EXPR;
29071 else if (!strcmp (p, "update"))
29072 code = OMP_ATOMIC;
29073 else if (!strcmp (p, "capture"))
29074 code = OMP_ATOMIC_CAPTURE_NEW;
29075 else
29076 p = NULL;
29077 if (p)
29078 cp_lexer_consume_token (parser->lexer);
29080 if (!seq_cst)
29082 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29083 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29084 cp_lexer_consume_token (parser->lexer);
29086 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29088 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29089 const char *p = IDENTIFIER_POINTER (id);
29091 if (!strcmp (p, "seq_cst"))
29093 seq_cst = true;
29094 cp_lexer_consume_token (parser->lexer);
29098 cp_parser_require_pragma_eol (parser, pragma_tok);
29100 switch (code)
29102 case OMP_ATOMIC_READ:
29103 case NOP_EXPR: /* atomic write */
29104 v = cp_parser_unary_expression (parser);
29105 if (v == error_mark_node)
29106 goto saw_error;
29107 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29108 goto saw_error;
29109 if (code == NOP_EXPR)
29110 lhs = cp_parser_expression (parser);
29111 else
29112 lhs = cp_parser_unary_expression (parser);
29113 if (lhs == error_mark_node)
29114 goto saw_error;
29115 if (code == NOP_EXPR)
29117 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29118 opcode. */
29119 code = OMP_ATOMIC;
29120 rhs = lhs;
29121 lhs = v;
29122 v = NULL_TREE;
29124 goto done;
29125 case OMP_ATOMIC_CAPTURE_NEW:
29126 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29128 cp_lexer_consume_token (parser->lexer);
29129 structured_block = true;
29131 else
29133 v = cp_parser_unary_expression (parser);
29134 if (v == error_mark_node)
29135 goto saw_error;
29136 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29137 goto saw_error;
29139 default:
29140 break;
29143 restart:
29144 lhs = cp_parser_unary_expression (parser);
29145 orig_lhs = lhs;
29146 switch (TREE_CODE (lhs))
29148 case ERROR_MARK:
29149 goto saw_error;
29151 case POSTINCREMENT_EXPR:
29152 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29153 code = OMP_ATOMIC_CAPTURE_OLD;
29154 /* FALLTHROUGH */
29155 case PREINCREMENT_EXPR:
29156 lhs = TREE_OPERAND (lhs, 0);
29157 opcode = PLUS_EXPR;
29158 rhs = integer_one_node;
29159 break;
29161 case POSTDECREMENT_EXPR:
29162 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29163 code = OMP_ATOMIC_CAPTURE_OLD;
29164 /* FALLTHROUGH */
29165 case PREDECREMENT_EXPR:
29166 lhs = TREE_OPERAND (lhs, 0);
29167 opcode = MINUS_EXPR;
29168 rhs = integer_one_node;
29169 break;
29171 case COMPOUND_EXPR:
29172 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29173 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29174 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29175 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29176 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29177 (TREE_OPERAND (lhs, 1), 0), 0)))
29178 == BOOLEAN_TYPE)
29179 /* Undo effects of boolean_increment for post {in,de}crement. */
29180 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29181 /* FALLTHRU */
29182 case MODIFY_EXPR:
29183 if (TREE_CODE (lhs) == MODIFY_EXPR
29184 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29186 /* Undo effects of boolean_increment. */
29187 if (integer_onep (TREE_OPERAND (lhs, 1)))
29189 /* This is pre or post increment. */
29190 rhs = TREE_OPERAND (lhs, 1);
29191 lhs = TREE_OPERAND (lhs, 0);
29192 opcode = NOP_EXPR;
29193 if (code == OMP_ATOMIC_CAPTURE_NEW
29194 && !structured_block
29195 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29196 code = OMP_ATOMIC_CAPTURE_OLD;
29197 break;
29200 /* FALLTHRU */
29201 default:
29202 switch (cp_lexer_peek_token (parser->lexer)->type)
29204 case CPP_MULT_EQ:
29205 opcode = MULT_EXPR;
29206 break;
29207 case CPP_DIV_EQ:
29208 opcode = TRUNC_DIV_EXPR;
29209 break;
29210 case CPP_PLUS_EQ:
29211 opcode = PLUS_EXPR;
29212 break;
29213 case CPP_MINUS_EQ:
29214 opcode = MINUS_EXPR;
29215 break;
29216 case CPP_LSHIFT_EQ:
29217 opcode = LSHIFT_EXPR;
29218 break;
29219 case CPP_RSHIFT_EQ:
29220 opcode = RSHIFT_EXPR;
29221 break;
29222 case CPP_AND_EQ:
29223 opcode = BIT_AND_EXPR;
29224 break;
29225 case CPP_OR_EQ:
29226 opcode = BIT_IOR_EXPR;
29227 break;
29228 case CPP_XOR_EQ:
29229 opcode = BIT_XOR_EXPR;
29230 break;
29231 case CPP_EQ:
29232 enum cp_parser_prec oprec;
29233 cp_token *token;
29234 cp_lexer_consume_token (parser->lexer);
29235 cp_parser_parse_tentatively (parser);
29236 rhs1 = cp_parser_simple_cast_expression (parser);
29237 if (rhs1 == error_mark_node)
29239 cp_parser_abort_tentative_parse (parser);
29240 cp_parser_simple_cast_expression (parser);
29241 goto saw_error;
29243 token = cp_lexer_peek_token (parser->lexer);
29244 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29246 cp_parser_abort_tentative_parse (parser);
29247 cp_parser_parse_tentatively (parser);
29248 rhs = cp_parser_binary_expression (parser, false, true,
29249 PREC_NOT_OPERATOR, NULL);
29250 if (rhs == error_mark_node)
29252 cp_parser_abort_tentative_parse (parser);
29253 cp_parser_binary_expression (parser, false, true,
29254 PREC_NOT_OPERATOR, NULL);
29255 goto saw_error;
29257 switch (TREE_CODE (rhs))
29259 case MULT_EXPR:
29260 case TRUNC_DIV_EXPR:
29261 case PLUS_EXPR:
29262 case MINUS_EXPR:
29263 case LSHIFT_EXPR:
29264 case RSHIFT_EXPR:
29265 case BIT_AND_EXPR:
29266 case BIT_IOR_EXPR:
29267 case BIT_XOR_EXPR:
29268 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29270 if (cp_parser_parse_definitely (parser))
29272 opcode = TREE_CODE (rhs);
29273 rhs1 = TREE_OPERAND (rhs, 0);
29274 rhs = TREE_OPERAND (rhs, 1);
29275 goto stmt_done;
29277 else
29278 goto saw_error;
29280 break;
29281 default:
29282 break;
29284 cp_parser_abort_tentative_parse (parser);
29285 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29287 rhs = cp_parser_expression (parser);
29288 if (rhs == error_mark_node)
29289 goto saw_error;
29290 opcode = NOP_EXPR;
29291 rhs1 = NULL_TREE;
29292 goto stmt_done;
29294 cp_parser_error (parser,
29295 "invalid form of %<#pragma omp atomic%>");
29296 goto saw_error;
29298 if (!cp_parser_parse_definitely (parser))
29299 goto saw_error;
29300 switch (token->type)
29302 case CPP_SEMICOLON:
29303 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29305 code = OMP_ATOMIC_CAPTURE_OLD;
29306 v = lhs;
29307 lhs = NULL_TREE;
29308 lhs1 = rhs1;
29309 rhs1 = NULL_TREE;
29310 cp_lexer_consume_token (parser->lexer);
29311 goto restart;
29313 else if (structured_block)
29315 opcode = NOP_EXPR;
29316 rhs = rhs1;
29317 rhs1 = NULL_TREE;
29318 goto stmt_done;
29320 cp_parser_error (parser,
29321 "invalid form of %<#pragma omp atomic%>");
29322 goto saw_error;
29323 case CPP_MULT:
29324 opcode = MULT_EXPR;
29325 break;
29326 case CPP_DIV:
29327 opcode = TRUNC_DIV_EXPR;
29328 break;
29329 case CPP_PLUS:
29330 opcode = PLUS_EXPR;
29331 break;
29332 case CPP_MINUS:
29333 opcode = MINUS_EXPR;
29334 break;
29335 case CPP_LSHIFT:
29336 opcode = LSHIFT_EXPR;
29337 break;
29338 case CPP_RSHIFT:
29339 opcode = RSHIFT_EXPR;
29340 break;
29341 case CPP_AND:
29342 opcode = BIT_AND_EXPR;
29343 break;
29344 case CPP_OR:
29345 opcode = BIT_IOR_EXPR;
29346 break;
29347 case CPP_XOR:
29348 opcode = BIT_XOR_EXPR;
29349 break;
29350 default:
29351 cp_parser_error (parser,
29352 "invalid operator for %<#pragma omp atomic%>");
29353 goto saw_error;
29355 oprec = TOKEN_PRECEDENCE (token);
29356 gcc_assert (oprec != PREC_NOT_OPERATOR);
29357 if (commutative_tree_code (opcode))
29358 oprec = (enum cp_parser_prec) (oprec - 1);
29359 cp_lexer_consume_token (parser->lexer);
29360 rhs = cp_parser_binary_expression (parser, false, false,
29361 oprec, NULL);
29362 if (rhs == error_mark_node)
29363 goto saw_error;
29364 goto stmt_done;
29365 /* FALLTHROUGH */
29366 default:
29367 cp_parser_error (parser,
29368 "invalid operator for %<#pragma omp atomic%>");
29369 goto saw_error;
29371 cp_lexer_consume_token (parser->lexer);
29373 rhs = cp_parser_expression (parser);
29374 if (rhs == error_mark_node)
29375 goto saw_error;
29376 break;
29378 stmt_done:
29379 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29381 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29382 goto saw_error;
29383 v = cp_parser_unary_expression (parser);
29384 if (v == error_mark_node)
29385 goto saw_error;
29386 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29387 goto saw_error;
29388 lhs1 = cp_parser_unary_expression (parser);
29389 if (lhs1 == error_mark_node)
29390 goto saw_error;
29392 if (structured_block)
29394 cp_parser_consume_semicolon_at_end_of_statement (parser);
29395 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29397 done:
29398 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29399 if (!structured_block)
29400 cp_parser_consume_semicolon_at_end_of_statement (parser);
29401 return;
29403 saw_error:
29404 cp_parser_skip_to_end_of_block_or_statement (parser);
29405 if (structured_block)
29407 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29408 cp_lexer_consume_token (parser->lexer);
29409 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29411 cp_parser_skip_to_end_of_block_or_statement (parser);
29412 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29413 cp_lexer_consume_token (parser->lexer);
29419 /* OpenMP 2.5:
29420 # pragma omp barrier new-line */
29422 static void
29423 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29425 cp_parser_require_pragma_eol (parser, pragma_tok);
29426 finish_omp_barrier ();
29429 /* OpenMP 2.5:
29430 # pragma omp critical [(name)] new-line
29431 structured-block */
29433 static tree
29434 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29436 tree stmt, name = NULL;
29438 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29440 cp_lexer_consume_token (parser->lexer);
29442 name = cp_parser_identifier (parser);
29444 if (name == error_mark_node
29445 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29446 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29447 /*or_comma=*/false,
29448 /*consume_paren=*/true);
29449 if (name == error_mark_node)
29450 name = NULL;
29452 cp_parser_require_pragma_eol (parser, pragma_tok);
29454 stmt = cp_parser_omp_structured_block (parser);
29455 return c_finish_omp_critical (input_location, stmt, name);
29458 /* OpenMP 2.5:
29459 # pragma omp flush flush-vars[opt] new-line
29461 flush-vars:
29462 ( variable-list ) */
29464 static void
29465 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29467 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29468 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29469 cp_parser_require_pragma_eol (parser, pragma_tok);
29471 finish_omp_flush ();
29474 /* Helper function, to parse omp for increment expression. */
29476 static tree
29477 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29479 tree cond = cp_parser_binary_expression (parser, false, true,
29480 PREC_NOT_OPERATOR, NULL);
29481 if (cond == error_mark_node
29482 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29484 cp_parser_skip_to_end_of_statement (parser);
29485 return error_mark_node;
29488 switch (TREE_CODE (cond))
29490 case GT_EXPR:
29491 case GE_EXPR:
29492 case LT_EXPR:
29493 case LE_EXPR:
29494 break;
29495 case NE_EXPR:
29496 if (code == CILK_SIMD || code == CILK_FOR)
29497 break;
29498 /* Fall through: OpenMP disallows NE_EXPR. */
29499 default:
29500 return error_mark_node;
29503 /* If decl is an iterator, preserve LHS and RHS of the relational
29504 expr until finish_omp_for. */
29505 if (decl
29506 && (type_dependent_expression_p (decl)
29507 || CLASS_TYPE_P (TREE_TYPE (decl))))
29508 return cond;
29510 return build_x_binary_op (input_location, TREE_CODE (cond),
29511 TREE_OPERAND (cond, 0), ERROR_MARK,
29512 TREE_OPERAND (cond, 1), ERROR_MARK,
29513 /*overload=*/NULL, tf_warning_or_error);
29516 /* Helper function, to parse omp for increment expression. */
29518 static tree
29519 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29521 cp_token *token = cp_lexer_peek_token (parser->lexer);
29522 enum tree_code op;
29523 tree lhs, rhs;
29524 cp_id_kind idk;
29525 bool decl_first;
29527 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29529 op = (token->type == CPP_PLUS_PLUS
29530 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29531 cp_lexer_consume_token (parser->lexer);
29532 lhs = cp_parser_simple_cast_expression (parser);
29533 if (lhs != decl)
29534 return error_mark_node;
29535 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29538 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29539 if (lhs != decl)
29540 return error_mark_node;
29542 token = cp_lexer_peek_token (parser->lexer);
29543 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29545 op = (token->type == CPP_PLUS_PLUS
29546 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29547 cp_lexer_consume_token (parser->lexer);
29548 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29551 op = cp_parser_assignment_operator_opt (parser);
29552 if (op == ERROR_MARK)
29553 return error_mark_node;
29555 if (op != NOP_EXPR)
29557 rhs = cp_parser_assignment_expression (parser);
29558 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29559 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29562 lhs = cp_parser_binary_expression (parser, false, false,
29563 PREC_ADDITIVE_EXPRESSION, NULL);
29564 token = cp_lexer_peek_token (parser->lexer);
29565 decl_first = lhs == decl;
29566 if (decl_first)
29567 lhs = NULL_TREE;
29568 if (token->type != CPP_PLUS
29569 && token->type != CPP_MINUS)
29570 return error_mark_node;
29574 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29575 cp_lexer_consume_token (parser->lexer);
29576 rhs = cp_parser_binary_expression (parser, false, false,
29577 PREC_ADDITIVE_EXPRESSION, NULL);
29578 token = cp_lexer_peek_token (parser->lexer);
29579 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29581 if (lhs == NULL_TREE)
29583 if (op == PLUS_EXPR)
29584 lhs = rhs;
29585 else
29586 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29587 tf_warning_or_error);
29589 else
29590 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29591 ERROR_MARK, NULL, tf_warning_or_error);
29594 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29596 if (!decl_first)
29598 if (rhs != decl || op == MINUS_EXPR)
29599 return error_mark_node;
29600 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29602 else
29603 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29605 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29608 /* Parse the initialization statement of either an OpenMP for loop or
29609 a Cilk Plus for loop.
29611 Return true if the resulting construct should have an
29612 OMP_CLAUSE_PRIVATE added to it. */
29614 static bool
29615 cp_parser_omp_for_loop_init (cp_parser *parser,
29616 enum tree_code code,
29617 tree &this_pre_body,
29618 vec<tree, va_gc> *for_block,
29619 tree &init,
29620 tree &decl,
29621 tree &real_decl)
29623 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29624 return false;
29626 bool add_private_clause = false;
29628 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29630 init-expr:
29631 var = lb
29632 integer-type var = lb
29633 random-access-iterator-type var = lb
29634 pointer-type var = lb
29636 cp_decl_specifier_seq type_specifiers;
29638 /* First, try to parse as an initialized declaration. See
29639 cp_parser_condition, from whence the bulk of this is copied. */
29641 cp_parser_parse_tentatively (parser);
29642 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29643 /*is_trailing_return=*/false,
29644 &type_specifiers);
29645 if (cp_parser_parse_definitely (parser))
29647 /* If parsing a type specifier seq succeeded, then this
29648 MUST be a initialized declaration. */
29649 tree asm_specification, attributes;
29650 cp_declarator *declarator;
29652 declarator = cp_parser_declarator (parser,
29653 CP_PARSER_DECLARATOR_NAMED,
29654 /*ctor_dtor_or_conv_p=*/NULL,
29655 /*parenthesized_p=*/NULL,
29656 /*member_p=*/false,
29657 /*friend_p=*/false);
29658 attributes = cp_parser_attributes_opt (parser);
29659 asm_specification = cp_parser_asm_specification_opt (parser);
29661 if (declarator == cp_error_declarator)
29662 cp_parser_skip_to_end_of_statement (parser);
29664 else
29666 tree pushed_scope, auto_node;
29668 decl = start_decl (declarator, &type_specifiers,
29669 SD_INITIALIZED, attributes,
29670 /*prefix_attributes=*/NULL_TREE,
29671 &pushed_scope);
29673 auto_node = type_uses_auto (TREE_TYPE (decl));
29674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29676 if (cp_lexer_next_token_is (parser->lexer,
29677 CPP_OPEN_PAREN))
29679 if (code != CILK_SIMD && code != CILK_FOR)
29680 error ("parenthesized initialization is not allowed in "
29681 "OpenMP %<for%> loop");
29682 else
29683 error ("parenthesized initialization is "
29684 "not allowed in for-loop");
29686 else
29687 /* Trigger an error. */
29688 cp_parser_require (parser, CPP_EQ, RT_EQ);
29690 init = error_mark_node;
29691 cp_parser_skip_to_end_of_statement (parser);
29693 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29694 || type_dependent_expression_p (decl)
29695 || auto_node)
29697 bool is_direct_init, is_non_constant_init;
29699 init = cp_parser_initializer (parser,
29700 &is_direct_init,
29701 &is_non_constant_init);
29703 if (auto_node)
29705 TREE_TYPE (decl)
29706 = do_auto_deduction (TREE_TYPE (decl), init,
29707 auto_node);
29709 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29710 && !type_dependent_expression_p (decl))
29711 goto non_class;
29714 cp_finish_decl (decl, init, !is_non_constant_init,
29715 asm_specification,
29716 LOOKUP_ONLYCONVERTING);
29717 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29719 vec_safe_push (for_block, this_pre_body);
29720 init = NULL_TREE;
29722 else
29723 init = pop_stmt_list (this_pre_body);
29724 this_pre_body = NULL_TREE;
29726 else
29728 /* Consume '='. */
29729 cp_lexer_consume_token (parser->lexer);
29730 init = cp_parser_assignment_expression (parser);
29732 non_class:
29733 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29734 init = error_mark_node;
29735 else
29736 cp_finish_decl (decl, NULL_TREE,
29737 /*init_const_expr_p=*/false,
29738 asm_specification,
29739 LOOKUP_ONLYCONVERTING);
29742 if (pushed_scope)
29743 pop_scope (pushed_scope);
29746 else
29748 cp_id_kind idk;
29749 /* If parsing a type specifier sequence failed, then
29750 this MUST be a simple expression. */
29751 if (code == CILK_FOR)
29752 error ("%<_Cilk_for%> allows expression instead of declaration only "
29753 "in C, not in C++");
29754 cp_parser_parse_tentatively (parser);
29755 decl = cp_parser_primary_expression (parser, false, false,
29756 false, &idk);
29757 if (!cp_parser_error_occurred (parser)
29758 && decl
29759 && DECL_P (decl)
29760 && CLASS_TYPE_P (TREE_TYPE (decl)))
29762 tree rhs;
29764 cp_parser_parse_definitely (parser);
29765 cp_parser_require (parser, CPP_EQ, RT_EQ);
29766 rhs = cp_parser_assignment_expression (parser);
29767 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29768 decl, NOP_EXPR,
29769 rhs,
29770 tf_warning_or_error));
29771 add_private_clause = true;
29773 else
29775 decl = NULL;
29776 cp_parser_abort_tentative_parse (parser);
29777 init = cp_parser_expression (parser);
29778 if (init)
29780 if (TREE_CODE (init) == MODIFY_EXPR
29781 || TREE_CODE (init) == MODOP_EXPR)
29782 real_decl = TREE_OPERAND (init, 0);
29786 return add_private_clause;
29789 /* Parse the restricted form of the for statement allowed by OpenMP. */
29791 static tree
29792 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29793 tree *cclauses)
29795 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29796 tree real_decl, initv, condv, incrv, declv;
29797 tree this_pre_body, cl;
29798 location_t loc_first;
29799 bool collapse_err = false;
29800 int i, collapse = 1, nbraces = 0;
29801 vec<tree, va_gc> *for_block = make_tree_vector ();
29803 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29804 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29805 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29807 gcc_assert (collapse >= 1);
29809 declv = make_tree_vec (collapse);
29810 initv = make_tree_vec (collapse);
29811 condv = make_tree_vec (collapse);
29812 incrv = make_tree_vec (collapse);
29814 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29816 for (i = 0; i < collapse; i++)
29818 int bracecount = 0;
29819 bool add_private_clause = false;
29820 location_t loc;
29822 if (code != CILK_FOR
29823 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29825 cp_parser_error (parser, "for statement expected");
29826 return NULL;
29828 if (code == CILK_FOR
29829 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
29831 cp_parser_error (parser, "_Cilk_for statement expected");
29832 return NULL;
29834 loc = cp_lexer_consume_token (parser->lexer)->location;
29836 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29837 return NULL;
29839 init = decl = real_decl = NULL;
29840 this_pre_body = push_stmt_list ();
29842 add_private_clause
29843 |= cp_parser_omp_for_loop_init (parser, code,
29844 this_pre_body, for_block,
29845 init, decl, real_decl);
29847 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29848 if (this_pre_body)
29850 this_pre_body = pop_stmt_list (this_pre_body);
29851 if (pre_body)
29853 tree t = pre_body;
29854 pre_body = push_stmt_list ();
29855 add_stmt (t);
29856 add_stmt (this_pre_body);
29857 pre_body = pop_stmt_list (pre_body);
29859 else
29860 pre_body = this_pre_body;
29863 if (decl)
29864 real_decl = decl;
29865 if (cclauses != NULL
29866 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29867 && real_decl != NULL_TREE)
29869 tree *c;
29870 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29871 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29872 && OMP_CLAUSE_DECL (*c) == real_decl)
29874 error_at (loc, "iteration variable %qD"
29875 " should not be firstprivate", real_decl);
29876 *c = OMP_CLAUSE_CHAIN (*c);
29878 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29879 && OMP_CLAUSE_DECL (*c) == real_decl)
29881 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29882 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29883 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29884 OMP_CLAUSE_DECL (l) = real_decl;
29885 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29886 if (code == OMP_SIMD)
29888 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29889 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29891 else
29893 OMP_CLAUSE_CHAIN (l) = clauses;
29894 clauses = l;
29896 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29897 CP_OMP_CLAUSE_INFO (*c) = NULL;
29898 add_private_clause = false;
29900 else
29902 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29903 && OMP_CLAUSE_DECL (*c) == real_decl)
29904 add_private_clause = false;
29905 c = &OMP_CLAUSE_CHAIN (*c);
29909 if (add_private_clause)
29911 tree c;
29912 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29914 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29915 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29916 && OMP_CLAUSE_DECL (c) == decl)
29917 break;
29918 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29919 && OMP_CLAUSE_DECL (c) == decl)
29920 error_at (loc, "iteration variable %qD "
29921 "should not be firstprivate",
29922 decl);
29923 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29924 && OMP_CLAUSE_DECL (c) == decl)
29925 error_at (loc, "iteration variable %qD should not be reduction",
29926 decl);
29928 if (c == NULL)
29930 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29931 OMP_CLAUSE_DECL (c) = decl;
29932 c = finish_omp_clauses (c);
29933 if (c)
29935 OMP_CLAUSE_CHAIN (c) = clauses;
29936 clauses = c;
29941 cond = NULL;
29942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29943 cond = cp_parser_omp_for_cond (parser, decl, code);
29944 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29946 incr = NULL;
29947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29949 /* If decl is an iterator, preserve the operator on decl
29950 until finish_omp_for. */
29951 if (real_decl
29952 && ((processing_template_decl
29953 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29954 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29955 incr = cp_parser_omp_for_incr (parser, real_decl);
29956 else
29957 incr = cp_parser_expression (parser);
29958 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29959 SET_EXPR_LOCATION (incr, input_location);
29962 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29963 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29964 /*or_comma=*/false,
29965 /*consume_paren=*/true);
29967 TREE_VEC_ELT (declv, i) = decl;
29968 TREE_VEC_ELT (initv, i) = init;
29969 TREE_VEC_ELT (condv, i) = cond;
29970 TREE_VEC_ELT (incrv, i) = incr;
29972 if (i == collapse - 1)
29973 break;
29975 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29976 in between the collapsed for loops to be still considered perfectly
29977 nested. Hopefully the final version clarifies this.
29978 For now handle (multiple) {'s and empty statements. */
29979 cp_parser_parse_tentatively (parser);
29982 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29983 break;
29984 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29986 cp_lexer_consume_token (parser->lexer);
29987 bracecount++;
29989 else if (bracecount
29990 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29991 cp_lexer_consume_token (parser->lexer);
29992 else
29994 loc = cp_lexer_peek_token (parser->lexer)->location;
29995 error_at (loc, "not enough collapsed for loops");
29996 collapse_err = true;
29997 cp_parser_abort_tentative_parse (parser);
29998 declv = NULL_TREE;
29999 break;
30002 while (1);
30004 if (declv)
30006 cp_parser_parse_definitely (parser);
30007 nbraces += bracecount;
30011 /* Note that we saved the original contents of this flag when we entered
30012 the structured block, and so we don't need to re-save it here. */
30013 if (code == CILK_SIMD || code == CILK_FOR)
30014 parser->in_statement = IN_CILK_SIMD_FOR;
30015 else
30016 parser->in_statement = IN_OMP_FOR;
30018 /* Note that the grammar doesn't call for a structured block here,
30019 though the loop as a whole is a structured block. */
30020 body = push_stmt_list ();
30021 cp_parser_statement (parser, NULL_TREE, false, NULL);
30022 body = pop_stmt_list (body);
30024 if (declv == NULL_TREE)
30025 ret = NULL_TREE;
30026 else
30027 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30028 pre_body, clauses);
30030 while (nbraces)
30032 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30034 cp_lexer_consume_token (parser->lexer);
30035 nbraces--;
30037 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30038 cp_lexer_consume_token (parser->lexer);
30039 else
30041 if (!collapse_err)
30043 error_at (cp_lexer_peek_token (parser->lexer)->location,
30044 "collapsed loops not perfectly nested");
30046 collapse_err = true;
30047 cp_parser_statement_seq_opt (parser, NULL);
30048 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30049 break;
30053 while (!for_block->is_empty ())
30054 add_stmt (pop_stmt_list (for_block->pop ()));
30055 release_tree_vector (for_block);
30057 return ret;
30060 /* Helper function for OpenMP parsing, split clauses and call
30061 finish_omp_clauses on each of the set of clauses afterwards. */
30063 static void
30064 cp_omp_split_clauses (location_t loc, enum tree_code code,
30065 omp_clause_mask mask, tree clauses, tree *cclauses)
30067 int i;
30068 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30069 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30070 if (cclauses[i])
30071 cclauses[i] = finish_omp_clauses (cclauses[i]);
30074 /* OpenMP 4.0:
30075 #pragma omp simd simd-clause[optseq] new-line
30076 for-loop */
30078 #define OMP_SIMD_CLAUSE_MASK \
30079 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
30080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30087 static tree
30088 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30089 char *p_name, omp_clause_mask mask, tree *cclauses)
30091 tree clauses, sb, ret;
30092 unsigned int save;
30093 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30095 strcat (p_name, " simd");
30096 mask |= OMP_SIMD_CLAUSE_MASK;
30097 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30099 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30100 cclauses == NULL);
30101 if (cclauses)
30103 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30104 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30107 sb = begin_omp_structured_block ();
30108 save = cp_parser_begin_omp_structured_block (parser);
30110 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30112 cp_parser_end_omp_structured_block (parser, save);
30113 add_stmt (finish_omp_structured_block (sb));
30115 return ret;
30118 /* OpenMP 2.5:
30119 #pragma omp for for-clause[optseq] new-line
30120 for-loop
30122 OpenMP 4.0:
30123 #pragma omp for simd for-simd-clause[optseq] new-line
30124 for-loop */
30126 #define OMP_FOR_CLAUSE_MASK \
30127 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
30132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
30133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
30134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30136 static tree
30137 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30138 char *p_name, omp_clause_mask mask, tree *cclauses)
30140 tree clauses, sb, ret;
30141 unsigned int save;
30142 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30144 strcat (p_name, " for");
30145 mask |= OMP_FOR_CLAUSE_MASK;
30146 if (cclauses)
30147 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30149 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30151 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30152 const char *p = IDENTIFIER_POINTER (id);
30154 if (strcmp (p, "simd") == 0)
30156 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30157 if (cclauses == NULL)
30158 cclauses = cclauses_buf;
30160 cp_lexer_consume_token (parser->lexer);
30161 if (!flag_openmp) /* flag_openmp_simd */
30162 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30163 cclauses);
30164 sb = begin_omp_structured_block ();
30165 save = cp_parser_begin_omp_structured_block (parser);
30166 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30167 cclauses);
30168 cp_parser_end_omp_structured_block (parser, save);
30169 tree body = finish_omp_structured_block (sb);
30170 if (ret == NULL)
30171 return ret;
30172 ret = make_node (OMP_FOR);
30173 TREE_TYPE (ret) = void_type_node;
30174 OMP_FOR_BODY (ret) = body;
30175 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30176 SET_EXPR_LOCATION (ret, loc);
30177 add_stmt (ret);
30178 return ret;
30181 if (!flag_openmp) /* flag_openmp_simd */
30183 cp_parser_require_pragma_eol (parser, pragma_tok);
30184 return NULL_TREE;
30187 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30188 cclauses == NULL);
30189 if (cclauses)
30191 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30192 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30195 sb = begin_omp_structured_block ();
30196 save = cp_parser_begin_omp_structured_block (parser);
30198 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30200 cp_parser_end_omp_structured_block (parser, save);
30201 add_stmt (finish_omp_structured_block (sb));
30203 return ret;
30206 /* OpenMP 2.5:
30207 # pragma omp master new-line
30208 structured-block */
30210 static tree
30211 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30213 cp_parser_require_pragma_eol (parser, pragma_tok);
30214 return c_finish_omp_master (input_location,
30215 cp_parser_omp_structured_block (parser));
30218 /* OpenMP 2.5:
30219 # pragma omp ordered new-line
30220 structured-block */
30222 static tree
30223 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30225 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30226 cp_parser_require_pragma_eol (parser, pragma_tok);
30227 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30230 /* OpenMP 2.5:
30232 section-scope:
30233 { section-sequence }
30235 section-sequence:
30236 section-directive[opt] structured-block
30237 section-sequence section-directive structured-block */
30239 static tree
30240 cp_parser_omp_sections_scope (cp_parser *parser)
30242 tree stmt, substmt;
30243 bool error_suppress = false;
30244 cp_token *tok;
30246 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30247 return NULL_TREE;
30249 stmt = push_stmt_list ();
30251 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30253 substmt = cp_parser_omp_structured_block (parser);
30254 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30255 add_stmt (substmt);
30258 while (1)
30260 tok = cp_lexer_peek_token (parser->lexer);
30261 if (tok->type == CPP_CLOSE_BRACE)
30262 break;
30263 if (tok->type == CPP_EOF)
30264 break;
30266 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30268 cp_lexer_consume_token (parser->lexer);
30269 cp_parser_require_pragma_eol (parser, tok);
30270 error_suppress = false;
30272 else if (!error_suppress)
30274 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30275 error_suppress = true;
30278 substmt = cp_parser_omp_structured_block (parser);
30279 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30280 add_stmt (substmt);
30282 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30284 substmt = pop_stmt_list (stmt);
30286 stmt = make_node (OMP_SECTIONS);
30287 TREE_TYPE (stmt) = void_type_node;
30288 OMP_SECTIONS_BODY (stmt) = substmt;
30290 add_stmt (stmt);
30291 return stmt;
30294 /* OpenMP 2.5:
30295 # pragma omp sections sections-clause[optseq] newline
30296 sections-scope */
30298 #define OMP_SECTIONS_CLAUSE_MASK \
30299 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30305 static tree
30306 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30307 char *p_name, omp_clause_mask mask, tree *cclauses)
30309 tree clauses, ret;
30310 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30312 strcat (p_name, " sections");
30313 mask |= OMP_SECTIONS_CLAUSE_MASK;
30314 if (cclauses)
30315 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30317 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30318 cclauses == NULL);
30319 if (cclauses)
30321 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30322 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30325 ret = cp_parser_omp_sections_scope (parser);
30326 if (ret)
30327 OMP_SECTIONS_CLAUSES (ret) = clauses;
30329 return ret;
30332 /* OpenMP 2.5:
30333 # pragma omp parallel parallel-clause[optseq] new-line
30334 structured-block
30335 # pragma omp parallel for parallel-for-clause[optseq] new-line
30336 structured-block
30337 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30338 structured-block
30340 OpenMP 4.0:
30341 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30342 structured-block */
30344 #define OMP_PARALLEL_CLAUSE_MASK \
30345 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30355 static tree
30356 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30357 char *p_name, omp_clause_mask mask, tree *cclauses)
30359 tree stmt, clauses, block;
30360 unsigned int save;
30361 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30363 strcat (p_name, " parallel");
30364 mask |= OMP_PARALLEL_CLAUSE_MASK;
30366 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30368 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30369 if (cclauses == NULL)
30370 cclauses = cclauses_buf;
30372 cp_lexer_consume_token (parser->lexer);
30373 if (!flag_openmp) /* flag_openmp_simd */
30374 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30375 block = begin_omp_parallel ();
30376 save = cp_parser_begin_omp_structured_block (parser);
30377 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30378 cp_parser_end_omp_structured_block (parser, save);
30379 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30380 block);
30381 if (ret == NULL_TREE)
30382 return ret;
30383 OMP_PARALLEL_COMBINED (stmt) = 1;
30384 return stmt;
30386 else if (cclauses)
30388 error_at (loc, "expected %<for%> after %qs", p_name);
30389 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30390 return NULL_TREE;
30392 else if (!flag_openmp) /* flag_openmp_simd */
30394 cp_parser_require_pragma_eol (parser, pragma_tok);
30395 return NULL_TREE;
30397 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30399 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30400 const char *p = IDENTIFIER_POINTER (id);
30401 if (strcmp (p, "sections") == 0)
30403 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30404 cclauses = cclauses_buf;
30406 cp_lexer_consume_token (parser->lexer);
30407 block = begin_omp_parallel ();
30408 save = cp_parser_begin_omp_structured_block (parser);
30409 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30410 cp_parser_end_omp_structured_block (parser, save);
30411 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30412 block);
30413 OMP_PARALLEL_COMBINED (stmt) = 1;
30414 return stmt;
30418 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30420 block = begin_omp_parallel ();
30421 save = cp_parser_begin_omp_structured_block (parser);
30422 cp_parser_statement (parser, NULL_TREE, false, NULL);
30423 cp_parser_end_omp_structured_block (parser, save);
30424 stmt = finish_omp_parallel (clauses, block);
30425 return stmt;
30428 /* OpenMP 2.5:
30429 # pragma omp single single-clause[optseq] new-line
30430 structured-block */
30432 #define OMP_SINGLE_CLAUSE_MASK \
30433 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30438 static tree
30439 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30441 tree stmt = make_node (OMP_SINGLE);
30442 TREE_TYPE (stmt) = void_type_node;
30444 OMP_SINGLE_CLAUSES (stmt)
30445 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30446 "#pragma omp single", pragma_tok);
30447 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30449 return add_stmt (stmt);
30452 /* OpenMP 3.0:
30453 # pragma omp task task-clause[optseq] new-line
30454 structured-block */
30456 #define OMP_TASK_CLAUSE_MASK \
30457 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30467 static tree
30468 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30470 tree clauses, block;
30471 unsigned int save;
30473 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30474 "#pragma omp task", pragma_tok);
30475 block = begin_omp_task ();
30476 save = cp_parser_begin_omp_structured_block (parser);
30477 cp_parser_statement (parser, NULL_TREE, false, NULL);
30478 cp_parser_end_omp_structured_block (parser, save);
30479 return finish_omp_task (clauses, block);
30482 /* OpenMP 3.0:
30483 # pragma omp taskwait new-line */
30485 static void
30486 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30488 cp_parser_require_pragma_eol (parser, pragma_tok);
30489 finish_omp_taskwait ();
30492 /* OpenMP 3.1:
30493 # pragma omp taskyield new-line */
30495 static void
30496 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30498 cp_parser_require_pragma_eol (parser, pragma_tok);
30499 finish_omp_taskyield ();
30502 /* OpenMP 4.0:
30503 # pragma omp taskgroup new-line
30504 structured-block */
30506 static tree
30507 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30509 cp_parser_require_pragma_eol (parser, pragma_tok);
30510 return c_finish_omp_taskgroup (input_location,
30511 cp_parser_omp_structured_block (parser));
30515 /* OpenMP 2.5:
30516 # pragma omp threadprivate (variable-list) */
30518 static void
30519 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30521 tree vars;
30523 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30524 cp_parser_require_pragma_eol (parser, pragma_tok);
30526 finish_omp_threadprivate (vars);
30529 /* OpenMP 4.0:
30530 # pragma omp cancel cancel-clause[optseq] new-line */
30532 #define OMP_CANCEL_CLAUSE_MASK \
30533 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30539 static void
30540 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30542 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30543 "#pragma omp cancel", pragma_tok);
30544 finish_omp_cancel (clauses);
30547 /* OpenMP 4.0:
30548 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30550 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30551 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30556 static void
30557 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30559 tree clauses;
30560 bool point_seen = false;
30562 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30564 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30565 const char *p = IDENTIFIER_POINTER (id);
30567 if (strcmp (p, "point") == 0)
30569 cp_lexer_consume_token (parser->lexer);
30570 point_seen = true;
30573 if (!point_seen)
30575 cp_parser_error (parser, "expected %<point%>");
30576 cp_parser_require_pragma_eol (parser, pragma_tok);
30577 return;
30580 clauses = cp_parser_omp_all_clauses (parser,
30581 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30582 "#pragma omp cancellation point",
30583 pragma_tok);
30584 finish_omp_cancellation_point (clauses);
30587 /* OpenMP 4.0:
30588 #pragma omp distribute distribute-clause[optseq] new-line
30589 for-loop */
30591 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30592 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30597 static tree
30598 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30599 char *p_name, omp_clause_mask mask, tree *cclauses)
30601 tree clauses, sb, ret;
30602 unsigned int save;
30603 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30605 strcat (p_name, " distribute");
30606 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30608 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30610 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30611 const char *p = IDENTIFIER_POINTER (id);
30612 bool simd = false;
30613 bool parallel = false;
30615 if (strcmp (p, "simd") == 0)
30616 simd = true;
30617 else
30618 parallel = strcmp (p, "parallel") == 0;
30619 if (parallel || simd)
30621 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30622 if (cclauses == NULL)
30623 cclauses = cclauses_buf;
30624 cp_lexer_consume_token (parser->lexer);
30625 if (!flag_openmp) /* flag_openmp_simd */
30627 if (simd)
30628 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30629 cclauses);
30630 else
30631 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30632 cclauses);
30634 sb = begin_omp_structured_block ();
30635 save = cp_parser_begin_omp_structured_block (parser);
30636 if (simd)
30637 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30638 cclauses);
30639 else
30640 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30641 cclauses);
30642 cp_parser_end_omp_structured_block (parser, save);
30643 tree body = finish_omp_structured_block (sb);
30644 if (ret == NULL)
30645 return ret;
30646 ret = make_node (OMP_DISTRIBUTE);
30647 TREE_TYPE (ret) = void_type_node;
30648 OMP_FOR_BODY (ret) = body;
30649 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30650 SET_EXPR_LOCATION (ret, loc);
30651 add_stmt (ret);
30652 return ret;
30655 if (!flag_openmp) /* flag_openmp_simd */
30657 cp_parser_require_pragma_eol (parser, pragma_tok);
30658 return NULL_TREE;
30661 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30662 cclauses == NULL);
30663 if (cclauses)
30665 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30666 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30669 sb = begin_omp_structured_block ();
30670 save = cp_parser_begin_omp_structured_block (parser);
30672 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30674 cp_parser_end_omp_structured_block (parser, save);
30675 add_stmt (finish_omp_structured_block (sb));
30677 return ret;
30680 /* OpenMP 4.0:
30681 # pragma omp teams teams-clause[optseq] new-line
30682 structured-block */
30684 #define OMP_TEAMS_CLAUSE_MASK \
30685 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30693 static tree
30694 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30695 char *p_name, omp_clause_mask mask, tree *cclauses)
30697 tree clauses, sb, ret;
30698 unsigned int save;
30699 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30701 strcat (p_name, " teams");
30702 mask |= OMP_TEAMS_CLAUSE_MASK;
30704 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30706 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30707 const char *p = IDENTIFIER_POINTER (id);
30708 if (strcmp (p, "distribute") == 0)
30710 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30711 if (cclauses == NULL)
30712 cclauses = cclauses_buf;
30714 cp_lexer_consume_token (parser->lexer);
30715 if (!flag_openmp) /* flag_openmp_simd */
30716 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30717 cclauses);
30718 sb = begin_omp_structured_block ();
30719 save = cp_parser_begin_omp_structured_block (parser);
30720 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30721 cclauses);
30722 cp_parser_end_omp_structured_block (parser, save);
30723 tree body = finish_omp_structured_block (sb);
30724 if (ret == NULL)
30725 return ret;
30726 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30727 ret = make_node (OMP_TEAMS);
30728 TREE_TYPE (ret) = void_type_node;
30729 OMP_TEAMS_CLAUSES (ret) = clauses;
30730 OMP_TEAMS_BODY (ret) = body;
30731 return add_stmt (ret);
30734 if (!flag_openmp) /* flag_openmp_simd */
30736 cp_parser_require_pragma_eol (parser, pragma_tok);
30737 return NULL_TREE;
30740 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30741 cclauses == NULL);
30742 if (cclauses)
30744 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30745 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30748 tree stmt = make_node (OMP_TEAMS);
30749 TREE_TYPE (stmt) = void_type_node;
30750 OMP_TEAMS_CLAUSES (stmt) = clauses;
30751 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30753 return add_stmt (stmt);
30756 /* OpenMP 4.0:
30757 # pragma omp target data target-data-clause[optseq] new-line
30758 structured-block */
30760 #define OMP_TARGET_DATA_CLAUSE_MASK \
30761 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30765 static tree
30766 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30768 tree stmt = make_node (OMP_TARGET_DATA);
30769 TREE_TYPE (stmt) = void_type_node;
30771 OMP_TARGET_DATA_CLAUSES (stmt)
30772 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30773 "#pragma omp target data", pragma_tok);
30774 keep_next_level (true);
30775 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30777 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30778 return add_stmt (stmt);
30781 /* OpenMP 4.0:
30782 # pragma omp target update target-update-clause[optseq] new-line */
30784 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30785 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30790 static bool
30791 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30792 enum pragma_context context)
30794 if (context == pragma_stmt)
30796 error_at (pragma_tok->location,
30797 "%<#pragma omp target update%> may only be "
30798 "used in compound statements");
30799 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30800 return false;
30803 tree clauses
30804 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30805 "#pragma omp target update", pragma_tok);
30806 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30807 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30809 error_at (pragma_tok->location,
30810 "%<#pragma omp target update must contain at least one "
30811 "%<from%> or %<to%> clauses");
30812 return false;
30815 tree stmt = make_node (OMP_TARGET_UPDATE);
30816 TREE_TYPE (stmt) = void_type_node;
30817 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30818 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30819 add_stmt (stmt);
30820 return false;
30823 /* OpenMP 4.0:
30824 # pragma omp target target-clause[optseq] new-line
30825 structured-block */
30827 #define OMP_TARGET_CLAUSE_MASK \
30828 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30832 static bool
30833 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30834 enum pragma_context context)
30836 if (context != pragma_stmt && context != pragma_compound)
30838 cp_parser_error (parser, "expected declaration specifiers");
30839 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30840 return false;
30843 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30845 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30846 const char *p = IDENTIFIER_POINTER (id);
30848 if (strcmp (p, "teams") == 0)
30850 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30851 char p_name[sizeof ("#pragma omp target teams distribute "
30852 "parallel for simd")];
30854 cp_lexer_consume_token (parser->lexer);
30855 strcpy (p_name, "#pragma omp target");
30856 if (!flag_openmp) /* flag_openmp_simd */
30858 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30859 OMP_TARGET_CLAUSE_MASK,
30860 cclauses);
30861 return stmt != NULL_TREE;
30863 keep_next_level (true);
30864 tree sb = begin_omp_structured_block ();
30865 unsigned save = cp_parser_begin_omp_structured_block (parser);
30866 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30867 OMP_TARGET_CLAUSE_MASK, cclauses);
30868 cp_parser_end_omp_structured_block (parser, save);
30869 tree body = finish_omp_structured_block (sb);
30870 if (ret == NULL_TREE)
30871 return false;
30872 tree stmt = make_node (OMP_TARGET);
30873 TREE_TYPE (stmt) = void_type_node;
30874 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30875 OMP_TARGET_BODY (stmt) = body;
30876 add_stmt (stmt);
30877 return true;
30879 else if (!flag_openmp) /* flag_openmp_simd */
30881 cp_parser_require_pragma_eol (parser, pragma_tok);
30882 return false;
30884 else if (strcmp (p, "data") == 0)
30886 cp_lexer_consume_token (parser->lexer);
30887 cp_parser_omp_target_data (parser, pragma_tok);
30888 return true;
30890 else if (strcmp (p, "update") == 0)
30892 cp_lexer_consume_token (parser->lexer);
30893 return cp_parser_omp_target_update (parser, pragma_tok, context);
30897 tree stmt = make_node (OMP_TARGET);
30898 TREE_TYPE (stmt) = void_type_node;
30900 OMP_TARGET_CLAUSES (stmt)
30901 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30902 "#pragma omp target", pragma_tok);
30903 keep_next_level (true);
30904 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30906 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30907 add_stmt (stmt);
30908 return true;
30911 /* OpenMP 4.0:
30912 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30914 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30915 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30922 static void
30923 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30924 enum pragma_context context)
30926 bool first_p = parser->omp_declare_simd == NULL;
30927 cp_omp_declare_simd_data data;
30928 if (first_p)
30930 data.error_seen = false;
30931 data.fndecl_seen = false;
30932 data.tokens = vNULL;
30933 parser->omp_declare_simd = &data;
30935 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30936 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30937 cp_lexer_consume_token (parser->lexer);
30938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30939 parser->omp_declare_simd->error_seen = true;
30940 cp_parser_require_pragma_eol (parser, pragma_tok);
30941 struct cp_token_cache *cp
30942 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30943 parser->omp_declare_simd->tokens.safe_push (cp);
30944 if (first_p)
30946 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30947 cp_parser_pragma (parser, context);
30948 switch (context)
30950 case pragma_external:
30951 cp_parser_declaration (parser);
30952 break;
30953 case pragma_member:
30954 cp_parser_member_declaration (parser);
30955 break;
30956 case pragma_objc_icode:
30957 cp_parser_block_declaration (parser, /*statement_p=*/false);
30958 break;
30959 default:
30960 cp_parser_declaration_statement (parser);
30961 break;
30963 if (parser->omp_declare_simd
30964 && !parser->omp_declare_simd->error_seen
30965 && !parser->omp_declare_simd->fndecl_seen)
30966 error_at (pragma_tok->location,
30967 "%<#pragma omp declare simd%> not immediately followed by "
30968 "function declaration or definition");
30969 data.tokens.release ();
30970 parser->omp_declare_simd = NULL;
30974 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30975 This function is modelled similar to the late parsing of omp declare
30976 simd. */
30978 static tree
30979 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30981 struct cp_token_cache *ce;
30982 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30983 int ii = 0;
30985 if (parser->omp_declare_simd != NULL)
30987 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30988 " marked as a Cilk Plus SIMD-enabled function");
30989 XDELETE (parser->cilk_simd_fn_info);
30990 parser->cilk_simd_fn_info = NULL;
30991 return attrs;
30993 if (!info->error_seen && info->fndecl_seen)
30995 error ("vector attribute not immediately followed by a single function"
30996 " declaration or definition");
30997 info->error_seen = true;
30999 if (info->error_seen)
31000 return attrs;
31002 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31004 tree c, cl;
31006 cp_parser_push_lexer_for_tokens (parser, ce);
31007 parser->lexer->in_pragma = true;
31008 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31009 "SIMD-enabled functions attribute",
31010 NULL);
31011 cp_parser_pop_lexer (parser);
31012 if (cl)
31013 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31015 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31016 TREE_CHAIN (c) = attrs;
31017 attrs = c;
31019 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31020 TREE_CHAIN (c) = attrs;
31021 if (processing_template_decl)
31022 ATTR_IS_DEPENDENT (c) = 1;
31023 attrs = c;
31025 info->fndecl_seen = true;
31026 XDELETE (parser->cilk_simd_fn_info);
31027 parser->cilk_simd_fn_info = NULL;
31028 return attrs;
31031 /* Finalize #pragma omp declare simd clauses after direct declarator has
31032 been parsed, and put that into "omp declare simd" attribute. */
31034 static tree
31035 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31037 struct cp_token_cache *ce;
31038 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31039 int i;
31041 if (!data->error_seen && data->fndecl_seen)
31043 error ("%<#pragma omp declare simd%> not immediately followed by "
31044 "a single function declaration or definition");
31045 data->error_seen = true;
31046 return attrs;
31048 if (data->error_seen)
31049 return attrs;
31051 FOR_EACH_VEC_ELT (data->tokens, i, ce)
31053 tree c, cl;
31055 cp_parser_push_lexer_for_tokens (parser, ce);
31056 parser->lexer->in_pragma = true;
31057 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31058 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31059 cp_lexer_consume_token (parser->lexer);
31060 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31061 "#pragma omp declare simd", pragma_tok);
31062 cp_parser_pop_lexer (parser);
31063 if (cl)
31064 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31065 c = build_tree_list (get_identifier ("omp declare simd"), cl);
31066 TREE_CHAIN (c) = attrs;
31067 if (processing_template_decl)
31068 ATTR_IS_DEPENDENT (c) = 1;
31069 attrs = c;
31072 data->fndecl_seen = true;
31073 return attrs;
31077 /* OpenMP 4.0:
31078 # pragma omp declare target new-line
31079 declarations and definitions
31080 # pragma omp end declare target new-line */
31082 static void
31083 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31085 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31086 scope_chain->omp_declare_target_attribute++;
31089 static void
31090 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31092 const char *p = "";
31093 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31095 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31096 p = IDENTIFIER_POINTER (id);
31098 if (strcmp (p, "declare") == 0)
31100 cp_lexer_consume_token (parser->lexer);
31101 p = "";
31102 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31104 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31105 p = IDENTIFIER_POINTER (id);
31107 if (strcmp (p, "target") == 0)
31108 cp_lexer_consume_token (parser->lexer);
31109 else
31111 cp_parser_error (parser, "expected %<target%>");
31112 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31113 return;
31116 else
31118 cp_parser_error (parser, "expected %<declare%>");
31119 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31120 return;
31122 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31123 if (!scope_chain->omp_declare_target_attribute)
31124 error_at (pragma_tok->location,
31125 "%<#pragma omp end declare target%> without corresponding "
31126 "%<#pragma omp declare target%>");
31127 else
31128 scope_chain->omp_declare_target_attribute--;
31131 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
31132 expression and optional initializer clause of
31133 #pragma omp declare reduction. We store the expression(s) as
31134 either 3, 6 or 7 special statements inside of the artificial function's
31135 body. The first two statements are DECL_EXPRs for the artificial
31136 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
31137 expression that uses those variables.
31138 If there was any INITIALIZER clause, this is followed by further statements,
31139 the fourth and fifth statements are DECL_EXPRs for the artificial
31140 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
31141 constructor variant (first token after open paren is not omp_priv),
31142 then the sixth statement is a statement with the function call expression
31143 that uses the OMP_PRIV and optionally OMP_ORIG variable.
31144 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
31145 to initialize the OMP_PRIV artificial variable and there is seventh
31146 statement, a DECL_EXPR of the OMP_PRIV statement again. */
31148 static bool
31149 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
31151 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
31152 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
31153 type = TREE_TYPE (type);
31154 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
31155 DECL_ARTIFICIAL (omp_out) = 1;
31156 pushdecl (omp_out);
31157 add_decl_expr (omp_out);
31158 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
31159 DECL_ARTIFICIAL (omp_in) = 1;
31160 pushdecl (omp_in);
31161 add_decl_expr (omp_in);
31162 tree combiner;
31163 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
31165 keep_next_level (true);
31166 tree block = begin_omp_structured_block ();
31167 combiner = cp_parser_expression (parser);
31168 finish_expr_stmt (combiner);
31169 block = finish_omp_structured_block (block);
31170 add_stmt (block);
31172 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31173 return false;
31175 const char *p = "";
31176 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31178 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31179 p = IDENTIFIER_POINTER (id);
31182 if (strcmp (p, "initializer") == 0)
31184 cp_lexer_consume_token (parser->lexer);
31185 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31186 return false;
31188 p = "";
31189 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31191 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31192 p = IDENTIFIER_POINTER (id);
31195 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
31196 DECL_ARTIFICIAL (omp_priv) = 1;
31197 pushdecl (omp_priv);
31198 add_decl_expr (omp_priv);
31199 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
31200 DECL_ARTIFICIAL (omp_orig) = 1;
31201 pushdecl (omp_orig);
31202 add_decl_expr (omp_orig);
31204 keep_next_level (true);
31205 block = begin_omp_structured_block ();
31207 bool ctor = false;
31208 if (strcmp (p, "omp_priv") == 0)
31210 bool is_direct_init, is_non_constant_init;
31211 ctor = true;
31212 cp_lexer_consume_token (parser->lexer);
31213 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
31214 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
31215 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31216 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
31217 == CPP_CLOSE_PAREN
31218 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
31219 == CPP_CLOSE_PAREN))
31221 finish_omp_structured_block (block);
31222 error ("invalid initializer clause");
31223 return false;
31225 initializer = cp_parser_initializer (parser, &is_direct_init,
31226 &is_non_constant_init);
31227 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
31228 NULL_TREE, LOOKUP_ONLYCONVERTING);
31230 else
31232 cp_parser_parse_tentatively (parser);
31233 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
31234 /*check_dependency_p=*/true,
31235 /*template_p=*/NULL,
31236 /*declarator_p=*/false,
31237 /*optional_p=*/false);
31238 vec<tree, va_gc> *args;
31239 if (fn_name == error_mark_node
31240 || cp_parser_error_occurred (parser)
31241 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
31242 || ((args = cp_parser_parenthesized_expression_list
31243 (parser, non_attr, /*cast_p=*/false,
31244 /*allow_expansion_p=*/true,
31245 /*non_constant_p=*/NULL)),
31246 cp_parser_error_occurred (parser)))
31248 finish_omp_structured_block (block);
31249 cp_parser_abort_tentative_parse (parser);
31250 cp_parser_error (parser, "expected id-expression (arguments)");
31251 return false;
31253 unsigned int i;
31254 tree arg;
31255 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31256 if (arg == omp_priv
31257 || (TREE_CODE (arg) == ADDR_EXPR
31258 && TREE_OPERAND (arg, 0) == omp_priv))
31259 break;
31260 cp_parser_abort_tentative_parse (parser);
31261 if (arg == NULL_TREE)
31262 error ("one of the initializer call arguments should be %<omp_priv%>"
31263 " or %<&omp_priv%>");
31264 initializer = cp_parser_postfix_expression (parser, false, false, false,
31265 false, NULL);
31266 finish_expr_stmt (initializer);
31269 block = finish_omp_structured_block (block);
31270 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31271 add_stmt (block);
31273 if (ctor)
31274 add_decl_expr (omp_orig);
31276 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31277 return false;
31280 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31281 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31283 return true;
31286 /* OpenMP 4.0
31287 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31288 initializer-clause[opt] new-line
31290 initializer-clause:
31291 initializer (omp_priv initializer)
31292 initializer (function-name (argument-list)) */
31294 static void
31295 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31296 enum pragma_context)
31298 auto_vec<tree> types;
31299 enum tree_code reduc_code = ERROR_MARK;
31300 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31301 unsigned int i;
31302 cp_token *first_token;
31303 cp_token_cache *cp;
31304 int errs;
31305 void *p;
31307 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31308 p = obstack_alloc (&declarator_obstack, 0);
31310 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31311 goto fail;
31313 switch (cp_lexer_peek_token (parser->lexer)->type)
31315 case CPP_PLUS:
31316 reduc_code = PLUS_EXPR;
31317 break;
31318 case CPP_MULT:
31319 reduc_code = MULT_EXPR;
31320 break;
31321 case CPP_MINUS:
31322 reduc_code = MINUS_EXPR;
31323 break;
31324 case CPP_AND:
31325 reduc_code = BIT_AND_EXPR;
31326 break;
31327 case CPP_XOR:
31328 reduc_code = BIT_XOR_EXPR;
31329 break;
31330 case CPP_OR:
31331 reduc_code = BIT_IOR_EXPR;
31332 break;
31333 case CPP_AND_AND:
31334 reduc_code = TRUTH_ANDIF_EXPR;
31335 break;
31336 case CPP_OR_OR:
31337 reduc_code = TRUTH_ORIF_EXPR;
31338 break;
31339 case CPP_NAME:
31340 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31341 break;
31342 default:
31343 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31344 "%<|%>, %<&&%>, %<||%> or identifier");
31345 goto fail;
31348 if (reduc_code != ERROR_MARK)
31349 cp_lexer_consume_token (parser->lexer);
31351 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31352 if (reduc_id == error_mark_node)
31353 goto fail;
31355 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31356 goto fail;
31358 /* Types may not be defined in declare reduction type list. */
31359 const char *saved_message;
31360 saved_message = parser->type_definition_forbidden_message;
31361 parser->type_definition_forbidden_message
31362 = G_("types may not be defined in declare reduction type list");
31363 bool saved_colon_corrects_to_scope_p;
31364 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31365 parser->colon_corrects_to_scope_p = false;
31366 bool saved_colon_doesnt_start_class_def_p;
31367 saved_colon_doesnt_start_class_def_p
31368 = parser->colon_doesnt_start_class_def_p;
31369 parser->colon_doesnt_start_class_def_p = true;
31371 while (true)
31373 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31374 type = cp_parser_type_id (parser);
31375 if (type == error_mark_node)
31377 else if (ARITHMETIC_TYPE_P (type)
31378 && (orig_reduc_id == NULL_TREE
31379 || (TREE_CODE (type) != COMPLEX_TYPE
31380 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31381 "min") == 0
31382 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31383 "max") == 0))))
31384 error_at (loc, "predeclared arithmetic type %qT in "
31385 "%<#pragma omp declare reduction%>", type);
31386 else if (TREE_CODE (type) == FUNCTION_TYPE
31387 || TREE_CODE (type) == METHOD_TYPE
31388 || TREE_CODE (type) == ARRAY_TYPE)
31389 error_at (loc, "function or array type %qT in "
31390 "%<#pragma omp declare reduction%>", type);
31391 else if (TREE_CODE (type) == REFERENCE_TYPE)
31392 error_at (loc, "reference type %qT in "
31393 "%<#pragma omp declare reduction%>", type);
31394 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31395 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31396 "%<#pragma omp declare reduction%>", type);
31397 else
31398 types.safe_push (type);
31400 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31401 cp_lexer_consume_token (parser->lexer);
31402 else
31403 break;
31406 /* Restore the saved message. */
31407 parser->type_definition_forbidden_message = saved_message;
31408 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31409 parser->colon_doesnt_start_class_def_p
31410 = saved_colon_doesnt_start_class_def_p;
31412 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31413 || types.is_empty ())
31415 fail:
31416 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31417 goto done;
31420 first_token = cp_lexer_peek_token (parser->lexer);
31421 cp = NULL;
31422 errs = errorcount;
31423 FOR_EACH_VEC_ELT (types, i, type)
31425 tree fntype
31426 = build_function_type_list (void_type_node,
31427 cp_build_reference_type (type, false),
31428 NULL_TREE);
31429 tree this_reduc_id = reduc_id;
31430 if (!dependent_type_p (type))
31431 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31432 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31433 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31434 DECL_ARTIFICIAL (fndecl) = 1;
31435 DECL_EXTERNAL (fndecl) = 1;
31436 DECL_DECLARED_INLINE_P (fndecl) = 1;
31437 DECL_IGNORED_P (fndecl) = 1;
31438 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31439 DECL_ATTRIBUTES (fndecl)
31440 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31441 DECL_ATTRIBUTES (fndecl));
31442 if (processing_template_decl)
31443 fndecl = push_template_decl (fndecl);
31444 bool block_scope = false;
31445 tree block = NULL_TREE;
31446 if (current_function_decl)
31448 block_scope = true;
31449 DECL_CONTEXT (fndecl) = global_namespace;
31450 if (!processing_template_decl)
31451 pushdecl (fndecl);
31453 else if (current_class_type)
31455 if (cp == NULL)
31457 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31458 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31459 cp_lexer_consume_token (parser->lexer);
31460 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31461 goto fail;
31462 cp = cp_token_cache_new (first_token,
31463 cp_lexer_peek_nth_token (parser->lexer,
31464 2));
31466 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31467 finish_member_declaration (fndecl);
31468 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31469 DECL_PENDING_INLINE_P (fndecl) = 1;
31470 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31471 continue;
31473 else
31475 DECL_CONTEXT (fndecl) = current_namespace;
31476 pushdecl (fndecl);
31478 if (!block_scope)
31479 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31480 else
31481 block = begin_omp_structured_block ();
31482 if (cp)
31484 cp_parser_push_lexer_for_tokens (parser, cp);
31485 parser->lexer->in_pragma = true;
31487 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31489 if (!block_scope)
31490 finish_function (0);
31491 else
31492 DECL_CONTEXT (fndecl) = current_function_decl;
31493 if (cp)
31494 cp_parser_pop_lexer (parser);
31495 goto fail;
31497 if (cp)
31498 cp_parser_pop_lexer (parser);
31499 if (!block_scope)
31500 finish_function (0);
31501 else
31503 DECL_CONTEXT (fndecl) = current_function_decl;
31504 block = finish_omp_structured_block (block);
31505 if (TREE_CODE (block) == BIND_EXPR)
31506 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31507 else if (TREE_CODE (block) == STATEMENT_LIST)
31508 DECL_SAVED_TREE (fndecl) = block;
31509 if (processing_template_decl)
31510 add_decl_expr (fndecl);
31512 cp_check_omp_declare_reduction (fndecl);
31513 if (cp == NULL && types.length () > 1)
31514 cp = cp_token_cache_new (first_token,
31515 cp_lexer_peek_nth_token (parser->lexer, 2));
31516 if (errs != errorcount)
31517 break;
31520 cp_parser_require_pragma_eol (parser, pragma_tok);
31522 done:
31523 /* Free any declarators allocated. */
31524 obstack_free (&declarator_obstack, p);
31527 /* OpenMP 4.0
31528 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31529 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31530 initializer-clause[opt] new-line
31531 #pragma omp declare target new-line */
31533 static void
31534 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31535 enum pragma_context context)
31537 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31539 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31540 const char *p = IDENTIFIER_POINTER (id);
31542 if (strcmp (p, "simd") == 0)
31544 cp_lexer_consume_token (parser->lexer);
31545 cp_parser_omp_declare_simd (parser, pragma_tok,
31546 context);
31547 return;
31549 cp_ensure_no_omp_declare_simd (parser);
31550 if (strcmp (p, "reduction") == 0)
31552 cp_lexer_consume_token (parser->lexer);
31553 cp_parser_omp_declare_reduction (parser, pragma_tok,
31554 context);
31555 return;
31557 if (!flag_openmp) /* flag_openmp_simd */
31559 cp_parser_require_pragma_eol (parser, pragma_tok);
31560 return;
31562 if (strcmp (p, "target") == 0)
31564 cp_lexer_consume_token (parser->lexer);
31565 cp_parser_omp_declare_target (parser, pragma_tok);
31566 return;
31569 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31570 "or %<target%>");
31571 cp_parser_require_pragma_eol (parser, pragma_tok);
31574 /* Main entry point to OpenMP statement pragmas. */
31576 static void
31577 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31579 tree stmt;
31580 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31581 omp_clause_mask mask (0);
31583 switch (pragma_tok->pragma_kind)
31585 case PRAGMA_OMP_ATOMIC:
31586 cp_parser_omp_atomic (parser, pragma_tok);
31587 return;
31588 case PRAGMA_OMP_CRITICAL:
31589 stmt = cp_parser_omp_critical (parser, pragma_tok);
31590 break;
31591 case PRAGMA_OMP_DISTRIBUTE:
31592 strcpy (p_name, "#pragma omp");
31593 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31594 break;
31595 case PRAGMA_OMP_FOR:
31596 strcpy (p_name, "#pragma omp");
31597 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31598 break;
31599 case PRAGMA_OMP_MASTER:
31600 stmt = cp_parser_omp_master (parser, pragma_tok);
31601 break;
31602 case PRAGMA_OMP_ORDERED:
31603 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31604 break;
31605 case PRAGMA_OMP_PARALLEL:
31606 strcpy (p_name, "#pragma omp");
31607 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31608 break;
31609 case PRAGMA_OMP_SECTIONS:
31610 strcpy (p_name, "#pragma omp");
31611 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31612 break;
31613 case PRAGMA_OMP_SIMD:
31614 strcpy (p_name, "#pragma omp");
31615 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31616 break;
31617 case PRAGMA_OMP_SINGLE:
31618 stmt = cp_parser_omp_single (parser, pragma_tok);
31619 break;
31620 case PRAGMA_OMP_TASK:
31621 stmt = cp_parser_omp_task (parser, pragma_tok);
31622 break;
31623 case PRAGMA_OMP_TASKGROUP:
31624 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31625 break;
31626 case PRAGMA_OMP_TEAMS:
31627 strcpy (p_name, "#pragma omp");
31628 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31629 break;
31630 default:
31631 gcc_unreachable ();
31634 if (stmt)
31635 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31638 /* Transactional Memory parsing routines. */
31640 /* Parse a transaction attribute.
31642 txn-attribute:
31643 attribute
31644 [ [ identifier ] ]
31646 ??? Simplify this when C++0x bracket attributes are
31647 implemented properly. */
31649 static tree
31650 cp_parser_txn_attribute_opt (cp_parser *parser)
31652 cp_token *token;
31653 tree attr_name, attr = NULL;
31655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31656 return cp_parser_attributes_opt (parser);
31658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31659 return NULL_TREE;
31660 cp_lexer_consume_token (parser->lexer);
31661 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31662 goto error1;
31664 token = cp_lexer_peek_token (parser->lexer);
31665 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31667 token = cp_lexer_consume_token (parser->lexer);
31669 attr_name = (token->type == CPP_KEYWORD
31670 /* For keywords, use the canonical spelling,
31671 not the parsed identifier. */
31672 ? ridpointers[(int) token->keyword]
31673 : token->u.value);
31674 attr = build_tree_list (attr_name, NULL_TREE);
31676 else
31677 cp_parser_error (parser, "expected identifier");
31679 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31680 error1:
31681 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31682 return attr;
31685 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31687 transaction-statement:
31688 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31689 compound-statement
31690 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31693 static tree
31694 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31696 unsigned char old_in = parser->in_transaction;
31697 unsigned char this_in = 1, new_in;
31698 cp_token *token;
31699 tree stmt, attrs, noex;
31701 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31702 || keyword == RID_TRANSACTION_RELAXED);
31703 token = cp_parser_require_keyword (parser, keyword,
31704 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31705 : RT_TRANSACTION_RELAXED));
31706 gcc_assert (token != NULL);
31708 if (keyword == RID_TRANSACTION_RELAXED)
31709 this_in |= TM_STMT_ATTR_RELAXED;
31710 else
31712 attrs = cp_parser_txn_attribute_opt (parser);
31713 if (attrs)
31714 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31717 /* Parse a noexcept specification. */
31718 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31720 /* Keep track if we're in the lexical scope of an outer transaction. */
31721 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31723 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31725 parser->in_transaction = new_in;
31726 cp_parser_compound_statement (parser, NULL, false, false);
31727 parser->in_transaction = old_in;
31729 finish_transaction_stmt (stmt, NULL, this_in, noex);
31731 return stmt;
31734 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31736 transaction-expression:
31737 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31738 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31741 static tree
31742 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31744 unsigned char old_in = parser->in_transaction;
31745 unsigned char this_in = 1;
31746 cp_token *token;
31747 tree expr, noex;
31748 bool noex_expr;
31750 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31751 || keyword == RID_TRANSACTION_RELAXED);
31753 if (!flag_tm)
31754 error (keyword == RID_TRANSACTION_RELAXED
31755 ? G_("%<__transaction_relaxed%> without transactional memory "
31756 "support enabled")
31757 : G_("%<__transaction_atomic%> without transactional memory "
31758 "support enabled"));
31760 token = cp_parser_require_keyword (parser, keyword,
31761 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31762 : RT_TRANSACTION_RELAXED));
31763 gcc_assert (token != NULL);
31765 if (keyword == RID_TRANSACTION_RELAXED)
31766 this_in |= TM_STMT_ATTR_RELAXED;
31768 /* Set this early. This might mean that we allow transaction_cancel in
31769 an expression that we find out later actually has to be a constexpr.
31770 However, we expect that cxx_constant_value will be able to deal with
31771 this; also, if the noexcept has no constexpr, then what we parse next
31772 really is a transaction's body. */
31773 parser->in_transaction = this_in;
31775 /* Parse a noexcept specification. */
31776 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31777 true);
31779 if (!noex || !noex_expr
31780 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31782 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31784 expr = cp_parser_expression (parser);
31785 expr = finish_parenthesized_expr (expr);
31787 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31789 else
31791 /* The only expression that is available got parsed for the noexcept
31792 already. noexcept is true then. */
31793 expr = noex;
31794 noex = boolean_true_node;
31797 expr = build_transaction_expr (token->location, expr, this_in, noex);
31798 parser->in_transaction = old_in;
31800 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31801 return error_mark_node;
31803 return (flag_tm ? expr : error_mark_node);
31806 /* Parse a function-transaction-block.
31808 function-transaction-block:
31809 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31810 function-body
31811 __transaction_atomic txn-attribute[opt] function-try-block
31812 __transaction_relaxed ctor-initializer[opt] function-body
31813 __transaction_relaxed function-try-block
31816 static bool
31817 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31819 unsigned char old_in = parser->in_transaction;
31820 unsigned char new_in = 1;
31821 tree compound_stmt, stmt, attrs;
31822 bool ctor_initializer_p;
31823 cp_token *token;
31825 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31826 || keyword == RID_TRANSACTION_RELAXED);
31827 token = cp_parser_require_keyword (parser, keyword,
31828 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31829 : RT_TRANSACTION_RELAXED));
31830 gcc_assert (token != NULL);
31832 if (keyword == RID_TRANSACTION_RELAXED)
31833 new_in |= TM_STMT_ATTR_RELAXED;
31834 else
31836 attrs = cp_parser_txn_attribute_opt (parser);
31837 if (attrs)
31838 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31841 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31843 parser->in_transaction = new_in;
31845 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31846 ctor_initializer_p = cp_parser_function_try_block (parser);
31847 else
31848 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31849 (parser, /*in_function_try_block=*/false);
31851 parser->in_transaction = old_in;
31853 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31855 return ctor_initializer_p;
31858 /* Parse a __transaction_cancel statement.
31860 cancel-statement:
31861 __transaction_cancel txn-attribute[opt] ;
31862 __transaction_cancel txn-attribute[opt] throw-expression ;
31864 ??? Cancel and throw is not yet implemented. */
31866 static tree
31867 cp_parser_transaction_cancel (cp_parser *parser)
31869 cp_token *token;
31870 bool is_outer = false;
31871 tree stmt, attrs;
31873 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31874 RT_TRANSACTION_CANCEL);
31875 gcc_assert (token != NULL);
31877 attrs = cp_parser_txn_attribute_opt (parser);
31878 if (attrs)
31879 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31881 /* ??? Parse cancel-and-throw here. */
31883 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31885 if (!flag_tm)
31887 error_at (token->location, "%<__transaction_cancel%> without "
31888 "transactional memory support enabled");
31889 return error_mark_node;
31891 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31893 error_at (token->location, "%<__transaction_cancel%> within a "
31894 "%<__transaction_relaxed%>");
31895 return error_mark_node;
31897 else if (is_outer)
31899 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31900 && !is_tm_may_cancel_outer (current_function_decl))
31902 error_at (token->location, "outer %<__transaction_cancel%> not "
31903 "within outer %<__transaction_atomic%>");
31904 error_at (token->location,
31905 " or a %<transaction_may_cancel_outer%> function");
31906 return error_mark_node;
31909 else if (parser->in_transaction == 0)
31911 error_at (token->location, "%<__transaction_cancel%> not within "
31912 "%<__transaction_atomic%>");
31913 return error_mark_node;
31916 stmt = build_tm_abort_call (token->location, is_outer);
31917 add_stmt (stmt);
31919 return stmt;
31922 /* The parser. */
31924 static GTY (()) cp_parser *the_parser;
31927 /* Special handling for the first token or line in the file. The first
31928 thing in the file might be #pragma GCC pch_preprocess, which loads a
31929 PCH file, which is a GC collection point. So we need to handle this
31930 first pragma without benefit of an existing lexer structure.
31932 Always returns one token to the caller in *FIRST_TOKEN. This is
31933 either the true first token of the file, or the first token after
31934 the initial pragma. */
31936 static void
31937 cp_parser_initial_pragma (cp_token *first_token)
31939 tree name = NULL;
31941 cp_lexer_get_preprocessor_token (NULL, first_token);
31942 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31943 return;
31945 cp_lexer_get_preprocessor_token (NULL, first_token);
31946 if (first_token->type == CPP_STRING)
31948 name = first_token->u.value;
31950 cp_lexer_get_preprocessor_token (NULL, first_token);
31951 if (first_token->type != CPP_PRAGMA_EOL)
31952 error_at (first_token->location,
31953 "junk at end of %<#pragma GCC pch_preprocess%>");
31955 else
31956 error_at (first_token->location, "expected string literal");
31958 /* Skip to the end of the pragma. */
31959 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31960 cp_lexer_get_preprocessor_token (NULL, first_token);
31962 /* Now actually load the PCH file. */
31963 if (name)
31964 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31966 /* Read one more token to return to our caller. We have to do this
31967 after reading the PCH file in, since its pointers have to be
31968 live. */
31969 cp_lexer_get_preprocessor_token (NULL, first_token);
31972 /* Parses the grainsize pragma for the _Cilk_for statement.
31973 Syntax:
31974 #pragma cilk grainsize = <VALUE>. */
31976 static void
31977 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
31979 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
31981 tree exp = cp_parser_binary_expression (parser, false, false,
31982 PREC_NOT_OPERATOR, NULL);
31983 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31984 if (!exp || exp == error_mark_node)
31986 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
31987 return;
31990 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
31991 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31992 cp_parser_cilk_for (parser, exp);
31993 else
31994 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
31995 "%<#pragma cilk grainsize%> is not followed by "
31996 "%<_Cilk_for%>");
31997 return;
31999 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32002 /* Normal parsing of a pragma token. Here we can (and must) use the
32003 regular lexer. */
32005 static bool
32006 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32008 cp_token *pragma_tok;
32009 unsigned int id;
32011 pragma_tok = cp_lexer_consume_token (parser->lexer);
32012 gcc_assert (pragma_tok->type == CPP_PRAGMA);
32013 parser->lexer->in_pragma = true;
32015 id = pragma_tok->pragma_kind;
32016 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32017 cp_ensure_no_omp_declare_simd (parser);
32018 switch (id)
32020 case PRAGMA_GCC_PCH_PREPROCESS:
32021 error_at (pragma_tok->location,
32022 "%<#pragma GCC pch_preprocess%> must be first");
32023 break;
32025 case PRAGMA_OMP_BARRIER:
32026 switch (context)
32028 case pragma_compound:
32029 cp_parser_omp_barrier (parser, pragma_tok);
32030 return false;
32031 case pragma_stmt:
32032 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32033 "used in compound statements");
32034 break;
32035 default:
32036 goto bad_stmt;
32038 break;
32040 case PRAGMA_OMP_FLUSH:
32041 switch (context)
32043 case pragma_compound:
32044 cp_parser_omp_flush (parser, pragma_tok);
32045 return false;
32046 case pragma_stmt:
32047 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32048 "used in compound statements");
32049 break;
32050 default:
32051 goto bad_stmt;
32053 break;
32055 case PRAGMA_OMP_TASKWAIT:
32056 switch (context)
32058 case pragma_compound:
32059 cp_parser_omp_taskwait (parser, pragma_tok);
32060 return false;
32061 case pragma_stmt:
32062 error_at (pragma_tok->location,
32063 "%<#pragma omp taskwait%> may only be "
32064 "used in compound statements");
32065 break;
32066 default:
32067 goto bad_stmt;
32069 break;
32071 case PRAGMA_OMP_TASKYIELD:
32072 switch (context)
32074 case pragma_compound:
32075 cp_parser_omp_taskyield (parser, pragma_tok);
32076 return false;
32077 case pragma_stmt:
32078 error_at (pragma_tok->location,
32079 "%<#pragma omp taskyield%> may only be "
32080 "used in compound statements");
32081 break;
32082 default:
32083 goto bad_stmt;
32085 break;
32087 case PRAGMA_OMP_CANCEL:
32088 switch (context)
32090 case pragma_compound:
32091 cp_parser_omp_cancel (parser, pragma_tok);
32092 return false;
32093 case pragma_stmt:
32094 error_at (pragma_tok->location,
32095 "%<#pragma omp cancel%> may only be "
32096 "used in compound statements");
32097 break;
32098 default:
32099 goto bad_stmt;
32101 break;
32103 case PRAGMA_OMP_CANCELLATION_POINT:
32104 switch (context)
32106 case pragma_compound:
32107 cp_parser_omp_cancellation_point (parser, pragma_tok);
32108 return false;
32109 case pragma_stmt:
32110 error_at (pragma_tok->location,
32111 "%<#pragma omp cancellation point%> may only be "
32112 "used in compound statements");
32113 break;
32114 default:
32115 goto bad_stmt;
32117 break;
32119 case PRAGMA_OMP_THREADPRIVATE:
32120 cp_parser_omp_threadprivate (parser, pragma_tok);
32121 return false;
32123 case PRAGMA_OMP_DECLARE_REDUCTION:
32124 cp_parser_omp_declare (parser, pragma_tok, context);
32125 return false;
32127 case PRAGMA_OMP_ATOMIC:
32128 case PRAGMA_OMP_CRITICAL:
32129 case PRAGMA_OMP_DISTRIBUTE:
32130 case PRAGMA_OMP_FOR:
32131 case PRAGMA_OMP_MASTER:
32132 case PRAGMA_OMP_ORDERED:
32133 case PRAGMA_OMP_PARALLEL:
32134 case PRAGMA_OMP_SECTIONS:
32135 case PRAGMA_OMP_SIMD:
32136 case PRAGMA_OMP_SINGLE:
32137 case PRAGMA_OMP_TASK:
32138 case PRAGMA_OMP_TASKGROUP:
32139 case PRAGMA_OMP_TEAMS:
32140 if (context != pragma_stmt && context != pragma_compound)
32141 goto bad_stmt;
32142 cp_parser_omp_construct (parser, pragma_tok);
32143 return true;
32145 case PRAGMA_OMP_TARGET:
32146 return cp_parser_omp_target (parser, pragma_tok, context);
32148 case PRAGMA_OMP_END_DECLARE_TARGET:
32149 cp_parser_omp_end_declare_target (parser, pragma_tok);
32150 return false;
32152 case PRAGMA_OMP_SECTION:
32153 error_at (pragma_tok->location,
32154 "%<#pragma omp section%> may only be used in "
32155 "%<#pragma omp sections%> construct");
32156 break;
32158 case PRAGMA_IVDEP:
32160 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32161 cp_token *tok;
32162 tok = cp_lexer_peek_token (the_parser->lexer);
32163 if (tok->type != CPP_KEYWORD
32164 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
32165 && tok->keyword != RID_DO))
32167 cp_parser_error (parser, "for, while or do statement expected");
32168 return false;
32170 cp_parser_iteration_statement (parser, true);
32171 return true;
32174 case PRAGMA_CILK_SIMD:
32175 if (context == pragma_external)
32177 error_at (pragma_tok->location,
32178 "%<#pragma simd%> must be inside a function");
32179 break;
32181 cp_parser_cilk_simd (parser, pragma_tok);
32182 return true;
32184 case PRAGMA_CILK_GRAINSIZE:
32185 if (context == pragma_external)
32187 error_at (pragma_tok->location,
32188 "%<#pragma cilk grainsize%> must be inside a function");
32189 break;
32192 /* Ignore the pragma if Cilk Plus is not enabled. */
32193 if (flag_cilkplus)
32195 cp_parser_cilk_grainsize (parser, pragma_tok);
32196 return true;
32198 else
32200 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
32201 "%<#pragma cilk grainsize%>");
32202 break;
32205 default:
32206 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
32207 c_invoke_pragma_handler (id);
32208 break;
32210 bad_stmt:
32211 cp_parser_error (parser, "expected declaration specifiers");
32212 break;
32215 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32216 return false;
32219 /* The interface the pragma parsers have to the lexer. */
32221 enum cpp_ttype
32222 pragma_lex (tree *value)
32224 cp_token *tok;
32225 enum cpp_ttype ret;
32227 tok = cp_lexer_peek_token (the_parser->lexer);
32229 ret = tok->type;
32230 *value = tok->u.value;
32232 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
32233 ret = CPP_EOF;
32234 else if (ret == CPP_STRING)
32235 *value = cp_parser_string_literal (the_parser, false, false);
32236 else
32238 cp_lexer_consume_token (the_parser->lexer);
32239 if (ret == CPP_KEYWORD)
32240 ret = CPP_NAME;
32243 return ret;
32247 /* External interface. */
32249 /* Parse one entire translation unit. */
32251 void
32252 c_parse_file (void)
32254 static bool already_called = false;
32256 if (already_called)
32257 fatal_error ("inter-module optimizations not implemented for C++");
32258 already_called = true;
32260 the_parser = cp_parser_new ();
32261 push_deferring_access_checks (flag_access_control
32262 ? dk_no_deferred : dk_no_check);
32263 cp_parser_translation_unit (the_parser);
32264 the_parser = NULL;
32267 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32268 vectorlength clause:
32269 Syntax:
32270 vectorlength ( constant-expression ) */
32272 static tree
32273 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32274 bool is_simd_fn)
32276 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32277 tree expr;
32278 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32279 safelen clause. Thus, vectorlength is represented as OMP 4.0
32280 safelen. For SIMD-enabled function it is represented by OMP 4.0
32281 simdlen. */
32282 if (!is_simd_fn)
32283 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32284 loc);
32285 else
32286 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32287 loc);
32289 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32290 return error_mark_node;
32292 expr = cp_parser_constant_expression (parser);
32293 expr = maybe_constant_value (expr);
32295 /* If expr == error_mark_node, then don't emit any errors nor
32296 create a clause. if any of the above functions returns
32297 error mark node then they would have emitted an error message. */
32298 if (expr == error_mark_node)
32300 else if (!TREE_TYPE (expr)
32301 || !TREE_CONSTANT (expr)
32302 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32303 error_at (loc, "vectorlength must be an integer constant");
32304 else if (TREE_CONSTANT (expr)
32305 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32306 error_at (loc, "vectorlength must be a power of 2");
32307 else
32309 tree c;
32310 if (!is_simd_fn)
32312 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32313 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32314 OMP_CLAUSE_CHAIN (c) = clauses;
32315 clauses = c;
32317 else
32319 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32320 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32321 OMP_CLAUSE_CHAIN (c) = clauses;
32322 clauses = c;
32326 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32327 return error_mark_node;
32328 return clauses;
32331 /* Handles the Cilk Plus #pragma simd linear clause.
32332 Syntax:
32333 linear ( simd-linear-variable-list )
32335 simd-linear-variable-list:
32336 simd-linear-variable
32337 simd-linear-variable-list , simd-linear-variable
32339 simd-linear-variable:
32340 id-expression
32341 id-expression : simd-linear-step
32343 simd-linear-step:
32344 conditional-expression */
32346 static tree
32347 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32349 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32351 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32352 return clauses;
32353 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32355 cp_parser_error (parser, "expected identifier");
32356 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32357 return error_mark_node;
32360 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32361 parser->colon_corrects_to_scope_p = false;
32362 while (1)
32364 cp_token *token = cp_lexer_peek_token (parser->lexer);
32365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32367 cp_parser_error (parser, "expected variable-name");
32368 clauses = error_mark_node;
32369 break;
32372 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32373 false, false);
32374 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32375 token->location);
32376 if (decl == error_mark_node)
32378 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32379 token->location);
32380 clauses = error_mark_node;
32382 else
32384 tree e = NULL_TREE;
32385 tree step_size = integer_one_node;
32387 /* If present, parse the linear step. Otherwise, assume the default
32388 value of 1. */
32389 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32391 cp_lexer_consume_token (parser->lexer);
32393 e = cp_parser_assignment_expression (parser);
32394 e = maybe_constant_value (e);
32396 if (e == error_mark_node)
32398 /* If an error has occurred, then the whole pragma is
32399 considered ill-formed. Thus, no reason to keep
32400 parsing. */
32401 clauses = error_mark_node;
32402 break;
32404 else if (type_dependent_expression_p (e)
32405 || value_dependent_expression_p (e)
32406 || (TREE_TYPE (e)
32407 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32408 && (TREE_CONSTANT (e)
32409 || DECL_P (e))))
32410 step_size = e;
32411 else
32412 cp_parser_error (parser,
32413 "step size must be an integer constant "
32414 "expression or an integer variable");
32417 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32418 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32419 OMP_CLAUSE_DECL (l) = decl;
32420 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32421 OMP_CLAUSE_CHAIN (l) = clauses;
32422 clauses = l;
32424 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32425 cp_lexer_consume_token (parser->lexer);
32426 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32427 break;
32428 else
32430 error_at (cp_lexer_peek_token (parser->lexer)->location,
32431 "expected %<,%> or %<)%> after %qE", decl);
32432 clauses = error_mark_node;
32433 break;
32436 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32437 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32438 return clauses;
32441 /* Returns the name of the next clause. If the clause is not
32442 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32443 token is not consumed. Otherwise, the appropriate enum from the
32444 pragma_simd_clause is returned and the token is consumed. */
32446 static pragma_omp_clause
32447 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32449 pragma_omp_clause clause_type;
32450 cp_token *token = cp_lexer_peek_token (parser->lexer);
32452 if (token->keyword == RID_PRIVATE)
32453 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32454 else if (!token->u.value || token->type != CPP_NAME)
32455 return PRAGMA_CILK_CLAUSE_NONE;
32456 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32457 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32458 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32459 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32460 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32461 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32462 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32463 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32464 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32465 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32466 else
32467 return PRAGMA_CILK_CLAUSE_NONE;
32469 cp_lexer_consume_token (parser->lexer);
32470 return clause_type;
32473 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32475 static tree
32476 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32478 tree clauses = NULL_TREE;
32480 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32481 && clauses != error_mark_node)
32483 pragma_omp_clause c_kind;
32484 c_kind = cp_parser_cilk_simd_clause_name (parser);
32485 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32486 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32487 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32488 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32489 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32490 /* Use the OpenMP 4.0 equivalent function. */
32491 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32492 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32493 /* Use the OpenMP 4.0 equivalent function. */
32494 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32495 clauses);
32496 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32497 /* Use the OMP 4.0 equivalent function. */
32498 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32499 clauses);
32500 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32501 /* Use the OMP 4.0 equivalent function. */
32502 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32503 else
32505 clauses = error_mark_node;
32506 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32507 break;
32511 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32513 if (clauses == error_mark_node)
32514 return error_mark_node;
32515 else
32516 return c_finish_cilk_clauses (clauses);
32519 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32521 static void
32522 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32524 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32526 if (clauses == error_mark_node)
32527 return;
32529 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32531 error_at (cp_lexer_peek_token (parser->lexer)->location,
32532 "for statement expected");
32533 return;
32536 tree sb = begin_omp_structured_block ();
32537 int save = cp_parser_begin_omp_structured_block (parser);
32538 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32539 if (ret)
32540 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32541 cp_parser_end_omp_structured_block (parser, save);
32542 add_stmt (finish_omp_structured_block (sb));
32545 /* Main entry-point for parsing Cilk Plus _Cilk_for
32546 loops. The return value is error_mark_node
32547 when errors happen and CILK_FOR tree on success. */
32549 static tree
32550 cp_parser_cilk_for (cp_parser *parser, tree grain)
32552 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
32553 gcc_unreachable ();
32555 tree sb = begin_omp_structured_block ();
32556 int save = cp_parser_begin_omp_structured_block (parser);
32558 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
32559 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
32560 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
32561 clauses = finish_omp_clauses (clauses);
32563 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
32564 if (ret)
32565 cpp_validate_cilk_plus_loop (ret);
32566 else
32567 ret = error_mark_node;
32569 cp_parser_end_omp_structured_block (parser, save);
32570 add_stmt (finish_omp_structured_block (sb));
32571 return ret;
32574 /* Create an identifier for a generic parameter type (a synthesized
32575 template parameter implied by `auto' or a concept identifier). */
32577 static GTY(()) int generic_parm_count;
32578 static tree
32579 make_generic_type_name ()
32581 char buf[32];
32582 sprintf (buf, "auto:%d", ++generic_parm_count);
32583 return get_identifier (buf);
32586 /* Predicate that behaves as is_auto_or_concept but matches the parent
32587 node of the generic type rather than the generic type itself. This
32588 allows for type transformation in add_implicit_template_parms. */
32590 static inline bool
32591 tree_type_is_auto_or_concept (const_tree t)
32593 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32596 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32597 (creating a new template parameter list if necessary). Returns the newly
32598 created template type parm. */
32600 tree
32601 synthesize_implicit_template_parm (cp_parser *parser)
32603 gcc_assert (current_binding_level->kind == sk_function_parms);
32605 /* We are either continuing a function template that already contains implicit
32606 template parameters, creating a new fully-implicit function template, or
32607 extending an existing explicit function template with implicit template
32608 parameters. */
32610 cp_binding_level *const entry_scope = current_binding_level;
32612 bool become_template = false;
32613 cp_binding_level *parent_scope = 0;
32615 if (parser->implicit_template_scope)
32617 gcc_assert (parser->implicit_template_parms);
32619 current_binding_level = parser->implicit_template_scope;
32621 else
32623 /* Roll back to the existing template parameter scope (in the case of
32624 extending an explicit function template) or introduce a new template
32625 parameter scope ahead of the function parameter scope (or class scope
32626 in the case of out-of-line member definitions). The function scope is
32627 added back after template parameter synthesis below. */
32629 cp_binding_level *scope = entry_scope;
32631 while (scope->kind == sk_function_parms)
32633 parent_scope = scope;
32634 scope = scope->level_chain;
32636 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32638 /* If not defining a class, then any class scope is a scope level in
32639 an out-of-line member definition. In this case simply wind back
32640 beyond the first such scope to inject the template parameter list.
32641 Otherwise wind back to the class being defined. The latter can
32642 occur in class member friend declarations such as:
32644 class A {
32645 void foo (auto);
32647 class B {
32648 friend void A::foo (auto);
32651 The template parameter list synthesized for the friend declaration
32652 must be injected in the scope of 'B'. This can also occur in
32653 erroneous cases such as:
32655 struct A {
32656 struct B {
32657 void foo (auto);
32659 void B::foo (auto) {}
32662 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32663 but, nevertheless, the template parameter list synthesized for the
32664 declarator should be injected into the scope of 'A' as if the
32665 ill-formed template was specified explicitly. */
32667 while (scope->kind == sk_class && !scope->defining_class_p)
32669 parent_scope = scope;
32670 scope = scope->level_chain;
32674 current_binding_level = scope;
32676 if (scope->kind != sk_template_parms
32677 || !function_being_declared_is_template_p (parser))
32679 /* Introduce a new template parameter list for implicit template
32680 parameters. */
32682 become_template = true;
32684 parser->implicit_template_scope
32685 = begin_scope (sk_template_parms, NULL);
32687 ++processing_template_decl;
32689 parser->fully_implicit_function_template_p = true;
32690 ++parser->num_template_parameter_lists;
32692 else
32694 /* Synthesize implicit template parameters at the end of the explicit
32695 template parameter list. */
32697 gcc_assert (current_template_parms);
32699 parser->implicit_template_scope = scope;
32701 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32702 parser->implicit_template_parms
32703 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32707 /* Synthesize a new template parameter and track the current template
32708 parameter chain with implicit_template_parms. */
32710 tree synth_id = make_generic_type_name ();
32711 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32712 synth_id);
32713 tree new_parm
32714 = process_template_parm (parser->implicit_template_parms,
32715 input_location,
32716 build_tree_list (NULL_TREE, synth_tmpl_parm),
32717 /*non_type=*/false,
32718 /*param_pack=*/false);
32721 if (parser->implicit_template_parms)
32722 parser->implicit_template_parms
32723 = TREE_CHAIN (parser->implicit_template_parms);
32724 else
32725 parser->implicit_template_parms = new_parm;
32727 tree new_type = TREE_TYPE (getdecls ());
32729 /* If creating a fully implicit function template, start the new implicit
32730 template parameter list with this synthesized type, otherwise grow the
32731 current template parameter list. */
32733 if (become_template)
32735 parent_scope->level_chain = current_binding_level;
32737 tree new_parms = make_tree_vec (1);
32738 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32739 current_template_parms = tree_cons (size_int (processing_template_decl),
32740 new_parms, current_template_parms);
32742 else
32744 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32745 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32746 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32747 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32750 current_binding_level = entry_scope;
32752 return new_type;
32755 /* Finish the declaration of a fully implicit function template. Such a
32756 template has no explicit template parameter list so has not been through the
32757 normal template head and tail processing. synthesize_implicit_template_parm
32758 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32759 provided if the declaration is a class member such that its template
32760 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32761 form is returned. Otherwise NULL_TREE is returned. */
32763 tree
32764 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32766 gcc_assert (parser->fully_implicit_function_template_p);
32768 if (member_decl_opt && member_decl_opt != error_mark_node
32769 && DECL_VIRTUAL_P (member_decl_opt))
32771 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32772 "implicit templates may not be %<virtual%>");
32773 DECL_VIRTUAL_P (member_decl_opt) = false;
32776 if (member_decl_opt)
32777 member_decl_opt = finish_member_template_decl (member_decl_opt);
32778 end_template_decl ();
32780 parser->fully_implicit_function_template_p = false;
32781 --parser->num_template_parameter_lists;
32783 return member_decl_opt;
32786 #include "gt-cp-parser.h"