Implement P0258R2 - helper for C++17
[official-gcc.git] / gcc / cp / parser.c
blobc2bd4421e14ec638ca3b2f7991b00f6b571f3c48
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2016 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 "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "omp-low.h"
39 #include "gomp-constants.h"
40 #include "c-family/c-indentation.h"
41 #include "context.h"
42 #include "cp-cilkplus.h"
43 #include "gcc-rich-location.h"
44 #include "tree-iterator.h"
47 /* The lexer. */
49 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
50 and c-lex.c) and the C++ parser. */
52 static cp_token eof_token =
54 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
57 /* The various kinds of non integral constant we encounter. */
58 enum non_integral_constant {
59 NIC_NONE,
60 /* floating-point literal */
61 NIC_FLOAT,
62 /* %<this%> */
63 NIC_THIS,
64 /* %<__FUNCTION__%> */
65 NIC_FUNC_NAME,
66 /* %<__PRETTY_FUNCTION__%> */
67 NIC_PRETTY_FUNC,
68 /* %<__func__%> */
69 NIC_C99_FUNC,
70 /* "%<va_arg%> */
71 NIC_VA_ARG,
72 /* a cast */
73 NIC_CAST,
74 /* %<typeid%> operator */
75 NIC_TYPEID,
76 /* non-constant compound literals */
77 NIC_NCC,
78 /* a function call */
79 NIC_FUNC_CALL,
80 /* an increment */
81 NIC_INC,
82 /* an decrement */
83 NIC_DEC,
84 /* an array reference */
85 NIC_ARRAY_REF,
86 /* %<->%> */
87 NIC_ARROW,
88 /* %<.%> */
89 NIC_POINT,
90 /* the address of a label */
91 NIC_ADDR_LABEL,
92 /* %<*%> */
93 NIC_STAR,
94 /* %<&%> */
95 NIC_ADDR,
96 /* %<++%> */
97 NIC_PREINCREMENT,
98 /* %<--%> */
99 NIC_PREDECREMENT,
100 /* %<new%> */
101 NIC_NEW,
102 /* %<delete%> */
103 NIC_DEL,
104 /* calls to overloaded operators */
105 NIC_OVERLOADED,
106 /* an assignment */
107 NIC_ASSIGNMENT,
108 /* a comma operator */
109 NIC_COMMA,
110 /* a call to a constructor */
111 NIC_CONSTRUCTOR,
112 /* a transaction expression */
113 NIC_TRANSACTION
116 /* The various kinds of errors about name-lookup failing. */
117 enum name_lookup_error {
118 /* NULL */
119 NLE_NULL,
120 /* is not a type */
121 NLE_TYPE,
122 /* is not a class or namespace */
123 NLE_CXX98,
124 /* is not a class, namespace, or enumeration */
125 NLE_NOT_CXX98
128 /* The various kinds of required token */
129 enum required_token {
130 RT_NONE,
131 RT_SEMICOLON, /* ';' */
132 RT_OPEN_PAREN, /* '(' */
133 RT_CLOSE_BRACE, /* '}' */
134 RT_OPEN_BRACE, /* '{' */
135 RT_CLOSE_SQUARE, /* ']' */
136 RT_OPEN_SQUARE, /* '[' */
137 RT_COMMA, /* ',' */
138 RT_SCOPE, /* '::' */
139 RT_LESS, /* '<' */
140 RT_GREATER, /* '>' */
141 RT_EQ, /* '=' */
142 RT_ELLIPSIS, /* '...' */
143 RT_MULT, /* '*' */
144 RT_COMPL, /* '~' */
145 RT_COLON, /* ':' */
146 RT_COLON_SCOPE, /* ':' or '::' */
147 RT_CLOSE_PAREN, /* ')' */
148 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
149 RT_PRAGMA_EOL, /* end of line */
150 RT_NAME, /* identifier */
152 /* The type is CPP_KEYWORD */
153 RT_NEW, /* new */
154 RT_DELETE, /* delete */
155 RT_RETURN, /* return */
156 RT_WHILE, /* while */
157 RT_EXTERN, /* extern */
158 RT_STATIC_ASSERT, /* static_assert */
159 RT_DECLTYPE, /* decltype */
160 RT_OPERATOR, /* operator */
161 RT_CLASS, /* class */
162 RT_TEMPLATE, /* template */
163 RT_NAMESPACE, /* namespace */
164 RT_USING, /* using */
165 RT_ASM, /* asm */
166 RT_TRY, /* try */
167 RT_CATCH, /* catch */
168 RT_THROW, /* throw */
169 RT_LABEL, /* __label__ */
170 RT_AT_TRY, /* @try */
171 RT_AT_SYNCHRONIZED, /* @synchronized */
172 RT_AT_THROW, /* @throw */
174 RT_SELECT, /* selection-statement */
175 RT_INTERATION, /* iteration-statement */
176 RT_JUMP, /* jump-statement */
177 RT_CLASS_KEY, /* class-key */
178 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
179 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
180 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
181 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
185 reverting it on destruction. */
187 class type_id_in_expr_sentinel
189 cp_parser *parser;
190 bool saved;
191 public:
192 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
193 : parser (parser),
194 saved (parser->in_type_id_in_expr_p)
195 { parser->in_type_id_in_expr_p = set; }
196 ~type_id_in_expr_sentinel ()
197 { parser->in_type_id_in_expr_p = saved; }
200 /* Prototypes. */
202 static cp_lexer *cp_lexer_new_main
203 (void);
204 static cp_lexer *cp_lexer_new_from_tokens
205 (cp_token_cache *tokens);
206 static void cp_lexer_destroy
207 (cp_lexer *);
208 static int cp_lexer_saving_tokens
209 (const cp_lexer *);
210 static cp_token *cp_lexer_token_at
211 (cp_lexer *, cp_token_position);
212 static void cp_lexer_get_preprocessor_token
213 (cp_lexer *, cp_token *);
214 static inline cp_token *cp_lexer_peek_token
215 (cp_lexer *);
216 static cp_token *cp_lexer_peek_nth_token
217 (cp_lexer *, size_t);
218 static inline bool cp_lexer_next_token_is
219 (cp_lexer *, enum cpp_ttype);
220 static bool cp_lexer_next_token_is_not
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_keyword
223 (cp_lexer *, enum rid);
224 static cp_token *cp_lexer_consume_token
225 (cp_lexer *);
226 static void cp_lexer_purge_token
227 (cp_lexer *);
228 static void cp_lexer_purge_tokens_after
229 (cp_lexer *, cp_token_position);
230 static void cp_lexer_save_tokens
231 (cp_lexer *);
232 static void cp_lexer_commit_tokens
233 (cp_lexer *);
234 static void cp_lexer_rollback_tokens
235 (cp_lexer *);
236 static void cp_lexer_print_token
237 (FILE *, cp_token *);
238 static inline bool cp_lexer_debugging_p
239 (cp_lexer *);
240 static void cp_lexer_start_debugging
241 (cp_lexer *) ATTRIBUTE_UNUSED;
242 static void cp_lexer_stop_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static cp_token_cache *cp_token_cache_new
246 (cp_token *, cp_token *);
248 static void cp_parser_initial_pragma
249 (cp_token *);
251 static tree cp_literal_operator_id
252 (const char *);
254 static void cp_parser_cilk_simd
255 (cp_parser *, cp_token *, bool *);
256 static tree cp_parser_cilk_for
257 (cp_parser *, tree, bool *);
258 static bool cp_parser_omp_declare_reduction_exprs
259 (tree, cp_parser *);
260 static tree cp_parser_cilk_simd_vectorlength
261 (cp_parser *, tree, bool);
262 static void cp_finalize_oacc_routine
263 (cp_parser *, tree, bool);
265 /* Manifest constants. */
266 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
267 #define CP_SAVED_TOKEN_STACK 5
269 /* Variables. */
271 /* The stream to which debugging output should be written. */
272 static FILE *cp_lexer_debug_stream;
274 /* Nonzero if we are parsing an unevaluated operand: an operand to
275 sizeof, typeof, or alignof. */
276 int cp_unevaluated_operand;
278 /* Dump up to NUM tokens in BUFFER to FILE starting with token
279 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
280 first token in BUFFER. If NUM is 0, dump all the tokens. If
281 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
282 highlighted by surrounding it in [[ ]]. */
284 static void
285 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
286 cp_token *start_token, unsigned num,
287 cp_token *curr_token)
289 unsigned i, nprinted;
290 cp_token *token;
291 bool do_print;
293 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
295 if (buffer == NULL)
296 return;
298 if (num == 0)
299 num = buffer->length ();
301 if (start_token == NULL)
302 start_token = buffer->address ();
304 if (start_token > buffer->address ())
306 cp_lexer_print_token (file, &(*buffer)[0]);
307 fprintf (file, " ... ");
310 do_print = false;
311 nprinted = 0;
312 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
314 if (token == start_token)
315 do_print = true;
317 if (!do_print)
318 continue;
320 nprinted++;
321 if (token == curr_token)
322 fprintf (file, "[[");
324 cp_lexer_print_token (file, token);
326 if (token == curr_token)
327 fprintf (file, "]]");
329 switch (token->type)
331 case CPP_SEMICOLON:
332 case CPP_OPEN_BRACE:
333 case CPP_CLOSE_BRACE:
334 case CPP_EOF:
335 fputc ('\n', file);
336 break;
338 default:
339 fputc (' ', file);
343 if (i == num && i < buffer->length ())
345 fprintf (file, " ... ");
346 cp_lexer_print_token (file, &buffer->last ());
349 fprintf (file, "\n");
353 /* Dump all tokens in BUFFER to stderr. */
355 void
356 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
358 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> &ref)
364 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
367 DEBUG_FUNCTION void
368 debug (vec<cp_token, va_gc> *ptr)
370 if (ptr)
371 debug (*ptr);
372 else
373 fprintf (stderr, "<nil>\n");
377 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
378 description for T. */
380 static void
381 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
383 if (t)
385 fprintf (file, "%s: ", desc);
386 print_node_brief (file, "", t, 0);
391 /* Dump parser context C to FILE. */
393 static void
394 cp_debug_print_context (FILE *file, cp_parser_context *c)
396 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
397 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
398 print_node_brief (file, "", c->object_type, 0);
399 fprintf (file, "}\n");
403 /* Print the stack of parsing contexts to FILE starting with FIRST. */
405 static void
406 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
408 unsigned i;
409 cp_parser_context *c;
411 fprintf (file, "Parsing context stack:\n");
412 for (i = 0, c = first; c; c = c->next, i++)
414 fprintf (file, "\t#%u: ", i);
415 cp_debug_print_context (file, c);
420 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
422 static void
423 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
425 if (flag)
426 fprintf (file, "%s: true\n", desc);
430 /* Print an unparsed function entry UF to FILE. */
432 static void
433 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
435 unsigned i;
436 cp_default_arg_entry *default_arg_fn;
437 tree fn;
439 fprintf (file, "\tFunctions with default args:\n");
440 for (i = 0;
441 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
442 i++)
444 fprintf (file, "\t\tClass type: ");
445 print_node_brief (file, "", default_arg_fn->class_type, 0);
446 fprintf (file, "\t\tDeclaration: ");
447 print_node_brief (file, "", default_arg_fn->decl, 0);
448 fprintf (file, "\n");
451 fprintf (file, "\n\tFunctions with definitions that require "
452 "post-processing\n\t\t");
453 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
455 print_node_brief (file, "", fn, 0);
456 fprintf (file, " ");
458 fprintf (file, "\n");
460 fprintf (file, "\n\tNon-static data members with initializers that require "
461 "post-processing\n\t\t");
462 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
464 print_node_brief (file, "", fn, 0);
465 fprintf (file, " ");
467 fprintf (file, "\n");
471 /* Print the stack of unparsed member functions S to FILE. */
473 static void
474 cp_debug_print_unparsed_queues (FILE *file,
475 vec<cp_unparsed_functions_entry, va_gc> *s)
477 unsigned i;
478 cp_unparsed_functions_entry *uf;
480 fprintf (file, "Unparsed functions\n");
481 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
483 fprintf (file, "#%u:\n", i);
484 cp_debug_print_unparsed_function (file, uf);
489 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
490 the given PARSER. If FILE is NULL, the output is printed on stderr. */
492 static void
493 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
495 cp_token *next_token, *first_token, *start_token;
497 if (file == NULL)
498 file = stderr;
500 next_token = parser->lexer->next_token;
501 first_token = parser->lexer->buffer->address ();
502 start_token = (next_token > first_token + window_size / 2)
503 ? next_token - window_size / 2
504 : first_token;
505 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
506 next_token);
510 /* Dump debugging information for the given PARSER. If FILE is NULL,
511 the output is printed on stderr. */
513 void
514 cp_debug_parser (FILE *file, cp_parser *parser)
516 const size_t window_size = 20;
517 cp_token *token;
518 expanded_location eloc;
520 if (file == NULL)
521 file = stderr;
523 fprintf (file, "Parser state\n\n");
524 fprintf (file, "Number of tokens: %u\n",
525 vec_safe_length (parser->lexer->buffer));
526 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
527 cp_debug_print_tree_if_set (file, "Object scope",
528 parser->object_scope);
529 cp_debug_print_tree_if_set (file, "Qualifying scope",
530 parser->qualifying_scope);
531 cp_debug_print_context_stack (file, parser->context);
532 cp_debug_print_flag (file, "Allow GNU extensions",
533 parser->allow_gnu_extensions_p);
534 cp_debug_print_flag (file, "'>' token is greater-than",
535 parser->greater_than_is_operator_p);
536 cp_debug_print_flag (file, "Default args allowed in current "
537 "parameter list", parser->default_arg_ok_p);
538 cp_debug_print_flag (file, "Parsing integral constant-expression",
539 parser->integral_constant_expression_p);
540 cp_debug_print_flag (file, "Allow non-constant expression in current "
541 "constant-expression",
542 parser->allow_non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Seen non-constant expression",
544 parser->non_integral_constant_expression_p);
545 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
546 "current context",
547 parser->local_variables_forbidden_p);
548 cp_debug_print_flag (file, "In unbraced linkage specification",
549 parser->in_unbraced_linkage_specification_p);
550 cp_debug_print_flag (file, "Parsing a declarator",
551 parser->in_declarator_p);
552 cp_debug_print_flag (file, "In template argument list",
553 parser->in_template_argument_list_p);
554 cp_debug_print_flag (file, "Parsing an iteration statement",
555 parser->in_statement & IN_ITERATION_STMT);
556 cp_debug_print_flag (file, "Parsing a switch statement",
557 parser->in_statement & IN_SWITCH_STMT);
558 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
559 parser->in_statement & IN_OMP_BLOCK);
560 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
561 parser->in_statement & IN_CILK_SIMD_FOR);
562 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
563 parser->in_statement & IN_OMP_FOR);
564 cp_debug_print_flag (file, "Parsing an if statement",
565 parser->in_statement & IN_IF_STMT);
566 cp_debug_print_flag (file, "Parsing a type-id in an expression "
567 "context", parser->in_type_id_in_expr_p);
568 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
569 parser->implicit_extern_c);
570 cp_debug_print_flag (file, "String expressions should be translated "
571 "to execution character set",
572 parser->translate_strings_p);
573 cp_debug_print_flag (file, "Parsing function body outside of a "
574 "local class", parser->in_function_body);
575 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
576 parser->colon_corrects_to_scope_p);
577 cp_debug_print_flag (file, "Colon doesn't start a class definition",
578 parser->colon_doesnt_start_class_def_p);
579 if (parser->type_definition_forbidden_message)
580 fprintf (file, "Error message for forbidden type definitions: %s\n",
581 parser->type_definition_forbidden_message);
582 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
583 fprintf (file, "Number of class definitions in progress: %u\n",
584 parser->num_classes_being_defined);
585 fprintf (file, "Number of template parameter lists for the current "
586 "declaration: %u\n", parser->num_template_parameter_lists);
587 cp_debug_parser_tokens (file, parser, window_size);
588 token = parser->lexer->next_token;
589 fprintf (file, "Next token to parse:\n");
590 fprintf (file, "\tToken: ");
591 cp_lexer_print_token (file, token);
592 eloc = expand_location (token->location);
593 fprintf (file, "\n\tFile: %s\n", eloc.file);
594 fprintf (file, "\tLine: %d\n", eloc.line);
595 fprintf (file, "\tColumn: %d\n", eloc.column);
598 DEBUG_FUNCTION void
599 debug (cp_parser &ref)
601 cp_debug_parser (stderr, &ref);
604 DEBUG_FUNCTION void
605 debug (cp_parser *ptr)
607 if (ptr)
608 debug (*ptr);
609 else
610 fprintf (stderr, "<nil>\n");
613 /* Allocate memory for a new lexer object and return it. */
615 static cp_lexer *
616 cp_lexer_alloc (void)
618 cp_lexer *lexer;
620 c_common_no_more_pch ();
622 /* Allocate the memory. */
623 lexer = ggc_cleared_alloc<cp_lexer> ();
625 /* Initially we are not debugging. */
626 lexer->debugging_p = false;
628 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
630 /* Create the buffer. */
631 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
633 return lexer;
637 /* Create a new main C++ lexer, the lexer that gets tokens from the
638 preprocessor. */
640 static cp_lexer *
641 cp_lexer_new_main (void)
643 cp_lexer *lexer;
644 cp_token token;
646 /* It's possible that parsing the first pragma will load a PCH file,
647 which is a GC collection point. So we have to do that before
648 allocating any memory. */
649 cp_parser_initial_pragma (&token);
651 lexer = cp_lexer_alloc ();
653 /* Put the first token in the buffer. */
654 lexer->buffer->quick_push (token);
656 /* Get the remaining tokens from the preprocessor. */
657 while (token.type != CPP_EOF)
659 cp_lexer_get_preprocessor_token (lexer, &token);
660 vec_safe_push (lexer->buffer, token);
663 lexer->last_token = lexer->buffer->address ()
664 + lexer->buffer->length ()
665 - 1;
666 lexer->next_token = lexer->buffer->length ()
667 ? lexer->buffer->address ()
668 : &eof_token;
670 /* Subsequent preprocessor diagnostics should use compiler
671 diagnostic functions to get the compiler source location. */
672 done_lexing = true;
674 gcc_assert (!lexer->next_token->purged_p);
675 return lexer;
678 /* Create a new lexer whose token stream is primed with the tokens in
679 CACHE. When these tokens are exhausted, no new tokens will be read. */
681 static cp_lexer *
682 cp_lexer_new_from_tokens (cp_token_cache *cache)
684 cp_token *first = cache->first;
685 cp_token *last = cache->last;
686 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
688 /* We do not own the buffer. */
689 lexer->buffer = NULL;
690 lexer->next_token = first == last ? &eof_token : first;
691 lexer->last_token = last;
693 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
695 /* Initially we are not debugging. */
696 lexer->debugging_p = false;
698 gcc_assert (!lexer->next_token->purged_p);
699 return lexer;
702 /* Frees all resources associated with LEXER. */
704 static void
705 cp_lexer_destroy (cp_lexer *lexer)
707 vec_free (lexer->buffer);
708 lexer->saved_tokens.release ();
709 ggc_free (lexer);
712 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
713 be used. The point of this flag is to help the compiler to fold away calls
714 to cp_lexer_debugging_p within this source file at compile time, when the
715 lexer is not being debugged. */
717 #define LEXER_DEBUGGING_ENABLED_P false
719 /* Returns nonzero if debugging information should be output. */
721 static inline bool
722 cp_lexer_debugging_p (cp_lexer *lexer)
724 if (!LEXER_DEBUGGING_ENABLED_P)
725 return false;
727 return lexer->debugging_p;
731 static inline cp_token_position
732 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
734 gcc_assert (!previous_p || lexer->next_token != &eof_token);
736 return lexer->next_token - previous_p;
739 static inline cp_token *
740 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
742 return pos;
745 static inline void
746 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
748 lexer->next_token = cp_lexer_token_at (lexer, pos);
751 static inline cp_token_position
752 cp_lexer_previous_token_position (cp_lexer *lexer)
754 if (lexer->next_token == &eof_token)
755 return lexer->last_token - 1;
756 else
757 return cp_lexer_token_position (lexer, true);
760 static inline cp_token *
761 cp_lexer_previous_token (cp_lexer *lexer)
763 cp_token_position tp = cp_lexer_previous_token_position (lexer);
765 /* Skip past purged tokens. */
766 while (tp->purged_p)
768 gcc_assert (tp != lexer->buffer->address ());
769 tp--;
772 return cp_lexer_token_at (lexer, tp);
775 /* nonzero if we are presently saving tokens. */
777 static inline int
778 cp_lexer_saving_tokens (const cp_lexer* lexer)
780 return lexer->saved_tokens.length () != 0;
783 /* Store the next token from the preprocessor in *TOKEN. Return true
784 if we reach EOF. If LEXER is NULL, assume we are handling an
785 initial #pragma pch_preprocess, and thus want the lexer to return
786 processed strings. */
788 static void
789 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
791 static int is_extern_c = 0;
793 /* Get a new token from the preprocessor. */
794 token->type
795 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
796 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
797 token->keyword = RID_MAX;
798 token->purged_p = false;
799 token->error_reported = false;
801 /* On some systems, some header files are surrounded by an
802 implicit extern "C" block. Set a flag in the token if it
803 comes from such a header. */
804 is_extern_c += pending_lang_change;
805 pending_lang_change = 0;
806 token->implicit_extern_c = is_extern_c > 0;
808 /* Check to see if this token is a keyword. */
809 if (token->type == CPP_NAME)
811 if (C_IS_RESERVED_WORD (token->u.value))
813 /* Mark this token as a keyword. */
814 token->type = CPP_KEYWORD;
815 /* Record which keyword. */
816 token->keyword = C_RID_CODE (token->u.value);
818 else
820 if (warn_cxx11_compat
821 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
822 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
824 /* Warn about the C++0x keyword (but still treat it as
825 an identifier). */
826 warning (OPT_Wc__11_compat,
827 "identifier %qE is a keyword in C++11",
828 token->u.value);
830 /* Clear out the C_RID_CODE so we don't warn about this
831 particular identifier-turned-keyword again. */
832 C_SET_RID_CODE (token->u.value, RID_MAX);
835 token->keyword = RID_MAX;
838 else if (token->type == CPP_AT_NAME)
840 /* This only happens in Objective-C++; it must be a keyword. */
841 token->type = CPP_KEYWORD;
842 switch (C_RID_CODE (token->u.value))
844 /* Replace 'class' with '@class', 'private' with '@private',
845 etc. This prevents confusion with the C++ keyword
846 'class', and makes the tokens consistent with other
847 Objective-C 'AT' keywords. For example '@class' is
848 reported as RID_AT_CLASS which is consistent with
849 '@synchronized', which is reported as
850 RID_AT_SYNCHRONIZED.
852 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
853 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
854 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
855 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
856 case RID_THROW: token->keyword = RID_AT_THROW; break;
857 case RID_TRY: token->keyword = RID_AT_TRY; break;
858 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
859 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
860 default: token->keyword = C_RID_CODE (token->u.value);
865 /* Update the globals input_location and the input file stack from TOKEN. */
866 static inline void
867 cp_lexer_set_source_position_from_token (cp_token *token)
869 if (token->type != CPP_EOF)
871 input_location = token->location;
875 /* Update the globals input_location and the input file stack from LEXER. */
876 static inline void
877 cp_lexer_set_source_position (cp_lexer *lexer)
879 cp_token *token = cp_lexer_peek_token (lexer);
880 cp_lexer_set_source_position_from_token (token);
883 /* Return a pointer to the next token in the token stream, but do not
884 consume it. */
886 static inline cp_token *
887 cp_lexer_peek_token (cp_lexer *lexer)
889 if (cp_lexer_debugging_p (lexer))
891 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
892 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
893 putc ('\n', cp_lexer_debug_stream);
895 return lexer->next_token;
898 /* Return true if the next token has the indicated TYPE. */
900 static inline bool
901 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
903 return cp_lexer_peek_token (lexer)->type == type;
906 /* Return true if the next token does not have the indicated TYPE. */
908 static inline bool
909 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
911 return !cp_lexer_next_token_is (lexer, type);
914 /* Return true if the next token is the indicated KEYWORD. */
916 static inline bool
917 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
919 return cp_lexer_peek_token (lexer)->keyword == keyword;
922 static inline bool
923 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
925 return cp_lexer_peek_nth_token (lexer, n)->type == type;
928 static inline bool
929 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
931 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
934 /* Return true if the next token is not the indicated KEYWORD. */
936 static inline bool
937 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
939 return cp_lexer_peek_token (lexer)->keyword != keyword;
942 /* Return true if KEYWORD can start a decl-specifier. */
944 bool
945 cp_keyword_starts_decl_specifier_p (enum rid keyword)
947 switch (keyword)
949 /* auto specifier: storage-class-specifier in C++,
950 simple-type-specifier in C++0x. */
951 case RID_AUTO:
952 /* Storage classes. */
953 case RID_REGISTER:
954 case RID_STATIC:
955 case RID_EXTERN:
956 case RID_MUTABLE:
957 case RID_THREAD:
958 /* Elaborated type specifiers. */
959 case RID_ENUM:
960 case RID_CLASS:
961 case RID_STRUCT:
962 case RID_UNION:
963 case RID_TYPENAME:
964 /* Simple type specifiers. */
965 case RID_CHAR:
966 case RID_CHAR16:
967 case RID_CHAR32:
968 case RID_WCHAR:
969 case RID_BOOL:
970 case RID_SHORT:
971 case RID_INT:
972 case RID_LONG:
973 case RID_SIGNED:
974 case RID_UNSIGNED:
975 case RID_FLOAT:
976 case RID_DOUBLE:
977 case RID_VOID:
978 /* GNU extensions. */
979 case RID_ATTRIBUTE:
980 case RID_TYPEOF:
981 /* C++0x extensions. */
982 case RID_DECLTYPE:
983 case RID_UNDERLYING_TYPE:
984 case RID_CONSTEXPR:
985 return true;
987 default:
988 if (keyword >= RID_FIRST_INT_N
989 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
990 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
991 return true;
992 return false;
996 /* Return true if the next token is a keyword for a decl-specifier. */
998 static bool
999 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1001 cp_token *token;
1003 token = cp_lexer_peek_token (lexer);
1004 return cp_keyword_starts_decl_specifier_p (token->keyword);
1007 /* Returns TRUE iff the token T begins a decltype type. */
1009 static bool
1010 token_is_decltype (cp_token *t)
1012 return (t->keyword == RID_DECLTYPE
1013 || t->type == CPP_DECLTYPE);
1016 /* Returns TRUE iff the next token begins a decltype type. */
1018 static bool
1019 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1021 cp_token *t = cp_lexer_peek_token (lexer);
1022 return token_is_decltype (t);
1025 /* Called when processing a token with tree_check_value; perform or defer the
1026 associated checks and return the value. */
1028 static tree
1029 saved_checks_value (struct tree_check *check_value)
1031 /* Perform any access checks that were deferred. */
1032 vec<deferred_access_check, va_gc> *checks;
1033 deferred_access_check *chk;
1034 checks = check_value->checks;
1035 if (checks)
1037 int i;
1038 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1039 perform_or_defer_access_check (chk->binfo,
1040 chk->decl,
1041 chk->diag_decl, tf_warning_or_error);
1043 /* Return the stored value. */
1044 return check_value->value;
1047 /* Return a pointer to the Nth token in the token stream. If N is 1,
1048 then this is precisely equivalent to cp_lexer_peek_token (except
1049 that it is not inline). One would like to disallow that case, but
1050 there is one case (cp_parser_nth_token_starts_template_id) where
1051 the caller passes a variable for N and it might be 1. */
1053 static cp_token *
1054 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1056 cp_token *token;
1058 /* N is 1-based, not zero-based. */
1059 gcc_assert (n > 0);
1061 if (cp_lexer_debugging_p (lexer))
1062 fprintf (cp_lexer_debug_stream,
1063 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1065 --n;
1066 token = lexer->next_token;
1067 gcc_assert (!n || token != &eof_token);
1068 while (n != 0)
1070 ++token;
1071 if (token == lexer->last_token)
1073 token = &eof_token;
1074 break;
1077 if (!token->purged_p)
1078 --n;
1081 if (cp_lexer_debugging_p (lexer))
1083 cp_lexer_print_token (cp_lexer_debug_stream, token);
1084 putc ('\n', cp_lexer_debug_stream);
1087 return token;
1090 /* Return the next token, and advance the lexer's next_token pointer
1091 to point to the next non-purged token. */
1093 static cp_token *
1094 cp_lexer_consume_token (cp_lexer* lexer)
1096 cp_token *token = lexer->next_token;
1098 gcc_assert (token != &eof_token);
1099 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1103 lexer->next_token++;
1104 if (lexer->next_token == lexer->last_token)
1106 lexer->next_token = &eof_token;
1107 break;
1111 while (lexer->next_token->purged_p);
1113 cp_lexer_set_source_position_from_token (token);
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer))
1118 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1119 cp_lexer_print_token (cp_lexer_debug_stream, token);
1120 putc ('\n', cp_lexer_debug_stream);
1123 return token;
1126 /* Permanently remove the next token from the token stream, and
1127 advance the next_token pointer to refer to the next non-purged
1128 token. */
1130 static void
1131 cp_lexer_purge_token (cp_lexer *lexer)
1133 cp_token *tok = lexer->next_token;
1135 gcc_assert (tok != &eof_token);
1136 tok->purged_p = true;
1137 tok->location = UNKNOWN_LOCATION;
1138 tok->u.value = NULL_TREE;
1139 tok->keyword = RID_MAX;
1143 tok++;
1144 if (tok == lexer->last_token)
1146 tok = &eof_token;
1147 break;
1150 while (tok->purged_p);
1151 lexer->next_token = tok;
1154 /* Permanently remove all tokens after TOK, up to, but not
1155 including, the token that will be returned next by
1156 cp_lexer_peek_token. */
1158 static void
1159 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1161 cp_token *peek = lexer->next_token;
1163 if (peek == &eof_token)
1164 peek = lexer->last_token;
1166 gcc_assert (tok < peek);
1168 for ( tok += 1; tok != peek; tok += 1)
1170 tok->purged_p = true;
1171 tok->location = UNKNOWN_LOCATION;
1172 tok->u.value = NULL_TREE;
1173 tok->keyword = RID_MAX;
1177 /* Begin saving tokens. All tokens consumed after this point will be
1178 preserved. */
1180 static void
1181 cp_lexer_save_tokens (cp_lexer* lexer)
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer))
1185 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1187 lexer->saved_tokens.safe_push (lexer->next_token);
1190 /* Commit to the portion of the token stream most recently saved. */
1192 static void
1193 cp_lexer_commit_tokens (cp_lexer* lexer)
1195 /* Provide debugging output. */
1196 if (cp_lexer_debugging_p (lexer))
1197 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1199 lexer->saved_tokens.pop ();
1202 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1203 to the token stream. Stop saving tokens. */
1205 static void
1206 cp_lexer_rollback_tokens (cp_lexer* lexer)
1208 /* Provide debugging output. */
1209 if (cp_lexer_debugging_p (lexer))
1210 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1212 lexer->next_token = lexer->saved_tokens.pop ();
1215 /* RAII wrapper around the above functions, with sanity checking. Creating
1216 a variable saves tokens, which are committed when the variable is
1217 destroyed unless they are explicitly rolled back by calling the rollback
1218 member function. */
1220 struct saved_token_sentinel
1222 cp_lexer *lexer;
1223 unsigned len;
1224 bool commit;
1225 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1227 len = lexer->saved_tokens.length ();
1228 cp_lexer_save_tokens (lexer);
1230 void rollback ()
1232 cp_lexer_rollback_tokens (lexer);
1233 commit = false;
1235 ~saved_token_sentinel()
1237 if (commit)
1238 cp_lexer_commit_tokens (lexer);
1239 gcc_assert (lexer->saved_tokens.length () == len);
1243 /* Print a representation of the TOKEN on the STREAM. */
1245 static void
1246 cp_lexer_print_token (FILE * stream, cp_token *token)
1248 /* We don't use cpp_type2name here because the parser defines
1249 a few tokens of its own. */
1250 static const char *const token_names[] = {
1251 /* cpplib-defined token types */
1252 #define OP(e, s) #e,
1253 #define TK(e, s) #e,
1254 TTYPE_TABLE
1255 #undef OP
1256 #undef TK
1257 /* C++ parser token types - see "Manifest constants", above. */
1258 "KEYWORD",
1259 "TEMPLATE_ID",
1260 "NESTED_NAME_SPECIFIER",
1263 /* For some tokens, print the associated data. */
1264 switch (token->type)
1266 case CPP_KEYWORD:
1267 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1268 For example, `struct' is mapped to an INTEGER_CST. */
1269 if (!identifier_p (token->u.value))
1270 break;
1271 /* fall through */
1272 case CPP_NAME:
1273 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1274 break;
1276 case CPP_STRING:
1277 case CPP_STRING16:
1278 case CPP_STRING32:
1279 case CPP_WSTRING:
1280 case CPP_UTF8STRING:
1281 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1282 break;
1284 case CPP_NUMBER:
1285 print_generic_expr (stream, token->u.value, 0);
1286 break;
1288 default:
1289 /* If we have a name for the token, print it out. Otherwise, we
1290 simply give the numeric code. */
1291 if (token->type < ARRAY_SIZE(token_names))
1292 fputs (token_names[token->type], stream);
1293 else
1294 fprintf (stream, "[%d]", token->type);
1295 break;
1299 DEBUG_FUNCTION void
1300 debug (cp_token &ref)
1302 cp_lexer_print_token (stderr, &ref);
1303 fprintf (stderr, "\n");
1306 DEBUG_FUNCTION void
1307 debug (cp_token *ptr)
1309 if (ptr)
1310 debug (*ptr);
1311 else
1312 fprintf (stderr, "<nil>\n");
1316 /* Start emitting debugging information. */
1318 static void
1319 cp_lexer_start_debugging (cp_lexer* lexer)
1321 if (!LEXER_DEBUGGING_ENABLED_P)
1322 fatal_error (input_location,
1323 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1325 lexer->debugging_p = true;
1326 cp_lexer_debug_stream = stderr;
1329 /* Stop emitting debugging information. */
1331 static void
1332 cp_lexer_stop_debugging (cp_lexer* lexer)
1334 if (!LEXER_DEBUGGING_ENABLED_P)
1335 fatal_error (input_location,
1336 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1338 lexer->debugging_p = false;
1339 cp_lexer_debug_stream = NULL;
1342 /* Create a new cp_token_cache, representing a range of tokens. */
1344 static cp_token_cache *
1345 cp_token_cache_new (cp_token *first, cp_token *last)
1347 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1348 cache->first = first;
1349 cache->last = last;
1350 return cache;
1353 /* Diagnose if #pragma omp declare simd isn't followed immediately
1354 by function declaration or definition. */
1356 static inline void
1357 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1359 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1361 error ("%<#pragma omp declare simd%> not immediately followed by "
1362 "function declaration or definition");
1363 parser->omp_declare_simd = NULL;
1367 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1368 and put that into "omp declare simd" attribute. */
1370 static inline void
1371 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1373 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1375 if (fndecl == error_mark_node)
1377 parser->omp_declare_simd = NULL;
1378 return;
1380 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1382 cp_ensure_no_omp_declare_simd (parser);
1383 return;
1388 /* Diagnose if #pragma acc routine isn't followed immediately by function
1389 declaration or definition. */
1391 static inline void
1392 cp_ensure_no_oacc_routine (cp_parser *parser)
1394 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1396 error_at (parser->oacc_routine->loc,
1397 "%<#pragma acc routine%> not immediately followed by "
1398 "function declaration or definition");
1399 parser->oacc_routine = NULL;
1403 /* Decl-specifiers. */
1405 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1407 static void
1408 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1410 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1413 /* Declarators. */
1415 /* Nothing other than the parser should be creating declarators;
1416 declarators are a semi-syntactic representation of C++ entities.
1417 Other parts of the front end that need to create entities (like
1418 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1420 static cp_declarator *make_call_declarator
1421 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1422 static cp_declarator *make_array_declarator
1423 (cp_declarator *, tree);
1424 static cp_declarator *make_pointer_declarator
1425 (cp_cv_quals, cp_declarator *, tree);
1426 static cp_declarator *make_reference_declarator
1427 (cp_cv_quals, cp_declarator *, bool, tree);
1428 static cp_declarator *make_ptrmem_declarator
1429 (cp_cv_quals, tree, cp_declarator *, tree);
1431 /* An erroneous declarator. */
1432 static cp_declarator *cp_error_declarator;
1434 /* The obstack on which declarators and related data structures are
1435 allocated. */
1436 static struct obstack declarator_obstack;
1438 /* Alloc BYTES from the declarator memory pool. */
1440 static inline void *
1441 alloc_declarator (size_t bytes)
1443 return obstack_alloc (&declarator_obstack, bytes);
1446 /* Allocate a declarator of the indicated KIND. Clear fields that are
1447 common to all declarators. */
1449 static cp_declarator *
1450 make_declarator (cp_declarator_kind kind)
1452 cp_declarator *declarator;
1454 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1455 declarator->kind = kind;
1456 declarator->attributes = NULL_TREE;
1457 declarator->std_attributes = NULL_TREE;
1458 declarator->declarator = NULL;
1459 declarator->parameter_pack_p = false;
1460 declarator->id_loc = UNKNOWN_LOCATION;
1462 return declarator;
1465 /* Make a declarator for a generalized identifier. If
1466 QUALIFYING_SCOPE is non-NULL, the identifier is
1467 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1468 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1469 is, if any. */
1471 static cp_declarator *
1472 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1473 special_function_kind sfk)
1475 cp_declarator *declarator;
1477 /* It is valid to write:
1479 class C { void f(); };
1480 typedef C D;
1481 void D::f();
1483 The standard is not clear about whether `typedef const C D' is
1484 legal; as of 2002-09-15 the committee is considering that
1485 question. EDG 3.0 allows that syntax. Therefore, we do as
1486 well. */
1487 if (qualifying_scope && TYPE_P (qualifying_scope))
1488 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1490 gcc_assert (identifier_p (unqualified_name)
1491 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1492 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1494 declarator = make_declarator (cdk_id);
1495 declarator->u.id.qualifying_scope = qualifying_scope;
1496 declarator->u.id.unqualified_name = unqualified_name;
1497 declarator->u.id.sfk = sfk;
1499 return declarator;
1502 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1503 of modifiers such as const or volatile to apply to the pointer
1504 type, represented as identifiers. ATTRIBUTES represent the attributes that
1505 appertain to the pointer or reference. */
1507 cp_declarator *
1508 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1509 tree attributes)
1511 cp_declarator *declarator;
1513 declarator = make_declarator (cdk_pointer);
1514 declarator->declarator = target;
1515 declarator->u.pointer.qualifiers = cv_qualifiers;
1516 declarator->u.pointer.class_type = NULL_TREE;
1517 if (target)
1519 declarator->id_loc = target->id_loc;
1520 declarator->parameter_pack_p = target->parameter_pack_p;
1521 target->parameter_pack_p = false;
1523 else
1524 declarator->parameter_pack_p = false;
1526 declarator->std_attributes = attributes;
1528 return declarator;
1531 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1532 represent the attributes that appertain to the pointer or
1533 reference. */
1535 cp_declarator *
1536 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1537 bool rvalue_ref, tree attributes)
1539 cp_declarator *declarator;
1541 declarator = make_declarator (cdk_reference);
1542 declarator->declarator = target;
1543 declarator->u.reference.qualifiers = cv_qualifiers;
1544 declarator->u.reference.rvalue_ref = rvalue_ref;
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 declarator->std_attributes = attributes;
1556 return declarator;
1559 /* Like make_pointer_declarator -- but for a pointer to a non-static
1560 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1561 appertain to the pointer or reference. */
1563 cp_declarator *
1564 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1565 cp_declarator *pointee,
1566 tree attributes)
1568 cp_declarator *declarator;
1570 declarator = make_declarator (cdk_ptrmem);
1571 declarator->declarator = pointee;
1572 declarator->u.pointer.qualifiers = cv_qualifiers;
1573 declarator->u.pointer.class_type = class_type;
1575 if (pointee)
1577 declarator->parameter_pack_p = pointee->parameter_pack_p;
1578 pointee->parameter_pack_p = false;
1580 else
1581 declarator->parameter_pack_p = false;
1583 declarator->std_attributes = attributes;
1585 return declarator;
1588 /* Make a declarator for the function given by TARGET, with the
1589 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1590 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1591 indicates what exceptions can be thrown. */
1593 cp_declarator *
1594 make_call_declarator (cp_declarator *target,
1595 tree parms,
1596 cp_cv_quals cv_qualifiers,
1597 cp_virt_specifiers virt_specifiers,
1598 cp_ref_qualifier ref_qualifier,
1599 tree tx_qualifier,
1600 tree exception_specification,
1601 tree late_return_type,
1602 tree requires_clause)
1604 cp_declarator *declarator;
1606 declarator = make_declarator (cdk_function);
1607 declarator->declarator = target;
1608 declarator->u.function.parameters = parms;
1609 declarator->u.function.qualifiers = cv_qualifiers;
1610 declarator->u.function.virt_specifiers = virt_specifiers;
1611 declarator->u.function.ref_qualifier = ref_qualifier;
1612 declarator->u.function.tx_qualifier = tx_qualifier;
1613 declarator->u.function.exception_specification = exception_specification;
1614 declarator->u.function.late_return_type = late_return_type;
1615 declarator->u.function.requires_clause = requires_clause;
1616 if (target)
1618 declarator->id_loc = target->id_loc;
1619 declarator->parameter_pack_p = target->parameter_pack_p;
1620 target->parameter_pack_p = false;
1622 else
1623 declarator->parameter_pack_p = false;
1625 return declarator;
1628 /* Make a declarator for an array of BOUNDS elements, each of which is
1629 defined by ELEMENT. */
1631 cp_declarator *
1632 make_array_declarator (cp_declarator *element, tree bounds)
1634 cp_declarator *declarator;
1636 declarator = make_declarator (cdk_array);
1637 declarator->declarator = element;
1638 declarator->u.array.bounds = bounds;
1639 if (element)
1641 declarator->id_loc = element->id_loc;
1642 declarator->parameter_pack_p = element->parameter_pack_p;
1643 element->parameter_pack_p = false;
1645 else
1646 declarator->parameter_pack_p = false;
1648 return declarator;
1651 /* Determine whether the declarator we've seen so far can be a
1652 parameter pack, when followed by an ellipsis. */
1653 static bool
1654 declarator_can_be_parameter_pack (cp_declarator *declarator)
1656 if (declarator && declarator->parameter_pack_p)
1657 /* We already saw an ellipsis. */
1658 return false;
1660 /* Search for a declarator name, or any other declarator that goes
1661 after the point where the ellipsis could appear in a parameter
1662 pack. If we find any of these, then this declarator can not be
1663 made into a parameter pack. */
1664 bool found = false;
1665 while (declarator && !found)
1667 switch ((int)declarator->kind)
1669 case cdk_id:
1670 case cdk_array:
1671 found = true;
1672 break;
1674 case cdk_error:
1675 return true;
1677 default:
1678 declarator = declarator->declarator;
1679 break;
1683 return !found;
1686 cp_parameter_declarator *no_parameters;
1688 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1689 DECLARATOR and DEFAULT_ARGUMENT. */
1691 cp_parameter_declarator *
1692 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1693 cp_declarator *declarator,
1694 tree default_argument,
1695 bool template_parameter_pack_p = false)
1697 cp_parameter_declarator *parameter;
1699 parameter = ((cp_parameter_declarator *)
1700 alloc_declarator (sizeof (cp_parameter_declarator)));
1701 parameter->next = NULL;
1702 if (decl_specifiers)
1703 parameter->decl_specifiers = *decl_specifiers;
1704 else
1705 clear_decl_specs (&parameter->decl_specifiers);
1706 parameter->declarator = declarator;
1707 parameter->default_argument = default_argument;
1708 parameter->template_parameter_pack_p = template_parameter_pack_p;
1710 return parameter;
1713 /* Returns true iff DECLARATOR is a declaration for a function. */
1715 static bool
1716 function_declarator_p (const cp_declarator *declarator)
1718 while (declarator)
1720 if (declarator->kind == cdk_function
1721 && declarator->declarator->kind == cdk_id)
1722 return true;
1723 if (declarator->kind == cdk_id
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1728 return false;
1731 /* The parser. */
1733 /* Overview
1734 --------
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1757 Methodology
1758 -----------
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1777 Future Improvements
1778 -------------------
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1789 enum
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1829 enum cp_parser_prec
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1881 /* Prototypes. */
1883 /* Constructors and destructors. */
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1888 /* Class variables. */
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1942 cp_parser_context *context;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1968 return context;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1996 /* Prototypes. */
1998 /* Constructors and destructors. */
2000 static cp_parser *cp_parser_new
2001 (void);
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2102 /* Statements [gram.stmt.stmt] */
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2144 /* Declarations [gram.dcl.dcl] */
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2204 /* Declarators [gram.dcl.decl] */
2206 static tree cp_parser_init_declarator
2207 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2208 bool, bool, int, bool *, tree *, location_t *, tree *);
2209 static cp_declarator *cp_parser_declarator
2210 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2211 static cp_declarator *cp_parser_direct_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2213 static enum tree_code cp_parser_ptr_operator
2214 (cp_parser *, tree *, cp_cv_quals *, tree *);
2215 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2216 (cp_parser *);
2217 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2218 (cp_parser *);
2219 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2220 (cp_parser *);
2221 static tree cp_parser_tx_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_late_return_type_opt
2224 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2225 static tree cp_parser_declarator_id
2226 (cp_parser *, bool);
2227 static tree cp_parser_type_id
2228 (cp_parser *);
2229 static tree cp_parser_template_type_arg
2230 (cp_parser *);
2231 static tree cp_parser_trailing_type_id (cp_parser *);
2232 static tree cp_parser_type_id_1
2233 (cp_parser *, bool, bool);
2234 static void cp_parser_type_specifier_seq
2235 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2236 static tree cp_parser_parameter_declaration_clause
2237 (cp_parser *);
2238 static tree cp_parser_parameter_declaration_list
2239 (cp_parser *, bool *);
2240 static cp_parameter_declarator *cp_parser_parameter_declaration
2241 (cp_parser *, bool, bool *);
2242 static tree cp_parser_default_argument
2243 (cp_parser *, bool);
2244 static void cp_parser_function_body
2245 (cp_parser *, bool);
2246 static tree cp_parser_initializer
2247 (cp_parser *, bool *, bool *);
2248 static cp_expr cp_parser_initializer_clause
2249 (cp_parser *, bool *);
2250 static cp_expr cp_parser_braced_list
2251 (cp_parser*, bool*);
2252 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2253 (cp_parser *, bool *);
2255 static bool cp_parser_ctor_initializer_opt_and_function_body
2256 (cp_parser *, bool);
2258 static tree cp_parser_late_parsing_omp_declare_simd
2259 (cp_parser *, tree);
2261 static tree cp_parser_late_parsing_cilk_simd_fn_info
2262 (cp_parser *, tree);
2264 static tree cp_parser_late_parsing_oacc_routine
2265 (cp_parser *, tree);
2267 static tree synthesize_implicit_template_parm
2268 (cp_parser *, tree);
2269 static tree finish_fully_implicit_template
2270 (cp_parser *, tree);
2272 /* Classes [gram.class] */
2274 static tree cp_parser_class_name
2275 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2276 static tree cp_parser_class_specifier
2277 (cp_parser *);
2278 static tree cp_parser_class_head
2279 (cp_parser *, bool *);
2280 static enum tag_types cp_parser_class_key
2281 (cp_parser *);
2282 static void cp_parser_type_parameter_key
2283 (cp_parser* parser);
2284 static void cp_parser_member_specification_opt
2285 (cp_parser *);
2286 static void cp_parser_member_declaration
2287 (cp_parser *);
2288 static tree cp_parser_pure_specifier
2289 (cp_parser *);
2290 static tree cp_parser_constant_initializer
2291 (cp_parser *);
2293 /* Derived classes [gram.class.derived] */
2295 static tree cp_parser_base_clause
2296 (cp_parser *);
2297 static tree cp_parser_base_specifier
2298 (cp_parser *);
2300 /* Special member functions [gram.special] */
2302 static tree cp_parser_conversion_function_id
2303 (cp_parser *);
2304 static tree cp_parser_conversion_type_id
2305 (cp_parser *);
2306 static cp_declarator *cp_parser_conversion_declarator_opt
2307 (cp_parser *);
2308 static bool cp_parser_ctor_initializer_opt
2309 (cp_parser *);
2310 static void cp_parser_mem_initializer_list
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer_id
2315 (cp_parser *);
2317 /* Overloading [gram.over] */
2319 static cp_expr cp_parser_operator_function_id
2320 (cp_parser *);
2321 static cp_expr cp_parser_operator
2322 (cp_parser *);
2324 /* Templates [gram.temp] */
2326 static void cp_parser_template_declaration
2327 (cp_parser *, bool);
2328 static tree cp_parser_template_parameter_list
2329 (cp_parser *);
2330 static tree cp_parser_template_parameter
2331 (cp_parser *, bool *, bool *);
2332 static tree cp_parser_type_parameter
2333 (cp_parser *, bool *);
2334 static tree cp_parser_template_id
2335 (cp_parser *, bool, bool, enum tag_types, bool);
2336 static tree cp_parser_template_name
2337 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2338 static tree cp_parser_template_argument_list
2339 (cp_parser *);
2340 static tree cp_parser_template_argument
2341 (cp_parser *);
2342 static void cp_parser_explicit_instantiation
2343 (cp_parser *);
2344 static void cp_parser_explicit_specialization
2345 (cp_parser *);
2347 /* Exception handling [gram.exception] */
2349 static tree cp_parser_try_block
2350 (cp_parser *);
2351 static bool cp_parser_function_try_block
2352 (cp_parser *);
2353 static void cp_parser_handler_seq
2354 (cp_parser *);
2355 static void cp_parser_handler
2356 (cp_parser *);
2357 static tree cp_parser_exception_declaration
2358 (cp_parser *);
2359 static tree cp_parser_throw_expression
2360 (cp_parser *);
2361 static tree cp_parser_exception_specification_opt
2362 (cp_parser *);
2363 static tree cp_parser_type_id_list
2364 (cp_parser *);
2366 /* GNU Extensions */
2368 static tree cp_parser_asm_specification_opt
2369 (cp_parser *);
2370 static tree cp_parser_asm_operand_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_clobber_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_label_list
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_gnu_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_std_attribute_p
2381 (cp_parser *);
2382 static bool cp_nth_tokens_can_be_std_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_gnu_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_attribute_p
2387 (cp_parser *, size_t);
2388 static tree cp_parser_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attribute_list
2393 (cp_parser *);
2394 static tree cp_parser_std_attribute
2395 (cp_parser *, tree);
2396 static tree cp_parser_std_attribute_spec
2397 (cp_parser *);
2398 static tree cp_parser_std_attribute_spec_seq
2399 (cp_parser *);
2400 static bool cp_parser_extension_opt
2401 (cp_parser *, int *);
2402 static void cp_parser_label_declaration
2403 (cp_parser *);
2405 /* Concept Extensions */
2407 static tree cp_parser_requires_clause
2408 (cp_parser *);
2409 static tree cp_parser_requires_clause_opt
2410 (cp_parser *);
2411 static tree cp_parser_requires_expression
2412 (cp_parser *);
2413 static tree cp_parser_requirement_parameter_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement_body
2416 (cp_parser *);
2417 static tree cp_parser_requirement_list
2418 (cp_parser *);
2419 static tree cp_parser_requirement
2420 (cp_parser *);
2421 static tree cp_parser_simple_requirement
2422 (cp_parser *);
2423 static tree cp_parser_compound_requirement
2424 (cp_parser *);
2425 static tree cp_parser_type_requirement
2426 (cp_parser *);
2427 static tree cp_parser_nested_requirement
2428 (cp_parser *);
2430 /* Transactional Memory Extensions */
2432 static tree cp_parser_transaction
2433 (cp_parser *, cp_token *);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser *, enum rid);
2436 static bool cp_parser_function_transaction
2437 (cp_parser *, enum rid);
2438 static tree cp_parser_transaction_cancel
2439 (cp_parser *);
2441 enum pragma_context {
2442 pragma_external,
2443 pragma_member,
2444 pragma_objc_icode,
2445 pragma_stmt,
2446 pragma_compound
2448 static bool cp_parser_pragma
2449 (cp_parser *, enum pragma_context, bool *);
2451 /* Objective-C++ Productions */
2453 static tree cp_parser_objc_message_receiver
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_args
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_encode_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_defs_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_protocol_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_selector_expression
2466 (cp_parser *);
2467 static cp_expr cp_parser_objc_expression
2468 (cp_parser *);
2469 static bool cp_parser_objc_selector_p
2470 (enum cpp_ttype);
2471 static tree cp_parser_objc_selector
2472 (cp_parser *);
2473 static tree cp_parser_objc_protocol_refs_opt
2474 (cp_parser *);
2475 static void cp_parser_objc_declaration
2476 (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2478 (cp_parser *);
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_synthesize_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_dynamic_declaration
2486 (cp_parser *) ;
2487 static tree cp_parser_objc_struct_declaration
2488 (cp_parser *) ;
2490 /* Utility Routines */
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser *, tree, location_t);
2496 static tree cp_parser_maybe_treat_template_as_class
2497 (tree, bool);
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser *, cp_declarator *, location_t);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser *, unsigned, location_t, cp_declarator *);
2502 static cp_expr cp_parser_simple_cast_expression
2503 (cp_parser *);
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser *, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser *, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser *, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser *, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec<deferred_access_check, va_gc> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser *, tree);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2522 static tree cp_parser_save_nsdmi
2523 (cp_parser *);
2524 static tree cp_parser_enclosed_template_argument_list
2525 (cp_parser *);
2526 static void cp_parser_save_default_args
2527 (cp_parser *, tree);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser *, tree);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser *, tree, tree, tree);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser *, tree);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser *, tree);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser *, enum rid);
2538 static tree cp_parser_trait_expr
2539 (cp_parser *, enum rid);
2540 static bool cp_parser_declares_only_class_p
2541 (cp_parser *);
2542 static void cp_parser_set_storage_class
2543 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq *decl_specs,
2548 cp_decl_spec ds, cp_token *);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq *);
2551 static void cp_parser_required_error
2552 (cp_parser *, required_token, bool);
2553 static cp_token *cp_parser_require
2554 (cp_parser *, enum cpp_ttype, required_token);
2555 static cp_token *cp_parser_require_keyword
2556 (cp_parser *, enum rid, required_token);
2557 static bool cp_parser_token_starts_function_definition_p
2558 (cp_token *);
2559 static bool cp_parser_next_token_starts_class_definition_p
2560 (cp_parser *);
2561 static bool cp_parser_next_token_ends_template_argument_p
2562 (cp_parser *);
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser *, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2566 (cp_token *);
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2568 (cp_token *);
2569 static void cp_parser_check_class_key
2570 (enum tag_types, tree type);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type, location_t location);
2573 static bool cp_parser_optional_template_keyword
2574 (cp_parser *);
2575 static void cp_parser_pre_parsed_nested_name_specifier
2576 (cp_parser *);
2577 static bool cp_parser_cache_group
2578 (cp_parser *, enum cpp_ttype, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser *parser, bool nsdmi);
2581 static void cp_parser_parse_tentatively
2582 (cp_parser *);
2583 static void cp_parser_commit_to_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_commit_to_topmost_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_abort_tentative_parse
2588 (cp_parser *);
2589 static bool cp_parser_parse_definitely
2590 (cp_parser *);
2591 static inline bool cp_parser_parsing_tentatively
2592 (cp_parser *);
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2594 (cp_parser *);
2595 static void cp_parser_error
2596 (cp_parser *, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser *, tree, tree, name_lookup_error, location_t);
2599 static bool cp_parser_simulate_error
2600 (cp_parser *);
2601 static bool cp_parser_check_type_definition
2602 (cp_parser *);
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator *, tree, location_t type_location);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser *, tree, enum tag_types, location_t location);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser *, non_integral_constant);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser *, tree, location_t);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2612 (cp_parser *);
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser *, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_block_or_statement
2620 (cp_parser *);
2621 static bool cp_parser_skip_to_closing_brace
2622 (cp_parser *);
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2624 (cp_parser *);
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2628 (cp_parser *);
2629 static bool cp_parser_allow_gnu_extensions_p
2630 (cp_parser *);
2631 static bool cp_parser_is_pure_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_keyword
2636 (cp_token *, enum rid);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser *, tree, location_t location);
2639 static cp_declarator * cp_parser_make_indirect_declarator
2640 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2641 static bool cp_parser_compound_literal_p
2642 (cp_parser *);
2643 static bool cp_parser_array_designator_p
2644 (cp_parser *);
2645 static bool cp_parser_init_statement_p
2646 (cp_parser *);
2647 static bool cp_parser_skip_to_closing_square_bracket
2648 (cp_parser *);
2650 /* Concept-related syntactic transformations */
2652 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2653 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2661 ++cp_unevaluated_operand;
2662 ++c_inhibit_evaluation_warnings;
2665 cp_unevaluated::~cp_unevaluated ()
2667 --c_inhibit_evaluation_warnings;
2668 --cp_unevaluated_operand;
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2674 /* Returns nonzero if we are parsing tentatively. */
2676 static inline bool
2677 cp_parser_parsing_tentatively (cp_parser* parser)
2679 return parser->context->next != NULL;
2682 /* Returns nonzero if TOKEN is a string literal. */
2684 static bool
2685 cp_parser_is_pure_string_literal (cp_token* token)
2687 return (token->type == CPP_STRING ||
2688 token->type == CPP_STRING16 ||
2689 token->type == CPP_STRING32 ||
2690 token->type == CPP_WSTRING ||
2691 token->type == CPP_UTF8STRING);
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2697 static bool
2698 cp_parser_is_string_literal (cp_token* token)
2700 return (cp_parser_is_pure_string_literal (token) ||
2701 token->type == CPP_STRING_USERDEF ||
2702 token->type == CPP_STRING16_USERDEF ||
2703 token->type == CPP_STRING32_USERDEF ||
2704 token->type == CPP_WSTRING_USERDEF ||
2705 token->type == CPP_UTF8STRING_USERDEF);
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2710 static bool
2711 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2713 return token->keyword == keyword;
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717 PRAGMA_NONE. */
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token *token)
2722 if (token->type != CPP_PRAGMA)
2723 return PRAGMA_NONE;
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2737 of the marker. */
2739 static bool
2740 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2741 location_t *out_loc)
2743 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2744 if (token2->type != tok1_kind)
2745 return false;
2746 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2747 if (token3->type != tok1_kind)
2748 return false;
2749 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2750 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2751 return false;
2753 /* It must be at the start of the line. */
2754 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2755 if (LOCATION_COLUMN (start_loc) != 1)
2756 return false;
2758 /* We have a conflict marker. Construct a location of the form:
2759 <<<<<<<
2760 ^~~~~~~
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc = get_finish (token4->location);
2763 *out_loc = make_location (start_loc, start_loc, finish_loc);
2765 return true;
2768 /* If not parsing tentatively, issue a diagnostic of the form
2769 FILE:LINE: MESSAGE before TOKEN
2770 where TOKEN is the next token in the input stream. MESSAGE
2771 (specified by the caller) is usually of the form "expected
2772 OTHER-TOKEN". */
2774 static void
2775 cp_parser_error (cp_parser* parser, const char* gmsgid)
2777 if (!cp_parser_simulate_error (parser))
2779 cp_token *token = cp_lexer_peek_token (parser->lexer);
2780 /* This diagnostic makes more sense if it is tagged to the line
2781 of the token we just peeked at. */
2782 cp_lexer_set_source_position_from_token (token);
2784 if (token->type == CPP_PRAGMA)
2786 error_at (token->location,
2787 "%<#pragma%> is not allowed here");
2788 cp_parser_skip_to_pragma_eol (parser, token);
2789 return;
2792 /* If this is actually a conflict marker, report it as such. */
2793 if (token->type == CPP_LSHIFT
2794 || token->type == CPP_RSHIFT
2795 || token->type == CPP_EQ_EQ)
2797 location_t loc;
2798 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2800 error_at (loc, "version control conflict marker in file");
2801 return;
2805 c_parse_error (gmsgid,
2806 /* Because c_parser_error does not understand
2807 CPP_KEYWORD, keywords are treated like
2808 identifiers. */
2809 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2810 token->u.value, token->flags);
2814 /* Issue an error about name-lookup failing. NAME is the
2815 IDENTIFIER_NODE DECL is the result of
2816 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2817 the thing that we hoped to find. */
2819 static void
2820 cp_parser_name_lookup_error (cp_parser* parser,
2821 tree name,
2822 tree decl,
2823 name_lookup_error desired,
2824 location_t location)
2826 /* If name lookup completely failed, tell the user that NAME was not
2827 declared. */
2828 if (decl == error_mark_node)
2830 if (parser->scope && parser->scope != global_namespace)
2831 error_at (location, "%<%E::%E%> has not been declared",
2832 parser->scope, name);
2833 else if (parser->scope == global_namespace)
2834 error_at (location, "%<::%E%> has not been declared", name);
2835 else if (parser->object_scope
2836 && !CLASS_TYPE_P (parser->object_scope))
2837 error_at (location, "request for member %qE in non-class type %qT",
2838 name, parser->object_scope);
2839 else if (parser->object_scope)
2840 error_at (location, "%<%T::%E%> has not been declared",
2841 parser->object_scope, name);
2842 else
2843 error_at (location, "%qE has not been declared", name);
2845 else if (parser->scope && parser->scope != global_namespace)
2847 switch (desired)
2849 case NLE_TYPE:
2850 error_at (location, "%<%E::%E%> is not a type",
2851 parser->scope, name);
2852 break;
2853 case NLE_CXX98:
2854 error_at (location, "%<%E::%E%> is not a class or namespace",
2855 parser->scope, name);
2856 break;
2857 case NLE_NOT_CXX98:
2858 error_at (location,
2859 "%<%E::%E%> is not a class, namespace, or enumeration",
2860 parser->scope, name);
2861 break;
2862 default:
2863 gcc_unreachable ();
2867 else if (parser->scope == global_namespace)
2869 switch (desired)
2871 case NLE_TYPE:
2872 error_at (location, "%<::%E%> is not a type", name);
2873 break;
2874 case NLE_CXX98:
2875 error_at (location, "%<::%E%> is not a class or namespace", name);
2876 break;
2877 case NLE_NOT_CXX98:
2878 error_at (location,
2879 "%<::%E%> is not a class, namespace, or enumeration",
2880 name);
2881 break;
2882 default:
2883 gcc_unreachable ();
2886 else
2888 switch (desired)
2890 case NLE_TYPE:
2891 error_at (location, "%qE is not a type", name);
2892 break;
2893 case NLE_CXX98:
2894 error_at (location, "%qE is not a class or namespace", name);
2895 break;
2896 case NLE_NOT_CXX98:
2897 error_at (location,
2898 "%qE is not a class, namespace, or enumeration", name);
2899 break;
2900 default:
2901 gcc_unreachable ();
2906 /* If we are parsing tentatively, remember that an error has occurred
2907 during this tentative parse. Returns true if the error was
2908 simulated; false if a message should be issued by the caller. */
2910 static bool
2911 cp_parser_simulate_error (cp_parser* parser)
2913 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2915 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2916 return true;
2918 return false;
2921 /* This function is called when a type is defined. If type
2922 definitions are forbidden at this point, an error message is
2923 issued. */
2925 static bool
2926 cp_parser_check_type_definition (cp_parser* parser)
2928 /* If types are forbidden here, issue a message. */
2929 if (parser->type_definition_forbidden_message)
2931 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2932 in the message need to be interpreted. */
2933 error (parser->type_definition_forbidden_message);
2934 return false;
2936 return true;
2939 /* This function is called when the DECLARATOR is processed. The TYPE
2940 was a type defined in the decl-specifiers. If it is invalid to
2941 define a type in the decl-specifiers for DECLARATOR, an error is
2942 issued. TYPE_LOCATION is the location of TYPE and is used
2943 for error reporting. */
2945 static void
2946 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2947 tree type, location_t type_location)
2949 /* [dcl.fct] forbids type definitions in return types.
2950 Unfortunately, it's not easy to know whether or not we are
2951 processing a return type until after the fact. */
2952 while (declarator
2953 && (declarator->kind == cdk_pointer
2954 || declarator->kind == cdk_reference
2955 || declarator->kind == cdk_ptrmem))
2956 declarator = declarator->declarator;
2957 if (declarator
2958 && declarator->kind == cdk_function)
2960 error_at (type_location,
2961 "new types may not be defined in a return type");
2962 inform (type_location,
2963 "(perhaps a semicolon is missing after the definition of %qT)",
2964 type);
2968 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2969 "<" in any valid C++ program. If the next token is indeed "<",
2970 issue a message warning the user about what appears to be an
2971 invalid attempt to form a template-id. LOCATION is the location
2972 of the type-specifier (TYPE) */
2974 static void
2975 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2976 tree type,
2977 enum tag_types tag_type,
2978 location_t location)
2980 cp_token_position start = 0;
2982 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2984 if (TYPE_P (type))
2985 error_at (location, "%qT is not a template", type);
2986 else if (identifier_p (type))
2988 if (tag_type != none_type)
2989 error_at (location, "%qE is not a class template", type);
2990 else
2991 error_at (location, "%qE is not a template", type);
2993 else
2994 error_at (location, "invalid template-id");
2995 /* Remember the location of the invalid "<". */
2996 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2997 start = cp_lexer_token_position (parser->lexer, true);
2998 /* Consume the "<". */
2999 cp_lexer_consume_token (parser->lexer);
3000 /* Parse the template arguments. */
3001 cp_parser_enclosed_template_argument_list (parser);
3002 /* Permanently remove the invalid template arguments so that
3003 this error message is not issued again. */
3004 if (start)
3005 cp_lexer_purge_tokens_after (parser->lexer, start);
3009 /* If parsing an integral constant-expression, issue an error message
3010 about the fact that THING appeared and return true. Otherwise,
3011 return false. In either case, set
3012 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3014 static bool
3015 cp_parser_non_integral_constant_expression (cp_parser *parser,
3016 non_integral_constant thing)
3018 parser->non_integral_constant_expression_p = true;
3019 if (parser->integral_constant_expression_p)
3021 if (!parser->allow_non_integral_constant_expression_p)
3023 const char *msg = NULL;
3024 switch (thing)
3026 case NIC_FLOAT:
3027 error ("floating-point literal "
3028 "cannot appear in a constant-expression");
3029 return true;
3030 case NIC_CAST:
3031 error ("a cast to a type other than an integral or "
3032 "enumeration type cannot appear in a "
3033 "constant-expression");
3034 return true;
3035 case NIC_TYPEID:
3036 error ("%<typeid%> operator "
3037 "cannot appear in a constant-expression");
3038 return true;
3039 case NIC_NCC:
3040 error ("non-constant compound literals "
3041 "cannot appear in a constant-expression");
3042 return true;
3043 case NIC_FUNC_CALL:
3044 error ("a function call "
3045 "cannot appear in a constant-expression");
3046 return true;
3047 case NIC_INC:
3048 error ("an increment "
3049 "cannot appear in a constant-expression");
3050 return true;
3051 case NIC_DEC:
3052 error ("an decrement "
3053 "cannot appear in a constant-expression");
3054 return true;
3055 case NIC_ARRAY_REF:
3056 error ("an array reference "
3057 "cannot appear in a constant-expression");
3058 return true;
3059 case NIC_ADDR_LABEL:
3060 error ("the address of a label "
3061 "cannot appear in a constant-expression");
3062 return true;
3063 case NIC_OVERLOADED:
3064 error ("calls to overloaded operators "
3065 "cannot appear in a constant-expression");
3066 return true;
3067 case NIC_ASSIGNMENT:
3068 error ("an assignment cannot appear in a constant-expression");
3069 return true;
3070 case NIC_COMMA:
3071 error ("a comma operator "
3072 "cannot appear in a constant-expression");
3073 return true;
3074 case NIC_CONSTRUCTOR:
3075 error ("a call to a constructor "
3076 "cannot appear in a constant-expression");
3077 return true;
3078 case NIC_TRANSACTION:
3079 error ("a transaction expression "
3080 "cannot appear in a constant-expression");
3081 return true;
3082 case NIC_THIS:
3083 msg = "this";
3084 break;
3085 case NIC_FUNC_NAME:
3086 msg = "__FUNCTION__";
3087 break;
3088 case NIC_PRETTY_FUNC:
3089 msg = "__PRETTY_FUNCTION__";
3090 break;
3091 case NIC_C99_FUNC:
3092 msg = "__func__";
3093 break;
3094 case NIC_VA_ARG:
3095 msg = "va_arg";
3096 break;
3097 case NIC_ARROW:
3098 msg = "->";
3099 break;
3100 case NIC_POINT:
3101 msg = ".";
3102 break;
3103 case NIC_STAR:
3104 msg = "*";
3105 break;
3106 case NIC_ADDR:
3107 msg = "&";
3108 break;
3109 case NIC_PREINCREMENT:
3110 msg = "++";
3111 break;
3112 case NIC_PREDECREMENT:
3113 msg = "--";
3114 break;
3115 case NIC_NEW:
3116 msg = "new";
3117 break;
3118 case NIC_DEL:
3119 msg = "delete";
3120 break;
3121 default:
3122 gcc_unreachable ();
3124 if (msg)
3125 error ("%qs cannot appear in a constant-expression", msg);
3126 return true;
3129 return false;
3132 /* Emit a diagnostic for an invalid type name. This function commits
3133 to the current active tentative parse, if any. (Otherwise, the
3134 problematic construct might be encountered again later, resulting
3135 in duplicate error messages.) LOCATION is the location of ID. */
3137 static void
3138 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3139 location_t location)
3141 tree decl, ambiguous_decls;
3142 cp_parser_commit_to_tentative_parse (parser);
3143 /* Try to lookup the identifier. */
3144 decl = cp_parser_lookup_name (parser, id, none_type,
3145 /*is_template=*/false,
3146 /*is_namespace=*/false,
3147 /*check_dependency=*/true,
3148 &ambiguous_decls, location);
3149 if (ambiguous_decls)
3150 /* If the lookup was ambiguous, an error will already have
3151 been issued. */
3152 return;
3153 /* If the lookup found a template-name, it means that the user forgot
3154 to specify an argument list. Emit a useful error message. */
3155 if (DECL_TYPE_TEMPLATE_P (decl))
3157 error_at (location,
3158 "invalid use of template-name %qE without an argument list",
3159 decl);
3160 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3161 inform (location, "class template argument deduction is only available "
3162 "with -std=c++1z or -std=gnu++1z");
3163 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3165 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3166 error_at (location, "invalid use of destructor %qD as a type", id);
3167 else if (TREE_CODE (decl) == TYPE_DECL)
3168 /* Something like 'unsigned A a;' */
3169 error_at (location, "invalid combination of multiple type-specifiers");
3170 else if (!parser->scope)
3172 /* Issue an error message. */
3173 const char *suggestion = NULL;
3174 if (TREE_CODE (id) == IDENTIFIER_NODE)
3175 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3176 if (suggestion)
3178 gcc_rich_location richloc (location);
3179 richloc.add_fixit_replace (suggestion);
3180 error_at_rich_loc (&richloc,
3181 "%qE does not name a type; did you mean %qs?",
3182 id, suggestion);
3184 else
3185 error_at (location, "%qE does not name a type", id);
3186 /* If we're in a template class, it's possible that the user was
3187 referring to a type from a base class. For example:
3189 template <typename T> struct A { typedef T X; };
3190 template <typename T> struct B : public A<T> { X x; };
3192 The user should have said "typename A<T>::X". */
3193 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3194 inform (location, "C++11 %<constexpr%> only available with "
3195 "-std=c++11 or -std=gnu++11");
3196 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3197 inform (location, "C++11 %<noexcept%> only available with "
3198 "-std=c++11 or -std=gnu++11");
3199 else if (cxx_dialect < cxx11
3200 && TREE_CODE (id) == IDENTIFIER_NODE
3201 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3202 inform (location, "C++11 %<thread_local%> only available with "
3203 "-std=c++11 or -std=gnu++11");
3204 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3205 inform (location, "%<concept%> only available with -fconcepts");
3206 else if (processing_template_decl && current_class_type
3207 && TYPE_BINFO (current_class_type))
3209 tree b;
3211 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3213 b = TREE_CHAIN (b))
3215 tree base_type = BINFO_TYPE (b);
3216 if (CLASS_TYPE_P (base_type)
3217 && dependent_type_p (base_type))
3219 tree field;
3220 /* Go from a particular instantiation of the
3221 template (which will have an empty TYPE_FIELDs),
3222 to the main version. */
3223 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3224 for (field = TYPE_FIELDS (base_type);
3225 field;
3226 field = DECL_CHAIN (field))
3227 if (TREE_CODE (field) == TYPE_DECL
3228 && DECL_NAME (field) == id)
3230 inform (location,
3231 "(perhaps %<typename %T::%E%> was intended)",
3232 BINFO_TYPE (b), id);
3233 break;
3235 if (field)
3236 break;
3241 /* Here we diagnose qualified-ids where the scope is actually correct,
3242 but the identifier does not resolve to a valid type name. */
3243 else if (parser->scope != error_mark_node)
3245 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3247 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3248 error_at (location_of (id),
3249 "%qE in namespace %qE does not name a template type",
3250 id, parser->scope);
3251 else
3252 error_at (location_of (id),
3253 "%qE in namespace %qE does not name a type",
3254 id, parser->scope);
3255 if (DECL_P (decl))
3256 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3258 else if (CLASS_TYPE_P (parser->scope)
3259 && constructor_name_p (id, parser->scope))
3261 /* A<T>::A<T>() */
3262 error_at (location, "%<%T::%E%> names the constructor, not"
3263 " the type", parser->scope, id);
3264 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3265 error_at (location, "and %qT has no template constructors",
3266 parser->scope);
3268 else if (TYPE_P (parser->scope)
3269 && dependent_scope_p (parser->scope))
3270 error_at (location, "need %<typename%> before %<%T::%E%> because "
3271 "%qT is a dependent scope",
3272 parser->scope, id, parser->scope);
3273 else if (TYPE_P (parser->scope))
3275 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3276 error_at (location_of (id),
3277 "%qE in %q#T does not name a template type",
3278 id, parser->scope);
3279 else
3280 error_at (location_of (id),
3281 "%qE in %q#T does not name a type",
3282 id, parser->scope);
3283 if (DECL_P (decl))
3284 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3286 else
3287 gcc_unreachable ();
3291 /* Check for a common situation where a type-name should be present,
3292 but is not, and issue a sensible error message. Returns true if an
3293 invalid type-name was detected.
3295 The situation handled by this function are variable declarations of the
3296 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3297 Usually, `ID' should name a type, but if we got here it means that it
3298 does not. We try to emit the best possible error message depending on
3299 how exactly the id-expression looks like. */
3301 static bool
3302 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3304 tree id;
3305 cp_token *token = cp_lexer_peek_token (parser->lexer);
3307 /* Avoid duplicate error about ambiguous lookup. */
3308 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3310 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3311 if (next->type == CPP_NAME && next->error_reported)
3312 goto out;
3315 cp_parser_parse_tentatively (parser);
3316 id = cp_parser_id_expression (parser,
3317 /*template_keyword_p=*/false,
3318 /*check_dependency_p=*/true,
3319 /*template_p=*/NULL,
3320 /*declarator_p=*/true,
3321 /*optional_p=*/false);
3322 /* If the next token is a (, this is a function with no explicit return
3323 type, i.e. constructor, destructor or conversion op. */
3324 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3325 || TREE_CODE (id) == TYPE_DECL)
3327 cp_parser_abort_tentative_parse (parser);
3328 return false;
3330 if (!cp_parser_parse_definitely (parser))
3331 return false;
3333 /* Emit a diagnostic for the invalid type. */
3334 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3335 out:
3336 /* If we aren't in the middle of a declarator (i.e. in a
3337 parameter-declaration-clause), skip to the end of the declaration;
3338 there's no point in trying to process it. */
3339 if (!parser->in_declarator_p)
3340 cp_parser_skip_to_end_of_block_or_statement (parser);
3341 return true;
3344 /* Consume tokens up to, and including, the next non-nested closing `)'.
3345 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3346 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3347 found an unnested token of that type. */
3349 static int
3350 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3351 bool recovering,
3352 cpp_ttype or_ttype,
3353 bool consume_paren)
3355 unsigned paren_depth = 0;
3356 unsigned brace_depth = 0;
3357 unsigned square_depth = 0;
3359 if (recovering && or_ttype == CPP_EOF
3360 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3361 return 0;
3363 while (true)
3365 cp_token * token = cp_lexer_peek_token (parser->lexer);
3367 /* Have we found what we're looking for before the closing paren? */
3368 if (token->type == or_ttype && or_ttype != CPP_EOF
3369 && !brace_depth && !paren_depth && !square_depth)
3370 return -1;
3372 switch (token->type)
3374 case CPP_EOF:
3375 case CPP_PRAGMA_EOL:
3376 /* If we've run out of tokens, then there is no closing `)'. */
3377 return 0;
3379 /* This is good for lambda expression capture-lists. */
3380 case CPP_OPEN_SQUARE:
3381 ++square_depth;
3382 break;
3383 case CPP_CLOSE_SQUARE:
3384 if (!square_depth--)
3385 return 0;
3386 break;
3388 case CPP_SEMICOLON:
3389 /* This matches the processing in skip_to_end_of_statement. */
3390 if (!brace_depth)
3391 return 0;
3392 break;
3394 case CPP_OPEN_BRACE:
3395 ++brace_depth;
3396 break;
3397 case CPP_CLOSE_BRACE:
3398 if (!brace_depth--)
3399 return 0;
3400 break;
3402 case CPP_OPEN_PAREN:
3403 if (!brace_depth)
3404 ++paren_depth;
3405 break;
3407 case CPP_CLOSE_PAREN:
3408 if (!brace_depth && !paren_depth--)
3410 if (consume_paren)
3411 cp_lexer_consume_token (parser->lexer);
3412 return 1;
3414 break;
3416 default:
3417 break;
3420 /* Consume the token. */
3421 cp_lexer_consume_token (parser->lexer);
3425 /* Consume tokens up to, and including, the next non-nested closing `)'.
3426 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3427 are doing error recovery. Returns -1 if OR_COMMA is true and we
3428 found an unnested token of that type. */
3430 static int
3431 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3432 bool recovering,
3433 bool or_comma,
3434 bool consume_paren)
3436 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3437 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3438 ttype, consume_paren);
3441 /* Consume tokens until we reach the end of the current statement.
3442 Normally, that will be just before consuming a `;'. However, if a
3443 non-nested `}' comes first, then we stop before consuming that. */
3445 static void
3446 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3448 unsigned nesting_depth = 0;
3450 /* Unwind generic function template scope if necessary. */
3451 if (parser->fully_implicit_function_template_p)
3452 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3454 while (true)
3456 cp_token *token = cp_lexer_peek_token (parser->lexer);
3458 switch (token->type)
3460 case CPP_EOF:
3461 case CPP_PRAGMA_EOL:
3462 /* If we've run out of tokens, stop. */
3463 return;
3465 case CPP_SEMICOLON:
3466 /* If the next token is a `;', we have reached the end of the
3467 statement. */
3468 if (!nesting_depth)
3469 return;
3470 break;
3472 case CPP_CLOSE_BRACE:
3473 /* If this is a non-nested '}', stop before consuming it.
3474 That way, when confronted with something like:
3476 { 3 + }
3478 we stop before consuming the closing '}', even though we
3479 have not yet reached a `;'. */
3480 if (nesting_depth == 0)
3481 return;
3483 /* If it is the closing '}' for a block that we have
3484 scanned, stop -- but only after consuming the token.
3485 That way given:
3487 void f g () { ... }
3488 typedef int I;
3490 we will stop after the body of the erroneously declared
3491 function, but before consuming the following `typedef'
3492 declaration. */
3493 if (--nesting_depth == 0)
3495 cp_lexer_consume_token (parser->lexer);
3496 return;
3498 break;
3500 case CPP_OPEN_BRACE:
3501 ++nesting_depth;
3502 break;
3504 default:
3505 break;
3508 /* Consume the token. */
3509 cp_lexer_consume_token (parser->lexer);
3513 /* This function is called at the end of a statement or declaration.
3514 If the next token is a semicolon, it is consumed; otherwise, error
3515 recovery is attempted. */
3517 static void
3518 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3520 /* Look for the trailing `;'. */
3521 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3523 /* If there is additional (erroneous) input, skip to the end of
3524 the statement. */
3525 cp_parser_skip_to_end_of_statement (parser);
3526 /* If the next token is now a `;', consume it. */
3527 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3528 cp_lexer_consume_token (parser->lexer);
3532 /* Skip tokens until we have consumed an entire block, or until we
3533 have consumed a non-nested `;'. */
3535 static void
3536 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3538 int nesting_depth = 0;
3540 /* Unwind generic function template scope if necessary. */
3541 if (parser->fully_implicit_function_template_p)
3542 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3544 while (nesting_depth >= 0)
3546 cp_token *token = cp_lexer_peek_token (parser->lexer);
3548 switch (token->type)
3550 case CPP_EOF:
3551 case CPP_PRAGMA_EOL:
3552 /* If we've run out of tokens, stop. */
3553 return;
3555 case CPP_SEMICOLON:
3556 /* Stop if this is an unnested ';'. */
3557 if (!nesting_depth)
3558 nesting_depth = -1;
3559 break;
3561 case CPP_CLOSE_BRACE:
3562 /* Stop if this is an unnested '}', or closes the outermost
3563 nesting level. */
3564 nesting_depth--;
3565 if (nesting_depth < 0)
3566 return;
3567 if (!nesting_depth)
3568 nesting_depth = -1;
3569 break;
3571 case CPP_OPEN_BRACE:
3572 /* Nest. */
3573 nesting_depth++;
3574 break;
3576 default:
3577 break;
3580 /* Consume the token. */
3581 cp_lexer_consume_token (parser->lexer);
3585 /* Skip tokens until a non-nested closing curly brace is the next
3586 token, or there are no more tokens. Return true in the first case,
3587 false otherwise. */
3589 static bool
3590 cp_parser_skip_to_closing_brace (cp_parser *parser)
3592 unsigned nesting_depth = 0;
3594 while (true)
3596 cp_token *token = cp_lexer_peek_token (parser->lexer);
3598 switch (token->type)
3600 case CPP_EOF:
3601 case CPP_PRAGMA_EOL:
3602 /* If we've run out of tokens, stop. */
3603 return false;
3605 case CPP_CLOSE_BRACE:
3606 /* If the next token is a non-nested `}', then we have reached
3607 the end of the current block. */
3608 if (nesting_depth-- == 0)
3609 return true;
3610 break;
3612 case CPP_OPEN_BRACE:
3613 /* If it the next token is a `{', then we are entering a new
3614 block. Consume the entire block. */
3615 ++nesting_depth;
3616 break;
3618 default:
3619 break;
3622 /* Consume the token. */
3623 cp_lexer_consume_token (parser->lexer);
3627 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3628 parameter is the PRAGMA token, allowing us to purge the entire pragma
3629 sequence. */
3631 static void
3632 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3634 cp_token *token;
3636 parser->lexer->in_pragma = false;
3639 token = cp_lexer_consume_token (parser->lexer);
3640 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3642 /* Ensure that the pragma is not parsed again. */
3643 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3646 /* Require pragma end of line, resyncing with it as necessary. The
3647 arguments are as for cp_parser_skip_to_pragma_eol. */
3649 static void
3650 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3652 parser->lexer->in_pragma = false;
3653 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3654 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3657 /* This is a simple wrapper around make_typename_type. When the id is
3658 an unresolved identifier node, we can provide a superior diagnostic
3659 using cp_parser_diagnose_invalid_type_name. */
3661 static tree
3662 cp_parser_make_typename_type (cp_parser *parser, tree id,
3663 location_t id_location)
3665 tree result;
3666 if (identifier_p (id))
3668 result = make_typename_type (parser->scope, id, typename_type,
3669 /*complain=*/tf_none);
3670 if (result == error_mark_node)
3671 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3672 return result;
3674 return make_typename_type (parser->scope, id, typename_type, tf_error);
3677 /* This is a wrapper around the
3678 make_{pointer,ptrmem,reference}_declarator functions that decides
3679 which one to call based on the CODE and CLASS_TYPE arguments. The
3680 CODE argument should be one of the values returned by
3681 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3682 appertain to the pointer or reference. */
3684 static cp_declarator *
3685 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3686 cp_cv_quals cv_qualifiers,
3687 cp_declarator *target,
3688 tree attributes)
3690 if (code == ERROR_MARK)
3691 return cp_error_declarator;
3693 if (code == INDIRECT_REF)
3694 if (class_type == NULL_TREE)
3695 return make_pointer_declarator (cv_qualifiers, target, attributes);
3696 else
3697 return make_ptrmem_declarator (cv_qualifiers, class_type,
3698 target, attributes);
3699 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3700 return make_reference_declarator (cv_qualifiers, target,
3701 false, attributes);
3702 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3703 return make_reference_declarator (cv_qualifiers, target,
3704 true, attributes);
3705 gcc_unreachable ();
3708 /* Create a new C++ parser. */
3710 static cp_parser *
3711 cp_parser_new (void)
3713 cp_parser *parser;
3714 cp_lexer *lexer;
3715 unsigned i;
3717 /* cp_lexer_new_main is called before doing GC allocation because
3718 cp_lexer_new_main might load a PCH file. */
3719 lexer = cp_lexer_new_main ();
3721 /* Initialize the binops_by_token so that we can get the tree
3722 directly from the token. */
3723 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3724 binops_by_token[binops[i].token_type] = binops[i];
3726 parser = ggc_cleared_alloc<cp_parser> ();
3727 parser->lexer = lexer;
3728 parser->context = cp_parser_context_new (NULL);
3730 /* For now, we always accept GNU extensions. */
3731 parser->allow_gnu_extensions_p = 1;
3733 /* The `>' token is a greater-than operator, not the end of a
3734 template-id. */
3735 parser->greater_than_is_operator_p = true;
3737 parser->default_arg_ok_p = true;
3739 /* We are not parsing a constant-expression. */
3740 parser->integral_constant_expression_p = false;
3741 parser->allow_non_integral_constant_expression_p = false;
3742 parser->non_integral_constant_expression_p = false;
3744 /* Local variable names are not forbidden. */
3745 parser->local_variables_forbidden_p = false;
3747 /* We are not processing an `extern "C"' declaration. */
3748 parser->in_unbraced_linkage_specification_p = false;
3750 /* We are not processing a declarator. */
3751 parser->in_declarator_p = false;
3753 /* We are not processing a template-argument-list. */
3754 parser->in_template_argument_list_p = false;
3756 /* We are not in an iteration statement. */
3757 parser->in_statement = 0;
3759 /* We are not in a switch statement. */
3760 parser->in_switch_statement_p = false;
3762 /* We are not parsing a type-id inside an expression. */
3763 parser->in_type_id_in_expr_p = false;
3765 /* Declarations aren't implicitly extern "C". */
3766 parser->implicit_extern_c = false;
3768 /* String literals should be translated to the execution character set. */
3769 parser->translate_strings_p = true;
3771 /* We are not parsing a function body. */
3772 parser->in_function_body = false;
3774 /* We can correct until told otherwise. */
3775 parser->colon_corrects_to_scope_p = true;
3777 /* The unparsed function queue is empty. */
3778 push_unparsed_function_queues (parser);
3780 /* There are no classes being defined. */
3781 parser->num_classes_being_defined = 0;
3783 /* No template parameters apply. */
3784 parser->num_template_parameter_lists = 0;
3786 /* Special parsing data structures. */
3787 parser->omp_declare_simd = NULL;
3788 parser->cilk_simd_fn_info = NULL;
3789 parser->oacc_routine = NULL;
3791 /* Not declaring an implicit function template. */
3792 parser->auto_is_implicit_function_template_parm_p = false;
3793 parser->fully_implicit_function_template_p = false;
3794 parser->implicit_template_parms = 0;
3795 parser->implicit_template_scope = 0;
3797 /* Allow constrained-type-specifiers. */
3798 parser->prevent_constrained_type_specifiers = 0;
3800 return parser;
3803 /* Create a cp_lexer structure which will emit the tokens in CACHE
3804 and push it onto the parser's lexer stack. This is used for delayed
3805 parsing of in-class method bodies and default arguments, and should
3806 not be confused with tentative parsing. */
3807 static void
3808 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3810 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3811 lexer->next = parser->lexer;
3812 parser->lexer = lexer;
3814 /* Move the current source position to that of the first token in the
3815 new lexer. */
3816 cp_lexer_set_source_position_from_token (lexer->next_token);
3819 /* Pop the top lexer off the parser stack. This is never used for the
3820 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3821 static void
3822 cp_parser_pop_lexer (cp_parser *parser)
3824 cp_lexer *lexer = parser->lexer;
3825 parser->lexer = lexer->next;
3826 cp_lexer_destroy (lexer);
3828 /* Put the current source position back where it was before this
3829 lexer was pushed. */
3830 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3833 /* Lexical conventions [gram.lex] */
3835 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3836 identifier. */
3838 static cp_expr
3839 cp_parser_identifier (cp_parser* parser)
3841 cp_token *token;
3843 /* Look for the identifier. */
3844 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3845 /* Return the value. */
3846 if (token)
3847 return cp_expr (token->u.value, token->location);
3848 else
3849 return error_mark_node;
3852 /* Parse a sequence of adjacent string constants. Returns a
3853 TREE_STRING representing the combined, nul-terminated string
3854 constant. If TRANSLATE is true, translate the string to the
3855 execution character set. If WIDE_OK is true, a wide string is
3856 invalid here.
3858 C++98 [lex.string] says that if a narrow string literal token is
3859 adjacent to a wide string literal token, the behavior is undefined.
3860 However, C99 6.4.5p4 says that this results in a wide string literal.
3861 We follow C99 here, for consistency with the C front end.
3863 This code is largely lifted from lex_string() in c-lex.c.
3865 FUTURE: ObjC++ will need to handle @-strings here. */
3866 static cp_expr
3867 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3868 bool lookup_udlit = true)
3870 tree value;
3871 size_t count;
3872 struct obstack str_ob;
3873 cpp_string str, istr, *strs;
3874 cp_token *tok;
3875 enum cpp_ttype type, curr_type;
3876 int have_suffix_p = 0;
3877 tree string_tree;
3878 tree suffix_id = NULL_TREE;
3879 bool curr_tok_is_userdef_p = false;
3881 tok = cp_lexer_peek_token (parser->lexer);
3882 if (!cp_parser_is_string_literal (tok))
3884 cp_parser_error (parser, "expected string-literal");
3885 return error_mark_node;
3888 location_t loc = tok->location;
3890 if (cpp_userdef_string_p (tok->type))
3892 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3893 curr_type = cpp_userdef_string_remove_type (tok->type);
3894 curr_tok_is_userdef_p = true;
3896 else
3898 string_tree = tok->u.value;
3899 curr_type = tok->type;
3901 type = curr_type;
3903 /* Try to avoid the overhead of creating and destroying an obstack
3904 for the common case of just one string. */
3905 if (!cp_parser_is_string_literal
3906 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3908 cp_lexer_consume_token (parser->lexer);
3910 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3911 str.len = TREE_STRING_LENGTH (string_tree);
3912 count = 1;
3914 if (curr_tok_is_userdef_p)
3916 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3917 have_suffix_p = 1;
3918 curr_type = cpp_userdef_string_remove_type (tok->type);
3920 else
3921 curr_type = tok->type;
3923 strs = &str;
3925 else
3927 location_t last_tok_loc = tok->location;
3928 gcc_obstack_init (&str_ob);
3929 count = 0;
3933 cp_lexer_consume_token (parser->lexer);
3934 count++;
3935 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3936 str.len = TREE_STRING_LENGTH (string_tree);
3938 if (curr_tok_is_userdef_p)
3940 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3941 if (have_suffix_p == 0)
3943 suffix_id = curr_suffix_id;
3944 have_suffix_p = 1;
3946 else if (have_suffix_p == 1
3947 && curr_suffix_id != suffix_id)
3949 error ("inconsistent user-defined literal suffixes"
3950 " %qD and %qD in string literal",
3951 suffix_id, curr_suffix_id);
3952 have_suffix_p = -1;
3954 curr_type = cpp_userdef_string_remove_type (tok->type);
3956 else
3957 curr_type = tok->type;
3959 if (type != curr_type)
3961 if (type == CPP_STRING)
3962 type = curr_type;
3963 else if (curr_type != CPP_STRING)
3965 rich_location rich_loc (line_table, tok->location);
3966 rich_loc.add_range (last_tok_loc, false);
3967 error_at_rich_loc (&rich_loc,
3968 "unsupported non-standard concatenation "
3969 "of string literals");
3973 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3975 last_tok_loc = tok->location;
3977 tok = cp_lexer_peek_token (parser->lexer);
3978 if (cpp_userdef_string_p (tok->type))
3980 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3981 curr_type = cpp_userdef_string_remove_type (tok->type);
3982 curr_tok_is_userdef_p = true;
3984 else
3986 string_tree = tok->u.value;
3987 curr_type = tok->type;
3988 curr_tok_is_userdef_p = false;
3991 while (cp_parser_is_string_literal (tok));
3993 /* A string literal built by concatenation has its caret=start at
3994 the start of the initial string, and its finish at the finish of
3995 the final string literal. */
3996 loc = make_location (loc, loc, get_finish (last_tok_loc));
3998 strs = (cpp_string *) obstack_finish (&str_ob);
4001 if (type != CPP_STRING && !wide_ok)
4003 cp_parser_error (parser, "a wide string is invalid in this context");
4004 type = CPP_STRING;
4007 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4008 (parse_in, strs, count, &istr, type))
4010 value = build_string (istr.len, (const char *)istr.text);
4011 free (CONST_CAST (unsigned char *, istr.text));
4013 switch (type)
4015 default:
4016 case CPP_STRING:
4017 case CPP_UTF8STRING:
4018 TREE_TYPE (value) = char_array_type_node;
4019 break;
4020 case CPP_STRING16:
4021 TREE_TYPE (value) = char16_array_type_node;
4022 break;
4023 case CPP_STRING32:
4024 TREE_TYPE (value) = char32_array_type_node;
4025 break;
4026 case CPP_WSTRING:
4027 TREE_TYPE (value) = wchar_array_type_node;
4028 break;
4031 value = fix_string_type (value);
4033 if (have_suffix_p)
4035 tree literal = build_userdef_literal (suffix_id, value,
4036 OT_NONE, NULL_TREE);
4037 if (lookup_udlit)
4038 value = cp_parser_userdef_string_literal (literal);
4039 else
4040 value = literal;
4043 else
4044 /* cpp_interpret_string has issued an error. */
4045 value = error_mark_node;
4047 if (count > 1)
4048 obstack_free (&str_ob, 0);
4050 return cp_expr (value, loc);
4053 /* Look up a literal operator with the name and the exact arguments. */
4055 static tree
4056 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4058 tree decl, fns;
4059 decl = lookup_name (name);
4060 if (!decl || !is_overloaded_fn (decl))
4061 return error_mark_node;
4063 for (fns = decl; fns; fns = OVL_NEXT (fns))
4065 unsigned int ix;
4066 bool found = true;
4067 tree fn = OVL_CURRENT (fns);
4068 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4069 if (parmtypes != NULL_TREE)
4071 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4072 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4074 tree tparm = TREE_VALUE (parmtypes);
4075 tree targ = TREE_TYPE ((*args)[ix]);
4076 bool ptr = TYPE_PTR_P (tparm);
4077 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4078 if ((ptr || arr || !same_type_p (tparm, targ))
4079 && (!ptr || !arr
4080 || !same_type_p (TREE_TYPE (tparm),
4081 TREE_TYPE (targ))))
4082 found = false;
4084 if (found
4085 && ix == vec_safe_length (args)
4086 /* May be this should be sufficient_parms_p instead,
4087 depending on how exactly should user-defined literals
4088 work in presence of default arguments on the literal
4089 operator parameters. */
4090 && parmtypes == void_list_node)
4091 return decl;
4095 return error_mark_node;
4098 /* Parse a user-defined char constant. Returns a call to a user-defined
4099 literal operator taking the character as an argument. */
4101 static cp_expr
4102 cp_parser_userdef_char_literal (cp_parser *parser)
4104 cp_token *token = cp_lexer_consume_token (parser->lexer);
4105 tree literal = token->u.value;
4106 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4107 tree value = USERDEF_LITERAL_VALUE (literal);
4108 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4109 tree decl, result;
4111 /* Build up a call to the user-defined operator */
4112 /* Lookup the name we got back from the id-expression. */
4113 vec<tree, va_gc> *args = make_tree_vector ();
4114 vec_safe_push (args, value);
4115 decl = lookup_literal_operator (name, args);
4116 if (!decl || decl == error_mark_node)
4118 error ("unable to find character literal operator %qD with %qT argument",
4119 name, TREE_TYPE (value));
4120 release_tree_vector (args);
4121 return error_mark_node;
4123 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4124 release_tree_vector (args);
4125 return result;
4128 /* A subroutine of cp_parser_userdef_numeric_literal to
4129 create a char... template parameter pack from a string node. */
4131 static tree
4132 make_char_string_pack (tree value)
4134 tree charvec;
4135 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4136 const char *str = TREE_STRING_POINTER (value);
4137 int i, len = TREE_STRING_LENGTH (value) - 1;
4138 tree argvec = make_tree_vec (1);
4140 /* Fill in CHARVEC with all of the parameters. */
4141 charvec = make_tree_vec (len);
4142 for (i = 0; i < len; ++i)
4143 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4145 /* Build the argument packs. */
4146 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4147 TREE_TYPE (argpack) = char_type_node;
4149 TREE_VEC_ELT (argvec, 0) = argpack;
4151 return argvec;
4154 /* A subroutine of cp_parser_userdef_numeric_literal to
4155 create a char... template parameter pack from a string node. */
4157 static tree
4158 make_string_pack (tree value)
4160 tree charvec;
4161 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4162 const unsigned char *str
4163 = (const unsigned char *) TREE_STRING_POINTER (value);
4164 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4165 int len = TREE_STRING_LENGTH (value) / sz - 1;
4166 tree argvec = make_tree_vec (2);
4168 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4169 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4171 /* First template parm is character type. */
4172 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4174 /* Fill in CHARVEC with all of the parameters. */
4175 charvec = make_tree_vec (len);
4176 for (int i = 0; i < len; ++i)
4177 TREE_VEC_ELT (charvec, i)
4178 = double_int_to_tree (str_char_type_node,
4179 double_int::from_buffer (str + i * sz, sz));
4181 /* Build the argument packs. */
4182 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4183 TREE_TYPE (argpack) = str_char_type_node;
4185 TREE_VEC_ELT (argvec, 1) = argpack;
4187 return argvec;
4190 /* Parse a user-defined numeric constant. returns a call to a user-defined
4191 literal operator. */
4193 static cp_expr
4194 cp_parser_userdef_numeric_literal (cp_parser *parser)
4196 cp_token *token = cp_lexer_consume_token (parser->lexer);
4197 tree literal = token->u.value;
4198 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4199 tree value = USERDEF_LITERAL_VALUE (literal);
4200 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4201 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4202 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4203 tree decl, result;
4204 vec<tree, va_gc> *args;
4206 /* Look for a literal operator taking the exact type of numeric argument
4207 as the literal value. */
4208 args = make_tree_vector ();
4209 vec_safe_push (args, value);
4210 decl = lookup_literal_operator (name, args);
4211 if (decl && decl != error_mark_node)
4213 result = finish_call_expr (decl, &args, false, true,
4214 tf_warning_or_error);
4216 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4218 warning_at (token->location, OPT_Woverflow,
4219 "integer literal exceeds range of %qT type",
4220 long_long_unsigned_type_node);
4222 else
4224 if (overflow > 0)
4225 warning_at (token->location, OPT_Woverflow,
4226 "floating literal exceeds range of %qT type",
4227 long_double_type_node);
4228 else if (overflow < 0)
4229 warning_at (token->location, OPT_Woverflow,
4230 "floating literal truncated to zero");
4233 release_tree_vector (args);
4234 return result;
4236 release_tree_vector (args);
4238 /* If the numeric argument didn't work, look for a raw literal
4239 operator taking a const char* argument consisting of the number
4240 in string format. */
4241 args = make_tree_vector ();
4242 vec_safe_push (args, num_string);
4243 decl = lookup_literal_operator (name, args);
4244 if (decl && decl != error_mark_node)
4246 result = finish_call_expr (decl, &args, false, true,
4247 tf_warning_or_error);
4248 release_tree_vector (args);
4249 return result;
4251 release_tree_vector (args);
4253 /* If the raw literal didn't work, look for a non-type template
4254 function with parameter pack char.... Call the function with
4255 template parameter characters representing the number. */
4256 args = make_tree_vector ();
4257 decl = lookup_literal_operator (name, args);
4258 if (decl && decl != error_mark_node)
4260 tree tmpl_args = make_char_string_pack (num_string);
4261 decl = lookup_template_function (decl, tmpl_args);
4262 result = finish_call_expr (decl, &args, false, true,
4263 tf_warning_or_error);
4264 release_tree_vector (args);
4265 return result;
4268 release_tree_vector (args);
4270 error ("unable to find numeric literal operator %qD", name);
4271 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4272 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4273 "to enable more built-in suffixes");
4274 return error_mark_node;
4277 /* Parse a user-defined string constant. Returns a call to a user-defined
4278 literal operator taking a character pointer and the length of the string
4279 as arguments. */
4281 static tree
4282 cp_parser_userdef_string_literal (tree literal)
4284 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4285 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4286 tree value = USERDEF_LITERAL_VALUE (literal);
4287 int len = TREE_STRING_LENGTH (value)
4288 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4289 tree decl, result;
4290 vec<tree, va_gc> *args;
4292 /* Build up a call to the user-defined operator. */
4293 /* Lookup the name we got back from the id-expression. */
4294 args = make_tree_vector ();
4295 vec_safe_push (args, value);
4296 vec_safe_push (args, build_int_cst (size_type_node, len));
4297 decl = lookup_literal_operator (name, args);
4299 if (decl && decl != error_mark_node)
4301 result = finish_call_expr (decl, &args, false, true,
4302 tf_warning_or_error);
4303 release_tree_vector (args);
4304 return result;
4306 release_tree_vector (args);
4308 /* Look for a template function with typename parameter CharT
4309 and parameter pack CharT... Call the function with
4310 template parameter characters representing the string. */
4311 args = make_tree_vector ();
4312 decl = lookup_literal_operator (name, args);
4313 if (decl && decl != error_mark_node)
4315 tree tmpl_args = make_string_pack (value);
4316 decl = lookup_template_function (decl, tmpl_args);
4317 result = finish_call_expr (decl, &args, false, true,
4318 tf_warning_or_error);
4319 release_tree_vector (args);
4320 return result;
4322 release_tree_vector (args);
4324 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4325 name, TREE_TYPE (value), size_type_node);
4326 return error_mark_node;
4330 /* Basic concepts [gram.basic] */
4332 /* Parse a translation-unit.
4334 translation-unit:
4335 declaration-seq [opt]
4337 Returns TRUE if all went well. */
4339 static bool
4340 cp_parser_translation_unit (cp_parser* parser)
4342 /* The address of the first non-permanent object on the declarator
4343 obstack. */
4344 static void *declarator_obstack_base;
4346 bool success;
4348 /* Create the declarator obstack, if necessary. */
4349 if (!cp_error_declarator)
4351 gcc_obstack_init (&declarator_obstack);
4352 /* Create the error declarator. */
4353 cp_error_declarator = make_declarator (cdk_error);
4354 /* Create the empty parameter list. */
4355 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4356 /* Remember where the base of the declarator obstack lies. */
4357 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4360 cp_parser_declaration_seq_opt (parser);
4362 /* If there are no tokens left then all went well. */
4363 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4365 /* Get rid of the token array; we don't need it any more. */
4366 cp_lexer_destroy (parser->lexer);
4367 parser->lexer = NULL;
4369 /* This file might have been a context that's implicitly extern
4370 "C". If so, pop the lang context. (Only relevant for PCH.) */
4371 if (parser->implicit_extern_c)
4373 pop_lang_context ();
4374 parser->implicit_extern_c = false;
4377 /* Finish up. */
4378 finish_translation_unit ();
4380 success = true;
4382 else
4384 cp_parser_error (parser, "expected declaration");
4385 success = false;
4388 /* Make sure the declarator obstack was fully cleaned up. */
4389 gcc_assert (obstack_next_free (&declarator_obstack)
4390 == declarator_obstack_base);
4392 /* All went well. */
4393 return success;
4396 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4397 decltype context. */
4399 static inline tsubst_flags_t
4400 complain_flags (bool decltype_p)
4402 tsubst_flags_t complain = tf_warning_or_error;
4403 if (decltype_p)
4404 complain |= tf_decltype;
4405 return complain;
4408 /* We're about to parse a collection of statements. If we're currently
4409 parsing tentatively, set up a firewall so that any nested
4410 cp_parser_commit_to_tentative_parse won't affect the current context. */
4412 static cp_token_position
4413 cp_parser_start_tentative_firewall (cp_parser *parser)
4415 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4416 return 0;
4418 cp_parser_parse_tentatively (parser);
4419 cp_parser_commit_to_topmost_tentative_parse (parser);
4420 return cp_lexer_token_position (parser->lexer, false);
4423 /* We've finished parsing the collection of statements. Wrap up the
4424 firewall and replace the relevant tokens with the parsed form. */
4426 static void
4427 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4428 tree expr)
4430 if (!start)
4431 return;
4433 /* Finish the firewall level. */
4434 cp_parser_parse_definitely (parser);
4435 /* And remember the result of the parse for when we try again. */
4436 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4437 token->type = CPP_PREPARSED_EXPR;
4438 token->u.value = expr;
4439 token->keyword = RID_MAX;
4440 cp_lexer_purge_tokens_after (parser->lexer, start);
4443 /* Like the above functions, but let the user modify the tokens. Used by
4444 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4445 later parses, so it makes sense to localize the effects of
4446 cp_parser_commit_to_tentative_parse. */
4448 struct tentative_firewall
4450 cp_parser *parser;
4451 bool set;
4453 tentative_firewall (cp_parser *p): parser(p)
4455 /* If we're currently parsing tentatively, start a committed level as a
4456 firewall and then an inner tentative parse. */
4457 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4459 cp_parser_parse_tentatively (parser);
4460 cp_parser_commit_to_topmost_tentative_parse (parser);
4461 cp_parser_parse_tentatively (parser);
4465 ~tentative_firewall()
4467 if (set)
4469 /* Finish the inner tentative parse and the firewall, propagating any
4470 uncommitted error state to the outer tentative parse. */
4471 bool err = cp_parser_error_occurred (parser);
4472 cp_parser_parse_definitely (parser);
4473 cp_parser_parse_definitely (parser);
4474 if (err)
4475 cp_parser_simulate_error (parser);
4480 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4481 enclosing parentheses. */
4483 static cp_expr
4484 cp_parser_statement_expr (cp_parser *parser)
4486 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4488 /* Consume the '('. */
4489 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4490 cp_lexer_consume_token (parser->lexer);
4491 /* Start the statement-expression. */
4492 tree expr = begin_stmt_expr ();
4493 /* Parse the compound-statement. */
4494 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4495 /* Finish up. */
4496 expr = finish_stmt_expr (expr, false);
4497 /* Consume the ')'. */
4498 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4499 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4500 cp_parser_skip_to_end_of_statement (parser);
4502 cp_parser_end_tentative_firewall (parser, start, expr);
4503 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4504 return cp_expr (expr, combined_loc);
4507 /* Expressions [gram.expr] */
4509 /* Parse a fold-operator.
4511 fold-operator:
4512 - * / % ^ & | = < > << >>
4513 = -= *= /= %= ^= &= |= <<= >>=
4514 == != <= >= && || , .* ->*
4516 This returns the tree code corresponding to the matched operator
4517 as an int. When the current token matches a compound assignment
4518 opertor, the resulting tree code is the negative value of the
4519 non-assignment operator. */
4521 static int
4522 cp_parser_fold_operator (cp_token *token)
4524 switch (token->type)
4526 case CPP_PLUS: return PLUS_EXPR;
4527 case CPP_MINUS: return MINUS_EXPR;
4528 case CPP_MULT: return MULT_EXPR;
4529 case CPP_DIV: return TRUNC_DIV_EXPR;
4530 case CPP_MOD: return TRUNC_MOD_EXPR;
4531 case CPP_XOR: return BIT_XOR_EXPR;
4532 case CPP_AND: return BIT_AND_EXPR;
4533 case CPP_OR: return BIT_IOR_EXPR;
4534 case CPP_LSHIFT: return LSHIFT_EXPR;
4535 case CPP_RSHIFT: return RSHIFT_EXPR;
4537 case CPP_EQ: return -NOP_EXPR;
4538 case CPP_PLUS_EQ: return -PLUS_EXPR;
4539 case CPP_MINUS_EQ: return -MINUS_EXPR;
4540 case CPP_MULT_EQ: return -MULT_EXPR;
4541 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4542 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4543 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4544 case CPP_AND_EQ: return -BIT_AND_EXPR;
4545 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4546 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4547 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4549 case CPP_EQ_EQ: return EQ_EXPR;
4550 case CPP_NOT_EQ: return NE_EXPR;
4551 case CPP_LESS: return LT_EXPR;
4552 case CPP_GREATER: return GT_EXPR;
4553 case CPP_LESS_EQ: return LE_EXPR;
4554 case CPP_GREATER_EQ: return GE_EXPR;
4556 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4557 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4559 case CPP_COMMA: return COMPOUND_EXPR;
4561 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4562 case CPP_DEREF_STAR: return MEMBER_REF;
4564 default: return ERROR_MARK;
4568 /* Returns true if CODE indicates a binary expression, which is not allowed in
4569 the LHS of a fold-expression. More codes will need to be added to use this
4570 function in other contexts. */
4572 static bool
4573 is_binary_op (tree_code code)
4575 switch (code)
4577 case PLUS_EXPR:
4578 case POINTER_PLUS_EXPR:
4579 case MINUS_EXPR:
4580 case MULT_EXPR:
4581 case TRUNC_DIV_EXPR:
4582 case TRUNC_MOD_EXPR:
4583 case BIT_XOR_EXPR:
4584 case BIT_AND_EXPR:
4585 case BIT_IOR_EXPR:
4586 case LSHIFT_EXPR:
4587 case RSHIFT_EXPR:
4589 case MODOP_EXPR:
4591 case EQ_EXPR:
4592 case NE_EXPR:
4593 case LE_EXPR:
4594 case GE_EXPR:
4595 case LT_EXPR:
4596 case GT_EXPR:
4598 case TRUTH_ANDIF_EXPR:
4599 case TRUTH_ORIF_EXPR:
4601 case COMPOUND_EXPR:
4603 case DOTSTAR_EXPR:
4604 case MEMBER_REF:
4605 return true;
4607 default:
4608 return false;
4612 /* If the next token is a suitable fold operator, consume it and return as
4613 the function above. */
4615 static int
4616 cp_parser_fold_operator (cp_parser *parser)
4618 cp_token* token = cp_lexer_peek_token (parser->lexer);
4619 int code = cp_parser_fold_operator (token);
4620 if (code != ERROR_MARK)
4621 cp_lexer_consume_token (parser->lexer);
4622 return code;
4625 /* Parse a fold-expression.
4627 fold-expression:
4628 ( ... folding-operator cast-expression)
4629 ( cast-expression folding-operator ... )
4630 ( cast-expression folding operator ... folding-operator cast-expression)
4632 Note that the '(' and ')' are matched in primary expression. */
4634 static cp_expr
4635 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4637 cp_id_kind pidk;
4639 // Left fold.
4640 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4642 cp_lexer_consume_token (parser->lexer);
4643 int op = cp_parser_fold_operator (parser);
4644 if (op == ERROR_MARK)
4646 cp_parser_error (parser, "expected binary operator");
4647 return error_mark_node;
4650 tree expr = cp_parser_cast_expression (parser, false, false,
4651 false, &pidk);
4652 if (expr == error_mark_node)
4653 return error_mark_node;
4654 return finish_left_unary_fold_expr (expr, op);
4657 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4658 int op = cp_parser_fold_operator (parser);
4659 if (op == ERROR_MARK)
4661 cp_parser_error (parser, "expected binary operator");
4662 return error_mark_node;
4665 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4667 cp_parser_error (parser, "expected ...");
4668 return error_mark_node;
4670 cp_lexer_consume_token (parser->lexer);
4672 /* The operands of a fold-expression are cast-expressions, so binary or
4673 conditional expressions are not allowed. We check this here to avoid
4674 tentative parsing. */
4675 if (is_binary_op (TREE_CODE (expr1)))
4676 error_at (location_of (expr1),
4677 "binary expression in operand of fold-expression");
4678 else if (TREE_CODE (expr1) == COND_EXPR)
4679 error_at (location_of (expr1),
4680 "conditional expression in operand of fold-expression");
4682 // Right fold.
4683 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4684 return finish_right_unary_fold_expr (expr1, op);
4686 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4688 cp_parser_error (parser, "mismatched operator in fold-expression");
4689 return error_mark_node;
4691 cp_lexer_consume_token (parser->lexer);
4693 // Binary left or right fold.
4694 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4695 if (expr2 == error_mark_node)
4696 return error_mark_node;
4697 return finish_binary_fold_expr (expr1, expr2, op);
4700 /* Parse a primary-expression.
4702 primary-expression:
4703 literal
4704 this
4705 ( expression )
4706 id-expression
4707 lambda-expression (C++11)
4709 GNU Extensions:
4711 primary-expression:
4712 ( compound-statement )
4713 __builtin_va_arg ( assignment-expression , type-id )
4714 __builtin_offsetof ( type-id , offsetof-expression )
4716 C++ Extensions:
4717 __has_nothrow_assign ( type-id )
4718 __has_nothrow_constructor ( type-id )
4719 __has_nothrow_copy ( type-id )
4720 __has_trivial_assign ( type-id )
4721 __has_trivial_constructor ( type-id )
4722 __has_trivial_copy ( type-id )
4723 __has_trivial_destructor ( type-id )
4724 __has_virtual_destructor ( type-id )
4725 __is_abstract ( type-id )
4726 __is_base_of ( type-id , type-id )
4727 __is_class ( type-id )
4728 __is_empty ( type-id )
4729 __is_enum ( type-id )
4730 __is_final ( type-id )
4731 __is_literal_type ( type-id )
4732 __is_pod ( type-id )
4733 __is_polymorphic ( type-id )
4734 __is_std_layout ( type-id )
4735 __is_trivial ( type-id )
4736 __is_union ( type-id )
4738 Objective-C++ Extension:
4740 primary-expression:
4741 objc-expression
4743 literal:
4744 __null
4746 ADDRESS_P is true iff this expression was immediately preceded by
4747 "&" and therefore might denote a pointer-to-member. CAST_P is true
4748 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4749 true iff this expression is a template argument.
4751 Returns a representation of the expression. Upon return, *IDK
4752 indicates what kind of id-expression (if any) was present. */
4754 static cp_expr
4755 cp_parser_primary_expression (cp_parser *parser,
4756 bool address_p,
4757 bool cast_p,
4758 bool template_arg_p,
4759 bool decltype_p,
4760 cp_id_kind *idk)
4762 cp_token *token = NULL;
4764 /* Assume the primary expression is not an id-expression. */
4765 *idk = CP_ID_KIND_NONE;
4767 /* Peek at the next token. */
4768 token = cp_lexer_peek_token (parser->lexer);
4769 switch ((int) token->type)
4771 /* literal:
4772 integer-literal
4773 character-literal
4774 floating-literal
4775 string-literal
4776 boolean-literal
4777 pointer-literal
4778 user-defined-literal */
4779 case CPP_CHAR:
4780 case CPP_CHAR16:
4781 case CPP_CHAR32:
4782 case CPP_WCHAR:
4783 case CPP_UTF8CHAR:
4784 case CPP_NUMBER:
4785 case CPP_PREPARSED_EXPR:
4786 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4787 return cp_parser_userdef_numeric_literal (parser);
4788 token = cp_lexer_consume_token (parser->lexer);
4789 if (TREE_CODE (token->u.value) == FIXED_CST)
4791 error_at (token->location,
4792 "fixed-point types not supported in C++");
4793 return error_mark_node;
4795 /* Floating-point literals are only allowed in an integral
4796 constant expression if they are cast to an integral or
4797 enumeration type. */
4798 if (TREE_CODE (token->u.value) == REAL_CST
4799 && parser->integral_constant_expression_p
4800 && pedantic)
4802 /* CAST_P will be set even in invalid code like "int(2.7 +
4803 ...)". Therefore, we have to check that the next token
4804 is sure to end the cast. */
4805 if (cast_p)
4807 cp_token *next_token;
4809 next_token = cp_lexer_peek_token (parser->lexer);
4810 if (/* The comma at the end of an
4811 enumerator-definition. */
4812 next_token->type != CPP_COMMA
4813 /* The curly brace at the end of an enum-specifier. */
4814 && next_token->type != CPP_CLOSE_BRACE
4815 /* The end of a statement. */
4816 && next_token->type != CPP_SEMICOLON
4817 /* The end of the cast-expression. */
4818 && next_token->type != CPP_CLOSE_PAREN
4819 /* The end of an array bound. */
4820 && next_token->type != CPP_CLOSE_SQUARE
4821 /* The closing ">" in a template-argument-list. */
4822 && (next_token->type != CPP_GREATER
4823 || parser->greater_than_is_operator_p)
4824 /* C++0x only: A ">>" treated like two ">" tokens,
4825 in a template-argument-list. */
4826 && (next_token->type != CPP_RSHIFT
4827 || (cxx_dialect == cxx98)
4828 || parser->greater_than_is_operator_p))
4829 cast_p = false;
4832 /* If we are within a cast, then the constraint that the
4833 cast is to an integral or enumeration type will be
4834 checked at that point. If we are not within a cast, then
4835 this code is invalid. */
4836 if (!cast_p)
4837 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4839 return cp_expr (token->u.value, token->location);
4841 case CPP_CHAR_USERDEF:
4842 case CPP_CHAR16_USERDEF:
4843 case CPP_CHAR32_USERDEF:
4844 case CPP_WCHAR_USERDEF:
4845 case CPP_UTF8CHAR_USERDEF:
4846 return cp_parser_userdef_char_literal (parser);
4848 case CPP_STRING:
4849 case CPP_STRING16:
4850 case CPP_STRING32:
4851 case CPP_WSTRING:
4852 case CPP_UTF8STRING:
4853 case CPP_STRING_USERDEF:
4854 case CPP_STRING16_USERDEF:
4855 case CPP_STRING32_USERDEF:
4856 case CPP_WSTRING_USERDEF:
4857 case CPP_UTF8STRING_USERDEF:
4858 /* ??? Should wide strings be allowed when parser->translate_strings_p
4859 is false (i.e. in attributes)? If not, we can kill the third
4860 argument to cp_parser_string_literal. */
4861 return cp_parser_string_literal (parser,
4862 parser->translate_strings_p,
4863 true);
4865 case CPP_OPEN_PAREN:
4866 /* If we see `( { ' then we are looking at the beginning of
4867 a GNU statement-expression. */
4868 if (cp_parser_allow_gnu_extensions_p (parser)
4869 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4871 /* Statement-expressions are not allowed by the standard. */
4872 pedwarn (token->location, OPT_Wpedantic,
4873 "ISO C++ forbids braced-groups within expressions");
4875 /* And they're not allowed outside of a function-body; you
4876 cannot, for example, write:
4878 int i = ({ int j = 3; j + 1; });
4880 at class or namespace scope. */
4881 if (!parser->in_function_body
4882 || parser->in_template_argument_list_p)
4884 error_at (token->location,
4885 "statement-expressions are not allowed outside "
4886 "functions nor in template-argument lists");
4887 cp_parser_skip_to_end_of_block_or_statement (parser);
4888 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4889 cp_lexer_consume_token (parser->lexer);
4890 return error_mark_node;
4892 else
4893 return cp_parser_statement_expr (parser);
4895 /* Otherwise it's a normal parenthesized expression. */
4897 cp_expr expr;
4898 bool saved_greater_than_is_operator_p;
4900 location_t open_paren_loc = token->location;
4902 /* Consume the `('. */
4903 cp_lexer_consume_token (parser->lexer);
4904 /* Within a parenthesized expression, a `>' token is always
4905 the greater-than operator. */
4906 saved_greater_than_is_operator_p
4907 = parser->greater_than_is_operator_p;
4908 parser->greater_than_is_operator_p = true;
4910 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4911 /* Left fold expression. */
4912 expr = NULL_TREE;
4913 else
4914 /* Parse the parenthesized expression. */
4915 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4917 token = cp_lexer_peek_token (parser->lexer);
4918 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4920 expr = cp_parser_fold_expression (parser, expr);
4921 if (expr != error_mark_node
4922 && cxx_dialect < cxx1z
4923 && !in_system_header_at (input_location))
4924 pedwarn (input_location, 0, "fold-expressions only available "
4925 "with -std=c++1z or -std=gnu++1z");
4927 else
4928 /* Let the front end know that this expression was
4929 enclosed in parentheses. This matters in case, for
4930 example, the expression is of the form `A::B', since
4931 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4932 not. */
4933 expr = finish_parenthesized_expr (expr);
4935 /* DR 705: Wrapping an unqualified name in parentheses
4936 suppresses arg-dependent lookup. We want to pass back
4937 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4938 (c++/37862), but none of the others. */
4939 if (*idk != CP_ID_KIND_QUALIFIED)
4940 *idk = CP_ID_KIND_NONE;
4942 /* The `>' token might be the end of a template-id or
4943 template-parameter-list now. */
4944 parser->greater_than_is_operator_p
4945 = saved_greater_than_is_operator_p;
4947 /* Consume the `)'. */
4948 token = cp_lexer_peek_token (parser->lexer);
4949 location_t close_paren_loc = token->location;
4950 expr.set_range (open_paren_loc, close_paren_loc);
4951 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4952 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4953 cp_parser_skip_to_end_of_statement (parser);
4955 return expr;
4958 case CPP_OPEN_SQUARE:
4960 if (c_dialect_objc ())
4962 /* We might have an Objective-C++ message. */
4963 cp_parser_parse_tentatively (parser);
4964 tree msg = cp_parser_objc_message_expression (parser);
4965 /* If that works out, we're done ... */
4966 if (cp_parser_parse_definitely (parser))
4967 return msg;
4968 /* ... else, fall though to see if it's a lambda. */
4970 cp_expr lam = cp_parser_lambda_expression (parser);
4971 /* Don't warn about a failed tentative parse. */
4972 if (cp_parser_error_occurred (parser))
4973 return error_mark_node;
4974 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4975 return lam;
4978 case CPP_OBJC_STRING:
4979 if (c_dialect_objc ())
4980 /* We have an Objective-C++ string literal. */
4981 return cp_parser_objc_expression (parser);
4982 cp_parser_error (parser, "expected primary-expression");
4983 return error_mark_node;
4985 case CPP_KEYWORD:
4986 switch (token->keyword)
4988 /* These two are the boolean literals. */
4989 case RID_TRUE:
4990 cp_lexer_consume_token (parser->lexer);
4991 return cp_expr (boolean_true_node, token->location);
4992 case RID_FALSE:
4993 cp_lexer_consume_token (parser->lexer);
4994 return cp_expr (boolean_false_node, token->location);
4996 /* The `__null' literal. */
4997 case RID_NULL:
4998 cp_lexer_consume_token (parser->lexer);
4999 return cp_expr (null_node, token->location);
5001 /* The `nullptr' literal. */
5002 case RID_NULLPTR:
5003 cp_lexer_consume_token (parser->lexer);
5004 return cp_expr (nullptr_node, token->location);
5006 /* Recognize the `this' keyword. */
5007 case RID_THIS:
5008 cp_lexer_consume_token (parser->lexer);
5009 if (parser->local_variables_forbidden_p)
5011 error_at (token->location,
5012 "%<this%> may not be used in this context");
5013 return error_mark_node;
5015 /* Pointers cannot appear in constant-expressions. */
5016 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5017 return error_mark_node;
5018 return cp_expr (finish_this_expr (), token->location);
5020 /* The `operator' keyword can be the beginning of an
5021 id-expression. */
5022 case RID_OPERATOR:
5023 goto id_expression;
5025 case RID_FUNCTION_NAME:
5026 case RID_PRETTY_FUNCTION_NAME:
5027 case RID_C99_FUNCTION_NAME:
5029 non_integral_constant name;
5031 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5032 __func__ are the names of variables -- but they are
5033 treated specially. Therefore, they are handled here,
5034 rather than relying on the generic id-expression logic
5035 below. Grammatically, these names are id-expressions.
5037 Consume the token. */
5038 token = cp_lexer_consume_token (parser->lexer);
5040 switch (token->keyword)
5042 case RID_FUNCTION_NAME:
5043 name = NIC_FUNC_NAME;
5044 break;
5045 case RID_PRETTY_FUNCTION_NAME:
5046 name = NIC_PRETTY_FUNC;
5047 break;
5048 case RID_C99_FUNCTION_NAME:
5049 name = NIC_C99_FUNC;
5050 break;
5051 default:
5052 gcc_unreachable ();
5055 if (cp_parser_non_integral_constant_expression (parser, name))
5056 return error_mark_node;
5058 /* Look up the name. */
5059 return finish_fname (token->u.value);
5062 case RID_VA_ARG:
5064 tree expression;
5065 tree type;
5066 source_location type_location;
5067 location_t start_loc
5068 = cp_lexer_peek_token (parser->lexer)->location;
5069 /* The `__builtin_va_arg' construct is used to handle
5070 `va_arg'. Consume the `__builtin_va_arg' token. */
5071 cp_lexer_consume_token (parser->lexer);
5072 /* Look for the opening `('. */
5073 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5074 /* Now, parse the assignment-expression. */
5075 expression = cp_parser_assignment_expression (parser);
5076 /* Look for the `,'. */
5077 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5078 type_location = cp_lexer_peek_token (parser->lexer)->location;
5079 /* Parse the type-id. */
5081 type_id_in_expr_sentinel s (parser);
5082 type = cp_parser_type_id (parser);
5084 /* Look for the closing `)'. */
5085 location_t finish_loc
5086 = cp_lexer_peek_token (parser->lexer)->location;
5087 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5088 /* Using `va_arg' in a constant-expression is not
5089 allowed. */
5090 if (cp_parser_non_integral_constant_expression (parser,
5091 NIC_VA_ARG))
5092 return error_mark_node;
5093 /* Construct a location of the form:
5094 __builtin_va_arg (v, int)
5095 ~~~~~~~~~~~~~~~~~~~~~^~~~
5096 with the caret at the type, ranging from the start of the
5097 "__builtin_va_arg" token to the close paren. */
5098 location_t combined_loc
5099 = make_location (type_location, start_loc, finish_loc);
5100 return build_x_va_arg (combined_loc, expression, type);
5103 case RID_OFFSETOF:
5104 return cp_parser_builtin_offsetof (parser);
5106 case RID_HAS_NOTHROW_ASSIGN:
5107 case RID_HAS_NOTHROW_CONSTRUCTOR:
5108 case RID_HAS_NOTHROW_COPY:
5109 case RID_HAS_TRIVIAL_ASSIGN:
5110 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5111 case RID_HAS_TRIVIAL_COPY:
5112 case RID_HAS_TRIVIAL_DESTRUCTOR:
5113 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5114 case RID_HAS_VIRTUAL_DESTRUCTOR:
5115 case RID_IS_ABSTRACT:
5116 case RID_IS_BASE_OF:
5117 case RID_IS_CLASS:
5118 case RID_IS_EMPTY:
5119 case RID_IS_ENUM:
5120 case RID_IS_FINAL:
5121 case RID_IS_LITERAL_TYPE:
5122 case RID_IS_POD:
5123 case RID_IS_POLYMORPHIC:
5124 case RID_IS_SAME_AS:
5125 case RID_IS_STD_LAYOUT:
5126 case RID_IS_TRIVIAL:
5127 case RID_IS_TRIVIALLY_ASSIGNABLE:
5128 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5129 case RID_IS_TRIVIALLY_COPYABLE:
5130 case RID_IS_UNION:
5131 return cp_parser_trait_expr (parser, token->keyword);
5133 // C++ concepts
5134 case RID_REQUIRES:
5135 return cp_parser_requires_expression (parser);
5137 /* Objective-C++ expressions. */
5138 case RID_AT_ENCODE:
5139 case RID_AT_PROTOCOL:
5140 case RID_AT_SELECTOR:
5141 return cp_parser_objc_expression (parser);
5143 case RID_TEMPLATE:
5144 if (parser->in_function_body
5145 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5146 == CPP_LESS))
5148 error_at (token->location,
5149 "a template declaration cannot appear at block scope");
5150 cp_parser_skip_to_end_of_block_or_statement (parser);
5151 return error_mark_node;
5153 /* FALLTHRU */
5154 default:
5155 cp_parser_error (parser, "expected primary-expression");
5156 return error_mark_node;
5159 /* An id-expression can start with either an identifier, a
5160 `::' as the beginning of a qualified-id, or the "operator"
5161 keyword. */
5162 case CPP_NAME:
5163 case CPP_SCOPE:
5164 case CPP_TEMPLATE_ID:
5165 case CPP_NESTED_NAME_SPECIFIER:
5167 id_expression:
5168 cp_expr id_expression;
5169 cp_expr decl;
5170 const char *error_msg;
5171 bool template_p;
5172 bool done;
5173 cp_token *id_expr_token;
5175 /* Parse the id-expression. */
5176 id_expression
5177 = cp_parser_id_expression (parser,
5178 /*template_keyword_p=*/false,
5179 /*check_dependency_p=*/true,
5180 &template_p,
5181 /*declarator_p=*/false,
5182 /*optional_p=*/false);
5183 if (id_expression == error_mark_node)
5184 return error_mark_node;
5185 id_expr_token = token;
5186 token = cp_lexer_peek_token (parser->lexer);
5187 done = (token->type != CPP_OPEN_SQUARE
5188 && token->type != CPP_OPEN_PAREN
5189 && token->type != CPP_DOT
5190 && token->type != CPP_DEREF
5191 && token->type != CPP_PLUS_PLUS
5192 && token->type != CPP_MINUS_MINUS);
5193 /* If we have a template-id, then no further lookup is
5194 required. If the template-id was for a template-class, we
5195 will sometimes have a TYPE_DECL at this point. */
5196 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5197 || TREE_CODE (id_expression) == TYPE_DECL)
5198 decl = id_expression;
5199 /* Look up the name. */
5200 else
5202 tree ambiguous_decls;
5204 /* If we already know that this lookup is ambiguous, then
5205 we've already issued an error message; there's no reason
5206 to check again. */
5207 if (id_expr_token->type == CPP_NAME
5208 && id_expr_token->error_reported)
5210 cp_parser_simulate_error (parser);
5211 return error_mark_node;
5214 decl = cp_parser_lookup_name (parser, id_expression,
5215 none_type,
5216 template_p,
5217 /*is_namespace=*/false,
5218 /*check_dependency=*/true,
5219 &ambiguous_decls,
5220 id_expr_token->location);
5221 /* If the lookup was ambiguous, an error will already have
5222 been issued. */
5223 if (ambiguous_decls)
5224 return error_mark_node;
5226 /* In Objective-C++, we may have an Objective-C 2.0
5227 dot-syntax for classes here. */
5228 if (c_dialect_objc ()
5229 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5230 && TREE_CODE (decl) == TYPE_DECL
5231 && objc_is_class_name (decl))
5233 tree component;
5234 cp_lexer_consume_token (parser->lexer);
5235 component = cp_parser_identifier (parser);
5236 if (component == error_mark_node)
5237 return error_mark_node;
5239 tree result = objc_build_class_component_ref (id_expression,
5240 component);
5241 /* Build a location of the form:
5242 expr.component
5243 ~~~~~^~~~~~~~~
5244 with caret at the start of the component name (at
5245 input_location), ranging from the start of the id_expression
5246 to the end of the component name. */
5247 location_t combined_loc
5248 = make_location (input_location, id_expression.get_start (),
5249 get_finish (input_location));
5250 protected_set_expr_location (result, combined_loc);
5251 return result;
5254 /* In Objective-C++, an instance variable (ivar) may be preferred
5255 to whatever cp_parser_lookup_name() found.
5256 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5257 rest of c-family, we have to do a little extra work to preserve
5258 any location information in cp_expr "decl". Given that
5259 objc_lookup_ivar is implemented in "c-family" and "objc", we
5260 have a trip through the pure "tree" type, rather than cp_expr.
5261 Naively copying it back to "decl" would implicitly give the
5262 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5263 store an EXPR_LOCATION. Hence we only update "decl" (and
5264 hence its location_t) if we get back a different tree node. */
5265 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5266 id_expression);
5267 if (decl_tree != decl.get_value ())
5268 decl = cp_expr (decl_tree);
5270 /* If name lookup gives us a SCOPE_REF, then the
5271 qualifying scope was dependent. */
5272 if (TREE_CODE (decl) == SCOPE_REF)
5274 /* At this point, we do not know if DECL is a valid
5275 integral constant expression. We assume that it is
5276 in fact such an expression, so that code like:
5278 template <int N> struct A {
5279 int a[B<N>::i];
5282 is accepted. At template-instantiation time, we
5283 will check that B<N>::i is actually a constant. */
5284 return decl;
5286 /* Check to see if DECL is a local variable in a context
5287 where that is forbidden. */
5288 if (parser->local_variables_forbidden_p
5289 && local_variable_p (decl))
5291 /* It might be that we only found DECL because we are
5292 trying to be generous with pre-ISO scoping rules.
5293 For example, consider:
5295 int i;
5296 void g() {
5297 for (int i = 0; i < 10; ++i) {}
5298 extern void f(int j = i);
5301 Here, name look up will originally find the out
5302 of scope `i'. We need to issue a warning message,
5303 but then use the global `i'. */
5304 decl = check_for_out_of_scope_variable (decl);
5305 if (local_variable_p (decl))
5307 error_at (id_expr_token->location,
5308 "local variable %qD may not appear in this context",
5309 decl.get_value ());
5310 return error_mark_node;
5315 decl = (finish_id_expression
5316 (id_expression, decl, parser->scope,
5317 idk,
5318 parser->integral_constant_expression_p,
5319 parser->allow_non_integral_constant_expression_p,
5320 &parser->non_integral_constant_expression_p,
5321 template_p, done, address_p,
5322 template_arg_p,
5323 &error_msg,
5324 id_expr_token->location));
5325 if (error_msg)
5326 cp_parser_error (parser, error_msg);
5327 decl.set_location (id_expr_token->location);
5328 return decl;
5331 /* Anything else is an error. */
5332 default:
5333 cp_parser_error (parser, "expected primary-expression");
5334 return error_mark_node;
5338 static inline cp_expr
5339 cp_parser_primary_expression (cp_parser *parser,
5340 bool address_p,
5341 bool cast_p,
5342 bool template_arg_p,
5343 cp_id_kind *idk)
5345 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5346 /*decltype*/false, idk);
5349 /* Parse an id-expression.
5351 id-expression:
5352 unqualified-id
5353 qualified-id
5355 qualified-id:
5356 :: [opt] nested-name-specifier template [opt] unqualified-id
5357 :: identifier
5358 :: operator-function-id
5359 :: template-id
5361 Return a representation of the unqualified portion of the
5362 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5363 a `::' or nested-name-specifier.
5365 Often, if the id-expression was a qualified-id, the caller will
5366 want to make a SCOPE_REF to represent the qualified-id. This
5367 function does not do this in order to avoid wastefully creating
5368 SCOPE_REFs when they are not required.
5370 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5371 `template' keyword.
5373 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5374 uninstantiated templates.
5376 If *TEMPLATE_P is non-NULL, it is set to true iff the
5377 `template' keyword is used to explicitly indicate that the entity
5378 named is a template.
5380 If DECLARATOR_P is true, the id-expression is appearing as part of
5381 a declarator, rather than as part of an expression. */
5383 static cp_expr
5384 cp_parser_id_expression (cp_parser *parser,
5385 bool template_keyword_p,
5386 bool check_dependency_p,
5387 bool *template_p,
5388 bool declarator_p,
5389 bool optional_p)
5391 bool global_scope_p;
5392 bool nested_name_specifier_p;
5394 /* Assume the `template' keyword was not used. */
5395 if (template_p)
5396 *template_p = template_keyword_p;
5398 /* Look for the optional `::' operator. */
5399 global_scope_p
5400 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5401 != NULL_TREE);
5402 /* Look for the optional nested-name-specifier. */
5403 nested_name_specifier_p
5404 = (cp_parser_nested_name_specifier_opt (parser,
5405 /*typename_keyword_p=*/false,
5406 check_dependency_p,
5407 /*type_p=*/false,
5408 declarator_p)
5409 != NULL_TREE);
5410 /* If there is a nested-name-specifier, then we are looking at
5411 the first qualified-id production. */
5412 if (nested_name_specifier_p)
5414 tree saved_scope;
5415 tree saved_object_scope;
5416 tree saved_qualifying_scope;
5417 tree unqualified_id;
5418 bool is_template;
5420 /* See if the next token is the `template' keyword. */
5421 if (!template_p)
5422 template_p = &is_template;
5423 *template_p = cp_parser_optional_template_keyword (parser);
5424 /* Name lookup we do during the processing of the
5425 unqualified-id might obliterate SCOPE. */
5426 saved_scope = parser->scope;
5427 saved_object_scope = parser->object_scope;
5428 saved_qualifying_scope = parser->qualifying_scope;
5429 /* Process the final unqualified-id. */
5430 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5431 check_dependency_p,
5432 declarator_p,
5433 /*optional_p=*/false);
5434 /* Restore the SAVED_SCOPE for our caller. */
5435 parser->scope = saved_scope;
5436 parser->object_scope = saved_object_scope;
5437 parser->qualifying_scope = saved_qualifying_scope;
5439 return unqualified_id;
5441 /* Otherwise, if we are in global scope, then we are looking at one
5442 of the other qualified-id productions. */
5443 else if (global_scope_p)
5445 cp_token *token;
5446 tree id;
5448 /* Peek at the next token. */
5449 token = cp_lexer_peek_token (parser->lexer);
5451 /* If it's an identifier, and the next token is not a "<", then
5452 we can avoid the template-id case. This is an optimization
5453 for this common case. */
5454 if (token->type == CPP_NAME
5455 && !cp_parser_nth_token_starts_template_argument_list_p
5456 (parser, 2))
5457 return cp_parser_identifier (parser);
5459 cp_parser_parse_tentatively (parser);
5460 /* Try a template-id. */
5461 id = cp_parser_template_id (parser,
5462 /*template_keyword_p=*/false,
5463 /*check_dependency_p=*/true,
5464 none_type,
5465 declarator_p);
5466 /* If that worked, we're done. */
5467 if (cp_parser_parse_definitely (parser))
5468 return id;
5470 /* Peek at the next token. (Changes in the token buffer may
5471 have invalidated the pointer obtained above.) */
5472 token = cp_lexer_peek_token (parser->lexer);
5474 switch (token->type)
5476 case CPP_NAME:
5477 return cp_parser_identifier (parser);
5479 case CPP_KEYWORD:
5480 if (token->keyword == RID_OPERATOR)
5481 return cp_parser_operator_function_id (parser);
5482 /* Fall through. */
5484 default:
5485 cp_parser_error (parser, "expected id-expression");
5486 return error_mark_node;
5489 else
5490 return cp_parser_unqualified_id (parser, template_keyword_p,
5491 /*check_dependency_p=*/true,
5492 declarator_p,
5493 optional_p);
5496 /* Parse an unqualified-id.
5498 unqualified-id:
5499 identifier
5500 operator-function-id
5501 conversion-function-id
5502 ~ class-name
5503 template-id
5505 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5506 keyword, in a construct like `A::template ...'.
5508 Returns a representation of unqualified-id. For the `identifier'
5509 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5510 production a BIT_NOT_EXPR is returned; the operand of the
5511 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5512 other productions, see the documentation accompanying the
5513 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5514 names are looked up in uninstantiated templates. If DECLARATOR_P
5515 is true, the unqualified-id is appearing as part of a declarator,
5516 rather than as part of an expression. */
5518 static cp_expr
5519 cp_parser_unqualified_id (cp_parser* parser,
5520 bool template_keyword_p,
5521 bool check_dependency_p,
5522 bool declarator_p,
5523 bool optional_p)
5525 cp_token *token;
5527 /* Peek at the next token. */
5528 token = cp_lexer_peek_token (parser->lexer);
5530 switch ((int) token->type)
5532 case CPP_NAME:
5534 tree id;
5536 /* We don't know yet whether or not this will be a
5537 template-id. */
5538 cp_parser_parse_tentatively (parser);
5539 /* Try a template-id. */
5540 id = cp_parser_template_id (parser, template_keyword_p,
5541 check_dependency_p,
5542 none_type,
5543 declarator_p);
5544 /* If it worked, we're done. */
5545 if (cp_parser_parse_definitely (parser))
5546 return id;
5547 /* Otherwise, it's an ordinary identifier. */
5548 return cp_parser_identifier (parser);
5551 case CPP_TEMPLATE_ID:
5552 return cp_parser_template_id (parser, template_keyword_p,
5553 check_dependency_p,
5554 none_type,
5555 declarator_p);
5557 case CPP_COMPL:
5559 tree type_decl;
5560 tree qualifying_scope;
5561 tree object_scope;
5562 tree scope;
5563 bool done;
5565 /* Consume the `~' token. */
5566 cp_lexer_consume_token (parser->lexer);
5567 /* Parse the class-name. The standard, as written, seems to
5568 say that:
5570 template <typename T> struct S { ~S (); };
5571 template <typename T> S<T>::~S() {}
5573 is invalid, since `~' must be followed by a class-name, but
5574 `S<T>' is dependent, and so not known to be a class.
5575 That's not right; we need to look in uninstantiated
5576 templates. A further complication arises from:
5578 template <typename T> void f(T t) {
5579 t.T::~T();
5582 Here, it is not possible to look up `T' in the scope of `T'
5583 itself. We must look in both the current scope, and the
5584 scope of the containing complete expression.
5586 Yet another issue is:
5588 struct S {
5589 int S;
5590 ~S();
5593 S::~S() {}
5595 The standard does not seem to say that the `S' in `~S'
5596 should refer to the type `S' and not the data member
5597 `S::S'. */
5599 /* DR 244 says that we look up the name after the "~" in the
5600 same scope as we looked up the qualifying name. That idea
5601 isn't fully worked out; it's more complicated than that. */
5602 scope = parser->scope;
5603 object_scope = parser->object_scope;
5604 qualifying_scope = parser->qualifying_scope;
5606 /* Check for invalid scopes. */
5607 if (scope == error_mark_node)
5609 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5610 cp_lexer_consume_token (parser->lexer);
5611 return error_mark_node;
5613 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5615 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5616 error_at (token->location,
5617 "scope %qT before %<~%> is not a class-name",
5618 scope);
5619 cp_parser_simulate_error (parser);
5620 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5621 cp_lexer_consume_token (parser->lexer);
5622 return error_mark_node;
5624 gcc_assert (!scope || TYPE_P (scope));
5626 /* If the name is of the form "X::~X" it's OK even if X is a
5627 typedef. */
5628 token = cp_lexer_peek_token (parser->lexer);
5629 if (scope
5630 && token->type == CPP_NAME
5631 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5632 != CPP_LESS)
5633 && (token->u.value == TYPE_IDENTIFIER (scope)
5634 || (CLASS_TYPE_P (scope)
5635 && constructor_name_p (token->u.value, scope))))
5637 cp_lexer_consume_token (parser->lexer);
5638 return build_nt (BIT_NOT_EXPR, scope);
5641 /* ~auto means the destructor of whatever the object is. */
5642 if (cp_parser_is_keyword (token, RID_AUTO))
5644 if (cxx_dialect < cxx14)
5645 pedwarn (input_location, 0,
5646 "%<~auto%> only available with "
5647 "-std=c++14 or -std=gnu++14");
5648 cp_lexer_consume_token (parser->lexer);
5649 return build_nt (BIT_NOT_EXPR, make_auto ());
5652 /* If there was an explicit qualification (S::~T), first look
5653 in the scope given by the qualification (i.e., S).
5655 Note: in the calls to cp_parser_class_name below we pass
5656 typename_type so that lookup finds the injected-class-name
5657 rather than the constructor. */
5658 done = false;
5659 type_decl = NULL_TREE;
5660 if (scope)
5662 cp_parser_parse_tentatively (parser);
5663 type_decl = cp_parser_class_name (parser,
5664 /*typename_keyword_p=*/false,
5665 /*template_keyword_p=*/false,
5666 typename_type,
5667 /*check_dependency=*/false,
5668 /*class_head_p=*/false,
5669 declarator_p);
5670 if (cp_parser_parse_definitely (parser))
5671 done = true;
5673 /* In "N::S::~S", look in "N" as well. */
5674 if (!done && scope && qualifying_scope)
5676 cp_parser_parse_tentatively (parser);
5677 parser->scope = qualifying_scope;
5678 parser->object_scope = NULL_TREE;
5679 parser->qualifying_scope = NULL_TREE;
5680 type_decl
5681 = cp_parser_class_name (parser,
5682 /*typename_keyword_p=*/false,
5683 /*template_keyword_p=*/false,
5684 typename_type,
5685 /*check_dependency=*/false,
5686 /*class_head_p=*/false,
5687 declarator_p);
5688 if (cp_parser_parse_definitely (parser))
5689 done = true;
5691 /* In "p->S::~T", look in the scope given by "*p" as well. */
5692 else if (!done && object_scope)
5694 cp_parser_parse_tentatively (parser);
5695 parser->scope = object_scope;
5696 parser->object_scope = NULL_TREE;
5697 parser->qualifying_scope = NULL_TREE;
5698 type_decl
5699 = cp_parser_class_name (parser,
5700 /*typename_keyword_p=*/false,
5701 /*template_keyword_p=*/false,
5702 typename_type,
5703 /*check_dependency=*/false,
5704 /*class_head_p=*/false,
5705 declarator_p);
5706 if (cp_parser_parse_definitely (parser))
5707 done = true;
5709 /* Look in the surrounding context. */
5710 if (!done)
5712 parser->scope = NULL_TREE;
5713 parser->object_scope = NULL_TREE;
5714 parser->qualifying_scope = NULL_TREE;
5715 if (processing_template_decl)
5716 cp_parser_parse_tentatively (parser);
5717 type_decl
5718 = cp_parser_class_name (parser,
5719 /*typename_keyword_p=*/false,
5720 /*template_keyword_p=*/false,
5721 typename_type,
5722 /*check_dependency=*/false,
5723 /*class_head_p=*/false,
5724 declarator_p);
5725 if (processing_template_decl
5726 && ! cp_parser_parse_definitely (parser))
5728 /* We couldn't find a type with this name. If we're parsing
5729 tentatively, fail and try something else. */
5730 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5732 cp_parser_simulate_error (parser);
5733 return error_mark_node;
5735 /* Otherwise, accept it and check for a match at instantiation
5736 time. */
5737 type_decl = cp_parser_identifier (parser);
5738 if (type_decl != error_mark_node)
5739 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5740 return type_decl;
5743 /* If an error occurred, assume that the name of the
5744 destructor is the same as the name of the qualifying
5745 class. That allows us to keep parsing after running
5746 into ill-formed destructor names. */
5747 if (type_decl == error_mark_node && scope)
5748 return build_nt (BIT_NOT_EXPR, scope);
5749 else if (type_decl == error_mark_node)
5750 return error_mark_node;
5752 /* Check that destructor name and scope match. */
5753 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5755 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5756 error_at (token->location,
5757 "declaration of %<~%T%> as member of %qT",
5758 type_decl, scope);
5759 cp_parser_simulate_error (parser);
5760 return error_mark_node;
5763 /* [class.dtor]
5765 A typedef-name that names a class shall not be used as the
5766 identifier in the declarator for a destructor declaration. */
5767 if (declarator_p
5768 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5769 && !DECL_SELF_REFERENCE_P (type_decl)
5770 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5771 error_at (token->location,
5772 "typedef-name %qD used as destructor declarator",
5773 type_decl);
5775 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5778 case CPP_KEYWORD:
5779 if (token->keyword == RID_OPERATOR)
5781 cp_expr id;
5783 /* This could be a template-id, so we try that first. */
5784 cp_parser_parse_tentatively (parser);
5785 /* Try a template-id. */
5786 id = cp_parser_template_id (parser, template_keyword_p,
5787 /*check_dependency_p=*/true,
5788 none_type,
5789 declarator_p);
5790 /* If that worked, we're done. */
5791 if (cp_parser_parse_definitely (parser))
5792 return id;
5793 /* We still don't know whether we're looking at an
5794 operator-function-id or a conversion-function-id. */
5795 cp_parser_parse_tentatively (parser);
5796 /* Try an operator-function-id. */
5797 id = cp_parser_operator_function_id (parser);
5798 /* If that didn't work, try a conversion-function-id. */
5799 if (!cp_parser_parse_definitely (parser))
5800 id = cp_parser_conversion_function_id (parser);
5801 else if (UDLIT_OPER_P (id))
5803 /* 17.6.3.3.5 */
5804 const char *name = UDLIT_OP_SUFFIX (id);
5805 if (name[0] != '_' && !in_system_header_at (input_location)
5806 && declarator_p)
5807 warning (0, "literal operator suffixes not preceded by %<_%>"
5808 " are reserved for future standardization");
5811 return id;
5813 /* Fall through. */
5815 default:
5816 if (optional_p)
5817 return NULL_TREE;
5818 cp_parser_error (parser, "expected unqualified-id");
5819 return error_mark_node;
5823 /* Parse an (optional) nested-name-specifier.
5825 nested-name-specifier: [C++98]
5826 class-or-namespace-name :: nested-name-specifier [opt]
5827 class-or-namespace-name :: template nested-name-specifier [opt]
5829 nested-name-specifier: [C++0x]
5830 type-name ::
5831 namespace-name ::
5832 nested-name-specifier identifier ::
5833 nested-name-specifier template [opt] simple-template-id ::
5835 PARSER->SCOPE should be set appropriately before this function is
5836 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5837 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5838 in name lookups.
5840 Sets PARSER->SCOPE to the class (TYPE) or namespace
5841 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5842 it unchanged if there is no nested-name-specifier. Returns the new
5843 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5845 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5846 part of a declaration and/or decl-specifier. */
5848 static tree
5849 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5850 bool typename_keyword_p,
5851 bool check_dependency_p,
5852 bool type_p,
5853 bool is_declaration)
5855 bool success = false;
5856 cp_token_position start = 0;
5857 cp_token *token;
5859 /* Remember where the nested-name-specifier starts. */
5860 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5862 start = cp_lexer_token_position (parser->lexer, false);
5863 push_deferring_access_checks (dk_deferred);
5866 while (true)
5868 tree new_scope;
5869 tree old_scope;
5870 tree saved_qualifying_scope;
5871 bool template_keyword_p;
5873 /* Spot cases that cannot be the beginning of a
5874 nested-name-specifier. */
5875 token = cp_lexer_peek_token (parser->lexer);
5877 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5878 the already parsed nested-name-specifier. */
5879 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5881 /* Grab the nested-name-specifier and continue the loop. */
5882 cp_parser_pre_parsed_nested_name_specifier (parser);
5883 /* If we originally encountered this nested-name-specifier
5884 with IS_DECLARATION set to false, we will not have
5885 resolved TYPENAME_TYPEs, so we must do so here. */
5886 if (is_declaration
5887 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5889 new_scope = resolve_typename_type (parser->scope,
5890 /*only_current_p=*/false);
5891 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5892 parser->scope = new_scope;
5894 success = true;
5895 continue;
5898 /* Spot cases that cannot be the beginning of a
5899 nested-name-specifier. On the second and subsequent times
5900 through the loop, we look for the `template' keyword. */
5901 if (success && token->keyword == RID_TEMPLATE)
5903 /* A template-id can start a nested-name-specifier. */
5904 else if (token->type == CPP_TEMPLATE_ID)
5906 /* DR 743: decltype can be used in a nested-name-specifier. */
5907 else if (token_is_decltype (token))
5909 else
5911 /* If the next token is not an identifier, then it is
5912 definitely not a type-name or namespace-name. */
5913 if (token->type != CPP_NAME)
5914 break;
5915 /* If the following token is neither a `<' (to begin a
5916 template-id), nor a `::', then we are not looking at a
5917 nested-name-specifier. */
5918 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5920 if (token->type == CPP_COLON
5921 && parser->colon_corrects_to_scope_p
5922 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5924 error_at (token->location,
5925 "found %<:%> in nested-name-specifier, expected %<::%>");
5926 token->type = CPP_SCOPE;
5929 if (token->type != CPP_SCOPE
5930 && !cp_parser_nth_token_starts_template_argument_list_p
5931 (parser, 2))
5932 break;
5935 /* The nested-name-specifier is optional, so we parse
5936 tentatively. */
5937 cp_parser_parse_tentatively (parser);
5939 /* Look for the optional `template' keyword, if this isn't the
5940 first time through the loop. */
5941 if (success)
5942 template_keyword_p = cp_parser_optional_template_keyword (parser);
5943 else
5944 template_keyword_p = false;
5946 /* Save the old scope since the name lookup we are about to do
5947 might destroy it. */
5948 old_scope = parser->scope;
5949 saved_qualifying_scope = parser->qualifying_scope;
5950 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5951 look up names in "X<T>::I" in order to determine that "Y" is
5952 a template. So, if we have a typename at this point, we make
5953 an effort to look through it. */
5954 if (is_declaration
5955 && !typename_keyword_p
5956 && parser->scope
5957 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5958 parser->scope = resolve_typename_type (parser->scope,
5959 /*only_current_p=*/false);
5960 /* Parse the qualifying entity. */
5961 new_scope
5962 = cp_parser_qualifying_entity (parser,
5963 typename_keyword_p,
5964 template_keyword_p,
5965 check_dependency_p,
5966 type_p,
5967 is_declaration);
5968 /* Look for the `::' token. */
5969 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5971 /* If we found what we wanted, we keep going; otherwise, we're
5972 done. */
5973 if (!cp_parser_parse_definitely (parser))
5975 bool error_p = false;
5977 /* Restore the OLD_SCOPE since it was valid before the
5978 failed attempt at finding the last
5979 class-or-namespace-name. */
5980 parser->scope = old_scope;
5981 parser->qualifying_scope = saved_qualifying_scope;
5983 /* If the next token is a decltype, and the one after that is a
5984 `::', then the decltype has failed to resolve to a class or
5985 enumeration type. Give this error even when parsing
5986 tentatively since it can't possibly be valid--and we're going
5987 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5988 won't get another chance.*/
5989 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5990 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5991 == CPP_SCOPE))
5993 token = cp_lexer_consume_token (parser->lexer);
5994 error_at (token->location, "decltype evaluates to %qT, "
5995 "which is not a class or enumeration type",
5996 token->u.tree_check_value->value);
5997 parser->scope = error_mark_node;
5998 error_p = true;
5999 /* As below. */
6000 success = true;
6001 cp_lexer_consume_token (parser->lexer);
6004 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6005 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6007 /* If we have a non-type template-id followed by ::, it can't
6008 possibly be valid. */
6009 token = cp_lexer_peek_token (parser->lexer);
6010 tree tid = token->u.tree_check_value->value;
6011 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6012 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6014 tree tmpl = NULL_TREE;
6015 if (is_overloaded_fn (tid))
6017 tree fns = get_fns (tid);
6018 if (!OVL_CHAIN (fns))
6019 tmpl = OVL_CURRENT (fns);
6020 error_at (token->location, "function template-id %qD "
6021 "in nested-name-specifier", tid);
6023 else
6025 /* Variable template. */
6026 tmpl = TREE_OPERAND (tid, 0);
6027 gcc_assert (variable_template_p (tmpl));
6028 error_at (token->location, "variable template-id %qD "
6029 "in nested-name-specifier", tid);
6031 if (tmpl)
6032 inform (DECL_SOURCE_LOCATION (tmpl),
6033 "%qD declared here", tmpl);
6035 parser->scope = error_mark_node;
6036 error_p = true;
6037 /* As below. */
6038 success = true;
6039 cp_lexer_consume_token (parser->lexer);
6040 cp_lexer_consume_token (parser->lexer);
6044 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6045 break;
6046 /* If the next token is an identifier, and the one after
6047 that is a `::', then any valid interpretation would have
6048 found a class-or-namespace-name. */
6049 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6050 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6051 == CPP_SCOPE)
6052 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6053 != CPP_COMPL))
6055 token = cp_lexer_consume_token (parser->lexer);
6056 if (!error_p)
6058 if (!token->error_reported)
6060 tree decl;
6061 tree ambiguous_decls;
6063 decl = cp_parser_lookup_name (parser, token->u.value,
6064 none_type,
6065 /*is_template=*/false,
6066 /*is_namespace=*/false,
6067 /*check_dependency=*/true,
6068 &ambiguous_decls,
6069 token->location);
6070 if (TREE_CODE (decl) == TEMPLATE_DECL)
6071 error_at (token->location,
6072 "%qD used without template parameters",
6073 decl);
6074 else if (ambiguous_decls)
6076 // cp_parser_lookup_name has the same diagnostic,
6077 // thus make sure to emit it at most once.
6078 if (cp_parser_uncommitted_to_tentative_parse_p
6079 (parser))
6081 error_at (token->location,
6082 "reference to %qD is ambiguous",
6083 token->u.value);
6084 print_candidates (ambiguous_decls);
6086 decl = error_mark_node;
6088 else
6090 if (cxx_dialect != cxx98)
6091 cp_parser_name_lookup_error
6092 (parser, token->u.value, decl, NLE_NOT_CXX98,
6093 token->location);
6094 else
6095 cp_parser_name_lookup_error
6096 (parser, token->u.value, decl, NLE_CXX98,
6097 token->location);
6100 parser->scope = error_mark_node;
6101 error_p = true;
6102 /* Treat this as a successful nested-name-specifier
6103 due to:
6105 [basic.lookup.qual]
6107 If the name found is not a class-name (clause
6108 _class_) or namespace-name (_namespace.def_), the
6109 program is ill-formed. */
6110 success = true;
6112 cp_lexer_consume_token (parser->lexer);
6114 break;
6116 /* We've found one valid nested-name-specifier. */
6117 success = true;
6118 /* Name lookup always gives us a DECL. */
6119 if (TREE_CODE (new_scope) == TYPE_DECL)
6120 new_scope = TREE_TYPE (new_scope);
6121 /* Uses of "template" must be followed by actual templates. */
6122 if (template_keyword_p
6123 && !(CLASS_TYPE_P (new_scope)
6124 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6125 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6126 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6127 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6128 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6129 == TEMPLATE_ID_EXPR)))
6130 permerror (input_location, TYPE_P (new_scope)
6131 ? G_("%qT is not a template")
6132 : G_("%qD is not a template"),
6133 new_scope);
6134 /* If it is a class scope, try to complete it; we are about to
6135 be looking up names inside the class. */
6136 if (TYPE_P (new_scope)
6137 /* Since checking types for dependency can be expensive,
6138 avoid doing it if the type is already complete. */
6139 && !COMPLETE_TYPE_P (new_scope)
6140 /* Do not try to complete dependent types. */
6141 && !dependent_type_p (new_scope))
6143 new_scope = complete_type (new_scope);
6144 /* If it is a typedef to current class, use the current
6145 class instead, as the typedef won't have any names inside
6146 it yet. */
6147 if (!COMPLETE_TYPE_P (new_scope)
6148 && currently_open_class (new_scope))
6149 new_scope = TYPE_MAIN_VARIANT (new_scope);
6151 /* Make sure we look in the right scope the next time through
6152 the loop. */
6153 parser->scope = new_scope;
6156 /* If parsing tentatively, replace the sequence of tokens that makes
6157 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6158 token. That way, should we re-parse the token stream, we will
6159 not have to repeat the effort required to do the parse, nor will
6160 we issue duplicate error messages. */
6161 if (success && start)
6163 cp_token *token;
6165 token = cp_lexer_token_at (parser->lexer, start);
6166 /* Reset the contents of the START token. */
6167 token->type = CPP_NESTED_NAME_SPECIFIER;
6168 /* Retrieve any deferred checks. Do not pop this access checks yet
6169 so the memory will not be reclaimed during token replacing below. */
6170 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6171 token->u.tree_check_value->value = parser->scope;
6172 token->u.tree_check_value->checks = get_deferred_access_checks ();
6173 token->u.tree_check_value->qualifying_scope =
6174 parser->qualifying_scope;
6175 token->keyword = RID_MAX;
6177 /* Purge all subsequent tokens. */
6178 cp_lexer_purge_tokens_after (parser->lexer, start);
6181 if (start)
6182 pop_to_parent_deferring_access_checks ();
6184 return success ? parser->scope : NULL_TREE;
6187 /* Parse a nested-name-specifier. See
6188 cp_parser_nested_name_specifier_opt for details. This function
6189 behaves identically, except that it will an issue an error if no
6190 nested-name-specifier is present. */
6192 static tree
6193 cp_parser_nested_name_specifier (cp_parser *parser,
6194 bool typename_keyword_p,
6195 bool check_dependency_p,
6196 bool type_p,
6197 bool is_declaration)
6199 tree scope;
6201 /* Look for the nested-name-specifier. */
6202 scope = cp_parser_nested_name_specifier_opt (parser,
6203 typename_keyword_p,
6204 check_dependency_p,
6205 type_p,
6206 is_declaration);
6207 /* If it was not present, issue an error message. */
6208 if (!scope)
6210 cp_parser_error (parser, "expected nested-name-specifier");
6211 parser->scope = NULL_TREE;
6214 return scope;
6217 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6218 this is either a class-name or a namespace-name (which corresponds
6219 to the class-or-namespace-name production in the grammar). For
6220 C++0x, it can also be a type-name that refers to an enumeration
6221 type or a simple-template-id.
6223 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6224 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6225 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6226 TYPE_P is TRUE iff the next name should be taken as a class-name,
6227 even the same name is declared to be another entity in the same
6228 scope.
6230 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6231 specified by the class-or-namespace-name. If neither is found the
6232 ERROR_MARK_NODE is returned. */
6234 static tree
6235 cp_parser_qualifying_entity (cp_parser *parser,
6236 bool typename_keyword_p,
6237 bool template_keyword_p,
6238 bool check_dependency_p,
6239 bool type_p,
6240 bool is_declaration)
6242 tree saved_scope;
6243 tree saved_qualifying_scope;
6244 tree saved_object_scope;
6245 tree scope;
6246 bool only_class_p;
6247 bool successful_parse_p;
6249 /* DR 743: decltype can appear in a nested-name-specifier. */
6250 if (cp_lexer_next_token_is_decltype (parser->lexer))
6252 scope = cp_parser_decltype (parser);
6253 if (TREE_CODE (scope) != ENUMERAL_TYPE
6254 && !MAYBE_CLASS_TYPE_P (scope))
6256 cp_parser_simulate_error (parser);
6257 return error_mark_node;
6259 if (TYPE_NAME (scope))
6260 scope = TYPE_NAME (scope);
6261 return scope;
6264 /* Before we try to parse the class-name, we must save away the
6265 current PARSER->SCOPE since cp_parser_class_name will destroy
6266 it. */
6267 saved_scope = parser->scope;
6268 saved_qualifying_scope = parser->qualifying_scope;
6269 saved_object_scope = parser->object_scope;
6270 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6271 there is no need to look for a namespace-name. */
6272 only_class_p = template_keyword_p
6273 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6274 if (!only_class_p)
6275 cp_parser_parse_tentatively (parser);
6276 scope = cp_parser_class_name (parser,
6277 typename_keyword_p,
6278 template_keyword_p,
6279 type_p ? class_type : none_type,
6280 check_dependency_p,
6281 /*class_head_p=*/false,
6282 is_declaration,
6283 /*enum_ok=*/cxx_dialect > cxx98);
6284 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6285 /* If that didn't work, try for a namespace-name. */
6286 if (!only_class_p && !successful_parse_p)
6288 /* Restore the saved scope. */
6289 parser->scope = saved_scope;
6290 parser->qualifying_scope = saved_qualifying_scope;
6291 parser->object_scope = saved_object_scope;
6292 /* If we are not looking at an identifier followed by the scope
6293 resolution operator, then this is not part of a
6294 nested-name-specifier. (Note that this function is only used
6295 to parse the components of a nested-name-specifier.) */
6296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6297 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6298 return error_mark_node;
6299 scope = cp_parser_namespace_name (parser);
6302 return scope;
6305 /* Return true if we are looking at a compound-literal, false otherwise. */
6307 static bool
6308 cp_parser_compound_literal_p (cp_parser *parser)
6310 /* Consume the `('. */
6311 cp_lexer_consume_token (parser->lexer);
6313 cp_lexer_save_tokens (parser->lexer);
6315 /* Skip tokens until the next token is a closing parenthesis.
6316 If we find the closing `)', and the next token is a `{', then
6317 we are looking at a compound-literal. */
6318 bool compound_literal_p
6319 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6320 /*consume_paren=*/true)
6321 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6323 /* Roll back the tokens we skipped. */
6324 cp_lexer_rollback_tokens (parser->lexer);
6326 return compound_literal_p;
6329 /* Parse a postfix-expression.
6331 postfix-expression:
6332 primary-expression
6333 postfix-expression [ expression ]
6334 postfix-expression ( expression-list [opt] )
6335 simple-type-specifier ( expression-list [opt] )
6336 typename :: [opt] nested-name-specifier identifier
6337 ( expression-list [opt] )
6338 typename :: [opt] nested-name-specifier template [opt] template-id
6339 ( expression-list [opt] )
6340 postfix-expression . template [opt] id-expression
6341 postfix-expression -> template [opt] id-expression
6342 postfix-expression . pseudo-destructor-name
6343 postfix-expression -> pseudo-destructor-name
6344 postfix-expression ++
6345 postfix-expression --
6346 dynamic_cast < type-id > ( expression )
6347 static_cast < type-id > ( expression )
6348 reinterpret_cast < type-id > ( expression )
6349 const_cast < type-id > ( expression )
6350 typeid ( expression )
6351 typeid ( type-id )
6353 GNU Extension:
6355 postfix-expression:
6356 ( type-id ) { initializer-list , [opt] }
6358 This extension is a GNU version of the C99 compound-literal
6359 construct. (The C99 grammar uses `type-name' instead of `type-id',
6360 but they are essentially the same concept.)
6362 If ADDRESS_P is true, the postfix expression is the operand of the
6363 `&' operator. CAST_P is true if this expression is the target of a
6364 cast.
6366 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6367 class member access expressions [expr.ref].
6369 Returns a representation of the expression. */
6371 static cp_expr
6372 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6373 bool member_access_only_p, bool decltype_p,
6374 cp_id_kind * pidk_return)
6376 cp_token *token;
6377 location_t loc;
6378 enum rid keyword;
6379 cp_id_kind idk = CP_ID_KIND_NONE;
6380 cp_expr postfix_expression = NULL_TREE;
6381 bool is_member_access = false;
6382 int saved_in_statement = -1;
6384 /* Peek at the next token. */
6385 token = cp_lexer_peek_token (parser->lexer);
6386 loc = token->location;
6387 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6389 /* Some of the productions are determined by keywords. */
6390 keyword = token->keyword;
6391 switch (keyword)
6393 case RID_DYNCAST:
6394 case RID_STATCAST:
6395 case RID_REINTCAST:
6396 case RID_CONSTCAST:
6398 tree type;
6399 cp_expr expression;
6400 const char *saved_message;
6401 bool saved_in_type_id_in_expr_p;
6403 /* All of these can be handled in the same way from the point
6404 of view of parsing. Begin by consuming the token
6405 identifying the cast. */
6406 cp_lexer_consume_token (parser->lexer);
6408 /* New types cannot be defined in the cast. */
6409 saved_message = parser->type_definition_forbidden_message;
6410 parser->type_definition_forbidden_message
6411 = G_("types may not be defined in casts");
6413 /* Look for the opening `<'. */
6414 cp_parser_require (parser, CPP_LESS, RT_LESS);
6415 /* Parse the type to which we are casting. */
6416 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6417 parser->in_type_id_in_expr_p = true;
6418 type = cp_parser_type_id (parser);
6419 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6420 /* Look for the closing `>'. */
6421 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6422 /* Restore the old message. */
6423 parser->type_definition_forbidden_message = saved_message;
6425 bool saved_greater_than_is_operator_p
6426 = parser->greater_than_is_operator_p;
6427 parser->greater_than_is_operator_p = true;
6429 /* And the expression which is being cast. */
6430 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6431 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6432 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6433 RT_CLOSE_PAREN);
6434 location_t end_loc = close_paren ?
6435 close_paren->location : UNKNOWN_LOCATION;
6437 parser->greater_than_is_operator_p
6438 = saved_greater_than_is_operator_p;
6440 /* Only type conversions to integral or enumeration types
6441 can be used in constant-expressions. */
6442 if (!cast_valid_in_integral_constant_expression_p (type)
6443 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6444 return error_mark_node;
6446 switch (keyword)
6448 case RID_DYNCAST:
6449 postfix_expression
6450 = build_dynamic_cast (type, expression, tf_warning_or_error);
6451 break;
6452 case RID_STATCAST:
6453 postfix_expression
6454 = build_static_cast (type, expression, tf_warning_or_error);
6455 break;
6456 case RID_REINTCAST:
6457 postfix_expression
6458 = build_reinterpret_cast (type, expression,
6459 tf_warning_or_error);
6460 break;
6461 case RID_CONSTCAST:
6462 postfix_expression
6463 = build_const_cast (type, expression, tf_warning_or_error);
6464 break;
6465 default:
6466 gcc_unreachable ();
6469 /* Construct a location e.g. :
6470 reinterpret_cast <int *> (expr)
6471 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6472 ranging from the start of the "*_cast" token to the final closing
6473 paren, with the caret at the start. */
6474 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6475 postfix_expression.set_location (cp_cast_loc);
6477 break;
6479 case RID_TYPEID:
6481 tree type;
6482 const char *saved_message;
6483 bool saved_in_type_id_in_expr_p;
6485 /* Consume the `typeid' token. */
6486 cp_lexer_consume_token (parser->lexer);
6487 /* Look for the `(' token. */
6488 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6489 /* Types cannot be defined in a `typeid' expression. */
6490 saved_message = parser->type_definition_forbidden_message;
6491 parser->type_definition_forbidden_message
6492 = G_("types may not be defined in a %<typeid%> expression");
6493 /* We can't be sure yet whether we're looking at a type-id or an
6494 expression. */
6495 cp_parser_parse_tentatively (parser);
6496 /* Try a type-id first. */
6497 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6498 parser->in_type_id_in_expr_p = true;
6499 type = cp_parser_type_id (parser);
6500 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6501 /* Look for the `)' token. Otherwise, we can't be sure that
6502 we're not looking at an expression: consider `typeid (int
6503 (3))', for example. */
6504 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6505 /* If all went well, simply lookup the type-id. */
6506 if (cp_parser_parse_definitely (parser))
6507 postfix_expression = get_typeid (type, tf_warning_or_error);
6508 /* Otherwise, fall back to the expression variant. */
6509 else
6511 tree expression;
6513 /* Look for an expression. */
6514 expression = cp_parser_expression (parser, & idk);
6515 /* Compute its typeid. */
6516 postfix_expression = build_typeid (expression, tf_warning_or_error);
6517 /* Look for the `)' token. */
6518 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6520 /* Restore the saved message. */
6521 parser->type_definition_forbidden_message = saved_message;
6522 /* `typeid' may not appear in an integral constant expression. */
6523 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6524 return error_mark_node;
6526 break;
6528 case RID_TYPENAME:
6530 tree type;
6531 /* The syntax permitted here is the same permitted for an
6532 elaborated-type-specifier. */
6533 ++parser->prevent_constrained_type_specifiers;
6534 type = cp_parser_elaborated_type_specifier (parser,
6535 /*is_friend=*/false,
6536 /*is_declaration=*/false);
6537 --parser->prevent_constrained_type_specifiers;
6538 postfix_expression = cp_parser_functional_cast (parser, type);
6540 break;
6542 case RID_CILK_SPAWN:
6544 location_t cilk_spawn_loc
6545 = cp_lexer_peek_token (parser->lexer)->location;
6546 cp_lexer_consume_token (parser->lexer);
6547 token = cp_lexer_peek_token (parser->lexer);
6548 if (token->type == CPP_SEMICOLON)
6550 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6551 "an expression");
6552 postfix_expression = error_mark_node;
6553 break;
6555 else if (!current_function_decl)
6557 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6558 "inside a function");
6559 postfix_expression = error_mark_node;
6560 break;
6562 else
6564 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6565 saved_in_statement = parser->in_statement;
6566 parser->in_statement |= IN_CILK_SPAWN;
6568 cfun->calls_cilk_spawn = 1;
6569 postfix_expression =
6570 cp_parser_postfix_expression (parser, false, false,
6571 false, false, &idk);
6572 if (!flag_cilkplus)
6574 error_at (token->location, "-fcilkplus must be enabled to use"
6575 " %<_Cilk_spawn%>");
6576 cfun->calls_cilk_spawn = 0;
6578 else if (saved_in_statement & IN_CILK_SPAWN)
6580 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6581 "are not permitted");
6582 postfix_expression = error_mark_node;
6583 cfun->calls_cilk_spawn = 0;
6585 else
6587 location_t loc = postfix_expression.get_location ();
6588 postfix_expression = build_cilk_spawn (token->location,
6589 postfix_expression);
6590 /* Build a location of the form:
6591 _Cilk_spawn expr
6592 ~~~~~~~~~~~~^~~~
6593 with caret at the expr, ranging from the start of the
6594 _Cilk_spawn token to the end of the expression. */
6595 location_t combined_loc =
6596 make_location (loc, cilk_spawn_loc, get_finish (loc));
6597 postfix_expression.set_location (combined_loc);
6598 if (postfix_expression != error_mark_node)
6599 SET_EXPR_LOCATION (postfix_expression, input_location);
6600 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6602 break;
6605 case RID_BUILTIN_SHUFFLE:
6607 vec<tree, va_gc> *vec;
6608 unsigned int i;
6609 tree p;
6611 cp_lexer_consume_token (parser->lexer);
6612 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6613 /*cast_p=*/false, /*allow_expansion_p=*/true,
6614 /*non_constant_p=*/NULL);
6615 if (vec == NULL)
6616 return error_mark_node;
6618 FOR_EACH_VEC_ELT (*vec, i, p)
6619 mark_exp_read (p);
6621 if (vec->length () == 2)
6622 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6623 tf_warning_or_error);
6624 else if (vec->length () == 3)
6625 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6626 tf_warning_or_error);
6627 else
6629 error_at (loc, "wrong number of arguments to "
6630 "%<__builtin_shuffle%>");
6631 return error_mark_node;
6633 break;
6636 default:
6638 tree type;
6640 /* If the next thing is a simple-type-specifier, we may be
6641 looking at a functional cast. We could also be looking at
6642 an id-expression. So, we try the functional cast, and if
6643 that doesn't work we fall back to the primary-expression. */
6644 cp_parser_parse_tentatively (parser);
6645 /* Look for the simple-type-specifier. */
6646 ++parser->prevent_constrained_type_specifiers;
6647 type = cp_parser_simple_type_specifier (parser,
6648 /*decl_specs=*/NULL,
6649 CP_PARSER_FLAGS_NONE);
6650 --parser->prevent_constrained_type_specifiers;
6651 /* Parse the cast itself. */
6652 if (!cp_parser_error_occurred (parser))
6653 postfix_expression
6654 = cp_parser_functional_cast (parser, type);
6655 /* If that worked, we're done. */
6656 if (cp_parser_parse_definitely (parser))
6657 break;
6659 /* If the functional-cast didn't work out, try a
6660 compound-literal. */
6661 if (cp_parser_allow_gnu_extensions_p (parser)
6662 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6664 cp_expr initializer = NULL_TREE;
6666 cp_parser_parse_tentatively (parser);
6668 /* Avoid calling cp_parser_type_id pointlessly, see comment
6669 in cp_parser_cast_expression about c++/29234. */
6670 if (!cp_parser_compound_literal_p (parser))
6671 cp_parser_simulate_error (parser);
6672 else
6674 /* Parse the type. */
6675 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6676 parser->in_type_id_in_expr_p = true;
6677 type = cp_parser_type_id (parser);
6678 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6679 /* Look for the `)'. */
6680 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6683 /* If things aren't going well, there's no need to
6684 keep going. */
6685 if (!cp_parser_error_occurred (parser))
6687 bool non_constant_p;
6688 /* Parse the brace-enclosed initializer list. */
6689 initializer = cp_parser_braced_list (parser,
6690 &non_constant_p);
6692 /* If that worked, we're definitely looking at a
6693 compound-literal expression. */
6694 if (cp_parser_parse_definitely (parser))
6696 /* Warn the user that a compound literal is not
6697 allowed in standard C++. */
6698 pedwarn (input_location, OPT_Wpedantic,
6699 "ISO C++ forbids compound-literals");
6700 /* For simplicity, we disallow compound literals in
6701 constant-expressions. We could
6702 allow compound literals of integer type, whose
6703 initializer was a constant, in constant
6704 expressions. Permitting that usage, as a further
6705 extension, would not change the meaning of any
6706 currently accepted programs. (Of course, as
6707 compound literals are not part of ISO C++, the
6708 standard has nothing to say.) */
6709 if (cp_parser_non_integral_constant_expression (parser,
6710 NIC_NCC))
6712 postfix_expression = error_mark_node;
6713 break;
6715 /* Form the representation of the compound-literal. */
6716 postfix_expression
6717 = finish_compound_literal (type, initializer,
6718 tf_warning_or_error);
6719 postfix_expression.set_location (initializer.get_location ());
6720 break;
6724 /* It must be a primary-expression. */
6725 postfix_expression
6726 = cp_parser_primary_expression (parser, address_p, cast_p,
6727 /*template_arg_p=*/false,
6728 decltype_p,
6729 &idk);
6731 break;
6734 /* Note that we don't need to worry about calling build_cplus_new on a
6735 class-valued CALL_EXPR in decltype when it isn't the end of the
6736 postfix-expression; unary_complex_lvalue will take care of that for
6737 all these cases. */
6739 /* Keep looping until the postfix-expression is complete. */
6740 while (true)
6742 if (idk == CP_ID_KIND_UNQUALIFIED
6743 && identifier_p (postfix_expression)
6744 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6745 /* It is not a Koenig lookup function call. */
6746 postfix_expression
6747 = unqualified_name_lookup_error (postfix_expression);
6749 /* Peek at the next token. */
6750 token = cp_lexer_peek_token (parser->lexer);
6752 switch (token->type)
6754 case CPP_OPEN_SQUARE:
6755 if (cp_next_tokens_can_be_std_attribute_p (parser))
6757 cp_parser_error (parser,
6758 "two consecutive %<[%> shall "
6759 "only introduce an attribute");
6760 return error_mark_node;
6762 postfix_expression
6763 = cp_parser_postfix_open_square_expression (parser,
6764 postfix_expression,
6765 false,
6766 decltype_p);
6767 postfix_expression.set_range (start_loc,
6768 postfix_expression.get_location ());
6770 idk = CP_ID_KIND_NONE;
6771 is_member_access = false;
6772 break;
6774 case CPP_OPEN_PAREN:
6775 /* postfix-expression ( expression-list [opt] ) */
6777 bool koenig_p;
6778 bool is_builtin_constant_p;
6779 bool saved_integral_constant_expression_p = false;
6780 bool saved_non_integral_constant_expression_p = false;
6781 tsubst_flags_t complain = complain_flags (decltype_p);
6782 vec<tree, va_gc> *args;
6783 location_t close_paren_loc = UNKNOWN_LOCATION;
6785 is_member_access = false;
6787 is_builtin_constant_p
6788 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6789 if (is_builtin_constant_p)
6791 /* The whole point of __builtin_constant_p is to allow
6792 non-constant expressions to appear as arguments. */
6793 saved_integral_constant_expression_p
6794 = parser->integral_constant_expression_p;
6795 saved_non_integral_constant_expression_p
6796 = parser->non_integral_constant_expression_p;
6797 parser->integral_constant_expression_p = false;
6799 args = (cp_parser_parenthesized_expression_list
6800 (parser, non_attr,
6801 /*cast_p=*/false, /*allow_expansion_p=*/true,
6802 /*non_constant_p=*/NULL,
6803 /*close_paren_loc=*/&close_paren_loc));
6804 if (is_builtin_constant_p)
6806 parser->integral_constant_expression_p
6807 = saved_integral_constant_expression_p;
6808 parser->non_integral_constant_expression_p
6809 = saved_non_integral_constant_expression_p;
6812 if (args == NULL)
6814 postfix_expression = error_mark_node;
6815 break;
6818 /* Function calls are not permitted in
6819 constant-expressions. */
6820 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6821 && cp_parser_non_integral_constant_expression (parser,
6822 NIC_FUNC_CALL))
6824 postfix_expression = error_mark_node;
6825 release_tree_vector (args);
6826 break;
6829 koenig_p = false;
6830 if (idk == CP_ID_KIND_UNQUALIFIED
6831 || idk == CP_ID_KIND_TEMPLATE_ID)
6833 if (identifier_p (postfix_expression))
6835 if (!args->is_empty ())
6837 koenig_p = true;
6838 if (!any_type_dependent_arguments_p (args))
6839 postfix_expression
6840 = perform_koenig_lookup (postfix_expression, args,
6841 complain);
6843 else
6844 postfix_expression
6845 = unqualified_fn_lookup_error (postfix_expression);
6847 /* We do not perform argument-dependent lookup if
6848 normal lookup finds a non-function, in accordance
6849 with the expected resolution of DR 218. */
6850 else if (!args->is_empty ()
6851 && is_overloaded_fn (postfix_expression))
6853 tree fn = get_first_fn (postfix_expression);
6854 fn = STRIP_TEMPLATE (fn);
6856 /* Do not do argument dependent lookup if regular
6857 lookup finds a member function or a block-scope
6858 function declaration. [basic.lookup.argdep]/3 */
6859 if (!DECL_FUNCTION_MEMBER_P (fn)
6860 && !DECL_LOCAL_FUNCTION_P (fn))
6862 koenig_p = true;
6863 if (!any_type_dependent_arguments_p (args))
6864 postfix_expression
6865 = perform_koenig_lookup (postfix_expression, args,
6866 complain);
6871 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6872 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6873 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6874 && vec_safe_length (args) == 3)
6876 tree arg0 = (*args)[0];
6877 tree arg1 = (*args)[1];
6878 tree arg2 = (*args)[2];
6879 int literal_mask = ((!!integer_zerop (arg1) << 1)
6880 | (!!integer_zerop (arg2) << 2));
6881 if (TREE_CODE (arg2) == CONST_DECL)
6882 arg2 = DECL_INITIAL (arg2);
6883 warn_for_memset (input_location, arg0, arg2, literal_mask);
6886 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6888 tree instance = TREE_OPERAND (postfix_expression, 0);
6889 tree fn = TREE_OPERAND (postfix_expression, 1);
6891 if (processing_template_decl
6892 && (type_dependent_object_expression_p (instance)
6893 || (!BASELINK_P (fn)
6894 && TREE_CODE (fn) != FIELD_DECL)
6895 || type_dependent_expression_p (fn)
6896 || any_type_dependent_arguments_p (args)))
6898 postfix_expression
6899 = build_nt_call_vec (postfix_expression, args);
6900 release_tree_vector (args);
6901 break;
6904 if (BASELINK_P (fn))
6906 postfix_expression
6907 = (build_new_method_call
6908 (instance, fn, &args, NULL_TREE,
6909 (idk == CP_ID_KIND_QUALIFIED
6910 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6911 : LOOKUP_NORMAL),
6912 /*fn_p=*/NULL,
6913 complain));
6915 else
6916 postfix_expression
6917 = finish_call_expr (postfix_expression, &args,
6918 /*disallow_virtual=*/false,
6919 /*koenig_p=*/false,
6920 complain);
6922 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6923 || TREE_CODE (postfix_expression) == MEMBER_REF
6924 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6925 postfix_expression = (build_offset_ref_call_from_tree
6926 (postfix_expression, &args,
6927 complain));
6928 else if (idk == CP_ID_KIND_QUALIFIED)
6929 /* A call to a static class member, or a namespace-scope
6930 function. */
6931 postfix_expression
6932 = finish_call_expr (postfix_expression, &args,
6933 /*disallow_virtual=*/true,
6934 koenig_p,
6935 complain);
6936 else
6937 /* All other function calls. */
6938 postfix_expression
6939 = finish_call_expr (postfix_expression, &args,
6940 /*disallow_virtual=*/false,
6941 koenig_p,
6942 complain);
6944 if (close_paren_loc != UNKNOWN_LOCATION)
6946 location_t combined_loc = make_location (token->location,
6947 start_loc,
6948 close_paren_loc);
6949 postfix_expression.set_location (combined_loc);
6952 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6953 idk = CP_ID_KIND_NONE;
6955 release_tree_vector (args);
6957 break;
6959 case CPP_DOT:
6960 case CPP_DEREF:
6961 /* postfix-expression . template [opt] id-expression
6962 postfix-expression . pseudo-destructor-name
6963 postfix-expression -> template [opt] id-expression
6964 postfix-expression -> pseudo-destructor-name */
6966 /* Consume the `.' or `->' operator. */
6967 cp_lexer_consume_token (parser->lexer);
6969 postfix_expression
6970 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6971 postfix_expression,
6972 false, &idk, loc);
6974 is_member_access = true;
6975 break;
6977 case CPP_PLUS_PLUS:
6978 /* postfix-expression ++ */
6979 /* Consume the `++' token. */
6980 cp_lexer_consume_token (parser->lexer);
6981 /* Generate a representation for the complete expression. */
6982 postfix_expression
6983 = finish_increment_expr (postfix_expression,
6984 POSTINCREMENT_EXPR);
6985 /* Increments may not appear in constant-expressions. */
6986 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6987 postfix_expression = error_mark_node;
6988 idk = CP_ID_KIND_NONE;
6989 is_member_access = false;
6990 break;
6992 case CPP_MINUS_MINUS:
6993 /* postfix-expression -- */
6994 /* Consume the `--' token. */
6995 cp_lexer_consume_token (parser->lexer);
6996 /* Generate a representation for the complete expression. */
6997 postfix_expression
6998 = finish_increment_expr (postfix_expression,
6999 POSTDECREMENT_EXPR);
7000 /* Decrements may not appear in constant-expressions. */
7001 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7002 postfix_expression = error_mark_node;
7003 idk = CP_ID_KIND_NONE;
7004 is_member_access = false;
7005 break;
7007 default:
7008 if (pidk_return != NULL)
7009 * pidk_return = idk;
7010 if (member_access_only_p)
7011 return is_member_access
7012 ? postfix_expression
7013 : cp_expr (error_mark_node);
7014 else
7015 return postfix_expression;
7019 /* We should never get here. */
7020 gcc_unreachable ();
7021 return error_mark_node;
7024 /* This function parses Cilk Plus array notations. If a normal array expr. is
7025 parsed then the array index is passed back to the caller through *INIT_INDEX
7026 and the function returns a NULL_TREE. If array notation expr. is parsed,
7027 then *INIT_INDEX is ignored by the caller and the function returns
7028 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7029 error_mark_node. */
7031 static tree
7032 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7033 tree array_value)
7035 cp_token *token = NULL;
7036 tree length_index, stride = NULL_TREE, value_tree, array_type;
7037 if (!array_value || array_value == error_mark_node)
7039 cp_parser_skip_to_end_of_statement (parser);
7040 return error_mark_node;
7043 array_type = TREE_TYPE (array_value);
7045 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7046 parser->colon_corrects_to_scope_p = false;
7047 token = cp_lexer_peek_token (parser->lexer);
7049 if (!token)
7051 cp_parser_error (parser, "expected %<:%> or numeral");
7052 return error_mark_node;
7054 else if (token->type == CPP_COLON)
7056 /* Consume the ':'. */
7057 cp_lexer_consume_token (parser->lexer);
7059 /* If we are here, then we have a case like this A[:]. */
7060 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7062 cp_parser_error (parser, "expected %<]%>");
7063 cp_parser_skip_to_end_of_statement (parser);
7064 return error_mark_node;
7066 *init_index = NULL_TREE;
7067 stride = NULL_TREE;
7068 length_index = NULL_TREE;
7070 else
7072 /* If we are here, then there are three valid possibilities:
7073 1. ARRAY [ EXP ]
7074 2. ARRAY [ EXP : EXP ]
7075 3. ARRAY [ EXP : EXP : EXP ] */
7077 *init_index = cp_parser_expression (parser);
7078 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7080 /* This indicates that we have a normal array expression. */
7081 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7082 return NULL_TREE;
7085 /* Consume the ':'. */
7086 cp_lexer_consume_token (parser->lexer);
7087 length_index = cp_parser_expression (parser);
7088 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7090 cp_lexer_consume_token (parser->lexer);
7091 stride = cp_parser_expression (parser);
7094 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7096 if (*init_index == error_mark_node || length_index == error_mark_node
7097 || stride == error_mark_node || array_type == error_mark_node)
7099 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7100 cp_lexer_consume_token (parser->lexer);
7101 return error_mark_node;
7103 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7105 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7106 length_index, stride, array_type);
7107 return value_tree;
7110 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7111 by cp_parser_builtin_offsetof. We're looking for
7113 postfix-expression [ expression ]
7114 postfix-expression [ braced-init-list ] (C++11)
7116 FOR_OFFSETOF is set if we're being called in that context, which
7117 changes how we deal with integer constant expressions. */
7119 static tree
7120 cp_parser_postfix_open_square_expression (cp_parser *parser,
7121 tree postfix_expression,
7122 bool for_offsetof,
7123 bool decltype_p)
7125 tree index = NULL_TREE;
7126 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7127 bool saved_greater_than_is_operator_p;
7129 /* Consume the `[' token. */
7130 cp_lexer_consume_token (parser->lexer);
7132 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7133 parser->greater_than_is_operator_p = true;
7135 /* Parse the index expression. */
7136 /* ??? For offsetof, there is a question of what to allow here. If
7137 offsetof is not being used in an integral constant expression context,
7138 then we *could* get the right answer by computing the value at runtime.
7139 If we are in an integral constant expression context, then we might
7140 could accept any constant expression; hard to say without analysis.
7141 Rather than open the barn door too wide right away, allow only integer
7142 constant expressions here. */
7143 if (for_offsetof)
7144 index = cp_parser_constant_expression (parser);
7145 else
7147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7149 bool expr_nonconst_p;
7150 cp_lexer_set_source_position (parser->lexer);
7151 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7152 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7153 if (flag_cilkplus
7154 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7156 error_at (cp_lexer_peek_token (parser->lexer)->location,
7157 "braced list index is not allowed with array "
7158 "notation");
7159 cp_parser_skip_to_end_of_statement (parser);
7160 return error_mark_node;
7163 else if (flag_cilkplus)
7165 /* Here are have these two options:
7166 ARRAY[EXP : EXP] - Array notation expr with default
7167 stride of 1.
7168 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7169 stride. */
7170 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7171 postfix_expression);
7172 if (an_exp)
7173 return an_exp;
7175 else
7176 index = cp_parser_expression (parser);
7179 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7181 /* Look for the closing `]'. */
7182 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7184 /* Build the ARRAY_REF. */
7185 postfix_expression = grok_array_decl (loc, postfix_expression,
7186 index, decltype_p);
7188 /* When not doing offsetof, array references are not permitted in
7189 constant-expressions. */
7190 if (!for_offsetof
7191 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7192 postfix_expression = error_mark_node;
7194 return postfix_expression;
7197 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7198 by cp_parser_builtin_offsetof. We're looking for
7200 postfix-expression . template [opt] id-expression
7201 postfix-expression . pseudo-destructor-name
7202 postfix-expression -> template [opt] id-expression
7203 postfix-expression -> pseudo-destructor-name
7205 FOR_OFFSETOF is set if we're being called in that context. That sorta
7206 limits what of the above we'll actually accept, but nevermind.
7207 TOKEN_TYPE is the "." or "->" token, which will already have been
7208 removed from the stream. */
7210 static tree
7211 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7212 enum cpp_ttype token_type,
7213 cp_expr postfix_expression,
7214 bool for_offsetof, cp_id_kind *idk,
7215 location_t location)
7217 tree name;
7218 bool dependent_p;
7219 bool pseudo_destructor_p;
7220 tree scope = NULL_TREE;
7221 location_t start_loc = postfix_expression.get_start ();
7223 /* If this is a `->' operator, dereference the pointer. */
7224 if (token_type == CPP_DEREF)
7225 postfix_expression = build_x_arrow (location, postfix_expression,
7226 tf_warning_or_error);
7227 /* Check to see whether or not the expression is type-dependent and
7228 not the current instantiation. */
7229 dependent_p = type_dependent_object_expression_p (postfix_expression);
7230 /* The identifier following the `->' or `.' is not qualified. */
7231 parser->scope = NULL_TREE;
7232 parser->qualifying_scope = NULL_TREE;
7233 parser->object_scope = NULL_TREE;
7234 *idk = CP_ID_KIND_NONE;
7236 /* Enter the scope corresponding to the type of the object
7237 given by the POSTFIX_EXPRESSION. */
7238 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7240 scope = TREE_TYPE (postfix_expression);
7241 /* According to the standard, no expression should ever have
7242 reference type. Unfortunately, we do not currently match
7243 the standard in this respect in that our internal representation
7244 of an expression may have reference type even when the standard
7245 says it does not. Therefore, we have to manually obtain the
7246 underlying type here. */
7247 scope = non_reference (scope);
7248 /* The type of the POSTFIX_EXPRESSION must be complete. */
7249 /* Unlike the object expression in other contexts, *this is not
7250 required to be of complete type for purposes of class member
7251 access (5.2.5) outside the member function body. */
7252 if (postfix_expression != current_class_ref
7253 && !(processing_template_decl
7254 && current_class_type
7255 && (same_type_ignoring_top_level_qualifiers_p
7256 (scope, current_class_type))))
7257 scope = complete_type_or_else (scope, postfix_expression);
7258 /* Let the name lookup machinery know that we are processing a
7259 class member access expression. */
7260 parser->context->object_type = scope;
7261 /* If something went wrong, we want to be able to discern that case,
7262 as opposed to the case where there was no SCOPE due to the type
7263 of expression being dependent. */
7264 if (!scope)
7265 scope = error_mark_node;
7266 /* If the SCOPE was erroneous, make the various semantic analysis
7267 functions exit quickly -- and without issuing additional error
7268 messages. */
7269 if (scope == error_mark_node)
7270 postfix_expression = error_mark_node;
7272 else
7273 /* Tell cp_parser_lookup_name that there was an object, even though it's
7274 type-dependent. */
7275 parser->context->object_type = unknown_type_node;
7277 /* Assume this expression is not a pseudo-destructor access. */
7278 pseudo_destructor_p = false;
7280 /* If the SCOPE is a scalar type, then, if this is a valid program,
7281 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7282 is type dependent, it can be pseudo-destructor-name or something else.
7283 Try to parse it as pseudo-destructor-name first. */
7284 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7286 tree s;
7287 tree type;
7289 cp_parser_parse_tentatively (parser);
7290 /* Parse the pseudo-destructor-name. */
7291 s = NULL_TREE;
7292 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7293 &s, &type);
7294 if (dependent_p
7295 && (cp_parser_error_occurred (parser)
7296 || !SCALAR_TYPE_P (type)))
7297 cp_parser_abort_tentative_parse (parser);
7298 else if (cp_parser_parse_definitely (parser))
7300 pseudo_destructor_p = true;
7301 postfix_expression
7302 = finish_pseudo_destructor_expr (postfix_expression,
7303 s, type, location);
7307 if (!pseudo_destructor_p)
7309 /* If the SCOPE is not a scalar type, we are looking at an
7310 ordinary class member access expression, rather than a
7311 pseudo-destructor-name. */
7312 bool template_p;
7313 cp_token *token = cp_lexer_peek_token (parser->lexer);
7314 /* Parse the id-expression. */
7315 name = (cp_parser_id_expression
7316 (parser,
7317 cp_parser_optional_template_keyword (parser),
7318 /*check_dependency_p=*/true,
7319 &template_p,
7320 /*declarator_p=*/false,
7321 /*optional_p=*/false));
7322 /* In general, build a SCOPE_REF if the member name is qualified.
7323 However, if the name was not dependent and has already been
7324 resolved; there is no need to build the SCOPE_REF. For example;
7326 struct X { void f(); };
7327 template <typename T> void f(T* t) { t->X::f(); }
7329 Even though "t" is dependent, "X::f" is not and has been resolved
7330 to a BASELINK; there is no need to include scope information. */
7332 /* But we do need to remember that there was an explicit scope for
7333 virtual function calls. */
7334 if (parser->scope)
7335 *idk = CP_ID_KIND_QUALIFIED;
7337 /* If the name is a template-id that names a type, we will get a
7338 TYPE_DECL here. That is invalid code. */
7339 if (TREE_CODE (name) == TYPE_DECL)
7341 error_at (token->location, "invalid use of %qD", name);
7342 postfix_expression = error_mark_node;
7344 else
7346 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7348 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7350 error_at (token->location, "%<%D::%D%> is not a class member",
7351 parser->scope, name);
7352 postfix_expression = error_mark_node;
7354 else
7355 name = build_qualified_name (/*type=*/NULL_TREE,
7356 parser->scope,
7357 name,
7358 template_p);
7359 parser->scope = NULL_TREE;
7360 parser->qualifying_scope = NULL_TREE;
7361 parser->object_scope = NULL_TREE;
7363 if (parser->scope && name && BASELINK_P (name))
7364 adjust_result_of_qualified_name_lookup
7365 (name, parser->scope, scope);
7366 postfix_expression
7367 = finish_class_member_access_expr (postfix_expression, name,
7368 template_p,
7369 tf_warning_or_error);
7370 /* Build a location e.g.:
7371 ptr->access_expr
7372 ~~~^~~~~~~~~~~~~
7373 where the caret is at the deref token, ranging from
7374 the start of postfix_expression to the end of the access expr. */
7375 location_t end_loc
7376 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7377 location_t combined_loc
7378 = make_location (input_location, start_loc, end_loc);
7379 protected_set_expr_location (postfix_expression, combined_loc);
7383 /* We no longer need to look up names in the scope of the object on
7384 the left-hand side of the `.' or `->' operator. */
7385 parser->context->object_type = NULL_TREE;
7387 /* Outside of offsetof, these operators may not appear in
7388 constant-expressions. */
7389 if (!for_offsetof
7390 && (cp_parser_non_integral_constant_expression
7391 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7392 postfix_expression = error_mark_node;
7394 return postfix_expression;
7397 /* Parse a parenthesized expression-list.
7399 expression-list:
7400 assignment-expression
7401 expression-list, assignment-expression
7403 attribute-list:
7404 expression-list
7405 identifier
7406 identifier, expression-list
7408 CAST_P is true if this expression is the target of a cast.
7410 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7411 argument pack.
7413 Returns a vector of trees. Each element is a representation of an
7414 assignment-expression. NULL is returned if the ( and or ) are
7415 missing. An empty, but allocated, vector is returned on no
7416 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7417 if we are parsing an attribute list for an attribute that wants a
7418 plain identifier argument, normal_attr for an attribute that wants
7419 an expression, or non_attr if we aren't parsing an attribute list. If
7420 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7421 not all of the expressions in the list were constant.
7422 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7423 will be written to with the location of the closing parenthesis. If
7424 an error occurs, it may or may not be written to. */
7426 static vec<tree, va_gc> *
7427 cp_parser_parenthesized_expression_list (cp_parser* parser,
7428 int is_attribute_list,
7429 bool cast_p,
7430 bool allow_expansion_p,
7431 bool *non_constant_p,
7432 location_t *close_paren_loc)
7434 vec<tree, va_gc> *expression_list;
7435 bool fold_expr_p = is_attribute_list != non_attr;
7436 tree identifier = NULL_TREE;
7437 bool saved_greater_than_is_operator_p;
7439 /* Assume all the expressions will be constant. */
7440 if (non_constant_p)
7441 *non_constant_p = false;
7443 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7444 return NULL;
7446 expression_list = make_tree_vector ();
7448 /* Within a parenthesized expression, a `>' token is always
7449 the greater-than operator. */
7450 saved_greater_than_is_operator_p
7451 = parser->greater_than_is_operator_p;
7452 parser->greater_than_is_operator_p = true;
7454 /* Consume expressions until there are no more. */
7455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7456 while (true)
7458 tree expr;
7460 /* At the beginning of attribute lists, check to see if the
7461 next token is an identifier. */
7462 if (is_attribute_list == id_attr
7463 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7465 cp_token *token;
7467 /* Consume the identifier. */
7468 token = cp_lexer_consume_token (parser->lexer);
7469 /* Save the identifier. */
7470 identifier = token->u.value;
7472 else
7474 bool expr_non_constant_p;
7476 /* Parse the next assignment-expression. */
7477 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7479 /* A braced-init-list. */
7480 cp_lexer_set_source_position (parser->lexer);
7481 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7482 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7483 if (non_constant_p && expr_non_constant_p)
7484 *non_constant_p = true;
7486 else if (non_constant_p)
7488 expr = (cp_parser_constant_expression
7489 (parser, /*allow_non_constant_p=*/true,
7490 &expr_non_constant_p));
7491 if (expr_non_constant_p)
7492 *non_constant_p = true;
7494 else
7495 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7496 cast_p);
7498 if (fold_expr_p)
7499 expr = instantiate_non_dependent_expr (expr);
7501 /* If we have an ellipsis, then this is an expression
7502 expansion. */
7503 if (allow_expansion_p
7504 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7506 /* Consume the `...'. */
7507 cp_lexer_consume_token (parser->lexer);
7509 /* Build the argument pack. */
7510 expr = make_pack_expansion (expr);
7513 /* Add it to the list. We add error_mark_node
7514 expressions to the list, so that we can still tell if
7515 the correct form for a parenthesized expression-list
7516 is found. That gives better errors. */
7517 vec_safe_push (expression_list, expr);
7519 if (expr == error_mark_node)
7520 goto skip_comma;
7523 /* After the first item, attribute lists look the same as
7524 expression lists. */
7525 is_attribute_list = non_attr;
7527 get_comma:;
7528 /* If the next token isn't a `,', then we are done. */
7529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7530 break;
7532 /* Otherwise, consume the `,' and keep going. */
7533 cp_lexer_consume_token (parser->lexer);
7536 if (close_paren_loc)
7537 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7539 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7541 int ending;
7543 skip_comma:;
7544 /* We try and resync to an unnested comma, as that will give the
7545 user better diagnostics. */
7546 ending = cp_parser_skip_to_closing_parenthesis (parser,
7547 /*recovering=*/true,
7548 /*or_comma=*/true,
7549 /*consume_paren=*/true);
7550 if (ending < 0)
7551 goto get_comma;
7552 if (!ending)
7554 parser->greater_than_is_operator_p
7555 = saved_greater_than_is_operator_p;
7556 return NULL;
7560 parser->greater_than_is_operator_p
7561 = saved_greater_than_is_operator_p;
7563 if (identifier)
7564 vec_safe_insert (expression_list, 0, identifier);
7566 return expression_list;
7569 /* Parse a pseudo-destructor-name.
7571 pseudo-destructor-name:
7572 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7573 :: [opt] nested-name-specifier template template-id :: ~ type-name
7574 :: [opt] nested-name-specifier [opt] ~ type-name
7576 If either of the first two productions is used, sets *SCOPE to the
7577 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7578 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7579 or ERROR_MARK_NODE if the parse fails. */
7581 static void
7582 cp_parser_pseudo_destructor_name (cp_parser* parser,
7583 tree object,
7584 tree* scope,
7585 tree* type)
7587 bool nested_name_specifier_p;
7589 /* Handle ~auto. */
7590 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7591 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7592 && !type_dependent_expression_p (object))
7594 if (cxx_dialect < cxx14)
7595 pedwarn (input_location, 0,
7596 "%<~auto%> only available with "
7597 "-std=c++14 or -std=gnu++14");
7598 cp_lexer_consume_token (parser->lexer);
7599 cp_lexer_consume_token (parser->lexer);
7600 *scope = NULL_TREE;
7601 *type = TREE_TYPE (object);
7602 return;
7605 /* Assume that things will not work out. */
7606 *type = error_mark_node;
7608 /* Look for the optional `::' operator. */
7609 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7610 /* Look for the optional nested-name-specifier. */
7611 nested_name_specifier_p
7612 = (cp_parser_nested_name_specifier_opt (parser,
7613 /*typename_keyword_p=*/false,
7614 /*check_dependency_p=*/true,
7615 /*type_p=*/false,
7616 /*is_declaration=*/false)
7617 != NULL_TREE);
7618 /* Now, if we saw a nested-name-specifier, we might be doing the
7619 second production. */
7620 if (nested_name_specifier_p
7621 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7623 /* Consume the `template' keyword. */
7624 cp_lexer_consume_token (parser->lexer);
7625 /* Parse the template-id. */
7626 cp_parser_template_id (parser,
7627 /*template_keyword_p=*/true,
7628 /*check_dependency_p=*/false,
7629 class_type,
7630 /*is_declaration=*/true);
7631 /* Look for the `::' token. */
7632 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7634 /* If the next token is not a `~', then there might be some
7635 additional qualification. */
7636 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7638 /* At this point, we're looking for "type-name :: ~". The type-name
7639 must not be a class-name, since this is a pseudo-destructor. So,
7640 it must be either an enum-name, or a typedef-name -- both of which
7641 are just identifiers. So, we peek ahead to check that the "::"
7642 and "~" tokens are present; if they are not, then we can avoid
7643 calling type_name. */
7644 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7645 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7646 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7648 cp_parser_error (parser, "non-scalar type");
7649 return;
7652 /* Look for the type-name. */
7653 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7654 if (*scope == error_mark_node)
7655 return;
7657 /* Look for the `::' token. */
7658 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7660 else
7661 *scope = NULL_TREE;
7663 /* Look for the `~'. */
7664 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7666 /* Once we see the ~, this has to be a pseudo-destructor. */
7667 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7668 cp_parser_commit_to_topmost_tentative_parse (parser);
7670 /* Look for the type-name again. We are not responsible for
7671 checking that it matches the first type-name. */
7672 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7675 /* Parse a unary-expression.
7677 unary-expression:
7678 postfix-expression
7679 ++ cast-expression
7680 -- cast-expression
7681 unary-operator cast-expression
7682 sizeof unary-expression
7683 sizeof ( type-id )
7684 alignof ( type-id ) [C++0x]
7685 new-expression
7686 delete-expression
7688 GNU Extensions:
7690 unary-expression:
7691 __extension__ cast-expression
7692 __alignof__ unary-expression
7693 __alignof__ ( type-id )
7694 alignof unary-expression [C++0x]
7695 __real__ cast-expression
7696 __imag__ cast-expression
7697 && identifier
7698 sizeof ( type-id ) { initializer-list , [opt] }
7699 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7700 __alignof__ ( type-id ) { initializer-list , [opt] }
7702 ADDRESS_P is true iff the unary-expression is appearing as the
7703 operand of the `&' operator. CAST_P is true if this expression is
7704 the target of a cast.
7706 Returns a representation of the expression. */
7708 static cp_expr
7709 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7710 bool address_p, bool cast_p, bool decltype_p)
7712 cp_token *token;
7713 enum tree_code unary_operator;
7715 /* Peek at the next token. */
7716 token = cp_lexer_peek_token (parser->lexer);
7717 /* Some keywords give away the kind of expression. */
7718 if (token->type == CPP_KEYWORD)
7720 enum rid keyword = token->keyword;
7722 switch (keyword)
7724 case RID_ALIGNOF:
7725 case RID_SIZEOF:
7727 tree operand, ret;
7728 enum tree_code op;
7729 location_t first_loc;
7731 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7732 /* Consume the token. */
7733 cp_lexer_consume_token (parser->lexer);
7734 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7735 /* Parse the operand. */
7736 operand = cp_parser_sizeof_operand (parser, keyword);
7738 if (TYPE_P (operand))
7739 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7740 else
7742 /* ISO C++ defines alignof only with types, not with
7743 expressions. So pedwarn if alignof is used with a non-
7744 type expression. However, __alignof__ is ok. */
7745 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7746 pedwarn (token->location, OPT_Wpedantic,
7747 "ISO C++ does not allow %<alignof%> "
7748 "with a non-type");
7750 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7752 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7753 SIZEOF_EXPR with the original operand. */
7754 if (op == SIZEOF_EXPR && ret != error_mark_node)
7756 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7758 if (!processing_template_decl && TYPE_P (operand))
7760 ret = build_min (SIZEOF_EXPR, size_type_node,
7761 build1 (NOP_EXPR, operand,
7762 error_mark_node));
7763 SIZEOF_EXPR_TYPE_P (ret) = 1;
7765 else
7766 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7767 TREE_SIDE_EFFECTS (ret) = 0;
7768 TREE_READONLY (ret) = 1;
7770 SET_EXPR_LOCATION (ret, first_loc);
7772 return ret;
7775 case RID_NEW:
7776 return cp_parser_new_expression (parser);
7778 case RID_DELETE:
7779 return cp_parser_delete_expression (parser);
7781 case RID_EXTENSION:
7783 /* The saved value of the PEDANTIC flag. */
7784 int saved_pedantic;
7785 tree expr;
7787 /* Save away the PEDANTIC flag. */
7788 cp_parser_extension_opt (parser, &saved_pedantic);
7789 /* Parse the cast-expression. */
7790 expr = cp_parser_simple_cast_expression (parser);
7791 /* Restore the PEDANTIC flag. */
7792 pedantic = saved_pedantic;
7794 return expr;
7797 case RID_REALPART:
7798 case RID_IMAGPART:
7800 tree expression;
7802 /* Consume the `__real__' or `__imag__' token. */
7803 cp_lexer_consume_token (parser->lexer);
7804 /* Parse the cast-expression. */
7805 expression = cp_parser_simple_cast_expression (parser);
7806 /* Create the complete representation. */
7807 return build_x_unary_op (token->location,
7808 (keyword == RID_REALPART
7809 ? REALPART_EXPR : IMAGPART_EXPR),
7810 expression,
7811 tf_warning_or_error);
7813 break;
7815 case RID_TRANSACTION_ATOMIC:
7816 case RID_TRANSACTION_RELAXED:
7817 return cp_parser_transaction_expression (parser, keyword);
7819 case RID_NOEXCEPT:
7821 tree expr;
7822 const char *saved_message;
7823 bool saved_integral_constant_expression_p;
7824 bool saved_non_integral_constant_expression_p;
7825 bool saved_greater_than_is_operator_p;
7827 cp_lexer_consume_token (parser->lexer);
7828 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7830 saved_message = parser->type_definition_forbidden_message;
7831 parser->type_definition_forbidden_message
7832 = G_("types may not be defined in %<noexcept%> expressions");
7834 saved_integral_constant_expression_p
7835 = parser->integral_constant_expression_p;
7836 saved_non_integral_constant_expression_p
7837 = parser->non_integral_constant_expression_p;
7838 parser->integral_constant_expression_p = false;
7840 saved_greater_than_is_operator_p
7841 = parser->greater_than_is_operator_p;
7842 parser->greater_than_is_operator_p = true;
7844 ++cp_unevaluated_operand;
7845 ++c_inhibit_evaluation_warnings;
7846 ++cp_noexcept_operand;
7847 expr = cp_parser_expression (parser);
7848 --cp_noexcept_operand;
7849 --c_inhibit_evaluation_warnings;
7850 --cp_unevaluated_operand;
7852 parser->greater_than_is_operator_p
7853 = saved_greater_than_is_operator_p;
7855 parser->integral_constant_expression_p
7856 = saved_integral_constant_expression_p;
7857 parser->non_integral_constant_expression_p
7858 = saved_non_integral_constant_expression_p;
7860 parser->type_definition_forbidden_message = saved_message;
7862 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7863 return finish_noexcept_expr (expr, tf_warning_or_error);
7866 default:
7867 break;
7871 /* Look for the `:: new' and `:: delete', which also signal the
7872 beginning of a new-expression, or delete-expression,
7873 respectively. If the next token is `::', then it might be one of
7874 these. */
7875 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7877 enum rid keyword;
7879 /* See if the token after the `::' is one of the keywords in
7880 which we're interested. */
7881 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7882 /* If it's `new', we have a new-expression. */
7883 if (keyword == RID_NEW)
7884 return cp_parser_new_expression (parser);
7885 /* Similarly, for `delete'. */
7886 else if (keyword == RID_DELETE)
7887 return cp_parser_delete_expression (parser);
7890 /* Look for a unary operator. */
7891 unary_operator = cp_parser_unary_operator (token);
7892 /* The `++' and `--' operators can be handled similarly, even though
7893 they are not technically unary-operators in the grammar. */
7894 if (unary_operator == ERROR_MARK)
7896 if (token->type == CPP_PLUS_PLUS)
7897 unary_operator = PREINCREMENT_EXPR;
7898 else if (token->type == CPP_MINUS_MINUS)
7899 unary_operator = PREDECREMENT_EXPR;
7900 /* Handle the GNU address-of-label extension. */
7901 else if (cp_parser_allow_gnu_extensions_p (parser)
7902 && token->type == CPP_AND_AND)
7904 tree identifier;
7905 tree expression;
7906 location_t start_loc = token->location;
7908 /* Consume the '&&' token. */
7909 cp_lexer_consume_token (parser->lexer);
7910 /* Look for the identifier. */
7911 location_t finish_loc
7912 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7913 identifier = cp_parser_identifier (parser);
7914 /* Construct a location of the form:
7915 &&label
7916 ^~~~~~~
7917 with caret==start at the "&&", finish at the end of the label. */
7918 location_t combined_loc
7919 = make_location (start_loc, start_loc, finish_loc);
7920 /* Create an expression representing the address. */
7921 expression = finish_label_address_expr (identifier, combined_loc);
7922 if (cp_parser_non_integral_constant_expression (parser,
7923 NIC_ADDR_LABEL))
7924 expression = error_mark_node;
7925 return expression;
7928 if (unary_operator != ERROR_MARK)
7930 cp_expr cast_expression;
7931 cp_expr expression = error_mark_node;
7932 non_integral_constant non_constant_p = NIC_NONE;
7933 location_t loc = token->location;
7934 tsubst_flags_t complain = complain_flags (decltype_p);
7936 /* Consume the operator token. */
7937 token = cp_lexer_consume_token (parser->lexer);
7938 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7940 /* Parse the cast-expression. */
7941 cast_expression
7942 = cp_parser_cast_expression (parser,
7943 unary_operator == ADDR_EXPR,
7944 /*cast_p=*/false,
7945 /*decltype*/false,
7946 pidk);
7948 /* Make a location:
7949 OP_TOKEN CAST_EXPRESSION
7950 ^~~~~~~~~~~~~~~~~~~~~~~~~
7951 with start==caret at the operator token, and
7952 extending to the end of the cast_expression. */
7953 loc = make_location (loc, loc, cast_expression.get_finish ());
7955 /* Now, build an appropriate representation. */
7956 switch (unary_operator)
7958 case INDIRECT_REF:
7959 non_constant_p = NIC_STAR;
7960 expression = build_x_indirect_ref (loc, cast_expression,
7961 RO_UNARY_STAR,
7962 complain);
7963 /* TODO: build_x_indirect_ref does not always honor the
7964 location, so ensure it is set. */
7965 expression.set_location (loc);
7966 break;
7968 case ADDR_EXPR:
7969 non_constant_p = NIC_ADDR;
7970 /* Fall through. */
7971 case BIT_NOT_EXPR:
7972 expression = build_x_unary_op (loc, unary_operator,
7973 cast_expression,
7974 complain);
7975 /* TODO: build_x_unary_op does not always honor the location,
7976 so ensure it is set. */
7977 expression.set_location (loc);
7978 break;
7980 case PREINCREMENT_EXPR:
7981 case PREDECREMENT_EXPR:
7982 non_constant_p = unary_operator == PREINCREMENT_EXPR
7983 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7984 /* Fall through. */
7985 case NEGATE_EXPR:
7986 /* Immediately fold negation of a constant, unless the constant is 0
7987 (since -0 == 0) or it would overflow. */
7988 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7989 && CONSTANT_CLASS_P (cast_expression)
7990 && !integer_zerop (cast_expression)
7991 && !TREE_OVERFLOW (cast_expression))
7993 tree folded = fold_build1 (unary_operator,
7994 TREE_TYPE (cast_expression),
7995 cast_expression);
7996 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7998 expression = cp_expr (folded, loc);
7999 break;
8002 /* Fall through. */
8003 case UNARY_PLUS_EXPR:
8004 case TRUTH_NOT_EXPR:
8005 expression = finish_unary_op_expr (loc, unary_operator,
8006 cast_expression, complain);
8007 break;
8009 default:
8010 gcc_unreachable ();
8013 if (non_constant_p != NIC_NONE
8014 && cp_parser_non_integral_constant_expression (parser,
8015 non_constant_p))
8016 expression = error_mark_node;
8018 return expression;
8021 return cp_parser_postfix_expression (parser, address_p, cast_p,
8022 /*member_access_only_p=*/false,
8023 decltype_p,
8024 pidk);
8027 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8028 unary-operator, the corresponding tree code is returned. */
8030 static enum tree_code
8031 cp_parser_unary_operator (cp_token* token)
8033 switch (token->type)
8035 case CPP_MULT:
8036 return INDIRECT_REF;
8038 case CPP_AND:
8039 return ADDR_EXPR;
8041 case CPP_PLUS:
8042 return UNARY_PLUS_EXPR;
8044 case CPP_MINUS:
8045 return NEGATE_EXPR;
8047 case CPP_NOT:
8048 return TRUTH_NOT_EXPR;
8050 case CPP_COMPL:
8051 return BIT_NOT_EXPR;
8053 default:
8054 return ERROR_MARK;
8058 /* Parse a new-expression.
8060 new-expression:
8061 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8062 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8064 Returns a representation of the expression. */
8066 static tree
8067 cp_parser_new_expression (cp_parser* parser)
8069 bool global_scope_p;
8070 vec<tree, va_gc> *placement;
8071 tree type;
8072 vec<tree, va_gc> *initializer;
8073 tree nelts = NULL_TREE;
8074 tree ret;
8076 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8078 /* Look for the optional `::' operator. */
8079 global_scope_p
8080 = (cp_parser_global_scope_opt (parser,
8081 /*current_scope_valid_p=*/false)
8082 != NULL_TREE);
8083 /* Look for the `new' operator. */
8084 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8085 /* There's no easy way to tell a new-placement from the
8086 `( type-id )' construct. */
8087 cp_parser_parse_tentatively (parser);
8088 /* Look for a new-placement. */
8089 placement = cp_parser_new_placement (parser);
8090 /* If that didn't work out, there's no new-placement. */
8091 if (!cp_parser_parse_definitely (parser))
8093 if (placement != NULL)
8094 release_tree_vector (placement);
8095 placement = NULL;
8098 /* If the next token is a `(', then we have a parenthesized
8099 type-id. */
8100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8102 cp_token *token;
8103 const char *saved_message = parser->type_definition_forbidden_message;
8105 /* Consume the `('. */
8106 cp_lexer_consume_token (parser->lexer);
8108 /* Parse the type-id. */
8109 parser->type_definition_forbidden_message
8110 = G_("types may not be defined in a new-expression");
8112 type_id_in_expr_sentinel s (parser);
8113 type = cp_parser_type_id (parser);
8115 parser->type_definition_forbidden_message = saved_message;
8117 /* Look for the closing `)'. */
8118 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8119 token = cp_lexer_peek_token (parser->lexer);
8120 /* There should not be a direct-new-declarator in this production,
8121 but GCC used to allowed this, so we check and emit a sensible error
8122 message for this case. */
8123 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8125 error_at (token->location,
8126 "array bound forbidden after parenthesized type-id");
8127 inform (token->location,
8128 "try removing the parentheses around the type-id");
8129 cp_parser_direct_new_declarator (parser);
8132 /* Otherwise, there must be a new-type-id. */
8133 else
8134 type = cp_parser_new_type_id (parser, &nelts);
8136 /* If the next token is a `(' or '{', then we have a new-initializer. */
8137 cp_token *token = cp_lexer_peek_token (parser->lexer);
8138 if (token->type == CPP_OPEN_PAREN
8139 || token->type == CPP_OPEN_BRACE)
8140 initializer = cp_parser_new_initializer (parser);
8141 else
8142 initializer = NULL;
8144 /* A new-expression may not appear in an integral constant
8145 expression. */
8146 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8147 ret = error_mark_node;
8148 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8149 of a new-type-id or type-id of a new-expression, the new-expression shall
8150 contain a new-initializer of the form ( assignment-expression )".
8151 Additionally, consistently with the spirit of DR 1467, we want to accept
8152 'new auto { 2 }' too. */
8153 else if (type_uses_auto (type)
8154 && (vec_safe_length (initializer) != 1
8155 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8156 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8158 error_at (token->location,
8159 "initialization of new-expression for type %<auto%> "
8160 "requires exactly one element");
8161 ret = error_mark_node;
8163 else
8165 /* Construct a location e.g.:
8166 ptr = new int[100]
8167 ^~~~~~~~~~~~
8168 with caret == start at the start of the "new" token, and the end
8169 at the end of the final token we consumed. */
8170 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8171 location_t end_loc = get_finish (end_tok->location);
8172 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8174 /* Create a representation of the new-expression. */
8175 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8176 tf_warning_or_error);
8177 protected_set_expr_location (ret, combined_loc);
8180 if (placement != NULL)
8181 release_tree_vector (placement);
8182 if (initializer != NULL)
8183 release_tree_vector (initializer);
8185 return ret;
8188 /* Parse a new-placement.
8190 new-placement:
8191 ( expression-list )
8193 Returns the same representation as for an expression-list. */
8195 static vec<tree, va_gc> *
8196 cp_parser_new_placement (cp_parser* parser)
8198 vec<tree, va_gc> *expression_list;
8200 /* Parse the expression-list. */
8201 expression_list = (cp_parser_parenthesized_expression_list
8202 (parser, non_attr, /*cast_p=*/false,
8203 /*allow_expansion_p=*/true,
8204 /*non_constant_p=*/NULL));
8206 if (expression_list && expression_list->is_empty ())
8207 error ("expected expression-list or type-id");
8209 return expression_list;
8212 /* Parse a new-type-id.
8214 new-type-id:
8215 type-specifier-seq new-declarator [opt]
8217 Returns the TYPE allocated. If the new-type-id indicates an array
8218 type, *NELTS is set to the number of elements in the last array
8219 bound; the TYPE will not include the last array bound. */
8221 static tree
8222 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8224 cp_decl_specifier_seq type_specifier_seq;
8225 cp_declarator *new_declarator;
8226 cp_declarator *declarator;
8227 cp_declarator *outer_declarator;
8228 const char *saved_message;
8230 /* The type-specifier sequence must not contain type definitions.
8231 (It cannot contain declarations of new types either, but if they
8232 are not definitions we will catch that because they are not
8233 complete.) */
8234 saved_message = parser->type_definition_forbidden_message;
8235 parser->type_definition_forbidden_message
8236 = G_("types may not be defined in a new-type-id");
8237 /* Parse the type-specifier-seq. */
8238 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8239 /*is_trailing_return=*/false,
8240 &type_specifier_seq);
8241 /* Restore the old message. */
8242 parser->type_definition_forbidden_message = saved_message;
8244 if (type_specifier_seq.type == error_mark_node)
8245 return error_mark_node;
8247 /* Parse the new-declarator. */
8248 new_declarator = cp_parser_new_declarator_opt (parser);
8250 /* Determine the number of elements in the last array dimension, if
8251 any. */
8252 *nelts = NULL_TREE;
8253 /* Skip down to the last array dimension. */
8254 declarator = new_declarator;
8255 outer_declarator = NULL;
8256 while (declarator && (declarator->kind == cdk_pointer
8257 || declarator->kind == cdk_ptrmem))
8259 outer_declarator = declarator;
8260 declarator = declarator->declarator;
8262 while (declarator
8263 && declarator->kind == cdk_array
8264 && declarator->declarator
8265 && declarator->declarator->kind == cdk_array)
8267 outer_declarator = declarator;
8268 declarator = declarator->declarator;
8271 if (declarator && declarator->kind == cdk_array)
8273 *nelts = declarator->u.array.bounds;
8274 if (*nelts == error_mark_node)
8275 *nelts = integer_one_node;
8277 if (outer_declarator)
8278 outer_declarator->declarator = declarator->declarator;
8279 else
8280 new_declarator = NULL;
8283 return groktypename (&type_specifier_seq, new_declarator, false);
8286 /* Parse an (optional) new-declarator.
8288 new-declarator:
8289 ptr-operator new-declarator [opt]
8290 direct-new-declarator
8292 Returns the declarator. */
8294 static cp_declarator *
8295 cp_parser_new_declarator_opt (cp_parser* parser)
8297 enum tree_code code;
8298 tree type, std_attributes = NULL_TREE;
8299 cp_cv_quals cv_quals;
8301 /* We don't know if there's a ptr-operator next, or not. */
8302 cp_parser_parse_tentatively (parser);
8303 /* Look for a ptr-operator. */
8304 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8305 /* If that worked, look for more new-declarators. */
8306 if (cp_parser_parse_definitely (parser))
8308 cp_declarator *declarator;
8310 /* Parse another optional declarator. */
8311 declarator = cp_parser_new_declarator_opt (parser);
8313 declarator = cp_parser_make_indirect_declarator
8314 (code, type, cv_quals, declarator, std_attributes);
8316 return declarator;
8319 /* If the next token is a `[', there is a direct-new-declarator. */
8320 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8321 return cp_parser_direct_new_declarator (parser);
8323 return NULL;
8326 /* Parse a direct-new-declarator.
8328 direct-new-declarator:
8329 [ expression ]
8330 direct-new-declarator [constant-expression]
8334 static cp_declarator *
8335 cp_parser_direct_new_declarator (cp_parser* parser)
8337 cp_declarator *declarator = NULL;
8339 while (true)
8341 tree expression;
8342 cp_token *token;
8344 /* Look for the opening `['. */
8345 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8347 token = cp_lexer_peek_token (parser->lexer);
8348 expression = cp_parser_expression (parser);
8349 /* The standard requires that the expression have integral
8350 type. DR 74 adds enumeration types. We believe that the
8351 real intent is that these expressions be handled like the
8352 expression in a `switch' condition, which also allows
8353 classes with a single conversion to integral or
8354 enumeration type. */
8355 if (!processing_template_decl)
8357 expression
8358 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8359 expression,
8360 /*complain=*/true);
8361 if (!expression)
8363 error_at (token->location,
8364 "expression in new-declarator must have integral "
8365 "or enumeration type");
8366 expression = error_mark_node;
8370 /* Look for the closing `]'. */
8371 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8373 /* Add this bound to the declarator. */
8374 declarator = make_array_declarator (declarator, expression);
8376 /* If the next token is not a `[', then there are no more
8377 bounds. */
8378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8379 break;
8382 return declarator;
8385 /* Parse a new-initializer.
8387 new-initializer:
8388 ( expression-list [opt] )
8389 braced-init-list
8391 Returns a representation of the expression-list. */
8393 static vec<tree, va_gc> *
8394 cp_parser_new_initializer (cp_parser* parser)
8396 vec<tree, va_gc> *expression_list;
8398 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8400 tree t;
8401 bool expr_non_constant_p;
8402 cp_lexer_set_source_position (parser->lexer);
8403 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8404 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8405 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8406 expression_list = make_tree_vector_single (t);
8408 else
8409 expression_list = (cp_parser_parenthesized_expression_list
8410 (parser, non_attr, /*cast_p=*/false,
8411 /*allow_expansion_p=*/true,
8412 /*non_constant_p=*/NULL));
8414 return expression_list;
8417 /* Parse a delete-expression.
8419 delete-expression:
8420 :: [opt] delete cast-expression
8421 :: [opt] delete [ ] cast-expression
8423 Returns a representation of the expression. */
8425 static tree
8426 cp_parser_delete_expression (cp_parser* parser)
8428 bool global_scope_p;
8429 bool array_p;
8430 tree expression;
8432 /* Look for the optional `::' operator. */
8433 global_scope_p
8434 = (cp_parser_global_scope_opt (parser,
8435 /*current_scope_valid_p=*/false)
8436 != NULL_TREE);
8437 /* Look for the `delete' keyword. */
8438 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8439 /* See if the array syntax is in use. */
8440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8442 /* Consume the `[' token. */
8443 cp_lexer_consume_token (parser->lexer);
8444 /* Look for the `]' token. */
8445 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8446 /* Remember that this is the `[]' construct. */
8447 array_p = true;
8449 else
8450 array_p = false;
8452 /* Parse the cast-expression. */
8453 expression = cp_parser_simple_cast_expression (parser);
8455 /* A delete-expression may not appear in an integral constant
8456 expression. */
8457 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8458 return error_mark_node;
8460 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8461 tf_warning_or_error);
8464 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8465 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8466 0 otherwise. */
8468 static int
8469 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8471 cp_token *token = cp_lexer_peek_token (parser->lexer);
8472 switch (token->type)
8474 case CPP_COMMA:
8475 case CPP_SEMICOLON:
8476 case CPP_QUERY:
8477 case CPP_COLON:
8478 case CPP_CLOSE_SQUARE:
8479 case CPP_CLOSE_PAREN:
8480 case CPP_CLOSE_BRACE:
8481 case CPP_OPEN_BRACE:
8482 case CPP_DOT:
8483 case CPP_DOT_STAR:
8484 case CPP_DEREF:
8485 case CPP_DEREF_STAR:
8486 case CPP_DIV:
8487 case CPP_MOD:
8488 case CPP_LSHIFT:
8489 case CPP_RSHIFT:
8490 case CPP_LESS:
8491 case CPP_GREATER:
8492 case CPP_LESS_EQ:
8493 case CPP_GREATER_EQ:
8494 case CPP_EQ_EQ:
8495 case CPP_NOT_EQ:
8496 case CPP_EQ:
8497 case CPP_MULT_EQ:
8498 case CPP_DIV_EQ:
8499 case CPP_MOD_EQ:
8500 case CPP_PLUS_EQ:
8501 case CPP_MINUS_EQ:
8502 case CPP_RSHIFT_EQ:
8503 case CPP_LSHIFT_EQ:
8504 case CPP_AND_EQ:
8505 case CPP_XOR_EQ:
8506 case CPP_OR_EQ:
8507 case CPP_XOR:
8508 case CPP_OR:
8509 case CPP_OR_OR:
8510 case CPP_EOF:
8511 case CPP_ELLIPSIS:
8512 return 0;
8514 case CPP_OPEN_PAREN:
8515 /* In ((type ()) () the last () isn't a valid cast-expression,
8516 so the whole must be parsed as postfix-expression. */
8517 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8518 != CPP_CLOSE_PAREN;
8520 case CPP_OPEN_SQUARE:
8521 /* '[' may start a primary-expression in obj-c++ and in C++11,
8522 as a lambda-expression, eg, '(void)[]{}'. */
8523 if (cxx_dialect >= cxx11)
8524 return -1;
8525 return c_dialect_objc ();
8527 case CPP_PLUS_PLUS:
8528 case CPP_MINUS_MINUS:
8529 /* '++' and '--' may or may not start a cast-expression:
8531 struct T { void operator++(int); };
8532 void f() { (T())++; }
8536 int a;
8537 (int)++a; */
8538 return -1;
8540 default:
8541 return 1;
8545 /* Parse a cast-expression.
8547 cast-expression:
8548 unary-expression
8549 ( type-id ) cast-expression
8551 ADDRESS_P is true iff the unary-expression is appearing as the
8552 operand of the `&' operator. CAST_P is true if this expression is
8553 the target of a cast.
8555 Returns a representation of the expression. */
8557 static cp_expr
8558 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8559 bool decltype_p, cp_id_kind * pidk)
8561 /* If it's a `(', then we might be looking at a cast. */
8562 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8564 tree type = NULL_TREE;
8565 cp_expr expr (NULL_TREE);
8566 int cast_expression = 0;
8567 const char *saved_message;
8569 /* There's no way to know yet whether or not this is a cast.
8570 For example, `(int (3))' is a unary-expression, while `(int)
8571 3' is a cast. So, we resort to parsing tentatively. */
8572 cp_parser_parse_tentatively (parser);
8573 /* Types may not be defined in a cast. */
8574 saved_message = parser->type_definition_forbidden_message;
8575 parser->type_definition_forbidden_message
8576 = G_("types may not be defined in casts");
8577 /* Consume the `('. */
8578 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8579 location_t open_paren_loc = open_paren->location;
8581 /* A very tricky bit is that `(struct S) { 3 }' is a
8582 compound-literal (which we permit in C++ as an extension).
8583 But, that construct is not a cast-expression -- it is a
8584 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8585 is legal; if the compound-literal were a cast-expression,
8586 you'd need an extra set of parentheses.) But, if we parse
8587 the type-id, and it happens to be a class-specifier, then we
8588 will commit to the parse at that point, because we cannot
8589 undo the action that is done when creating a new class. So,
8590 then we cannot back up and do a postfix-expression.
8592 Another tricky case is the following (c++/29234):
8594 struct S { void operator () (); };
8596 void foo ()
8598 ( S()() );
8601 As a type-id we parse the parenthesized S()() as a function
8602 returning a function, groktypename complains and we cannot
8603 back up in this case either.
8605 Therefore, we scan ahead to the closing `)', and check to see
8606 if the tokens after the `)' can start a cast-expression. Otherwise
8607 we are dealing with an unary-expression, a postfix-expression
8608 or something else.
8610 Yet another tricky case, in C++11, is the following (c++/54891):
8612 (void)[]{};
8614 The issue is that usually, besides the case of lambda-expressions,
8615 the parenthesized type-id cannot be followed by '[', and, eg, we
8616 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8617 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8618 we don't commit, we try a cast-expression, then an unary-expression.
8620 Save tokens so that we can put them back. */
8621 cp_lexer_save_tokens (parser->lexer);
8623 /* We may be looking at a cast-expression. */
8624 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8625 /*consume_paren=*/true))
8626 cast_expression
8627 = cp_parser_tokens_start_cast_expression (parser);
8629 /* Roll back the tokens we skipped. */
8630 cp_lexer_rollback_tokens (parser->lexer);
8631 /* If we aren't looking at a cast-expression, simulate an error so
8632 that the call to cp_parser_error_occurred below returns true. */
8633 if (!cast_expression)
8634 cp_parser_simulate_error (parser);
8635 else
8637 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8638 parser->in_type_id_in_expr_p = true;
8639 /* Look for the type-id. */
8640 type = cp_parser_type_id (parser);
8641 /* Look for the closing `)'. */
8642 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8643 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8646 /* Restore the saved message. */
8647 parser->type_definition_forbidden_message = saved_message;
8649 /* At this point this can only be either a cast or a
8650 parenthesized ctor such as `(T ())' that looks like a cast to
8651 function returning T. */
8652 if (!cp_parser_error_occurred (parser))
8654 /* Only commit if the cast-expression doesn't start with
8655 '++', '--', or '[' in C++11. */
8656 if (cast_expression > 0)
8657 cp_parser_commit_to_topmost_tentative_parse (parser);
8659 expr = cp_parser_cast_expression (parser,
8660 /*address_p=*/false,
8661 /*cast_p=*/true,
8662 /*decltype_p=*/false,
8663 pidk);
8665 if (cp_parser_parse_definitely (parser))
8667 /* Warn about old-style casts, if so requested. */
8668 if (warn_old_style_cast
8669 && !in_system_header_at (input_location)
8670 && !VOID_TYPE_P (type)
8671 && current_lang_name != lang_name_c)
8672 warning (OPT_Wold_style_cast, "use of old-style cast");
8674 /* Only type conversions to integral or enumeration types
8675 can be used in constant-expressions. */
8676 if (!cast_valid_in_integral_constant_expression_p (type)
8677 && cp_parser_non_integral_constant_expression (parser,
8678 NIC_CAST))
8679 return error_mark_node;
8681 /* Perform the cast. */
8682 /* Make a location:
8683 (TYPE) EXPR
8684 ^~~~~~~~~~~
8685 with start==caret at the open paren, extending to the
8686 end of "expr". */
8687 location_t cast_loc = make_location (open_paren_loc,
8688 open_paren_loc,
8689 expr.get_finish ());
8690 expr = build_c_cast (cast_loc, type, expr);
8691 return expr;
8694 else
8695 cp_parser_abort_tentative_parse (parser);
8698 /* If we get here, then it's not a cast, so it must be a
8699 unary-expression. */
8700 return cp_parser_unary_expression (parser, pidk, address_p,
8701 cast_p, decltype_p);
8704 /* Parse a binary expression of the general form:
8706 pm-expression:
8707 cast-expression
8708 pm-expression .* cast-expression
8709 pm-expression ->* cast-expression
8711 multiplicative-expression:
8712 pm-expression
8713 multiplicative-expression * pm-expression
8714 multiplicative-expression / pm-expression
8715 multiplicative-expression % pm-expression
8717 additive-expression:
8718 multiplicative-expression
8719 additive-expression + multiplicative-expression
8720 additive-expression - multiplicative-expression
8722 shift-expression:
8723 additive-expression
8724 shift-expression << additive-expression
8725 shift-expression >> additive-expression
8727 relational-expression:
8728 shift-expression
8729 relational-expression < shift-expression
8730 relational-expression > shift-expression
8731 relational-expression <= shift-expression
8732 relational-expression >= shift-expression
8734 GNU Extension:
8736 relational-expression:
8737 relational-expression <? shift-expression
8738 relational-expression >? shift-expression
8740 equality-expression:
8741 relational-expression
8742 equality-expression == relational-expression
8743 equality-expression != relational-expression
8745 and-expression:
8746 equality-expression
8747 and-expression & equality-expression
8749 exclusive-or-expression:
8750 and-expression
8751 exclusive-or-expression ^ and-expression
8753 inclusive-or-expression:
8754 exclusive-or-expression
8755 inclusive-or-expression | exclusive-or-expression
8757 logical-and-expression:
8758 inclusive-or-expression
8759 logical-and-expression && inclusive-or-expression
8761 logical-or-expression:
8762 logical-and-expression
8763 logical-or-expression || logical-and-expression
8765 All these are implemented with a single function like:
8767 binary-expression:
8768 simple-cast-expression
8769 binary-expression <token> binary-expression
8771 CAST_P is true if this expression is the target of a cast.
8773 The binops_by_token map is used to get the tree codes for each <token> type.
8774 binary-expressions are associated according to a precedence table. */
8776 #define TOKEN_PRECEDENCE(token) \
8777 (((token->type == CPP_GREATER \
8778 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8779 && !parser->greater_than_is_operator_p) \
8780 ? PREC_NOT_OPERATOR \
8781 : binops_by_token[token->type].prec)
8783 static cp_expr
8784 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8785 bool no_toplevel_fold_p,
8786 bool decltype_p,
8787 enum cp_parser_prec prec,
8788 cp_id_kind * pidk)
8790 cp_parser_expression_stack stack;
8791 cp_parser_expression_stack_entry *sp = &stack[0];
8792 cp_parser_expression_stack_entry current;
8793 cp_expr rhs;
8794 cp_token *token;
8795 enum tree_code rhs_type;
8796 enum cp_parser_prec new_prec, lookahead_prec;
8797 tree overload;
8799 /* Parse the first expression. */
8800 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8801 ? TRUTH_NOT_EXPR : ERROR_MARK);
8802 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8803 cast_p, decltype_p, pidk);
8804 current.prec = prec;
8806 if (cp_parser_error_occurred (parser))
8807 return error_mark_node;
8809 for (;;)
8811 /* Get an operator token. */
8812 token = cp_lexer_peek_token (parser->lexer);
8814 if (warn_cxx11_compat
8815 && token->type == CPP_RSHIFT
8816 && !parser->greater_than_is_operator_p)
8818 if (warning_at (token->location, OPT_Wc__11_compat,
8819 "%<>>%> operator is treated"
8820 " as two right angle brackets in C++11"))
8821 inform (token->location,
8822 "suggest parentheses around %<>>%> expression");
8825 new_prec = TOKEN_PRECEDENCE (token);
8826 if (new_prec != PREC_NOT_OPERATOR
8827 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8828 /* This is a fold-expression; handle it later. */
8829 new_prec = PREC_NOT_OPERATOR;
8831 /* Popping an entry off the stack means we completed a subexpression:
8832 - either we found a token which is not an operator (`>' where it is not
8833 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8834 will happen repeatedly;
8835 - or, we found an operator which has lower priority. This is the case
8836 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8837 parsing `3 * 4'. */
8838 if (new_prec <= current.prec)
8840 if (sp == stack)
8841 break;
8842 else
8843 goto pop;
8846 get_rhs:
8847 current.tree_type = binops_by_token[token->type].tree_type;
8848 current.loc = token->location;
8850 /* We used the operator token. */
8851 cp_lexer_consume_token (parser->lexer);
8853 /* For "false && x" or "true || x", x will never be executed;
8854 disable warnings while evaluating it. */
8855 if (current.tree_type == TRUTH_ANDIF_EXPR)
8856 c_inhibit_evaluation_warnings +=
8857 cp_fully_fold (current.lhs) == truthvalue_false_node;
8858 else if (current.tree_type == TRUTH_ORIF_EXPR)
8859 c_inhibit_evaluation_warnings +=
8860 cp_fully_fold (current.lhs) == truthvalue_true_node;
8862 /* Extract another operand. It may be the RHS of this expression
8863 or the LHS of a new, higher priority expression. */
8864 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8865 ? TRUTH_NOT_EXPR : ERROR_MARK);
8866 rhs = cp_parser_simple_cast_expression (parser);
8868 /* Get another operator token. Look up its precedence to avoid
8869 building a useless (immediately popped) stack entry for common
8870 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8871 token = cp_lexer_peek_token (parser->lexer);
8872 lookahead_prec = TOKEN_PRECEDENCE (token);
8873 if (lookahead_prec != PREC_NOT_OPERATOR
8874 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8875 lookahead_prec = PREC_NOT_OPERATOR;
8876 if (lookahead_prec > new_prec)
8878 /* ... and prepare to parse the RHS of the new, higher priority
8879 expression. Since precedence levels on the stack are
8880 monotonically increasing, we do not have to care about
8881 stack overflows. */
8882 *sp = current;
8883 ++sp;
8884 current.lhs = rhs;
8885 current.lhs_type = rhs_type;
8886 current.prec = new_prec;
8887 new_prec = lookahead_prec;
8888 goto get_rhs;
8890 pop:
8891 lookahead_prec = new_prec;
8892 /* If the stack is not empty, we have parsed into LHS the right side
8893 (`4' in the example above) of an expression we had suspended.
8894 We can use the information on the stack to recover the LHS (`3')
8895 from the stack together with the tree code (`MULT_EXPR'), and
8896 the precedence of the higher level subexpression
8897 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8898 which will be used to actually build the additive expression. */
8899 rhs = current.lhs;
8900 rhs_type = current.lhs_type;
8901 --sp;
8902 current = *sp;
8905 /* Undo the disabling of warnings done above. */
8906 if (current.tree_type == TRUTH_ANDIF_EXPR)
8907 c_inhibit_evaluation_warnings -=
8908 cp_fully_fold (current.lhs) == truthvalue_false_node;
8909 else if (current.tree_type == TRUTH_ORIF_EXPR)
8910 c_inhibit_evaluation_warnings -=
8911 cp_fully_fold (current.lhs) == truthvalue_true_node;
8913 if (warn_logical_not_paren
8914 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8915 && current.lhs_type == TRUTH_NOT_EXPR
8916 /* Avoid warning for !!x == y. */
8917 && (TREE_CODE (current.lhs) != NE_EXPR
8918 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8919 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8920 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8921 /* Avoid warning for !b == y where b is boolean. */
8922 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8923 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8924 != BOOLEAN_TYPE))))
8925 /* Avoid warning for !!b == y where b is boolean. */
8926 && (!DECL_P (current.lhs)
8927 || TREE_TYPE (current.lhs) == NULL_TREE
8928 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8929 warn_logical_not_parentheses (current.loc, current.tree_type,
8930 current.lhs, maybe_constant_value (rhs));
8932 overload = NULL;
8934 location_t combined_loc = make_location (current.loc,
8935 current.lhs.get_start (),
8936 rhs.get_finish ());
8938 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8939 ERROR_MARK for everything that is not a binary expression.
8940 This makes warn_about_parentheses miss some warnings that
8941 involve unary operators. For unary expressions we should
8942 pass the correct tree_code unless the unary expression was
8943 surrounded by parentheses.
8945 if (no_toplevel_fold_p
8946 && lookahead_prec <= current.prec
8947 && sp == stack)
8948 current.lhs = build2_loc (combined_loc,
8949 current.tree_type,
8950 TREE_CODE_CLASS (current.tree_type)
8951 == tcc_comparison
8952 ? boolean_type_node : TREE_TYPE (current.lhs),
8953 current.lhs, rhs);
8954 else
8956 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8957 current.lhs, current.lhs_type,
8958 rhs, rhs_type, &overload,
8959 complain_flags (decltype_p));
8960 /* TODO: build_x_binary_op doesn't always honor the location. */
8961 current.lhs.set_location (combined_loc);
8963 current.lhs_type = current.tree_type;
8965 /* If the binary operator required the use of an overloaded operator,
8966 then this expression cannot be an integral constant-expression.
8967 An overloaded operator can be used even if both operands are
8968 otherwise permissible in an integral constant-expression if at
8969 least one of the operands is of enumeration type. */
8971 if (overload
8972 && cp_parser_non_integral_constant_expression (parser,
8973 NIC_OVERLOADED))
8974 return error_mark_node;
8977 return current.lhs;
8980 static cp_expr
8981 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8982 bool no_toplevel_fold_p,
8983 enum cp_parser_prec prec,
8984 cp_id_kind * pidk)
8986 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8987 /*decltype*/false, prec, pidk);
8990 /* Parse the `? expression : assignment-expression' part of a
8991 conditional-expression. The LOGICAL_OR_EXPR is the
8992 logical-or-expression that started the conditional-expression.
8993 Returns a representation of the entire conditional-expression.
8995 This routine is used by cp_parser_assignment_expression.
8997 ? expression : assignment-expression
8999 GNU Extensions:
9001 ? : assignment-expression */
9003 static tree
9004 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9006 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9007 cp_expr assignment_expr;
9008 struct cp_token *token;
9009 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9011 /* Consume the `?' token. */
9012 cp_lexer_consume_token (parser->lexer);
9013 token = cp_lexer_peek_token (parser->lexer);
9014 if (cp_parser_allow_gnu_extensions_p (parser)
9015 && token->type == CPP_COLON)
9017 pedwarn (token->location, OPT_Wpedantic,
9018 "ISO C++ does not allow ?: with omitted middle operand");
9019 /* Implicit true clause. */
9020 expr = NULL_TREE;
9021 c_inhibit_evaluation_warnings +=
9022 folded_logical_or_expr == truthvalue_true_node;
9023 warn_for_omitted_condop (token->location, logical_or_expr);
9025 else
9027 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9028 parser->colon_corrects_to_scope_p = false;
9029 /* Parse the expression. */
9030 c_inhibit_evaluation_warnings +=
9031 folded_logical_or_expr == truthvalue_false_node;
9032 expr = cp_parser_expression (parser);
9033 c_inhibit_evaluation_warnings +=
9034 ((folded_logical_or_expr == truthvalue_true_node)
9035 - (folded_logical_or_expr == truthvalue_false_node));
9036 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9039 /* The next token should be a `:'. */
9040 cp_parser_require (parser, CPP_COLON, RT_COLON);
9041 /* Parse the assignment-expression. */
9042 assignment_expr = cp_parser_assignment_expression (parser);
9043 c_inhibit_evaluation_warnings -=
9044 folded_logical_or_expr == truthvalue_true_node;
9046 /* Make a location:
9047 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9048 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9049 with the caret at the "?", ranging from the start of
9050 the logical_or_expr to the end of the assignment_expr. */
9051 loc = make_location (loc,
9052 logical_or_expr.get_start (),
9053 assignment_expr.get_finish ());
9055 /* Build the conditional-expression. */
9056 return build_x_conditional_expr (loc, logical_or_expr,
9057 expr,
9058 assignment_expr,
9059 tf_warning_or_error);
9062 /* Parse an assignment-expression.
9064 assignment-expression:
9065 conditional-expression
9066 logical-or-expression assignment-operator assignment_expression
9067 throw-expression
9069 CAST_P is true if this expression is the target of a cast.
9070 DECLTYPE_P is true if this expression is the operand of decltype.
9072 Returns a representation for the expression. */
9074 static cp_expr
9075 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9076 bool cast_p, bool decltype_p)
9078 cp_expr expr;
9080 /* If the next token is the `throw' keyword, then we're looking at
9081 a throw-expression. */
9082 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9083 expr = cp_parser_throw_expression (parser);
9084 /* Otherwise, it must be that we are looking at a
9085 logical-or-expression. */
9086 else
9088 /* Parse the binary expressions (logical-or-expression). */
9089 expr = cp_parser_binary_expression (parser, cast_p, false,
9090 decltype_p,
9091 PREC_NOT_OPERATOR, pidk);
9092 /* If the next token is a `?' then we're actually looking at a
9093 conditional-expression. */
9094 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9095 return cp_parser_question_colon_clause (parser, expr);
9096 else
9098 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9100 /* If it's an assignment-operator, we're using the second
9101 production. */
9102 enum tree_code assignment_operator
9103 = cp_parser_assignment_operator_opt (parser);
9104 if (assignment_operator != ERROR_MARK)
9106 bool non_constant_p;
9108 /* Parse the right-hand side of the assignment. */
9109 cp_expr rhs = cp_parser_initializer_clause (parser,
9110 &non_constant_p);
9112 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9113 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9115 /* An assignment may not appear in a
9116 constant-expression. */
9117 if (cp_parser_non_integral_constant_expression (parser,
9118 NIC_ASSIGNMENT))
9119 return error_mark_node;
9120 /* Build the assignment expression. Its default
9121 location:
9122 LHS = RHS
9123 ~~~~^~~~~
9124 is the location of the '=' token as the
9125 caret, ranging from the start of the lhs to the
9126 end of the rhs. */
9127 loc = make_location (loc,
9128 expr.get_start (),
9129 rhs.get_finish ());
9130 expr = build_x_modify_expr (loc, expr,
9131 assignment_operator,
9132 rhs,
9133 complain_flags (decltype_p));
9134 /* TODO: build_x_modify_expr doesn't honor the location,
9135 so we must set it here. */
9136 expr.set_location (loc);
9141 return expr;
9144 /* Parse an (optional) assignment-operator.
9146 assignment-operator: one of
9147 = *= /= %= += -= >>= <<= &= ^= |=
9149 GNU Extension:
9151 assignment-operator: one of
9152 <?= >?=
9154 If the next token is an assignment operator, the corresponding tree
9155 code is returned, and the token is consumed. For example, for
9156 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9157 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9158 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9159 operator, ERROR_MARK is returned. */
9161 static enum tree_code
9162 cp_parser_assignment_operator_opt (cp_parser* parser)
9164 enum tree_code op;
9165 cp_token *token;
9167 /* Peek at the next token. */
9168 token = cp_lexer_peek_token (parser->lexer);
9170 switch (token->type)
9172 case CPP_EQ:
9173 op = NOP_EXPR;
9174 break;
9176 case CPP_MULT_EQ:
9177 op = MULT_EXPR;
9178 break;
9180 case CPP_DIV_EQ:
9181 op = TRUNC_DIV_EXPR;
9182 break;
9184 case CPP_MOD_EQ:
9185 op = TRUNC_MOD_EXPR;
9186 break;
9188 case CPP_PLUS_EQ:
9189 op = PLUS_EXPR;
9190 break;
9192 case CPP_MINUS_EQ:
9193 op = MINUS_EXPR;
9194 break;
9196 case CPP_RSHIFT_EQ:
9197 op = RSHIFT_EXPR;
9198 break;
9200 case CPP_LSHIFT_EQ:
9201 op = LSHIFT_EXPR;
9202 break;
9204 case CPP_AND_EQ:
9205 op = BIT_AND_EXPR;
9206 break;
9208 case CPP_XOR_EQ:
9209 op = BIT_XOR_EXPR;
9210 break;
9212 case CPP_OR_EQ:
9213 op = BIT_IOR_EXPR;
9214 break;
9216 default:
9217 /* Nothing else is an assignment operator. */
9218 op = ERROR_MARK;
9221 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9222 if (op != ERROR_MARK
9223 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9224 op = ERROR_MARK;
9226 /* If it was an assignment operator, consume it. */
9227 if (op != ERROR_MARK)
9228 cp_lexer_consume_token (parser->lexer);
9230 return op;
9233 /* Parse an expression.
9235 expression:
9236 assignment-expression
9237 expression , assignment-expression
9239 CAST_P is true if this expression is the target of a cast.
9240 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9241 except possibly parenthesized or on the RHS of a comma (N3276).
9243 Returns a representation of the expression. */
9245 static cp_expr
9246 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9247 bool cast_p, bool decltype_p)
9249 cp_expr expression = NULL_TREE;
9250 location_t loc = UNKNOWN_LOCATION;
9252 while (true)
9254 cp_expr assignment_expression;
9256 /* Parse the next assignment-expression. */
9257 assignment_expression
9258 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9260 /* We don't create a temporary for a call that is the immediate operand
9261 of decltype or on the RHS of a comma. But when we see a comma, we
9262 need to create a temporary for a call on the LHS. */
9263 if (decltype_p && !processing_template_decl
9264 && TREE_CODE (assignment_expression) == CALL_EXPR
9265 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9266 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9267 assignment_expression
9268 = build_cplus_new (TREE_TYPE (assignment_expression),
9269 assignment_expression, tf_warning_or_error);
9271 /* If this is the first assignment-expression, we can just
9272 save it away. */
9273 if (!expression)
9274 expression = assignment_expression;
9275 else
9277 /* Create a location with caret at the comma, ranging
9278 from the start of the LHS to the end of the RHS. */
9279 loc = make_location (loc,
9280 expression.get_start (),
9281 assignment_expression.get_finish ());
9282 expression = build_x_compound_expr (loc, expression,
9283 assignment_expression,
9284 complain_flags (decltype_p));
9285 expression.set_location (loc);
9287 /* If the next token is not a comma, or we're in a fold-expression, then
9288 we are done with the expression. */
9289 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9290 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9291 break;
9292 /* Consume the `,'. */
9293 loc = cp_lexer_peek_token (parser->lexer)->location;
9294 cp_lexer_consume_token (parser->lexer);
9295 /* A comma operator cannot appear in a constant-expression. */
9296 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9297 expression = error_mark_node;
9300 return expression;
9303 /* Parse a constant-expression.
9305 constant-expression:
9306 conditional-expression
9308 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9309 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9310 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9311 is false, NON_CONSTANT_P should be NULL. */
9313 static cp_expr
9314 cp_parser_constant_expression (cp_parser* parser,
9315 bool allow_non_constant_p,
9316 bool *non_constant_p)
9318 bool saved_integral_constant_expression_p;
9319 bool saved_allow_non_integral_constant_expression_p;
9320 bool saved_non_integral_constant_expression_p;
9321 cp_expr expression;
9323 /* It might seem that we could simply parse the
9324 conditional-expression, and then check to see if it were
9325 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9326 one that the compiler can figure out is constant, possibly after
9327 doing some simplifications or optimizations. The standard has a
9328 precise definition of constant-expression, and we must honor
9329 that, even though it is somewhat more restrictive.
9331 For example:
9333 int i[(2, 3)];
9335 is not a legal declaration, because `(2, 3)' is not a
9336 constant-expression. The `,' operator is forbidden in a
9337 constant-expression. However, GCC's constant-folding machinery
9338 will fold this operation to an INTEGER_CST for `3'. */
9340 /* Save the old settings. */
9341 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9342 saved_allow_non_integral_constant_expression_p
9343 = parser->allow_non_integral_constant_expression_p;
9344 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9345 /* We are now parsing a constant-expression. */
9346 parser->integral_constant_expression_p = true;
9347 parser->allow_non_integral_constant_expression_p
9348 = (allow_non_constant_p || cxx_dialect >= cxx11);
9349 parser->non_integral_constant_expression_p = false;
9350 /* Although the grammar says "conditional-expression", we parse an
9351 "assignment-expression", which also permits "throw-expression"
9352 and the use of assignment operators. In the case that
9353 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9354 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9355 actually essential that we look for an assignment-expression.
9356 For example, cp_parser_initializer_clauses uses this function to
9357 determine whether a particular assignment-expression is in fact
9358 constant. */
9359 expression = cp_parser_assignment_expression (parser);
9360 /* Restore the old settings. */
9361 parser->integral_constant_expression_p
9362 = saved_integral_constant_expression_p;
9363 parser->allow_non_integral_constant_expression_p
9364 = saved_allow_non_integral_constant_expression_p;
9365 if (cxx_dialect >= cxx11)
9367 /* Require an rvalue constant expression here; that's what our
9368 callers expect. Reference constant expressions are handled
9369 separately in e.g. cp_parser_template_argument. */
9370 bool is_const = potential_rvalue_constant_expression (expression);
9371 parser->non_integral_constant_expression_p = !is_const;
9372 if (!is_const && !allow_non_constant_p)
9373 require_potential_rvalue_constant_expression (expression);
9375 if (allow_non_constant_p)
9376 *non_constant_p = parser->non_integral_constant_expression_p;
9377 parser->non_integral_constant_expression_p
9378 = saved_non_integral_constant_expression_p;
9380 return expression;
9383 /* Parse __builtin_offsetof.
9385 offsetof-expression:
9386 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9388 offsetof-member-designator:
9389 id-expression
9390 | offsetof-member-designator "." id-expression
9391 | offsetof-member-designator "[" expression "]"
9392 | offsetof-member-designator "->" id-expression */
9394 static cp_expr
9395 cp_parser_builtin_offsetof (cp_parser *parser)
9397 int save_ice_p, save_non_ice_p;
9398 tree type;
9399 cp_expr expr;
9400 cp_id_kind dummy;
9401 cp_token *token;
9402 location_t finish_loc;
9404 /* We're about to accept non-integral-constant things, but will
9405 definitely yield an integral constant expression. Save and
9406 restore these values around our local parsing. */
9407 save_ice_p = parser->integral_constant_expression_p;
9408 save_non_ice_p = parser->non_integral_constant_expression_p;
9410 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9412 /* Consume the "__builtin_offsetof" token. */
9413 cp_lexer_consume_token (parser->lexer);
9414 /* Consume the opening `('. */
9415 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9416 /* Parse the type-id. */
9417 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9418 type = cp_parser_type_id (parser);
9419 /* Look for the `,'. */
9420 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9421 token = cp_lexer_peek_token (parser->lexer);
9423 /* Build the (type *)null that begins the traditional offsetof macro. */
9424 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9425 tf_warning_or_error);
9427 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9428 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9429 true, &dummy, token->location);
9430 while (true)
9432 token = cp_lexer_peek_token (parser->lexer);
9433 switch (token->type)
9435 case CPP_OPEN_SQUARE:
9436 /* offsetof-member-designator "[" expression "]" */
9437 expr = cp_parser_postfix_open_square_expression (parser, expr,
9438 true, false);
9439 break;
9441 case CPP_DEREF:
9442 /* offsetof-member-designator "->" identifier */
9443 expr = grok_array_decl (token->location, expr,
9444 integer_zero_node, false);
9445 /* FALLTHRU */
9447 case CPP_DOT:
9448 /* offsetof-member-designator "." identifier */
9449 cp_lexer_consume_token (parser->lexer);
9450 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9451 expr, true, &dummy,
9452 token->location);
9453 break;
9455 case CPP_CLOSE_PAREN:
9456 /* Consume the ")" token. */
9457 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9458 cp_lexer_consume_token (parser->lexer);
9459 goto success;
9461 default:
9462 /* Error. We know the following require will fail, but
9463 that gives the proper error message. */
9464 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9465 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9466 expr = error_mark_node;
9467 goto failure;
9471 success:
9472 /* Make a location of the form:
9473 __builtin_offsetof (struct s, f)
9474 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9475 with caret at the type-id, ranging from the start of the
9476 "_builtin_offsetof" token to the close paren. */
9477 loc = make_location (loc, start_loc, finish_loc);
9478 /* The result will be an INTEGER_CST, so we need to explicitly
9479 preserve the location. */
9480 expr = cp_expr (finish_offsetof (expr, loc), loc);
9482 failure:
9483 parser->integral_constant_expression_p = save_ice_p;
9484 parser->non_integral_constant_expression_p = save_non_ice_p;
9486 return expr;
9489 /* Parse a trait expression.
9491 Returns a representation of the expression, the underlying type
9492 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9494 static tree
9495 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9497 cp_trait_kind kind;
9498 tree type1, type2 = NULL_TREE;
9499 bool binary = false;
9500 bool variadic = false;
9502 switch (keyword)
9504 case RID_HAS_NOTHROW_ASSIGN:
9505 kind = CPTK_HAS_NOTHROW_ASSIGN;
9506 break;
9507 case RID_HAS_NOTHROW_CONSTRUCTOR:
9508 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9509 break;
9510 case RID_HAS_NOTHROW_COPY:
9511 kind = CPTK_HAS_NOTHROW_COPY;
9512 break;
9513 case RID_HAS_TRIVIAL_ASSIGN:
9514 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9515 break;
9516 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9517 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9518 break;
9519 case RID_HAS_TRIVIAL_COPY:
9520 kind = CPTK_HAS_TRIVIAL_COPY;
9521 break;
9522 case RID_HAS_TRIVIAL_DESTRUCTOR:
9523 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9524 break;
9525 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9526 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9527 break;
9528 case RID_HAS_VIRTUAL_DESTRUCTOR:
9529 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9530 break;
9531 case RID_IS_ABSTRACT:
9532 kind = CPTK_IS_ABSTRACT;
9533 break;
9534 case RID_IS_BASE_OF:
9535 kind = CPTK_IS_BASE_OF;
9536 binary = true;
9537 break;
9538 case RID_IS_CLASS:
9539 kind = CPTK_IS_CLASS;
9540 break;
9541 case RID_IS_EMPTY:
9542 kind = CPTK_IS_EMPTY;
9543 break;
9544 case RID_IS_ENUM:
9545 kind = CPTK_IS_ENUM;
9546 break;
9547 case RID_IS_FINAL:
9548 kind = CPTK_IS_FINAL;
9549 break;
9550 case RID_IS_LITERAL_TYPE:
9551 kind = CPTK_IS_LITERAL_TYPE;
9552 break;
9553 case RID_IS_POD:
9554 kind = CPTK_IS_POD;
9555 break;
9556 case RID_IS_POLYMORPHIC:
9557 kind = CPTK_IS_POLYMORPHIC;
9558 break;
9559 case RID_IS_SAME_AS:
9560 kind = CPTK_IS_SAME_AS;
9561 binary = true;
9562 break;
9563 case RID_IS_STD_LAYOUT:
9564 kind = CPTK_IS_STD_LAYOUT;
9565 break;
9566 case RID_IS_TRIVIAL:
9567 kind = CPTK_IS_TRIVIAL;
9568 break;
9569 case RID_IS_TRIVIALLY_ASSIGNABLE:
9570 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9571 binary = true;
9572 break;
9573 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9574 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9575 variadic = true;
9576 break;
9577 case RID_IS_TRIVIALLY_COPYABLE:
9578 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9579 break;
9580 case RID_IS_UNION:
9581 kind = CPTK_IS_UNION;
9582 break;
9583 case RID_UNDERLYING_TYPE:
9584 kind = CPTK_UNDERLYING_TYPE;
9585 break;
9586 case RID_BASES:
9587 kind = CPTK_BASES;
9588 break;
9589 case RID_DIRECT_BASES:
9590 kind = CPTK_DIRECT_BASES;
9591 break;
9592 default:
9593 gcc_unreachable ();
9596 /* Consume the token. */
9597 cp_lexer_consume_token (parser->lexer);
9599 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9602 type_id_in_expr_sentinel s (parser);
9603 type1 = cp_parser_type_id (parser);
9606 if (type1 == error_mark_node)
9607 return error_mark_node;
9609 if (binary)
9611 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9614 type_id_in_expr_sentinel s (parser);
9615 type2 = cp_parser_type_id (parser);
9618 if (type2 == error_mark_node)
9619 return error_mark_node;
9621 else if (variadic)
9623 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9625 cp_lexer_consume_token (parser->lexer);
9626 tree elt = cp_parser_type_id (parser);
9627 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9629 cp_lexer_consume_token (parser->lexer);
9630 elt = make_pack_expansion (elt);
9632 if (elt == error_mark_node)
9633 return error_mark_node;
9634 type2 = tree_cons (NULL_TREE, elt, type2);
9638 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9640 /* Complete the trait expression, which may mean either processing
9641 the trait expr now or saving it for template instantiation. */
9642 switch (kind)
9644 case CPTK_UNDERLYING_TYPE:
9645 return finish_underlying_type (type1);
9646 case CPTK_BASES:
9647 return finish_bases (type1, false);
9648 case CPTK_DIRECT_BASES:
9649 return finish_bases (type1, true);
9650 default:
9651 return finish_trait_expr (kind, type1, type2);
9655 /* Lambdas that appear in variable initializer or default argument scope
9656 get that in their mangling, so we need to record it. We might as well
9657 use the count for function and namespace scopes as well. */
9658 static GTY(()) tree lambda_scope;
9659 static GTY(()) int lambda_count;
9660 struct GTY(()) tree_int
9662 tree t;
9663 int i;
9665 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9667 static void
9668 start_lambda_scope (tree decl)
9670 tree_int ti;
9671 gcc_assert (decl);
9672 /* Once we're inside a function, we ignore other scopes and just push
9673 the function again so that popping works properly. */
9674 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9675 decl = current_function_decl;
9676 ti.t = lambda_scope;
9677 ti.i = lambda_count;
9678 vec_safe_push (lambda_scope_stack, ti);
9679 if (lambda_scope != decl)
9681 /* Don't reset the count if we're still in the same function. */
9682 lambda_scope = decl;
9683 lambda_count = 0;
9687 static void
9688 record_lambda_scope (tree lambda)
9690 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9691 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9694 static void
9695 finish_lambda_scope (void)
9697 tree_int *p = &lambda_scope_stack->last ();
9698 if (lambda_scope != p->t)
9700 lambda_scope = p->t;
9701 lambda_count = p->i;
9703 lambda_scope_stack->pop ();
9706 /* Parse a lambda expression.
9708 lambda-expression:
9709 lambda-introducer lambda-declarator [opt] compound-statement
9711 Returns a representation of the expression. */
9713 static cp_expr
9714 cp_parser_lambda_expression (cp_parser* parser)
9716 tree lambda_expr = build_lambda_expr ();
9717 tree type;
9718 bool ok = true;
9719 cp_token *token = cp_lexer_peek_token (parser->lexer);
9720 cp_token_position start = 0;
9722 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9724 if (cp_unevaluated_operand)
9726 if (!token->error_reported)
9728 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9729 "lambda-expression in unevaluated context");
9730 token->error_reported = true;
9732 ok = false;
9734 else if (parser->in_template_argument_list_p)
9736 if (!token->error_reported)
9738 error_at (token->location, "lambda-expression in template-argument");
9739 token->error_reported = true;
9741 ok = false;
9744 /* We may be in the middle of deferred access check. Disable
9745 it now. */
9746 push_deferring_access_checks (dk_no_deferred);
9748 cp_parser_lambda_introducer (parser, lambda_expr);
9750 type = begin_lambda_type (lambda_expr);
9751 if (type == error_mark_node)
9752 return error_mark_node;
9754 record_lambda_scope (lambda_expr);
9756 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9757 determine_visibility (TYPE_NAME (type));
9759 /* Now that we've started the type, add the capture fields for any
9760 explicit captures. */
9761 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9764 /* Inside the class, surrounding template-parameter-lists do not apply. */
9765 unsigned int saved_num_template_parameter_lists
9766 = parser->num_template_parameter_lists;
9767 unsigned char in_statement = parser->in_statement;
9768 bool in_switch_statement_p = parser->in_switch_statement_p;
9769 bool fully_implicit_function_template_p
9770 = parser->fully_implicit_function_template_p;
9771 tree implicit_template_parms = parser->implicit_template_parms;
9772 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9773 bool auto_is_implicit_function_template_parm_p
9774 = parser->auto_is_implicit_function_template_parm_p;
9776 parser->num_template_parameter_lists = 0;
9777 parser->in_statement = 0;
9778 parser->in_switch_statement_p = false;
9779 parser->fully_implicit_function_template_p = false;
9780 parser->implicit_template_parms = 0;
9781 parser->implicit_template_scope = 0;
9782 parser->auto_is_implicit_function_template_parm_p = false;
9784 /* By virtue of defining a local class, a lambda expression has access to
9785 the private variables of enclosing classes. */
9787 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9789 if (ok && cp_parser_error_occurred (parser))
9790 ok = false;
9792 if (ok)
9794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9795 && cp_parser_start_tentative_firewall (parser))
9796 start = token;
9797 cp_parser_lambda_body (parser, lambda_expr);
9799 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9801 if (cp_parser_skip_to_closing_brace (parser))
9802 cp_lexer_consume_token (parser->lexer);
9805 /* The capture list was built up in reverse order; fix that now. */
9806 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9807 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9809 if (ok)
9810 maybe_add_lambda_conv_op (type);
9812 type = finish_struct (type, /*attributes=*/NULL_TREE);
9814 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9815 parser->in_statement = in_statement;
9816 parser->in_switch_statement_p = in_switch_statement_p;
9817 parser->fully_implicit_function_template_p
9818 = fully_implicit_function_template_p;
9819 parser->implicit_template_parms = implicit_template_parms;
9820 parser->implicit_template_scope = implicit_template_scope;
9821 parser->auto_is_implicit_function_template_parm_p
9822 = auto_is_implicit_function_template_parm_p;
9825 /* This field is only used during parsing of the lambda. */
9826 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9828 /* This lambda shouldn't have any proxies left at this point. */
9829 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9830 /* And now that we're done, push proxies for an enclosing lambda. */
9831 insert_pending_capture_proxies ();
9833 if (ok)
9834 lambda_expr = build_lambda_object (lambda_expr);
9835 else
9836 lambda_expr = error_mark_node;
9838 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9840 pop_deferring_access_checks ();
9842 return lambda_expr;
9845 /* Parse the beginning of a lambda expression.
9847 lambda-introducer:
9848 [ lambda-capture [opt] ]
9850 LAMBDA_EXPR is the current representation of the lambda expression. */
9852 static void
9853 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9855 /* Need commas after the first capture. */
9856 bool first = true;
9858 /* Eat the leading `['. */
9859 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9861 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9862 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9863 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9864 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9865 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9866 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9868 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9870 cp_lexer_consume_token (parser->lexer);
9871 first = false;
9874 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9876 cp_token* capture_token;
9877 tree capture_id;
9878 tree capture_init_expr;
9879 cp_id_kind idk = CP_ID_KIND_NONE;
9880 bool explicit_init_p = false;
9882 enum capture_kind_type
9884 BY_COPY,
9885 BY_REFERENCE
9887 enum capture_kind_type capture_kind = BY_COPY;
9889 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9891 error ("expected end of capture-list");
9892 return;
9895 if (first)
9896 first = false;
9897 else
9898 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9900 /* Possibly capture `this'. */
9901 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9903 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9904 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9905 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9906 "with by-copy capture default");
9907 cp_lexer_consume_token (parser->lexer);
9908 add_capture (lambda_expr,
9909 /*id=*/this_identifier,
9910 /*initializer=*/finish_this_expr (),
9911 /*by_reference_p=*/true,
9912 explicit_init_p);
9913 continue;
9916 /* Possibly capture `*this'. */
9917 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
9918 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
9920 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9921 if (cxx_dialect < cxx1z)
9922 pedwarn (loc, 0, "%<*this%> capture only available with "
9923 "-std=c++1z or -std=gnu++1z");
9924 cp_lexer_consume_token (parser->lexer);
9925 cp_lexer_consume_token (parser->lexer);
9926 add_capture (lambda_expr,
9927 /*id=*/this_identifier,
9928 /*initializer=*/finish_this_expr (),
9929 /*by_reference_p=*/false,
9930 explicit_init_p);
9931 continue;
9934 /* Remember whether we want to capture as a reference or not. */
9935 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9937 capture_kind = BY_REFERENCE;
9938 cp_lexer_consume_token (parser->lexer);
9941 /* Get the identifier. */
9942 capture_token = cp_lexer_peek_token (parser->lexer);
9943 capture_id = cp_parser_identifier (parser);
9945 if (capture_id == error_mark_node)
9946 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9947 delimiters, but I modified this to stop on unnested ']' as well. It
9948 was already changed to stop on unnested '}', so the
9949 "closing_parenthesis" name is no more misleading with my change. */
9951 cp_parser_skip_to_closing_parenthesis (parser,
9952 /*recovering=*/true,
9953 /*or_comma=*/true,
9954 /*consume_paren=*/true);
9955 break;
9958 /* Find the initializer for this capture. */
9959 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9960 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9961 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9963 bool direct, non_constant;
9964 /* An explicit initializer exists. */
9965 if (cxx_dialect < cxx14)
9966 pedwarn (input_location, 0,
9967 "lambda capture initializers "
9968 "only available with -std=c++14 or -std=gnu++14");
9969 capture_init_expr = cp_parser_initializer (parser, &direct,
9970 &non_constant);
9971 explicit_init_p = true;
9972 if (capture_init_expr == NULL_TREE)
9974 error ("empty initializer for lambda init-capture");
9975 capture_init_expr = error_mark_node;
9978 else
9980 const char* error_msg;
9982 /* Turn the identifier into an id-expression. */
9983 capture_init_expr
9984 = cp_parser_lookup_name_simple (parser, capture_id,
9985 capture_token->location);
9987 if (capture_init_expr == error_mark_node)
9989 unqualified_name_lookup_error (capture_id);
9990 continue;
9992 else if (DECL_P (capture_init_expr)
9993 && (!VAR_P (capture_init_expr)
9994 && TREE_CODE (capture_init_expr) != PARM_DECL))
9996 error_at (capture_token->location,
9997 "capture of non-variable %qD ",
9998 capture_init_expr);
9999 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10000 "%q#D declared here", capture_init_expr);
10001 continue;
10003 if (VAR_P (capture_init_expr)
10004 && decl_storage_duration (capture_init_expr) != dk_auto)
10006 if (pedwarn (capture_token->location, 0, "capture of variable "
10007 "%qD with non-automatic storage duration",
10008 capture_init_expr))
10009 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10010 "%q#D declared here", capture_init_expr);
10011 continue;
10014 capture_init_expr
10015 = finish_id_expression
10016 (capture_id,
10017 capture_init_expr,
10018 parser->scope,
10019 &idk,
10020 /*integral_constant_expression_p=*/false,
10021 /*allow_non_integral_constant_expression_p=*/false,
10022 /*non_integral_constant_expression_p=*/NULL,
10023 /*template_p=*/false,
10024 /*done=*/true,
10025 /*address_p=*/false,
10026 /*template_arg_p=*/false,
10027 &error_msg,
10028 capture_token->location);
10030 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10032 cp_lexer_consume_token (parser->lexer);
10033 capture_init_expr = make_pack_expansion (capture_init_expr);
10035 else
10036 check_for_bare_parameter_packs (capture_init_expr);
10039 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10040 && !explicit_init_p)
10042 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10043 && capture_kind == BY_COPY)
10044 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10045 "of %qD redundant with by-copy capture default",
10046 capture_id);
10047 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10048 && capture_kind == BY_REFERENCE)
10049 pedwarn (capture_token->location, 0, "explicit by-reference "
10050 "capture of %qD redundant with by-reference capture "
10051 "default", capture_id);
10054 add_capture (lambda_expr,
10055 capture_id,
10056 capture_init_expr,
10057 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10058 explicit_init_p);
10061 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10064 /* Parse the (optional) middle of a lambda expression.
10066 lambda-declarator:
10067 < template-parameter-list [opt] >
10068 ( parameter-declaration-clause [opt] )
10069 attribute-specifier [opt]
10070 decl-specifier-seq [opt]
10071 exception-specification [opt]
10072 lambda-return-type-clause [opt]
10074 LAMBDA_EXPR is the current representation of the lambda expression. */
10076 static bool
10077 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10079 /* 5.1.1.4 of the standard says:
10080 If a lambda-expression does not include a lambda-declarator, it is as if
10081 the lambda-declarator were ().
10082 This means an empty parameter list, no attributes, and no exception
10083 specification. */
10084 tree param_list = void_list_node;
10085 tree attributes = NULL_TREE;
10086 tree exception_spec = NULL_TREE;
10087 tree template_param_list = NULL_TREE;
10088 tree tx_qual = NULL_TREE;
10089 cp_decl_specifier_seq lambda_specs;
10090 clear_decl_specs (&lambda_specs);
10092 /* The template-parameter-list is optional, but must begin with
10093 an opening angle if present. */
10094 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10096 if (cxx_dialect < cxx14)
10097 pedwarn (parser->lexer->next_token->location, 0,
10098 "lambda templates are only available with "
10099 "-std=c++14 or -std=gnu++14");
10101 cp_lexer_consume_token (parser->lexer);
10103 template_param_list = cp_parser_template_parameter_list (parser);
10105 cp_parser_skip_to_end_of_template_parameter_list (parser);
10107 /* We just processed one more parameter list. */
10108 ++parser->num_template_parameter_lists;
10111 /* The parameter-declaration-clause is optional (unless
10112 template-parameter-list was given), but must begin with an
10113 opening parenthesis if present. */
10114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10116 cp_lexer_consume_token (parser->lexer);
10118 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10120 /* Parse parameters. */
10121 param_list = cp_parser_parameter_declaration_clause (parser);
10123 /* Default arguments shall not be specified in the
10124 parameter-declaration-clause of a lambda-declarator. */
10125 if (cxx_dialect < cxx14)
10126 for (tree t = param_list; t; t = TREE_CHAIN (t))
10127 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10128 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10129 "default argument specified for lambda parameter");
10131 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10133 attributes = cp_parser_attributes_opt (parser);
10135 /* In the decl-specifier-seq of the lambda-declarator, each
10136 decl-specifier shall either be mutable or constexpr. */
10137 int declares_class_or_enum;
10138 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10139 cp_parser_decl_specifier_seq (parser,
10140 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10141 &lambda_specs, &declares_class_or_enum);
10142 if (lambda_specs.storage_class == sc_mutable)
10144 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10145 if (lambda_specs.conflicting_specifiers_p)
10146 error_at (lambda_specs.locations[ds_storage_class],
10147 "duplicate %<mutable%>");
10150 tx_qual = cp_parser_tx_qualifier_opt (parser);
10152 /* Parse optional exception specification. */
10153 exception_spec = cp_parser_exception_specification_opt (parser);
10155 /* Parse optional trailing return type. */
10156 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10158 cp_lexer_consume_token (parser->lexer);
10159 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10160 = cp_parser_trailing_type_id (parser);
10163 /* The function parameters must be in scope all the way until after the
10164 trailing-return-type in case of decltype. */
10165 pop_bindings_and_leave_scope ();
10167 else if (template_param_list != NULL_TREE) // generate diagnostic
10168 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10170 /* Create the function call operator.
10172 Messing with declarators like this is no uglier than building up the
10173 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10174 other code. */
10176 cp_decl_specifier_seq return_type_specs;
10177 cp_declarator* declarator;
10178 tree fco;
10179 int quals;
10180 void *p;
10182 clear_decl_specs (&return_type_specs);
10183 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10184 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10185 else
10186 /* Maybe we will deduce the return type later. */
10187 return_type_specs.type = make_auto ();
10189 if (lambda_specs.locations[ds_constexpr])
10191 if (cxx_dialect >= cxx1z)
10192 return_type_specs.locations[ds_constexpr]
10193 = lambda_specs.locations[ds_constexpr];
10194 else
10195 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10196 "lambda only available with -std=c++1z or -std=gnu++1z");
10199 p = obstack_alloc (&declarator_obstack, 0);
10201 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10202 sfk_none);
10204 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10205 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10206 declarator = make_call_declarator (declarator, param_list, quals,
10207 VIRT_SPEC_UNSPECIFIED,
10208 REF_QUAL_NONE,
10209 tx_qual,
10210 exception_spec,
10211 /*late_return_type=*/NULL_TREE,
10212 /*requires_clause*/NULL_TREE);
10213 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10215 fco = grokmethod (&return_type_specs,
10216 declarator,
10217 attributes);
10218 if (fco != error_mark_node)
10220 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10221 DECL_ARTIFICIAL (fco) = 1;
10222 /* Give the object parameter a different name. */
10223 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10224 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10225 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10227 if (template_param_list)
10229 fco = finish_member_template_decl (fco);
10230 finish_template_decl (template_param_list);
10231 --parser->num_template_parameter_lists;
10233 else if (parser->fully_implicit_function_template_p)
10234 fco = finish_fully_implicit_template (parser, fco);
10236 finish_member_declaration (fco);
10238 obstack_free (&declarator_obstack, p);
10240 return (fco != error_mark_node);
10244 /* Parse the body of a lambda expression, which is simply
10246 compound-statement
10248 but which requires special handling.
10249 LAMBDA_EXPR is the current representation of the lambda expression. */
10251 static void
10252 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10254 bool nested = (current_function_decl != NULL_TREE);
10255 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10256 if (nested)
10257 push_function_context ();
10258 else
10259 /* Still increment function_depth so that we don't GC in the
10260 middle of an expression. */
10261 ++function_depth;
10262 vec<tree> omp_privatization_save;
10263 save_omp_privatization_clauses (omp_privatization_save);
10264 /* Clear this in case we're in the middle of a default argument. */
10265 parser->local_variables_forbidden_p = false;
10267 /* Finish the function call operator
10268 - class_specifier
10269 + late_parsing_for_member
10270 + function_definition_after_declarator
10271 + ctor_initializer_opt_and_function_body */
10273 tree fco = lambda_function (lambda_expr);
10274 tree body;
10275 bool done = false;
10276 tree compound_stmt;
10277 tree cap;
10279 /* Let the front end know that we are going to be defining this
10280 function. */
10281 start_preparsed_function (fco,
10282 NULL_TREE,
10283 SF_PRE_PARSED | SF_INCLASS_INLINE);
10285 start_lambda_scope (fco);
10286 body = begin_function_body ();
10288 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10289 goto out;
10291 /* Push the proxies for any explicit captures. */
10292 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10293 cap = TREE_CHAIN (cap))
10294 build_capture_proxy (TREE_PURPOSE (cap));
10296 compound_stmt = begin_compound_stmt (0);
10298 /* 5.1.1.4 of the standard says:
10299 If a lambda-expression does not include a trailing-return-type, it
10300 is as if the trailing-return-type denotes the following type:
10301 * if the compound-statement is of the form
10302 { return attribute-specifier [opt] expression ; }
10303 the type of the returned expression after lvalue-to-rvalue
10304 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10305 (_conv.array_ 4.2), and function-to-pointer conversion
10306 (_conv.func_ 4.3);
10307 * otherwise, void. */
10309 /* In a lambda that has neither a lambda-return-type-clause
10310 nor a deducible form, errors should be reported for return statements
10311 in the body. Since we used void as the placeholder return type, parsing
10312 the body as usual will give such desired behavior. */
10313 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10314 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10315 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10317 tree expr = NULL_TREE;
10318 cp_id_kind idk = CP_ID_KIND_NONE;
10320 /* Parse tentatively in case there's more after the initial return
10321 statement. */
10322 cp_parser_parse_tentatively (parser);
10324 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10326 expr = cp_parser_expression (parser, &idk);
10328 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10329 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10331 if (cp_parser_parse_definitely (parser))
10333 if (!processing_template_decl)
10335 tree type = lambda_return_type (expr);
10336 apply_deduced_return_type (fco, type);
10337 if (type == error_mark_node)
10338 expr = error_mark_node;
10341 /* Will get error here if type not deduced yet. */
10342 finish_return_stmt (expr);
10344 done = true;
10348 if (!done)
10350 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10351 cp_parser_label_declaration (parser);
10352 cp_parser_statement_seq_opt (parser, NULL_TREE);
10353 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10356 finish_compound_stmt (compound_stmt);
10358 out:
10359 finish_function_body (body);
10360 finish_lambda_scope ();
10362 /* Finish the function and generate code for it if necessary. */
10363 tree fn = finish_function (/*inline*/2);
10365 /* Only expand if the call op is not a template. */
10366 if (!DECL_TEMPLATE_INFO (fco))
10367 expand_or_defer_fn (fn);
10370 restore_omp_privatization_clauses (omp_privatization_save);
10371 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10372 if (nested)
10373 pop_function_context();
10374 else
10375 --function_depth;
10378 /* Statements [gram.stmt.stmt] */
10380 /* Parse a statement.
10382 statement:
10383 labeled-statement
10384 expression-statement
10385 compound-statement
10386 selection-statement
10387 iteration-statement
10388 jump-statement
10389 declaration-statement
10390 try-block
10392 C++11:
10394 statement:
10395 labeled-statement
10396 attribute-specifier-seq (opt) expression-statement
10397 attribute-specifier-seq (opt) compound-statement
10398 attribute-specifier-seq (opt) selection-statement
10399 attribute-specifier-seq (opt) iteration-statement
10400 attribute-specifier-seq (opt) jump-statement
10401 declaration-statement
10402 attribute-specifier-seq (opt) try-block
10404 init-statement:
10405 expression-statement
10406 simple-declaration
10408 TM Extension:
10410 statement:
10411 atomic-statement
10413 IN_COMPOUND is true when the statement is nested inside a
10414 cp_parser_compound_statement; this matters for certain pragmas.
10416 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10417 is a (possibly labeled) if statement which is not enclosed in braces
10418 and has an else clause. This is used to implement -Wparentheses.
10420 CHAIN is a vector of if-else-if conditions. */
10422 static void
10423 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10424 bool in_compound, bool *if_p, vec<tree> *chain)
10426 tree statement, std_attrs = NULL_TREE;
10427 cp_token *token;
10428 location_t statement_location, attrs_location;
10430 restart:
10431 if (if_p != NULL)
10432 *if_p = false;
10433 /* There is no statement yet. */
10434 statement = NULL_TREE;
10436 saved_token_sentinel saved_tokens (parser->lexer);
10437 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10438 if (c_dialect_objc ())
10439 /* In obj-c++, seeing '[[' might be the either the beginning of
10440 c++11 attributes, or a nested objc-message-expression. So
10441 let's parse the c++11 attributes tentatively. */
10442 cp_parser_parse_tentatively (parser);
10443 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10444 if (c_dialect_objc ())
10446 if (!cp_parser_parse_definitely (parser))
10447 std_attrs = NULL_TREE;
10450 /* Peek at the next token. */
10451 token = cp_lexer_peek_token (parser->lexer);
10452 /* Remember the location of the first token in the statement. */
10453 statement_location = token->location;
10454 /* If this is a keyword, then that will often determine what kind of
10455 statement we have. */
10456 if (token->type == CPP_KEYWORD)
10458 enum rid keyword = token->keyword;
10460 switch (keyword)
10462 case RID_CASE:
10463 case RID_DEFAULT:
10464 /* Looks like a labeled-statement with a case label.
10465 Parse the label, and then use tail recursion to parse
10466 the statement. */
10467 cp_parser_label_for_labeled_statement (parser, std_attrs);
10468 in_compound = false;
10469 goto restart;
10471 case RID_IF:
10472 case RID_SWITCH:
10473 statement = cp_parser_selection_statement (parser, if_p, chain);
10474 break;
10476 case RID_WHILE:
10477 case RID_DO:
10478 case RID_FOR:
10479 statement = cp_parser_iteration_statement (parser, if_p, false);
10480 break;
10482 case RID_CILK_FOR:
10483 if (!flag_cilkplus)
10485 error_at (cp_lexer_peek_token (parser->lexer)->location,
10486 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10487 cp_lexer_consume_token (parser->lexer);
10488 statement = error_mark_node;
10490 else
10491 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10492 break;
10494 case RID_BREAK:
10495 case RID_CONTINUE:
10496 case RID_RETURN:
10497 case RID_GOTO:
10498 statement = cp_parser_jump_statement (parser);
10499 break;
10501 case RID_CILK_SYNC:
10502 cp_lexer_consume_token (parser->lexer);
10503 if (flag_cilkplus)
10505 tree sync_expr = build_cilk_sync ();
10506 SET_EXPR_LOCATION (sync_expr,
10507 token->location);
10508 statement = finish_expr_stmt (sync_expr);
10510 else
10512 error_at (token->location, "-fcilkplus must be enabled to use"
10513 " %<_Cilk_sync%>");
10514 statement = error_mark_node;
10516 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10517 break;
10519 /* Objective-C++ exception-handling constructs. */
10520 case RID_AT_TRY:
10521 case RID_AT_CATCH:
10522 case RID_AT_FINALLY:
10523 case RID_AT_SYNCHRONIZED:
10524 case RID_AT_THROW:
10525 statement = cp_parser_objc_statement (parser);
10526 break;
10528 case RID_TRY:
10529 statement = cp_parser_try_block (parser);
10530 break;
10532 case RID_NAMESPACE:
10533 /* This must be a namespace alias definition. */
10534 cp_parser_declaration_statement (parser);
10535 return;
10537 case RID_TRANSACTION_ATOMIC:
10538 case RID_TRANSACTION_RELAXED:
10539 case RID_SYNCHRONIZED:
10540 case RID_ATOMIC_NOEXCEPT:
10541 case RID_ATOMIC_CANCEL:
10542 statement = cp_parser_transaction (parser, token);
10543 break;
10544 case RID_TRANSACTION_CANCEL:
10545 statement = cp_parser_transaction_cancel (parser);
10546 break;
10548 default:
10549 /* It might be a keyword like `int' that can start a
10550 declaration-statement. */
10551 break;
10554 else if (token->type == CPP_NAME)
10556 /* If the next token is a `:', then we are looking at a
10557 labeled-statement. */
10558 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10559 if (token->type == CPP_COLON)
10561 /* Looks like a labeled-statement with an ordinary label.
10562 Parse the label, and then use tail recursion to parse
10563 the statement. */
10565 cp_parser_label_for_labeled_statement (parser, std_attrs);
10566 in_compound = false;
10567 goto restart;
10570 /* Anything that starts with a `{' must be a compound-statement. */
10571 else if (token->type == CPP_OPEN_BRACE)
10572 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10573 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10574 a statement all its own. */
10575 else if (token->type == CPP_PRAGMA)
10577 /* Only certain OpenMP pragmas are attached to statements, and thus
10578 are considered statements themselves. All others are not. In
10579 the context of a compound, accept the pragma as a "statement" and
10580 return so that we can check for a close brace. Otherwise we
10581 require a real statement and must go back and read one. */
10582 if (in_compound)
10583 cp_parser_pragma (parser, pragma_compound, if_p);
10584 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10585 goto restart;
10586 return;
10588 else if (token->type == CPP_EOF)
10590 cp_parser_error (parser, "expected statement");
10591 return;
10594 /* Everything else must be a declaration-statement or an
10595 expression-statement. Try for the declaration-statement
10596 first, unless we are looking at a `;', in which case we know that
10597 we have an expression-statement. */
10598 if (!statement)
10600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10602 if (std_attrs != NULL_TREE)
10604 /* Attributes should be parsed as part of the the
10605 declaration, so let's un-parse them. */
10606 saved_tokens.rollback();
10607 std_attrs = NULL_TREE;
10610 cp_parser_parse_tentatively (parser);
10611 /* Try to parse the declaration-statement. */
10612 cp_parser_declaration_statement (parser);
10613 /* If that worked, we're done. */
10614 if (cp_parser_parse_definitely (parser))
10615 return;
10617 /* Look for an expression-statement instead. */
10618 statement = cp_parser_expression_statement (parser, in_statement_expr);
10620 /* Handle [[fallthrough]];. */
10621 if (attribute_fallthrough_p (std_attrs))
10623 /* The next token after the fallthrough attribute is ';'. */
10624 if (statement == NULL_TREE)
10626 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10627 statement = build_call_expr_internal_loc (statement_location,
10628 IFN_FALLTHROUGH,
10629 void_type_node, 0);
10630 finish_expr_stmt (statement);
10632 else
10633 warning_at (statement_location, OPT_Wattributes,
10634 "%<fallthrough%> attribute not followed by %<;%>");
10635 std_attrs = NULL_TREE;
10639 /* Set the line number for the statement. */
10640 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10641 SET_EXPR_LOCATION (statement, statement_location);
10643 /* Allow "[[fallthrough]];", but warn otherwise. */
10644 if (std_attrs != NULL_TREE)
10645 warning_at (attrs_location,
10646 OPT_Wattributes,
10647 "attributes at the beginning of statement are ignored");
10650 /* Parse the label for a labeled-statement, i.e.
10652 identifier :
10653 case constant-expression :
10654 default :
10656 GNU Extension:
10657 case constant-expression ... constant-expression : statement
10659 When a label is parsed without errors, the label is added to the
10660 parse tree by the finish_* functions, so this function doesn't
10661 have to return the label. */
10663 static void
10664 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10666 cp_token *token;
10667 tree label = NULL_TREE;
10668 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10670 /* The next token should be an identifier. */
10671 token = cp_lexer_peek_token (parser->lexer);
10672 if (token->type != CPP_NAME
10673 && token->type != CPP_KEYWORD)
10675 cp_parser_error (parser, "expected labeled-statement");
10676 return;
10679 /* Remember whether this case or a user-defined label is allowed to fall
10680 through to. */
10681 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10683 parser->colon_corrects_to_scope_p = false;
10684 switch (token->keyword)
10686 case RID_CASE:
10688 tree expr, expr_hi;
10689 cp_token *ellipsis;
10691 /* Consume the `case' token. */
10692 cp_lexer_consume_token (parser->lexer);
10693 /* Parse the constant-expression. */
10694 expr = cp_parser_constant_expression (parser);
10695 if (check_for_bare_parameter_packs (expr))
10696 expr = error_mark_node;
10698 ellipsis = cp_lexer_peek_token (parser->lexer);
10699 if (ellipsis->type == CPP_ELLIPSIS)
10701 /* Consume the `...' token. */
10702 cp_lexer_consume_token (parser->lexer);
10703 expr_hi = cp_parser_constant_expression (parser);
10704 if (check_for_bare_parameter_packs (expr_hi))
10705 expr_hi = error_mark_node;
10707 /* We don't need to emit warnings here, as the common code
10708 will do this for us. */
10710 else
10711 expr_hi = NULL_TREE;
10713 if (parser->in_switch_statement_p)
10715 tree l = finish_case_label (token->location, expr, expr_hi);
10716 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10717 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10719 else
10720 error_at (token->location,
10721 "case label %qE not within a switch statement",
10722 expr);
10724 break;
10726 case RID_DEFAULT:
10727 /* Consume the `default' token. */
10728 cp_lexer_consume_token (parser->lexer);
10730 if (parser->in_switch_statement_p)
10732 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10733 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10734 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10736 else
10737 error_at (token->location, "case label not within a switch statement");
10738 break;
10740 default:
10741 /* Anything else must be an ordinary label. */
10742 label = finish_label_stmt (cp_parser_identifier (parser));
10743 if (label && TREE_CODE (label) == LABEL_DECL)
10744 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10745 break;
10748 /* Require the `:' token. */
10749 cp_parser_require (parser, CPP_COLON, RT_COLON);
10751 /* An ordinary label may optionally be followed by attributes.
10752 However, this is only permitted if the attributes are then
10753 followed by a semicolon. This is because, for backward
10754 compatibility, when parsing
10755 lab: __attribute__ ((unused)) int i;
10756 we want the attribute to attach to "i", not "lab". */
10757 if (label != NULL_TREE
10758 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10760 tree attrs;
10761 cp_parser_parse_tentatively (parser);
10762 attrs = cp_parser_gnu_attributes_opt (parser);
10763 if (attrs == NULL_TREE
10764 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10765 cp_parser_abort_tentative_parse (parser);
10766 else if (!cp_parser_parse_definitely (parser))
10768 else
10769 attributes = chainon (attributes, attrs);
10772 if (attributes != NULL_TREE)
10773 cplus_decl_attributes (&label, attributes, 0);
10775 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10778 /* Parse an expression-statement.
10780 expression-statement:
10781 expression [opt] ;
10783 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10784 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10785 indicates whether this expression-statement is part of an
10786 expression statement. */
10788 static tree
10789 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10791 tree statement = NULL_TREE;
10792 cp_token *token = cp_lexer_peek_token (parser->lexer);
10793 location_t loc = token->location;
10795 /* There might be attribute fallthrough. */
10796 tree attr = cp_parser_gnu_attributes_opt (parser);
10798 /* If the next token is a ';', then there is no expression
10799 statement. */
10800 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10802 statement = cp_parser_expression (parser);
10803 if (statement == error_mark_node
10804 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10806 cp_parser_skip_to_end_of_block_or_statement (parser);
10807 return error_mark_node;
10811 /* Handle [[fallthrough]];. */
10812 if (attribute_fallthrough_p (attr))
10814 /* The next token after the fallthrough attribute is ';'. */
10815 if (statement == NULL_TREE)
10816 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10817 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10818 void_type_node, 0);
10819 else
10820 warning_at (loc, OPT_Wattributes,
10821 "%<fallthrough%> attribute not followed by %<;%>");
10822 attr = NULL_TREE;
10825 /* Allow "[[fallthrough]];", but warn otherwise. */
10826 if (attr != NULL_TREE)
10827 warning_at (loc, OPT_Wattributes,
10828 "attributes at the beginning of statement are ignored");
10830 /* Give a helpful message for "A<T>::type t;" and the like. */
10831 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10832 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10834 if (TREE_CODE (statement) == SCOPE_REF)
10835 error_at (token->location, "need %<typename%> before %qE because "
10836 "%qT is a dependent scope",
10837 statement, TREE_OPERAND (statement, 0));
10838 else if (is_overloaded_fn (statement)
10839 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10841 /* A::A a; */
10842 tree fn = get_first_fn (statement);
10843 error_at (token->location,
10844 "%<%T::%D%> names the constructor, not the type",
10845 DECL_CONTEXT (fn), DECL_NAME (fn));
10849 /* Consume the final `;'. */
10850 cp_parser_consume_semicolon_at_end_of_statement (parser);
10852 if (in_statement_expr
10853 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10854 /* This is the final expression statement of a statement
10855 expression. */
10856 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10857 else if (statement)
10858 statement = finish_expr_stmt (statement);
10860 return statement;
10863 /* Parse a compound-statement.
10865 compound-statement:
10866 { statement-seq [opt] }
10868 GNU extension:
10870 compound-statement:
10871 { label-declaration-seq [opt] statement-seq [opt] }
10873 label-declaration-seq:
10874 label-declaration
10875 label-declaration-seq label-declaration
10877 Returns a tree representing the statement. */
10879 static tree
10880 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10881 int bcs_flags, bool function_body)
10883 tree compound_stmt;
10885 /* Consume the `{'. */
10886 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10887 return error_mark_node;
10888 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10889 && !function_body && cxx_dialect < cxx14)
10890 pedwarn (input_location, OPT_Wpedantic,
10891 "compound-statement in constexpr function");
10892 /* Begin the compound-statement. */
10893 compound_stmt = begin_compound_stmt (bcs_flags);
10894 /* If the next keyword is `__label__' we have a label declaration. */
10895 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10896 cp_parser_label_declaration (parser);
10897 /* Parse an (optional) statement-seq. */
10898 cp_parser_statement_seq_opt (parser, in_statement_expr);
10899 /* Finish the compound-statement. */
10900 finish_compound_stmt (compound_stmt);
10901 /* Consume the `}'. */
10902 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10904 return compound_stmt;
10907 /* Parse an (optional) statement-seq.
10909 statement-seq:
10910 statement
10911 statement-seq [opt] statement */
10913 static void
10914 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10916 /* Scan statements until there aren't any more. */
10917 while (true)
10919 cp_token *token = cp_lexer_peek_token (parser->lexer);
10921 /* If we are looking at a `}', then we have run out of
10922 statements; the same is true if we have reached the end
10923 of file, or have stumbled upon a stray '@end'. */
10924 if (token->type == CPP_CLOSE_BRACE
10925 || token->type == CPP_EOF
10926 || token->type == CPP_PRAGMA_EOL
10927 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10928 break;
10930 /* If we are in a compound statement and find 'else' then
10931 something went wrong. */
10932 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10934 if (parser->in_statement & IN_IF_STMT)
10935 break;
10936 else
10938 token = cp_lexer_consume_token (parser->lexer);
10939 error_at (token->location, "%<else%> without a previous %<if%>");
10943 /* Parse the statement. */
10944 cp_parser_statement (parser, in_statement_expr, true, NULL);
10948 /* Return true if we're looking at (init; cond), false otherwise. */
10950 static bool
10951 cp_parser_init_statement_p (cp_parser *parser)
10953 /* Save tokens so that we can put them back. */
10954 cp_lexer_save_tokens (parser->lexer);
10956 /* Look for ';' that is not nested in () or {}. */
10957 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
10958 /*recovering=*/false,
10959 CPP_SEMICOLON,
10960 /*consume_paren=*/false);
10962 /* Roll back the tokens we skipped. */
10963 cp_lexer_rollback_tokens (parser->lexer);
10965 return ret == -1;
10968 /* Parse a selection-statement.
10970 selection-statement:
10971 if ( init-statement [opt] condition ) statement
10972 if ( init-statement [opt] condition ) statement else statement
10973 switch ( init-statement [opt] condition ) statement
10975 Returns the new IF_STMT or SWITCH_STMT.
10977 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10978 is a (possibly labeled) if statement which is not enclosed in
10979 braces and has an else clause. This is used to implement
10980 -Wparentheses.
10982 CHAIN is a vector of if-else-if conditions. This is used to implement
10983 -Wduplicated-cond. */
10985 static tree
10986 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10987 vec<tree> *chain)
10989 cp_token *token;
10990 enum rid keyword;
10991 token_indent_info guard_tinfo;
10993 if (if_p != NULL)
10994 *if_p = false;
10996 /* Peek at the next token. */
10997 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10998 guard_tinfo = get_token_indent_info (token);
11000 /* See what kind of keyword it is. */
11001 keyword = token->keyword;
11002 switch (keyword)
11004 case RID_IF:
11005 case RID_SWITCH:
11007 tree statement;
11008 tree condition;
11010 bool cx = false;
11011 if (keyword == RID_IF
11012 && cp_lexer_next_token_is_keyword (parser->lexer,
11013 RID_CONSTEXPR))
11015 cx = true;
11016 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11017 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11018 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11019 "with -std=c++1z or -std=gnu++1z");
11022 /* Look for the `('. */
11023 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11025 cp_parser_skip_to_end_of_statement (parser);
11026 return error_mark_node;
11029 /* Begin the selection-statement. */
11030 if (keyword == RID_IF)
11032 statement = begin_if_stmt ();
11033 IF_STMT_CONSTEXPR_P (statement) = cx;
11035 else
11036 statement = begin_switch_stmt ();
11038 /* Parse the optional init-statement. */
11039 if (cp_parser_init_statement_p (parser))
11041 tree decl;
11042 if (cxx_dialect < cxx1z)
11043 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11044 "init-statement in selection statements only available "
11045 "with -std=c++1z or -std=gnu++1z");
11046 cp_parser_init_statement (parser, &decl);
11049 /* Parse the condition. */
11050 condition = cp_parser_condition (parser);
11051 /* Look for the `)'. */
11052 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11053 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11054 /*consume_paren=*/true);
11056 if (keyword == RID_IF)
11058 bool nested_if;
11059 unsigned char in_statement;
11061 /* Add the condition. */
11062 condition = finish_if_stmt_cond (condition, statement);
11064 if (warn_duplicated_cond)
11065 warn_duplicated_cond_add_or_warn (token->location, condition,
11066 &chain);
11068 /* Parse the then-clause. */
11069 in_statement = parser->in_statement;
11070 parser->in_statement |= IN_IF_STMT;
11072 /* Outside a template, the non-selected branch of a constexpr
11073 if is a 'discarded statement', i.e. unevaluated. */
11074 bool was_discarded = parser->in_discarded_stmt;
11075 bool discard_then = (cx && !processing_template_decl
11076 && integer_zerop (condition));
11077 if (discard_then)
11079 parser->in_discarded_stmt = true;
11080 ++c_inhibit_evaluation_warnings;
11083 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11084 guard_tinfo);
11086 parser->in_statement = in_statement;
11088 finish_then_clause (statement);
11090 if (discard_then)
11092 THEN_CLAUSE (statement) = NULL_TREE;
11093 parser->in_discarded_stmt = was_discarded;
11094 --c_inhibit_evaluation_warnings;
11097 /* If the next token is `else', parse the else-clause. */
11098 if (cp_lexer_next_token_is_keyword (parser->lexer,
11099 RID_ELSE))
11101 bool discard_else = (cx && !processing_template_decl
11102 && integer_nonzerop (condition));
11103 if (discard_else)
11105 parser->in_discarded_stmt = true;
11106 ++c_inhibit_evaluation_warnings;
11109 guard_tinfo
11110 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11111 /* Consume the `else' keyword. */
11112 cp_lexer_consume_token (parser->lexer);
11113 if (warn_duplicated_cond)
11115 if (cp_lexer_next_token_is_keyword (parser->lexer,
11116 RID_IF)
11117 && chain == NULL)
11119 /* We've got "if (COND) else if (COND2)". Start
11120 the condition chain and add COND as the first
11121 element. */
11122 chain = new vec<tree> ();
11123 if (!CONSTANT_CLASS_P (condition)
11124 && !TREE_SIDE_EFFECTS (condition))
11126 /* Wrap it in a NOP_EXPR so that we can set the
11127 location of the condition. */
11128 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11129 condition);
11130 SET_EXPR_LOCATION (e, token->location);
11131 chain->safe_push (e);
11134 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11135 RID_IF))
11137 /* This is if-else without subsequent if. Zap the
11138 condition chain; we would have already warned at
11139 this point. */
11140 delete chain;
11141 chain = NULL;
11144 begin_else_clause (statement);
11145 /* Parse the else-clause. */
11146 cp_parser_implicitly_scoped_statement (parser, NULL,
11147 guard_tinfo, chain);
11149 finish_else_clause (statement);
11151 /* If we are currently parsing a then-clause, then
11152 IF_P will not be NULL. We set it to true to
11153 indicate that this if statement has an else clause.
11154 This may trigger the Wparentheses warning below
11155 when we get back up to the parent if statement. */
11156 if (if_p != NULL)
11157 *if_p = true;
11159 if (discard_else)
11161 ELSE_CLAUSE (statement) = NULL_TREE;
11162 parser->in_discarded_stmt = was_discarded;
11163 --c_inhibit_evaluation_warnings;
11166 else
11168 /* This if statement does not have an else clause. If
11169 NESTED_IF is true, then the then-clause has an if
11170 statement which does have an else clause. We warn
11171 about the potential ambiguity. */
11172 if (nested_if)
11173 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11174 "suggest explicit braces to avoid ambiguous"
11175 " %<else%>");
11176 if (warn_duplicated_cond)
11178 /* We don't need the condition chain anymore. */
11179 delete chain;
11180 chain = NULL;
11184 /* Now we're all done with the if-statement. */
11185 finish_if_stmt (statement);
11187 else
11189 bool in_switch_statement_p;
11190 unsigned char in_statement;
11192 /* Add the condition. */
11193 finish_switch_cond (condition, statement);
11195 /* Parse the body of the switch-statement. */
11196 in_switch_statement_p = parser->in_switch_statement_p;
11197 in_statement = parser->in_statement;
11198 parser->in_switch_statement_p = true;
11199 parser->in_statement |= IN_SWITCH_STMT;
11200 cp_parser_implicitly_scoped_statement (parser, if_p,
11201 guard_tinfo);
11202 parser->in_switch_statement_p = in_switch_statement_p;
11203 parser->in_statement = in_statement;
11205 /* Now we're all done with the switch-statement. */
11206 finish_switch_stmt (statement);
11209 return statement;
11211 break;
11213 default:
11214 cp_parser_error (parser, "expected selection-statement");
11215 return error_mark_node;
11219 /* Parse a condition.
11221 condition:
11222 expression
11223 type-specifier-seq declarator = initializer-clause
11224 type-specifier-seq declarator braced-init-list
11226 GNU Extension:
11228 condition:
11229 type-specifier-seq declarator asm-specification [opt]
11230 attributes [opt] = assignment-expression
11232 Returns the expression that should be tested. */
11234 static tree
11235 cp_parser_condition (cp_parser* parser)
11237 cp_decl_specifier_seq type_specifiers;
11238 const char *saved_message;
11239 int declares_class_or_enum;
11241 /* Try the declaration first. */
11242 cp_parser_parse_tentatively (parser);
11243 /* New types are not allowed in the type-specifier-seq for a
11244 condition. */
11245 saved_message = parser->type_definition_forbidden_message;
11246 parser->type_definition_forbidden_message
11247 = G_("types may not be defined in conditions");
11248 /* Parse the type-specifier-seq. */
11249 cp_parser_decl_specifier_seq (parser,
11250 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11251 &type_specifiers,
11252 &declares_class_or_enum);
11253 /* Restore the saved message. */
11254 parser->type_definition_forbidden_message = saved_message;
11255 /* If all is well, we might be looking at a declaration. */
11256 if (!cp_parser_error_occurred (parser))
11258 tree decl;
11259 tree asm_specification;
11260 tree attributes;
11261 cp_declarator *declarator;
11262 tree initializer = NULL_TREE;
11264 /* Parse the declarator. */
11265 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11266 /*ctor_dtor_or_conv_p=*/NULL,
11267 /*parenthesized_p=*/NULL,
11268 /*member_p=*/false,
11269 /*friend_p=*/false);
11270 /* Parse the attributes. */
11271 attributes = cp_parser_attributes_opt (parser);
11272 /* Parse the asm-specification. */
11273 asm_specification = cp_parser_asm_specification_opt (parser);
11274 /* If the next token is not an `=' or '{', then we might still be
11275 looking at an expression. For example:
11277 if (A(a).x)
11279 looks like a decl-specifier-seq and a declarator -- but then
11280 there is no `=', so this is an expression. */
11281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11282 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11283 cp_parser_simulate_error (parser);
11285 /* If we did see an `=' or '{', then we are looking at a declaration
11286 for sure. */
11287 if (cp_parser_parse_definitely (parser))
11289 tree pushed_scope;
11290 bool non_constant_p;
11291 int flags = LOOKUP_ONLYCONVERTING;
11293 /* Create the declaration. */
11294 decl = start_decl (declarator, &type_specifiers,
11295 /*initialized_p=*/true,
11296 attributes, /*prefix_attributes=*/NULL_TREE,
11297 &pushed_scope);
11299 /* Parse the initializer. */
11300 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11302 initializer = cp_parser_braced_list (parser, &non_constant_p);
11303 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11304 flags = 0;
11306 else
11308 /* Consume the `='. */
11309 cp_parser_require (parser, CPP_EQ, RT_EQ);
11310 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11312 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11313 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11315 /* Process the initializer. */
11316 cp_finish_decl (decl,
11317 initializer, !non_constant_p,
11318 asm_specification,
11319 flags);
11321 if (pushed_scope)
11322 pop_scope (pushed_scope);
11324 return convert_from_reference (decl);
11327 /* If we didn't even get past the declarator successfully, we are
11328 definitely not looking at a declaration. */
11329 else
11330 cp_parser_abort_tentative_parse (parser);
11332 /* Otherwise, we are looking at an expression. */
11333 return cp_parser_expression (parser);
11336 /* Parses a for-statement or range-for-statement until the closing ')',
11337 not included. */
11339 static tree
11340 cp_parser_for (cp_parser *parser, bool ivdep)
11342 tree init, scope, decl;
11343 bool is_range_for;
11345 /* Begin the for-statement. */
11346 scope = begin_for_scope (&init);
11348 /* Parse the initialization. */
11349 is_range_for = cp_parser_init_statement (parser, &decl);
11351 if (is_range_for)
11352 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11353 else
11354 return cp_parser_c_for (parser, scope, init, ivdep);
11357 static tree
11358 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11360 /* Normal for loop */
11361 tree condition = NULL_TREE;
11362 tree expression = NULL_TREE;
11363 tree stmt;
11365 stmt = begin_for_stmt (scope, init);
11366 /* The init-statement has already been parsed in
11367 cp_parser_init_statement, so no work is needed here. */
11368 finish_init_stmt (stmt);
11370 /* If there's a condition, process it. */
11371 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11372 condition = cp_parser_condition (parser);
11373 else if (ivdep)
11375 cp_parser_error (parser, "missing loop condition in loop with "
11376 "%<GCC ivdep%> pragma");
11377 condition = error_mark_node;
11379 finish_for_cond (condition, stmt, ivdep);
11380 /* Look for the `;'. */
11381 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11383 /* If there's an expression, process it. */
11384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11385 expression = cp_parser_expression (parser);
11386 finish_for_expr (expression, stmt);
11388 return stmt;
11391 /* Tries to parse a range-based for-statement:
11393 range-based-for:
11394 decl-specifier-seq declarator : expression
11396 The decl-specifier-seq declarator and the `:' are already parsed by
11397 cp_parser_init_statement. If processing_template_decl it returns a
11398 newly created RANGE_FOR_STMT; if not, it is converted to a
11399 regular FOR_STMT. */
11401 static tree
11402 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11403 bool ivdep)
11405 tree stmt, range_expr;
11406 cxx_binding *binding = NULL;
11407 tree name = NULL_TREE;
11409 /* Get the range declaration momentarily out of the way so that
11410 the range expression doesn't clash with it. */
11411 if (range_decl != error_mark_node)
11413 name = DECL_NAME (range_decl);
11414 binding = IDENTIFIER_BINDING (name);
11415 IDENTIFIER_BINDING (name) = binding->previous;
11418 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11420 bool expr_non_constant_p;
11421 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11423 else
11424 range_expr = cp_parser_expression (parser);
11426 /* Put the range declaration back into scope. */
11427 if (range_decl != error_mark_node)
11429 binding->previous = IDENTIFIER_BINDING (name);
11430 IDENTIFIER_BINDING (name) = binding;
11433 /* If in template, STMT is converted to a normal for-statement
11434 at instantiation. If not, it is done just ahead. */
11435 if (processing_template_decl)
11437 if (check_for_bare_parameter_packs (range_expr))
11438 range_expr = error_mark_node;
11439 stmt = begin_range_for_stmt (scope, init);
11440 if (ivdep)
11441 RANGE_FOR_IVDEP (stmt) = 1;
11442 finish_range_for_decl (stmt, range_decl, range_expr);
11443 if (!type_dependent_expression_p (range_expr)
11444 /* do_auto_deduction doesn't mess with template init-lists. */
11445 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11446 do_range_for_auto_deduction (range_decl, range_expr);
11448 else
11450 stmt = begin_for_stmt (scope, init);
11451 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11453 return stmt;
11456 /* Subroutine of cp_convert_range_for: given the initializer expression,
11457 builds up the range temporary. */
11459 static tree
11460 build_range_temp (tree range_expr)
11462 tree range_type, range_temp;
11464 /* Find out the type deduced by the declaration
11465 `auto &&__range = range_expr'. */
11466 range_type = cp_build_reference_type (make_auto (), true);
11467 range_type = do_auto_deduction (range_type, range_expr,
11468 type_uses_auto (range_type));
11470 /* Create the __range variable. */
11471 range_temp = build_decl (input_location, VAR_DECL,
11472 get_identifier ("__for_range"), range_type);
11473 TREE_USED (range_temp) = 1;
11474 DECL_ARTIFICIAL (range_temp) = 1;
11476 return range_temp;
11479 /* Used by cp_parser_range_for in template context: we aren't going to
11480 do a full conversion yet, but we still need to resolve auto in the
11481 type of the for-range-declaration if present. This is basically
11482 a shortcut version of cp_convert_range_for. */
11484 static void
11485 do_range_for_auto_deduction (tree decl, tree range_expr)
11487 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11488 if (auto_node)
11490 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11491 range_temp = convert_from_reference (build_range_temp (range_expr));
11492 iter_type = (cp_parser_perform_range_for_lookup
11493 (range_temp, &begin_dummy, &end_dummy));
11494 if (iter_type)
11496 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11497 iter_type);
11498 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11499 tf_warning_or_error);
11500 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11501 iter_decl, auto_node);
11506 /* Converts a range-based for-statement into a normal
11507 for-statement, as per the definition.
11509 for (RANGE_DECL : RANGE_EXPR)
11510 BLOCK
11512 should be equivalent to:
11515 auto &&__range = RANGE_EXPR;
11516 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11517 __begin != __end;
11518 ++__begin)
11520 RANGE_DECL = *__begin;
11521 BLOCK
11525 If RANGE_EXPR is an array:
11526 BEGIN_EXPR = __range
11527 END_EXPR = __range + ARRAY_SIZE(__range)
11528 Else if RANGE_EXPR has a member 'begin' or 'end':
11529 BEGIN_EXPR = __range.begin()
11530 END_EXPR = __range.end()
11531 Else:
11532 BEGIN_EXPR = begin(__range)
11533 END_EXPR = end(__range);
11535 If __range has a member 'begin' but not 'end', or vice versa, we must
11536 still use the second alternative (it will surely fail, however).
11537 When calling begin()/end() in the third alternative we must use
11538 argument dependent lookup, but always considering 'std' as an associated
11539 namespace. */
11541 tree
11542 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11543 bool ivdep)
11545 tree begin, end;
11546 tree iter_type, begin_expr, end_expr;
11547 tree condition, expression;
11549 if (range_decl == error_mark_node || range_expr == error_mark_node)
11550 /* If an error happened previously do nothing or else a lot of
11551 unhelpful errors would be issued. */
11552 begin_expr = end_expr = iter_type = error_mark_node;
11553 else
11555 tree range_temp;
11557 if (VAR_P (range_expr)
11558 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11559 /* Can't bind a reference to an array of runtime bound. */
11560 range_temp = range_expr;
11561 else
11563 range_temp = build_range_temp (range_expr);
11564 pushdecl (range_temp);
11565 cp_finish_decl (range_temp, range_expr,
11566 /*is_constant_init*/false, NULL_TREE,
11567 LOOKUP_ONLYCONVERTING);
11568 range_temp = convert_from_reference (range_temp);
11570 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11571 &begin_expr, &end_expr);
11574 /* The new for initialization statement. */
11575 begin = build_decl (input_location, VAR_DECL,
11576 get_identifier ("__for_begin"), iter_type);
11577 TREE_USED (begin) = 1;
11578 DECL_ARTIFICIAL (begin) = 1;
11579 pushdecl (begin);
11580 cp_finish_decl (begin, begin_expr,
11581 /*is_constant_init*/false, NULL_TREE,
11582 LOOKUP_ONLYCONVERTING);
11584 if (cxx_dialect >= cxx1z)
11585 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11586 end = build_decl (input_location, VAR_DECL,
11587 get_identifier ("__for_end"), iter_type);
11588 TREE_USED (end) = 1;
11589 DECL_ARTIFICIAL (end) = 1;
11590 pushdecl (end);
11591 cp_finish_decl (end, end_expr,
11592 /*is_constant_init*/false, NULL_TREE,
11593 LOOKUP_ONLYCONVERTING);
11595 finish_init_stmt (statement);
11597 /* The new for condition. */
11598 condition = build_x_binary_op (input_location, NE_EXPR,
11599 begin, ERROR_MARK,
11600 end, ERROR_MARK,
11601 NULL, tf_warning_or_error);
11602 finish_for_cond (condition, statement, ivdep);
11604 /* The new increment expression. */
11605 expression = finish_unary_op_expr (input_location,
11606 PREINCREMENT_EXPR, begin,
11607 tf_warning_or_error);
11608 finish_for_expr (expression, statement);
11610 /* The declaration is initialized with *__begin inside the loop body. */
11611 cp_finish_decl (range_decl,
11612 build_x_indirect_ref (input_location, begin, RO_NULL,
11613 tf_warning_or_error),
11614 /*is_constant_init*/false, NULL_TREE,
11615 LOOKUP_ONLYCONVERTING);
11617 return statement;
11620 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11621 We need to solve both at the same time because the method used
11622 depends on the existence of members begin or end.
11623 Returns the type deduced for the iterator expression. */
11625 static tree
11626 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11628 if (error_operand_p (range))
11630 *begin = *end = error_mark_node;
11631 return error_mark_node;
11634 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11636 error ("range-based %<for%> expression of type %qT "
11637 "has incomplete type", TREE_TYPE (range));
11638 *begin = *end = error_mark_node;
11639 return error_mark_node;
11641 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11643 /* If RANGE is an array, we will use pointer arithmetic. */
11644 *begin = decay_conversion (range, tf_warning_or_error);
11645 *end = build_binary_op (input_location, PLUS_EXPR,
11646 range,
11647 array_type_nelts_top (TREE_TYPE (range)),
11649 return TREE_TYPE (*begin);
11651 else
11653 /* If it is not an array, we must do a bit of magic. */
11654 tree id_begin, id_end;
11655 tree member_begin, member_end;
11657 *begin = *end = error_mark_node;
11659 id_begin = get_identifier ("begin");
11660 id_end = get_identifier ("end");
11661 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11662 /*protect=*/2, /*want_type=*/false,
11663 tf_warning_or_error);
11664 member_end = lookup_member (TREE_TYPE (range), id_end,
11665 /*protect=*/2, /*want_type=*/false,
11666 tf_warning_or_error);
11668 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11670 /* Use the member functions. */
11671 if (member_begin != NULL_TREE)
11672 *begin = cp_parser_range_for_member_function (range, id_begin);
11673 else
11674 error ("range-based %<for%> expression of type %qT has an "
11675 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11677 if (member_end != NULL_TREE)
11678 *end = cp_parser_range_for_member_function (range, id_end);
11679 else
11680 error ("range-based %<for%> expression of type %qT has a "
11681 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11683 else
11685 /* Use global functions with ADL. */
11686 vec<tree, va_gc> *vec;
11687 vec = make_tree_vector ();
11689 vec_safe_push (vec, range);
11691 member_begin = perform_koenig_lookup (id_begin, vec,
11692 tf_warning_or_error);
11693 *begin = finish_call_expr (member_begin, &vec, false, true,
11694 tf_warning_or_error);
11695 member_end = perform_koenig_lookup (id_end, vec,
11696 tf_warning_or_error);
11697 *end = finish_call_expr (member_end, &vec, false, true,
11698 tf_warning_or_error);
11700 release_tree_vector (vec);
11703 /* Last common checks. */
11704 if (*begin == error_mark_node || *end == error_mark_node)
11706 /* If one of the expressions is an error do no more checks. */
11707 *begin = *end = error_mark_node;
11708 return error_mark_node;
11710 else if (type_dependent_expression_p (*begin)
11711 || type_dependent_expression_p (*end))
11712 /* Can happen, when, eg, in a template context, Koenig lookup
11713 can't resolve begin/end (c++/58503). */
11714 return NULL_TREE;
11715 else
11717 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11718 /* The unqualified type of the __begin and __end temporaries should
11719 be the same, as required by the multiple auto declaration. */
11720 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11722 if (cxx_dialect >= cxx1z
11723 && (build_x_binary_op (input_location, NE_EXPR,
11724 *begin, ERROR_MARK,
11725 *end, ERROR_MARK,
11726 NULL, tf_none)
11727 != error_mark_node))
11728 /* P0184R0 allows __begin and __end to have different types,
11729 but make sure they are comparable so we can give a better
11730 diagnostic. */;
11731 else
11732 error ("inconsistent begin/end types in range-based %<for%> "
11733 "statement: %qT and %qT",
11734 TREE_TYPE (*begin), TREE_TYPE (*end));
11736 return iter_type;
11741 /* Helper function for cp_parser_perform_range_for_lookup.
11742 Builds a tree for RANGE.IDENTIFIER(). */
11744 static tree
11745 cp_parser_range_for_member_function (tree range, tree identifier)
11747 tree member, res;
11748 vec<tree, va_gc> *vec;
11750 member = finish_class_member_access_expr (range, identifier,
11751 false, tf_warning_or_error);
11752 if (member == error_mark_node)
11753 return error_mark_node;
11755 vec = make_tree_vector ();
11756 res = finish_call_expr (member, &vec,
11757 /*disallow_virtual=*/false,
11758 /*koenig_p=*/false,
11759 tf_warning_or_error);
11760 release_tree_vector (vec);
11761 return res;
11764 /* Parse an iteration-statement.
11766 iteration-statement:
11767 while ( condition ) statement
11768 do statement while ( expression ) ;
11769 for ( init-statement condition [opt] ; expression [opt] )
11770 statement
11772 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11774 static tree
11775 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11777 cp_token *token;
11778 enum rid keyword;
11779 tree statement;
11780 unsigned char in_statement;
11781 token_indent_info guard_tinfo;
11783 /* Peek at the next token. */
11784 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11785 if (!token)
11786 return error_mark_node;
11788 guard_tinfo = get_token_indent_info (token);
11790 /* Remember whether or not we are already within an iteration
11791 statement. */
11792 in_statement = parser->in_statement;
11794 /* See what kind of keyword it is. */
11795 keyword = token->keyword;
11796 switch (keyword)
11798 case RID_WHILE:
11800 tree condition;
11802 /* Begin the while-statement. */
11803 statement = begin_while_stmt ();
11804 /* Look for the `('. */
11805 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11806 /* Parse the condition. */
11807 condition = cp_parser_condition (parser);
11808 finish_while_stmt_cond (condition, statement, ivdep);
11809 /* Look for the `)'. */
11810 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11811 /* Parse the dependent statement. */
11812 parser->in_statement = IN_ITERATION_STMT;
11813 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11814 parser->in_statement = in_statement;
11815 /* We're done with the while-statement. */
11816 finish_while_stmt (statement);
11818 break;
11820 case RID_DO:
11822 tree expression;
11824 /* Begin the do-statement. */
11825 statement = begin_do_stmt ();
11826 /* Parse the body of the do-statement. */
11827 parser->in_statement = IN_ITERATION_STMT;
11828 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11829 parser->in_statement = in_statement;
11830 finish_do_body (statement);
11831 /* Look for the `while' keyword. */
11832 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11833 /* Look for the `('. */
11834 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11835 /* Parse the expression. */
11836 expression = cp_parser_expression (parser);
11837 /* We're done with the do-statement. */
11838 finish_do_stmt (expression, statement, ivdep);
11839 /* Look for the `)'. */
11840 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11841 /* Look for the `;'. */
11842 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11844 break;
11846 case RID_FOR:
11848 /* Look for the `('. */
11849 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11851 statement = cp_parser_for (parser, ivdep);
11853 /* Look for the `)'. */
11854 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11856 /* Parse the body of the for-statement. */
11857 parser->in_statement = IN_ITERATION_STMT;
11858 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11859 parser->in_statement = in_statement;
11861 /* We're done with the for-statement. */
11862 finish_for_stmt (statement);
11864 break;
11866 default:
11867 cp_parser_error (parser, "expected iteration-statement");
11868 statement = error_mark_node;
11869 break;
11872 return statement;
11875 /* Parse a init-statement or the declarator of a range-based-for.
11876 Returns true if a range-based-for declaration is seen.
11878 init-statement:
11879 expression-statement
11880 simple-declaration */
11882 static bool
11883 cp_parser_init_statement (cp_parser* parser, tree *decl)
11885 /* If the next token is a `;', then we have an empty
11886 expression-statement. Grammatically, this is also a
11887 simple-declaration, but an invalid one, because it does not
11888 declare anything. Therefore, if we did not handle this case
11889 specially, we would issue an error message about an invalid
11890 declaration. */
11891 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11893 bool is_range_for = false;
11894 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11896 /* A colon is used in range-based for. */
11897 parser->colon_corrects_to_scope_p = false;
11899 /* We're going to speculatively look for a declaration, falling back
11900 to an expression, if necessary. */
11901 cp_parser_parse_tentatively (parser);
11902 /* Parse the declaration. */
11903 cp_parser_simple_declaration (parser,
11904 /*function_definition_allowed_p=*/false,
11905 decl);
11906 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11907 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11909 /* It is a range-for, consume the ':' */
11910 cp_lexer_consume_token (parser->lexer);
11911 is_range_for = true;
11912 if (cxx_dialect < cxx11)
11914 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11915 "range-based %<for%> loops only available with "
11916 "-std=c++11 or -std=gnu++11");
11917 *decl = error_mark_node;
11920 else
11921 /* The ';' is not consumed yet because we told
11922 cp_parser_simple_declaration not to. */
11923 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11925 if (cp_parser_parse_definitely (parser))
11926 return is_range_for;
11927 /* If the tentative parse failed, then we shall need to look for an
11928 expression-statement. */
11930 /* If we are here, it is an expression-statement. */
11931 cp_parser_expression_statement (parser, NULL_TREE);
11932 return false;
11935 /* Parse a jump-statement.
11937 jump-statement:
11938 break ;
11939 continue ;
11940 return expression [opt] ;
11941 return braced-init-list ;
11942 goto identifier ;
11944 GNU extension:
11946 jump-statement:
11947 goto * expression ;
11949 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11951 static tree
11952 cp_parser_jump_statement (cp_parser* parser)
11954 tree statement = error_mark_node;
11955 cp_token *token;
11956 enum rid keyword;
11957 unsigned char in_statement;
11959 /* Peek at the next token. */
11960 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11961 if (!token)
11962 return error_mark_node;
11964 /* See what kind of keyword it is. */
11965 keyword = token->keyword;
11966 switch (keyword)
11968 case RID_BREAK:
11969 in_statement = parser->in_statement & ~IN_IF_STMT;
11970 switch (in_statement)
11972 case 0:
11973 error_at (token->location, "break statement not within loop or switch");
11974 break;
11975 default:
11976 gcc_assert ((in_statement & IN_SWITCH_STMT)
11977 || in_statement == IN_ITERATION_STMT);
11978 statement = finish_break_stmt ();
11979 if (in_statement == IN_ITERATION_STMT)
11980 break_maybe_infinite_loop ();
11981 break;
11982 case IN_OMP_BLOCK:
11983 error_at (token->location, "invalid exit from OpenMP structured block");
11984 break;
11985 case IN_OMP_FOR:
11986 error_at (token->location, "break statement used with OpenMP for loop");
11987 break;
11988 case IN_CILK_SIMD_FOR:
11989 error_at (token->location, "break statement used with Cilk Plus for loop");
11990 break;
11992 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11993 break;
11995 case RID_CONTINUE:
11996 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11998 case 0:
11999 error_at (token->location, "continue statement not within a loop");
12000 break;
12001 case IN_CILK_SIMD_FOR:
12002 error_at (token->location,
12003 "continue statement within %<#pragma simd%> loop body");
12004 /* Fall through. */
12005 case IN_ITERATION_STMT:
12006 case IN_OMP_FOR:
12007 statement = finish_continue_stmt ();
12008 break;
12009 case IN_OMP_BLOCK:
12010 error_at (token->location, "invalid exit from OpenMP structured block");
12011 break;
12012 default:
12013 gcc_unreachable ();
12015 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12016 break;
12018 case RID_RETURN:
12020 tree expr;
12021 bool expr_non_constant_p;
12023 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12025 cp_lexer_set_source_position (parser->lexer);
12026 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12027 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12029 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12030 expr = cp_parser_expression (parser);
12031 else
12032 /* If the next token is a `;', then there is no
12033 expression. */
12034 expr = NULL_TREE;
12035 /* Build the return-statement. */
12036 if (current_function_auto_return_pattern && parser->in_discarded_stmt)
12037 /* Don't deduce from a discarded return statement. */;
12038 else
12039 statement = finish_return_stmt (expr);
12040 /* Look for the final `;'. */
12041 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12043 break;
12045 case RID_GOTO:
12046 if (parser->in_function_body
12047 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12049 error ("%<goto%> in %<constexpr%> function");
12050 cp_function_chain->invalid_constexpr = true;
12053 /* Create the goto-statement. */
12054 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12056 /* Issue a warning about this use of a GNU extension. */
12057 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12058 /* Consume the '*' token. */
12059 cp_lexer_consume_token (parser->lexer);
12060 /* Parse the dependent expression. */
12061 finish_goto_stmt (cp_parser_expression (parser));
12063 else
12064 finish_goto_stmt (cp_parser_identifier (parser));
12065 /* Look for the final `;'. */
12066 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12067 break;
12069 default:
12070 cp_parser_error (parser, "expected jump-statement");
12071 break;
12074 return statement;
12077 /* Parse a declaration-statement.
12079 declaration-statement:
12080 block-declaration */
12082 static void
12083 cp_parser_declaration_statement (cp_parser* parser)
12085 void *p;
12087 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12088 p = obstack_alloc (&declarator_obstack, 0);
12090 /* Parse the block-declaration. */
12091 cp_parser_block_declaration (parser, /*statement_p=*/true);
12093 /* Free any declarators allocated. */
12094 obstack_free (&declarator_obstack, p);
12097 /* Some dependent statements (like `if (cond) statement'), are
12098 implicitly in their own scope. In other words, if the statement is
12099 a single statement (as opposed to a compound-statement), it is
12100 none-the-less treated as if it were enclosed in braces. Any
12101 declarations appearing in the dependent statement are out of scope
12102 after control passes that point. This function parses a statement,
12103 but ensures that is in its own scope, even if it is not a
12104 compound-statement.
12106 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12107 is a (possibly labeled) if statement which is not enclosed in
12108 braces and has an else clause. This is used to implement
12109 -Wparentheses.
12111 CHAIN is a vector of if-else-if conditions. This is used to implement
12112 -Wduplicated-cond.
12114 Returns the new statement. */
12116 static tree
12117 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12118 const token_indent_info &guard_tinfo,
12119 vec<tree> *chain)
12121 tree statement;
12122 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12123 token_indent_info body_tinfo
12124 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12126 if (if_p != NULL)
12127 *if_p = false;
12129 /* Mark if () ; with a special NOP_EXPR. */
12130 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12132 cp_lexer_consume_token (parser->lexer);
12133 statement = add_stmt (build_empty_stmt (body_loc));
12135 if (guard_tinfo.keyword == RID_IF
12136 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12137 warning_at (body_loc, OPT_Wempty_body,
12138 "suggest braces around empty body in an %<if%> statement");
12139 else if (guard_tinfo.keyword == RID_ELSE)
12140 warning_at (body_loc, OPT_Wempty_body,
12141 "suggest braces around empty body in an %<else%> statement");
12143 /* if a compound is opened, we simply parse the statement directly. */
12144 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12145 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12146 /* If the token is not a `{', then we must take special action. */
12147 else
12149 /* Create a compound-statement. */
12150 statement = begin_compound_stmt (0);
12151 /* Parse the dependent-statement. */
12152 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12153 /* Finish the dummy compound-statement. */
12154 finish_compound_stmt (statement);
12157 token_indent_info next_tinfo
12158 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12159 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12161 /* Return the statement. */
12162 return statement;
12165 /* For some dependent statements (like `while (cond) statement'), we
12166 have already created a scope. Therefore, even if the dependent
12167 statement is a compound-statement, we do not want to create another
12168 scope. */
12170 static void
12171 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12172 const token_indent_info &guard_tinfo)
12174 /* If the token is a `{', then we must take special action. */
12175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12177 token_indent_info body_tinfo
12178 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12180 cp_parser_statement (parser, NULL_TREE, false, if_p);
12181 token_indent_info next_tinfo
12182 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12183 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12185 else
12187 /* Avoid calling cp_parser_compound_statement, so that we
12188 don't create a new scope. Do everything else by hand. */
12189 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12190 /* If the next keyword is `__label__' we have a label declaration. */
12191 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12192 cp_parser_label_declaration (parser);
12193 /* Parse an (optional) statement-seq. */
12194 cp_parser_statement_seq_opt (parser, NULL_TREE);
12195 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12199 /* Declarations [gram.dcl.dcl] */
12201 /* Parse an optional declaration-sequence.
12203 declaration-seq:
12204 declaration
12205 declaration-seq declaration */
12207 static void
12208 cp_parser_declaration_seq_opt (cp_parser* parser)
12210 while (true)
12212 cp_token *token;
12214 token = cp_lexer_peek_token (parser->lexer);
12216 if (token->type == CPP_CLOSE_BRACE
12217 || token->type == CPP_EOF
12218 || token->type == CPP_PRAGMA_EOL)
12219 break;
12221 if (token->type == CPP_SEMICOLON)
12223 /* A declaration consisting of a single semicolon is
12224 invalid. Allow it unless we're being pedantic. */
12225 cp_lexer_consume_token (parser->lexer);
12226 if (!in_system_header_at (input_location))
12227 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12228 continue;
12231 /* If we're entering or exiting a region that's implicitly
12232 extern "C", modify the lang context appropriately. */
12233 if (!parser->implicit_extern_c && token->implicit_extern_c)
12235 push_lang_context (lang_name_c);
12236 parser->implicit_extern_c = true;
12238 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12240 pop_lang_context ();
12241 parser->implicit_extern_c = false;
12244 if (token->type == CPP_PRAGMA)
12246 /* A top-level declaration can consist solely of a #pragma.
12247 A nested declaration cannot, so this is done here and not
12248 in cp_parser_declaration. (A #pragma at block scope is
12249 handled in cp_parser_statement.) */
12250 cp_parser_pragma (parser, pragma_external, NULL);
12251 continue;
12254 /* Parse the declaration itself. */
12255 cp_parser_declaration (parser);
12259 /* Parse a declaration.
12261 declaration:
12262 block-declaration
12263 function-definition
12264 template-declaration
12265 explicit-instantiation
12266 explicit-specialization
12267 linkage-specification
12268 namespace-definition
12270 C++17:
12271 deduction-guide
12273 GNU extension:
12275 declaration:
12276 __extension__ declaration */
12278 static void
12279 cp_parser_declaration (cp_parser* parser)
12281 cp_token token1;
12282 cp_token token2;
12283 int saved_pedantic;
12284 void *p;
12285 tree attributes = NULL_TREE;
12287 /* Check for the `__extension__' keyword. */
12288 if (cp_parser_extension_opt (parser, &saved_pedantic))
12290 /* Parse the qualified declaration. */
12291 cp_parser_declaration (parser);
12292 /* Restore the PEDANTIC flag. */
12293 pedantic = saved_pedantic;
12295 return;
12298 /* Try to figure out what kind of declaration is present. */
12299 token1 = *cp_lexer_peek_token (parser->lexer);
12301 if (token1.type != CPP_EOF)
12302 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12303 else
12305 token2.type = CPP_EOF;
12306 token2.keyword = RID_MAX;
12309 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12310 p = obstack_alloc (&declarator_obstack, 0);
12312 /* If the next token is `extern' and the following token is a string
12313 literal, then we have a linkage specification. */
12314 if (token1.keyword == RID_EXTERN
12315 && cp_parser_is_pure_string_literal (&token2))
12316 cp_parser_linkage_specification (parser);
12317 /* If the next token is `template', then we have either a template
12318 declaration, an explicit instantiation, or an explicit
12319 specialization. */
12320 else if (token1.keyword == RID_TEMPLATE)
12322 /* `template <>' indicates a template specialization. */
12323 if (token2.type == CPP_LESS
12324 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12325 cp_parser_explicit_specialization (parser);
12326 /* `template <' indicates a template declaration. */
12327 else if (token2.type == CPP_LESS)
12328 cp_parser_template_declaration (parser, /*member_p=*/false);
12329 /* Anything else must be an explicit instantiation. */
12330 else
12331 cp_parser_explicit_instantiation (parser);
12333 /* If the next token is `export', then we have a template
12334 declaration. */
12335 else if (token1.keyword == RID_EXPORT)
12336 cp_parser_template_declaration (parser, /*member_p=*/false);
12337 /* If the next token is `extern', 'static' or 'inline' and the one
12338 after that is `template', we have a GNU extended explicit
12339 instantiation directive. */
12340 else if (cp_parser_allow_gnu_extensions_p (parser)
12341 && (token1.keyword == RID_EXTERN
12342 || token1.keyword == RID_STATIC
12343 || token1.keyword == RID_INLINE)
12344 && token2.keyword == RID_TEMPLATE)
12345 cp_parser_explicit_instantiation (parser);
12346 /* If the next token is `namespace', check for a named or unnamed
12347 namespace definition. */
12348 else if (token1.keyword == RID_NAMESPACE
12349 && (/* A named namespace definition. */
12350 (token2.type == CPP_NAME
12351 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12352 != CPP_EQ))
12353 || (token2.type == CPP_OPEN_SQUARE
12354 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12355 == CPP_OPEN_SQUARE)
12356 /* An unnamed namespace definition. */
12357 || token2.type == CPP_OPEN_BRACE
12358 || token2.keyword == RID_ATTRIBUTE))
12359 cp_parser_namespace_definition (parser);
12360 /* An inline (associated) namespace definition. */
12361 else if (token1.keyword == RID_INLINE
12362 && token2.keyword == RID_NAMESPACE)
12363 cp_parser_namespace_definition (parser);
12364 /* Objective-C++ declaration/definition. */
12365 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12366 cp_parser_objc_declaration (parser, NULL_TREE);
12367 else if (c_dialect_objc ()
12368 && token1.keyword == RID_ATTRIBUTE
12369 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12370 cp_parser_objc_declaration (parser, attributes);
12371 /* At this point we may have a template declared by a concept
12372 introduction. */
12373 else if (flag_concepts
12374 && cp_parser_template_declaration_after_export (parser,
12375 /*member_p=*/false))
12376 /* We did. */;
12377 else
12378 /* Try to parse a block-declaration, or a function-definition. */
12379 cp_parser_block_declaration (parser, /*statement_p=*/false);
12381 /* Free any declarators allocated. */
12382 obstack_free (&declarator_obstack, p);
12385 /* Parse a block-declaration.
12387 block-declaration:
12388 simple-declaration
12389 asm-definition
12390 namespace-alias-definition
12391 using-declaration
12392 using-directive
12394 GNU Extension:
12396 block-declaration:
12397 __extension__ block-declaration
12399 C++0x Extension:
12401 block-declaration:
12402 static_assert-declaration
12404 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12405 part of a declaration-statement. */
12407 static void
12408 cp_parser_block_declaration (cp_parser *parser,
12409 bool statement_p)
12411 cp_token *token1;
12412 int saved_pedantic;
12414 /* Check for the `__extension__' keyword. */
12415 if (cp_parser_extension_opt (parser, &saved_pedantic))
12417 /* Parse the qualified declaration. */
12418 cp_parser_block_declaration (parser, statement_p);
12419 /* Restore the PEDANTIC flag. */
12420 pedantic = saved_pedantic;
12422 return;
12425 /* Peek at the next token to figure out which kind of declaration is
12426 present. */
12427 token1 = cp_lexer_peek_token (parser->lexer);
12429 /* If the next keyword is `asm', we have an asm-definition. */
12430 if (token1->keyword == RID_ASM)
12432 if (statement_p)
12433 cp_parser_commit_to_tentative_parse (parser);
12434 cp_parser_asm_definition (parser);
12436 /* If the next keyword is `namespace', we have a
12437 namespace-alias-definition. */
12438 else if (token1->keyword == RID_NAMESPACE)
12439 cp_parser_namespace_alias_definition (parser);
12440 /* If the next keyword is `using', we have a
12441 using-declaration, a using-directive, or an alias-declaration. */
12442 else if (token1->keyword == RID_USING)
12444 cp_token *token2;
12446 if (statement_p)
12447 cp_parser_commit_to_tentative_parse (parser);
12448 /* If the token after `using' is `namespace', then we have a
12449 using-directive. */
12450 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12451 if (token2->keyword == RID_NAMESPACE)
12452 cp_parser_using_directive (parser);
12453 /* If the second token after 'using' is '=', then we have an
12454 alias-declaration. */
12455 else if (cxx_dialect >= cxx11
12456 && token2->type == CPP_NAME
12457 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12458 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12459 cp_parser_alias_declaration (parser);
12460 /* Otherwise, it's a using-declaration. */
12461 else
12462 cp_parser_using_declaration (parser,
12463 /*access_declaration_p=*/false);
12465 /* If the next keyword is `__label__' we have a misplaced label
12466 declaration. */
12467 else if (token1->keyword == RID_LABEL)
12469 cp_lexer_consume_token (parser->lexer);
12470 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12471 cp_parser_skip_to_end_of_statement (parser);
12472 /* If the next token is now a `;', consume it. */
12473 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12474 cp_lexer_consume_token (parser->lexer);
12476 /* If the next token is `static_assert' we have a static assertion. */
12477 else if (token1->keyword == RID_STATIC_ASSERT)
12478 cp_parser_static_assert (parser, /*member_p=*/false);
12479 /* Anything else must be a simple-declaration. */
12480 else
12481 cp_parser_simple_declaration (parser, !statement_p,
12482 /*maybe_range_for_decl*/NULL);
12485 /* Parse a simple-declaration.
12487 simple-declaration:
12488 decl-specifier-seq [opt] init-declarator-list [opt] ;
12490 init-declarator-list:
12491 init-declarator
12492 init-declarator-list , init-declarator
12494 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12495 function-definition as a simple-declaration.
12497 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12498 parsed declaration if it is an uninitialized single declarator not followed
12499 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12500 if present, will not be consumed. */
12502 static void
12503 cp_parser_simple_declaration (cp_parser* parser,
12504 bool function_definition_allowed_p,
12505 tree *maybe_range_for_decl)
12507 cp_decl_specifier_seq decl_specifiers;
12508 int declares_class_or_enum;
12509 bool saw_declarator;
12510 location_t comma_loc = UNKNOWN_LOCATION;
12511 location_t init_loc = UNKNOWN_LOCATION;
12513 if (maybe_range_for_decl)
12514 *maybe_range_for_decl = NULL_TREE;
12516 /* Defer access checks until we know what is being declared; the
12517 checks for names appearing in the decl-specifier-seq should be
12518 done as if we were in the scope of the thing being declared. */
12519 push_deferring_access_checks (dk_deferred);
12521 /* Parse the decl-specifier-seq. We have to keep track of whether
12522 or not the decl-specifier-seq declares a named class or
12523 enumeration type, since that is the only case in which the
12524 init-declarator-list is allowed to be empty.
12526 [dcl.dcl]
12528 In a simple-declaration, the optional init-declarator-list can be
12529 omitted only when declaring a class or enumeration, that is when
12530 the decl-specifier-seq contains either a class-specifier, an
12531 elaborated-type-specifier, or an enum-specifier. */
12532 cp_parser_decl_specifier_seq (parser,
12533 CP_PARSER_FLAGS_OPTIONAL,
12534 &decl_specifiers,
12535 &declares_class_or_enum);
12536 /* We no longer need to defer access checks. */
12537 stop_deferring_access_checks ();
12539 /* In a block scope, a valid declaration must always have a
12540 decl-specifier-seq. By not trying to parse declarators, we can
12541 resolve the declaration/expression ambiguity more quickly. */
12542 if (!function_definition_allowed_p
12543 && !decl_specifiers.any_specifiers_p)
12545 cp_parser_error (parser, "expected declaration");
12546 goto done;
12549 /* If the next two tokens are both identifiers, the code is
12550 erroneous. The usual cause of this situation is code like:
12552 T t;
12554 where "T" should name a type -- but does not. */
12555 if (!decl_specifiers.any_type_specifiers_p
12556 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12558 /* If parsing tentatively, we should commit; we really are
12559 looking at a declaration. */
12560 cp_parser_commit_to_tentative_parse (parser);
12561 /* Give up. */
12562 goto done;
12565 /* If we have seen at least one decl-specifier, and the next token
12566 is not a parenthesis, then we must be looking at a declaration.
12567 (After "int (" we might be looking at a functional cast.) */
12568 if (decl_specifiers.any_specifiers_p
12569 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12570 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12571 && !cp_parser_error_occurred (parser))
12572 cp_parser_commit_to_tentative_parse (parser);
12574 tree last_type;
12576 last_type = NULL_TREE;
12578 /* Keep going until we hit the `;' at the end of the simple
12579 declaration. */
12580 saw_declarator = false;
12581 while (cp_lexer_next_token_is_not (parser->lexer,
12582 CPP_SEMICOLON))
12584 cp_token *token;
12585 bool function_definition_p;
12586 tree decl;
12587 tree auto_result = NULL_TREE;
12589 if (saw_declarator)
12591 /* If we are processing next declarator, comma is expected */
12592 token = cp_lexer_peek_token (parser->lexer);
12593 gcc_assert (token->type == CPP_COMMA);
12594 cp_lexer_consume_token (parser->lexer);
12595 if (maybe_range_for_decl)
12597 *maybe_range_for_decl = error_mark_node;
12598 if (comma_loc == UNKNOWN_LOCATION)
12599 comma_loc = token->location;
12602 else
12603 saw_declarator = true;
12605 /* Parse the init-declarator. */
12606 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12607 /*checks=*/NULL,
12608 function_definition_allowed_p,
12609 /*member_p=*/false,
12610 declares_class_or_enum,
12611 &function_definition_p,
12612 maybe_range_for_decl,
12613 &init_loc,
12614 &auto_result);
12615 /* If an error occurred while parsing tentatively, exit quickly.
12616 (That usually happens when in the body of a function; each
12617 statement is treated as a declaration-statement until proven
12618 otherwise.) */
12619 if (cp_parser_error_occurred (parser))
12620 goto done;
12622 if (auto_result)
12624 if (last_type && last_type != error_mark_node
12625 && !same_type_p (auto_result, last_type))
12627 /* If the list of declarators contains more than one declarator,
12628 the type of each declared variable is determined as described
12629 above. If the type deduced for the template parameter U is not
12630 the same in each deduction, the program is ill-formed. */
12631 error_at (decl_specifiers.locations[ds_type_spec],
12632 "inconsistent deduction for %qT: %qT and then %qT",
12633 decl_specifiers.type, last_type, auto_result);
12634 last_type = error_mark_node;
12636 else
12637 last_type = auto_result;
12640 /* Handle function definitions specially. */
12641 if (function_definition_p)
12643 /* If the next token is a `,', then we are probably
12644 processing something like:
12646 void f() {}, *p;
12648 which is erroneous. */
12649 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12651 cp_token *token = cp_lexer_peek_token (parser->lexer);
12652 error_at (token->location,
12653 "mixing"
12654 " declarations and function-definitions is forbidden");
12656 /* Otherwise, we're done with the list of declarators. */
12657 else
12659 pop_deferring_access_checks ();
12660 return;
12663 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12664 *maybe_range_for_decl = decl;
12665 /* The next token should be either a `,' or a `;'. */
12666 token = cp_lexer_peek_token (parser->lexer);
12667 /* If it's a `,', there are more declarators to come. */
12668 if (token->type == CPP_COMMA)
12669 /* will be consumed next time around */;
12670 /* If it's a `;', we are done. */
12671 else if (token->type == CPP_SEMICOLON)
12672 break;
12673 else if (maybe_range_for_decl)
12675 if (declares_class_or_enum && token->type == CPP_COLON)
12676 permerror (decl_specifiers.locations[ds_type_spec],
12677 "types may not be defined in a for-range-declaration");
12678 break;
12680 /* Anything else is an error. */
12681 else
12683 /* If we have already issued an error message we don't need
12684 to issue another one. */
12685 if ((decl != error_mark_node
12686 && DECL_INITIAL (decl) != error_mark_node)
12687 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12688 cp_parser_error (parser, "expected %<,%> or %<;%>");
12689 /* Skip tokens until we reach the end of the statement. */
12690 cp_parser_skip_to_end_of_statement (parser);
12691 /* If the next token is now a `;', consume it. */
12692 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12693 cp_lexer_consume_token (parser->lexer);
12694 goto done;
12696 /* After the first time around, a function-definition is not
12697 allowed -- even if it was OK at first. For example:
12699 int i, f() {}
12701 is not valid. */
12702 function_definition_allowed_p = false;
12705 /* Issue an error message if no declarators are present, and the
12706 decl-specifier-seq does not itself declare a class or
12707 enumeration: [dcl.dcl]/3. */
12708 if (!saw_declarator)
12710 if (cp_parser_declares_only_class_p (parser))
12712 if (!declares_class_or_enum
12713 && decl_specifiers.type
12714 && OVERLOAD_TYPE_P (decl_specifiers.type))
12715 /* Ensure an error is issued anyway when finish_decltype_type,
12716 called via cp_parser_decl_specifier_seq, returns a class or
12717 an enumeration (c++/51786). */
12718 decl_specifiers.type = NULL_TREE;
12719 shadow_tag (&decl_specifiers);
12721 /* Perform any deferred access checks. */
12722 perform_deferred_access_checks (tf_warning_or_error);
12725 /* Consume the `;'. */
12726 if (!maybe_range_for_decl)
12727 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12728 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12730 if (init_loc != UNKNOWN_LOCATION)
12731 error_at (init_loc, "initializer in range-based %<for%> loop");
12732 if (comma_loc != UNKNOWN_LOCATION)
12733 error_at (comma_loc,
12734 "multiple declarations in range-based %<for%> loop");
12737 done:
12738 pop_deferring_access_checks ();
12741 /* Parse a decl-specifier-seq.
12743 decl-specifier-seq:
12744 decl-specifier-seq [opt] decl-specifier
12745 decl-specifier attribute-specifier-seq [opt] (C++11)
12747 decl-specifier:
12748 storage-class-specifier
12749 type-specifier
12750 function-specifier
12751 friend
12752 typedef
12754 GNU Extension:
12756 decl-specifier:
12757 attributes
12759 Concepts Extension:
12761 decl-specifier:
12762 concept
12764 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12766 The parser flags FLAGS is used to control type-specifier parsing.
12768 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12769 flags:
12771 1: one of the decl-specifiers is an elaborated-type-specifier
12772 (i.e., a type declaration)
12773 2: one of the decl-specifiers is an enum-specifier or a
12774 class-specifier (i.e., a type definition)
12778 static void
12779 cp_parser_decl_specifier_seq (cp_parser* parser,
12780 cp_parser_flags flags,
12781 cp_decl_specifier_seq *decl_specs,
12782 int* declares_class_or_enum)
12784 bool constructor_possible_p = !parser->in_declarator_p;
12785 bool found_decl_spec = false;
12786 cp_token *start_token = NULL;
12787 cp_decl_spec ds;
12789 /* Clear DECL_SPECS. */
12790 clear_decl_specs (decl_specs);
12792 /* Assume no class or enumeration type is declared. */
12793 *declares_class_or_enum = 0;
12795 /* Keep reading specifiers until there are no more to read. */
12796 while (true)
12798 bool constructor_p;
12799 cp_token *token;
12800 ds = ds_last;
12802 /* Peek at the next token. */
12803 token = cp_lexer_peek_token (parser->lexer);
12805 /* Save the first token of the decl spec list for error
12806 reporting. */
12807 if (!start_token)
12808 start_token = token;
12809 /* Handle attributes. */
12810 if (cp_next_tokens_can_be_attribute_p (parser))
12812 /* Parse the attributes. */
12813 tree attrs = cp_parser_attributes_opt (parser);
12815 /* In a sequence of declaration specifiers, c++11 attributes
12816 appertain to the type that precede them. In that case
12817 [dcl.spec]/1 says:
12819 The attribute-specifier-seq affects the type only for
12820 the declaration it appears in, not other declarations
12821 involving the same type.
12823 But for now let's force the user to position the
12824 attribute either at the beginning of the declaration or
12825 after the declarator-id, which would clearly mean that it
12826 applies to the declarator. */
12827 if (cxx11_attribute_p (attrs))
12829 if (!found_decl_spec)
12830 /* The c++11 attribute is at the beginning of the
12831 declaration. It appertains to the entity being
12832 declared. */;
12833 else
12835 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12837 /* This is an attribute following a
12838 class-specifier. */
12839 if (decl_specs->type_definition_p)
12840 warn_misplaced_attr_for_class_type (token->location,
12841 decl_specs->type);
12842 attrs = NULL_TREE;
12844 else
12846 decl_specs->std_attributes
12847 = chainon (decl_specs->std_attributes,
12848 attrs);
12849 if (decl_specs->locations[ds_std_attribute] == 0)
12850 decl_specs->locations[ds_std_attribute] = token->location;
12852 continue;
12856 decl_specs->attributes
12857 = chainon (decl_specs->attributes,
12858 attrs);
12859 if (decl_specs->locations[ds_attribute] == 0)
12860 decl_specs->locations[ds_attribute] = token->location;
12861 continue;
12863 /* Assume we will find a decl-specifier keyword. */
12864 found_decl_spec = true;
12865 /* If the next token is an appropriate keyword, we can simply
12866 add it to the list. */
12867 switch (token->keyword)
12869 /* decl-specifier:
12870 friend
12871 constexpr */
12872 case RID_FRIEND:
12873 if (!at_class_scope_p ())
12875 error_at (token->location, "%<friend%> used outside of class");
12876 cp_lexer_purge_token (parser->lexer);
12878 else
12880 ds = ds_friend;
12881 /* Consume the token. */
12882 cp_lexer_consume_token (parser->lexer);
12884 break;
12886 case RID_CONSTEXPR:
12887 ds = ds_constexpr;
12888 cp_lexer_consume_token (parser->lexer);
12889 break;
12891 case RID_CONCEPT:
12892 ds = ds_concept;
12893 cp_lexer_consume_token (parser->lexer);
12894 break;
12896 /* function-specifier:
12897 inline
12898 virtual
12899 explicit */
12900 case RID_INLINE:
12901 case RID_VIRTUAL:
12902 case RID_EXPLICIT:
12903 cp_parser_function_specifier_opt (parser, decl_specs);
12904 break;
12906 /* decl-specifier:
12907 typedef */
12908 case RID_TYPEDEF:
12909 ds = ds_typedef;
12910 /* Consume the token. */
12911 cp_lexer_consume_token (parser->lexer);
12912 /* A constructor declarator cannot appear in a typedef. */
12913 constructor_possible_p = false;
12914 /* The "typedef" keyword can only occur in a declaration; we
12915 may as well commit at this point. */
12916 cp_parser_commit_to_tentative_parse (parser);
12918 if (decl_specs->storage_class != sc_none)
12919 decl_specs->conflicting_specifiers_p = true;
12920 break;
12922 /* storage-class-specifier:
12923 auto
12924 register
12925 static
12926 extern
12927 mutable
12929 GNU Extension:
12930 thread */
12931 case RID_AUTO:
12932 if (cxx_dialect == cxx98)
12934 /* Consume the token. */
12935 cp_lexer_consume_token (parser->lexer);
12937 /* Complain about `auto' as a storage specifier, if
12938 we're complaining about C++0x compatibility. */
12939 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12940 " changes meaning in C++11; please remove it");
12942 /* Set the storage class anyway. */
12943 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12944 token);
12946 else
12947 /* C++0x auto type-specifier. */
12948 found_decl_spec = false;
12949 break;
12951 case RID_REGISTER:
12952 case RID_STATIC:
12953 case RID_EXTERN:
12954 case RID_MUTABLE:
12955 /* Consume the token. */
12956 cp_lexer_consume_token (parser->lexer);
12957 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12958 token);
12959 break;
12960 case RID_THREAD:
12961 /* Consume the token. */
12962 ds = ds_thread;
12963 cp_lexer_consume_token (parser->lexer);
12964 break;
12966 default:
12967 /* We did not yet find a decl-specifier yet. */
12968 found_decl_spec = false;
12969 break;
12972 if (found_decl_spec
12973 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12974 && token->keyword != RID_CONSTEXPR)
12975 error ("decl-specifier invalid in condition");
12977 if (found_decl_spec
12978 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
12979 && token->keyword != RID_MUTABLE
12980 && token->keyword != RID_CONSTEXPR)
12981 error_at (token->location, "%qD invalid in lambda",
12982 ridpointers[token->keyword]);
12984 if (ds != ds_last)
12985 set_and_check_decl_spec_loc (decl_specs, ds, token);
12987 /* Constructors are a special case. The `S' in `S()' is not a
12988 decl-specifier; it is the beginning of the declarator. */
12989 constructor_p
12990 = (!found_decl_spec
12991 && constructor_possible_p
12992 && (cp_parser_constructor_declarator_p
12993 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12995 /* If we don't have a DECL_SPEC yet, then we must be looking at
12996 a type-specifier. */
12997 if (!found_decl_spec && !constructor_p)
12999 int decl_spec_declares_class_or_enum;
13000 bool is_cv_qualifier;
13001 tree type_spec;
13003 type_spec
13004 = cp_parser_type_specifier (parser, flags,
13005 decl_specs,
13006 /*is_declaration=*/true,
13007 &decl_spec_declares_class_or_enum,
13008 &is_cv_qualifier);
13009 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13011 /* If this type-specifier referenced a user-defined type
13012 (a typedef, class-name, etc.), then we can't allow any
13013 more such type-specifiers henceforth.
13015 [dcl.spec]
13017 The longest sequence of decl-specifiers that could
13018 possibly be a type name is taken as the
13019 decl-specifier-seq of a declaration. The sequence shall
13020 be self-consistent as described below.
13022 [dcl.type]
13024 As a general rule, at most one type-specifier is allowed
13025 in the complete decl-specifier-seq of a declaration. The
13026 only exceptions are the following:
13028 -- const or volatile can be combined with any other
13029 type-specifier.
13031 -- signed or unsigned can be combined with char, long,
13032 short, or int.
13034 -- ..
13036 Example:
13038 typedef char* Pc;
13039 void g (const int Pc);
13041 Here, Pc is *not* part of the decl-specifier seq; it's
13042 the declarator. Therefore, once we see a type-specifier
13043 (other than a cv-qualifier), we forbid any additional
13044 user-defined types. We *do* still allow things like `int
13045 int' to be considered a decl-specifier-seq, and issue the
13046 error message later. */
13047 if (type_spec && !is_cv_qualifier)
13048 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13049 /* A constructor declarator cannot follow a type-specifier. */
13050 if (type_spec)
13052 constructor_possible_p = false;
13053 found_decl_spec = true;
13054 if (!is_cv_qualifier)
13055 decl_specs->any_type_specifiers_p = true;
13059 /* If we still do not have a DECL_SPEC, then there are no more
13060 decl-specifiers. */
13061 if (!found_decl_spec)
13062 break;
13064 decl_specs->any_specifiers_p = true;
13065 /* After we see one decl-specifier, further decl-specifiers are
13066 always optional. */
13067 flags |= CP_PARSER_FLAGS_OPTIONAL;
13070 /* Don't allow a friend specifier with a class definition. */
13071 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13072 && (*declares_class_or_enum & 2))
13073 error_at (decl_specs->locations[ds_friend],
13074 "class definition may not be declared a friend");
13077 /* Parse an (optional) storage-class-specifier.
13079 storage-class-specifier:
13080 auto
13081 register
13082 static
13083 extern
13084 mutable
13086 GNU Extension:
13088 storage-class-specifier:
13089 thread
13091 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13093 static tree
13094 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13096 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13098 case RID_AUTO:
13099 if (cxx_dialect != cxx98)
13100 return NULL_TREE;
13101 /* Fall through for C++98. */
13102 gcc_fallthrough ();
13104 case RID_REGISTER:
13105 case RID_STATIC:
13106 case RID_EXTERN:
13107 case RID_MUTABLE:
13108 case RID_THREAD:
13109 /* Consume the token. */
13110 return cp_lexer_consume_token (parser->lexer)->u.value;
13112 default:
13113 return NULL_TREE;
13117 /* Parse an (optional) function-specifier.
13119 function-specifier:
13120 inline
13121 virtual
13122 explicit
13124 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13125 Updates DECL_SPECS, if it is non-NULL. */
13127 static tree
13128 cp_parser_function_specifier_opt (cp_parser* parser,
13129 cp_decl_specifier_seq *decl_specs)
13131 cp_token *token = cp_lexer_peek_token (parser->lexer);
13132 switch (token->keyword)
13134 case RID_INLINE:
13135 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13136 break;
13138 case RID_VIRTUAL:
13139 /* 14.5.2.3 [temp.mem]
13141 A member function template shall not be virtual. */
13142 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13143 && current_class_type)
13144 error_at (token->location, "templates may not be %<virtual%>");
13145 else
13146 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13147 break;
13149 case RID_EXPLICIT:
13150 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13151 break;
13153 default:
13154 return NULL_TREE;
13157 /* Consume the token. */
13158 return cp_lexer_consume_token (parser->lexer)->u.value;
13161 /* Parse a linkage-specification.
13163 linkage-specification:
13164 extern string-literal { declaration-seq [opt] }
13165 extern string-literal declaration */
13167 static void
13168 cp_parser_linkage_specification (cp_parser* parser)
13170 tree linkage;
13172 /* Look for the `extern' keyword. */
13173 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13175 /* Look for the string-literal. */
13176 linkage = cp_parser_string_literal (parser, false, false);
13178 /* Transform the literal into an identifier. If the literal is a
13179 wide-character string, or contains embedded NULs, then we can't
13180 handle it as the user wants. */
13181 if (strlen (TREE_STRING_POINTER (linkage))
13182 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13184 cp_parser_error (parser, "invalid linkage-specification");
13185 /* Assume C++ linkage. */
13186 linkage = lang_name_cplusplus;
13188 else
13189 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13191 /* We're now using the new linkage. */
13192 push_lang_context (linkage);
13194 /* If the next token is a `{', then we're using the first
13195 production. */
13196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13198 cp_ensure_no_omp_declare_simd (parser);
13199 cp_ensure_no_oacc_routine (parser);
13201 /* Consume the `{' token. */
13202 cp_lexer_consume_token (parser->lexer);
13203 /* Parse the declarations. */
13204 cp_parser_declaration_seq_opt (parser);
13205 /* Look for the closing `}'. */
13206 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13208 /* Otherwise, there's just one declaration. */
13209 else
13211 bool saved_in_unbraced_linkage_specification_p;
13213 saved_in_unbraced_linkage_specification_p
13214 = parser->in_unbraced_linkage_specification_p;
13215 parser->in_unbraced_linkage_specification_p = true;
13216 cp_parser_declaration (parser);
13217 parser->in_unbraced_linkage_specification_p
13218 = saved_in_unbraced_linkage_specification_p;
13221 /* We're done with the linkage-specification. */
13222 pop_lang_context ();
13225 /* Parse a static_assert-declaration.
13227 static_assert-declaration:
13228 static_assert ( constant-expression , string-literal ) ;
13229 static_assert ( constant-expression ) ; (C++1Z)
13231 If MEMBER_P, this static_assert is a class member. */
13233 static void
13234 cp_parser_static_assert(cp_parser *parser, bool member_p)
13236 tree condition;
13237 tree message;
13238 cp_token *token;
13239 location_t saved_loc;
13240 bool dummy;
13242 /* Peek at the `static_assert' token so we can keep track of exactly
13243 where the static assertion started. */
13244 token = cp_lexer_peek_token (parser->lexer);
13245 saved_loc = token->location;
13247 /* Look for the `static_assert' keyword. */
13248 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13249 RT_STATIC_ASSERT))
13250 return;
13252 /* We know we are in a static assertion; commit to any tentative
13253 parse. */
13254 if (cp_parser_parsing_tentatively (parser))
13255 cp_parser_commit_to_tentative_parse (parser);
13257 /* Parse the `(' starting the static assertion condition. */
13258 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13260 /* Parse the constant-expression. Allow a non-constant expression
13261 here in order to give better diagnostics in finish_static_assert. */
13262 condition =
13263 cp_parser_constant_expression (parser,
13264 /*allow_non_constant_p=*/true,
13265 /*non_constant_p=*/&dummy);
13267 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13269 if (cxx_dialect < cxx1z)
13270 pedwarn (input_location, OPT_Wpedantic,
13271 "static_assert without a message "
13272 "only available with -std=c++1z or -std=gnu++1z");
13273 /* Eat the ')' */
13274 cp_lexer_consume_token (parser->lexer);
13275 message = build_string (1, "");
13276 TREE_TYPE (message) = char_array_type_node;
13277 fix_string_type (message);
13279 else
13281 /* Parse the separating `,'. */
13282 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13284 /* Parse the string-literal message. */
13285 message = cp_parser_string_literal (parser,
13286 /*translate=*/false,
13287 /*wide_ok=*/true);
13289 /* A `)' completes the static assertion. */
13290 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13291 cp_parser_skip_to_closing_parenthesis (parser,
13292 /*recovering=*/true,
13293 /*or_comma=*/false,
13294 /*consume_paren=*/true);
13297 /* A semicolon terminates the declaration. */
13298 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13300 /* Complete the static assertion, which may mean either processing
13301 the static assert now or saving it for template instantiation. */
13302 finish_static_assert (condition, message, saved_loc, member_p);
13305 /* Parse the expression in decltype ( expression ). */
13307 static tree
13308 cp_parser_decltype_expr (cp_parser *parser,
13309 bool &id_expression_or_member_access_p)
13311 cp_token *id_expr_start_token;
13312 tree expr;
13314 /* Since we're going to preserve any side-effects from this parse, set up a
13315 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13316 in the expression. */
13317 tentative_firewall firewall (parser);
13319 /* First, try parsing an id-expression. */
13320 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13321 cp_parser_parse_tentatively (parser);
13322 expr = cp_parser_id_expression (parser,
13323 /*template_keyword_p=*/false,
13324 /*check_dependency_p=*/true,
13325 /*template_p=*/NULL,
13326 /*declarator_p=*/false,
13327 /*optional_p=*/false);
13329 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13331 bool non_integral_constant_expression_p = false;
13332 tree id_expression = expr;
13333 cp_id_kind idk;
13334 const char *error_msg;
13336 if (identifier_p (expr))
13337 /* Lookup the name we got back from the id-expression. */
13338 expr = cp_parser_lookup_name_simple (parser, expr,
13339 id_expr_start_token->location);
13341 if (expr
13342 && expr != error_mark_node
13343 && TREE_CODE (expr) != TYPE_DECL
13344 && (TREE_CODE (expr) != BIT_NOT_EXPR
13345 || !TYPE_P (TREE_OPERAND (expr, 0)))
13346 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13348 /* Complete lookup of the id-expression. */
13349 expr = (finish_id_expression
13350 (id_expression, expr, parser->scope, &idk,
13351 /*integral_constant_expression_p=*/false,
13352 /*allow_non_integral_constant_expression_p=*/true,
13353 &non_integral_constant_expression_p,
13354 /*template_p=*/false,
13355 /*done=*/true,
13356 /*address_p=*/false,
13357 /*template_arg_p=*/false,
13358 &error_msg,
13359 id_expr_start_token->location));
13361 if (expr == error_mark_node)
13362 /* We found an id-expression, but it was something that we
13363 should not have found. This is an error, not something
13364 we can recover from, so note that we found an
13365 id-expression and we'll recover as gracefully as
13366 possible. */
13367 id_expression_or_member_access_p = true;
13370 if (expr
13371 && expr != error_mark_node
13372 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13373 /* We have an id-expression. */
13374 id_expression_or_member_access_p = true;
13377 if (!id_expression_or_member_access_p)
13379 /* Abort the id-expression parse. */
13380 cp_parser_abort_tentative_parse (parser);
13382 /* Parsing tentatively, again. */
13383 cp_parser_parse_tentatively (parser);
13385 /* Parse a class member access. */
13386 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13387 /*cast_p=*/false, /*decltype*/true,
13388 /*member_access_only_p=*/true, NULL);
13390 if (expr
13391 && expr != error_mark_node
13392 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13393 /* We have an id-expression. */
13394 id_expression_or_member_access_p = true;
13397 if (id_expression_or_member_access_p)
13398 /* We have parsed the complete id-expression or member access. */
13399 cp_parser_parse_definitely (parser);
13400 else
13402 /* Abort our attempt to parse an id-expression or member access
13403 expression. */
13404 cp_parser_abort_tentative_parse (parser);
13406 /* Parse a full expression. */
13407 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13408 /*decltype_p=*/true);
13411 return expr;
13414 /* Parse a `decltype' type. Returns the type.
13416 simple-type-specifier:
13417 decltype ( expression )
13418 C++14 proposal:
13419 decltype ( auto ) */
13421 static tree
13422 cp_parser_decltype (cp_parser *parser)
13424 tree expr;
13425 bool id_expression_or_member_access_p = false;
13426 const char *saved_message;
13427 bool saved_integral_constant_expression_p;
13428 bool saved_non_integral_constant_expression_p;
13429 bool saved_greater_than_is_operator_p;
13430 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13432 if (start_token->type == CPP_DECLTYPE)
13434 /* Already parsed. */
13435 cp_lexer_consume_token (parser->lexer);
13436 return saved_checks_value (start_token->u.tree_check_value);
13439 /* Look for the `decltype' token. */
13440 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13441 return error_mark_node;
13443 /* Parse the opening `('. */
13444 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13445 return error_mark_node;
13447 /* decltype (auto) */
13448 if (cxx_dialect >= cxx14
13449 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13451 cp_lexer_consume_token (parser->lexer);
13452 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13453 return error_mark_node;
13454 expr = make_decltype_auto ();
13455 AUTO_IS_DECLTYPE (expr) = true;
13456 goto rewrite;
13459 /* Types cannot be defined in a `decltype' expression. Save away the
13460 old message. */
13461 saved_message = parser->type_definition_forbidden_message;
13463 /* And create the new one. */
13464 parser->type_definition_forbidden_message
13465 = G_("types may not be defined in %<decltype%> expressions");
13467 /* The restrictions on constant-expressions do not apply inside
13468 decltype expressions. */
13469 saved_integral_constant_expression_p
13470 = parser->integral_constant_expression_p;
13471 saved_non_integral_constant_expression_p
13472 = parser->non_integral_constant_expression_p;
13473 parser->integral_constant_expression_p = false;
13475 /* Within a parenthesized expression, a `>' token is always
13476 the greater-than operator. */
13477 saved_greater_than_is_operator_p
13478 = parser->greater_than_is_operator_p;
13479 parser->greater_than_is_operator_p = true;
13481 /* Do not actually evaluate the expression. */
13482 ++cp_unevaluated_operand;
13484 /* Do not warn about problems with the expression. */
13485 ++c_inhibit_evaluation_warnings;
13487 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13489 /* Go back to evaluating expressions. */
13490 --cp_unevaluated_operand;
13491 --c_inhibit_evaluation_warnings;
13493 /* The `>' token might be the end of a template-id or
13494 template-parameter-list now. */
13495 parser->greater_than_is_operator_p
13496 = saved_greater_than_is_operator_p;
13498 /* Restore the old message and the integral constant expression
13499 flags. */
13500 parser->type_definition_forbidden_message = saved_message;
13501 parser->integral_constant_expression_p
13502 = saved_integral_constant_expression_p;
13503 parser->non_integral_constant_expression_p
13504 = saved_non_integral_constant_expression_p;
13506 /* Parse to the closing `)'. */
13507 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13509 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13510 /*consume_paren=*/true);
13511 return error_mark_node;
13514 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13515 tf_warning_or_error);
13517 rewrite:
13518 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13519 it again. */
13520 start_token->type = CPP_DECLTYPE;
13521 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13522 start_token->u.tree_check_value->value = expr;
13523 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13524 start_token->keyword = RID_MAX;
13525 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13527 return expr;
13530 /* Special member functions [gram.special] */
13532 /* Parse a conversion-function-id.
13534 conversion-function-id:
13535 operator conversion-type-id
13537 Returns an IDENTIFIER_NODE representing the operator. */
13539 static tree
13540 cp_parser_conversion_function_id (cp_parser* parser)
13542 tree type;
13543 tree saved_scope;
13544 tree saved_qualifying_scope;
13545 tree saved_object_scope;
13546 tree pushed_scope = NULL_TREE;
13548 /* Look for the `operator' token. */
13549 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13550 return error_mark_node;
13551 /* When we parse the conversion-type-id, the current scope will be
13552 reset. However, we need that information in able to look up the
13553 conversion function later, so we save it here. */
13554 saved_scope = parser->scope;
13555 saved_qualifying_scope = parser->qualifying_scope;
13556 saved_object_scope = parser->object_scope;
13557 /* We must enter the scope of the class so that the names of
13558 entities declared within the class are available in the
13559 conversion-type-id. For example, consider:
13561 struct S {
13562 typedef int I;
13563 operator I();
13566 S::operator I() { ... }
13568 In order to see that `I' is a type-name in the definition, we
13569 must be in the scope of `S'. */
13570 if (saved_scope)
13571 pushed_scope = push_scope (saved_scope);
13572 /* Parse the conversion-type-id. */
13573 type = cp_parser_conversion_type_id (parser);
13574 /* Leave the scope of the class, if any. */
13575 if (pushed_scope)
13576 pop_scope (pushed_scope);
13577 /* Restore the saved scope. */
13578 parser->scope = saved_scope;
13579 parser->qualifying_scope = saved_qualifying_scope;
13580 parser->object_scope = saved_object_scope;
13581 /* If the TYPE is invalid, indicate failure. */
13582 if (type == error_mark_node)
13583 return error_mark_node;
13584 return mangle_conv_op_name_for_type (type);
13587 /* Parse a conversion-type-id:
13589 conversion-type-id:
13590 type-specifier-seq conversion-declarator [opt]
13592 Returns the TYPE specified. */
13594 static tree
13595 cp_parser_conversion_type_id (cp_parser* parser)
13597 tree attributes;
13598 cp_decl_specifier_seq type_specifiers;
13599 cp_declarator *declarator;
13600 tree type_specified;
13601 const char *saved_message;
13603 /* Parse the attributes. */
13604 attributes = cp_parser_attributes_opt (parser);
13606 saved_message = parser->type_definition_forbidden_message;
13607 parser->type_definition_forbidden_message
13608 = G_("types may not be defined in a conversion-type-id");
13610 /* Parse the type-specifiers. */
13611 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13612 /*is_trailing_return=*/false,
13613 &type_specifiers);
13615 parser->type_definition_forbidden_message = saved_message;
13617 /* If that didn't work, stop. */
13618 if (type_specifiers.type == error_mark_node)
13619 return error_mark_node;
13620 /* Parse the conversion-declarator. */
13621 declarator = cp_parser_conversion_declarator_opt (parser);
13623 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13624 /*initialized=*/0, &attributes);
13625 if (attributes)
13626 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13628 /* Don't give this error when parsing tentatively. This happens to
13629 work because we always parse this definitively once. */
13630 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13631 && type_uses_auto (type_specified))
13633 if (cxx_dialect < cxx14)
13635 error ("invalid use of %<auto%> in conversion operator");
13636 return error_mark_node;
13638 else if (template_parm_scope_p ())
13639 warning (0, "use of %<auto%> in member template "
13640 "conversion operator can never be deduced");
13643 return type_specified;
13646 /* Parse an (optional) conversion-declarator.
13648 conversion-declarator:
13649 ptr-operator conversion-declarator [opt]
13653 static cp_declarator *
13654 cp_parser_conversion_declarator_opt (cp_parser* parser)
13656 enum tree_code code;
13657 tree class_type, std_attributes = NULL_TREE;
13658 cp_cv_quals cv_quals;
13660 /* We don't know if there's a ptr-operator next, or not. */
13661 cp_parser_parse_tentatively (parser);
13662 /* Try the ptr-operator. */
13663 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13664 &std_attributes);
13665 /* If it worked, look for more conversion-declarators. */
13666 if (cp_parser_parse_definitely (parser))
13668 cp_declarator *declarator;
13670 /* Parse another optional declarator. */
13671 declarator = cp_parser_conversion_declarator_opt (parser);
13673 declarator = cp_parser_make_indirect_declarator
13674 (code, class_type, cv_quals, declarator, std_attributes);
13676 return declarator;
13679 return NULL;
13682 /* Parse an (optional) ctor-initializer.
13684 ctor-initializer:
13685 : mem-initializer-list
13687 Returns TRUE iff the ctor-initializer was actually present. */
13689 static bool
13690 cp_parser_ctor_initializer_opt (cp_parser* parser)
13692 /* If the next token is not a `:', then there is no
13693 ctor-initializer. */
13694 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13696 /* Do default initialization of any bases and members. */
13697 if (DECL_CONSTRUCTOR_P (current_function_decl))
13698 finish_mem_initializers (NULL_TREE);
13700 return false;
13703 /* Consume the `:' token. */
13704 cp_lexer_consume_token (parser->lexer);
13705 /* And the mem-initializer-list. */
13706 cp_parser_mem_initializer_list (parser);
13708 return true;
13711 /* Parse a mem-initializer-list.
13713 mem-initializer-list:
13714 mem-initializer ... [opt]
13715 mem-initializer ... [opt] , mem-initializer-list */
13717 static void
13718 cp_parser_mem_initializer_list (cp_parser* parser)
13720 tree mem_initializer_list = NULL_TREE;
13721 tree target_ctor = error_mark_node;
13722 cp_token *token = cp_lexer_peek_token (parser->lexer);
13724 /* Let the semantic analysis code know that we are starting the
13725 mem-initializer-list. */
13726 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13727 error_at (token->location,
13728 "only constructors take member initializers");
13730 /* Loop through the list. */
13731 while (true)
13733 tree mem_initializer;
13735 token = cp_lexer_peek_token (parser->lexer);
13736 /* Parse the mem-initializer. */
13737 mem_initializer = cp_parser_mem_initializer (parser);
13738 /* If the next token is a `...', we're expanding member initializers. */
13739 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13741 /* Consume the `...'. */
13742 cp_lexer_consume_token (parser->lexer);
13744 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13745 can be expanded but members cannot. */
13746 if (mem_initializer != error_mark_node
13747 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13749 error_at (token->location,
13750 "cannot expand initializer for member %<%D%>",
13751 TREE_PURPOSE (mem_initializer));
13752 mem_initializer = error_mark_node;
13755 /* Construct the pack expansion type. */
13756 if (mem_initializer != error_mark_node)
13757 mem_initializer = make_pack_expansion (mem_initializer);
13759 if (target_ctor != error_mark_node
13760 && mem_initializer != error_mark_node)
13762 error ("mem-initializer for %qD follows constructor delegation",
13763 TREE_PURPOSE (mem_initializer));
13764 mem_initializer = error_mark_node;
13766 /* Look for a target constructor. */
13767 if (mem_initializer != error_mark_node
13768 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13769 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13771 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13772 if (mem_initializer_list)
13774 error ("constructor delegation follows mem-initializer for %qD",
13775 TREE_PURPOSE (mem_initializer_list));
13776 mem_initializer = error_mark_node;
13778 target_ctor = mem_initializer;
13780 /* Add it to the list, unless it was erroneous. */
13781 if (mem_initializer != error_mark_node)
13783 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13784 mem_initializer_list = mem_initializer;
13786 /* If the next token is not a `,', we're done. */
13787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13788 break;
13789 /* Consume the `,' token. */
13790 cp_lexer_consume_token (parser->lexer);
13793 /* Perform semantic analysis. */
13794 if (DECL_CONSTRUCTOR_P (current_function_decl))
13795 finish_mem_initializers (mem_initializer_list);
13798 /* Parse a mem-initializer.
13800 mem-initializer:
13801 mem-initializer-id ( expression-list [opt] )
13802 mem-initializer-id braced-init-list
13804 GNU extension:
13806 mem-initializer:
13807 ( expression-list [opt] )
13809 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13810 class) or FIELD_DECL (for a non-static data member) to initialize;
13811 the TREE_VALUE is the expression-list. An empty initialization
13812 list is represented by void_list_node. */
13814 static tree
13815 cp_parser_mem_initializer (cp_parser* parser)
13817 tree mem_initializer_id;
13818 tree expression_list;
13819 tree member;
13820 cp_token *token = cp_lexer_peek_token (parser->lexer);
13822 /* Find out what is being initialized. */
13823 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13825 permerror (token->location,
13826 "anachronistic old-style base class initializer");
13827 mem_initializer_id = NULL_TREE;
13829 else
13831 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13832 if (mem_initializer_id == error_mark_node)
13833 return mem_initializer_id;
13835 member = expand_member_init (mem_initializer_id);
13836 if (member && !DECL_P (member))
13837 in_base_initializer = 1;
13839 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13841 bool expr_non_constant_p;
13842 cp_lexer_set_source_position (parser->lexer);
13843 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13844 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13845 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13846 expression_list = build_tree_list (NULL_TREE, expression_list);
13848 else
13850 vec<tree, va_gc> *vec;
13851 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13852 /*cast_p=*/false,
13853 /*allow_expansion_p=*/true,
13854 /*non_constant_p=*/NULL);
13855 if (vec == NULL)
13856 return error_mark_node;
13857 expression_list = build_tree_list_vec (vec);
13858 release_tree_vector (vec);
13861 if (expression_list == error_mark_node)
13862 return error_mark_node;
13863 if (!expression_list)
13864 expression_list = void_type_node;
13866 in_base_initializer = 0;
13868 return member ? build_tree_list (member, expression_list) : error_mark_node;
13871 /* Parse a mem-initializer-id.
13873 mem-initializer-id:
13874 :: [opt] nested-name-specifier [opt] class-name
13875 decltype-specifier (C++11)
13876 identifier
13878 Returns a TYPE indicating the class to be initialized for the first
13879 production (and the second in C++11). Returns an IDENTIFIER_NODE
13880 indicating the data member to be initialized for the last production. */
13882 static tree
13883 cp_parser_mem_initializer_id (cp_parser* parser)
13885 bool global_scope_p;
13886 bool nested_name_specifier_p;
13887 bool template_p = false;
13888 tree id;
13890 cp_token *token = cp_lexer_peek_token (parser->lexer);
13892 /* `typename' is not allowed in this context ([temp.res]). */
13893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13895 error_at (token->location,
13896 "keyword %<typename%> not allowed in this context (a qualified "
13897 "member initializer is implicitly a type)");
13898 cp_lexer_consume_token (parser->lexer);
13900 /* Look for the optional `::' operator. */
13901 global_scope_p
13902 = (cp_parser_global_scope_opt (parser,
13903 /*current_scope_valid_p=*/false)
13904 != NULL_TREE);
13905 /* Look for the optional nested-name-specifier. The simplest way to
13906 implement:
13908 [temp.res]
13910 The keyword `typename' is not permitted in a base-specifier or
13911 mem-initializer; in these contexts a qualified name that
13912 depends on a template-parameter is implicitly assumed to be a
13913 type name.
13915 is to assume that we have seen the `typename' keyword at this
13916 point. */
13917 nested_name_specifier_p
13918 = (cp_parser_nested_name_specifier_opt (parser,
13919 /*typename_keyword_p=*/true,
13920 /*check_dependency_p=*/true,
13921 /*type_p=*/true,
13922 /*is_declaration=*/true)
13923 != NULL_TREE);
13924 if (nested_name_specifier_p)
13925 template_p = cp_parser_optional_template_keyword (parser);
13926 /* If there is a `::' operator or a nested-name-specifier, then we
13927 are definitely looking for a class-name. */
13928 if (global_scope_p || nested_name_specifier_p)
13929 return cp_parser_class_name (parser,
13930 /*typename_keyword_p=*/true,
13931 /*template_keyword_p=*/template_p,
13932 typename_type,
13933 /*check_dependency_p=*/true,
13934 /*class_head_p=*/false,
13935 /*is_declaration=*/true);
13936 /* Otherwise, we could also be looking for an ordinary identifier. */
13937 cp_parser_parse_tentatively (parser);
13938 if (cp_lexer_next_token_is_decltype (parser->lexer))
13939 /* Try a decltype-specifier. */
13940 id = cp_parser_decltype (parser);
13941 else
13942 /* Otherwise, try a class-name. */
13943 id = cp_parser_class_name (parser,
13944 /*typename_keyword_p=*/true,
13945 /*template_keyword_p=*/false,
13946 none_type,
13947 /*check_dependency_p=*/true,
13948 /*class_head_p=*/false,
13949 /*is_declaration=*/true);
13950 /* If we found one, we're done. */
13951 if (cp_parser_parse_definitely (parser))
13952 return id;
13953 /* Otherwise, look for an ordinary identifier. */
13954 return cp_parser_identifier (parser);
13957 /* Overloading [gram.over] */
13959 /* Parse an operator-function-id.
13961 operator-function-id:
13962 operator operator
13964 Returns an IDENTIFIER_NODE for the operator which is a
13965 human-readable spelling of the identifier, e.g., `operator +'. */
13967 static cp_expr
13968 cp_parser_operator_function_id (cp_parser* parser)
13970 /* Look for the `operator' keyword. */
13971 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13972 return error_mark_node;
13973 /* And then the name of the operator itself. */
13974 return cp_parser_operator (parser);
13977 /* Return an identifier node for a user-defined literal operator.
13978 The suffix identifier is chained to the operator name identifier. */
13980 static tree
13981 cp_literal_operator_id (const char* name)
13983 tree identifier;
13984 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13985 + strlen (name) + 10);
13986 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13987 identifier = get_identifier (buffer);
13989 return identifier;
13992 /* Parse an operator.
13994 operator:
13995 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13996 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13997 || ++ -- , ->* -> () []
13999 GNU Extensions:
14001 operator:
14002 <? >? <?= >?=
14004 Returns an IDENTIFIER_NODE for the operator which is a
14005 human-readable spelling of the identifier, e.g., `operator +'. */
14007 static cp_expr
14008 cp_parser_operator (cp_parser* parser)
14010 tree id = NULL_TREE;
14011 cp_token *token;
14012 bool utf8 = false;
14014 /* Peek at the next token. */
14015 token = cp_lexer_peek_token (parser->lexer);
14017 location_t start_loc = token->location;
14019 /* Figure out which operator we have. */
14020 switch (token->type)
14022 case CPP_KEYWORD:
14024 enum tree_code op;
14026 /* The keyword should be either `new' or `delete'. */
14027 if (token->keyword == RID_NEW)
14028 op = NEW_EXPR;
14029 else if (token->keyword == RID_DELETE)
14030 op = DELETE_EXPR;
14031 else
14032 break;
14034 /* Consume the `new' or `delete' token. */
14035 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14037 /* Peek at the next token. */
14038 token = cp_lexer_peek_token (parser->lexer);
14039 /* If it's a `[' token then this is the array variant of the
14040 operator. */
14041 if (token->type == CPP_OPEN_SQUARE)
14043 /* Consume the `[' token. */
14044 cp_lexer_consume_token (parser->lexer);
14045 /* Look for the `]' token. */
14046 if (cp_token *close_token
14047 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14048 end_loc = close_token->location;
14049 id = ansi_opname (op == NEW_EXPR
14050 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14052 /* Otherwise, we have the non-array variant. */
14053 else
14054 id = ansi_opname (op);
14056 location_t loc = make_location (start_loc, start_loc, end_loc);
14058 return cp_expr (id, loc);
14061 case CPP_PLUS:
14062 id = ansi_opname (PLUS_EXPR);
14063 break;
14065 case CPP_MINUS:
14066 id = ansi_opname (MINUS_EXPR);
14067 break;
14069 case CPP_MULT:
14070 id = ansi_opname (MULT_EXPR);
14071 break;
14073 case CPP_DIV:
14074 id = ansi_opname (TRUNC_DIV_EXPR);
14075 break;
14077 case CPP_MOD:
14078 id = ansi_opname (TRUNC_MOD_EXPR);
14079 break;
14081 case CPP_XOR:
14082 id = ansi_opname (BIT_XOR_EXPR);
14083 break;
14085 case CPP_AND:
14086 id = ansi_opname (BIT_AND_EXPR);
14087 break;
14089 case CPP_OR:
14090 id = ansi_opname (BIT_IOR_EXPR);
14091 break;
14093 case CPP_COMPL:
14094 id = ansi_opname (BIT_NOT_EXPR);
14095 break;
14097 case CPP_NOT:
14098 id = ansi_opname (TRUTH_NOT_EXPR);
14099 break;
14101 case CPP_EQ:
14102 id = ansi_assopname (NOP_EXPR);
14103 break;
14105 case CPP_LESS:
14106 id = ansi_opname (LT_EXPR);
14107 break;
14109 case CPP_GREATER:
14110 id = ansi_opname (GT_EXPR);
14111 break;
14113 case CPP_PLUS_EQ:
14114 id = ansi_assopname (PLUS_EXPR);
14115 break;
14117 case CPP_MINUS_EQ:
14118 id = ansi_assopname (MINUS_EXPR);
14119 break;
14121 case CPP_MULT_EQ:
14122 id = ansi_assopname (MULT_EXPR);
14123 break;
14125 case CPP_DIV_EQ:
14126 id = ansi_assopname (TRUNC_DIV_EXPR);
14127 break;
14129 case CPP_MOD_EQ:
14130 id = ansi_assopname (TRUNC_MOD_EXPR);
14131 break;
14133 case CPP_XOR_EQ:
14134 id = ansi_assopname (BIT_XOR_EXPR);
14135 break;
14137 case CPP_AND_EQ:
14138 id = ansi_assopname (BIT_AND_EXPR);
14139 break;
14141 case CPP_OR_EQ:
14142 id = ansi_assopname (BIT_IOR_EXPR);
14143 break;
14145 case CPP_LSHIFT:
14146 id = ansi_opname (LSHIFT_EXPR);
14147 break;
14149 case CPP_RSHIFT:
14150 id = ansi_opname (RSHIFT_EXPR);
14151 break;
14153 case CPP_LSHIFT_EQ:
14154 id = ansi_assopname (LSHIFT_EXPR);
14155 break;
14157 case CPP_RSHIFT_EQ:
14158 id = ansi_assopname (RSHIFT_EXPR);
14159 break;
14161 case CPP_EQ_EQ:
14162 id = ansi_opname (EQ_EXPR);
14163 break;
14165 case CPP_NOT_EQ:
14166 id = ansi_opname (NE_EXPR);
14167 break;
14169 case CPP_LESS_EQ:
14170 id = ansi_opname (LE_EXPR);
14171 break;
14173 case CPP_GREATER_EQ:
14174 id = ansi_opname (GE_EXPR);
14175 break;
14177 case CPP_AND_AND:
14178 id = ansi_opname (TRUTH_ANDIF_EXPR);
14179 break;
14181 case CPP_OR_OR:
14182 id = ansi_opname (TRUTH_ORIF_EXPR);
14183 break;
14185 case CPP_PLUS_PLUS:
14186 id = ansi_opname (POSTINCREMENT_EXPR);
14187 break;
14189 case CPP_MINUS_MINUS:
14190 id = ansi_opname (PREDECREMENT_EXPR);
14191 break;
14193 case CPP_COMMA:
14194 id = ansi_opname (COMPOUND_EXPR);
14195 break;
14197 case CPP_DEREF_STAR:
14198 id = ansi_opname (MEMBER_REF);
14199 break;
14201 case CPP_DEREF:
14202 id = ansi_opname (COMPONENT_REF);
14203 break;
14205 case CPP_OPEN_PAREN:
14206 /* Consume the `('. */
14207 cp_lexer_consume_token (parser->lexer);
14208 /* Look for the matching `)'. */
14209 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14210 return ansi_opname (CALL_EXPR);
14212 case CPP_OPEN_SQUARE:
14213 /* Consume the `['. */
14214 cp_lexer_consume_token (parser->lexer);
14215 /* Look for the matching `]'. */
14216 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14217 return ansi_opname (ARRAY_REF);
14219 case CPP_UTF8STRING:
14220 case CPP_UTF8STRING_USERDEF:
14221 utf8 = true;
14222 /* FALLTHRU */
14223 case CPP_STRING:
14224 case CPP_WSTRING:
14225 case CPP_STRING16:
14226 case CPP_STRING32:
14227 case CPP_STRING_USERDEF:
14228 case CPP_WSTRING_USERDEF:
14229 case CPP_STRING16_USERDEF:
14230 case CPP_STRING32_USERDEF:
14232 tree str, string_tree;
14233 int sz, len;
14235 if (cxx_dialect == cxx98)
14236 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14238 /* Consume the string. */
14239 str = cp_parser_string_literal (parser, /*translate=*/true,
14240 /*wide_ok=*/true, /*lookup_udlit=*/false);
14241 if (str == error_mark_node)
14242 return error_mark_node;
14243 else if (TREE_CODE (str) == USERDEF_LITERAL)
14245 string_tree = USERDEF_LITERAL_VALUE (str);
14246 id = USERDEF_LITERAL_SUFFIX_ID (str);
14248 else
14250 string_tree = str;
14251 /* Look for the suffix identifier. */
14252 token = cp_lexer_peek_token (parser->lexer);
14253 if (token->type == CPP_NAME)
14254 id = cp_parser_identifier (parser);
14255 else if (token->type == CPP_KEYWORD)
14257 error ("unexpected keyword;"
14258 " remove space between quotes and suffix identifier");
14259 return error_mark_node;
14261 else
14263 error ("expected suffix identifier");
14264 return error_mark_node;
14267 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14268 (TREE_TYPE (TREE_TYPE (string_tree))));
14269 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14270 if (len != 0)
14272 error ("expected empty string after %<operator%> keyword");
14273 return error_mark_node;
14275 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14276 != char_type_node)
14278 error ("invalid encoding prefix in literal operator");
14279 return error_mark_node;
14281 if (id != error_mark_node)
14283 const char *name = IDENTIFIER_POINTER (id);
14284 id = cp_literal_operator_id (name);
14286 return id;
14289 default:
14290 /* Anything else is an error. */
14291 break;
14294 /* If we have selected an identifier, we need to consume the
14295 operator token. */
14296 if (id)
14297 cp_lexer_consume_token (parser->lexer);
14298 /* Otherwise, no valid operator name was present. */
14299 else
14301 cp_parser_error (parser, "expected operator");
14302 id = error_mark_node;
14305 return cp_expr (id, start_loc);
14308 /* Parse a template-declaration.
14310 template-declaration:
14311 export [opt] template < template-parameter-list > declaration
14313 If MEMBER_P is TRUE, this template-declaration occurs within a
14314 class-specifier.
14316 The grammar rule given by the standard isn't correct. What
14317 is really meant is:
14319 template-declaration:
14320 export [opt] template-parameter-list-seq
14321 decl-specifier-seq [opt] init-declarator [opt] ;
14322 export [opt] template-parameter-list-seq
14323 function-definition
14325 template-parameter-list-seq:
14326 template-parameter-list-seq [opt]
14327 template < template-parameter-list >
14329 Concept Extensions:
14331 template-parameter-list-seq:
14332 template < template-parameter-list > requires-clause [opt]
14334 requires-clause:
14335 requires logical-or-expression */
14337 static void
14338 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14340 /* Check for `export'. */
14341 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14343 /* Consume the `export' token. */
14344 cp_lexer_consume_token (parser->lexer);
14345 /* Warn that we do not support `export'. */
14346 warning (0, "keyword %<export%> not implemented, and will be ignored");
14349 cp_parser_template_declaration_after_export (parser, member_p);
14352 /* Parse a template-parameter-list.
14354 template-parameter-list:
14355 template-parameter
14356 template-parameter-list , template-parameter
14358 Returns a TREE_LIST. Each node represents a template parameter.
14359 The nodes are connected via their TREE_CHAINs. */
14361 static tree
14362 cp_parser_template_parameter_list (cp_parser* parser)
14364 tree parameter_list = NULL_TREE;
14366 begin_template_parm_list ();
14368 /* The loop below parses the template parms. We first need to know
14369 the total number of template parms to be able to compute proper
14370 canonical types of each dependent type. So after the loop, when
14371 we know the total number of template parms,
14372 end_template_parm_list computes the proper canonical types and
14373 fixes up the dependent types accordingly. */
14374 while (true)
14376 tree parameter;
14377 bool is_non_type;
14378 bool is_parameter_pack;
14379 location_t parm_loc;
14381 /* Parse the template-parameter. */
14382 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14383 parameter = cp_parser_template_parameter (parser,
14384 &is_non_type,
14385 &is_parameter_pack);
14386 /* Add it to the list. */
14387 if (parameter != error_mark_node)
14388 parameter_list = process_template_parm (parameter_list,
14389 parm_loc,
14390 parameter,
14391 is_non_type,
14392 is_parameter_pack);
14393 else
14395 tree err_parm = build_tree_list (parameter, parameter);
14396 parameter_list = chainon (parameter_list, err_parm);
14399 /* If the next token is not a `,', we're done. */
14400 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14401 break;
14402 /* Otherwise, consume the `,' token. */
14403 cp_lexer_consume_token (parser->lexer);
14406 return end_template_parm_list (parameter_list);
14409 /* Parse a introduction-list.
14411 introduction-list:
14412 introduced-parameter
14413 introduction-list , introduced-parameter
14415 introduced-parameter:
14416 ...[opt] identifier
14418 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14419 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14420 WILDCARD_DECL will also have DECL_NAME set and token location in
14421 DECL_SOURCE_LOCATION. */
14423 static tree
14424 cp_parser_introduction_list (cp_parser *parser)
14426 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14428 while (true)
14430 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14431 if (is_pack)
14432 cp_lexer_consume_token (parser->lexer);
14434 /* Build placeholder. */
14435 tree parm = build_nt (WILDCARD_DECL);
14436 DECL_SOURCE_LOCATION (parm)
14437 = cp_lexer_peek_token (parser->lexer)->location;
14438 DECL_NAME (parm) = cp_parser_identifier (parser);
14439 WILDCARD_PACK_P (parm) = is_pack;
14440 vec_safe_push (introduction_vec, parm);
14442 /* If the next token is not a `,', we're done. */
14443 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14444 break;
14445 /* Otherwise, consume the `,' token. */
14446 cp_lexer_consume_token (parser->lexer);
14449 /* Convert the vec into a TREE_VEC. */
14450 tree introduction_list = make_tree_vec (introduction_vec->length ());
14451 unsigned int n;
14452 tree parm;
14453 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14454 TREE_VEC_ELT (introduction_list, n) = parm;
14456 release_tree_vector (introduction_vec);
14457 return introduction_list;
14460 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14461 is an abstract declarator. */
14463 static inline cp_declarator*
14464 get_id_declarator (cp_declarator *declarator)
14466 cp_declarator *d = declarator;
14467 while (d && d->kind != cdk_id)
14468 d = d->declarator;
14469 return d;
14472 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14473 is an abstract declarator. */
14475 static inline tree
14476 get_unqualified_id (cp_declarator *declarator)
14478 declarator = get_id_declarator (declarator);
14479 if (declarator)
14480 return declarator->u.id.unqualified_name;
14481 else
14482 return NULL_TREE;
14485 /* Returns true if DECL represents a constrained-parameter. */
14487 static inline bool
14488 is_constrained_parameter (tree decl)
14490 return (decl
14491 && TREE_CODE (decl) == TYPE_DECL
14492 && CONSTRAINED_PARM_CONCEPT (decl)
14493 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14496 /* Returns true if PARM declares a constrained-parameter. */
14498 static inline bool
14499 is_constrained_parameter (cp_parameter_declarator *parm)
14501 return is_constrained_parameter (parm->decl_specifiers.type);
14504 /* Check that the type parameter is only a declarator-id, and that its
14505 type is not cv-qualified. */
14507 bool
14508 cp_parser_check_constrained_type_parm (cp_parser *parser,
14509 cp_parameter_declarator *parm)
14511 if (!parm->declarator)
14512 return true;
14514 if (parm->declarator->kind != cdk_id)
14516 cp_parser_error (parser, "invalid constrained type parameter");
14517 return false;
14520 /* Don't allow cv-qualified type parameters. */
14521 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14522 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14524 cp_parser_error (parser, "cv-qualified type parameter");
14525 return false;
14528 return true;
14531 /* Finish parsing/processing a template type parameter and checking
14532 various restrictions. */
14534 static inline tree
14535 cp_parser_constrained_type_template_parm (cp_parser *parser,
14536 tree id,
14537 cp_parameter_declarator* parmdecl)
14539 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14540 return finish_template_type_parm (class_type_node, id);
14541 else
14542 return error_mark_node;
14545 static tree
14546 finish_constrained_template_template_parm (tree proto, tree id)
14548 /* FIXME: This should probably be copied, and we may need to adjust
14549 the template parameter depths. */
14550 tree saved_parms = current_template_parms;
14551 begin_template_parm_list ();
14552 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14553 end_template_parm_list ();
14555 tree parm = finish_template_template_parm (class_type_node, id);
14556 current_template_parms = saved_parms;
14558 return parm;
14561 /* Finish parsing/processing a template template parameter by borrowing
14562 the template parameter list from the prototype parameter. */
14564 static tree
14565 cp_parser_constrained_template_template_parm (cp_parser *parser,
14566 tree proto,
14567 tree id,
14568 cp_parameter_declarator *parmdecl)
14570 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14571 return error_mark_node;
14572 return finish_constrained_template_template_parm (proto, id);
14575 /* Create a new non-type template parameter from the given PARM
14576 declarator. */
14578 static tree
14579 constrained_non_type_template_parm (bool *is_non_type,
14580 cp_parameter_declarator *parm)
14582 *is_non_type = true;
14583 cp_declarator *decl = parm->declarator;
14584 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14585 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14586 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14589 /* Build a constrained template parameter based on the PARMDECL
14590 declarator. The type of PARMDECL is the constrained type, which
14591 refers to the prototype template parameter that ultimately
14592 specifies the type of the declared parameter. */
14594 static tree
14595 finish_constrained_parameter (cp_parser *parser,
14596 cp_parameter_declarator *parmdecl,
14597 bool *is_non_type,
14598 bool *is_parameter_pack)
14600 tree decl = parmdecl->decl_specifiers.type;
14601 tree id = get_unqualified_id (parmdecl->declarator);
14602 tree def = parmdecl->default_argument;
14603 tree proto = DECL_INITIAL (decl);
14605 /* A template parameter constrained by a variadic concept shall also
14606 be declared as a template parameter pack. */
14607 bool is_variadic = template_parameter_pack_p (proto);
14608 if (is_variadic && !*is_parameter_pack)
14609 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14611 /* Build the parameter. Return an error if the declarator was invalid. */
14612 tree parm;
14613 if (TREE_CODE (proto) == TYPE_DECL)
14614 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14615 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14616 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14617 parmdecl);
14618 else
14619 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14620 if (parm == error_mark_node)
14621 return error_mark_node;
14623 /* Finish the parameter decl and create a node attaching the
14624 default argument and constraint. */
14625 parm = build_tree_list (def, parm);
14626 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14628 return parm;
14631 /* Returns true if the parsed type actually represents the declaration
14632 of a type template-parameter. */
14634 static inline bool
14635 declares_constrained_type_template_parameter (tree type)
14637 return (is_constrained_parameter (type)
14638 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14642 /* Returns true if the parsed type actually represents the declaration of
14643 a template template-parameter. */
14645 static bool
14646 declares_constrained_template_template_parameter (tree type)
14648 return (is_constrained_parameter (type)
14649 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14652 /* Parse a default argument for a type template-parameter.
14653 Note that diagnostics are handled in cp_parser_template_parameter. */
14655 static tree
14656 cp_parser_default_type_template_argument (cp_parser *parser)
14658 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14660 /* Consume the `=' token. */
14661 cp_lexer_consume_token (parser->lexer);
14663 cp_token *token = cp_lexer_peek_token (parser->lexer);
14665 /* Parse the default-argument. */
14666 push_deferring_access_checks (dk_no_deferred);
14667 tree default_argument = cp_parser_type_id (parser);
14668 pop_deferring_access_checks ();
14670 if (flag_concepts && type_uses_auto (default_argument))
14672 error_at (token->location,
14673 "invalid use of %<auto%> in default template argument");
14674 return error_mark_node;
14677 return default_argument;
14680 /* Parse a default argument for a template template-parameter. */
14682 static tree
14683 cp_parser_default_template_template_argument (cp_parser *parser)
14685 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14687 bool is_template;
14689 /* Consume the `='. */
14690 cp_lexer_consume_token (parser->lexer);
14691 /* Parse the id-expression. */
14692 push_deferring_access_checks (dk_no_deferred);
14693 /* save token before parsing the id-expression, for error
14694 reporting */
14695 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14696 tree default_argument
14697 = cp_parser_id_expression (parser,
14698 /*template_keyword_p=*/false,
14699 /*check_dependency_p=*/true,
14700 /*template_p=*/&is_template,
14701 /*declarator_p=*/false,
14702 /*optional_p=*/false);
14703 if (TREE_CODE (default_argument) == TYPE_DECL)
14704 /* If the id-expression was a template-id that refers to
14705 a template-class, we already have the declaration here,
14706 so no further lookup is needed. */
14708 else
14709 /* Look up the name. */
14710 default_argument
14711 = cp_parser_lookup_name (parser, default_argument,
14712 none_type,
14713 /*is_template=*/is_template,
14714 /*is_namespace=*/false,
14715 /*check_dependency=*/true,
14716 /*ambiguous_decls=*/NULL,
14717 token->location);
14718 /* See if the default argument is valid. */
14719 default_argument = check_template_template_default_arg (default_argument);
14720 pop_deferring_access_checks ();
14721 return default_argument;
14724 /* Parse a template-parameter.
14726 template-parameter:
14727 type-parameter
14728 parameter-declaration
14730 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14731 the parameter. The TREE_PURPOSE is the default value, if any.
14732 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14733 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14734 set to true iff this parameter is a parameter pack. */
14736 static tree
14737 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14738 bool *is_parameter_pack)
14740 cp_token *token;
14741 cp_parameter_declarator *parameter_declarator;
14742 tree parm;
14744 /* Assume it is a type parameter or a template parameter. */
14745 *is_non_type = false;
14746 /* Assume it not a parameter pack. */
14747 *is_parameter_pack = false;
14748 /* Peek at the next token. */
14749 token = cp_lexer_peek_token (parser->lexer);
14750 /* If it is `class' or `template', we have a type-parameter. */
14751 if (token->keyword == RID_TEMPLATE)
14752 return cp_parser_type_parameter (parser, is_parameter_pack);
14753 /* If it is `class' or `typename' we do not know yet whether it is a
14754 type parameter or a non-type parameter. Consider:
14756 template <typename T, typename T::X X> ...
14760 template <class C, class D*> ...
14762 Here, the first parameter is a type parameter, and the second is
14763 a non-type parameter. We can tell by looking at the token after
14764 the identifier -- if it is a `,', `=', or `>' then we have a type
14765 parameter. */
14766 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14768 /* Peek at the token after `class' or `typename'. */
14769 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14770 /* If it's an ellipsis, we have a template type parameter
14771 pack. */
14772 if (token->type == CPP_ELLIPSIS)
14773 return cp_parser_type_parameter (parser, is_parameter_pack);
14774 /* If it's an identifier, skip it. */
14775 if (token->type == CPP_NAME)
14776 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14777 /* Now, see if the token looks like the end of a template
14778 parameter. */
14779 if (token->type == CPP_COMMA
14780 || token->type == CPP_EQ
14781 || token->type == CPP_GREATER)
14782 return cp_parser_type_parameter (parser, is_parameter_pack);
14785 /* Otherwise, it is a non-type parameter or a constrained parameter.
14787 [temp.param]
14789 When parsing a default template-argument for a non-type
14790 template-parameter, the first non-nested `>' is taken as the end
14791 of the template parameter-list rather than a greater-than
14792 operator. */
14793 parameter_declarator
14794 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14795 /*parenthesized_p=*/NULL);
14797 if (!parameter_declarator)
14798 return error_mark_node;
14800 /* If the parameter declaration is marked as a parameter pack, set
14801 *IS_PARAMETER_PACK to notify the caller. */
14802 if (parameter_declarator->template_parameter_pack_p)
14803 *is_parameter_pack = true;
14805 if (parameter_declarator->default_argument)
14807 /* Can happen in some cases of erroneous input (c++/34892). */
14808 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14809 /* Consume the `...' for better error recovery. */
14810 cp_lexer_consume_token (parser->lexer);
14813 // The parameter may have been constrained.
14814 if (is_constrained_parameter (parameter_declarator))
14815 return finish_constrained_parameter (parser,
14816 parameter_declarator,
14817 is_non_type,
14818 is_parameter_pack);
14820 // Now we're sure that the parameter is a non-type parameter.
14821 *is_non_type = true;
14823 parm = grokdeclarator (parameter_declarator->declarator,
14824 &parameter_declarator->decl_specifiers,
14825 TPARM, /*initialized=*/0,
14826 /*attrlist=*/NULL);
14827 if (parm == error_mark_node)
14828 return error_mark_node;
14830 return build_tree_list (parameter_declarator->default_argument, parm);
14833 /* Parse a type-parameter.
14835 type-parameter:
14836 class identifier [opt]
14837 class identifier [opt] = type-id
14838 typename identifier [opt]
14839 typename identifier [opt] = type-id
14840 template < template-parameter-list > class identifier [opt]
14841 template < template-parameter-list > class identifier [opt]
14842 = id-expression
14844 GNU Extension (variadic templates):
14846 type-parameter:
14847 class ... identifier [opt]
14848 typename ... identifier [opt]
14850 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14851 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14852 the declaration of the parameter.
14854 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14856 static tree
14857 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14859 cp_token *token;
14860 tree parameter;
14862 /* Look for a keyword to tell us what kind of parameter this is. */
14863 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14864 if (!token)
14865 return error_mark_node;
14867 switch (token->keyword)
14869 case RID_CLASS:
14870 case RID_TYPENAME:
14872 tree identifier;
14873 tree default_argument;
14875 /* If the next token is an ellipsis, we have a template
14876 argument pack. */
14877 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14879 /* Consume the `...' token. */
14880 cp_lexer_consume_token (parser->lexer);
14881 maybe_warn_variadic_templates ();
14883 *is_parameter_pack = true;
14886 /* If the next token is an identifier, then it names the
14887 parameter. */
14888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14889 identifier = cp_parser_identifier (parser);
14890 else
14891 identifier = NULL_TREE;
14893 /* Create the parameter. */
14894 parameter = finish_template_type_parm (class_type_node, identifier);
14896 /* If the next token is an `=', we have a default argument. */
14897 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14899 default_argument
14900 = cp_parser_default_type_template_argument (parser);
14902 /* Template parameter packs cannot have default
14903 arguments. */
14904 if (*is_parameter_pack)
14906 if (identifier)
14907 error_at (token->location,
14908 "template parameter pack %qD cannot have a "
14909 "default argument", identifier);
14910 else
14911 error_at (token->location,
14912 "template parameter packs cannot have "
14913 "default arguments");
14914 default_argument = NULL_TREE;
14916 else if (check_for_bare_parameter_packs (default_argument))
14917 default_argument = error_mark_node;
14919 else
14920 default_argument = NULL_TREE;
14922 /* Create the combined representation of the parameter and the
14923 default argument. */
14924 parameter = build_tree_list (default_argument, parameter);
14926 break;
14928 case RID_TEMPLATE:
14930 tree identifier;
14931 tree default_argument;
14933 /* Look for the `<'. */
14934 cp_parser_require (parser, CPP_LESS, RT_LESS);
14935 /* Parse the template-parameter-list. */
14936 cp_parser_template_parameter_list (parser);
14937 /* Look for the `>'. */
14938 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14940 // If template requirements are present, parse them.
14941 if (flag_concepts)
14943 tree reqs = get_shorthand_constraints (current_template_parms);
14944 if (tree r = cp_parser_requires_clause_opt (parser))
14945 reqs = conjoin_constraints (reqs, normalize_expression (r));
14946 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14949 /* Look for the `class' or 'typename' keywords. */
14950 cp_parser_type_parameter_key (parser);
14951 /* If the next token is an ellipsis, we have a template
14952 argument pack. */
14953 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14955 /* Consume the `...' token. */
14956 cp_lexer_consume_token (parser->lexer);
14957 maybe_warn_variadic_templates ();
14959 *is_parameter_pack = true;
14961 /* If the next token is an `=', then there is a
14962 default-argument. If the next token is a `>', we are at
14963 the end of the parameter-list. If the next token is a `,',
14964 then we are at the end of this parameter. */
14965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14966 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14967 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14969 identifier = cp_parser_identifier (parser);
14970 /* Treat invalid names as if the parameter were nameless. */
14971 if (identifier == error_mark_node)
14972 identifier = NULL_TREE;
14974 else
14975 identifier = NULL_TREE;
14977 /* Create the template parameter. */
14978 parameter = finish_template_template_parm (class_type_node,
14979 identifier);
14981 /* If the next token is an `=', then there is a
14982 default-argument. */
14983 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14985 default_argument
14986 = cp_parser_default_template_template_argument (parser);
14988 /* Template parameter packs cannot have default
14989 arguments. */
14990 if (*is_parameter_pack)
14992 if (identifier)
14993 error_at (token->location,
14994 "template parameter pack %qD cannot "
14995 "have a default argument",
14996 identifier);
14997 else
14998 error_at (token->location, "template parameter packs cannot "
14999 "have default arguments");
15000 default_argument = NULL_TREE;
15003 else
15004 default_argument = NULL_TREE;
15006 /* Create the combined representation of the parameter and the
15007 default argument. */
15008 parameter = build_tree_list (default_argument, parameter);
15010 break;
15012 default:
15013 gcc_unreachable ();
15014 break;
15017 return parameter;
15020 /* Parse a template-id.
15022 template-id:
15023 template-name < template-argument-list [opt] >
15025 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15026 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15027 returned. Otherwise, if the template-name names a function, or set
15028 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15029 names a class, returns a TYPE_DECL for the specialization.
15031 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15032 uninstantiated templates. */
15034 static tree
15035 cp_parser_template_id (cp_parser *parser,
15036 bool template_keyword_p,
15037 bool check_dependency_p,
15038 enum tag_types tag_type,
15039 bool is_declaration)
15041 tree templ;
15042 tree arguments;
15043 tree template_id;
15044 cp_token_position start_of_id = 0;
15045 cp_token *next_token = NULL, *next_token_2 = NULL;
15046 bool is_identifier;
15048 /* If the next token corresponds to a template-id, there is no need
15049 to reparse it. */
15050 next_token = cp_lexer_peek_token (parser->lexer);
15051 if (next_token->type == CPP_TEMPLATE_ID)
15053 cp_lexer_consume_token (parser->lexer);
15054 return saved_checks_value (next_token->u.tree_check_value);
15057 /* Avoid performing name lookup if there is no possibility of
15058 finding a template-id. */
15059 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15060 || (next_token->type == CPP_NAME
15061 && !cp_parser_nth_token_starts_template_argument_list_p
15062 (parser, 2)))
15064 cp_parser_error (parser, "expected template-id");
15065 return error_mark_node;
15068 /* Remember where the template-id starts. */
15069 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15070 start_of_id = cp_lexer_token_position (parser->lexer, false);
15072 push_deferring_access_checks (dk_deferred);
15074 /* Parse the template-name. */
15075 is_identifier = false;
15076 templ = cp_parser_template_name (parser, template_keyword_p,
15077 check_dependency_p,
15078 is_declaration,
15079 tag_type,
15080 &is_identifier);
15081 if (templ == error_mark_node || is_identifier)
15083 pop_deferring_access_checks ();
15084 return templ;
15087 /* Since we're going to preserve any side-effects from this parse, set up a
15088 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15089 in the template arguments. */
15090 tentative_firewall firewall (parser);
15092 /* If we find the sequence `[:' after a template-name, it's probably
15093 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15094 parse correctly the argument list. */
15095 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15096 == CPP_OPEN_SQUARE)
15097 && next_token->flags & DIGRAPH
15098 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15099 == CPP_COLON)
15100 && !(next_token_2->flags & PREV_WHITE))
15102 cp_parser_parse_tentatively (parser);
15103 /* Change `:' into `::'. */
15104 next_token_2->type = CPP_SCOPE;
15105 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15106 CPP_LESS. */
15107 cp_lexer_consume_token (parser->lexer);
15109 /* Parse the arguments. */
15110 arguments = cp_parser_enclosed_template_argument_list (parser);
15111 if (!cp_parser_parse_definitely (parser))
15113 /* If we couldn't parse an argument list, then we revert our changes
15114 and return simply an error. Maybe this is not a template-id
15115 after all. */
15116 next_token_2->type = CPP_COLON;
15117 cp_parser_error (parser, "expected %<<%>");
15118 pop_deferring_access_checks ();
15119 return error_mark_node;
15121 /* Otherwise, emit an error about the invalid digraph, but continue
15122 parsing because we got our argument list. */
15123 if (permerror (next_token->location,
15124 "%<<::%> cannot begin a template-argument list"))
15126 static bool hint = false;
15127 inform (next_token->location,
15128 "%<<:%> is an alternate spelling for %<[%>."
15129 " Insert whitespace between %<<%> and %<::%>");
15130 if (!hint && !flag_permissive)
15132 inform (next_token->location, "(if you use %<-fpermissive%> "
15133 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15134 "accept your code)");
15135 hint = true;
15139 else
15141 /* Look for the `<' that starts the template-argument-list. */
15142 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15144 pop_deferring_access_checks ();
15145 return error_mark_node;
15147 /* Parse the arguments. */
15148 arguments = cp_parser_enclosed_template_argument_list (parser);
15151 /* Build a representation of the specialization. */
15152 if (identifier_p (templ))
15153 template_id = build_min_nt_loc (next_token->location,
15154 TEMPLATE_ID_EXPR,
15155 templ, arguments);
15156 else if (DECL_TYPE_TEMPLATE_P (templ)
15157 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15159 bool entering_scope;
15160 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15161 template (rather than some instantiation thereof) only if
15162 is not nested within some other construct. For example, in
15163 "template <typename T> void f(T) { A<T>::", A<T> is just an
15164 instantiation of A. */
15165 entering_scope = (template_parm_scope_p ()
15166 && cp_lexer_next_token_is (parser->lexer,
15167 CPP_SCOPE));
15168 template_id
15169 = finish_template_type (templ, arguments, entering_scope);
15171 /* A template-like identifier may be a partial concept id. */
15172 else if (flag_concepts
15173 && (template_id = (cp_parser_maybe_partial_concept_id
15174 (parser, templ, arguments))))
15175 return template_id;
15176 else if (variable_template_p (templ))
15178 template_id = lookup_template_variable (templ, arguments);
15179 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15180 SET_EXPR_LOCATION (template_id, next_token->location);
15182 else
15184 /* If it's not a class-template or a template-template, it should be
15185 a function-template. */
15186 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15187 || TREE_CODE (templ) == OVERLOAD
15188 || BASELINK_P (templ)));
15190 template_id = lookup_template_function (templ, arguments);
15191 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15192 SET_EXPR_LOCATION (template_id, next_token->location);
15195 /* If parsing tentatively, replace the sequence of tokens that makes
15196 up the template-id with a CPP_TEMPLATE_ID token. That way,
15197 should we re-parse the token stream, we will not have to repeat
15198 the effort required to do the parse, nor will we issue duplicate
15199 error messages about problems during instantiation of the
15200 template. */
15201 if (start_of_id
15202 /* Don't do this if we had a parse error in a declarator; re-parsing
15203 might succeed if a name changes meaning (60361). */
15204 && !(cp_parser_error_occurred (parser)
15205 && cp_parser_parsing_tentatively (parser)
15206 && parser->in_declarator_p))
15208 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15210 /* Reset the contents of the START_OF_ID token. */
15211 token->type = CPP_TEMPLATE_ID;
15213 /* Update the location to be of the form:
15214 template-name < template-argument-list [opt] >
15215 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15216 with caret == start at the start of the template-name,
15217 ranging until the closing '>'. */
15218 location_t finish_loc
15219 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15220 location_t combined_loc
15221 = make_location (token->location, token->location, finish_loc);
15222 token->location = combined_loc;
15224 /* Retrieve any deferred checks. Do not pop this access checks yet
15225 so the memory will not be reclaimed during token replacing below. */
15226 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15227 token->u.tree_check_value->value = template_id;
15228 token->u.tree_check_value->checks = get_deferred_access_checks ();
15229 token->keyword = RID_MAX;
15231 /* Purge all subsequent tokens. */
15232 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15234 /* ??? Can we actually assume that, if template_id ==
15235 error_mark_node, we will have issued a diagnostic to the
15236 user, as opposed to simply marking the tentative parse as
15237 failed? */
15238 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15239 error_at (token->location, "parse error in template argument list");
15242 pop_to_parent_deferring_access_checks ();
15243 return template_id;
15246 /* Parse a template-name.
15248 template-name:
15249 identifier
15251 The standard should actually say:
15253 template-name:
15254 identifier
15255 operator-function-id
15257 A defect report has been filed about this issue.
15259 A conversion-function-id cannot be a template name because they cannot
15260 be part of a template-id. In fact, looking at this code:
15262 a.operator K<int>()
15264 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15265 It is impossible to call a templated conversion-function-id with an
15266 explicit argument list, since the only allowed template parameter is
15267 the type to which it is converting.
15269 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15270 `template' keyword, in a construction like:
15272 T::template f<3>()
15274 In that case `f' is taken to be a template-name, even though there
15275 is no way of knowing for sure.
15277 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15278 name refers to a set of overloaded functions, at least one of which
15279 is a template, or an IDENTIFIER_NODE with the name of the template,
15280 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15281 names are looked up inside uninstantiated templates. */
15283 static tree
15284 cp_parser_template_name (cp_parser* parser,
15285 bool template_keyword_p,
15286 bool check_dependency_p,
15287 bool is_declaration,
15288 enum tag_types tag_type,
15289 bool *is_identifier)
15291 tree identifier;
15292 tree decl;
15293 tree fns;
15294 cp_token *token = cp_lexer_peek_token (parser->lexer);
15296 /* If the next token is `operator', then we have either an
15297 operator-function-id or a conversion-function-id. */
15298 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15300 /* We don't know whether we're looking at an
15301 operator-function-id or a conversion-function-id. */
15302 cp_parser_parse_tentatively (parser);
15303 /* Try an operator-function-id. */
15304 identifier = cp_parser_operator_function_id (parser);
15305 /* If that didn't work, try a conversion-function-id. */
15306 if (!cp_parser_parse_definitely (parser))
15308 cp_parser_error (parser, "expected template-name");
15309 return error_mark_node;
15312 /* Look for the identifier. */
15313 else
15314 identifier = cp_parser_identifier (parser);
15316 /* If we didn't find an identifier, we don't have a template-id. */
15317 if (identifier == error_mark_node)
15318 return error_mark_node;
15320 /* If the name immediately followed the `template' keyword, then it
15321 is a template-name. However, if the next token is not `<', then
15322 we do not treat it as a template-name, since it is not being used
15323 as part of a template-id. This enables us to handle constructs
15324 like:
15326 template <typename T> struct S { S(); };
15327 template <typename T> S<T>::S();
15329 correctly. We would treat `S' as a template -- if it were `S<T>'
15330 -- but we do not if there is no `<'. */
15332 if (processing_template_decl
15333 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15335 /* In a declaration, in a dependent context, we pretend that the
15336 "template" keyword was present in order to improve error
15337 recovery. For example, given:
15339 template <typename T> void f(T::X<int>);
15341 we want to treat "X<int>" as a template-id. */
15342 if (is_declaration
15343 && !template_keyword_p
15344 && parser->scope && TYPE_P (parser->scope)
15345 && check_dependency_p
15346 && dependent_scope_p (parser->scope)
15347 /* Do not do this for dtors (or ctors), since they never
15348 need the template keyword before their name. */
15349 && !constructor_name_p (identifier, parser->scope))
15351 cp_token_position start = 0;
15353 /* Explain what went wrong. */
15354 error_at (token->location, "non-template %qD used as template",
15355 identifier);
15356 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15357 parser->scope, identifier);
15358 /* If parsing tentatively, find the location of the "<" token. */
15359 if (cp_parser_simulate_error (parser))
15360 start = cp_lexer_token_position (parser->lexer, true);
15361 /* Parse the template arguments so that we can issue error
15362 messages about them. */
15363 cp_lexer_consume_token (parser->lexer);
15364 cp_parser_enclosed_template_argument_list (parser);
15365 /* Skip tokens until we find a good place from which to
15366 continue parsing. */
15367 cp_parser_skip_to_closing_parenthesis (parser,
15368 /*recovering=*/true,
15369 /*or_comma=*/true,
15370 /*consume_paren=*/false);
15371 /* If parsing tentatively, permanently remove the
15372 template argument list. That will prevent duplicate
15373 error messages from being issued about the missing
15374 "template" keyword. */
15375 if (start)
15376 cp_lexer_purge_tokens_after (parser->lexer, start);
15377 if (is_identifier)
15378 *is_identifier = true;
15379 return identifier;
15382 /* If the "template" keyword is present, then there is generally
15383 no point in doing name-lookup, so we just return IDENTIFIER.
15384 But, if the qualifying scope is non-dependent then we can
15385 (and must) do name-lookup normally. */
15386 if (template_keyword_p
15387 && (!parser->scope
15388 || (TYPE_P (parser->scope)
15389 && dependent_type_p (parser->scope))))
15390 return identifier;
15393 /* Look up the name. */
15394 decl = cp_parser_lookup_name (parser, identifier,
15395 tag_type,
15396 /*is_template=*/true,
15397 /*is_namespace=*/false,
15398 check_dependency_p,
15399 /*ambiguous_decls=*/NULL,
15400 token->location);
15402 decl = strip_using_decl (decl);
15404 /* If DECL is a template, then the name was a template-name. */
15405 if (TREE_CODE (decl) == TEMPLATE_DECL)
15407 if (TREE_DEPRECATED (decl)
15408 && deprecated_state != DEPRECATED_SUPPRESS)
15409 warn_deprecated_use (decl, NULL_TREE);
15411 else
15413 tree fn = NULL_TREE;
15415 /* The standard does not explicitly indicate whether a name that
15416 names a set of overloaded declarations, some of which are
15417 templates, is a template-name. However, such a name should
15418 be a template-name; otherwise, there is no way to form a
15419 template-id for the overloaded templates. */
15420 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15421 if (TREE_CODE (fns) == OVERLOAD)
15422 for (fn = fns; fn; fn = OVL_NEXT (fn))
15423 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15424 break;
15426 if (!fn)
15428 /* The name does not name a template. */
15429 cp_parser_error (parser, "expected template-name");
15430 return error_mark_node;
15434 /* If DECL is dependent, and refers to a function, then just return
15435 its name; we will look it up again during template instantiation. */
15436 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15438 tree scope = ovl_scope (decl);
15439 if (TYPE_P (scope) && dependent_type_p (scope))
15440 return identifier;
15443 return decl;
15446 /* Parse a template-argument-list.
15448 template-argument-list:
15449 template-argument ... [opt]
15450 template-argument-list , template-argument ... [opt]
15452 Returns a TREE_VEC containing the arguments. */
15454 static tree
15455 cp_parser_template_argument_list (cp_parser* parser)
15457 tree fixed_args[10];
15458 unsigned n_args = 0;
15459 unsigned alloced = 10;
15460 tree *arg_ary = fixed_args;
15461 tree vec;
15462 bool saved_in_template_argument_list_p;
15463 bool saved_ice_p;
15464 bool saved_non_ice_p;
15466 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15467 parser->in_template_argument_list_p = true;
15468 /* Even if the template-id appears in an integral
15469 constant-expression, the contents of the argument list do
15470 not. */
15471 saved_ice_p = parser->integral_constant_expression_p;
15472 parser->integral_constant_expression_p = false;
15473 saved_non_ice_p = parser->non_integral_constant_expression_p;
15474 parser->non_integral_constant_expression_p = false;
15476 /* Parse the arguments. */
15479 tree argument;
15481 if (n_args)
15482 /* Consume the comma. */
15483 cp_lexer_consume_token (parser->lexer);
15485 /* Parse the template-argument. */
15486 argument = cp_parser_template_argument (parser);
15488 /* If the next token is an ellipsis, we're expanding a template
15489 argument pack. */
15490 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15492 if (argument == error_mark_node)
15494 cp_token *token = cp_lexer_peek_token (parser->lexer);
15495 error_at (token->location,
15496 "expected parameter pack before %<...%>");
15498 /* Consume the `...' token. */
15499 cp_lexer_consume_token (parser->lexer);
15501 /* Make the argument into a TYPE_PACK_EXPANSION or
15502 EXPR_PACK_EXPANSION. */
15503 argument = make_pack_expansion (argument);
15506 if (n_args == alloced)
15508 alloced *= 2;
15510 if (arg_ary == fixed_args)
15512 arg_ary = XNEWVEC (tree, alloced);
15513 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15515 else
15516 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15518 arg_ary[n_args++] = argument;
15520 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15522 vec = make_tree_vec (n_args);
15524 while (n_args--)
15525 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15527 if (arg_ary != fixed_args)
15528 free (arg_ary);
15529 parser->non_integral_constant_expression_p = saved_non_ice_p;
15530 parser->integral_constant_expression_p = saved_ice_p;
15531 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15532 if (CHECKING_P)
15533 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15534 return vec;
15537 /* Parse a template-argument.
15539 template-argument:
15540 assignment-expression
15541 type-id
15542 id-expression
15544 The representation is that of an assignment-expression, type-id, or
15545 id-expression -- except that the qualified id-expression is
15546 evaluated, so that the value returned is either a DECL or an
15547 OVERLOAD.
15549 Although the standard says "assignment-expression", it forbids
15550 throw-expressions or assignments in the template argument.
15551 Therefore, we use "conditional-expression" instead. */
15553 static tree
15554 cp_parser_template_argument (cp_parser* parser)
15556 tree argument;
15557 bool template_p;
15558 bool address_p;
15559 bool maybe_type_id = false;
15560 cp_token *token = NULL, *argument_start_token = NULL;
15561 location_t loc = 0;
15562 cp_id_kind idk;
15564 /* There's really no way to know what we're looking at, so we just
15565 try each alternative in order.
15567 [temp.arg]
15569 In a template-argument, an ambiguity between a type-id and an
15570 expression is resolved to a type-id, regardless of the form of
15571 the corresponding template-parameter.
15573 Therefore, we try a type-id first. */
15574 cp_parser_parse_tentatively (parser);
15575 argument = cp_parser_template_type_arg (parser);
15576 /* If there was no error parsing the type-id but the next token is a
15577 '>>', our behavior depends on which dialect of C++ we're
15578 parsing. In C++98, we probably found a typo for '> >'. But there
15579 are type-id which are also valid expressions. For instance:
15581 struct X { int operator >> (int); };
15582 template <int V> struct Foo {};
15583 Foo<X () >> 5> r;
15585 Here 'X()' is a valid type-id of a function type, but the user just
15586 wanted to write the expression "X() >> 5". Thus, we remember that we
15587 found a valid type-id, but we still try to parse the argument as an
15588 expression to see what happens.
15590 In C++0x, the '>>' will be considered two separate '>'
15591 tokens. */
15592 if (!cp_parser_error_occurred (parser)
15593 && cxx_dialect == cxx98
15594 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15596 maybe_type_id = true;
15597 cp_parser_abort_tentative_parse (parser);
15599 else
15601 /* If the next token isn't a `,' or a `>', then this argument wasn't
15602 really finished. This means that the argument is not a valid
15603 type-id. */
15604 if (!cp_parser_next_token_ends_template_argument_p (parser))
15605 cp_parser_error (parser, "expected template-argument");
15606 /* If that worked, we're done. */
15607 if (cp_parser_parse_definitely (parser))
15608 return argument;
15610 /* We're still not sure what the argument will be. */
15611 cp_parser_parse_tentatively (parser);
15612 /* Try a template. */
15613 argument_start_token = cp_lexer_peek_token (parser->lexer);
15614 argument = cp_parser_id_expression (parser,
15615 /*template_keyword_p=*/false,
15616 /*check_dependency_p=*/true,
15617 &template_p,
15618 /*declarator_p=*/false,
15619 /*optional_p=*/false);
15620 /* If the next token isn't a `,' or a `>', then this argument wasn't
15621 really finished. */
15622 if (!cp_parser_next_token_ends_template_argument_p (parser))
15623 cp_parser_error (parser, "expected template-argument");
15624 if (!cp_parser_error_occurred (parser))
15626 /* Figure out what is being referred to. If the id-expression
15627 was for a class template specialization, then we will have a
15628 TYPE_DECL at this point. There is no need to do name lookup
15629 at this point in that case. */
15630 if (TREE_CODE (argument) != TYPE_DECL)
15631 argument = cp_parser_lookup_name (parser, argument,
15632 none_type,
15633 /*is_template=*/template_p,
15634 /*is_namespace=*/false,
15635 /*check_dependency=*/true,
15636 /*ambiguous_decls=*/NULL,
15637 argument_start_token->location);
15638 /* Handle a constrained-type-specifier for a non-type template
15639 parameter. */
15640 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15641 argument = decl;
15642 else if (TREE_CODE (argument) != TEMPLATE_DECL
15643 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15644 cp_parser_error (parser, "expected template-name");
15646 if (cp_parser_parse_definitely (parser))
15648 if (TREE_DEPRECATED (argument))
15649 warn_deprecated_use (argument, NULL_TREE);
15650 return argument;
15652 /* It must be a non-type argument. In C++17 any constant-expression is
15653 allowed. */
15654 if (cxx_dialect > cxx14)
15655 goto general_expr;
15657 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15659 -- an integral constant-expression of integral or enumeration
15660 type; or
15662 -- the name of a non-type template-parameter; or
15664 -- the name of an object or function with external linkage...
15666 -- the address of an object or function with external linkage...
15668 -- a pointer to member... */
15669 /* Look for a non-type template parameter. */
15670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15672 cp_parser_parse_tentatively (parser);
15673 argument = cp_parser_primary_expression (parser,
15674 /*address_p=*/false,
15675 /*cast_p=*/false,
15676 /*template_arg_p=*/true,
15677 &idk);
15678 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15679 || !cp_parser_next_token_ends_template_argument_p (parser))
15680 cp_parser_simulate_error (parser);
15681 if (cp_parser_parse_definitely (parser))
15682 return argument;
15685 /* If the next token is "&", the argument must be the address of an
15686 object or function with external linkage. */
15687 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15688 if (address_p)
15690 loc = cp_lexer_peek_token (parser->lexer)->location;
15691 cp_lexer_consume_token (parser->lexer);
15693 /* See if we might have an id-expression. */
15694 token = cp_lexer_peek_token (parser->lexer);
15695 if (token->type == CPP_NAME
15696 || token->keyword == RID_OPERATOR
15697 || token->type == CPP_SCOPE
15698 || token->type == CPP_TEMPLATE_ID
15699 || token->type == CPP_NESTED_NAME_SPECIFIER)
15701 cp_parser_parse_tentatively (parser);
15702 argument = cp_parser_primary_expression (parser,
15703 address_p,
15704 /*cast_p=*/false,
15705 /*template_arg_p=*/true,
15706 &idk);
15707 if (cp_parser_error_occurred (parser)
15708 || !cp_parser_next_token_ends_template_argument_p (parser))
15709 cp_parser_abort_tentative_parse (parser);
15710 else
15712 tree probe;
15714 if (INDIRECT_REF_P (argument))
15716 /* Strip the dereference temporarily. */
15717 gcc_assert (REFERENCE_REF_P (argument));
15718 argument = TREE_OPERAND (argument, 0);
15721 /* If we're in a template, we represent a qualified-id referring
15722 to a static data member as a SCOPE_REF even if the scope isn't
15723 dependent so that we can check access control later. */
15724 probe = argument;
15725 if (TREE_CODE (probe) == SCOPE_REF)
15726 probe = TREE_OPERAND (probe, 1);
15727 if (VAR_P (probe))
15729 /* A variable without external linkage might still be a
15730 valid constant-expression, so no error is issued here
15731 if the external-linkage check fails. */
15732 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15733 cp_parser_simulate_error (parser);
15735 else if (is_overloaded_fn (argument))
15736 /* All overloaded functions are allowed; if the external
15737 linkage test does not pass, an error will be issued
15738 later. */
15740 else if (address_p
15741 && (TREE_CODE (argument) == OFFSET_REF
15742 || TREE_CODE (argument) == SCOPE_REF))
15743 /* A pointer-to-member. */
15745 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15747 else
15748 cp_parser_simulate_error (parser);
15750 if (cp_parser_parse_definitely (parser))
15752 if (address_p)
15753 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15754 tf_warning_or_error);
15755 else
15756 argument = convert_from_reference (argument);
15757 return argument;
15761 /* If the argument started with "&", there are no other valid
15762 alternatives at this point. */
15763 if (address_p)
15765 cp_parser_error (parser, "invalid non-type template argument");
15766 return error_mark_node;
15769 general_expr:
15770 /* If the argument wasn't successfully parsed as a type-id followed
15771 by '>>', the argument can only be a constant expression now.
15772 Otherwise, we try parsing the constant-expression tentatively,
15773 because the argument could really be a type-id. */
15774 if (maybe_type_id)
15775 cp_parser_parse_tentatively (parser);
15777 if (cxx_dialect <= cxx14)
15778 argument = cp_parser_constant_expression (parser);
15779 else
15781 /* With C++17 generalized non-type template arguments we need to handle
15782 lvalue constant expressions, too. */
15783 argument = cp_parser_assignment_expression (parser);
15784 require_potential_constant_expression (argument);
15787 if (!maybe_type_id)
15788 return argument;
15789 if (!cp_parser_next_token_ends_template_argument_p (parser))
15790 cp_parser_error (parser, "expected template-argument");
15791 if (cp_parser_parse_definitely (parser))
15792 return argument;
15793 /* We did our best to parse the argument as a non type-id, but that
15794 was the only alternative that matched (albeit with a '>' after
15795 it). We can assume it's just a typo from the user, and a
15796 diagnostic will then be issued. */
15797 return cp_parser_template_type_arg (parser);
15800 /* Parse an explicit-instantiation.
15802 explicit-instantiation:
15803 template declaration
15805 Although the standard says `declaration', what it really means is:
15807 explicit-instantiation:
15808 template decl-specifier-seq [opt] declarator [opt] ;
15810 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15811 supposed to be allowed. A defect report has been filed about this
15812 issue.
15814 GNU Extension:
15816 explicit-instantiation:
15817 storage-class-specifier template
15818 decl-specifier-seq [opt] declarator [opt] ;
15819 function-specifier template
15820 decl-specifier-seq [opt] declarator [opt] ; */
15822 static void
15823 cp_parser_explicit_instantiation (cp_parser* parser)
15825 int declares_class_or_enum;
15826 cp_decl_specifier_seq decl_specifiers;
15827 tree extension_specifier = NULL_TREE;
15829 timevar_push (TV_TEMPLATE_INST);
15831 /* Look for an (optional) storage-class-specifier or
15832 function-specifier. */
15833 if (cp_parser_allow_gnu_extensions_p (parser))
15835 extension_specifier
15836 = cp_parser_storage_class_specifier_opt (parser);
15837 if (!extension_specifier)
15838 extension_specifier
15839 = cp_parser_function_specifier_opt (parser,
15840 /*decl_specs=*/NULL);
15843 /* Look for the `template' keyword. */
15844 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15845 /* Let the front end know that we are processing an explicit
15846 instantiation. */
15847 begin_explicit_instantiation ();
15848 /* [temp.explicit] says that we are supposed to ignore access
15849 control while processing explicit instantiation directives. */
15850 push_deferring_access_checks (dk_no_check);
15851 /* Parse a decl-specifier-seq. */
15852 cp_parser_decl_specifier_seq (parser,
15853 CP_PARSER_FLAGS_OPTIONAL,
15854 &decl_specifiers,
15855 &declares_class_or_enum);
15856 /* If there was exactly one decl-specifier, and it declared a class,
15857 and there's no declarator, then we have an explicit type
15858 instantiation. */
15859 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15861 tree type;
15863 type = check_tag_decl (&decl_specifiers,
15864 /*explicit_type_instantiation_p=*/true);
15865 /* Turn access control back on for names used during
15866 template instantiation. */
15867 pop_deferring_access_checks ();
15868 if (type)
15869 do_type_instantiation (type, extension_specifier,
15870 /*complain=*/tf_error);
15872 else
15874 cp_declarator *declarator;
15875 tree decl;
15877 /* Parse the declarator. */
15878 declarator
15879 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15880 /*ctor_dtor_or_conv_p=*/NULL,
15881 /*parenthesized_p=*/NULL,
15882 /*member_p=*/false,
15883 /*friend_p=*/false);
15884 if (declares_class_or_enum & 2)
15885 cp_parser_check_for_definition_in_return_type (declarator,
15886 decl_specifiers.type,
15887 decl_specifiers.locations[ds_type_spec]);
15888 if (declarator != cp_error_declarator)
15890 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15891 permerror (decl_specifiers.locations[ds_inline],
15892 "explicit instantiation shall not use"
15893 " %<inline%> specifier");
15894 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15895 permerror (decl_specifiers.locations[ds_constexpr],
15896 "explicit instantiation shall not use"
15897 " %<constexpr%> specifier");
15899 decl = grokdeclarator (declarator, &decl_specifiers,
15900 NORMAL, 0, &decl_specifiers.attributes);
15901 /* Turn access control back on for names used during
15902 template instantiation. */
15903 pop_deferring_access_checks ();
15904 /* Do the explicit instantiation. */
15905 do_decl_instantiation (decl, extension_specifier);
15907 else
15909 pop_deferring_access_checks ();
15910 /* Skip the body of the explicit instantiation. */
15911 cp_parser_skip_to_end_of_statement (parser);
15914 /* We're done with the instantiation. */
15915 end_explicit_instantiation ();
15917 cp_parser_consume_semicolon_at_end_of_statement (parser);
15919 timevar_pop (TV_TEMPLATE_INST);
15922 /* Parse an explicit-specialization.
15924 explicit-specialization:
15925 template < > declaration
15927 Although the standard says `declaration', what it really means is:
15929 explicit-specialization:
15930 template <> decl-specifier [opt] init-declarator [opt] ;
15931 template <> function-definition
15932 template <> explicit-specialization
15933 template <> template-declaration */
15935 static void
15936 cp_parser_explicit_specialization (cp_parser* parser)
15938 bool need_lang_pop;
15939 cp_token *token = cp_lexer_peek_token (parser->lexer);
15941 /* Look for the `template' keyword. */
15942 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15943 /* Look for the `<'. */
15944 cp_parser_require (parser, CPP_LESS, RT_LESS);
15945 /* Look for the `>'. */
15946 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15947 /* We have processed another parameter list. */
15948 ++parser->num_template_parameter_lists;
15949 /* [temp]
15951 A template ... explicit specialization ... shall not have C
15952 linkage. */
15953 if (current_lang_name == lang_name_c)
15955 error_at (token->location, "template specialization with C linkage");
15956 /* Give it C++ linkage to avoid confusing other parts of the
15957 front end. */
15958 push_lang_context (lang_name_cplusplus);
15959 need_lang_pop = true;
15961 else
15962 need_lang_pop = false;
15963 /* Let the front end know that we are beginning a specialization. */
15964 if (!begin_specialization ())
15966 end_specialization ();
15967 return;
15970 /* If the next keyword is `template', we need to figure out whether
15971 or not we're looking a template-declaration. */
15972 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15974 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15975 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15976 cp_parser_template_declaration_after_export (parser,
15977 /*member_p=*/false);
15978 else
15979 cp_parser_explicit_specialization (parser);
15981 else
15982 /* Parse the dependent declaration. */
15983 cp_parser_single_declaration (parser,
15984 /*checks=*/NULL,
15985 /*member_p=*/false,
15986 /*explicit_specialization_p=*/true,
15987 /*friend_p=*/NULL);
15988 /* We're done with the specialization. */
15989 end_specialization ();
15990 /* For the erroneous case of a template with C linkage, we pushed an
15991 implicit C++ linkage scope; exit that scope now. */
15992 if (need_lang_pop)
15993 pop_lang_context ();
15994 /* We're done with this parameter list. */
15995 --parser->num_template_parameter_lists;
15998 /* Parse a type-specifier.
16000 type-specifier:
16001 simple-type-specifier
16002 class-specifier
16003 enum-specifier
16004 elaborated-type-specifier
16005 cv-qualifier
16007 GNU Extension:
16009 type-specifier:
16010 __complex__
16012 Returns a representation of the type-specifier. For a
16013 class-specifier, enum-specifier, or elaborated-type-specifier, a
16014 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16016 The parser flags FLAGS is used to control type-specifier parsing.
16018 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16019 in a decl-specifier-seq.
16021 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16022 class-specifier, enum-specifier, or elaborated-type-specifier, then
16023 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16024 if a type is declared; 2 if it is defined. Otherwise, it is set to
16025 zero.
16027 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16028 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16029 is set to FALSE. */
16031 static tree
16032 cp_parser_type_specifier (cp_parser* parser,
16033 cp_parser_flags flags,
16034 cp_decl_specifier_seq *decl_specs,
16035 bool is_declaration,
16036 int* declares_class_or_enum,
16037 bool* is_cv_qualifier)
16039 tree type_spec = NULL_TREE;
16040 cp_token *token;
16041 enum rid keyword;
16042 cp_decl_spec ds = ds_last;
16044 /* Assume this type-specifier does not declare a new type. */
16045 if (declares_class_or_enum)
16046 *declares_class_or_enum = 0;
16047 /* And that it does not specify a cv-qualifier. */
16048 if (is_cv_qualifier)
16049 *is_cv_qualifier = false;
16050 /* Peek at the next token. */
16051 token = cp_lexer_peek_token (parser->lexer);
16053 /* If we're looking at a keyword, we can use that to guide the
16054 production we choose. */
16055 keyword = token->keyword;
16056 switch (keyword)
16058 case RID_ENUM:
16059 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16060 goto elaborated_type_specifier;
16062 /* Look for the enum-specifier. */
16063 type_spec = cp_parser_enum_specifier (parser);
16064 /* If that worked, we're done. */
16065 if (type_spec)
16067 if (declares_class_or_enum)
16068 *declares_class_or_enum = 2;
16069 if (decl_specs)
16070 cp_parser_set_decl_spec_type (decl_specs,
16071 type_spec,
16072 token,
16073 /*type_definition_p=*/true);
16074 return type_spec;
16076 else
16077 goto elaborated_type_specifier;
16079 /* Any of these indicate either a class-specifier, or an
16080 elaborated-type-specifier. */
16081 case RID_CLASS:
16082 case RID_STRUCT:
16083 case RID_UNION:
16084 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16085 goto elaborated_type_specifier;
16087 /* Parse tentatively so that we can back up if we don't find a
16088 class-specifier. */
16089 cp_parser_parse_tentatively (parser);
16090 /* Look for the class-specifier. */
16091 type_spec = cp_parser_class_specifier (parser);
16092 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16093 /* If that worked, we're done. */
16094 if (cp_parser_parse_definitely (parser))
16096 if (declares_class_or_enum)
16097 *declares_class_or_enum = 2;
16098 if (decl_specs)
16099 cp_parser_set_decl_spec_type (decl_specs,
16100 type_spec,
16101 token,
16102 /*type_definition_p=*/true);
16103 return type_spec;
16106 /* Fall through. */
16107 elaborated_type_specifier:
16108 /* We're declaring (not defining) a class or enum. */
16109 if (declares_class_or_enum)
16110 *declares_class_or_enum = 1;
16112 /* Fall through. */
16113 case RID_TYPENAME:
16114 /* Look for an elaborated-type-specifier. */
16115 type_spec
16116 = (cp_parser_elaborated_type_specifier
16117 (parser,
16118 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16119 is_declaration));
16120 if (decl_specs)
16121 cp_parser_set_decl_spec_type (decl_specs,
16122 type_spec,
16123 token,
16124 /*type_definition_p=*/false);
16125 return type_spec;
16127 case RID_CONST:
16128 ds = ds_const;
16129 if (is_cv_qualifier)
16130 *is_cv_qualifier = true;
16131 break;
16133 case RID_VOLATILE:
16134 ds = ds_volatile;
16135 if (is_cv_qualifier)
16136 *is_cv_qualifier = true;
16137 break;
16139 case RID_RESTRICT:
16140 ds = ds_restrict;
16141 if (is_cv_qualifier)
16142 *is_cv_qualifier = true;
16143 break;
16145 case RID_COMPLEX:
16146 /* The `__complex__' keyword is a GNU extension. */
16147 ds = ds_complex;
16148 break;
16150 default:
16151 break;
16154 /* Handle simple keywords. */
16155 if (ds != ds_last)
16157 if (decl_specs)
16159 set_and_check_decl_spec_loc (decl_specs, ds, token);
16160 decl_specs->any_specifiers_p = true;
16162 return cp_lexer_consume_token (parser->lexer)->u.value;
16165 /* If we do not already have a type-specifier, assume we are looking
16166 at a simple-type-specifier. */
16167 type_spec = cp_parser_simple_type_specifier (parser,
16168 decl_specs,
16169 flags);
16171 /* If we didn't find a type-specifier, and a type-specifier was not
16172 optional in this context, issue an error message. */
16173 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16175 cp_parser_error (parser, "expected type specifier");
16176 return error_mark_node;
16179 return type_spec;
16182 /* Parse a simple-type-specifier.
16184 simple-type-specifier:
16185 :: [opt] nested-name-specifier [opt] type-name
16186 :: [opt] nested-name-specifier template template-id
16187 char
16188 wchar_t
16189 bool
16190 short
16192 long
16193 signed
16194 unsigned
16195 float
16196 double
16197 void
16199 C++11 Extension:
16201 simple-type-specifier:
16202 auto
16203 decltype ( expression )
16204 char16_t
16205 char32_t
16206 __underlying_type ( type-id )
16208 C++17 extension:
16210 nested-name-specifier(opt) template-name
16212 GNU Extension:
16214 simple-type-specifier:
16215 __int128
16216 __typeof__ unary-expression
16217 __typeof__ ( type-id )
16218 __typeof__ ( type-id ) { initializer-list , [opt] }
16220 Concepts Extension:
16222 simple-type-specifier:
16223 constrained-type-specifier
16225 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16226 appropriately updated. */
16228 static tree
16229 cp_parser_simple_type_specifier (cp_parser* parser,
16230 cp_decl_specifier_seq *decl_specs,
16231 cp_parser_flags flags)
16233 tree type = NULL_TREE;
16234 cp_token *token;
16235 int idx;
16237 /* Peek at the next token. */
16238 token = cp_lexer_peek_token (parser->lexer);
16240 /* If we're looking at a keyword, things are easy. */
16241 switch (token->keyword)
16243 case RID_CHAR:
16244 if (decl_specs)
16245 decl_specs->explicit_char_p = true;
16246 type = char_type_node;
16247 break;
16248 case RID_CHAR16:
16249 type = char16_type_node;
16250 break;
16251 case RID_CHAR32:
16252 type = char32_type_node;
16253 break;
16254 case RID_WCHAR:
16255 type = wchar_type_node;
16256 break;
16257 case RID_BOOL:
16258 type = boolean_type_node;
16259 break;
16260 case RID_SHORT:
16261 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16262 type = short_integer_type_node;
16263 break;
16264 case RID_INT:
16265 if (decl_specs)
16266 decl_specs->explicit_int_p = true;
16267 type = integer_type_node;
16268 break;
16269 case RID_INT_N_0:
16270 case RID_INT_N_1:
16271 case RID_INT_N_2:
16272 case RID_INT_N_3:
16273 idx = token->keyword - RID_INT_N_0;
16274 if (! int_n_enabled_p [idx])
16275 break;
16276 if (decl_specs)
16278 decl_specs->explicit_intN_p = true;
16279 decl_specs->int_n_idx = idx;
16281 type = int_n_trees [idx].signed_type;
16282 break;
16283 case RID_LONG:
16284 if (decl_specs)
16285 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16286 type = long_integer_type_node;
16287 break;
16288 case RID_SIGNED:
16289 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16290 type = integer_type_node;
16291 break;
16292 case RID_UNSIGNED:
16293 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16294 type = unsigned_type_node;
16295 break;
16296 case RID_FLOAT:
16297 type = float_type_node;
16298 break;
16299 case RID_DOUBLE:
16300 type = double_type_node;
16301 break;
16302 case RID_VOID:
16303 type = void_type_node;
16304 break;
16306 case RID_AUTO:
16307 maybe_warn_cpp0x (CPP0X_AUTO);
16308 if (parser->auto_is_implicit_function_template_parm_p)
16310 /* The 'auto' might be the placeholder return type for a function decl
16311 with trailing return type. */
16312 bool have_trailing_return_fn_decl = false;
16314 cp_parser_parse_tentatively (parser);
16315 cp_lexer_consume_token (parser->lexer);
16316 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16317 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16318 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16319 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16321 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16323 cp_lexer_consume_token (parser->lexer);
16324 cp_parser_skip_to_closing_parenthesis (parser,
16325 /*recovering*/false,
16326 /*or_comma*/false,
16327 /*consume_paren*/true);
16328 continue;
16331 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16333 have_trailing_return_fn_decl = true;
16334 break;
16337 cp_lexer_consume_token (parser->lexer);
16339 cp_parser_abort_tentative_parse (parser);
16341 if (have_trailing_return_fn_decl)
16343 type = make_auto ();
16344 break;
16347 if (cxx_dialect >= cxx14)
16349 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16350 type = TREE_TYPE (type);
16352 else
16353 type = error_mark_node;
16355 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16357 if (cxx_dialect < cxx14)
16358 error_at (token->location,
16359 "use of %<auto%> in lambda parameter declaration "
16360 "only available with "
16361 "-std=c++14 or -std=gnu++14");
16363 else if (cxx_dialect < cxx14)
16364 error_at (token->location,
16365 "use of %<auto%> in parameter declaration "
16366 "only available with "
16367 "-std=c++14 or -std=gnu++14");
16368 else if (!flag_concepts)
16369 pedwarn (token->location, OPT_Wpedantic,
16370 "ISO C++ forbids use of %<auto%> in parameter "
16371 "declaration");
16373 else
16374 type = make_auto ();
16375 break;
16377 case RID_DECLTYPE:
16378 /* Since DR 743, decltype can either be a simple-type-specifier by
16379 itself or begin a nested-name-specifier. Parsing it will replace
16380 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16381 handling below decide what to do. */
16382 cp_parser_decltype (parser);
16383 cp_lexer_set_token_position (parser->lexer, token);
16384 break;
16386 case RID_TYPEOF:
16387 /* Consume the `typeof' token. */
16388 cp_lexer_consume_token (parser->lexer);
16389 /* Parse the operand to `typeof'. */
16390 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16391 /* If it is not already a TYPE, take its type. */
16392 if (!TYPE_P (type))
16393 type = finish_typeof (type);
16395 if (decl_specs)
16396 cp_parser_set_decl_spec_type (decl_specs, type,
16397 token,
16398 /*type_definition_p=*/false);
16400 return type;
16402 case RID_UNDERLYING_TYPE:
16403 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16404 if (decl_specs)
16405 cp_parser_set_decl_spec_type (decl_specs, type,
16406 token,
16407 /*type_definition_p=*/false);
16409 return type;
16411 case RID_BASES:
16412 case RID_DIRECT_BASES:
16413 type = cp_parser_trait_expr (parser, token->keyword);
16414 if (decl_specs)
16415 cp_parser_set_decl_spec_type (decl_specs, type,
16416 token,
16417 /*type_definition_p=*/false);
16418 return type;
16419 default:
16420 break;
16423 /* If token is an already-parsed decltype not followed by ::,
16424 it's a simple-type-specifier. */
16425 if (token->type == CPP_DECLTYPE
16426 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16428 type = saved_checks_value (token->u.tree_check_value);
16429 if (decl_specs)
16431 cp_parser_set_decl_spec_type (decl_specs, type,
16432 token,
16433 /*type_definition_p=*/false);
16434 /* Remember that we are handling a decltype in order to
16435 implement the resolution of DR 1510 when the argument
16436 isn't instantiation dependent. */
16437 decl_specs->decltype_p = true;
16439 cp_lexer_consume_token (parser->lexer);
16440 return type;
16443 /* If the type-specifier was for a built-in type, we're done. */
16444 if (type)
16446 /* Record the type. */
16447 if (decl_specs
16448 && (token->keyword != RID_SIGNED
16449 && token->keyword != RID_UNSIGNED
16450 && token->keyword != RID_SHORT
16451 && token->keyword != RID_LONG))
16452 cp_parser_set_decl_spec_type (decl_specs,
16453 type,
16454 token,
16455 /*type_definition_p=*/false);
16456 if (decl_specs)
16457 decl_specs->any_specifiers_p = true;
16459 /* Consume the token. */
16460 cp_lexer_consume_token (parser->lexer);
16462 if (type == error_mark_node)
16463 return error_mark_node;
16465 /* There is no valid C++ program where a non-template type is
16466 followed by a "<". That usually indicates that the user thought
16467 that the type was a template. */
16468 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16469 token->location);
16471 return TYPE_NAME (type);
16474 /* The type-specifier must be a user-defined type. */
16475 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16477 bool qualified_p;
16478 bool global_p;
16480 /* Don't gobble tokens or issue error messages if this is an
16481 optional type-specifier. */
16482 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16483 cp_parser_parse_tentatively (parser);
16485 token = cp_lexer_peek_token (parser->lexer);
16487 /* Look for the optional `::' operator. */
16488 global_p
16489 = (cp_parser_global_scope_opt (parser,
16490 /*current_scope_valid_p=*/false)
16491 != NULL_TREE);
16492 /* Look for the nested-name specifier. */
16493 qualified_p
16494 = (cp_parser_nested_name_specifier_opt (parser,
16495 /*typename_keyword_p=*/false,
16496 /*check_dependency_p=*/true,
16497 /*type_p=*/false,
16498 /*is_declaration=*/false)
16499 != NULL_TREE);
16500 /* If we have seen a nested-name-specifier, and the next token
16501 is `template', then we are using the template-id production. */
16502 if (parser->scope
16503 && cp_parser_optional_template_keyword (parser))
16505 /* Look for the template-id. */
16506 type = cp_parser_template_id (parser,
16507 /*template_keyword_p=*/true,
16508 /*check_dependency_p=*/true,
16509 none_type,
16510 /*is_declaration=*/false);
16511 /* If the template-id did not name a type, we are out of
16512 luck. */
16513 if (TREE_CODE (type) != TYPE_DECL)
16515 cp_parser_error (parser, "expected template-id for type");
16516 type = NULL_TREE;
16519 /* Otherwise, look for a type-name. */
16520 else
16521 type = cp_parser_type_name (parser);
16522 /* Keep track of all name-lookups performed in class scopes. */
16523 if (type
16524 && !global_p
16525 && !qualified_p
16526 && TREE_CODE (type) == TYPE_DECL
16527 && identifier_p (DECL_NAME (type)))
16528 maybe_note_name_used_in_class (DECL_NAME (type), type);
16529 /* If it didn't work out, we don't have a TYPE. */
16530 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16531 && !cp_parser_parse_definitely (parser))
16532 type = NULL_TREE;
16533 if (!type && cxx_dialect >= cxx1z)
16535 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16536 cp_parser_parse_tentatively (parser);
16538 cp_parser_global_scope_opt (parser,
16539 /*current_scope_valid_p=*/false);
16540 cp_parser_nested_name_specifier_opt (parser,
16541 /*typename_keyword_p=*/false,
16542 /*check_dependency_p=*/true,
16543 /*type_p=*/false,
16544 /*is_declaration=*/false);
16545 tree name = cp_parser_identifier (parser);
16546 if (name && TREE_CODE (name) == IDENTIFIER_NODE
16547 && parser->scope != error_mark_node)
16549 tree tmpl = cp_parser_lookup_name (parser, name,
16550 none_type,
16551 /*is_template=*/false,
16552 /*is_namespace=*/false,
16553 /*check_dependency=*/true,
16554 /*ambiguous_decls=*/NULL,
16555 token->location);
16556 if (tmpl && tmpl != error_mark_node
16557 && DECL_CLASS_TEMPLATE_P (tmpl))
16558 type = make_template_placeholder (tmpl);
16559 else
16561 type = error_mark_node;
16562 if (!cp_parser_simulate_error (parser))
16563 cp_parser_name_lookup_error (parser, name, tmpl,
16564 NLE_TYPE, token->location);
16567 else
16568 type = error_mark_node;
16570 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16571 && !cp_parser_parse_definitely (parser))
16572 type = NULL_TREE;
16574 if (type && decl_specs)
16575 cp_parser_set_decl_spec_type (decl_specs, type,
16576 token,
16577 /*type_definition_p=*/false);
16580 /* If we didn't get a type-name, issue an error message. */
16581 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16583 cp_parser_error (parser, "expected type-name");
16584 return error_mark_node;
16587 if (type && type != error_mark_node)
16589 /* See if TYPE is an Objective-C type, and if so, parse and
16590 accept any protocol references following it. Do this before
16591 the cp_parser_check_for_invalid_template_id() call, because
16592 Objective-C types can be followed by '<...>' which would
16593 enclose protocol names rather than template arguments, and so
16594 everything is fine. */
16595 if (c_dialect_objc () && !parser->scope
16596 && (objc_is_id (type) || objc_is_class_name (type)))
16598 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16599 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16601 /* Clobber the "unqualified" type previously entered into
16602 DECL_SPECS with the new, improved protocol-qualified version. */
16603 if (decl_specs)
16604 decl_specs->type = qual_type;
16606 return qual_type;
16609 /* There is no valid C++ program where a non-template type is
16610 followed by a "<". That usually indicates that the user
16611 thought that the type was a template. */
16612 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16613 none_type,
16614 token->location);
16617 return type;
16620 /* Parse a type-name.
16622 type-name:
16623 class-name
16624 enum-name
16625 typedef-name
16626 simple-template-id [in c++0x]
16628 enum-name:
16629 identifier
16631 typedef-name:
16632 identifier
16634 Concepts:
16636 type-name:
16637 concept-name
16638 partial-concept-id
16640 concept-name:
16641 identifier
16643 Returns a TYPE_DECL for the type. */
16645 static tree
16646 cp_parser_type_name (cp_parser* parser)
16648 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16651 /* See above. */
16652 static tree
16653 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16655 tree type_decl;
16657 /* We can't know yet whether it is a class-name or not. */
16658 cp_parser_parse_tentatively (parser);
16659 /* Try a class-name. */
16660 type_decl = cp_parser_class_name (parser,
16661 typename_keyword_p,
16662 /*template_keyword_p=*/false,
16663 none_type,
16664 /*check_dependency_p=*/true,
16665 /*class_head_p=*/false,
16666 /*is_declaration=*/false);
16667 /* If it's not a class-name, keep looking. */
16668 if (!cp_parser_parse_definitely (parser))
16670 if (cxx_dialect < cxx11)
16671 /* It must be a typedef-name or an enum-name. */
16672 return cp_parser_nonclass_name (parser);
16674 cp_parser_parse_tentatively (parser);
16675 /* It is either a simple-template-id representing an
16676 instantiation of an alias template... */
16677 type_decl = cp_parser_template_id (parser,
16678 /*template_keyword_p=*/false,
16679 /*check_dependency_p=*/true,
16680 none_type,
16681 /*is_declaration=*/false);
16682 /* Note that this must be an instantiation of an alias template
16683 because [temp.names]/6 says:
16685 A template-id that names an alias template specialization
16686 is a type-name.
16688 Whereas [temp.names]/7 says:
16690 A simple-template-id that names a class template
16691 specialization is a class-name.
16693 With concepts, this could also be a partial-concept-id that
16694 declares a non-type template parameter. */
16695 if (type_decl != NULL_TREE
16696 && TREE_CODE (type_decl) == TYPE_DECL
16697 && TYPE_DECL_ALIAS_P (type_decl))
16698 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16699 else if (is_constrained_parameter (type_decl))
16700 /* Don't do anything. */ ;
16701 else
16702 cp_parser_simulate_error (parser);
16704 if (!cp_parser_parse_definitely (parser))
16705 /* ... Or a typedef-name or an enum-name. */
16706 return cp_parser_nonclass_name (parser);
16709 return type_decl;
16712 /* Check if DECL and ARGS can form a constrained-type-specifier.
16713 If ARGS is non-null, we try to form a concept check of the
16714 form DECL<?, ARGS> where ? is a wildcard that matches any
16715 kind of template argument. If ARGS is NULL, then we try to
16716 form a concept check of the form DECL<?>. */
16718 static tree
16719 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16720 tree decl, tree args)
16722 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16724 /* If we a constrained-type-specifier cannot be deduced. */
16725 if (parser->prevent_constrained_type_specifiers)
16726 return NULL_TREE;
16728 /* A constrained type specifier can only be found in an
16729 overload set or as a reference to a template declaration.
16731 FIXME: This might be masking a bug. It's possible that
16732 that the deduction below is causing template specializations
16733 to be formed with the wildcard as an argument. */
16734 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16735 return NULL_TREE;
16737 /* Try to build a call expression that evaluates the
16738 concept. This can fail if the overload set refers
16739 only to non-templates. */
16740 tree placeholder = build_nt (WILDCARD_DECL);
16741 tree check = build_concept_check (decl, placeholder, args);
16742 if (check == error_mark_node)
16743 return NULL_TREE;
16745 /* Deduce the checked constraint and the prototype parameter.
16747 FIXME: In certain cases, failure to deduce should be a
16748 diagnosable error. */
16749 tree conc;
16750 tree proto;
16751 if (!deduce_constrained_parameter (check, conc, proto))
16752 return NULL_TREE;
16754 /* In template parameter scope, this results in a constrained
16755 parameter. Return a descriptor of that parm. */
16756 if (processing_template_parmlist)
16757 return build_constrained_parameter (conc, proto, args);
16759 /* In a parameter-declaration-clause, constrained-type
16760 specifiers result in invented template parameters. */
16761 if (parser->auto_is_implicit_function_template_parm_p)
16763 tree x = build_constrained_parameter (conc, proto, args);
16764 return synthesize_implicit_template_parm (parser, x);
16766 else
16768 /* Otherwise, we're in a context where the constrained
16769 type name is deduced and the constraint applies
16770 after deduction. */
16771 return make_constrained_auto (conc, args);
16774 return NULL_TREE;
16777 /* If DECL refers to a concept, return a TYPE_DECL representing
16778 the result of using the constrained type specifier in the
16779 current context. DECL refers to a concept if
16781 - it is an overload set containing a function concept taking a single
16782 type argument, or
16784 - it is a variable concept taking a single type argument. */
16786 static tree
16787 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16789 if (flag_concepts
16790 && (TREE_CODE (decl) == OVERLOAD
16791 || BASELINK_P (decl)
16792 || variable_concept_p (decl)))
16793 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16794 else
16795 return NULL_TREE;
16798 /* Check if DECL and ARGS form a partial-concept-id. If so,
16799 assign ID to the resulting constrained placeholder.
16801 Returns true if the partial-concept-id designates a placeholder
16802 and false otherwise. Note that *id is set to NULL_TREE in
16803 this case. */
16805 static tree
16806 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16808 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16811 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16812 or a concept-name.
16814 enum-name:
16815 identifier
16817 typedef-name:
16818 identifier
16820 concept-name:
16821 identifier
16823 Returns a TYPE_DECL for the type. */
16825 static tree
16826 cp_parser_nonclass_name (cp_parser* parser)
16828 tree type_decl;
16829 tree identifier;
16831 cp_token *token = cp_lexer_peek_token (parser->lexer);
16832 identifier = cp_parser_identifier (parser);
16833 if (identifier == error_mark_node)
16834 return error_mark_node;
16836 /* Look up the type-name. */
16837 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16839 type_decl = strip_using_decl (type_decl);
16841 /* If we found an overload set, then it may refer to a concept-name. */
16842 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16843 type_decl = decl;
16845 if (TREE_CODE (type_decl) != TYPE_DECL
16846 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16848 /* See if this is an Objective-C type. */
16849 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16850 tree type = objc_get_protocol_qualified_type (identifier, protos);
16851 if (type)
16852 type_decl = TYPE_NAME (type);
16855 /* Issue an error if we did not find a type-name. */
16856 if (TREE_CODE (type_decl) != TYPE_DECL
16857 /* In Objective-C, we have the complication that class names are
16858 normally type names and start declarations (eg, the
16859 "NSObject" in "NSObject *object;"), but can be used in an
16860 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16861 is an expression. So, a classname followed by a dot is not a
16862 valid type-name. */
16863 || (objc_is_class_name (TREE_TYPE (type_decl))
16864 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16866 if (!cp_parser_simulate_error (parser))
16867 cp_parser_name_lookup_error (parser, identifier, type_decl,
16868 NLE_TYPE, token->location);
16869 return error_mark_node;
16871 /* Remember that the name was used in the definition of the
16872 current class so that we can check later to see if the
16873 meaning would have been different after the class was
16874 entirely defined. */
16875 else if (type_decl != error_mark_node
16876 && !parser->scope)
16877 maybe_note_name_used_in_class (identifier, type_decl);
16879 return type_decl;
16882 /* Parse an elaborated-type-specifier. Note that the grammar given
16883 here incorporates the resolution to DR68.
16885 elaborated-type-specifier:
16886 class-key :: [opt] nested-name-specifier [opt] identifier
16887 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16888 enum-key :: [opt] nested-name-specifier [opt] identifier
16889 typename :: [opt] nested-name-specifier identifier
16890 typename :: [opt] nested-name-specifier template [opt]
16891 template-id
16893 GNU extension:
16895 elaborated-type-specifier:
16896 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16897 class-key attributes :: [opt] nested-name-specifier [opt]
16898 template [opt] template-id
16899 enum attributes :: [opt] nested-name-specifier [opt] identifier
16901 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16902 declared `friend'. If IS_DECLARATION is TRUE, then this
16903 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16904 something is being declared.
16906 Returns the TYPE specified. */
16908 static tree
16909 cp_parser_elaborated_type_specifier (cp_parser* parser,
16910 bool is_friend,
16911 bool is_declaration)
16913 enum tag_types tag_type;
16914 tree identifier;
16915 tree type = NULL_TREE;
16916 tree attributes = NULL_TREE;
16917 tree globalscope;
16918 cp_token *token = NULL;
16920 /* See if we're looking at the `enum' keyword. */
16921 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16923 /* Consume the `enum' token. */
16924 cp_lexer_consume_token (parser->lexer);
16925 /* Remember that it's an enumeration type. */
16926 tag_type = enum_type;
16927 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16928 enums) is used here. */
16929 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16930 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16932 pedwarn (input_location, 0, "elaborated-type-specifier "
16933 "for a scoped enum must not use the %<%D%> keyword",
16934 cp_lexer_peek_token (parser->lexer)->u.value);
16935 /* Consume the `struct' or `class' and parse it anyway. */
16936 cp_lexer_consume_token (parser->lexer);
16938 /* Parse the attributes. */
16939 attributes = cp_parser_attributes_opt (parser);
16941 /* Or, it might be `typename'. */
16942 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16943 RID_TYPENAME))
16945 /* Consume the `typename' token. */
16946 cp_lexer_consume_token (parser->lexer);
16947 /* Remember that it's a `typename' type. */
16948 tag_type = typename_type;
16950 /* Otherwise it must be a class-key. */
16951 else
16953 tag_type = cp_parser_class_key (parser);
16954 if (tag_type == none_type)
16955 return error_mark_node;
16956 /* Parse the attributes. */
16957 attributes = cp_parser_attributes_opt (parser);
16960 /* Look for the `::' operator. */
16961 globalscope = cp_parser_global_scope_opt (parser,
16962 /*current_scope_valid_p=*/false);
16963 /* Look for the nested-name-specifier. */
16964 if (tag_type == typename_type && !globalscope)
16966 if (!cp_parser_nested_name_specifier (parser,
16967 /*typename_keyword_p=*/true,
16968 /*check_dependency_p=*/true,
16969 /*type_p=*/true,
16970 is_declaration))
16971 return error_mark_node;
16973 else
16974 /* Even though `typename' is not present, the proposed resolution
16975 to Core Issue 180 says that in `class A<T>::B', `B' should be
16976 considered a type-name, even if `A<T>' is dependent. */
16977 cp_parser_nested_name_specifier_opt (parser,
16978 /*typename_keyword_p=*/true,
16979 /*check_dependency_p=*/true,
16980 /*type_p=*/true,
16981 is_declaration);
16982 /* For everything but enumeration types, consider a template-id.
16983 For an enumeration type, consider only a plain identifier. */
16984 if (tag_type != enum_type)
16986 bool template_p = false;
16987 tree decl;
16989 /* Allow the `template' keyword. */
16990 template_p = cp_parser_optional_template_keyword (parser);
16991 /* If we didn't see `template', we don't know if there's a
16992 template-id or not. */
16993 if (!template_p)
16994 cp_parser_parse_tentatively (parser);
16995 /* Parse the template-id. */
16996 token = cp_lexer_peek_token (parser->lexer);
16997 decl = cp_parser_template_id (parser, template_p,
16998 /*check_dependency_p=*/true,
16999 tag_type,
17000 is_declaration);
17001 /* If we didn't find a template-id, look for an ordinary
17002 identifier. */
17003 if (!template_p && !cp_parser_parse_definitely (parser))
17005 /* We can get here when cp_parser_template_id, called by
17006 cp_parser_class_name with tag_type == none_type, succeeds
17007 and caches a BASELINK. Then, when called again here,
17008 instead of failing and returning an error_mark_node
17009 returns it (see template/typename17.C in C++11).
17010 ??? Could we diagnose this earlier? */
17011 else if (tag_type == typename_type && BASELINK_P (decl))
17013 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17014 type = error_mark_node;
17016 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17017 in effect, then we must assume that, upon instantiation, the
17018 template will correspond to a class. */
17019 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17020 && tag_type == typename_type)
17021 type = make_typename_type (parser->scope, decl,
17022 typename_type,
17023 /*complain=*/tf_error);
17024 /* If the `typename' keyword is in effect and DECL is not a type
17025 decl, then type is non existent. */
17026 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17028 else if (TREE_CODE (decl) == TYPE_DECL)
17029 type = check_elaborated_type_specifier (tag_type, decl,
17030 /*allow_template_p=*/true);
17031 else if (decl == error_mark_node)
17032 type = error_mark_node;
17035 if (!type)
17037 token = cp_lexer_peek_token (parser->lexer);
17038 identifier = cp_parser_identifier (parser);
17040 if (identifier == error_mark_node)
17042 parser->scope = NULL_TREE;
17043 return error_mark_node;
17046 /* For a `typename', we needn't call xref_tag. */
17047 if (tag_type == typename_type
17048 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17049 return cp_parser_make_typename_type (parser, identifier,
17050 token->location);
17052 /* Template parameter lists apply only if we are not within a
17053 function parameter list. */
17054 bool template_parm_lists_apply
17055 = parser->num_template_parameter_lists;
17056 if (template_parm_lists_apply)
17057 for (cp_binding_level *s = current_binding_level;
17058 s && s->kind != sk_template_parms;
17059 s = s->level_chain)
17060 if (s->kind == sk_function_parms)
17061 template_parm_lists_apply = false;
17063 /* Look up a qualified name in the usual way. */
17064 if (parser->scope)
17066 tree decl;
17067 tree ambiguous_decls;
17069 decl = cp_parser_lookup_name (parser, identifier,
17070 tag_type,
17071 /*is_template=*/false,
17072 /*is_namespace=*/false,
17073 /*check_dependency=*/true,
17074 &ambiguous_decls,
17075 token->location);
17077 /* If the lookup was ambiguous, an error will already have been
17078 issued. */
17079 if (ambiguous_decls)
17080 return error_mark_node;
17082 /* If we are parsing friend declaration, DECL may be a
17083 TEMPLATE_DECL tree node here. However, we need to check
17084 whether this TEMPLATE_DECL results in valid code. Consider
17085 the following example:
17087 namespace N {
17088 template <class T> class C {};
17090 class X {
17091 template <class T> friend class N::C; // #1, valid code
17093 template <class T> class Y {
17094 friend class N::C; // #2, invalid code
17097 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17098 name lookup of `N::C'. We see that friend declaration must
17099 be template for the code to be valid. Note that
17100 processing_template_decl does not work here since it is
17101 always 1 for the above two cases. */
17103 decl = (cp_parser_maybe_treat_template_as_class
17104 (decl, /*tag_name_p=*/is_friend
17105 && template_parm_lists_apply));
17107 if (TREE_CODE (decl) != TYPE_DECL)
17109 cp_parser_diagnose_invalid_type_name (parser,
17110 identifier,
17111 token->location);
17112 return error_mark_node;
17115 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17117 bool allow_template = (template_parm_lists_apply
17118 || DECL_SELF_REFERENCE_P (decl));
17119 type = check_elaborated_type_specifier (tag_type, decl,
17120 allow_template);
17122 if (type == error_mark_node)
17123 return error_mark_node;
17126 /* Forward declarations of nested types, such as
17128 class C1::C2;
17129 class C1::C2::C3;
17131 are invalid unless all components preceding the final '::'
17132 are complete. If all enclosing types are complete, these
17133 declarations become merely pointless.
17135 Invalid forward declarations of nested types are errors
17136 caught elsewhere in parsing. Those that are pointless arrive
17137 here. */
17139 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17140 && !is_friend && !processing_explicit_instantiation)
17141 warning (0, "declaration %qD does not declare anything", decl);
17143 type = TREE_TYPE (decl);
17145 else
17147 /* An elaborated-type-specifier sometimes introduces a new type and
17148 sometimes names an existing type. Normally, the rule is that it
17149 introduces a new type only if there is not an existing type of
17150 the same name already in scope. For example, given:
17152 struct S {};
17153 void f() { struct S s; }
17155 the `struct S' in the body of `f' is the same `struct S' as in
17156 the global scope; the existing definition is used. However, if
17157 there were no global declaration, this would introduce a new
17158 local class named `S'.
17160 An exception to this rule applies to the following code:
17162 namespace N { struct S; }
17164 Here, the elaborated-type-specifier names a new type
17165 unconditionally; even if there is already an `S' in the
17166 containing scope this declaration names a new type.
17167 This exception only applies if the elaborated-type-specifier
17168 forms the complete declaration:
17170 [class.name]
17172 A declaration consisting solely of `class-key identifier ;' is
17173 either a redeclaration of the name in the current scope or a
17174 forward declaration of the identifier as a class name. It
17175 introduces the name into the current scope.
17177 We are in this situation precisely when the next token is a `;'.
17179 An exception to the exception is that a `friend' declaration does
17180 *not* name a new type; i.e., given:
17182 struct S { friend struct T; };
17184 `T' is not a new type in the scope of `S'.
17186 Also, `new struct S' or `sizeof (struct S)' never results in the
17187 definition of a new type; a new type can only be declared in a
17188 declaration context. */
17190 tag_scope ts;
17191 bool template_p;
17193 if (is_friend)
17194 /* Friends have special name lookup rules. */
17195 ts = ts_within_enclosing_non_class;
17196 else if (is_declaration
17197 && cp_lexer_next_token_is (parser->lexer,
17198 CPP_SEMICOLON))
17199 /* This is a `class-key identifier ;' */
17200 ts = ts_current;
17201 else
17202 ts = ts_global;
17204 template_p =
17205 (template_parm_lists_apply
17206 && (cp_parser_next_token_starts_class_definition_p (parser)
17207 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17208 /* An unqualified name was used to reference this type, so
17209 there were no qualifying templates. */
17210 if (template_parm_lists_apply
17211 && !cp_parser_check_template_parameters (parser,
17212 /*num_templates=*/0,
17213 token->location,
17214 /*declarator=*/NULL))
17215 return error_mark_node;
17216 type = xref_tag (tag_type, identifier, ts, template_p);
17220 if (type == error_mark_node)
17221 return error_mark_node;
17223 /* Allow attributes on forward declarations of classes. */
17224 if (attributes)
17226 if (TREE_CODE (type) == TYPENAME_TYPE)
17227 warning (OPT_Wattributes,
17228 "attributes ignored on uninstantiated type");
17229 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17230 && ! processing_explicit_instantiation)
17231 warning (OPT_Wattributes,
17232 "attributes ignored on template instantiation");
17233 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17234 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17235 else
17236 warning (OPT_Wattributes,
17237 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17240 if (tag_type != enum_type)
17242 /* Indicate whether this class was declared as a `class' or as a
17243 `struct'. */
17244 if (CLASS_TYPE_P (type))
17245 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17246 cp_parser_check_class_key (tag_type, type);
17249 /* A "<" cannot follow an elaborated type specifier. If that
17250 happens, the user was probably trying to form a template-id. */
17251 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17252 token->location);
17254 return type;
17257 /* Parse an enum-specifier.
17259 enum-specifier:
17260 enum-head { enumerator-list [opt] }
17261 enum-head { enumerator-list , } [C++0x]
17263 enum-head:
17264 enum-key identifier [opt] enum-base [opt]
17265 enum-key nested-name-specifier identifier enum-base [opt]
17267 enum-key:
17268 enum
17269 enum class [C++0x]
17270 enum struct [C++0x]
17272 enum-base: [C++0x]
17273 : type-specifier-seq
17275 opaque-enum-specifier:
17276 enum-key identifier enum-base [opt] ;
17278 GNU Extensions:
17279 enum-key attributes[opt] identifier [opt] enum-base [opt]
17280 { enumerator-list [opt] }attributes[opt]
17281 enum-key attributes[opt] identifier [opt] enum-base [opt]
17282 { enumerator-list, }attributes[opt] [C++0x]
17284 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17285 if the token stream isn't an enum-specifier after all. */
17287 static tree
17288 cp_parser_enum_specifier (cp_parser* parser)
17290 tree identifier;
17291 tree type = NULL_TREE;
17292 tree prev_scope;
17293 tree nested_name_specifier = NULL_TREE;
17294 tree attributes;
17295 bool scoped_enum_p = false;
17296 bool has_underlying_type = false;
17297 bool nested_being_defined = false;
17298 bool new_value_list = false;
17299 bool is_new_type = false;
17300 bool is_unnamed = false;
17301 tree underlying_type = NULL_TREE;
17302 cp_token *type_start_token = NULL;
17303 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17305 parser->colon_corrects_to_scope_p = false;
17307 /* Parse tentatively so that we can back up if we don't find a
17308 enum-specifier. */
17309 cp_parser_parse_tentatively (parser);
17311 /* Caller guarantees that the current token is 'enum', an identifier
17312 possibly follows, and the token after that is an opening brace.
17313 If we don't have an identifier, fabricate an anonymous name for
17314 the enumeration being defined. */
17315 cp_lexer_consume_token (parser->lexer);
17317 /* Parse the "class" or "struct", which indicates a scoped
17318 enumeration type in C++0x. */
17319 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17320 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17322 if (cxx_dialect < cxx11)
17323 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17325 /* Consume the `struct' or `class' token. */
17326 cp_lexer_consume_token (parser->lexer);
17328 scoped_enum_p = true;
17331 attributes = cp_parser_attributes_opt (parser);
17333 /* Clear the qualification. */
17334 parser->scope = NULL_TREE;
17335 parser->qualifying_scope = NULL_TREE;
17336 parser->object_scope = NULL_TREE;
17338 /* Figure out in what scope the declaration is being placed. */
17339 prev_scope = current_scope ();
17341 type_start_token = cp_lexer_peek_token (parser->lexer);
17343 push_deferring_access_checks (dk_no_check);
17344 nested_name_specifier
17345 = cp_parser_nested_name_specifier_opt (parser,
17346 /*typename_keyword_p=*/true,
17347 /*check_dependency_p=*/false,
17348 /*type_p=*/false,
17349 /*is_declaration=*/false);
17351 if (nested_name_specifier)
17353 tree name;
17355 identifier = cp_parser_identifier (parser);
17356 name = cp_parser_lookup_name (parser, identifier,
17357 enum_type,
17358 /*is_template=*/false,
17359 /*is_namespace=*/false,
17360 /*check_dependency=*/true,
17361 /*ambiguous_decls=*/NULL,
17362 input_location);
17363 if (name && name != error_mark_node)
17365 type = TREE_TYPE (name);
17366 if (TREE_CODE (type) == TYPENAME_TYPE)
17368 /* Are template enums allowed in ISO? */
17369 if (template_parm_scope_p ())
17370 pedwarn (type_start_token->location, OPT_Wpedantic,
17371 "%qD is an enumeration template", name);
17372 /* ignore a typename reference, for it will be solved by name
17373 in start_enum. */
17374 type = NULL_TREE;
17377 else if (nested_name_specifier == error_mark_node)
17378 /* We already issued an error. */;
17379 else
17381 error_at (type_start_token->location,
17382 "%qD does not name an enumeration in %qT",
17383 identifier, nested_name_specifier);
17384 nested_name_specifier = error_mark_node;
17387 else
17389 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17390 identifier = cp_parser_identifier (parser);
17391 else
17393 identifier = make_anon_name ();
17394 is_unnamed = true;
17395 if (scoped_enum_p)
17396 error_at (type_start_token->location,
17397 "unnamed scoped enum is not allowed");
17400 pop_deferring_access_checks ();
17402 /* Check for the `:' that denotes a specified underlying type in C++0x.
17403 Note that a ':' could also indicate a bitfield width, however. */
17404 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17406 cp_decl_specifier_seq type_specifiers;
17408 /* Consume the `:'. */
17409 cp_lexer_consume_token (parser->lexer);
17411 /* Parse the type-specifier-seq. */
17412 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17413 /*is_trailing_return=*/false,
17414 &type_specifiers);
17416 /* At this point this is surely not elaborated type specifier. */
17417 if (!cp_parser_parse_definitely (parser))
17418 return NULL_TREE;
17420 if (cxx_dialect < cxx11)
17421 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17423 has_underlying_type = true;
17425 /* If that didn't work, stop. */
17426 if (type_specifiers.type != error_mark_node)
17428 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17429 /*initialized=*/0, NULL);
17430 if (underlying_type == error_mark_node
17431 || check_for_bare_parameter_packs (underlying_type))
17432 underlying_type = NULL_TREE;
17436 /* Look for the `{' but don't consume it yet. */
17437 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17439 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17441 cp_parser_error (parser, "expected %<{%>");
17442 if (has_underlying_type)
17444 type = NULL_TREE;
17445 goto out;
17448 /* An opaque-enum-specifier must have a ';' here. */
17449 if ((scoped_enum_p || underlying_type)
17450 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17452 cp_parser_error (parser, "expected %<;%> or %<{%>");
17453 if (has_underlying_type)
17455 type = NULL_TREE;
17456 goto out;
17461 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17462 return NULL_TREE;
17464 if (nested_name_specifier)
17466 if (CLASS_TYPE_P (nested_name_specifier))
17468 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17469 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17470 push_scope (nested_name_specifier);
17472 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17474 push_nested_namespace (nested_name_specifier);
17478 /* Issue an error message if type-definitions are forbidden here. */
17479 if (!cp_parser_check_type_definition (parser))
17480 type = error_mark_node;
17481 else
17482 /* Create the new type. We do this before consuming the opening
17483 brace so the enum will be recorded as being on the line of its
17484 tag (or the 'enum' keyword, if there is no tag). */
17485 type = start_enum (identifier, type, underlying_type,
17486 attributes, scoped_enum_p, &is_new_type);
17488 /* If the next token is not '{' it is an opaque-enum-specifier or an
17489 elaborated-type-specifier. */
17490 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17492 timevar_push (TV_PARSE_ENUM);
17493 if (nested_name_specifier
17494 && nested_name_specifier != error_mark_node)
17496 /* The following catches invalid code such as:
17497 enum class S<int>::E { A, B, C }; */
17498 if (!processing_specialization
17499 && CLASS_TYPE_P (nested_name_specifier)
17500 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17501 error_at (type_start_token->location, "cannot add an enumerator "
17502 "list to a template instantiation");
17504 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17506 error_at (type_start_token->location,
17507 "%<%T::%E%> has not been declared",
17508 TYPE_CONTEXT (nested_name_specifier),
17509 nested_name_specifier);
17510 type = error_mark_node;
17512 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17513 && !CLASS_TYPE_P (nested_name_specifier))
17515 error_at (type_start_token->location, "nested name specifier "
17516 "%qT for enum declaration does not name a class "
17517 "or namespace", nested_name_specifier);
17518 type = error_mark_node;
17520 /* If that scope does not contain the scope in which the
17521 class was originally declared, the program is invalid. */
17522 else if (prev_scope && !is_ancestor (prev_scope,
17523 nested_name_specifier))
17525 if (at_namespace_scope_p ())
17526 error_at (type_start_token->location,
17527 "declaration of %qD in namespace %qD which does not "
17528 "enclose %qD",
17529 type, prev_scope, nested_name_specifier);
17530 else
17531 error_at (type_start_token->location,
17532 "declaration of %qD in %qD which does not "
17533 "enclose %qD",
17534 type, prev_scope, nested_name_specifier);
17535 type = error_mark_node;
17537 /* If that scope is the scope where the declaration is being placed
17538 the program is invalid. */
17539 else if (CLASS_TYPE_P (nested_name_specifier)
17540 && CLASS_TYPE_P (prev_scope)
17541 && same_type_p (nested_name_specifier, prev_scope))
17543 permerror (type_start_token->location,
17544 "extra qualification not allowed");
17545 nested_name_specifier = NULL_TREE;
17549 if (scoped_enum_p)
17550 begin_scope (sk_scoped_enum, type);
17552 /* Consume the opening brace. */
17553 cp_lexer_consume_token (parser->lexer);
17555 if (type == error_mark_node)
17556 ; /* Nothing to add */
17557 else if (OPAQUE_ENUM_P (type)
17558 || (cxx_dialect > cxx98 && processing_specialization))
17560 new_value_list = true;
17561 SET_OPAQUE_ENUM_P (type, false);
17562 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17564 else
17566 error_at (type_start_token->location,
17567 "multiple definition of %q#T", type);
17568 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17569 "previous definition here");
17570 type = error_mark_node;
17573 if (type == error_mark_node)
17574 cp_parser_skip_to_end_of_block_or_statement (parser);
17575 /* If the next token is not '}', then there are some enumerators. */
17576 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17578 if (is_unnamed && !scoped_enum_p)
17579 pedwarn (type_start_token->location, OPT_Wpedantic,
17580 "ISO C++ forbids empty unnamed enum");
17582 else
17583 cp_parser_enumerator_list (parser, type);
17585 /* Consume the final '}'. */
17586 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17588 if (scoped_enum_p)
17589 finish_scope ();
17590 timevar_pop (TV_PARSE_ENUM);
17592 else
17594 /* If a ';' follows, then it is an opaque-enum-specifier
17595 and additional restrictions apply. */
17596 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17598 if (is_unnamed)
17599 error_at (type_start_token->location,
17600 "opaque-enum-specifier without name");
17601 else if (nested_name_specifier)
17602 error_at (type_start_token->location,
17603 "opaque-enum-specifier must use a simple identifier");
17607 /* Look for trailing attributes to apply to this enumeration, and
17608 apply them if appropriate. */
17609 if (cp_parser_allow_gnu_extensions_p (parser))
17611 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17612 cplus_decl_attributes (&type,
17613 trailing_attr,
17614 (int) ATTR_FLAG_TYPE_IN_PLACE);
17617 /* Finish up the enumeration. */
17618 if (type != error_mark_node)
17620 if (new_value_list)
17621 finish_enum_value_list (type);
17622 if (is_new_type)
17623 finish_enum (type);
17626 if (nested_name_specifier)
17628 if (CLASS_TYPE_P (nested_name_specifier))
17630 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17631 pop_scope (nested_name_specifier);
17633 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17635 pop_nested_namespace (nested_name_specifier);
17638 out:
17639 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17640 return type;
17643 /* Parse an enumerator-list. The enumerators all have the indicated
17644 TYPE.
17646 enumerator-list:
17647 enumerator-definition
17648 enumerator-list , enumerator-definition */
17650 static void
17651 cp_parser_enumerator_list (cp_parser* parser, tree type)
17653 while (true)
17655 /* Parse an enumerator-definition. */
17656 cp_parser_enumerator_definition (parser, type);
17658 /* If the next token is not a ',', we've reached the end of
17659 the list. */
17660 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17661 break;
17662 /* Otherwise, consume the `,' and keep going. */
17663 cp_lexer_consume_token (parser->lexer);
17664 /* If the next token is a `}', there is a trailing comma. */
17665 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17667 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17668 pedwarn (input_location, OPT_Wpedantic,
17669 "comma at end of enumerator list");
17670 break;
17675 /* Parse an enumerator-definition. The enumerator has the indicated
17676 TYPE.
17678 enumerator-definition:
17679 enumerator
17680 enumerator = constant-expression
17682 enumerator:
17683 identifier
17685 GNU Extensions:
17687 enumerator-definition:
17688 enumerator attributes [opt]
17689 enumerator attributes [opt] = constant-expression */
17691 static void
17692 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17694 tree identifier;
17695 tree value;
17696 location_t loc;
17698 /* Save the input location because we are interested in the location
17699 of the identifier and not the location of the explicit value. */
17700 loc = cp_lexer_peek_token (parser->lexer)->location;
17702 /* Look for the identifier. */
17703 identifier = cp_parser_identifier (parser);
17704 if (identifier == error_mark_node)
17705 return;
17707 /* Parse any specified attributes. */
17708 tree attrs = cp_parser_attributes_opt (parser);
17710 /* If the next token is an '=', then there is an explicit value. */
17711 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17713 /* Consume the `=' token. */
17714 cp_lexer_consume_token (parser->lexer);
17715 /* Parse the value. */
17716 value = cp_parser_constant_expression (parser);
17718 else
17719 value = NULL_TREE;
17721 /* If we are processing a template, make sure the initializer of the
17722 enumerator doesn't contain any bare template parameter pack. */
17723 if (check_for_bare_parameter_packs (value))
17724 value = error_mark_node;
17726 /* Create the enumerator. */
17727 build_enumerator (identifier, value, type, attrs, loc);
17730 /* Parse a namespace-name.
17732 namespace-name:
17733 original-namespace-name
17734 namespace-alias
17736 Returns the NAMESPACE_DECL for the namespace. */
17738 static tree
17739 cp_parser_namespace_name (cp_parser* parser)
17741 tree identifier;
17742 tree namespace_decl;
17744 cp_token *token = cp_lexer_peek_token (parser->lexer);
17746 /* Get the name of the namespace. */
17747 identifier = cp_parser_identifier (parser);
17748 if (identifier == error_mark_node)
17749 return error_mark_node;
17751 /* Look up the identifier in the currently active scope. Look only
17752 for namespaces, due to:
17754 [basic.lookup.udir]
17756 When looking up a namespace-name in a using-directive or alias
17757 definition, only namespace names are considered.
17759 And:
17761 [basic.lookup.qual]
17763 During the lookup of a name preceding the :: scope resolution
17764 operator, object, function, and enumerator names are ignored.
17766 (Note that cp_parser_qualifying_entity only calls this
17767 function if the token after the name is the scope resolution
17768 operator.) */
17769 namespace_decl = cp_parser_lookup_name (parser, identifier,
17770 none_type,
17771 /*is_template=*/false,
17772 /*is_namespace=*/true,
17773 /*check_dependency=*/true,
17774 /*ambiguous_decls=*/NULL,
17775 token->location);
17776 /* If it's not a namespace, issue an error. */
17777 if (namespace_decl == error_mark_node
17778 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17780 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17781 error_at (token->location, "%qD is not a namespace-name", identifier);
17782 cp_parser_error (parser, "expected namespace-name");
17783 namespace_decl = error_mark_node;
17786 return namespace_decl;
17789 /* Parse a namespace-definition.
17791 namespace-definition:
17792 named-namespace-definition
17793 unnamed-namespace-definition
17795 named-namespace-definition:
17796 original-namespace-definition
17797 extension-namespace-definition
17799 original-namespace-definition:
17800 namespace identifier { namespace-body }
17802 extension-namespace-definition:
17803 namespace original-namespace-name { namespace-body }
17805 unnamed-namespace-definition:
17806 namespace { namespace-body } */
17808 static void
17809 cp_parser_namespace_definition (cp_parser* parser)
17811 tree identifier, attribs;
17812 bool has_visibility;
17813 bool is_inline;
17814 cp_token* token;
17815 int nested_definition_count = 0;
17817 cp_ensure_no_omp_declare_simd (parser);
17818 cp_ensure_no_oacc_routine (parser);
17819 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17821 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17822 is_inline = true;
17823 cp_lexer_consume_token (parser->lexer);
17825 else
17826 is_inline = false;
17828 /* Look for the `namespace' keyword. */
17829 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17831 /* Parse any specified attributes before the identifier. */
17832 attribs = cp_parser_attributes_opt (parser);
17834 /* Get the name of the namespace. We do not attempt to distinguish
17835 between an original-namespace-definition and an
17836 extension-namespace-definition at this point. The semantic
17837 analysis routines are responsible for that. */
17838 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17839 identifier = cp_parser_identifier (parser);
17840 else
17841 identifier = NULL_TREE;
17843 /* Parse any specified attributes after the identifier. */
17844 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17845 if (post_ident_attribs)
17847 if (attribs)
17848 attribs = chainon (attribs, post_ident_attribs);
17849 else
17850 attribs = post_ident_attribs;
17853 /* Start the namespace. */
17854 bool ok = push_namespace (identifier);
17856 /* Parse any nested namespace definition. */
17857 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17859 if (attribs)
17860 error_at (token->location, "a nested namespace definition cannot have attributes");
17861 if (cxx_dialect < cxx1z)
17862 pedwarn (input_location, OPT_Wpedantic,
17863 "nested namespace definitions only available with "
17864 "-std=c++1z or -std=gnu++1z");
17865 if (is_inline)
17866 error_at (token->location, "a nested namespace definition cannot be inline");
17867 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17869 cp_lexer_consume_token (parser->lexer);
17870 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17871 identifier = cp_parser_identifier (parser);
17872 else
17874 cp_parser_error (parser, "nested identifier required");
17875 break;
17877 ++nested_definition_count;
17878 push_namespace (identifier);
17882 /* Look for the `{' to validate starting the namespace. */
17883 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17885 /* "inline namespace" is equivalent to a stub namespace definition
17886 followed by a strong using directive. */
17887 if (is_inline && ok)
17889 tree name_space = current_namespace;
17890 /* Set up namespace association. */
17891 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17892 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17893 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17894 /* Import the contents of the inline namespace. */
17895 pop_namespace ();
17896 do_using_directive (name_space);
17897 push_namespace (identifier);
17900 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17902 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17904 /* Parse the body of the namespace. */
17905 cp_parser_namespace_body (parser);
17907 if (has_visibility)
17908 pop_visibility (1);
17910 /* Finish the nested namespace definitions. */
17911 while (nested_definition_count--)
17912 pop_namespace ();
17914 /* Finish the namespace. */
17915 if (ok)
17916 pop_namespace ();
17917 /* Look for the final `}'. */
17918 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17921 /* Parse a namespace-body.
17923 namespace-body:
17924 declaration-seq [opt] */
17926 static void
17927 cp_parser_namespace_body (cp_parser* parser)
17929 cp_parser_declaration_seq_opt (parser);
17932 /* Parse a namespace-alias-definition.
17934 namespace-alias-definition:
17935 namespace identifier = qualified-namespace-specifier ; */
17937 static void
17938 cp_parser_namespace_alias_definition (cp_parser* parser)
17940 tree identifier;
17941 tree namespace_specifier;
17943 cp_token *token = cp_lexer_peek_token (parser->lexer);
17945 /* Look for the `namespace' keyword. */
17946 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17947 /* Look for the identifier. */
17948 identifier = cp_parser_identifier (parser);
17949 if (identifier == error_mark_node)
17950 return;
17951 /* Look for the `=' token. */
17952 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17953 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17955 error_at (token->location, "%<namespace%> definition is not allowed here");
17956 /* Skip the definition. */
17957 cp_lexer_consume_token (parser->lexer);
17958 if (cp_parser_skip_to_closing_brace (parser))
17959 cp_lexer_consume_token (parser->lexer);
17960 return;
17962 cp_parser_require (parser, CPP_EQ, RT_EQ);
17963 /* Look for the qualified-namespace-specifier. */
17964 namespace_specifier
17965 = cp_parser_qualified_namespace_specifier (parser);
17966 /* Look for the `;' token. */
17967 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17969 /* Register the alias in the symbol table. */
17970 do_namespace_alias (identifier, namespace_specifier);
17973 /* Parse a qualified-namespace-specifier.
17975 qualified-namespace-specifier:
17976 :: [opt] nested-name-specifier [opt] namespace-name
17978 Returns a NAMESPACE_DECL corresponding to the specified
17979 namespace. */
17981 static tree
17982 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17984 /* Look for the optional `::'. */
17985 cp_parser_global_scope_opt (parser,
17986 /*current_scope_valid_p=*/false);
17988 /* Look for the optional nested-name-specifier. */
17989 cp_parser_nested_name_specifier_opt (parser,
17990 /*typename_keyword_p=*/false,
17991 /*check_dependency_p=*/true,
17992 /*type_p=*/false,
17993 /*is_declaration=*/true);
17995 return cp_parser_namespace_name (parser);
17998 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17999 access declaration.
18001 using-declaration:
18002 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18003 using :: unqualified-id ;
18005 access-declaration:
18006 qualified-id ;
18010 static bool
18011 cp_parser_using_declaration (cp_parser* parser,
18012 bool access_declaration_p)
18014 cp_token *token;
18015 bool typename_p = false;
18016 bool global_scope_p;
18017 tree decl;
18018 tree identifier;
18019 tree qscope;
18020 int oldcount = errorcount;
18021 cp_token *diag_token = NULL;
18023 if (access_declaration_p)
18025 diag_token = cp_lexer_peek_token (parser->lexer);
18026 cp_parser_parse_tentatively (parser);
18028 else
18030 /* Look for the `using' keyword. */
18031 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18033 /* Peek at the next token. */
18034 token = cp_lexer_peek_token (parser->lexer);
18035 /* See if it's `typename'. */
18036 if (token->keyword == RID_TYPENAME)
18038 /* Remember that we've seen it. */
18039 typename_p = true;
18040 /* Consume the `typename' token. */
18041 cp_lexer_consume_token (parser->lexer);
18045 /* Look for the optional global scope qualification. */
18046 global_scope_p
18047 = (cp_parser_global_scope_opt (parser,
18048 /*current_scope_valid_p=*/false)
18049 != NULL_TREE);
18051 /* If we saw `typename', or didn't see `::', then there must be a
18052 nested-name-specifier present. */
18053 if (typename_p || !global_scope_p)
18055 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18056 /*check_dependency_p=*/true,
18057 /*type_p=*/false,
18058 /*is_declaration=*/true);
18059 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18061 cp_parser_skip_to_end_of_block_or_statement (parser);
18062 return false;
18065 /* Otherwise, we could be in either of the two productions. In that
18066 case, treat the nested-name-specifier as optional. */
18067 else
18068 qscope = cp_parser_nested_name_specifier_opt (parser,
18069 /*typename_keyword_p=*/false,
18070 /*check_dependency_p=*/true,
18071 /*type_p=*/false,
18072 /*is_declaration=*/true);
18073 if (!qscope)
18074 qscope = global_namespace;
18075 else if (UNSCOPED_ENUM_P (qscope))
18076 qscope = CP_TYPE_CONTEXT (qscope);
18078 if (access_declaration_p && cp_parser_error_occurred (parser))
18079 /* Something has already gone wrong; there's no need to parse
18080 further. Since an error has occurred, the return value of
18081 cp_parser_parse_definitely will be false, as required. */
18082 return cp_parser_parse_definitely (parser);
18084 token = cp_lexer_peek_token (parser->lexer);
18085 /* Parse the unqualified-id. */
18086 identifier = cp_parser_unqualified_id (parser,
18087 /*template_keyword_p=*/false,
18088 /*check_dependency_p=*/true,
18089 /*declarator_p=*/true,
18090 /*optional_p=*/false);
18092 if (access_declaration_p)
18094 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18095 cp_parser_simulate_error (parser);
18096 if (!cp_parser_parse_definitely (parser))
18097 return false;
18100 /* The function we call to handle a using-declaration is different
18101 depending on what scope we are in. */
18102 if (qscope == error_mark_node || identifier == error_mark_node)
18104 else if (!identifier_p (identifier)
18105 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18106 /* [namespace.udecl]
18108 A using declaration shall not name a template-id. */
18109 error_at (token->location,
18110 "a template-id may not appear in a using-declaration");
18111 else
18113 if (at_class_scope_p ())
18115 /* Create the USING_DECL. */
18116 decl = do_class_using_decl (parser->scope, identifier);
18118 if (decl && typename_p)
18119 USING_DECL_TYPENAME_P (decl) = 1;
18121 if (check_for_bare_parameter_packs (decl))
18123 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18124 return false;
18126 else
18127 /* Add it to the list of members in this class. */
18128 finish_member_declaration (decl);
18130 else
18132 decl = cp_parser_lookup_name_simple (parser,
18133 identifier,
18134 token->location);
18135 if (decl == error_mark_node)
18136 cp_parser_name_lookup_error (parser, identifier,
18137 decl, NLE_NULL,
18138 token->location);
18139 else if (check_for_bare_parameter_packs (decl))
18141 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18142 return false;
18144 else if (!at_namespace_scope_p ())
18145 do_local_using_decl (decl, qscope, identifier);
18146 else
18147 do_toplevel_using_decl (decl, qscope, identifier);
18151 /* Look for the final `;'. */
18152 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18154 if (access_declaration_p && errorcount == oldcount)
18155 warning_at (diag_token->location, OPT_Wdeprecated,
18156 "access declarations are deprecated "
18157 "in favour of using-declarations; "
18158 "suggestion: add the %<using%> keyword");
18160 return true;
18163 /* Parse an alias-declaration.
18165 alias-declaration:
18166 using identifier attribute-specifier-seq [opt] = type-id */
18168 static tree
18169 cp_parser_alias_declaration (cp_parser* parser)
18171 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18172 location_t id_location;
18173 cp_declarator *declarator;
18174 cp_decl_specifier_seq decl_specs;
18175 bool member_p;
18176 const char *saved_message = NULL;
18178 /* Look for the `using' keyword. */
18179 cp_token *using_token
18180 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18181 if (using_token == NULL)
18182 return error_mark_node;
18184 id_location = cp_lexer_peek_token (parser->lexer)->location;
18185 id = cp_parser_identifier (parser);
18186 if (id == error_mark_node)
18187 return error_mark_node;
18189 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18190 attributes = cp_parser_attributes_opt (parser);
18191 if (attributes == error_mark_node)
18192 return error_mark_node;
18194 cp_parser_require (parser, CPP_EQ, RT_EQ);
18196 if (cp_parser_error_occurred (parser))
18197 return error_mark_node;
18199 cp_parser_commit_to_tentative_parse (parser);
18201 /* Now we are going to parse the type-id of the declaration. */
18204 [dcl.type]/3 says:
18206 "A type-specifier-seq shall not define a class or enumeration
18207 unless it appears in the type-id of an alias-declaration (7.1.3) that
18208 is not the declaration of a template-declaration."
18210 In other words, if we currently are in an alias template, the
18211 type-id should not define a type.
18213 So let's set parser->type_definition_forbidden_message in that
18214 case; cp_parser_check_type_definition (called by
18215 cp_parser_class_specifier) will then emit an error if a type is
18216 defined in the type-id. */
18217 if (parser->num_template_parameter_lists)
18219 saved_message = parser->type_definition_forbidden_message;
18220 parser->type_definition_forbidden_message =
18221 G_("types may not be defined in alias template declarations");
18224 type = cp_parser_type_id (parser);
18226 /* Restore the error message if need be. */
18227 if (parser->num_template_parameter_lists)
18228 parser->type_definition_forbidden_message = saved_message;
18230 if (type == error_mark_node
18231 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18233 cp_parser_skip_to_end_of_block_or_statement (parser);
18234 return error_mark_node;
18237 /* A typedef-name can also be introduced by an alias-declaration. The
18238 identifier following the using keyword becomes a typedef-name. It has
18239 the same semantics as if it were introduced by the typedef
18240 specifier. In particular, it does not define a new type and it shall
18241 not appear in the type-id. */
18243 clear_decl_specs (&decl_specs);
18244 decl_specs.type = type;
18245 if (attributes != NULL_TREE)
18247 decl_specs.attributes = attributes;
18248 set_and_check_decl_spec_loc (&decl_specs,
18249 ds_attribute,
18250 attrs_token);
18252 set_and_check_decl_spec_loc (&decl_specs,
18253 ds_typedef,
18254 using_token);
18255 set_and_check_decl_spec_loc (&decl_specs,
18256 ds_alias,
18257 using_token);
18259 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18260 declarator->id_loc = id_location;
18262 member_p = at_class_scope_p ();
18263 if (member_p)
18264 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18265 NULL_TREE, attributes);
18266 else
18267 decl = start_decl (declarator, &decl_specs, 0,
18268 attributes, NULL_TREE, &pushed_scope);
18269 if (decl == error_mark_node)
18270 return decl;
18272 // Attach constraints to the alias declaration.
18273 if (flag_concepts && current_template_parms)
18275 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18276 tree constr = build_constraints (reqs, NULL_TREE);
18277 set_constraints (decl, constr);
18280 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18282 if (pushed_scope)
18283 pop_scope (pushed_scope);
18285 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18286 added into the symbol table; otherwise, return the TYPE_DECL. */
18287 if (DECL_LANG_SPECIFIC (decl)
18288 && DECL_TEMPLATE_INFO (decl)
18289 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18291 decl = DECL_TI_TEMPLATE (decl);
18292 if (member_p)
18293 check_member_template (decl);
18296 return decl;
18299 /* Parse a using-directive.
18301 using-directive:
18302 using namespace :: [opt] nested-name-specifier [opt]
18303 namespace-name ; */
18305 static void
18306 cp_parser_using_directive (cp_parser* parser)
18308 tree namespace_decl;
18309 tree attribs;
18311 /* Look for the `using' keyword. */
18312 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18313 /* And the `namespace' keyword. */
18314 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18315 /* Look for the optional `::' operator. */
18316 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18317 /* And the optional nested-name-specifier. */
18318 cp_parser_nested_name_specifier_opt (parser,
18319 /*typename_keyword_p=*/false,
18320 /*check_dependency_p=*/true,
18321 /*type_p=*/false,
18322 /*is_declaration=*/true);
18323 /* Get the namespace being used. */
18324 namespace_decl = cp_parser_namespace_name (parser);
18325 /* And any specified attributes. */
18326 attribs = cp_parser_attributes_opt (parser);
18327 /* Update the symbol table. */
18328 parse_using_directive (namespace_decl, attribs);
18329 /* Look for the final `;'. */
18330 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18333 /* Parse an asm-definition.
18335 asm-definition:
18336 asm ( string-literal ) ;
18338 GNU Extension:
18340 asm-definition:
18341 asm volatile [opt] ( string-literal ) ;
18342 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18343 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18344 : asm-operand-list [opt] ) ;
18345 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18346 : asm-operand-list [opt]
18347 : asm-clobber-list [opt] ) ;
18348 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18349 : asm-clobber-list [opt]
18350 : asm-goto-list ) ; */
18352 static void
18353 cp_parser_asm_definition (cp_parser* parser)
18355 tree string;
18356 tree outputs = NULL_TREE;
18357 tree inputs = NULL_TREE;
18358 tree clobbers = NULL_TREE;
18359 tree labels = NULL_TREE;
18360 tree asm_stmt;
18361 bool volatile_p = false;
18362 bool extended_p = false;
18363 bool invalid_inputs_p = false;
18364 bool invalid_outputs_p = false;
18365 bool goto_p = false;
18366 required_token missing = RT_NONE;
18368 /* Look for the `asm' keyword. */
18369 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18371 if (parser->in_function_body
18372 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18374 error ("%<asm%> in %<constexpr%> function");
18375 cp_function_chain->invalid_constexpr = true;
18378 /* See if the next token is `volatile'. */
18379 if (cp_parser_allow_gnu_extensions_p (parser)
18380 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18382 /* Remember that we saw the `volatile' keyword. */
18383 volatile_p = true;
18384 /* Consume the token. */
18385 cp_lexer_consume_token (parser->lexer);
18387 if (cp_parser_allow_gnu_extensions_p (parser)
18388 && parser->in_function_body
18389 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18391 /* Remember that we saw the `goto' keyword. */
18392 goto_p = true;
18393 /* Consume the token. */
18394 cp_lexer_consume_token (parser->lexer);
18396 /* Look for the opening `('. */
18397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18398 return;
18399 /* Look for the string. */
18400 string = cp_parser_string_literal (parser, false, false);
18401 if (string == error_mark_node)
18403 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18404 /*consume_paren=*/true);
18405 return;
18408 /* If we're allowing GNU extensions, check for the extended assembly
18409 syntax. Unfortunately, the `:' tokens need not be separated by
18410 a space in C, and so, for compatibility, we tolerate that here
18411 too. Doing that means that we have to treat the `::' operator as
18412 two `:' tokens. */
18413 if (cp_parser_allow_gnu_extensions_p (parser)
18414 && parser->in_function_body
18415 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18416 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18418 bool inputs_p = false;
18419 bool clobbers_p = false;
18420 bool labels_p = false;
18422 /* The extended syntax was used. */
18423 extended_p = true;
18425 /* Look for outputs. */
18426 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18428 /* Consume the `:'. */
18429 cp_lexer_consume_token (parser->lexer);
18430 /* Parse the output-operands. */
18431 if (cp_lexer_next_token_is_not (parser->lexer,
18432 CPP_COLON)
18433 && cp_lexer_next_token_is_not (parser->lexer,
18434 CPP_SCOPE)
18435 && cp_lexer_next_token_is_not (parser->lexer,
18436 CPP_CLOSE_PAREN)
18437 && !goto_p)
18439 outputs = cp_parser_asm_operand_list (parser);
18440 if (outputs == error_mark_node)
18441 invalid_outputs_p = true;
18444 /* If the next token is `::', there are no outputs, and the
18445 next token is the beginning of the inputs. */
18446 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18447 /* The inputs are coming next. */
18448 inputs_p = true;
18450 /* Look for inputs. */
18451 if (inputs_p
18452 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18454 /* Consume the `:' or `::'. */
18455 cp_lexer_consume_token (parser->lexer);
18456 /* Parse the output-operands. */
18457 if (cp_lexer_next_token_is_not (parser->lexer,
18458 CPP_COLON)
18459 && cp_lexer_next_token_is_not (parser->lexer,
18460 CPP_SCOPE)
18461 && cp_lexer_next_token_is_not (parser->lexer,
18462 CPP_CLOSE_PAREN))
18464 inputs = cp_parser_asm_operand_list (parser);
18465 if (inputs == error_mark_node)
18466 invalid_inputs_p = true;
18469 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18470 /* The clobbers are coming next. */
18471 clobbers_p = true;
18473 /* Look for clobbers. */
18474 if (clobbers_p
18475 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18477 clobbers_p = true;
18478 /* Consume the `:' or `::'. */
18479 cp_lexer_consume_token (parser->lexer);
18480 /* Parse the clobbers. */
18481 if (cp_lexer_next_token_is_not (parser->lexer,
18482 CPP_COLON)
18483 && cp_lexer_next_token_is_not (parser->lexer,
18484 CPP_CLOSE_PAREN))
18485 clobbers = cp_parser_asm_clobber_list (parser);
18487 else if (goto_p
18488 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18489 /* The labels are coming next. */
18490 labels_p = true;
18492 /* Look for labels. */
18493 if (labels_p
18494 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18496 labels_p = true;
18497 /* Consume the `:' or `::'. */
18498 cp_lexer_consume_token (parser->lexer);
18499 /* Parse the labels. */
18500 labels = cp_parser_asm_label_list (parser);
18503 if (goto_p && !labels_p)
18504 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18506 else if (goto_p)
18507 missing = RT_COLON_SCOPE;
18509 /* Look for the closing `)'. */
18510 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18511 missing ? missing : RT_CLOSE_PAREN))
18512 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18513 /*consume_paren=*/true);
18514 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18516 if (!invalid_inputs_p && !invalid_outputs_p)
18518 /* Create the ASM_EXPR. */
18519 if (parser->in_function_body)
18521 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18522 inputs, clobbers, labels);
18523 /* If the extended syntax was not used, mark the ASM_EXPR. */
18524 if (!extended_p)
18526 tree temp = asm_stmt;
18527 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18528 temp = TREE_OPERAND (temp, 0);
18530 ASM_INPUT_P (temp) = 1;
18533 else
18534 symtab->finalize_toplevel_asm (string);
18538 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18539 type that comes from the decl-specifier-seq. */
18541 static tree
18542 strip_declarator_types (tree type, cp_declarator *declarator)
18544 for (cp_declarator *d = declarator; d;)
18545 switch (d->kind)
18547 case cdk_id:
18548 case cdk_error:
18549 d = NULL;
18550 break;
18552 default:
18553 if (TYPE_PTRMEMFUNC_P (type))
18554 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18555 type = TREE_TYPE (type);
18556 d = d->declarator;
18557 break;
18560 return type;
18563 /* Declarators [gram.dcl.decl] */
18565 /* Parse an init-declarator.
18567 init-declarator:
18568 declarator initializer [opt]
18570 GNU Extension:
18572 init-declarator:
18573 declarator asm-specification [opt] attributes [opt] initializer [opt]
18575 function-definition:
18576 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18577 function-body
18578 decl-specifier-seq [opt] declarator function-try-block
18580 GNU Extension:
18582 function-definition:
18583 __extension__ function-definition
18585 TM Extension:
18587 function-definition:
18588 decl-specifier-seq [opt] declarator function-transaction-block
18590 The DECL_SPECIFIERS apply to this declarator. Returns a
18591 representation of the entity declared. If MEMBER_P is TRUE, then
18592 this declarator appears in a class scope. The new DECL created by
18593 this declarator is returned.
18595 The CHECKS are access checks that should be performed once we know
18596 what entity is being declared (and, therefore, what classes have
18597 befriended it).
18599 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18600 for a function-definition here as well. If the declarator is a
18601 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18602 be TRUE upon return. By that point, the function-definition will
18603 have been completely parsed.
18605 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18606 is FALSE.
18608 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18609 parsed declaration if it is an uninitialized single declarator not followed
18610 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18611 if present, will not be consumed. If returned, this declarator will be
18612 created with SD_INITIALIZED but will not call cp_finish_decl.
18614 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18615 and there is an initializer, the pointed location_t is set to the
18616 location of the '=' or `(', or '{' in C++11 token introducing the
18617 initializer. */
18619 static tree
18620 cp_parser_init_declarator (cp_parser* parser,
18621 cp_decl_specifier_seq *decl_specifiers,
18622 vec<deferred_access_check, va_gc> *checks,
18623 bool function_definition_allowed_p,
18624 bool member_p,
18625 int declares_class_or_enum,
18626 bool* function_definition_p,
18627 tree* maybe_range_for_decl,
18628 location_t* init_loc,
18629 tree* auto_result)
18631 cp_token *token = NULL, *asm_spec_start_token = NULL,
18632 *attributes_start_token = NULL;
18633 cp_declarator *declarator;
18634 tree prefix_attributes;
18635 tree attributes = NULL;
18636 tree asm_specification;
18637 tree initializer;
18638 tree decl = NULL_TREE;
18639 tree scope;
18640 int is_initialized;
18641 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18642 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18643 "(...)". */
18644 enum cpp_ttype initialization_kind;
18645 bool is_direct_init = false;
18646 bool is_non_constant_init;
18647 int ctor_dtor_or_conv_p;
18648 bool friend_p = cp_parser_friend_p (decl_specifiers);
18649 tree pushed_scope = NULL_TREE;
18650 bool range_for_decl_p = false;
18651 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18652 location_t tmp_init_loc = UNKNOWN_LOCATION;
18654 /* Gather the attributes that were provided with the
18655 decl-specifiers. */
18656 prefix_attributes = decl_specifiers->attributes;
18658 /* Assume that this is not the declarator for a function
18659 definition. */
18660 if (function_definition_p)
18661 *function_definition_p = false;
18663 /* Default arguments are only permitted for function parameters. */
18664 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18665 parser->default_arg_ok_p = false;
18667 /* Defer access checks while parsing the declarator; we cannot know
18668 what names are accessible until we know what is being
18669 declared. */
18670 resume_deferring_access_checks ();
18672 token = cp_lexer_peek_token (parser->lexer);
18674 cp_parser_declarator_kind cdk = CP_PARSER_DECLARATOR_NAMED;
18675 if (token->type == CPP_OPEN_PAREN
18676 && decl_specifiers->type
18677 && is_auto (decl_specifiers->type)
18678 && CLASS_PLACEHOLDER_TEMPLATE (decl_specifiers->type))
18680 // C++17 deduction guide.
18681 cdk = CP_PARSER_DECLARATOR_ABSTRACT;
18683 for (int i = 0; i < ds_last; ++i)
18684 if (i != ds_type_spec
18685 && decl_specifiers->locations[i]
18686 && !cp_parser_simulate_error (parser))
18687 error_at (decl_specifiers->locations[i],
18688 "decl-specifier in declaration of deduction guide");
18691 /* Parse the declarator. */
18692 declarator
18693 = cp_parser_declarator (parser, cdk,
18694 &ctor_dtor_or_conv_p,
18695 /*parenthesized_p=*/NULL,
18696 member_p, friend_p);
18697 /* Gather up the deferred checks. */
18698 stop_deferring_access_checks ();
18700 parser->default_arg_ok_p = saved_default_arg_ok_p;
18702 /* If the DECLARATOR was erroneous, there's no need to go
18703 further. */
18704 if (declarator == cp_error_declarator)
18705 return error_mark_node;
18707 if (cdk == CP_PARSER_DECLARATOR_ABSTRACT)
18709 gcc_assert (declarator->kind == cdk_function
18710 && !declarator->declarator);
18711 tree t = CLASS_PLACEHOLDER_TEMPLATE (decl_specifiers->type);
18712 declarator->declarator = make_id_declarator (NULL_TREE, dguide_name (t),
18713 sfk_none);
18714 declarator->declarator->id_loc
18715 = decl_specifiers->locations[ds_type_spec];
18718 /* Check that the number of template-parameter-lists is OK. */
18719 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18720 token->location))
18721 return error_mark_node;
18723 if (declares_class_or_enum & 2)
18724 cp_parser_check_for_definition_in_return_type (declarator,
18725 decl_specifiers->type,
18726 decl_specifiers->locations[ds_type_spec]);
18728 /* Figure out what scope the entity declared by the DECLARATOR is
18729 located in. `grokdeclarator' sometimes changes the scope, so
18730 we compute it now. */
18731 scope = get_scope_of_declarator (declarator);
18733 /* Perform any lookups in the declared type which were thought to be
18734 dependent, but are not in the scope of the declarator. */
18735 decl_specifiers->type
18736 = maybe_update_decl_type (decl_specifiers->type, scope);
18738 /* If we're allowing GNU extensions, look for an
18739 asm-specification. */
18740 if (cp_parser_allow_gnu_extensions_p (parser))
18742 /* Look for an asm-specification. */
18743 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18744 asm_specification = cp_parser_asm_specification_opt (parser);
18746 else
18747 asm_specification = NULL_TREE;
18749 /* Look for attributes. */
18750 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18751 attributes = cp_parser_attributes_opt (parser);
18753 /* Peek at the next token. */
18754 token = cp_lexer_peek_token (parser->lexer);
18756 bool bogus_implicit_tmpl = false;
18758 if (function_declarator_p (declarator))
18760 /* Check to see if the token indicates the start of a
18761 function-definition. */
18762 if (cp_parser_token_starts_function_definition_p (token))
18764 if (!function_definition_allowed_p)
18766 /* If a function-definition should not appear here, issue an
18767 error message. */
18768 cp_parser_error (parser,
18769 "a function-definition is not allowed here");
18770 return error_mark_node;
18773 location_t func_brace_location
18774 = cp_lexer_peek_token (parser->lexer)->location;
18776 /* Neither attributes nor an asm-specification are allowed
18777 on a function-definition. */
18778 if (asm_specification)
18779 error_at (asm_spec_start_token->location,
18780 "an asm-specification is not allowed "
18781 "on a function-definition");
18782 if (attributes)
18783 error_at (attributes_start_token->location,
18784 "attributes are not allowed "
18785 "on a function-definition");
18786 /* This is a function-definition. */
18787 *function_definition_p = true;
18789 /* Parse the function definition. */
18790 if (member_p)
18791 decl = cp_parser_save_member_function_body (parser,
18792 decl_specifiers,
18793 declarator,
18794 prefix_attributes);
18795 else
18796 decl =
18797 (cp_parser_function_definition_from_specifiers_and_declarator
18798 (parser, decl_specifiers, prefix_attributes, declarator));
18800 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18802 /* This is where the prologue starts... */
18803 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18804 = func_brace_location;
18807 return decl;
18810 else if (parser->fully_implicit_function_template_p)
18812 /* A non-template declaration involving a function parameter list
18813 containing an implicit template parameter will be made into a
18814 template. If the resulting declaration is not going to be an
18815 actual function then finish the template scope here to prevent it.
18816 An error message will be issued once we have a decl to talk about.
18818 FIXME probably we should do type deduction rather than create an
18819 implicit template, but the standard currently doesn't allow it. */
18820 bogus_implicit_tmpl = true;
18821 finish_fully_implicit_template (parser, NULL_TREE);
18824 /* [dcl.dcl]
18826 Only in function declarations for constructors, destructors, and
18827 type conversions can the decl-specifier-seq be omitted.
18829 We explicitly postpone this check past the point where we handle
18830 function-definitions because we tolerate function-definitions
18831 that are missing their return types in some modes. */
18832 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18834 cp_parser_error (parser,
18835 "expected constructor, destructor, or type conversion");
18836 return error_mark_node;
18839 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18840 if (token->type == CPP_EQ
18841 || token->type == CPP_OPEN_PAREN
18842 || token->type == CPP_OPEN_BRACE)
18844 is_initialized = SD_INITIALIZED;
18845 initialization_kind = token->type;
18846 if (maybe_range_for_decl)
18847 *maybe_range_for_decl = error_mark_node;
18848 tmp_init_loc = token->location;
18849 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18850 *init_loc = tmp_init_loc;
18852 if (token->type == CPP_EQ
18853 && function_declarator_p (declarator))
18855 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18856 if (t2->keyword == RID_DEFAULT)
18857 is_initialized = SD_DEFAULTED;
18858 else if (t2->keyword == RID_DELETE)
18859 is_initialized = SD_DELETED;
18862 else
18864 /* If the init-declarator isn't initialized and isn't followed by a
18865 `,' or `;', it's not a valid init-declarator. */
18866 if (token->type != CPP_COMMA
18867 && token->type != CPP_SEMICOLON)
18869 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18870 range_for_decl_p = true;
18871 else
18873 if (!maybe_range_for_decl)
18874 cp_parser_error (parser, "expected initializer");
18875 return error_mark_node;
18878 is_initialized = SD_UNINITIALIZED;
18879 initialization_kind = CPP_EOF;
18882 /* Because start_decl has side-effects, we should only call it if we
18883 know we're going ahead. By this point, we know that we cannot
18884 possibly be looking at any other construct. */
18885 cp_parser_commit_to_tentative_parse (parser);
18887 /* Enter the newly declared entry in the symbol table. If we're
18888 processing a declaration in a class-specifier, we wait until
18889 after processing the initializer. */
18890 if (!member_p)
18892 if (parser->in_unbraced_linkage_specification_p)
18893 decl_specifiers->storage_class = sc_extern;
18894 decl = start_decl (declarator, decl_specifiers,
18895 range_for_decl_p? SD_INITIALIZED : is_initialized,
18896 attributes, prefix_attributes, &pushed_scope);
18897 cp_finalize_omp_declare_simd (parser, decl);
18898 cp_finalize_oacc_routine (parser, decl, false);
18899 /* Adjust location of decl if declarator->id_loc is more appropriate:
18900 set, and decl wasn't merged with another decl, in which case its
18901 location would be different from input_location, and more accurate. */
18902 if (DECL_P (decl)
18903 && declarator->id_loc != UNKNOWN_LOCATION
18904 && DECL_SOURCE_LOCATION (decl) == input_location)
18905 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18907 else if (scope)
18908 /* Enter the SCOPE. That way unqualified names appearing in the
18909 initializer will be looked up in SCOPE. */
18910 pushed_scope = push_scope (scope);
18912 /* Perform deferred access control checks, now that we know in which
18913 SCOPE the declared entity resides. */
18914 if (!member_p && decl)
18916 tree saved_current_function_decl = NULL_TREE;
18918 /* If the entity being declared is a function, pretend that we
18919 are in its scope. If it is a `friend', it may have access to
18920 things that would not otherwise be accessible. */
18921 if (TREE_CODE (decl) == FUNCTION_DECL)
18923 saved_current_function_decl = current_function_decl;
18924 current_function_decl = decl;
18927 /* Perform access checks for template parameters. */
18928 cp_parser_perform_template_parameter_access_checks (checks);
18930 /* Perform the access control checks for the declarator and the
18931 decl-specifiers. */
18932 perform_deferred_access_checks (tf_warning_or_error);
18934 /* Restore the saved value. */
18935 if (TREE_CODE (decl) == FUNCTION_DECL)
18936 current_function_decl = saved_current_function_decl;
18939 /* Parse the initializer. */
18940 initializer = NULL_TREE;
18941 is_direct_init = false;
18942 is_non_constant_init = true;
18943 if (is_initialized)
18945 if (function_declarator_p (declarator))
18947 if (initialization_kind == CPP_EQ)
18948 initializer = cp_parser_pure_specifier (parser);
18949 else
18951 /* If the declaration was erroneous, we don't really
18952 know what the user intended, so just silently
18953 consume the initializer. */
18954 if (decl != error_mark_node)
18955 error_at (tmp_init_loc, "initializer provided for function");
18956 cp_parser_skip_to_closing_parenthesis (parser,
18957 /*recovering=*/true,
18958 /*or_comma=*/false,
18959 /*consume_paren=*/true);
18962 else
18964 /* We want to record the extra mangling scope for in-class
18965 initializers of class members and initializers of static data
18966 member templates. The former involves deferring
18967 parsing of the initializer until end of class as with default
18968 arguments. So right here we only handle the latter. */
18969 if (!member_p && processing_template_decl)
18970 start_lambda_scope (decl);
18971 initializer = cp_parser_initializer (parser,
18972 &is_direct_init,
18973 &is_non_constant_init);
18974 if (!member_p && processing_template_decl)
18975 finish_lambda_scope ();
18976 if (initializer == error_mark_node)
18977 cp_parser_skip_to_end_of_statement (parser);
18981 /* The old parser allows attributes to appear after a parenthesized
18982 initializer. Mark Mitchell proposed removing this functionality
18983 on the GCC mailing lists on 2002-08-13. This parser accepts the
18984 attributes -- but ignores them. */
18985 if (cp_parser_allow_gnu_extensions_p (parser)
18986 && initialization_kind == CPP_OPEN_PAREN)
18987 if (cp_parser_attributes_opt (parser))
18988 warning (OPT_Wattributes,
18989 "attributes after parenthesized initializer ignored");
18991 /* And now complain about a non-function implicit template. */
18992 if (bogus_implicit_tmpl && decl != error_mark_node)
18993 error_at (DECL_SOURCE_LOCATION (decl),
18994 "non-function %qD declared as implicit template", decl);
18996 /* For an in-class declaration, use `grokfield' to create the
18997 declaration. */
18998 if (member_p)
19000 if (pushed_scope)
19002 pop_scope (pushed_scope);
19003 pushed_scope = NULL_TREE;
19005 decl = grokfield (declarator, decl_specifiers,
19006 initializer, !is_non_constant_init,
19007 /*asmspec=*/NULL_TREE,
19008 chainon (attributes, prefix_attributes));
19009 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19010 cp_parser_save_default_args (parser, decl);
19011 cp_finalize_omp_declare_simd (parser, decl);
19012 cp_finalize_oacc_routine (parser, decl, false);
19015 /* Finish processing the declaration. But, skip member
19016 declarations. */
19017 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19019 cp_finish_decl (decl,
19020 initializer, !is_non_constant_init,
19021 asm_specification,
19022 /* If the initializer is in parentheses, then this is
19023 a direct-initialization, which means that an
19024 `explicit' constructor is OK. Otherwise, an
19025 `explicit' constructor cannot be used. */
19026 ((is_direct_init || !is_initialized)
19027 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19029 else if ((cxx_dialect != cxx98) && friend_p
19030 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19031 /* Core issue #226 (C++0x only): A default template-argument
19032 shall not be specified in a friend class template
19033 declaration. */
19034 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19035 /*is_partial=*/false, /*is_friend_decl=*/1);
19037 if (!friend_p && pushed_scope)
19038 pop_scope (pushed_scope);
19040 if (function_declarator_p (declarator)
19041 && parser->fully_implicit_function_template_p)
19043 if (member_p)
19044 decl = finish_fully_implicit_template (parser, decl);
19045 else
19046 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19049 if (auto_result && is_initialized && decl_specifiers->type
19050 && type_uses_auto (decl_specifiers->type))
19051 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19053 return decl;
19056 /* Parse a declarator.
19058 declarator:
19059 direct-declarator
19060 ptr-operator declarator
19062 abstract-declarator:
19063 ptr-operator abstract-declarator [opt]
19064 direct-abstract-declarator
19066 GNU Extensions:
19068 declarator:
19069 attributes [opt] direct-declarator
19070 attributes [opt] ptr-operator declarator
19072 abstract-declarator:
19073 attributes [opt] ptr-operator abstract-declarator [opt]
19074 attributes [opt] direct-abstract-declarator
19076 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19077 detect constructor, destructor or conversion operators. It is set
19078 to -1 if the declarator is a name, and +1 if it is a
19079 function. Otherwise it is set to zero. Usually you just want to
19080 test for >0, but internally the negative value is used.
19082 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19083 a decl-specifier-seq unless it declares a constructor, destructor,
19084 or conversion. It might seem that we could check this condition in
19085 semantic analysis, rather than parsing, but that makes it difficult
19086 to handle something like `f()'. We want to notice that there are
19087 no decl-specifiers, and therefore realize that this is an
19088 expression, not a declaration.)
19090 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19091 the declarator is a direct-declarator of the form "(...)".
19093 MEMBER_P is true iff this declarator is a member-declarator.
19095 FRIEND_P is true iff this declarator is a friend. */
19097 static cp_declarator *
19098 cp_parser_declarator (cp_parser* parser,
19099 cp_parser_declarator_kind dcl_kind,
19100 int* ctor_dtor_or_conv_p,
19101 bool* parenthesized_p,
19102 bool member_p, bool friend_p)
19104 cp_declarator *declarator;
19105 enum tree_code code;
19106 cp_cv_quals cv_quals;
19107 tree class_type;
19108 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19110 /* Assume this is not a constructor, destructor, or type-conversion
19111 operator. */
19112 if (ctor_dtor_or_conv_p)
19113 *ctor_dtor_or_conv_p = 0;
19115 if (cp_parser_allow_gnu_extensions_p (parser))
19116 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19118 /* Check for the ptr-operator production. */
19119 cp_parser_parse_tentatively (parser);
19120 /* Parse the ptr-operator. */
19121 code = cp_parser_ptr_operator (parser,
19122 &class_type,
19123 &cv_quals,
19124 &std_attributes);
19126 /* If that worked, then we have a ptr-operator. */
19127 if (cp_parser_parse_definitely (parser))
19129 /* If a ptr-operator was found, then this declarator was not
19130 parenthesized. */
19131 if (parenthesized_p)
19132 *parenthesized_p = true;
19133 /* The dependent declarator is optional if we are parsing an
19134 abstract-declarator. */
19135 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19136 cp_parser_parse_tentatively (parser);
19138 /* Parse the dependent declarator. */
19139 declarator = cp_parser_declarator (parser, dcl_kind,
19140 /*ctor_dtor_or_conv_p=*/NULL,
19141 /*parenthesized_p=*/NULL,
19142 /*member_p=*/false,
19143 friend_p);
19145 /* If we are parsing an abstract-declarator, we must handle the
19146 case where the dependent declarator is absent. */
19147 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19148 && !cp_parser_parse_definitely (parser))
19149 declarator = NULL;
19151 declarator = cp_parser_make_indirect_declarator
19152 (code, class_type, cv_quals, declarator, std_attributes);
19154 /* Everything else is a direct-declarator. */
19155 else
19157 if (parenthesized_p)
19158 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19159 CPP_OPEN_PAREN);
19160 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19161 ctor_dtor_or_conv_p,
19162 member_p, friend_p);
19165 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19166 declarator->attributes = gnu_attributes;
19167 return declarator;
19170 /* Parse a direct-declarator or direct-abstract-declarator.
19172 direct-declarator:
19173 declarator-id
19174 direct-declarator ( parameter-declaration-clause )
19175 cv-qualifier-seq [opt]
19176 ref-qualifier [opt]
19177 exception-specification [opt]
19178 direct-declarator [ constant-expression [opt] ]
19179 ( declarator )
19181 direct-abstract-declarator:
19182 direct-abstract-declarator [opt]
19183 ( parameter-declaration-clause )
19184 cv-qualifier-seq [opt]
19185 ref-qualifier [opt]
19186 exception-specification [opt]
19187 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19188 ( abstract-declarator )
19190 Returns a representation of the declarator. DCL_KIND is
19191 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19192 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19193 we are parsing a direct-declarator. It is
19194 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19195 of ambiguity we prefer an abstract declarator, as per
19196 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19197 as for cp_parser_declarator. */
19199 static cp_declarator *
19200 cp_parser_direct_declarator (cp_parser* parser,
19201 cp_parser_declarator_kind dcl_kind,
19202 int* ctor_dtor_or_conv_p,
19203 bool member_p, bool friend_p)
19205 cp_token *token;
19206 cp_declarator *declarator = NULL;
19207 tree scope = NULL_TREE;
19208 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19209 bool saved_in_declarator_p = parser->in_declarator_p;
19210 bool first = true;
19211 tree pushed_scope = NULL_TREE;
19213 while (true)
19215 /* Peek at the next token. */
19216 token = cp_lexer_peek_token (parser->lexer);
19217 if (token->type == CPP_OPEN_PAREN)
19219 /* This is either a parameter-declaration-clause, or a
19220 parenthesized declarator. When we know we are parsing a
19221 named declarator, it must be a parenthesized declarator
19222 if FIRST is true. For instance, `(int)' is a
19223 parameter-declaration-clause, with an omitted
19224 direct-abstract-declarator. But `((*))', is a
19225 parenthesized abstract declarator. Finally, when T is a
19226 template parameter `(T)' is a
19227 parameter-declaration-clause, and not a parenthesized
19228 named declarator.
19230 We first try and parse a parameter-declaration-clause,
19231 and then try a nested declarator (if FIRST is true).
19233 It is not an error for it not to be a
19234 parameter-declaration-clause, even when FIRST is
19235 false. Consider,
19237 int i (int);
19238 int i (3);
19240 The first is the declaration of a function while the
19241 second is the definition of a variable, including its
19242 initializer.
19244 Having seen only the parenthesis, we cannot know which of
19245 these two alternatives should be selected. Even more
19246 complex are examples like:
19248 int i (int (a));
19249 int i (int (3));
19251 The former is a function-declaration; the latter is a
19252 variable initialization.
19254 Thus again, we try a parameter-declaration-clause, and if
19255 that fails, we back out and return. */
19257 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19259 tree params;
19260 bool is_declarator = false;
19262 /* In a member-declarator, the only valid interpretation
19263 of a parenthesis is the start of a
19264 parameter-declaration-clause. (It is invalid to
19265 initialize a static data member with a parenthesized
19266 initializer; only the "=" form of initialization is
19267 permitted.) */
19268 if (!member_p)
19269 cp_parser_parse_tentatively (parser);
19271 /* Consume the `('. */
19272 cp_lexer_consume_token (parser->lexer);
19273 if (first)
19275 /* If this is going to be an abstract declarator, we're
19276 in a declarator and we can't have default args. */
19277 parser->default_arg_ok_p = false;
19278 parser->in_declarator_p = true;
19281 begin_scope (sk_function_parms, NULL_TREE);
19283 /* Parse the parameter-declaration-clause. */
19284 params = cp_parser_parameter_declaration_clause (parser);
19286 /* Consume the `)'. */
19287 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19289 /* If all went well, parse the cv-qualifier-seq,
19290 ref-qualifier and the exception-specification. */
19291 if (member_p || cp_parser_parse_definitely (parser))
19293 cp_cv_quals cv_quals;
19294 cp_virt_specifiers virt_specifiers;
19295 cp_ref_qualifier ref_qual;
19296 tree exception_specification;
19297 tree late_return;
19298 tree attrs;
19299 bool memfn = (member_p || (pushed_scope
19300 && CLASS_TYPE_P (pushed_scope)));
19302 is_declarator = true;
19304 if (ctor_dtor_or_conv_p)
19305 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19306 first = false;
19308 /* Parse the cv-qualifier-seq. */
19309 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19310 /* Parse the ref-qualifier. */
19311 ref_qual = cp_parser_ref_qualifier_opt (parser);
19312 /* Parse the tx-qualifier. */
19313 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19314 /* And the exception-specification. */
19315 exception_specification
19316 = cp_parser_exception_specification_opt (parser);
19318 attrs = cp_parser_std_attribute_spec_seq (parser);
19320 /* In here, we handle cases where attribute is used after
19321 the function declaration. For example:
19322 void func (int x) __attribute__((vector(..))); */
19323 tree gnu_attrs = NULL_TREE;
19324 if (flag_cilkplus
19325 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19327 cp_parser_parse_tentatively (parser);
19328 tree attr = cp_parser_gnu_attributes_opt (parser);
19329 if (cp_lexer_next_token_is_not (parser->lexer,
19330 CPP_SEMICOLON)
19331 && cp_lexer_next_token_is_not (parser->lexer,
19332 CPP_OPEN_BRACE))
19333 cp_parser_abort_tentative_parse (parser);
19334 else if (!cp_parser_parse_definitely (parser))
19336 else
19337 gnu_attrs = attr;
19339 tree requires_clause = NULL_TREE;
19340 late_return = (cp_parser_late_return_type_opt
19341 (parser, declarator, requires_clause,
19342 memfn ? cv_quals : -1));
19344 /* Parse the virt-specifier-seq. */
19345 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19347 /* Create the function-declarator. */
19348 declarator = make_call_declarator (declarator,
19349 params,
19350 cv_quals,
19351 virt_specifiers,
19352 ref_qual,
19353 tx_qual,
19354 exception_specification,
19355 late_return,
19356 requires_clause);
19357 declarator->std_attributes = attrs;
19358 declarator->attributes = gnu_attrs;
19359 /* Any subsequent parameter lists are to do with
19360 return type, so are not those of the declared
19361 function. */
19362 parser->default_arg_ok_p = false;
19365 /* Remove the function parms from scope. */
19366 pop_bindings_and_leave_scope ();
19368 if (is_declarator)
19369 /* Repeat the main loop. */
19370 continue;
19373 /* If this is the first, we can try a parenthesized
19374 declarator. */
19375 if (first)
19377 bool saved_in_type_id_in_expr_p;
19379 parser->default_arg_ok_p = saved_default_arg_ok_p;
19380 parser->in_declarator_p = saved_in_declarator_p;
19382 /* Consume the `('. */
19383 cp_lexer_consume_token (parser->lexer);
19384 /* Parse the nested declarator. */
19385 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19386 parser->in_type_id_in_expr_p = true;
19387 declarator
19388 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19389 /*parenthesized_p=*/NULL,
19390 member_p, friend_p);
19391 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19392 first = false;
19393 /* Expect a `)'. */
19394 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19395 declarator = cp_error_declarator;
19396 if (declarator == cp_error_declarator)
19397 break;
19399 goto handle_declarator;
19401 /* Otherwise, we must be done. */
19402 else
19403 break;
19405 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19406 && token->type == CPP_OPEN_SQUARE
19407 && !cp_next_tokens_can_be_attribute_p (parser))
19409 /* Parse an array-declarator. */
19410 tree bounds, attrs;
19412 if (ctor_dtor_or_conv_p)
19413 *ctor_dtor_or_conv_p = 0;
19415 first = false;
19416 parser->default_arg_ok_p = false;
19417 parser->in_declarator_p = true;
19418 /* Consume the `['. */
19419 cp_lexer_consume_token (parser->lexer);
19420 /* Peek at the next token. */
19421 token = cp_lexer_peek_token (parser->lexer);
19422 /* If the next token is `]', then there is no
19423 constant-expression. */
19424 if (token->type != CPP_CLOSE_SQUARE)
19426 bool non_constant_p;
19427 bounds
19428 = cp_parser_constant_expression (parser,
19429 /*allow_non_constant=*/true,
19430 &non_constant_p);
19431 if (!non_constant_p)
19432 /* OK */;
19433 else if (error_operand_p (bounds))
19434 /* Already gave an error. */;
19435 else if (!parser->in_function_body
19436 || current_binding_level->kind == sk_function_parms)
19438 /* Normally, the array bound must be an integral constant
19439 expression. However, as an extension, we allow VLAs
19440 in function scopes as long as they aren't part of a
19441 parameter declaration. */
19442 cp_parser_error (parser,
19443 "array bound is not an integer constant");
19444 bounds = error_mark_node;
19446 else if (processing_template_decl
19447 && !type_dependent_expression_p (bounds))
19449 /* Remember this wasn't a constant-expression. */
19450 bounds = build_nop (TREE_TYPE (bounds), bounds);
19451 TREE_SIDE_EFFECTS (bounds) = 1;
19454 else
19455 bounds = NULL_TREE;
19456 /* Look for the closing `]'. */
19457 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19459 declarator = cp_error_declarator;
19460 break;
19463 attrs = cp_parser_std_attribute_spec_seq (parser);
19464 declarator = make_array_declarator (declarator, bounds);
19465 declarator->std_attributes = attrs;
19467 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19470 tree qualifying_scope;
19471 tree unqualified_name;
19472 tree attrs;
19473 special_function_kind sfk;
19474 bool abstract_ok;
19475 bool pack_expansion_p = false;
19476 cp_token *declarator_id_start_token;
19478 /* Parse a declarator-id */
19479 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19480 if (abstract_ok)
19482 cp_parser_parse_tentatively (parser);
19484 /* If we see an ellipsis, we should be looking at a
19485 parameter pack. */
19486 if (token->type == CPP_ELLIPSIS)
19488 /* Consume the `...' */
19489 cp_lexer_consume_token (parser->lexer);
19491 pack_expansion_p = true;
19495 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19496 unqualified_name
19497 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19498 qualifying_scope = parser->scope;
19499 if (abstract_ok)
19501 bool okay = false;
19503 if (!unqualified_name && pack_expansion_p)
19505 /* Check whether an error occurred. */
19506 okay = !cp_parser_error_occurred (parser);
19508 /* We already consumed the ellipsis to mark a
19509 parameter pack, but we have no way to report it,
19510 so abort the tentative parse. We will be exiting
19511 immediately anyway. */
19512 cp_parser_abort_tentative_parse (parser);
19514 else
19515 okay = cp_parser_parse_definitely (parser);
19517 if (!okay)
19518 unqualified_name = error_mark_node;
19519 else if (unqualified_name
19520 && (qualifying_scope
19521 || (!identifier_p (unqualified_name))))
19523 cp_parser_error (parser, "expected unqualified-id");
19524 unqualified_name = error_mark_node;
19528 if (!unqualified_name)
19529 return NULL;
19530 if (unqualified_name == error_mark_node)
19532 declarator = cp_error_declarator;
19533 pack_expansion_p = false;
19534 declarator->parameter_pack_p = false;
19535 break;
19538 attrs = cp_parser_std_attribute_spec_seq (parser);
19540 if (qualifying_scope && at_namespace_scope_p ()
19541 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19543 /* In the declaration of a member of a template class
19544 outside of the class itself, the SCOPE will sometimes
19545 be a TYPENAME_TYPE. For example, given:
19547 template <typename T>
19548 int S<T>::R::i = 3;
19550 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19551 this context, we must resolve S<T>::R to an ordinary
19552 type, rather than a typename type.
19554 The reason we normally avoid resolving TYPENAME_TYPEs
19555 is that a specialization of `S' might render
19556 `S<T>::R' not a type. However, if `S' is
19557 specialized, then this `i' will not be used, so there
19558 is no harm in resolving the types here. */
19559 tree type;
19561 /* Resolve the TYPENAME_TYPE. */
19562 type = resolve_typename_type (qualifying_scope,
19563 /*only_current_p=*/false);
19564 /* If that failed, the declarator is invalid. */
19565 if (TREE_CODE (type) == TYPENAME_TYPE)
19567 if (typedef_variant_p (type))
19568 error_at (declarator_id_start_token->location,
19569 "cannot define member of dependent typedef "
19570 "%qT", type);
19571 else
19572 error_at (declarator_id_start_token->location,
19573 "%<%T::%E%> is not a type",
19574 TYPE_CONTEXT (qualifying_scope),
19575 TYPE_IDENTIFIER (qualifying_scope));
19577 qualifying_scope = type;
19580 sfk = sfk_none;
19582 if (unqualified_name)
19584 tree class_type;
19586 if (qualifying_scope
19587 && CLASS_TYPE_P (qualifying_scope))
19588 class_type = qualifying_scope;
19589 else
19590 class_type = current_class_type;
19592 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19594 tree name_type = TREE_TYPE (unqualified_name);
19595 if (class_type && same_type_p (name_type, class_type))
19597 if (qualifying_scope
19598 && CLASSTYPE_USE_TEMPLATE (name_type))
19600 error_at (declarator_id_start_token->location,
19601 "invalid use of constructor as a template");
19602 inform (declarator_id_start_token->location,
19603 "use %<%T::%D%> instead of %<%T::%D%> to "
19604 "name the constructor in a qualified name",
19605 class_type,
19606 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19607 class_type, name_type);
19608 declarator = cp_error_declarator;
19609 break;
19611 else
19612 unqualified_name = constructor_name (class_type);
19614 else
19616 /* We do not attempt to print the declarator
19617 here because we do not have enough
19618 information about its original syntactic
19619 form. */
19620 cp_parser_error (parser, "invalid declarator");
19621 declarator = cp_error_declarator;
19622 break;
19626 if (class_type)
19628 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19629 sfk = sfk_destructor;
19630 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19631 sfk = sfk_conversion;
19632 else if (/* There's no way to declare a constructor
19633 for an unnamed type, even if the type
19634 got a name for linkage purposes. */
19635 !TYPE_WAS_UNNAMED (class_type)
19636 /* Handle correctly (c++/19200):
19638 struct S {
19639 struct T{};
19640 friend void S(T);
19643 and also:
19645 namespace N {
19646 void S();
19649 struct S {
19650 friend void N::S();
19651 }; */
19652 && !(friend_p
19653 && class_type != qualifying_scope)
19654 && constructor_name_p (unqualified_name,
19655 class_type))
19657 unqualified_name = constructor_name (class_type);
19658 sfk = sfk_constructor;
19660 else if (is_overloaded_fn (unqualified_name)
19661 && DECL_CONSTRUCTOR_P (get_first_fn
19662 (unqualified_name)))
19663 sfk = sfk_constructor;
19665 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19666 *ctor_dtor_or_conv_p = -1;
19669 declarator = make_id_declarator (qualifying_scope,
19670 unqualified_name,
19671 sfk);
19672 declarator->std_attributes = attrs;
19673 declarator->id_loc = token->location;
19674 declarator->parameter_pack_p = pack_expansion_p;
19676 if (pack_expansion_p)
19677 maybe_warn_variadic_templates ();
19680 handle_declarator:;
19681 scope = get_scope_of_declarator (declarator);
19682 if (scope)
19684 /* Any names that appear after the declarator-id for a
19685 member are looked up in the containing scope. */
19686 if (at_function_scope_p ())
19688 /* But declarations with qualified-ids can't appear in a
19689 function. */
19690 cp_parser_error (parser, "qualified-id in declaration");
19691 declarator = cp_error_declarator;
19692 break;
19694 pushed_scope = push_scope (scope);
19696 parser->in_declarator_p = true;
19697 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19698 || (declarator && declarator->kind == cdk_id))
19699 /* Default args are only allowed on function
19700 declarations. */
19701 parser->default_arg_ok_p = saved_default_arg_ok_p;
19702 else
19703 parser->default_arg_ok_p = false;
19705 first = false;
19707 /* We're done. */
19708 else
19709 break;
19712 /* For an abstract declarator, we might wind up with nothing at this
19713 point. That's an error; the declarator is not optional. */
19714 if (!declarator)
19715 cp_parser_error (parser, "expected declarator");
19717 /* If we entered a scope, we must exit it now. */
19718 if (pushed_scope)
19719 pop_scope (pushed_scope);
19721 parser->default_arg_ok_p = saved_default_arg_ok_p;
19722 parser->in_declarator_p = saved_in_declarator_p;
19724 return declarator;
19727 /* Parse a ptr-operator.
19729 ptr-operator:
19730 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19731 * cv-qualifier-seq [opt]
19733 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19734 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19736 GNU Extension:
19738 ptr-operator:
19739 & cv-qualifier-seq [opt]
19741 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19742 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19743 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19744 filled in with the TYPE containing the member. *CV_QUALS is
19745 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19746 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19747 Note that the tree codes returned by this function have nothing
19748 to do with the types of trees that will be eventually be created
19749 to represent the pointer or reference type being parsed. They are
19750 just constants with suggestive names. */
19751 static enum tree_code
19752 cp_parser_ptr_operator (cp_parser* parser,
19753 tree* type,
19754 cp_cv_quals *cv_quals,
19755 tree *attributes)
19757 enum tree_code code = ERROR_MARK;
19758 cp_token *token;
19759 tree attrs = NULL_TREE;
19761 /* Assume that it's not a pointer-to-member. */
19762 *type = NULL_TREE;
19763 /* And that there are no cv-qualifiers. */
19764 *cv_quals = TYPE_UNQUALIFIED;
19766 /* Peek at the next token. */
19767 token = cp_lexer_peek_token (parser->lexer);
19769 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19770 if (token->type == CPP_MULT)
19771 code = INDIRECT_REF;
19772 else if (token->type == CPP_AND)
19773 code = ADDR_EXPR;
19774 else if ((cxx_dialect != cxx98) &&
19775 token->type == CPP_AND_AND) /* C++0x only */
19776 code = NON_LVALUE_EXPR;
19778 if (code != ERROR_MARK)
19780 /* Consume the `*', `&' or `&&'. */
19781 cp_lexer_consume_token (parser->lexer);
19783 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19784 `&', if we are allowing GNU extensions. (The only qualifier
19785 that can legally appear after `&' is `restrict', but that is
19786 enforced during semantic analysis. */
19787 if (code == INDIRECT_REF
19788 || cp_parser_allow_gnu_extensions_p (parser))
19789 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19791 attrs = cp_parser_std_attribute_spec_seq (parser);
19792 if (attributes != NULL)
19793 *attributes = attrs;
19795 else
19797 /* Try the pointer-to-member case. */
19798 cp_parser_parse_tentatively (parser);
19799 /* Look for the optional `::' operator. */
19800 cp_parser_global_scope_opt (parser,
19801 /*current_scope_valid_p=*/false);
19802 /* Look for the nested-name specifier. */
19803 token = cp_lexer_peek_token (parser->lexer);
19804 cp_parser_nested_name_specifier (parser,
19805 /*typename_keyword_p=*/false,
19806 /*check_dependency_p=*/true,
19807 /*type_p=*/false,
19808 /*is_declaration=*/false);
19809 /* If we found it, and the next token is a `*', then we are
19810 indeed looking at a pointer-to-member operator. */
19811 if (!cp_parser_error_occurred (parser)
19812 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19814 /* Indicate that the `*' operator was used. */
19815 code = INDIRECT_REF;
19817 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19818 error_at (token->location, "%qD is a namespace", parser->scope);
19819 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19820 error_at (token->location, "cannot form pointer to member of "
19821 "non-class %q#T", parser->scope);
19822 else
19824 /* The type of which the member is a member is given by the
19825 current SCOPE. */
19826 *type = parser->scope;
19827 /* The next name will not be qualified. */
19828 parser->scope = NULL_TREE;
19829 parser->qualifying_scope = NULL_TREE;
19830 parser->object_scope = NULL_TREE;
19831 /* Look for optional c++11 attributes. */
19832 attrs = cp_parser_std_attribute_spec_seq (parser);
19833 if (attributes != NULL)
19834 *attributes = attrs;
19835 /* Look for the optional cv-qualifier-seq. */
19836 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19839 /* If that didn't work we don't have a ptr-operator. */
19840 if (!cp_parser_parse_definitely (parser))
19841 cp_parser_error (parser, "expected ptr-operator");
19844 return code;
19847 /* Parse an (optional) cv-qualifier-seq.
19849 cv-qualifier-seq:
19850 cv-qualifier cv-qualifier-seq [opt]
19852 cv-qualifier:
19853 const
19854 volatile
19856 GNU Extension:
19858 cv-qualifier:
19859 __restrict__
19861 Returns a bitmask representing the cv-qualifiers. */
19863 static cp_cv_quals
19864 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19866 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19868 while (true)
19870 cp_token *token;
19871 cp_cv_quals cv_qualifier;
19873 /* Peek at the next token. */
19874 token = cp_lexer_peek_token (parser->lexer);
19875 /* See if it's a cv-qualifier. */
19876 switch (token->keyword)
19878 case RID_CONST:
19879 cv_qualifier = TYPE_QUAL_CONST;
19880 break;
19882 case RID_VOLATILE:
19883 cv_qualifier = TYPE_QUAL_VOLATILE;
19884 break;
19886 case RID_RESTRICT:
19887 cv_qualifier = TYPE_QUAL_RESTRICT;
19888 break;
19890 default:
19891 cv_qualifier = TYPE_UNQUALIFIED;
19892 break;
19895 if (!cv_qualifier)
19896 break;
19898 if (cv_quals & cv_qualifier)
19900 error_at (token->location, "duplicate cv-qualifier");
19901 cp_lexer_purge_token (parser->lexer);
19903 else
19905 cp_lexer_consume_token (parser->lexer);
19906 cv_quals |= cv_qualifier;
19910 return cv_quals;
19913 /* Parse an (optional) ref-qualifier
19915 ref-qualifier:
19919 Returns cp_ref_qualifier representing ref-qualifier. */
19921 static cp_ref_qualifier
19922 cp_parser_ref_qualifier_opt (cp_parser* parser)
19924 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19926 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19927 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19928 return ref_qual;
19930 while (true)
19932 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19933 cp_token *token = cp_lexer_peek_token (parser->lexer);
19935 switch (token->type)
19937 case CPP_AND:
19938 curr_ref_qual = REF_QUAL_LVALUE;
19939 break;
19941 case CPP_AND_AND:
19942 curr_ref_qual = REF_QUAL_RVALUE;
19943 break;
19945 default:
19946 curr_ref_qual = REF_QUAL_NONE;
19947 break;
19950 if (!curr_ref_qual)
19951 break;
19952 else if (ref_qual)
19954 error_at (token->location, "multiple ref-qualifiers");
19955 cp_lexer_purge_token (parser->lexer);
19957 else
19959 ref_qual = curr_ref_qual;
19960 cp_lexer_consume_token (parser->lexer);
19964 return ref_qual;
19967 /* Parse an optional tx-qualifier.
19969 tx-qualifier:
19970 transaction_safe
19971 transaction_safe_dynamic */
19973 static tree
19974 cp_parser_tx_qualifier_opt (cp_parser *parser)
19976 cp_token *token = cp_lexer_peek_token (parser->lexer);
19977 if (token->type == CPP_NAME)
19979 tree name = token->u.value;
19980 const char *p = IDENTIFIER_POINTER (name);
19981 const int len = strlen ("transaction_safe");
19982 if (!strncmp (p, "transaction_safe", len))
19984 p += len;
19985 if (*p == '\0'
19986 || !strcmp (p, "_dynamic"))
19988 cp_lexer_consume_token (parser->lexer);
19989 if (!flag_tm)
19991 error ("%E requires %<-fgnu-tm%>", name);
19992 return NULL_TREE;
19994 else
19995 return name;
19999 return NULL_TREE;
20002 /* Parse an (optional) virt-specifier-seq.
20004 virt-specifier-seq:
20005 virt-specifier virt-specifier-seq [opt]
20007 virt-specifier:
20008 override
20009 final
20011 Returns a bitmask representing the virt-specifiers. */
20013 static cp_virt_specifiers
20014 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20016 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20018 while (true)
20020 cp_token *token;
20021 cp_virt_specifiers virt_specifier;
20023 /* Peek at the next token. */
20024 token = cp_lexer_peek_token (parser->lexer);
20025 /* See if it's a virt-specifier-qualifier. */
20026 if (token->type != CPP_NAME)
20027 break;
20028 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
20030 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20031 virt_specifier = VIRT_SPEC_OVERRIDE;
20033 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
20035 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20036 virt_specifier = VIRT_SPEC_FINAL;
20038 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
20040 virt_specifier = VIRT_SPEC_FINAL;
20042 else
20043 break;
20045 if (virt_specifiers & virt_specifier)
20047 error_at (token->location, "duplicate virt-specifier");
20048 cp_lexer_purge_token (parser->lexer);
20050 else
20052 cp_lexer_consume_token (parser->lexer);
20053 virt_specifiers |= virt_specifier;
20056 return virt_specifiers;
20059 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20060 is in scope even though it isn't real. */
20062 void
20063 inject_this_parameter (tree ctype, cp_cv_quals quals)
20065 tree this_parm;
20067 if (current_class_ptr)
20069 /* We don't clear this between NSDMIs. Is it already what we want? */
20070 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20071 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20072 && cp_type_quals (type) == quals)
20073 return;
20076 this_parm = build_this_parm (ctype, quals);
20077 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20078 current_class_ptr = NULL_TREE;
20079 current_class_ref
20080 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20081 current_class_ptr = this_parm;
20084 /* Return true iff our current scope is a non-static data member
20085 initializer. */
20087 bool
20088 parsing_nsdmi (void)
20090 /* We recognize NSDMI context by the context-less 'this' pointer set up
20091 by the function above. */
20092 if (current_class_ptr
20093 && TREE_CODE (current_class_ptr) == PARM_DECL
20094 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20095 return true;
20096 return false;
20099 /* Parse a late-specified return type, if any. This is not a separate
20100 non-terminal, but part of a function declarator, which looks like
20102 -> trailing-type-specifier-seq abstract-declarator(opt)
20104 Returns the type indicated by the type-id.
20106 In addition to this, parse any queued up #pragma omp declare simd
20107 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20108 #pragma acc routine clauses.
20110 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20111 function. */
20113 static tree
20114 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20115 tree& requires_clause, cp_cv_quals quals)
20117 cp_token *token;
20118 tree type = NULL_TREE;
20119 bool declare_simd_p = (parser->omp_declare_simd
20120 && declarator
20121 && declarator->kind == cdk_id);
20123 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20124 && declarator && declarator->kind == cdk_id);
20126 bool oacc_routine_p = (parser->oacc_routine
20127 && declarator
20128 && declarator->kind == cdk_id);
20130 /* Peek at the next token. */
20131 token = cp_lexer_peek_token (parser->lexer);
20132 /* A late-specified return type is indicated by an initial '->'. */
20133 if (token->type != CPP_DEREF
20134 && token->keyword != RID_REQUIRES
20135 && !(token->type == CPP_NAME
20136 && token->u.value == ridpointers[RID_REQUIRES])
20137 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20138 return NULL_TREE;
20140 tree save_ccp = current_class_ptr;
20141 tree save_ccr = current_class_ref;
20142 if (quals >= 0)
20144 /* DR 1207: 'this' is in scope in the trailing return type. */
20145 inject_this_parameter (current_class_type, quals);
20148 if (token->type == CPP_DEREF)
20150 /* Consume the ->. */
20151 cp_lexer_consume_token (parser->lexer);
20153 type = cp_parser_trailing_type_id (parser);
20156 /* Function declarations may be followed by a trailing
20157 requires-clause. */
20158 requires_clause = cp_parser_requires_clause_opt (parser);
20160 if (cilk_simd_fn_vector_p)
20161 declarator->attributes
20162 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20163 declarator->attributes);
20164 if (declare_simd_p)
20165 declarator->attributes
20166 = cp_parser_late_parsing_omp_declare_simd (parser,
20167 declarator->attributes);
20168 if (oacc_routine_p)
20169 declarator->attributes
20170 = cp_parser_late_parsing_oacc_routine (parser,
20171 declarator->attributes);
20173 if (quals >= 0)
20175 current_class_ptr = save_ccp;
20176 current_class_ref = save_ccr;
20179 return type;
20182 /* Parse a declarator-id.
20184 declarator-id:
20185 id-expression
20186 :: [opt] nested-name-specifier [opt] type-name
20188 In the `id-expression' case, the value returned is as for
20189 cp_parser_id_expression if the id-expression was an unqualified-id.
20190 If the id-expression was a qualified-id, then a SCOPE_REF is
20191 returned. The first operand is the scope (either a NAMESPACE_DECL
20192 or TREE_TYPE), but the second is still just a representation of an
20193 unqualified-id. */
20195 static tree
20196 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20198 tree id;
20199 /* The expression must be an id-expression. Assume that qualified
20200 names are the names of types so that:
20202 template <class T>
20203 int S<T>::R::i = 3;
20205 will work; we must treat `S<T>::R' as the name of a type.
20206 Similarly, assume that qualified names are templates, where
20207 required, so that:
20209 template <class T>
20210 int S<T>::R<T>::i = 3;
20212 will work, too. */
20213 id = cp_parser_id_expression (parser,
20214 /*template_keyword_p=*/false,
20215 /*check_dependency_p=*/false,
20216 /*template_p=*/NULL,
20217 /*declarator_p=*/true,
20218 optional_p);
20219 if (id && BASELINK_P (id))
20220 id = BASELINK_FUNCTIONS (id);
20221 return id;
20224 /* Parse a type-id.
20226 type-id:
20227 type-specifier-seq abstract-declarator [opt]
20229 Returns the TYPE specified. */
20231 static tree
20232 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20233 bool is_trailing_return)
20235 cp_decl_specifier_seq type_specifier_seq;
20236 cp_declarator *abstract_declarator;
20238 /* Parse the type-specifier-seq. */
20239 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20240 is_trailing_return,
20241 &type_specifier_seq);
20242 if (is_template_arg && type_specifier_seq.type
20243 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20244 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20245 /* A bare template name as a template argument is a template template
20246 argument, not a placeholder, so fail parsing it as a type argument. */
20248 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20249 cp_parser_simulate_error (parser);
20250 return error_mark_node;
20252 if (type_specifier_seq.type == error_mark_node)
20253 return error_mark_node;
20255 /* There might or might not be an abstract declarator. */
20256 cp_parser_parse_tentatively (parser);
20257 /* Look for the declarator. */
20258 abstract_declarator
20259 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20260 /*parenthesized_p=*/NULL,
20261 /*member_p=*/false,
20262 /*friend_p=*/false);
20263 /* Check to see if there really was a declarator. */
20264 if (!cp_parser_parse_definitely (parser))
20265 abstract_declarator = NULL;
20267 if (type_specifier_seq.type
20268 /* The concepts TS allows 'auto' as a type-id. */
20269 && (!flag_concepts || parser->in_type_id_in_expr_p)
20270 /* None of the valid uses of 'auto' in C++14 involve the type-id
20271 nonterminal, but it is valid in a trailing-return-type. */
20272 && !(cxx_dialect >= cxx14 && is_trailing_return)
20273 && type_uses_auto (type_specifier_seq.type))
20275 /* A type-id with type 'auto' is only ok if the abstract declarator
20276 is a function declarator with a late-specified return type.
20278 A type-id with 'auto' is also valid in a trailing-return-type
20279 in a compound-requirement. */
20280 if (abstract_declarator
20281 && abstract_declarator->kind == cdk_function
20282 && abstract_declarator->u.function.late_return_type)
20283 /* OK */;
20284 else if (parser->in_result_type_constraint_p)
20285 /* OK */;
20286 else
20288 error ("invalid use of %<auto%>");
20289 return error_mark_node;
20293 return groktypename (&type_specifier_seq, abstract_declarator,
20294 is_template_arg);
20297 static tree
20298 cp_parser_type_id (cp_parser *parser)
20300 return cp_parser_type_id_1 (parser, false, false);
20303 static tree
20304 cp_parser_template_type_arg (cp_parser *parser)
20306 tree r;
20307 const char *saved_message = parser->type_definition_forbidden_message;
20308 parser->type_definition_forbidden_message
20309 = G_("types may not be defined in template arguments");
20310 r = cp_parser_type_id_1 (parser, true, false);
20311 parser->type_definition_forbidden_message = saved_message;
20312 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20314 error ("invalid use of %<auto%> in template argument");
20315 r = error_mark_node;
20317 return r;
20320 static tree
20321 cp_parser_trailing_type_id (cp_parser *parser)
20323 return cp_parser_type_id_1 (parser, false, true);
20326 /* Parse a type-specifier-seq.
20328 type-specifier-seq:
20329 type-specifier type-specifier-seq [opt]
20331 GNU extension:
20333 type-specifier-seq:
20334 attributes type-specifier-seq [opt]
20336 If IS_DECLARATION is true, we are at the start of a "condition" or
20337 exception-declaration, so we might be followed by a declarator-id.
20339 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20340 i.e. we've just seen "->".
20342 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20344 static void
20345 cp_parser_type_specifier_seq (cp_parser* parser,
20346 bool is_declaration,
20347 bool is_trailing_return,
20348 cp_decl_specifier_seq *type_specifier_seq)
20350 bool seen_type_specifier = false;
20351 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20352 cp_token *start_token = NULL;
20354 /* Clear the TYPE_SPECIFIER_SEQ. */
20355 clear_decl_specs (type_specifier_seq);
20357 /* In the context of a trailing return type, enum E { } is an
20358 elaborated-type-specifier followed by a function-body, not an
20359 enum-specifier. */
20360 if (is_trailing_return)
20361 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20363 /* Parse the type-specifiers and attributes. */
20364 while (true)
20366 tree type_specifier;
20367 bool is_cv_qualifier;
20369 /* Check for attributes first. */
20370 if (cp_next_tokens_can_be_attribute_p (parser))
20372 type_specifier_seq->attributes =
20373 chainon (type_specifier_seq->attributes,
20374 cp_parser_attributes_opt (parser));
20375 continue;
20378 /* record the token of the beginning of the type specifier seq,
20379 for error reporting purposes*/
20380 if (!start_token)
20381 start_token = cp_lexer_peek_token (parser->lexer);
20383 /* Look for the type-specifier. */
20384 type_specifier = cp_parser_type_specifier (parser,
20385 flags,
20386 type_specifier_seq,
20387 /*is_declaration=*/false,
20388 NULL,
20389 &is_cv_qualifier);
20390 if (!type_specifier)
20392 /* If the first type-specifier could not be found, this is not a
20393 type-specifier-seq at all. */
20394 if (!seen_type_specifier)
20396 /* Set in_declarator_p to avoid skipping to the semicolon. */
20397 int in_decl = parser->in_declarator_p;
20398 parser->in_declarator_p = true;
20400 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20401 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20402 cp_parser_error (parser, "expected type-specifier");
20404 parser->in_declarator_p = in_decl;
20406 type_specifier_seq->type = error_mark_node;
20407 return;
20409 /* If subsequent type-specifiers could not be found, the
20410 type-specifier-seq is complete. */
20411 break;
20414 seen_type_specifier = true;
20415 /* The standard says that a condition can be:
20417 type-specifier-seq declarator = assignment-expression
20419 However, given:
20421 struct S {};
20422 if (int S = ...)
20424 we should treat the "S" as a declarator, not as a
20425 type-specifier. The standard doesn't say that explicitly for
20426 type-specifier-seq, but it does say that for
20427 decl-specifier-seq in an ordinary declaration. Perhaps it
20428 would be clearer just to allow a decl-specifier-seq here, and
20429 then add a semantic restriction that if any decl-specifiers
20430 that are not type-specifiers appear, the program is invalid. */
20431 if (is_declaration && !is_cv_qualifier)
20432 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20436 /* Return whether the function currently being declared has an associated
20437 template parameter list. */
20439 static bool
20440 function_being_declared_is_template_p (cp_parser* parser)
20442 if (!current_template_parms || processing_template_parmlist)
20443 return false;
20445 if (parser->implicit_template_scope)
20446 return true;
20448 if (at_class_scope_p ()
20449 && TYPE_BEING_DEFINED (current_class_type))
20450 return parser->num_template_parameter_lists != 0;
20452 return ((int) parser->num_template_parameter_lists > template_class_depth
20453 (current_class_type));
20456 /* Parse a parameter-declaration-clause.
20458 parameter-declaration-clause:
20459 parameter-declaration-list [opt] ... [opt]
20460 parameter-declaration-list , ...
20462 Returns a representation for the parameter declarations. A return
20463 value of NULL indicates a parameter-declaration-clause consisting
20464 only of an ellipsis. */
20466 static tree
20467 cp_parser_parameter_declaration_clause (cp_parser* parser)
20469 tree parameters;
20470 cp_token *token;
20471 bool ellipsis_p;
20472 bool is_error;
20474 struct cleanup {
20475 cp_parser* parser;
20476 int auto_is_implicit_function_template_parm_p;
20477 ~cleanup() {
20478 parser->auto_is_implicit_function_template_parm_p
20479 = auto_is_implicit_function_template_parm_p;
20481 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20483 (void) cleanup;
20485 if (!processing_specialization
20486 && !processing_template_parmlist
20487 && !processing_explicit_instantiation)
20488 if (!current_function_decl
20489 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20490 parser->auto_is_implicit_function_template_parm_p = true;
20492 /* Peek at the next token. */
20493 token = cp_lexer_peek_token (parser->lexer);
20494 /* Check for trivial parameter-declaration-clauses. */
20495 if (token->type == CPP_ELLIPSIS)
20497 /* Consume the `...' token. */
20498 cp_lexer_consume_token (parser->lexer);
20499 return NULL_TREE;
20501 else if (token->type == CPP_CLOSE_PAREN)
20502 /* There are no parameters. */
20504 #ifndef NO_IMPLICIT_EXTERN_C
20505 if (in_system_header_at (input_location)
20506 && current_class_type == NULL
20507 && current_lang_name == lang_name_c)
20508 return NULL_TREE;
20509 else
20510 #endif
20511 return void_list_node;
20513 /* Check for `(void)', too, which is a special case. */
20514 else if (token->keyword == RID_VOID
20515 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20516 == CPP_CLOSE_PAREN))
20518 /* Consume the `void' token. */
20519 cp_lexer_consume_token (parser->lexer);
20520 /* There are no parameters. */
20521 return void_list_node;
20524 /* Parse the parameter-declaration-list. */
20525 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20526 /* If a parse error occurred while parsing the
20527 parameter-declaration-list, then the entire
20528 parameter-declaration-clause is erroneous. */
20529 if (is_error)
20530 return NULL;
20532 /* Peek at the next token. */
20533 token = cp_lexer_peek_token (parser->lexer);
20534 /* If it's a `,', the clause should terminate with an ellipsis. */
20535 if (token->type == CPP_COMMA)
20537 /* Consume the `,'. */
20538 cp_lexer_consume_token (parser->lexer);
20539 /* Expect an ellipsis. */
20540 ellipsis_p
20541 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20543 /* It might also be `...' if the optional trailing `,' was
20544 omitted. */
20545 else if (token->type == CPP_ELLIPSIS)
20547 /* Consume the `...' token. */
20548 cp_lexer_consume_token (parser->lexer);
20549 /* And remember that we saw it. */
20550 ellipsis_p = true;
20552 else
20553 ellipsis_p = false;
20555 /* Finish the parameter list. */
20556 if (!ellipsis_p)
20557 parameters = chainon (parameters, void_list_node);
20559 return parameters;
20562 /* Parse a parameter-declaration-list.
20564 parameter-declaration-list:
20565 parameter-declaration
20566 parameter-declaration-list , parameter-declaration
20568 Returns a representation of the parameter-declaration-list, as for
20569 cp_parser_parameter_declaration_clause. However, the
20570 `void_list_node' is never appended to the list. Upon return,
20571 *IS_ERROR will be true iff an error occurred. */
20573 static tree
20574 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20576 tree parameters = NULL_TREE;
20577 tree *tail = &parameters;
20578 bool saved_in_unbraced_linkage_specification_p;
20579 int index = 0;
20581 /* Assume all will go well. */
20582 *is_error = false;
20583 /* The special considerations that apply to a function within an
20584 unbraced linkage specifications do not apply to the parameters
20585 to the function. */
20586 saved_in_unbraced_linkage_specification_p
20587 = parser->in_unbraced_linkage_specification_p;
20588 parser->in_unbraced_linkage_specification_p = false;
20590 /* Look for more parameters. */
20591 while (true)
20593 cp_parameter_declarator *parameter;
20594 tree decl = error_mark_node;
20595 bool parenthesized_p = false;
20596 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20597 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20598 (current_template_parms)) : 0);
20600 /* Parse the parameter. */
20601 parameter
20602 = cp_parser_parameter_declaration (parser,
20603 /*template_parm_p=*/false,
20604 &parenthesized_p);
20606 /* We don't know yet if the enclosing context is deprecated, so wait
20607 and warn in grokparms if appropriate. */
20608 deprecated_state = DEPRECATED_SUPPRESS;
20610 if (parameter)
20612 /* If a function parameter pack was specified and an implicit template
20613 parameter was introduced during cp_parser_parameter_declaration,
20614 change any implicit parameters introduced into packs. */
20615 if (parser->implicit_template_parms
20616 && parameter->declarator
20617 && parameter->declarator->parameter_pack_p)
20619 int latest_template_parm_idx = TREE_VEC_LENGTH
20620 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20622 if (latest_template_parm_idx != template_parm_idx)
20623 parameter->decl_specifiers.type = convert_generic_types_to_packs
20624 (parameter->decl_specifiers.type,
20625 template_parm_idx, latest_template_parm_idx);
20628 decl = grokdeclarator (parameter->declarator,
20629 &parameter->decl_specifiers,
20630 PARM,
20631 parameter->default_argument != NULL_TREE,
20632 &parameter->decl_specifiers.attributes);
20635 deprecated_state = DEPRECATED_NORMAL;
20637 /* If a parse error occurred parsing the parameter declaration,
20638 then the entire parameter-declaration-list is erroneous. */
20639 if (decl == error_mark_node)
20641 *is_error = true;
20642 parameters = error_mark_node;
20643 break;
20646 if (parameter->decl_specifiers.attributes)
20647 cplus_decl_attributes (&decl,
20648 parameter->decl_specifiers.attributes,
20650 if (DECL_NAME (decl))
20651 decl = pushdecl (decl);
20653 if (decl != error_mark_node)
20655 retrofit_lang_decl (decl);
20656 DECL_PARM_INDEX (decl) = ++index;
20657 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20660 /* Add the new parameter to the list. */
20661 *tail = build_tree_list (parameter->default_argument, decl);
20662 tail = &TREE_CHAIN (*tail);
20664 /* Peek at the next token. */
20665 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20666 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20667 /* These are for Objective-C++ */
20668 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20669 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20670 /* The parameter-declaration-list is complete. */
20671 break;
20672 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20674 cp_token *token;
20676 /* Peek at the next token. */
20677 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20678 /* If it's an ellipsis, then the list is complete. */
20679 if (token->type == CPP_ELLIPSIS)
20680 break;
20681 /* Otherwise, there must be more parameters. Consume the
20682 `,'. */
20683 cp_lexer_consume_token (parser->lexer);
20684 /* When parsing something like:
20686 int i(float f, double d)
20688 we can tell after seeing the declaration for "f" that we
20689 are not looking at an initialization of a variable "i",
20690 but rather at the declaration of a function "i".
20692 Due to the fact that the parsing of template arguments
20693 (as specified to a template-id) requires backtracking we
20694 cannot use this technique when inside a template argument
20695 list. */
20696 if (!parser->in_template_argument_list_p
20697 && !parser->in_type_id_in_expr_p
20698 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20699 /* However, a parameter-declaration of the form
20700 "float(f)" (which is a valid declaration of a
20701 parameter "f") can also be interpreted as an
20702 expression (the conversion of "f" to "float"). */
20703 && !parenthesized_p)
20704 cp_parser_commit_to_tentative_parse (parser);
20706 else
20708 cp_parser_error (parser, "expected %<,%> or %<...%>");
20709 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20710 cp_parser_skip_to_closing_parenthesis (parser,
20711 /*recovering=*/true,
20712 /*or_comma=*/false,
20713 /*consume_paren=*/false);
20714 break;
20718 parser->in_unbraced_linkage_specification_p
20719 = saved_in_unbraced_linkage_specification_p;
20721 /* Reset implicit_template_scope if we are about to leave the function
20722 parameter list that introduced it. Note that for out-of-line member
20723 definitions, there will be one or more class scopes before we get to
20724 the template parameter scope. */
20726 if (cp_binding_level *its = parser->implicit_template_scope)
20727 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20729 while (maybe_its->kind == sk_class)
20730 maybe_its = maybe_its->level_chain;
20731 if (maybe_its == its)
20733 parser->implicit_template_parms = 0;
20734 parser->implicit_template_scope = 0;
20738 return parameters;
20741 /* Parse a parameter declaration.
20743 parameter-declaration:
20744 decl-specifier-seq ... [opt] declarator
20745 decl-specifier-seq declarator = assignment-expression
20746 decl-specifier-seq ... [opt] abstract-declarator [opt]
20747 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20749 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20750 declares a template parameter. (In that case, a non-nested `>'
20751 token encountered during the parsing of the assignment-expression
20752 is not interpreted as a greater-than operator.)
20754 Returns a representation of the parameter, or NULL if an error
20755 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20756 true iff the declarator is of the form "(p)". */
20758 static cp_parameter_declarator *
20759 cp_parser_parameter_declaration (cp_parser *parser,
20760 bool template_parm_p,
20761 bool *parenthesized_p)
20763 int declares_class_or_enum;
20764 cp_decl_specifier_seq decl_specifiers;
20765 cp_declarator *declarator;
20766 tree default_argument;
20767 cp_token *token = NULL, *declarator_token_start = NULL;
20768 const char *saved_message;
20769 bool template_parameter_pack_p = false;
20771 /* In a template parameter, `>' is not an operator.
20773 [temp.param]
20775 When parsing a default template-argument for a non-type
20776 template-parameter, the first non-nested `>' is taken as the end
20777 of the template parameter-list rather than a greater-than
20778 operator. */
20780 /* Type definitions may not appear in parameter types. */
20781 saved_message = parser->type_definition_forbidden_message;
20782 parser->type_definition_forbidden_message
20783 = G_("types may not be defined in parameter types");
20785 /* Parse the declaration-specifiers. */
20786 cp_parser_decl_specifier_seq (parser,
20787 CP_PARSER_FLAGS_NONE,
20788 &decl_specifiers,
20789 &declares_class_or_enum);
20791 /* Complain about missing 'typename' or other invalid type names. */
20792 if (!decl_specifiers.any_type_specifiers_p
20793 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20794 decl_specifiers.type = error_mark_node;
20796 /* If an error occurred, there's no reason to attempt to parse the
20797 rest of the declaration. */
20798 if (cp_parser_error_occurred (parser))
20800 parser->type_definition_forbidden_message = saved_message;
20801 return NULL;
20804 /* Peek at the next token. */
20805 token = cp_lexer_peek_token (parser->lexer);
20807 /* If the next token is a `)', `,', `=', `>', or `...', then there
20808 is no declarator. However, when variadic templates are enabled,
20809 there may be a declarator following `...'. */
20810 if (token->type == CPP_CLOSE_PAREN
20811 || token->type == CPP_COMMA
20812 || token->type == CPP_EQ
20813 || token->type == CPP_GREATER)
20815 declarator = NULL;
20816 if (parenthesized_p)
20817 *parenthesized_p = false;
20819 /* Otherwise, there should be a declarator. */
20820 else
20822 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20823 parser->default_arg_ok_p = false;
20825 /* After seeing a decl-specifier-seq, if the next token is not a
20826 "(", there is no possibility that the code is a valid
20827 expression. Therefore, if parsing tentatively, we commit at
20828 this point. */
20829 if (!parser->in_template_argument_list_p
20830 /* In an expression context, having seen:
20832 (int((char ...
20834 we cannot be sure whether we are looking at a
20835 function-type (taking a "char" as a parameter) or a cast
20836 of some object of type "char" to "int". */
20837 && !parser->in_type_id_in_expr_p
20838 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20839 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20840 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20841 cp_parser_commit_to_tentative_parse (parser);
20842 /* Parse the declarator. */
20843 declarator_token_start = token;
20844 declarator = cp_parser_declarator (parser,
20845 CP_PARSER_DECLARATOR_EITHER,
20846 /*ctor_dtor_or_conv_p=*/NULL,
20847 parenthesized_p,
20848 /*member_p=*/false,
20849 /*friend_p=*/false);
20850 parser->default_arg_ok_p = saved_default_arg_ok_p;
20851 /* After the declarator, allow more attributes. */
20852 decl_specifiers.attributes
20853 = chainon (decl_specifiers.attributes,
20854 cp_parser_attributes_opt (parser));
20856 /* If the declarator is a template parameter pack, remember that and
20857 clear the flag in the declarator itself so we don't get errors
20858 from grokdeclarator. */
20859 if (template_parm_p && declarator && declarator->parameter_pack_p)
20861 declarator->parameter_pack_p = false;
20862 template_parameter_pack_p = true;
20866 /* If the next token is an ellipsis, and we have not seen a declarator
20867 name, and if either the type of the declarator contains parameter
20868 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20869 for, eg, abbreviated integral type names), then we actually have a
20870 parameter pack expansion expression. Otherwise, leave the ellipsis
20871 for a C-style variadic function. */
20872 token = cp_lexer_peek_token (parser->lexer);
20873 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20875 tree type = decl_specifiers.type;
20877 if (type && DECL_P (type))
20878 type = TREE_TYPE (type);
20880 if (((type
20881 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20882 && (template_parm_p || uses_parameter_packs (type)))
20883 || (!type && template_parm_p))
20884 && declarator_can_be_parameter_pack (declarator))
20886 /* Consume the `...'. */
20887 cp_lexer_consume_token (parser->lexer);
20888 maybe_warn_variadic_templates ();
20890 /* Build a pack expansion type */
20891 if (template_parm_p)
20892 template_parameter_pack_p = true;
20893 else if (declarator)
20894 declarator->parameter_pack_p = true;
20895 else
20896 decl_specifiers.type = make_pack_expansion (type);
20900 /* The restriction on defining new types applies only to the type
20901 of the parameter, not to the default argument. */
20902 parser->type_definition_forbidden_message = saved_message;
20904 /* If the next token is `=', then process a default argument. */
20905 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20907 tree type = decl_specifiers.type;
20908 token = cp_lexer_peek_token (parser->lexer);
20909 /* If we are defining a class, then the tokens that make up the
20910 default argument must be saved and processed later. */
20911 if (!template_parm_p && at_class_scope_p ()
20912 && TYPE_BEING_DEFINED (current_class_type)
20913 && !LAMBDA_TYPE_P (current_class_type))
20914 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20916 // A constrained-type-specifier may declare a type template-parameter.
20917 else if (declares_constrained_type_template_parameter (type))
20918 default_argument
20919 = cp_parser_default_type_template_argument (parser);
20921 // A constrained-type-specifier may declare a template-template-parameter.
20922 else if (declares_constrained_template_template_parameter (type))
20923 default_argument
20924 = cp_parser_default_template_template_argument (parser);
20926 /* Outside of a class definition, we can just parse the
20927 assignment-expression. */
20928 else
20929 default_argument
20930 = cp_parser_default_argument (parser, template_parm_p);
20932 if (!parser->default_arg_ok_p)
20934 permerror (token->location,
20935 "default arguments are only "
20936 "permitted for function parameters");
20938 else if ((declarator && declarator->parameter_pack_p)
20939 || template_parameter_pack_p
20940 || (decl_specifiers.type
20941 && PACK_EXPANSION_P (decl_specifiers.type)))
20943 /* Find the name of the parameter pack. */
20944 cp_declarator *id_declarator = declarator;
20945 while (id_declarator && id_declarator->kind != cdk_id)
20946 id_declarator = id_declarator->declarator;
20948 if (id_declarator && id_declarator->kind == cdk_id)
20949 error_at (declarator_token_start->location,
20950 template_parm_p
20951 ? G_("template parameter pack %qD "
20952 "cannot have a default argument")
20953 : G_("parameter pack %qD cannot have "
20954 "a default argument"),
20955 id_declarator->u.id.unqualified_name);
20956 else
20957 error_at (declarator_token_start->location,
20958 template_parm_p
20959 ? G_("template parameter pack cannot have "
20960 "a default argument")
20961 : G_("parameter pack cannot have a "
20962 "default argument"));
20964 default_argument = NULL_TREE;
20967 else
20968 default_argument = NULL_TREE;
20970 return make_parameter_declarator (&decl_specifiers,
20971 declarator,
20972 default_argument,
20973 template_parameter_pack_p);
20976 /* Parse a default argument and return it.
20978 TEMPLATE_PARM_P is true if this is a default argument for a
20979 non-type template parameter. */
20980 static tree
20981 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20983 tree default_argument = NULL_TREE;
20984 bool saved_greater_than_is_operator_p;
20985 bool saved_local_variables_forbidden_p;
20986 bool non_constant_p, is_direct_init;
20988 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20989 set correctly. */
20990 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20991 parser->greater_than_is_operator_p = !template_parm_p;
20992 /* Local variable names (and the `this' keyword) may not
20993 appear in a default argument. */
20994 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20995 parser->local_variables_forbidden_p = true;
20996 /* Parse the assignment-expression. */
20997 if (template_parm_p)
20998 push_deferring_access_checks (dk_no_deferred);
20999 tree saved_class_ptr = NULL_TREE;
21000 tree saved_class_ref = NULL_TREE;
21001 /* The "this" pointer is not valid in a default argument. */
21002 if (cfun)
21004 saved_class_ptr = current_class_ptr;
21005 cp_function_chain->x_current_class_ptr = NULL_TREE;
21006 saved_class_ref = current_class_ref;
21007 cp_function_chain->x_current_class_ref = NULL_TREE;
21009 default_argument
21010 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21011 /* Restore the "this" pointer. */
21012 if (cfun)
21014 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21015 cp_function_chain->x_current_class_ref = saved_class_ref;
21017 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21018 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21019 if (template_parm_p)
21020 pop_deferring_access_checks ();
21021 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21022 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21024 return default_argument;
21027 /* Parse a function-body.
21029 function-body:
21030 compound_statement */
21032 static void
21033 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21035 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21036 ? BCS_TRY_BLOCK : BCS_NORMAL),
21037 true);
21040 /* Parse a ctor-initializer-opt followed by a function-body. Return
21041 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21042 is true we are parsing a function-try-block. */
21044 static bool
21045 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21046 bool in_function_try_block)
21048 tree body, list;
21049 bool ctor_initializer_p;
21050 const bool check_body_p =
21051 DECL_CONSTRUCTOR_P (current_function_decl)
21052 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21053 tree last = NULL;
21055 /* Begin the function body. */
21056 body = begin_function_body ();
21057 /* Parse the optional ctor-initializer. */
21058 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21060 /* If we're parsing a constexpr constructor definition, we need
21061 to check that the constructor body is indeed empty. However,
21062 before we get to cp_parser_function_body lot of junk has been
21063 generated, so we can't just check that we have an empty block.
21064 Rather we take a snapshot of the outermost block, and check whether
21065 cp_parser_function_body changed its state. */
21066 if (check_body_p)
21068 list = cur_stmt_list;
21069 if (STATEMENT_LIST_TAIL (list))
21070 last = STATEMENT_LIST_TAIL (list)->stmt;
21072 /* Parse the function-body. */
21073 cp_parser_function_body (parser, in_function_try_block);
21074 if (check_body_p)
21075 check_constexpr_ctor_body (last, list, /*complain=*/true);
21076 /* Finish the function body. */
21077 finish_function_body (body);
21079 return ctor_initializer_p;
21082 /* Parse an initializer.
21084 initializer:
21085 = initializer-clause
21086 ( expression-list )
21088 Returns an expression representing the initializer. If no
21089 initializer is present, NULL_TREE is returned.
21091 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21092 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21093 set to TRUE if there is no initializer present. If there is an
21094 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21095 is set to true; otherwise it is set to false. */
21097 static tree
21098 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21099 bool* non_constant_p)
21101 cp_token *token;
21102 tree init;
21104 /* Peek at the next token. */
21105 token = cp_lexer_peek_token (parser->lexer);
21107 /* Let our caller know whether or not this initializer was
21108 parenthesized. */
21109 *is_direct_init = (token->type != CPP_EQ);
21110 /* Assume that the initializer is constant. */
21111 *non_constant_p = false;
21113 if (token->type == CPP_EQ)
21115 /* Consume the `='. */
21116 cp_lexer_consume_token (parser->lexer);
21117 /* Parse the initializer-clause. */
21118 init = cp_parser_initializer_clause (parser, non_constant_p);
21120 else if (token->type == CPP_OPEN_PAREN)
21122 vec<tree, va_gc> *vec;
21123 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21124 /*cast_p=*/false,
21125 /*allow_expansion_p=*/true,
21126 non_constant_p);
21127 if (vec == NULL)
21128 return error_mark_node;
21129 init = build_tree_list_vec (vec);
21130 release_tree_vector (vec);
21132 else if (token->type == CPP_OPEN_BRACE)
21134 cp_lexer_set_source_position (parser->lexer);
21135 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21136 init = cp_parser_braced_list (parser, non_constant_p);
21137 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21139 else
21141 /* Anything else is an error. */
21142 cp_parser_error (parser, "expected initializer");
21143 init = error_mark_node;
21146 if (check_for_bare_parameter_packs (init))
21147 init = error_mark_node;
21149 return init;
21152 /* Parse an initializer-clause.
21154 initializer-clause:
21155 assignment-expression
21156 braced-init-list
21158 Returns an expression representing the initializer.
21160 If the `assignment-expression' production is used the value
21161 returned is simply a representation for the expression.
21163 Otherwise, calls cp_parser_braced_list. */
21165 static cp_expr
21166 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21168 cp_expr initializer;
21170 /* Assume the expression is constant. */
21171 *non_constant_p = false;
21173 /* If it is not a `{', then we are looking at an
21174 assignment-expression. */
21175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21177 initializer
21178 = cp_parser_constant_expression (parser,
21179 /*allow_non_constant_p=*/true,
21180 non_constant_p);
21182 else
21183 initializer = cp_parser_braced_list (parser, non_constant_p);
21185 return initializer;
21188 /* Parse a brace-enclosed initializer list.
21190 braced-init-list:
21191 { initializer-list , [opt] }
21194 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21195 the elements of the initializer-list (or NULL, if the last
21196 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21197 NULL_TREE. There is no way to detect whether or not the optional
21198 trailing `,' was provided. NON_CONSTANT_P is as for
21199 cp_parser_initializer. */
21201 static cp_expr
21202 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21204 tree initializer;
21205 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21207 /* Consume the `{' token. */
21208 cp_lexer_consume_token (parser->lexer);
21209 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21210 initializer = make_node (CONSTRUCTOR);
21211 /* If it's not a `}', then there is a non-trivial initializer. */
21212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21214 /* Parse the initializer list. */
21215 CONSTRUCTOR_ELTS (initializer)
21216 = cp_parser_initializer_list (parser, non_constant_p);
21217 /* A trailing `,' token is allowed. */
21218 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21219 cp_lexer_consume_token (parser->lexer);
21221 else
21222 *non_constant_p = false;
21223 /* Now, there should be a trailing `}'. */
21224 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21225 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21226 TREE_TYPE (initializer) = init_list_type_node;
21228 cp_expr result (initializer);
21229 /* Build a location of the form:
21230 { ... }
21231 ^~~~~~~
21232 with caret==start at the open brace, finish at the close brace. */
21233 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21234 result.set_location (combined_loc);
21235 return result;
21238 /* Consume tokens up to, and including, the next non-nested closing `]'.
21239 Returns true iff we found a closing `]'. */
21241 static bool
21242 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21244 unsigned square_depth = 0;
21246 while (true)
21248 cp_token * token = cp_lexer_peek_token (parser->lexer);
21250 switch (token->type)
21252 case CPP_EOF:
21253 case CPP_PRAGMA_EOL:
21254 /* If we've run out of tokens, then there is no closing `]'. */
21255 return false;
21257 case CPP_OPEN_SQUARE:
21258 ++square_depth;
21259 break;
21261 case CPP_CLOSE_SQUARE:
21262 if (!square_depth--)
21264 cp_lexer_consume_token (parser->lexer);
21265 return true;
21267 break;
21269 default:
21270 break;
21273 /* Consume the token. */
21274 cp_lexer_consume_token (parser->lexer);
21278 /* Return true if we are looking at an array-designator, false otherwise. */
21280 static bool
21281 cp_parser_array_designator_p (cp_parser *parser)
21283 /* Consume the `['. */
21284 cp_lexer_consume_token (parser->lexer);
21286 cp_lexer_save_tokens (parser->lexer);
21288 /* Skip tokens until the next token is a closing square bracket.
21289 If we find the closing `]', and the next token is a `=', then
21290 we are looking at an array designator. */
21291 bool array_designator_p
21292 = (cp_parser_skip_to_closing_square_bracket (parser)
21293 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21295 /* Roll back the tokens we skipped. */
21296 cp_lexer_rollback_tokens (parser->lexer);
21298 return array_designator_p;
21301 /* Parse an initializer-list.
21303 initializer-list:
21304 initializer-clause ... [opt]
21305 initializer-list , initializer-clause ... [opt]
21307 GNU Extension:
21309 initializer-list:
21310 designation initializer-clause ...[opt]
21311 initializer-list , designation initializer-clause ...[opt]
21313 designation:
21314 . identifier =
21315 identifier :
21316 [ constant-expression ] =
21318 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21319 for the initializer. If the INDEX of the elt is non-NULL, it is the
21320 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21321 as for cp_parser_initializer. */
21323 static vec<constructor_elt, va_gc> *
21324 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21326 vec<constructor_elt, va_gc> *v = NULL;
21328 /* Assume all of the expressions are constant. */
21329 *non_constant_p = false;
21331 /* Parse the rest of the list. */
21332 while (true)
21334 cp_token *token;
21335 tree designator;
21336 tree initializer;
21337 bool clause_non_constant_p;
21339 /* If the next token is an identifier and the following one is a
21340 colon, we are looking at the GNU designated-initializer
21341 syntax. */
21342 if (cp_parser_allow_gnu_extensions_p (parser)
21343 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21344 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21346 /* Warn the user that they are using an extension. */
21347 pedwarn (input_location, OPT_Wpedantic,
21348 "ISO C++ does not allow designated initializers");
21349 /* Consume the identifier. */
21350 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21351 /* Consume the `:'. */
21352 cp_lexer_consume_token (parser->lexer);
21354 /* Also handle the C99 syntax, '. id ='. */
21355 else if (cp_parser_allow_gnu_extensions_p (parser)
21356 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21357 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21358 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21360 /* Warn the user that they are using an extension. */
21361 pedwarn (input_location, OPT_Wpedantic,
21362 "ISO C++ does not allow C99 designated initializers");
21363 /* Consume the `.'. */
21364 cp_lexer_consume_token (parser->lexer);
21365 /* Consume the identifier. */
21366 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21367 /* Consume the `='. */
21368 cp_lexer_consume_token (parser->lexer);
21370 /* Also handle C99 array designators, '[ const ] ='. */
21371 else if (cp_parser_allow_gnu_extensions_p (parser)
21372 && !c_dialect_objc ()
21373 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21375 /* In C++11, [ could start a lambda-introducer. */
21376 bool non_const = false;
21378 cp_parser_parse_tentatively (parser);
21380 if (!cp_parser_array_designator_p (parser))
21382 cp_parser_simulate_error (parser);
21383 designator = NULL_TREE;
21385 else
21387 designator = cp_parser_constant_expression (parser, true,
21388 &non_const);
21389 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21390 cp_parser_require (parser, CPP_EQ, RT_EQ);
21393 if (!cp_parser_parse_definitely (parser))
21394 designator = NULL_TREE;
21395 else if (non_const)
21396 require_potential_rvalue_constant_expression (designator);
21398 else
21399 designator = NULL_TREE;
21401 /* Parse the initializer. */
21402 initializer = cp_parser_initializer_clause (parser,
21403 &clause_non_constant_p);
21404 /* If any clause is non-constant, so is the entire initializer. */
21405 if (clause_non_constant_p)
21406 *non_constant_p = true;
21408 /* If we have an ellipsis, this is an initializer pack
21409 expansion. */
21410 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21412 /* Consume the `...'. */
21413 cp_lexer_consume_token (parser->lexer);
21415 /* Turn the initializer into an initializer expansion. */
21416 initializer = make_pack_expansion (initializer);
21419 /* Add it to the vector. */
21420 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21422 /* If the next token is not a comma, we have reached the end of
21423 the list. */
21424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21425 break;
21427 /* Peek at the next token. */
21428 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21429 /* If the next token is a `}', then we're still done. An
21430 initializer-clause can have a trailing `,' after the
21431 initializer-list and before the closing `}'. */
21432 if (token->type == CPP_CLOSE_BRACE)
21433 break;
21435 /* Consume the `,' token. */
21436 cp_lexer_consume_token (parser->lexer);
21439 return v;
21442 /* Classes [gram.class] */
21444 /* Parse a class-name.
21446 class-name:
21447 identifier
21448 template-id
21450 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21451 to indicate that names looked up in dependent types should be
21452 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21453 keyword has been used to indicate that the name that appears next
21454 is a template. TAG_TYPE indicates the explicit tag given before
21455 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21456 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21457 is the class being defined in a class-head. If ENUM_OK is TRUE,
21458 enum-names are also accepted.
21460 Returns the TYPE_DECL representing the class. */
21462 static tree
21463 cp_parser_class_name (cp_parser *parser,
21464 bool typename_keyword_p,
21465 bool template_keyword_p,
21466 enum tag_types tag_type,
21467 bool check_dependency_p,
21468 bool class_head_p,
21469 bool is_declaration,
21470 bool enum_ok)
21472 tree decl;
21473 tree scope;
21474 bool typename_p;
21475 cp_token *token;
21476 tree identifier = NULL_TREE;
21478 /* All class-names start with an identifier. */
21479 token = cp_lexer_peek_token (parser->lexer);
21480 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21482 cp_parser_error (parser, "expected class-name");
21483 return error_mark_node;
21486 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21487 to a template-id, so we save it here. */
21488 scope = parser->scope;
21489 if (scope == error_mark_node)
21490 return error_mark_node;
21492 /* Any name names a type if we're following the `typename' keyword
21493 in a qualified name where the enclosing scope is type-dependent. */
21494 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21495 && dependent_type_p (scope));
21496 /* Handle the common case (an identifier, but not a template-id)
21497 efficiently. */
21498 if (token->type == CPP_NAME
21499 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21501 cp_token *identifier_token;
21502 bool ambiguous_p;
21504 /* Look for the identifier. */
21505 identifier_token = cp_lexer_peek_token (parser->lexer);
21506 ambiguous_p = identifier_token->error_reported;
21507 identifier = cp_parser_identifier (parser);
21508 /* If the next token isn't an identifier, we are certainly not
21509 looking at a class-name. */
21510 if (identifier == error_mark_node)
21511 decl = error_mark_node;
21512 /* If we know this is a type-name, there's no need to look it
21513 up. */
21514 else if (typename_p)
21515 decl = identifier;
21516 else
21518 tree ambiguous_decls;
21519 /* If we already know that this lookup is ambiguous, then
21520 we've already issued an error message; there's no reason
21521 to check again. */
21522 if (ambiguous_p)
21524 cp_parser_simulate_error (parser);
21525 return error_mark_node;
21527 /* If the next token is a `::', then the name must be a type
21528 name.
21530 [basic.lookup.qual]
21532 During the lookup for a name preceding the :: scope
21533 resolution operator, object, function, and enumerator
21534 names are ignored. */
21535 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21536 tag_type = scope_type;
21537 /* Look up the name. */
21538 decl = cp_parser_lookup_name (parser, identifier,
21539 tag_type,
21540 /*is_template=*/false,
21541 /*is_namespace=*/false,
21542 check_dependency_p,
21543 &ambiguous_decls,
21544 identifier_token->location);
21545 if (ambiguous_decls)
21547 if (cp_parser_parsing_tentatively (parser))
21548 cp_parser_simulate_error (parser);
21549 return error_mark_node;
21553 else
21555 /* Try a template-id. */
21556 decl = cp_parser_template_id (parser, template_keyword_p,
21557 check_dependency_p,
21558 tag_type,
21559 is_declaration);
21560 if (decl == error_mark_node)
21561 return error_mark_node;
21564 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21566 /* If this is a typename, create a TYPENAME_TYPE. */
21567 if (typename_p && decl != error_mark_node)
21569 decl = make_typename_type (scope, decl, typename_type,
21570 /*complain=*/tf_error);
21571 if (decl != error_mark_node)
21572 decl = TYPE_NAME (decl);
21575 decl = strip_using_decl (decl);
21577 /* Check to see that it is really the name of a class. */
21578 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21579 && identifier_p (TREE_OPERAND (decl, 0))
21580 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21581 /* Situations like this:
21583 template <typename T> struct A {
21584 typename T::template X<int>::I i;
21587 are problematic. Is `T::template X<int>' a class-name? The
21588 standard does not seem to be definitive, but there is no other
21589 valid interpretation of the following `::'. Therefore, those
21590 names are considered class-names. */
21592 decl = make_typename_type (scope, decl, tag_type, tf_error);
21593 if (decl != error_mark_node)
21594 decl = TYPE_NAME (decl);
21596 else if (TREE_CODE (decl) != TYPE_DECL
21597 || TREE_TYPE (decl) == error_mark_node
21598 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21599 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21600 /* In Objective-C 2.0, a classname followed by '.' starts a
21601 dot-syntax expression, and it's not a type-name. */
21602 || (c_dialect_objc ()
21603 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21604 && objc_is_class_name (decl)))
21605 decl = error_mark_node;
21607 if (decl == error_mark_node)
21608 cp_parser_error (parser, "expected class-name");
21609 else if (identifier && !parser->scope)
21610 maybe_note_name_used_in_class (identifier, decl);
21612 return decl;
21615 /* Parse a class-specifier.
21617 class-specifier:
21618 class-head { member-specification [opt] }
21620 Returns the TREE_TYPE representing the class. */
21622 static tree
21623 cp_parser_class_specifier_1 (cp_parser* parser)
21625 tree type;
21626 tree attributes = NULL_TREE;
21627 bool nested_name_specifier_p;
21628 unsigned saved_num_template_parameter_lists;
21629 bool saved_in_function_body;
21630 unsigned char in_statement;
21631 bool in_switch_statement_p;
21632 bool saved_in_unbraced_linkage_specification_p;
21633 tree old_scope = NULL_TREE;
21634 tree scope = NULL_TREE;
21635 cp_token *closing_brace;
21637 push_deferring_access_checks (dk_no_deferred);
21639 /* Parse the class-head. */
21640 type = cp_parser_class_head (parser,
21641 &nested_name_specifier_p);
21642 /* If the class-head was a semantic disaster, skip the entire body
21643 of the class. */
21644 if (!type)
21646 cp_parser_skip_to_end_of_block_or_statement (parser);
21647 pop_deferring_access_checks ();
21648 return error_mark_node;
21651 /* Look for the `{'. */
21652 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21654 pop_deferring_access_checks ();
21655 return error_mark_node;
21658 cp_ensure_no_omp_declare_simd (parser);
21659 cp_ensure_no_oacc_routine (parser);
21661 /* Issue an error message if type-definitions are forbidden here. */
21662 cp_parser_check_type_definition (parser);
21663 /* Remember that we are defining one more class. */
21664 ++parser->num_classes_being_defined;
21665 /* Inside the class, surrounding template-parameter-lists do not
21666 apply. */
21667 saved_num_template_parameter_lists
21668 = parser->num_template_parameter_lists;
21669 parser->num_template_parameter_lists = 0;
21670 /* We are not in a function body. */
21671 saved_in_function_body = parser->in_function_body;
21672 parser->in_function_body = false;
21673 /* Or in a loop. */
21674 in_statement = parser->in_statement;
21675 parser->in_statement = 0;
21676 /* Or in a switch. */
21677 in_switch_statement_p = parser->in_switch_statement_p;
21678 parser->in_switch_statement_p = false;
21679 /* We are not immediately inside an extern "lang" block. */
21680 saved_in_unbraced_linkage_specification_p
21681 = parser->in_unbraced_linkage_specification_p;
21682 parser->in_unbraced_linkage_specification_p = false;
21684 // Associate constraints with the type.
21685 if (flag_concepts)
21686 type = associate_classtype_constraints (type);
21688 /* Start the class. */
21689 if (nested_name_specifier_p)
21691 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21692 old_scope = push_inner_scope (scope);
21694 type = begin_class_definition (type);
21696 if (type == error_mark_node)
21697 /* If the type is erroneous, skip the entire body of the class. */
21698 cp_parser_skip_to_closing_brace (parser);
21699 else
21700 /* Parse the member-specification. */
21701 cp_parser_member_specification_opt (parser);
21703 /* Look for the trailing `}'. */
21704 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21705 /* Look for trailing attributes to apply to this class. */
21706 if (cp_parser_allow_gnu_extensions_p (parser))
21707 attributes = cp_parser_gnu_attributes_opt (parser);
21708 if (type != error_mark_node)
21709 type = finish_struct (type, attributes);
21710 if (nested_name_specifier_p)
21711 pop_inner_scope (old_scope, scope);
21713 /* We've finished a type definition. Check for the common syntax
21714 error of forgetting a semicolon after the definition. We need to
21715 be careful, as we can't just check for not-a-semicolon and be done
21716 with it; the user might have typed:
21718 class X { } c = ...;
21719 class X { } *p = ...;
21721 and so forth. Instead, enumerate all the possible tokens that
21722 might follow this production; if we don't see one of them, then
21723 complain and silently insert the semicolon. */
21725 cp_token *token = cp_lexer_peek_token (parser->lexer);
21726 bool want_semicolon = true;
21728 if (cp_next_tokens_can_be_std_attribute_p (parser))
21729 /* Don't try to parse c++11 attributes here. As per the
21730 grammar, that should be a task for
21731 cp_parser_decl_specifier_seq. */
21732 want_semicolon = false;
21734 switch (token->type)
21736 case CPP_NAME:
21737 case CPP_SEMICOLON:
21738 case CPP_MULT:
21739 case CPP_AND:
21740 case CPP_OPEN_PAREN:
21741 case CPP_CLOSE_PAREN:
21742 case CPP_COMMA:
21743 want_semicolon = false;
21744 break;
21746 /* While it's legal for type qualifiers and storage class
21747 specifiers to follow type definitions in the grammar, only
21748 compiler testsuites contain code like that. Assume that if
21749 we see such code, then what we're really seeing is a case
21750 like:
21752 class X { }
21753 const <type> var = ...;
21757 class Y { }
21758 static <type> func (...) ...
21760 i.e. the qualifier or specifier applies to the next
21761 declaration. To do so, however, we need to look ahead one
21762 more token to see if *that* token is a type specifier.
21764 This code could be improved to handle:
21766 class Z { }
21767 static const <type> var = ...; */
21768 case CPP_KEYWORD:
21769 if (keyword_is_decl_specifier (token->keyword))
21771 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21773 /* Handling user-defined types here would be nice, but very
21774 tricky. */
21775 want_semicolon
21776 = (lookahead->type == CPP_KEYWORD
21777 && keyword_begins_type_specifier (lookahead->keyword));
21779 break;
21780 default:
21781 break;
21784 /* If we don't have a type, then something is very wrong and we
21785 shouldn't try to do anything clever. Likewise for not seeing the
21786 closing brace. */
21787 if (closing_brace && TYPE_P (type) && want_semicolon)
21789 /* Locate the closing brace. */
21790 cp_token_position prev
21791 = cp_lexer_previous_token_position (parser->lexer);
21792 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21793 location_t loc = prev_token->location;
21795 /* We want to suggest insertion of a ';' immediately *after* the
21796 closing brace, so, if we can, offset the location by 1 column. */
21797 location_t next_loc = loc;
21798 if (!linemap_location_from_macro_expansion_p (line_table, loc))
21799 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
21801 rich_location richloc (line_table, next_loc);
21802 richloc.add_fixit_insert_before (next_loc, ";");
21804 if (CLASSTYPE_DECLARED_CLASS (type))
21805 error_at_rich_loc (&richloc,
21806 "expected %<;%> after class definition");
21807 else if (TREE_CODE (type) == RECORD_TYPE)
21808 error_at_rich_loc (&richloc,
21809 "expected %<;%> after struct definition");
21810 else if (TREE_CODE (type) == UNION_TYPE)
21811 error_at_rich_loc (&richloc,
21812 "expected %<;%> after union definition");
21813 else
21814 gcc_unreachable ();
21816 /* Unget one token and smash it to look as though we encountered
21817 a semicolon in the input stream. */
21818 cp_lexer_set_token_position (parser->lexer, prev);
21819 token = cp_lexer_peek_token (parser->lexer);
21820 token->type = CPP_SEMICOLON;
21821 token->keyword = RID_MAX;
21825 /* If this class is not itself within the scope of another class,
21826 then we need to parse the bodies of all of the queued function
21827 definitions. Note that the queued functions defined in a class
21828 are not always processed immediately following the
21829 class-specifier for that class. Consider:
21831 struct A {
21832 struct B { void f() { sizeof (A); } };
21835 If `f' were processed before the processing of `A' were
21836 completed, there would be no way to compute the size of `A'.
21837 Note that the nesting we are interested in here is lexical --
21838 not the semantic nesting given by TYPE_CONTEXT. In particular,
21839 for:
21841 struct A { struct B; };
21842 struct A::B { void f() { } };
21844 there is no need to delay the parsing of `A::B::f'. */
21845 if (--parser->num_classes_being_defined == 0)
21847 tree decl;
21848 tree class_type = NULL_TREE;
21849 tree pushed_scope = NULL_TREE;
21850 unsigned ix;
21851 cp_default_arg_entry *e;
21852 tree save_ccp, save_ccr;
21854 /* In a first pass, parse default arguments to the functions.
21855 Then, in a second pass, parse the bodies of the functions.
21856 This two-phased approach handles cases like:
21858 struct S {
21859 void f() { g(); }
21860 void g(int i = 3);
21864 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21866 decl = e->decl;
21867 /* If there are default arguments that have not yet been processed,
21868 take care of them now. */
21869 if (class_type != e->class_type)
21871 if (pushed_scope)
21872 pop_scope (pushed_scope);
21873 class_type = e->class_type;
21874 pushed_scope = push_scope (class_type);
21876 /* Make sure that any template parameters are in scope. */
21877 maybe_begin_member_template_processing (decl);
21878 /* Parse the default argument expressions. */
21879 cp_parser_late_parsing_default_args (parser, decl);
21880 /* Remove any template parameters from the symbol table. */
21881 maybe_end_member_template_processing ();
21883 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21884 /* Now parse any NSDMIs. */
21885 save_ccp = current_class_ptr;
21886 save_ccr = current_class_ref;
21887 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21889 if (class_type != DECL_CONTEXT (decl))
21891 if (pushed_scope)
21892 pop_scope (pushed_scope);
21893 class_type = DECL_CONTEXT (decl);
21894 pushed_scope = push_scope (class_type);
21896 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21897 cp_parser_late_parsing_nsdmi (parser, decl);
21899 vec_safe_truncate (unparsed_nsdmis, 0);
21900 current_class_ptr = save_ccp;
21901 current_class_ref = save_ccr;
21902 if (pushed_scope)
21903 pop_scope (pushed_scope);
21905 /* Now do some post-NSDMI bookkeeping. */
21906 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21907 after_nsdmi_defaulted_late_checks (class_type);
21908 vec_safe_truncate (unparsed_classes, 0);
21909 after_nsdmi_defaulted_late_checks (type);
21911 /* Now parse the body of the functions. */
21912 if (flag_openmp)
21914 /* OpenMP UDRs need to be parsed before all other functions. */
21915 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21916 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21917 cp_parser_late_parsing_for_member (parser, decl);
21918 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21919 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21920 cp_parser_late_parsing_for_member (parser, decl);
21922 else
21923 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21924 cp_parser_late_parsing_for_member (parser, decl);
21925 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21927 else
21928 vec_safe_push (unparsed_classes, type);
21930 /* Put back any saved access checks. */
21931 pop_deferring_access_checks ();
21933 /* Restore saved state. */
21934 parser->in_switch_statement_p = in_switch_statement_p;
21935 parser->in_statement = in_statement;
21936 parser->in_function_body = saved_in_function_body;
21937 parser->num_template_parameter_lists
21938 = saved_num_template_parameter_lists;
21939 parser->in_unbraced_linkage_specification_p
21940 = saved_in_unbraced_linkage_specification_p;
21942 return type;
21945 static tree
21946 cp_parser_class_specifier (cp_parser* parser)
21948 tree ret;
21949 timevar_push (TV_PARSE_STRUCT);
21950 ret = cp_parser_class_specifier_1 (parser);
21951 timevar_pop (TV_PARSE_STRUCT);
21952 return ret;
21955 /* Parse a class-head.
21957 class-head:
21958 class-key identifier [opt] base-clause [opt]
21959 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21960 class-key nested-name-specifier [opt] template-id
21961 base-clause [opt]
21963 class-virt-specifier:
21964 final
21966 GNU Extensions:
21967 class-key attributes identifier [opt] base-clause [opt]
21968 class-key attributes nested-name-specifier identifier base-clause [opt]
21969 class-key attributes nested-name-specifier [opt] template-id
21970 base-clause [opt]
21972 Upon return BASES is initialized to the list of base classes (or
21973 NULL, if there are none) in the same form returned by
21974 cp_parser_base_clause.
21976 Returns the TYPE of the indicated class. Sets
21977 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21978 involving a nested-name-specifier was used, and FALSE otherwise.
21980 Returns error_mark_node if this is not a class-head.
21982 Returns NULL_TREE if the class-head is syntactically valid, but
21983 semantically invalid in a way that means we should skip the entire
21984 body of the class. */
21986 static tree
21987 cp_parser_class_head (cp_parser* parser,
21988 bool* nested_name_specifier_p)
21990 tree nested_name_specifier;
21991 enum tag_types class_key;
21992 tree id = NULL_TREE;
21993 tree type = NULL_TREE;
21994 tree attributes;
21995 tree bases;
21996 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21997 bool template_id_p = false;
21998 bool qualified_p = false;
21999 bool invalid_nested_name_p = false;
22000 bool invalid_explicit_specialization_p = false;
22001 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22002 tree pushed_scope = NULL_TREE;
22003 unsigned num_templates;
22004 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22005 /* Assume no nested-name-specifier will be present. */
22006 *nested_name_specifier_p = false;
22007 /* Assume no template parameter lists will be used in defining the
22008 type. */
22009 num_templates = 0;
22010 parser->colon_corrects_to_scope_p = false;
22012 /* Look for the class-key. */
22013 class_key = cp_parser_class_key (parser);
22014 if (class_key == none_type)
22015 return error_mark_node;
22017 location_t class_head_start_location = input_location;
22019 /* Parse the attributes. */
22020 attributes = cp_parser_attributes_opt (parser);
22022 /* If the next token is `::', that is invalid -- but sometimes
22023 people do try to write:
22025 struct ::S {};
22027 Handle this gracefully by accepting the extra qualifier, and then
22028 issuing an error about it later if this really is a
22029 class-head. If it turns out just to be an elaborated type
22030 specifier, remain silent. */
22031 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22032 qualified_p = true;
22034 push_deferring_access_checks (dk_no_check);
22036 /* Determine the name of the class. Begin by looking for an
22037 optional nested-name-specifier. */
22038 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22039 nested_name_specifier
22040 = cp_parser_nested_name_specifier_opt (parser,
22041 /*typename_keyword_p=*/false,
22042 /*check_dependency_p=*/false,
22043 /*type_p=*/true,
22044 /*is_declaration=*/false);
22045 /* If there was a nested-name-specifier, then there *must* be an
22046 identifier. */
22047 if (nested_name_specifier)
22049 type_start_token = cp_lexer_peek_token (parser->lexer);
22050 /* Although the grammar says `identifier', it really means
22051 `class-name' or `template-name'. You are only allowed to
22052 define a class that has already been declared with this
22053 syntax.
22055 The proposed resolution for Core Issue 180 says that wherever
22056 you see `class T::X' you should treat `X' as a type-name.
22058 It is OK to define an inaccessible class; for example:
22060 class A { class B; };
22061 class A::B {};
22063 We do not know if we will see a class-name, or a
22064 template-name. We look for a class-name first, in case the
22065 class-name is a template-id; if we looked for the
22066 template-name first we would stop after the template-name. */
22067 cp_parser_parse_tentatively (parser);
22068 type = cp_parser_class_name (parser,
22069 /*typename_keyword_p=*/false,
22070 /*template_keyword_p=*/false,
22071 class_type,
22072 /*check_dependency_p=*/false,
22073 /*class_head_p=*/true,
22074 /*is_declaration=*/false);
22075 /* If that didn't work, ignore the nested-name-specifier. */
22076 if (!cp_parser_parse_definitely (parser))
22078 invalid_nested_name_p = true;
22079 type_start_token = cp_lexer_peek_token (parser->lexer);
22080 id = cp_parser_identifier (parser);
22081 if (id == error_mark_node)
22082 id = NULL_TREE;
22084 /* If we could not find a corresponding TYPE, treat this
22085 declaration like an unqualified declaration. */
22086 if (type == error_mark_node)
22087 nested_name_specifier = NULL_TREE;
22088 /* Otherwise, count the number of templates used in TYPE and its
22089 containing scopes. */
22090 else
22092 tree scope;
22094 for (scope = TREE_TYPE (type);
22095 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22096 scope = get_containing_scope (scope))
22097 if (TYPE_P (scope)
22098 && CLASS_TYPE_P (scope)
22099 && CLASSTYPE_TEMPLATE_INFO (scope)
22100 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22101 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22102 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22103 ++num_templates;
22106 /* Otherwise, the identifier is optional. */
22107 else
22109 /* We don't know whether what comes next is a template-id,
22110 an identifier, or nothing at all. */
22111 cp_parser_parse_tentatively (parser);
22112 /* Check for a template-id. */
22113 type_start_token = cp_lexer_peek_token (parser->lexer);
22114 id = cp_parser_template_id (parser,
22115 /*template_keyword_p=*/false,
22116 /*check_dependency_p=*/true,
22117 class_key,
22118 /*is_declaration=*/true);
22119 /* If that didn't work, it could still be an identifier. */
22120 if (!cp_parser_parse_definitely (parser))
22122 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22124 type_start_token = cp_lexer_peek_token (parser->lexer);
22125 id = cp_parser_identifier (parser);
22127 else
22128 id = NULL_TREE;
22130 else
22132 template_id_p = true;
22133 ++num_templates;
22137 pop_deferring_access_checks ();
22139 if (id)
22141 cp_parser_check_for_invalid_template_id (parser, id,
22142 class_key,
22143 type_start_token->location);
22145 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22147 /* If it's not a `:' or a `{' then we can't really be looking at a
22148 class-head, since a class-head only appears as part of a
22149 class-specifier. We have to detect this situation before calling
22150 xref_tag, since that has irreversible side-effects. */
22151 if (!cp_parser_next_token_starts_class_definition_p (parser))
22153 cp_parser_error (parser, "expected %<{%> or %<:%>");
22154 type = error_mark_node;
22155 goto out;
22158 /* At this point, we're going ahead with the class-specifier, even
22159 if some other problem occurs. */
22160 cp_parser_commit_to_tentative_parse (parser);
22161 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22163 cp_parser_error (parser,
22164 "cannot specify %<override%> for a class");
22165 type = error_mark_node;
22166 goto out;
22168 /* Issue the error about the overly-qualified name now. */
22169 if (qualified_p)
22171 cp_parser_error (parser,
22172 "global qualification of class name is invalid");
22173 type = error_mark_node;
22174 goto out;
22176 else if (invalid_nested_name_p)
22178 cp_parser_error (parser,
22179 "qualified name does not name a class");
22180 type = error_mark_node;
22181 goto out;
22183 else if (nested_name_specifier)
22185 tree scope;
22187 /* Reject typedef-names in class heads. */
22188 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22190 error_at (type_start_token->location,
22191 "invalid class name in declaration of %qD",
22192 type);
22193 type = NULL_TREE;
22194 goto done;
22197 /* Figure out in what scope the declaration is being placed. */
22198 scope = current_scope ();
22199 /* If that scope does not contain the scope in which the
22200 class was originally declared, the program is invalid. */
22201 if (scope && !is_ancestor (scope, nested_name_specifier))
22203 if (at_namespace_scope_p ())
22204 error_at (type_start_token->location,
22205 "declaration of %qD in namespace %qD which does not "
22206 "enclose %qD",
22207 type, scope, nested_name_specifier);
22208 else
22209 error_at (type_start_token->location,
22210 "declaration of %qD in %qD which does not enclose %qD",
22211 type, scope, nested_name_specifier);
22212 type = NULL_TREE;
22213 goto done;
22215 /* [dcl.meaning]
22217 A declarator-id shall not be qualified except for the
22218 definition of a ... nested class outside of its class
22219 ... [or] the definition or explicit instantiation of a
22220 class member of a namespace outside of its namespace. */
22221 if (scope == nested_name_specifier)
22223 permerror (nested_name_specifier_token_start->location,
22224 "extra qualification not allowed");
22225 nested_name_specifier = NULL_TREE;
22226 num_templates = 0;
22229 /* An explicit-specialization must be preceded by "template <>". If
22230 it is not, try to recover gracefully. */
22231 if (at_namespace_scope_p ()
22232 && parser->num_template_parameter_lists == 0
22233 && !processing_template_parmlist
22234 && template_id_p)
22236 /* Build a location of this form:
22237 struct typename <ARGS>
22238 ^~~~~~~~~~~~~~~~~~~~~~
22239 with caret==start at the start token, and
22240 finishing at the end of the type. */
22241 location_t reported_loc
22242 = make_location (class_head_start_location,
22243 class_head_start_location,
22244 get_finish (type_start_token->location));
22245 rich_location richloc (line_table, reported_loc);
22246 richloc.add_fixit_insert_before (class_head_start_location,
22247 "template <> ");
22248 error_at_rich_loc
22249 (&richloc,
22250 "an explicit specialization must be preceded by %<template <>%>");
22251 invalid_explicit_specialization_p = true;
22252 /* Take the same action that would have been taken by
22253 cp_parser_explicit_specialization. */
22254 ++parser->num_template_parameter_lists;
22255 begin_specialization ();
22257 /* There must be no "return" statements between this point and the
22258 end of this function; set "type "to the correct return value and
22259 use "goto done;" to return. */
22260 /* Make sure that the right number of template parameters were
22261 present. */
22262 if (!cp_parser_check_template_parameters (parser, num_templates,
22263 type_start_token->location,
22264 /*declarator=*/NULL))
22266 /* If something went wrong, there is no point in even trying to
22267 process the class-definition. */
22268 type = NULL_TREE;
22269 goto done;
22272 /* Look up the type. */
22273 if (template_id_p)
22275 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22276 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22277 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22279 error_at (type_start_token->location,
22280 "function template %qD redeclared as a class template", id);
22281 type = error_mark_node;
22283 else
22285 type = TREE_TYPE (id);
22286 type = maybe_process_partial_specialization (type);
22288 if (nested_name_specifier)
22289 pushed_scope = push_scope (nested_name_specifier);
22291 else if (nested_name_specifier)
22293 tree class_type;
22295 /* Given:
22297 template <typename T> struct S { struct T };
22298 template <typename T> struct S<T>::T { };
22300 we will get a TYPENAME_TYPE when processing the definition of
22301 `S::T'. We need to resolve it to the actual type before we
22302 try to define it. */
22303 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22305 class_type = resolve_typename_type (TREE_TYPE (type),
22306 /*only_current_p=*/false);
22307 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22308 type = TYPE_NAME (class_type);
22309 else
22311 cp_parser_error (parser, "could not resolve typename type");
22312 type = error_mark_node;
22316 if (maybe_process_partial_specialization (TREE_TYPE (type))
22317 == error_mark_node)
22319 type = NULL_TREE;
22320 goto done;
22323 class_type = current_class_type;
22324 /* Enter the scope indicated by the nested-name-specifier. */
22325 pushed_scope = push_scope (nested_name_specifier);
22326 /* Get the canonical version of this type. */
22327 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22328 /* Call push_template_decl if it seems like we should be defining a
22329 template either from the template headers or the type we're
22330 defining, so that we diagnose both extra and missing headers. */
22331 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22332 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22333 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22335 type = push_template_decl (type);
22336 if (type == error_mark_node)
22338 type = NULL_TREE;
22339 goto done;
22343 type = TREE_TYPE (type);
22344 *nested_name_specifier_p = true;
22346 else /* The name is not a nested name. */
22348 /* If the class was unnamed, create a dummy name. */
22349 if (!id)
22350 id = make_anon_name ();
22351 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22352 parser->num_template_parameter_lists);
22355 /* Indicate whether this class was declared as a `class' or as a
22356 `struct'. */
22357 if (TREE_CODE (type) == RECORD_TYPE)
22358 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22359 cp_parser_check_class_key (class_key, type);
22361 /* If this type was already complete, and we see another definition,
22362 that's an error. */
22363 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22365 error_at (type_start_token->location, "redefinition of %q#T",
22366 type);
22367 inform (location_of (type), "previous definition of %q#T",
22368 type);
22369 type = NULL_TREE;
22370 goto done;
22372 else if (type == error_mark_node)
22373 type = NULL_TREE;
22375 if (type)
22377 /* Apply attributes now, before any use of the class as a template
22378 argument in its base list. */
22379 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22380 fixup_attribute_variants (type);
22383 /* We will have entered the scope containing the class; the names of
22384 base classes should be looked up in that context. For example:
22386 struct A { struct B {}; struct C; };
22387 struct A::C : B {};
22389 is valid. */
22391 /* Get the list of base-classes, if there is one. */
22392 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22394 /* PR59482: enter the class scope so that base-specifiers are looked
22395 up correctly. */
22396 if (type)
22397 pushclass (type);
22398 bases = cp_parser_base_clause (parser);
22399 /* PR59482: get out of the previously pushed class scope so that the
22400 subsequent pops pop the right thing. */
22401 if (type)
22402 popclass ();
22404 else
22405 bases = NULL_TREE;
22407 /* If we're really defining a class, process the base classes.
22408 If they're invalid, fail. */
22409 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22410 xref_basetypes (type, bases);
22412 done:
22413 /* Leave the scope given by the nested-name-specifier. We will
22414 enter the class scope itself while processing the members. */
22415 if (pushed_scope)
22416 pop_scope (pushed_scope);
22418 if (invalid_explicit_specialization_p)
22420 end_specialization ();
22421 --parser->num_template_parameter_lists;
22424 if (type)
22425 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22426 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22427 CLASSTYPE_FINAL (type) = 1;
22428 out:
22429 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22430 return type;
22433 /* Parse a class-key.
22435 class-key:
22436 class
22437 struct
22438 union
22440 Returns the kind of class-key specified, or none_type to indicate
22441 error. */
22443 static enum tag_types
22444 cp_parser_class_key (cp_parser* parser)
22446 cp_token *token;
22447 enum tag_types tag_type;
22449 /* Look for the class-key. */
22450 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22451 if (!token)
22452 return none_type;
22454 /* Check to see if the TOKEN is a class-key. */
22455 tag_type = cp_parser_token_is_class_key (token);
22456 if (!tag_type)
22457 cp_parser_error (parser, "expected class-key");
22458 return tag_type;
22461 /* Parse a type-parameter-key.
22463 type-parameter-key:
22464 class
22465 typename
22468 static void
22469 cp_parser_type_parameter_key (cp_parser* parser)
22471 /* Look for the type-parameter-key. */
22472 enum tag_types tag_type = none_type;
22473 cp_token *token = cp_lexer_peek_token (parser->lexer);
22474 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22476 cp_lexer_consume_token (parser->lexer);
22477 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22478 /* typename is not allowed in a template template parameter
22479 by the standard until C++1Z. */
22480 pedwarn (token->location, OPT_Wpedantic,
22481 "ISO C++ forbids typename key in template template parameter;"
22482 " use -std=c++1z or -std=gnu++1z");
22484 else
22485 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22487 return;
22490 /* Parse an (optional) member-specification.
22492 member-specification:
22493 member-declaration member-specification [opt]
22494 access-specifier : member-specification [opt] */
22496 static void
22497 cp_parser_member_specification_opt (cp_parser* parser)
22499 while (true)
22501 cp_token *token;
22502 enum rid keyword;
22504 /* Peek at the next token. */
22505 token = cp_lexer_peek_token (parser->lexer);
22506 /* If it's a `}', or EOF then we've seen all the members. */
22507 if (token->type == CPP_CLOSE_BRACE
22508 || token->type == CPP_EOF
22509 || token->type == CPP_PRAGMA_EOL)
22510 break;
22512 /* See if this token is a keyword. */
22513 keyword = token->keyword;
22514 switch (keyword)
22516 case RID_PUBLIC:
22517 case RID_PROTECTED:
22518 case RID_PRIVATE:
22519 /* Consume the access-specifier. */
22520 cp_lexer_consume_token (parser->lexer);
22521 /* Remember which access-specifier is active. */
22522 current_access_specifier = token->u.value;
22523 /* Look for the `:'. */
22524 cp_parser_require (parser, CPP_COLON, RT_COLON);
22525 break;
22527 default:
22528 /* Accept #pragmas at class scope. */
22529 if (token->type == CPP_PRAGMA)
22531 cp_parser_pragma (parser, pragma_member, NULL);
22532 break;
22535 /* Otherwise, the next construction must be a
22536 member-declaration. */
22537 cp_parser_member_declaration (parser);
22542 /* Parse a member-declaration.
22544 member-declaration:
22545 decl-specifier-seq [opt] member-declarator-list [opt] ;
22546 function-definition ; [opt]
22547 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22548 using-declaration
22549 template-declaration
22550 alias-declaration
22552 member-declarator-list:
22553 member-declarator
22554 member-declarator-list , member-declarator
22556 member-declarator:
22557 declarator pure-specifier [opt]
22558 declarator constant-initializer [opt]
22559 identifier [opt] : constant-expression
22561 GNU Extensions:
22563 member-declaration:
22564 __extension__ member-declaration
22566 member-declarator:
22567 declarator attributes [opt] pure-specifier [opt]
22568 declarator attributes [opt] constant-initializer [opt]
22569 identifier [opt] attributes [opt] : constant-expression
22571 C++0x Extensions:
22573 member-declaration:
22574 static_assert-declaration */
22576 static void
22577 cp_parser_member_declaration (cp_parser* parser)
22579 cp_decl_specifier_seq decl_specifiers;
22580 tree prefix_attributes;
22581 tree decl;
22582 int declares_class_or_enum;
22583 bool friend_p;
22584 cp_token *token = NULL;
22585 cp_token *decl_spec_token_start = NULL;
22586 cp_token *initializer_token_start = NULL;
22587 int saved_pedantic;
22588 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22590 /* Check for the `__extension__' keyword. */
22591 if (cp_parser_extension_opt (parser, &saved_pedantic))
22593 /* Recurse. */
22594 cp_parser_member_declaration (parser);
22595 /* Restore the old value of the PEDANTIC flag. */
22596 pedantic = saved_pedantic;
22598 return;
22601 /* Check for a template-declaration. */
22602 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22604 /* An explicit specialization here is an error condition, and we
22605 expect the specialization handler to detect and report this. */
22606 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22607 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22608 cp_parser_explicit_specialization (parser);
22609 else
22610 cp_parser_template_declaration (parser, /*member_p=*/true);
22612 return;
22614 /* Check for a template introduction. */
22615 else if (cp_parser_template_declaration_after_export (parser, true))
22616 return;
22618 /* Check for a using-declaration. */
22619 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22621 if (cxx_dialect < cxx11)
22623 /* Parse the using-declaration. */
22624 cp_parser_using_declaration (parser,
22625 /*access_declaration_p=*/false);
22626 return;
22628 else
22630 tree decl;
22631 bool alias_decl_expected;
22632 cp_parser_parse_tentatively (parser);
22633 decl = cp_parser_alias_declaration (parser);
22634 /* Note that if we actually see the '=' token after the
22635 identifier, cp_parser_alias_declaration commits the
22636 tentative parse. In that case, we really expect an
22637 alias-declaration. Otherwise, we expect a using
22638 declaration. */
22639 alias_decl_expected =
22640 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22641 cp_parser_parse_definitely (parser);
22643 if (alias_decl_expected)
22644 finish_member_declaration (decl);
22645 else
22646 cp_parser_using_declaration (parser,
22647 /*access_declaration_p=*/false);
22648 return;
22652 /* Check for @defs. */
22653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22655 tree ivar, member;
22656 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22657 ivar = ivar_chains;
22658 while (ivar)
22660 member = ivar;
22661 ivar = TREE_CHAIN (member);
22662 TREE_CHAIN (member) = NULL_TREE;
22663 finish_member_declaration (member);
22665 return;
22668 /* If the next token is `static_assert' we have a static assertion. */
22669 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22671 cp_parser_static_assert (parser, /*member_p=*/true);
22672 return;
22675 parser->colon_corrects_to_scope_p = false;
22677 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22678 goto out;
22680 /* Parse the decl-specifier-seq. */
22681 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22682 cp_parser_decl_specifier_seq (parser,
22683 CP_PARSER_FLAGS_OPTIONAL,
22684 &decl_specifiers,
22685 &declares_class_or_enum);
22686 /* Check for an invalid type-name. */
22687 if (!decl_specifiers.any_type_specifiers_p
22688 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22689 goto out;
22690 /* If there is no declarator, then the decl-specifier-seq should
22691 specify a type. */
22692 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22694 /* If there was no decl-specifier-seq, and the next token is a
22695 `;', then we have something like:
22697 struct S { ; };
22699 [class.mem]
22701 Each member-declaration shall declare at least one member
22702 name of the class. */
22703 if (!decl_specifiers.any_specifiers_p)
22705 cp_token *token = cp_lexer_peek_token (parser->lexer);
22706 if (!in_system_header_at (token->location))
22707 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22709 else
22711 tree type;
22713 /* See if this declaration is a friend. */
22714 friend_p = cp_parser_friend_p (&decl_specifiers);
22715 /* If there were decl-specifiers, check to see if there was
22716 a class-declaration. */
22717 type = check_tag_decl (&decl_specifiers,
22718 /*explicit_type_instantiation_p=*/false);
22719 /* Nested classes have already been added to the class, but
22720 a `friend' needs to be explicitly registered. */
22721 if (friend_p)
22723 /* If the `friend' keyword was present, the friend must
22724 be introduced with a class-key. */
22725 if (!declares_class_or_enum && cxx_dialect < cxx11)
22726 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22727 "in C++03 a class-key must be used "
22728 "when declaring a friend");
22729 /* In this case:
22731 template <typename T> struct A {
22732 friend struct A<T>::B;
22735 A<T>::B will be represented by a TYPENAME_TYPE, and
22736 therefore not recognized by check_tag_decl. */
22737 if (!type)
22739 type = decl_specifiers.type;
22740 if (type && TREE_CODE (type) == TYPE_DECL)
22741 type = TREE_TYPE (type);
22743 if (!type || !TYPE_P (type))
22744 error_at (decl_spec_token_start->location,
22745 "friend declaration does not name a class or "
22746 "function");
22747 else
22748 make_friend_class (current_class_type, type,
22749 /*complain=*/true);
22751 /* If there is no TYPE, an error message will already have
22752 been issued. */
22753 else if (!type || type == error_mark_node)
22755 /* An anonymous aggregate has to be handled specially; such
22756 a declaration really declares a data member (with a
22757 particular type), as opposed to a nested class. */
22758 else if (ANON_AGGR_TYPE_P (type))
22760 /* C++11 9.5/6. */
22761 if (decl_specifiers.storage_class != sc_none)
22762 error_at (decl_spec_token_start->location,
22763 "a storage class on an anonymous aggregate "
22764 "in class scope is not allowed");
22766 /* Remove constructors and such from TYPE, now that we
22767 know it is an anonymous aggregate. */
22768 fixup_anonymous_aggr (type);
22769 /* And make the corresponding data member. */
22770 decl = build_decl (decl_spec_token_start->location,
22771 FIELD_DECL, NULL_TREE, type);
22772 /* Add it to the class. */
22773 finish_member_declaration (decl);
22775 else
22776 cp_parser_check_access_in_redeclaration
22777 (TYPE_NAME (type),
22778 decl_spec_token_start->location);
22781 else
22783 bool assume_semicolon = false;
22785 /* Clear attributes from the decl_specifiers but keep them
22786 around as prefix attributes that apply them to the entity
22787 being declared. */
22788 prefix_attributes = decl_specifiers.attributes;
22789 decl_specifiers.attributes = NULL_TREE;
22791 /* See if these declarations will be friends. */
22792 friend_p = cp_parser_friend_p (&decl_specifiers);
22794 /* Keep going until we hit the `;' at the end of the
22795 declaration. */
22796 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22798 tree attributes = NULL_TREE;
22799 tree first_attribute;
22801 /* Peek at the next token. */
22802 token = cp_lexer_peek_token (parser->lexer);
22804 /* Check for a bitfield declaration. */
22805 if (token->type == CPP_COLON
22806 || (token->type == CPP_NAME
22807 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22808 == CPP_COLON))
22810 tree identifier;
22811 tree width;
22813 /* Get the name of the bitfield. Note that we cannot just
22814 check TOKEN here because it may have been invalidated by
22815 the call to cp_lexer_peek_nth_token above. */
22816 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22817 identifier = cp_parser_identifier (parser);
22818 else
22819 identifier = NULL_TREE;
22821 /* Consume the `:' token. */
22822 cp_lexer_consume_token (parser->lexer);
22823 /* Get the width of the bitfield. */
22824 width
22825 = cp_parser_constant_expression (parser);
22827 /* Look for attributes that apply to the bitfield. */
22828 attributes = cp_parser_attributes_opt (parser);
22829 /* Remember which attributes are prefix attributes and
22830 which are not. */
22831 first_attribute = attributes;
22832 /* Combine the attributes. */
22833 attributes = chainon (prefix_attributes, attributes);
22835 /* Create the bitfield declaration. */
22836 decl = grokbitfield (identifier
22837 ? make_id_declarator (NULL_TREE,
22838 identifier,
22839 sfk_none)
22840 : NULL,
22841 &decl_specifiers,
22842 width,
22843 attributes);
22845 else
22847 cp_declarator *declarator;
22848 tree initializer;
22849 tree asm_specification;
22850 int ctor_dtor_or_conv_p;
22852 /* Parse the declarator. */
22853 declarator
22854 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22855 &ctor_dtor_or_conv_p,
22856 /*parenthesized_p=*/NULL,
22857 /*member_p=*/true,
22858 friend_p);
22860 /* If something went wrong parsing the declarator, make sure
22861 that we at least consume some tokens. */
22862 if (declarator == cp_error_declarator)
22864 /* Skip to the end of the statement. */
22865 cp_parser_skip_to_end_of_statement (parser);
22866 /* If the next token is not a semicolon, that is
22867 probably because we just skipped over the body of
22868 a function. So, we consume a semicolon if
22869 present, but do not issue an error message if it
22870 is not present. */
22871 if (cp_lexer_next_token_is (parser->lexer,
22872 CPP_SEMICOLON))
22873 cp_lexer_consume_token (parser->lexer);
22874 goto out;
22877 if (declares_class_or_enum & 2)
22878 cp_parser_check_for_definition_in_return_type
22879 (declarator, decl_specifiers.type,
22880 decl_specifiers.locations[ds_type_spec]);
22882 /* Look for an asm-specification. */
22883 asm_specification = cp_parser_asm_specification_opt (parser);
22884 /* Look for attributes that apply to the declaration. */
22885 attributes = cp_parser_attributes_opt (parser);
22886 /* Remember which attributes are prefix attributes and
22887 which are not. */
22888 first_attribute = attributes;
22889 /* Combine the attributes. */
22890 attributes = chainon (prefix_attributes, attributes);
22892 /* If it's an `=', then we have a constant-initializer or a
22893 pure-specifier. It is not correct to parse the
22894 initializer before registering the member declaration
22895 since the member declaration should be in scope while
22896 its initializer is processed. However, the rest of the
22897 front end does not yet provide an interface that allows
22898 us to handle this correctly. */
22899 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22901 /* In [class.mem]:
22903 A pure-specifier shall be used only in the declaration of
22904 a virtual function.
22906 A member-declarator can contain a constant-initializer
22907 only if it declares a static member of integral or
22908 enumeration type.
22910 Therefore, if the DECLARATOR is for a function, we look
22911 for a pure-specifier; otherwise, we look for a
22912 constant-initializer. When we call `grokfield', it will
22913 perform more stringent semantics checks. */
22914 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22915 if (function_declarator_p (declarator)
22916 || (decl_specifiers.type
22917 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22918 && declarator->kind == cdk_id
22919 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22920 == FUNCTION_TYPE)))
22921 initializer = cp_parser_pure_specifier (parser);
22922 else if (decl_specifiers.storage_class != sc_static)
22923 initializer = cp_parser_save_nsdmi (parser);
22924 else if (cxx_dialect >= cxx11)
22926 bool nonconst;
22927 /* Don't require a constant rvalue in C++11, since we
22928 might want a reference constant. We'll enforce
22929 constancy later. */
22930 cp_lexer_consume_token (parser->lexer);
22931 /* Parse the initializer. */
22932 initializer = cp_parser_initializer_clause (parser,
22933 &nonconst);
22935 else
22936 /* Parse the initializer. */
22937 initializer = cp_parser_constant_initializer (parser);
22939 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22940 && !function_declarator_p (declarator))
22942 bool x;
22943 if (decl_specifiers.storage_class != sc_static)
22944 initializer = cp_parser_save_nsdmi (parser);
22945 else
22946 initializer = cp_parser_initializer (parser, &x, &x);
22948 /* Otherwise, there is no initializer. */
22949 else
22950 initializer = NULL_TREE;
22952 /* See if we are probably looking at a function
22953 definition. We are certainly not looking at a
22954 member-declarator. Calling `grokfield' has
22955 side-effects, so we must not do it unless we are sure
22956 that we are looking at a member-declarator. */
22957 if (cp_parser_token_starts_function_definition_p
22958 (cp_lexer_peek_token (parser->lexer)))
22960 /* The grammar does not allow a pure-specifier to be
22961 used when a member function is defined. (It is
22962 possible that this fact is an oversight in the
22963 standard, since a pure function may be defined
22964 outside of the class-specifier. */
22965 if (initializer && initializer_token_start)
22966 error_at (initializer_token_start->location,
22967 "pure-specifier on function-definition");
22968 decl = cp_parser_save_member_function_body (parser,
22969 &decl_specifiers,
22970 declarator,
22971 attributes);
22972 if (parser->fully_implicit_function_template_p)
22973 decl = finish_fully_implicit_template (parser, decl);
22974 /* If the member was not a friend, declare it here. */
22975 if (!friend_p)
22976 finish_member_declaration (decl);
22977 /* Peek at the next token. */
22978 token = cp_lexer_peek_token (parser->lexer);
22979 /* If the next token is a semicolon, consume it. */
22980 if (token->type == CPP_SEMICOLON)
22981 cp_lexer_consume_token (parser->lexer);
22982 goto out;
22984 else
22985 if (declarator->kind == cdk_function)
22986 declarator->id_loc = token->location;
22987 /* Create the declaration. */
22988 decl = grokfield (declarator, &decl_specifiers,
22989 initializer, /*init_const_expr_p=*/true,
22990 asm_specification, attributes);
22991 if (parser->fully_implicit_function_template_p)
22993 if (friend_p)
22994 finish_fully_implicit_template (parser, 0);
22995 else
22996 decl = finish_fully_implicit_template (parser, decl);
23000 cp_finalize_omp_declare_simd (parser, decl);
23001 cp_finalize_oacc_routine (parser, decl, false);
23003 /* Reset PREFIX_ATTRIBUTES. */
23004 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23005 attributes = TREE_CHAIN (attributes);
23006 if (attributes)
23007 TREE_CHAIN (attributes) = NULL_TREE;
23009 /* If there is any qualification still in effect, clear it
23010 now; we will be starting fresh with the next declarator. */
23011 parser->scope = NULL_TREE;
23012 parser->qualifying_scope = NULL_TREE;
23013 parser->object_scope = NULL_TREE;
23014 /* If it's a `,', then there are more declarators. */
23015 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23017 cp_lexer_consume_token (parser->lexer);
23018 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23020 cp_token *token = cp_lexer_previous_token (parser->lexer);
23021 error_at (token->location,
23022 "stray %<,%> at end of member declaration");
23025 /* If the next token isn't a `;', then we have a parse error. */
23026 else if (cp_lexer_next_token_is_not (parser->lexer,
23027 CPP_SEMICOLON))
23029 /* The next token might be a ways away from where the
23030 actual semicolon is missing. Find the previous token
23031 and use that for our error position. */
23032 cp_token *token = cp_lexer_previous_token (parser->lexer);
23033 error_at (token->location,
23034 "expected %<;%> at end of member declaration");
23036 /* Assume that the user meant to provide a semicolon. If
23037 we were to cp_parser_skip_to_end_of_statement, we might
23038 skip to a semicolon inside a member function definition
23039 and issue nonsensical error messages. */
23040 assume_semicolon = true;
23043 if (decl)
23045 /* Add DECL to the list of members. */
23046 if (!friend_p
23047 /* Explicitly include, eg, NSDMIs, for better error
23048 recovery (c++/58650). */
23049 || !DECL_DECLARES_FUNCTION_P (decl))
23050 finish_member_declaration (decl);
23052 if (TREE_CODE (decl) == FUNCTION_DECL)
23053 cp_parser_save_default_args (parser, decl);
23054 else if (TREE_CODE (decl) == FIELD_DECL
23055 && !DECL_C_BIT_FIELD (decl)
23056 && DECL_INITIAL (decl))
23057 /* Add DECL to the queue of NSDMI to be parsed later. */
23058 vec_safe_push (unparsed_nsdmis, decl);
23061 if (assume_semicolon)
23062 goto out;
23066 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23067 out:
23068 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23071 /* Parse a pure-specifier.
23073 pure-specifier:
23076 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23077 Otherwise, ERROR_MARK_NODE is returned. */
23079 static tree
23080 cp_parser_pure_specifier (cp_parser* parser)
23082 cp_token *token;
23084 /* Look for the `=' token. */
23085 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23086 return error_mark_node;
23087 /* Look for the `0' token. */
23088 token = cp_lexer_peek_token (parser->lexer);
23090 if (token->type == CPP_EOF
23091 || token->type == CPP_PRAGMA_EOL)
23092 return error_mark_node;
23094 cp_lexer_consume_token (parser->lexer);
23096 /* Accept = default or = delete in c++0x mode. */
23097 if (token->keyword == RID_DEFAULT
23098 || token->keyword == RID_DELETE)
23100 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23101 return token->u.value;
23104 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23105 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23107 cp_parser_error (parser,
23108 "invalid pure specifier (only %<= 0%> is allowed)");
23109 cp_parser_skip_to_end_of_statement (parser);
23110 return error_mark_node;
23112 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23114 error_at (token->location, "templates may not be %<virtual%>");
23115 return error_mark_node;
23118 return integer_zero_node;
23121 /* Parse a constant-initializer.
23123 constant-initializer:
23124 = constant-expression
23126 Returns a representation of the constant-expression. */
23128 static tree
23129 cp_parser_constant_initializer (cp_parser* parser)
23131 /* Look for the `=' token. */
23132 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23133 return error_mark_node;
23135 /* It is invalid to write:
23137 struct S { static const int i = { 7 }; };
23140 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23142 cp_parser_error (parser,
23143 "a brace-enclosed initializer is not allowed here");
23144 /* Consume the opening brace. */
23145 cp_lexer_consume_token (parser->lexer);
23146 /* Skip the initializer. */
23147 cp_parser_skip_to_closing_brace (parser);
23148 /* Look for the trailing `}'. */
23149 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23151 return error_mark_node;
23154 return cp_parser_constant_expression (parser);
23157 /* Derived classes [gram.class.derived] */
23159 /* Parse a base-clause.
23161 base-clause:
23162 : base-specifier-list
23164 base-specifier-list:
23165 base-specifier ... [opt]
23166 base-specifier-list , base-specifier ... [opt]
23168 Returns a TREE_LIST representing the base-classes, in the order in
23169 which they were declared. The representation of each node is as
23170 described by cp_parser_base_specifier.
23172 In the case that no bases are specified, this function will return
23173 NULL_TREE, not ERROR_MARK_NODE. */
23175 static tree
23176 cp_parser_base_clause (cp_parser* parser)
23178 tree bases = NULL_TREE;
23180 /* Look for the `:' that begins the list. */
23181 cp_parser_require (parser, CPP_COLON, RT_COLON);
23183 /* Scan the base-specifier-list. */
23184 while (true)
23186 cp_token *token;
23187 tree base;
23188 bool pack_expansion_p = false;
23190 /* Look for the base-specifier. */
23191 base = cp_parser_base_specifier (parser);
23192 /* Look for the (optional) ellipsis. */
23193 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23195 /* Consume the `...'. */
23196 cp_lexer_consume_token (parser->lexer);
23198 pack_expansion_p = true;
23201 /* Add BASE to the front of the list. */
23202 if (base && base != error_mark_node)
23204 if (pack_expansion_p)
23205 /* Make this a pack expansion type. */
23206 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23208 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23210 TREE_CHAIN (base) = bases;
23211 bases = base;
23214 /* Peek at the next token. */
23215 token = cp_lexer_peek_token (parser->lexer);
23216 /* If it's not a comma, then the list is complete. */
23217 if (token->type != CPP_COMMA)
23218 break;
23219 /* Consume the `,'. */
23220 cp_lexer_consume_token (parser->lexer);
23223 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23224 base class had a qualified name. However, the next name that
23225 appears is certainly not qualified. */
23226 parser->scope = NULL_TREE;
23227 parser->qualifying_scope = NULL_TREE;
23228 parser->object_scope = NULL_TREE;
23230 return nreverse (bases);
23233 /* Parse a base-specifier.
23235 base-specifier:
23236 :: [opt] nested-name-specifier [opt] class-name
23237 virtual access-specifier [opt] :: [opt] nested-name-specifier
23238 [opt] class-name
23239 access-specifier virtual [opt] :: [opt] nested-name-specifier
23240 [opt] class-name
23242 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23243 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23244 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23245 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23247 static tree
23248 cp_parser_base_specifier (cp_parser* parser)
23250 cp_token *token;
23251 bool done = false;
23252 bool virtual_p = false;
23253 bool duplicate_virtual_error_issued_p = false;
23254 bool duplicate_access_error_issued_p = false;
23255 bool class_scope_p, template_p;
23256 tree access = access_default_node;
23257 tree type;
23259 /* Process the optional `virtual' and `access-specifier'. */
23260 while (!done)
23262 /* Peek at the next token. */
23263 token = cp_lexer_peek_token (parser->lexer);
23264 /* Process `virtual'. */
23265 switch (token->keyword)
23267 case RID_VIRTUAL:
23268 /* If `virtual' appears more than once, issue an error. */
23269 if (virtual_p && !duplicate_virtual_error_issued_p)
23271 cp_parser_error (parser,
23272 "%<virtual%> specified more than once in base-specified");
23273 duplicate_virtual_error_issued_p = true;
23276 virtual_p = true;
23278 /* Consume the `virtual' token. */
23279 cp_lexer_consume_token (parser->lexer);
23281 break;
23283 case RID_PUBLIC:
23284 case RID_PROTECTED:
23285 case RID_PRIVATE:
23286 /* If more than one access specifier appears, issue an
23287 error. */
23288 if (access != access_default_node
23289 && !duplicate_access_error_issued_p)
23291 cp_parser_error (parser,
23292 "more than one access specifier in base-specified");
23293 duplicate_access_error_issued_p = true;
23296 access = ridpointers[(int) token->keyword];
23298 /* Consume the access-specifier. */
23299 cp_lexer_consume_token (parser->lexer);
23301 break;
23303 default:
23304 done = true;
23305 break;
23308 /* It is not uncommon to see programs mechanically, erroneously, use
23309 the 'typename' keyword to denote (dependent) qualified types
23310 as base classes. */
23311 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23313 token = cp_lexer_peek_token (parser->lexer);
23314 if (!processing_template_decl)
23315 error_at (token->location,
23316 "keyword %<typename%> not allowed outside of templates");
23317 else
23318 error_at (token->location,
23319 "keyword %<typename%> not allowed in this context "
23320 "(the base class is implicitly a type)");
23321 cp_lexer_consume_token (parser->lexer);
23324 /* Look for the optional `::' operator. */
23325 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23326 /* Look for the nested-name-specifier. The simplest way to
23327 implement:
23329 [temp.res]
23331 The keyword `typename' is not permitted in a base-specifier or
23332 mem-initializer; in these contexts a qualified name that
23333 depends on a template-parameter is implicitly assumed to be a
23334 type name.
23336 is to pretend that we have seen the `typename' keyword at this
23337 point. */
23338 cp_parser_nested_name_specifier_opt (parser,
23339 /*typename_keyword_p=*/true,
23340 /*check_dependency_p=*/true,
23341 typename_type,
23342 /*is_declaration=*/true);
23343 /* If the base class is given by a qualified name, assume that names
23344 we see are type names or templates, as appropriate. */
23345 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23346 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23348 if (!parser->scope
23349 && cp_lexer_next_token_is_decltype (parser->lexer))
23350 /* DR 950 allows decltype as a base-specifier. */
23351 type = cp_parser_decltype (parser);
23352 else
23354 /* Otherwise, look for the class-name. */
23355 type = cp_parser_class_name (parser,
23356 class_scope_p,
23357 template_p,
23358 typename_type,
23359 /*check_dependency_p=*/true,
23360 /*class_head_p=*/false,
23361 /*is_declaration=*/true);
23362 type = TREE_TYPE (type);
23365 if (type == error_mark_node)
23366 return error_mark_node;
23368 return finish_base_specifier (type, access, virtual_p);
23371 /* Exception handling [gram.exception] */
23373 /* Parse an (optional) noexcept-specification.
23375 noexcept-specification:
23376 noexcept ( constant-expression ) [opt]
23378 If no noexcept-specification is present, returns NULL_TREE.
23379 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23380 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23381 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23382 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23383 in which case a boolean condition is returned instead. */
23385 static tree
23386 cp_parser_noexcept_specification_opt (cp_parser* parser,
23387 bool require_constexpr,
23388 bool* consumed_expr,
23389 bool return_cond)
23391 cp_token *token;
23392 const char *saved_message;
23394 /* Peek at the next token. */
23395 token = cp_lexer_peek_token (parser->lexer);
23397 /* Is it a noexcept-specification? */
23398 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23400 tree expr;
23401 cp_lexer_consume_token (parser->lexer);
23403 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23405 cp_lexer_consume_token (parser->lexer);
23407 if (require_constexpr)
23409 /* Types may not be defined in an exception-specification. */
23410 saved_message = parser->type_definition_forbidden_message;
23411 parser->type_definition_forbidden_message
23412 = G_("types may not be defined in an exception-specification");
23414 expr = cp_parser_constant_expression (parser);
23416 /* Restore the saved message. */
23417 parser->type_definition_forbidden_message = saved_message;
23419 else
23421 expr = cp_parser_expression (parser);
23422 *consumed_expr = true;
23425 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23427 else
23429 expr = boolean_true_node;
23430 if (!require_constexpr)
23431 *consumed_expr = false;
23434 /* We cannot build a noexcept-spec right away because this will check
23435 that expr is a constexpr. */
23436 if (!return_cond)
23437 return build_noexcept_spec (expr, tf_warning_or_error);
23438 else
23439 return expr;
23441 else
23442 return NULL_TREE;
23445 /* Parse an (optional) exception-specification.
23447 exception-specification:
23448 throw ( type-id-list [opt] )
23450 Returns a TREE_LIST representing the exception-specification. The
23451 TREE_VALUE of each node is a type. */
23453 static tree
23454 cp_parser_exception_specification_opt (cp_parser* parser)
23456 cp_token *token;
23457 tree type_id_list;
23458 const char *saved_message;
23460 /* Peek at the next token. */
23461 token = cp_lexer_peek_token (parser->lexer);
23463 /* Is it a noexcept-specification? */
23464 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23465 false);
23466 if (type_id_list != NULL_TREE)
23467 return type_id_list;
23469 /* If it's not `throw', then there's no exception-specification. */
23470 if (!cp_parser_is_keyword (token, RID_THROW))
23471 return NULL_TREE;
23473 #if 0
23474 /* Enable this once a lot of code has transitioned to noexcept? */
23475 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23476 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23477 "deprecated in C++0x; use %<noexcept%> instead");
23478 #endif
23480 /* Consume the `throw'. */
23481 cp_lexer_consume_token (parser->lexer);
23483 /* Look for the `('. */
23484 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23486 /* Peek at the next token. */
23487 token = cp_lexer_peek_token (parser->lexer);
23488 /* If it's not a `)', then there is a type-id-list. */
23489 if (token->type != CPP_CLOSE_PAREN)
23491 /* Types may not be defined in an exception-specification. */
23492 saved_message = parser->type_definition_forbidden_message;
23493 parser->type_definition_forbidden_message
23494 = G_("types may not be defined in an exception-specification");
23495 /* Parse the type-id-list. */
23496 type_id_list = cp_parser_type_id_list (parser);
23497 /* Restore the saved message. */
23498 parser->type_definition_forbidden_message = saved_message;
23500 else
23501 type_id_list = empty_except_spec;
23503 /* Look for the `)'. */
23504 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23506 return type_id_list;
23509 /* Parse an (optional) type-id-list.
23511 type-id-list:
23512 type-id ... [opt]
23513 type-id-list , type-id ... [opt]
23515 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23516 in the order that the types were presented. */
23518 static tree
23519 cp_parser_type_id_list (cp_parser* parser)
23521 tree types = NULL_TREE;
23523 while (true)
23525 cp_token *token;
23526 tree type;
23528 token = cp_lexer_peek_token (parser->lexer);
23530 /* Get the next type-id. */
23531 type = cp_parser_type_id (parser);
23532 /* Check for invalid 'auto'. */
23533 if (flag_concepts && type_uses_auto (type))
23535 error_at (token->location,
23536 "invalid use of %<auto%> in exception-specification");
23537 type = error_mark_node;
23539 /* Parse the optional ellipsis. */
23540 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23542 /* Consume the `...'. */
23543 cp_lexer_consume_token (parser->lexer);
23545 /* Turn the type into a pack expansion expression. */
23546 type = make_pack_expansion (type);
23548 /* Add it to the list. */
23549 types = add_exception_specifier (types, type, /*complain=*/1);
23550 /* Peek at the next token. */
23551 token = cp_lexer_peek_token (parser->lexer);
23552 /* If it is not a `,', we are done. */
23553 if (token->type != CPP_COMMA)
23554 break;
23555 /* Consume the `,'. */
23556 cp_lexer_consume_token (parser->lexer);
23559 return nreverse (types);
23562 /* Parse a try-block.
23564 try-block:
23565 try compound-statement handler-seq */
23567 static tree
23568 cp_parser_try_block (cp_parser* parser)
23570 tree try_block;
23572 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23573 if (parser->in_function_body
23574 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23575 error ("%<try%> in %<constexpr%> function");
23577 try_block = begin_try_block ();
23578 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23579 finish_try_block (try_block);
23580 cp_parser_handler_seq (parser);
23581 finish_handler_sequence (try_block);
23583 return try_block;
23586 /* Parse a function-try-block.
23588 function-try-block:
23589 try ctor-initializer [opt] function-body handler-seq */
23591 static bool
23592 cp_parser_function_try_block (cp_parser* parser)
23594 tree compound_stmt;
23595 tree try_block;
23596 bool ctor_initializer_p;
23598 /* Look for the `try' keyword. */
23599 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23600 return false;
23601 /* Let the rest of the front end know where we are. */
23602 try_block = begin_function_try_block (&compound_stmt);
23603 /* Parse the function-body. */
23604 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23605 (parser, /*in_function_try_block=*/true);
23606 /* We're done with the `try' part. */
23607 finish_function_try_block (try_block);
23608 /* Parse the handlers. */
23609 cp_parser_handler_seq (parser);
23610 /* We're done with the handlers. */
23611 finish_function_handler_sequence (try_block, compound_stmt);
23613 return ctor_initializer_p;
23616 /* Parse a handler-seq.
23618 handler-seq:
23619 handler handler-seq [opt] */
23621 static void
23622 cp_parser_handler_seq (cp_parser* parser)
23624 while (true)
23626 cp_token *token;
23628 /* Parse the handler. */
23629 cp_parser_handler (parser);
23630 /* Peek at the next token. */
23631 token = cp_lexer_peek_token (parser->lexer);
23632 /* If it's not `catch' then there are no more handlers. */
23633 if (!cp_parser_is_keyword (token, RID_CATCH))
23634 break;
23638 /* Parse a handler.
23640 handler:
23641 catch ( exception-declaration ) compound-statement */
23643 static void
23644 cp_parser_handler (cp_parser* parser)
23646 tree handler;
23647 tree declaration;
23649 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23650 handler = begin_handler ();
23651 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23652 declaration = cp_parser_exception_declaration (parser);
23653 finish_handler_parms (declaration, handler);
23654 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23655 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23656 finish_handler (handler);
23659 /* Parse an exception-declaration.
23661 exception-declaration:
23662 type-specifier-seq declarator
23663 type-specifier-seq abstract-declarator
23664 type-specifier-seq
23667 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23668 ellipsis variant is used. */
23670 static tree
23671 cp_parser_exception_declaration (cp_parser* parser)
23673 cp_decl_specifier_seq type_specifiers;
23674 cp_declarator *declarator;
23675 const char *saved_message;
23677 /* If it's an ellipsis, it's easy to handle. */
23678 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23680 /* Consume the `...' token. */
23681 cp_lexer_consume_token (parser->lexer);
23682 return NULL_TREE;
23685 /* Types may not be defined in exception-declarations. */
23686 saved_message = parser->type_definition_forbidden_message;
23687 parser->type_definition_forbidden_message
23688 = G_("types may not be defined in exception-declarations");
23690 /* Parse the type-specifier-seq. */
23691 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23692 /*is_trailing_return=*/false,
23693 &type_specifiers);
23694 /* If it's a `)', then there is no declarator. */
23695 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23696 declarator = NULL;
23697 else
23698 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23699 /*ctor_dtor_or_conv_p=*/NULL,
23700 /*parenthesized_p=*/NULL,
23701 /*member_p=*/false,
23702 /*friend_p=*/false);
23704 /* Restore the saved message. */
23705 parser->type_definition_forbidden_message = saved_message;
23707 if (!type_specifiers.any_specifiers_p)
23708 return error_mark_node;
23710 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23713 /* Parse a throw-expression.
23715 throw-expression:
23716 throw assignment-expression [opt]
23718 Returns a THROW_EXPR representing the throw-expression. */
23720 static tree
23721 cp_parser_throw_expression (cp_parser* parser)
23723 tree expression;
23724 cp_token* token;
23726 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23727 token = cp_lexer_peek_token (parser->lexer);
23728 /* Figure out whether or not there is an assignment-expression
23729 following the "throw" keyword. */
23730 if (token->type == CPP_COMMA
23731 || token->type == CPP_SEMICOLON
23732 || token->type == CPP_CLOSE_PAREN
23733 || token->type == CPP_CLOSE_SQUARE
23734 || token->type == CPP_CLOSE_BRACE
23735 || token->type == CPP_COLON)
23736 expression = NULL_TREE;
23737 else
23738 expression = cp_parser_assignment_expression (parser);
23740 return build_throw (expression);
23743 /* GNU Extensions */
23745 /* Parse an (optional) asm-specification.
23747 asm-specification:
23748 asm ( string-literal )
23750 If the asm-specification is present, returns a STRING_CST
23751 corresponding to the string-literal. Otherwise, returns
23752 NULL_TREE. */
23754 static tree
23755 cp_parser_asm_specification_opt (cp_parser* parser)
23757 cp_token *token;
23758 tree asm_specification;
23760 /* Peek at the next token. */
23761 token = cp_lexer_peek_token (parser->lexer);
23762 /* If the next token isn't the `asm' keyword, then there's no
23763 asm-specification. */
23764 if (!cp_parser_is_keyword (token, RID_ASM))
23765 return NULL_TREE;
23767 /* Consume the `asm' token. */
23768 cp_lexer_consume_token (parser->lexer);
23769 /* Look for the `('. */
23770 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23772 /* Look for the string-literal. */
23773 asm_specification = cp_parser_string_literal (parser, false, false);
23775 /* Look for the `)'. */
23776 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23778 return asm_specification;
23781 /* Parse an asm-operand-list.
23783 asm-operand-list:
23784 asm-operand
23785 asm-operand-list , asm-operand
23787 asm-operand:
23788 string-literal ( expression )
23789 [ string-literal ] string-literal ( expression )
23791 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23792 each node is the expression. The TREE_PURPOSE is itself a
23793 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23794 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23795 is a STRING_CST for the string literal before the parenthesis. Returns
23796 ERROR_MARK_NODE if any of the operands are invalid. */
23798 static tree
23799 cp_parser_asm_operand_list (cp_parser* parser)
23801 tree asm_operands = NULL_TREE;
23802 bool invalid_operands = false;
23804 while (true)
23806 tree string_literal;
23807 tree expression;
23808 tree name;
23810 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23812 /* Consume the `[' token. */
23813 cp_lexer_consume_token (parser->lexer);
23814 /* Read the operand name. */
23815 name = cp_parser_identifier (parser);
23816 if (name != error_mark_node)
23817 name = build_string (IDENTIFIER_LENGTH (name),
23818 IDENTIFIER_POINTER (name));
23819 /* Look for the closing `]'. */
23820 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23822 else
23823 name = NULL_TREE;
23824 /* Look for the string-literal. */
23825 string_literal = cp_parser_string_literal (parser, false, false);
23827 /* Look for the `('. */
23828 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23829 /* Parse the expression. */
23830 expression = cp_parser_expression (parser);
23831 /* Look for the `)'. */
23832 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23834 if (name == error_mark_node
23835 || string_literal == error_mark_node
23836 || expression == error_mark_node)
23837 invalid_operands = true;
23839 /* Add this operand to the list. */
23840 asm_operands = tree_cons (build_tree_list (name, string_literal),
23841 expression,
23842 asm_operands);
23843 /* If the next token is not a `,', there are no more
23844 operands. */
23845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23846 break;
23847 /* Consume the `,'. */
23848 cp_lexer_consume_token (parser->lexer);
23851 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23854 /* Parse an asm-clobber-list.
23856 asm-clobber-list:
23857 string-literal
23858 asm-clobber-list , string-literal
23860 Returns a TREE_LIST, indicating the clobbers in the order that they
23861 appeared. The TREE_VALUE of each node is a STRING_CST. */
23863 static tree
23864 cp_parser_asm_clobber_list (cp_parser* parser)
23866 tree clobbers = NULL_TREE;
23868 while (true)
23870 tree string_literal;
23872 /* Look for the string literal. */
23873 string_literal = cp_parser_string_literal (parser, false, false);
23874 /* Add it to the list. */
23875 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23876 /* If the next token is not a `,', then the list is
23877 complete. */
23878 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23879 break;
23880 /* Consume the `,' token. */
23881 cp_lexer_consume_token (parser->lexer);
23884 return clobbers;
23887 /* Parse an asm-label-list.
23889 asm-label-list:
23890 identifier
23891 asm-label-list , identifier
23893 Returns a TREE_LIST, indicating the labels in the order that they
23894 appeared. The TREE_VALUE of each node is a label. */
23896 static tree
23897 cp_parser_asm_label_list (cp_parser* parser)
23899 tree labels = NULL_TREE;
23901 while (true)
23903 tree identifier, label, name;
23905 /* Look for the identifier. */
23906 identifier = cp_parser_identifier (parser);
23907 if (!error_operand_p (identifier))
23909 label = lookup_label (identifier);
23910 if (TREE_CODE (label) == LABEL_DECL)
23912 TREE_USED (label) = 1;
23913 check_goto (label);
23914 name = build_string (IDENTIFIER_LENGTH (identifier),
23915 IDENTIFIER_POINTER (identifier));
23916 labels = tree_cons (name, label, labels);
23919 /* If the next token is not a `,', then the list is
23920 complete. */
23921 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23922 break;
23923 /* Consume the `,' token. */
23924 cp_lexer_consume_token (parser->lexer);
23927 return nreverse (labels);
23930 /* Return TRUE iff the next tokens in the stream are possibly the
23931 beginning of a GNU extension attribute. */
23933 static bool
23934 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23936 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23939 /* Return TRUE iff the next tokens in the stream are possibly the
23940 beginning of a standard C++-11 attribute specifier. */
23942 static bool
23943 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23945 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23948 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23949 beginning of a standard C++-11 attribute specifier. */
23951 static bool
23952 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23954 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23956 return (cxx_dialect >= cxx11
23957 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23958 || (token->type == CPP_OPEN_SQUARE
23959 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23960 && token->type == CPP_OPEN_SQUARE)));
23963 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23964 beginning of a GNU extension attribute. */
23966 static bool
23967 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23969 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23971 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23974 /* Return true iff the next tokens can be the beginning of either a
23975 GNU attribute list, or a standard C++11 attribute sequence. */
23977 static bool
23978 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23980 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23981 || cp_next_tokens_can_be_std_attribute_p (parser));
23984 /* Return true iff the next Nth tokens can be the beginning of either
23985 a GNU attribute list, or a standard C++11 attribute sequence. */
23987 static bool
23988 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23990 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23991 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23994 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23995 of GNU attributes, or return NULL. */
23997 static tree
23998 cp_parser_attributes_opt (cp_parser *parser)
24000 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24001 return cp_parser_gnu_attributes_opt (parser);
24002 return cp_parser_std_attribute_spec_seq (parser);
24005 #define CILK_SIMD_FN_CLAUSE_MASK \
24006 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24007 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24008 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24009 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24010 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24012 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24013 vector [(<clauses>)] */
24015 static void
24016 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24018 bool first_p = parser->cilk_simd_fn_info == NULL;
24019 cp_token *token = v_token;
24020 if (first_p)
24022 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24023 parser->cilk_simd_fn_info->error_seen = false;
24024 parser->cilk_simd_fn_info->fndecl_seen = false;
24025 parser->cilk_simd_fn_info->tokens = vNULL;
24026 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24028 int paren_scope = 0;
24029 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24031 cp_lexer_consume_token (parser->lexer);
24032 v_token = cp_lexer_peek_token (parser->lexer);
24033 paren_scope++;
24035 while (paren_scope > 0)
24037 token = cp_lexer_peek_token (parser->lexer);
24038 if (token->type == CPP_OPEN_PAREN)
24039 paren_scope++;
24040 else if (token->type == CPP_CLOSE_PAREN)
24041 paren_scope--;
24042 /* Do not push the last ')' */
24043 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24044 cp_lexer_consume_token (parser->lexer);
24047 token->type = CPP_PRAGMA_EOL;
24048 parser->lexer->next_token = token;
24049 cp_lexer_consume_token (parser->lexer);
24051 struct cp_token_cache *cp
24052 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24053 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24056 /* Parse an (optional) series of attributes.
24058 attributes:
24059 attributes attribute
24061 attribute:
24062 __attribute__ (( attribute-list [opt] ))
24064 The return value is as for cp_parser_gnu_attribute_list. */
24066 static tree
24067 cp_parser_gnu_attributes_opt (cp_parser* parser)
24069 tree attributes = NULL_TREE;
24071 while (true)
24073 cp_token *token;
24074 tree attribute_list;
24075 bool ok = true;
24077 /* Peek at the next token. */
24078 token = cp_lexer_peek_token (parser->lexer);
24079 /* If it's not `__attribute__', then we're done. */
24080 if (token->keyword != RID_ATTRIBUTE)
24081 break;
24083 /* Consume the `__attribute__' keyword. */
24084 cp_lexer_consume_token (parser->lexer);
24085 /* Look for the two `(' tokens. */
24086 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24087 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24089 /* Peek at the next token. */
24090 token = cp_lexer_peek_token (parser->lexer);
24091 if (token->type != CPP_CLOSE_PAREN)
24092 /* Parse the attribute-list. */
24093 attribute_list = cp_parser_gnu_attribute_list (parser);
24094 else
24095 /* If the next token is a `)', then there is no attribute
24096 list. */
24097 attribute_list = NULL;
24099 /* Look for the two `)' tokens. */
24100 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24101 ok = false;
24102 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24103 ok = false;
24104 if (!ok)
24105 cp_parser_skip_to_end_of_statement (parser);
24107 /* Add these new attributes to the list. */
24108 attributes = chainon (attributes, attribute_list);
24111 return attributes;
24114 /* Parse a GNU attribute-list.
24116 attribute-list:
24117 attribute
24118 attribute-list , attribute
24120 attribute:
24121 identifier
24122 identifier ( identifier )
24123 identifier ( identifier , expression-list )
24124 identifier ( expression-list )
24126 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24127 to an attribute. The TREE_PURPOSE of each node is the identifier
24128 indicating which attribute is in use. The TREE_VALUE represents
24129 the arguments, if any. */
24131 static tree
24132 cp_parser_gnu_attribute_list (cp_parser* parser)
24134 tree attribute_list = NULL_TREE;
24135 bool save_translate_strings_p = parser->translate_strings_p;
24137 parser->translate_strings_p = false;
24138 while (true)
24140 cp_token *token;
24141 tree identifier;
24142 tree attribute;
24144 /* Look for the identifier. We also allow keywords here; for
24145 example `__attribute__ ((const))' is legal. */
24146 token = cp_lexer_peek_token (parser->lexer);
24147 if (token->type == CPP_NAME
24148 || token->type == CPP_KEYWORD)
24150 tree arguments = NULL_TREE;
24152 /* Consume the token, but save it since we need it for the
24153 SIMD enabled function parsing. */
24154 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24156 /* Save away the identifier that indicates which attribute
24157 this is. */
24158 identifier = (token->type == CPP_KEYWORD)
24159 /* For keywords, use the canonical spelling, not the
24160 parsed identifier. */
24161 ? ridpointers[(int) token->keyword]
24162 : id_token->u.value;
24164 attribute = build_tree_list (identifier, NULL_TREE);
24166 /* Peek at the next token. */
24167 token = cp_lexer_peek_token (parser->lexer);
24168 /* If it's an `(', then parse the attribute arguments. */
24169 if (token->type == CPP_OPEN_PAREN)
24171 vec<tree, va_gc> *vec;
24172 int attr_flag = (attribute_takes_identifier_p (identifier)
24173 ? id_attr : normal_attr);
24174 if (is_cilkplus_vector_p (identifier))
24176 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24177 continue;
24179 else
24180 vec = cp_parser_parenthesized_expression_list
24181 (parser, attr_flag, /*cast_p=*/false,
24182 /*allow_expansion_p=*/false,
24183 /*non_constant_p=*/NULL);
24184 if (vec == NULL)
24185 arguments = error_mark_node;
24186 else
24188 arguments = build_tree_list_vec (vec);
24189 release_tree_vector (vec);
24191 /* Save the arguments away. */
24192 TREE_VALUE (attribute) = arguments;
24194 else if (is_cilkplus_vector_p (identifier))
24196 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24197 continue;
24200 if (arguments != error_mark_node)
24202 /* Add this attribute to the list. */
24203 TREE_CHAIN (attribute) = attribute_list;
24204 attribute_list = attribute;
24207 token = cp_lexer_peek_token (parser->lexer);
24209 /* Now, look for more attributes. If the next token isn't a
24210 `,', we're done. */
24211 if (token->type != CPP_COMMA)
24212 break;
24214 /* Consume the comma and keep going. */
24215 cp_lexer_consume_token (parser->lexer);
24217 parser->translate_strings_p = save_translate_strings_p;
24219 /* We built up the list in reverse order. */
24220 return nreverse (attribute_list);
24223 /* Parse a standard C++11 attribute.
24225 The returned representation is a TREE_LIST which TREE_PURPOSE is
24226 the scoped name of the attribute, and the TREE_VALUE is its
24227 arguments list.
24229 Note that the scoped name of the attribute is itself a TREE_LIST
24230 which TREE_PURPOSE is the namespace of the attribute, and
24231 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24232 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24233 and which TREE_PURPOSE is directly the attribute name.
24235 Clients of the attribute code should use get_attribute_namespace
24236 and get_attribute_name to get the actual namespace and name of
24237 attributes, regardless of their being GNU or C++11 attributes.
24239 attribute:
24240 attribute-token attribute-argument-clause [opt]
24242 attribute-token:
24243 identifier
24244 attribute-scoped-token
24246 attribute-scoped-token:
24247 attribute-namespace :: identifier
24249 attribute-namespace:
24250 identifier
24252 attribute-argument-clause:
24253 ( balanced-token-seq )
24255 balanced-token-seq:
24256 balanced-token [opt]
24257 balanced-token-seq balanced-token
24259 balanced-token:
24260 ( balanced-token-seq )
24261 [ balanced-token-seq ]
24262 { balanced-token-seq }. */
24264 static tree
24265 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24267 tree attribute, attr_id = NULL_TREE, arguments;
24268 cp_token *token;
24270 /* First, parse name of the attribute, a.k.a attribute-token. */
24272 token = cp_lexer_peek_token (parser->lexer);
24273 if (token->type == CPP_NAME)
24274 attr_id = token->u.value;
24275 else if (token->type == CPP_KEYWORD)
24276 attr_id = ridpointers[(int) token->keyword];
24277 else if (token->flags & NAMED_OP)
24278 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24280 if (attr_id == NULL_TREE)
24281 return NULL_TREE;
24283 cp_lexer_consume_token (parser->lexer);
24285 token = cp_lexer_peek_token (parser->lexer);
24286 if (token->type == CPP_SCOPE)
24288 /* We are seeing a scoped attribute token. */
24290 cp_lexer_consume_token (parser->lexer);
24291 if (attr_ns)
24292 error_at (token->location, "attribute using prefix used together "
24293 "with scoped attribute token");
24294 attr_ns = attr_id;
24296 token = cp_lexer_consume_token (parser->lexer);
24297 if (token->type == CPP_NAME)
24298 attr_id = token->u.value;
24299 else if (token->type == CPP_KEYWORD)
24300 attr_id = ridpointers[(int) token->keyword];
24301 else if (token->flags & NAMED_OP)
24302 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24303 else
24305 error_at (token->location,
24306 "expected an identifier for the attribute name");
24307 return error_mark_node;
24309 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24310 NULL_TREE);
24311 token = cp_lexer_peek_token (parser->lexer);
24313 else if (attr_ns)
24314 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24315 NULL_TREE);
24316 else
24318 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24319 NULL_TREE);
24320 /* C++11 noreturn attribute is equivalent to GNU's. */
24321 if (is_attribute_p ("noreturn", attr_id))
24322 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24323 /* C++14 deprecated attribute is equivalent to GNU's. */
24324 else if (is_attribute_p ("deprecated", attr_id))
24326 if (cxx_dialect == cxx11)
24327 pedwarn (token->location, OPT_Wpedantic,
24328 "%<deprecated%> is a C++14 feature;"
24329 " use %<gnu::deprecated%>");
24330 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24332 /* C++17 fallthrough attribute is equivalent to GNU's. */
24333 else if (is_attribute_p ("fallthrough", attr_id))
24335 if (cxx_dialect < cxx1z)
24336 pedwarn (token->location, OPT_Wpedantic,
24337 "%<fallthrough%> is a C++17 feature;"
24338 " use %<gnu::fallthrough%>");
24339 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24341 /* Transactional Memory TS optimize_for_synchronized attribute is
24342 equivalent to GNU transaction_callable. */
24343 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24344 TREE_PURPOSE (attribute)
24345 = get_identifier ("transaction_callable");
24346 /* Transactional Memory attributes are GNU attributes. */
24347 else if (tm_attr_to_mask (attr_id))
24348 TREE_PURPOSE (attribute) = attr_id;
24351 /* Now parse the optional argument clause of the attribute. */
24353 if (token->type != CPP_OPEN_PAREN)
24354 return attribute;
24357 vec<tree, va_gc> *vec;
24358 int attr_flag = normal_attr;
24360 if (attr_ns == get_identifier ("gnu")
24361 && attribute_takes_identifier_p (attr_id))
24362 /* A GNU attribute that takes an identifier in parameter. */
24363 attr_flag = id_attr;
24365 vec = cp_parser_parenthesized_expression_list
24366 (parser, attr_flag, /*cast_p=*/false,
24367 /*allow_expansion_p=*/true,
24368 /*non_constant_p=*/NULL);
24369 if (vec == NULL)
24370 arguments = error_mark_node;
24371 else
24373 arguments = build_tree_list_vec (vec);
24374 release_tree_vector (vec);
24377 if (arguments == error_mark_node)
24378 attribute = error_mark_node;
24379 else
24380 TREE_VALUE (attribute) = arguments;
24383 return attribute;
24386 /* Check that the attribute ATTRIBUTE appears at most once in the
24387 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24388 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24389 isn't implemented yet in GCC. */
24391 static void
24392 cp_parser_check_std_attribute (tree attributes, tree attribute)
24394 if (attributes)
24396 tree name = get_attribute_name (attribute);
24397 if (is_attribute_p ("noreturn", name)
24398 && lookup_attribute ("noreturn", attributes))
24399 error ("attribute %<noreturn%> can appear at most once "
24400 "in an attribute-list");
24401 else if (is_attribute_p ("deprecated", name)
24402 && lookup_attribute ("deprecated", attributes))
24403 error ("attribute %<deprecated%> can appear at most once "
24404 "in an attribute-list");
24408 /* Parse a list of standard C++-11 attributes.
24410 attribute-list:
24411 attribute [opt]
24412 attribute-list , attribute[opt]
24413 attribute ...
24414 attribute-list , attribute ...
24417 static tree
24418 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24420 tree attributes = NULL_TREE, attribute = NULL_TREE;
24421 cp_token *token = NULL;
24423 while (true)
24425 attribute = cp_parser_std_attribute (parser, attr_ns);
24426 if (attribute == error_mark_node)
24427 break;
24428 if (attribute != NULL_TREE)
24430 cp_parser_check_std_attribute (attributes, attribute);
24431 TREE_CHAIN (attribute) = attributes;
24432 attributes = attribute;
24434 token = cp_lexer_peek_token (parser->lexer);
24435 if (token->type == CPP_ELLIPSIS)
24437 cp_lexer_consume_token (parser->lexer);
24438 if (attribute == NULL_TREE)
24439 error_at (token->location,
24440 "expected attribute before %<...%>");
24441 else
24442 TREE_VALUE (attribute)
24443 = make_pack_expansion (TREE_VALUE (attribute));
24444 token = cp_lexer_peek_token (parser->lexer);
24446 if (token->type != CPP_COMMA)
24447 break;
24448 cp_lexer_consume_token (parser->lexer);
24450 attributes = nreverse (attributes);
24451 return attributes;
24454 /* Parse a standard C++-11 attribute specifier.
24456 attribute-specifier:
24457 [ [ attribute-using-prefix [opt] attribute-list ] ]
24458 alignment-specifier
24460 attribute-using-prefix:
24461 using attribute-namespace :
24463 alignment-specifier:
24464 alignas ( type-id ... [opt] )
24465 alignas ( alignment-expression ... [opt] ). */
24467 static tree
24468 cp_parser_std_attribute_spec (cp_parser *parser)
24470 tree attributes = NULL_TREE;
24471 cp_token *token = cp_lexer_peek_token (parser->lexer);
24473 if (token->type == CPP_OPEN_SQUARE
24474 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24476 tree attr_ns = NULL_TREE;
24478 cp_lexer_consume_token (parser->lexer);
24479 cp_lexer_consume_token (parser->lexer);
24481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24483 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24484 if (token->type == CPP_NAME)
24485 attr_ns = token->u.value;
24486 else if (token->type == CPP_KEYWORD)
24487 attr_ns = ridpointers[(int) token->keyword];
24488 else if (token->flags & NAMED_OP)
24489 attr_ns = get_identifier (cpp_type2name (token->type,
24490 token->flags));
24491 if (attr_ns
24492 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24494 if (cxx_dialect < cxx1z
24495 && !in_system_header_at (input_location))
24496 pedwarn (input_location, 0,
24497 "attribute using prefix only available "
24498 "with -std=c++1z or -std=gnu++1z");
24500 cp_lexer_consume_token (parser->lexer);
24501 cp_lexer_consume_token (parser->lexer);
24502 cp_lexer_consume_token (parser->lexer);
24504 else
24505 attr_ns = NULL_TREE;
24508 attributes = cp_parser_std_attribute_list (parser, attr_ns);
24510 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24511 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24512 cp_parser_skip_to_end_of_statement (parser);
24513 else
24514 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24515 when we are sure that we have actually parsed them. */
24516 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24518 else
24520 tree alignas_expr;
24522 /* Look for an alignment-specifier. */
24524 token = cp_lexer_peek_token (parser->lexer);
24526 if (token->type != CPP_KEYWORD
24527 || token->keyword != RID_ALIGNAS)
24528 return NULL_TREE;
24530 cp_lexer_consume_token (parser->lexer);
24531 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24533 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24535 cp_parser_error (parser, "expected %<(%>");
24536 return error_mark_node;
24539 cp_parser_parse_tentatively (parser);
24540 alignas_expr = cp_parser_type_id (parser);
24542 if (!cp_parser_parse_definitely (parser))
24544 gcc_assert (alignas_expr == error_mark_node
24545 || alignas_expr == NULL_TREE);
24547 alignas_expr =
24548 cp_parser_assignment_expression (parser);
24549 if (alignas_expr == error_mark_node)
24550 cp_parser_skip_to_end_of_statement (parser);
24551 if (alignas_expr == NULL_TREE
24552 || alignas_expr == error_mark_node)
24553 return alignas_expr;
24556 alignas_expr = cxx_alignas_expr (alignas_expr);
24557 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24559 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24561 cp_lexer_consume_token (parser->lexer);
24562 alignas_expr = make_pack_expansion (alignas_expr);
24565 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24567 cp_parser_error (parser, "expected %<)%>");
24568 return error_mark_node;
24571 /* Build the C++-11 representation of an 'aligned'
24572 attribute. */
24573 attributes =
24574 build_tree_list (build_tree_list (get_identifier ("gnu"),
24575 get_identifier ("aligned")),
24576 alignas_expr);
24579 return attributes;
24582 /* Parse a standard C++-11 attribute-specifier-seq.
24584 attribute-specifier-seq:
24585 attribute-specifier-seq [opt] attribute-specifier
24588 static tree
24589 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24591 tree attr_specs = NULL_TREE;
24592 tree attr_last = NULL_TREE;
24594 while (true)
24596 tree attr_spec = cp_parser_std_attribute_spec (parser);
24597 if (attr_spec == NULL_TREE)
24598 break;
24599 if (attr_spec == error_mark_node)
24600 return error_mark_node;
24602 if (attr_last)
24603 TREE_CHAIN (attr_last) = attr_spec;
24604 else
24605 attr_specs = attr_last = attr_spec;
24606 attr_last = tree_last (attr_last);
24609 return attr_specs;
24612 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24613 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24614 current value of the PEDANTIC flag, regardless of whether or not
24615 the `__extension__' keyword is present. The caller is responsible
24616 for restoring the value of the PEDANTIC flag. */
24618 static bool
24619 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24621 /* Save the old value of the PEDANTIC flag. */
24622 *saved_pedantic = pedantic;
24624 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24626 /* Consume the `__extension__' token. */
24627 cp_lexer_consume_token (parser->lexer);
24628 /* We're not being pedantic while the `__extension__' keyword is
24629 in effect. */
24630 pedantic = 0;
24632 return true;
24635 return false;
24638 /* Parse a label declaration.
24640 label-declaration:
24641 __label__ label-declarator-seq ;
24643 label-declarator-seq:
24644 identifier , label-declarator-seq
24645 identifier */
24647 static void
24648 cp_parser_label_declaration (cp_parser* parser)
24650 /* Look for the `__label__' keyword. */
24651 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24653 while (true)
24655 tree identifier;
24657 /* Look for an identifier. */
24658 identifier = cp_parser_identifier (parser);
24659 /* If we failed, stop. */
24660 if (identifier == error_mark_node)
24661 break;
24662 /* Declare it as a label. */
24663 finish_label_decl (identifier);
24664 /* If the next token is a `;', stop. */
24665 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24666 break;
24667 /* Look for the `,' separating the label declarations. */
24668 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24671 /* Look for the final `;'. */
24672 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24675 // -------------------------------------------------------------------------- //
24676 // Requires Clause
24678 // Parse a requires clause.
24680 // requires-clause:
24681 // 'requires' logical-or-expression
24683 // The required logical-or-expression must be a constant expression. Note
24684 // that we don't check that the expression is constepxr here. We defer until
24685 // we analyze constraints and then, we only check atomic constraints.
24686 static tree
24687 cp_parser_requires_clause (cp_parser *parser)
24689 // Parse the requires clause so that it is not automatically folded.
24690 ++processing_template_decl;
24691 tree expr = cp_parser_binary_expression (parser, false, false,
24692 PREC_NOT_OPERATOR, NULL);
24693 if (check_for_bare_parameter_packs (expr))
24694 expr = error_mark_node;
24695 --processing_template_decl;
24696 return expr;
24699 // Optionally parse a requires clause:
24700 static tree
24701 cp_parser_requires_clause_opt (cp_parser *parser)
24703 cp_token *tok = cp_lexer_peek_token (parser->lexer);
24704 if (tok->keyword != RID_REQUIRES)
24706 if (!flag_concepts && tok->type == CPP_NAME
24707 && tok->u.value == ridpointers[RID_REQUIRES])
24709 error_at (cp_lexer_peek_token (parser->lexer)->location,
24710 "%<requires%> only available with -fconcepts");
24711 /* Parse and discard the requires-clause. */
24712 cp_lexer_consume_token (parser->lexer);
24713 cp_parser_requires_clause (parser);
24715 return NULL_TREE;
24717 cp_lexer_consume_token (parser->lexer);
24718 return cp_parser_requires_clause (parser);
24722 /*---------------------------------------------------------------------------
24723 Requires expressions
24724 ---------------------------------------------------------------------------*/
24726 /* Parse a requires expression
24728 requirement-expression:
24729 'requires' requirement-parameter-list [opt] requirement-body */
24730 static tree
24731 cp_parser_requires_expression (cp_parser *parser)
24733 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24734 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24736 /* A requires-expression shall appear only within a concept
24737 definition or a requires-clause.
24739 TODO: Implement this diagnostic correctly. */
24740 if (!processing_template_decl)
24742 error_at (loc, "a requires expression cannot appear outside a template");
24743 cp_parser_skip_to_end_of_statement (parser);
24744 return error_mark_node;
24747 tree parms, reqs;
24749 /* Local parameters are delared as variables within the scope
24750 of the expression. They are not visible past the end of
24751 the expression. Expressions within the requires-expression
24752 are unevaluated. */
24753 struct scope_sentinel
24755 scope_sentinel ()
24757 ++cp_unevaluated_operand;
24758 begin_scope (sk_block, NULL_TREE);
24761 ~scope_sentinel ()
24763 pop_bindings_and_leave_scope ();
24764 --cp_unevaluated_operand;
24766 } s;
24768 /* Parse the optional parameter list. */
24769 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24771 parms = cp_parser_requirement_parameter_list (parser);
24772 if (parms == error_mark_node)
24773 return error_mark_node;
24775 else
24776 parms = NULL_TREE;
24778 /* Parse the requirement body. */
24779 reqs = cp_parser_requirement_body (parser);
24780 if (reqs == error_mark_node)
24781 return error_mark_node;
24784 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24785 the parm chain. */
24786 grokparms (parms, &parms);
24787 return finish_requires_expr (parms, reqs);
24790 /* Parse a parameterized requirement.
24792 requirement-parameter-list:
24793 '(' parameter-declaration-clause ')' */
24794 static tree
24795 cp_parser_requirement_parameter_list (cp_parser *parser)
24797 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24798 return error_mark_node;
24800 tree parms = cp_parser_parameter_declaration_clause (parser);
24802 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24803 return error_mark_node;
24805 return parms;
24808 /* Parse the body of a requirement.
24810 requirement-body:
24811 '{' requirement-list '}' */
24812 static tree
24813 cp_parser_requirement_body (cp_parser *parser)
24815 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24816 return error_mark_node;
24818 tree reqs = cp_parser_requirement_list (parser);
24820 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24821 return error_mark_node;
24823 return reqs;
24826 /* Parse a list of requirements.
24828 requirement-list:
24829 requirement
24830 requirement-list ';' requirement[opt] */
24831 static tree
24832 cp_parser_requirement_list (cp_parser *parser)
24834 tree result = NULL_TREE;
24835 while (true)
24837 tree req = cp_parser_requirement (parser);
24838 if (req == error_mark_node)
24839 return error_mark_node;
24841 result = tree_cons (NULL_TREE, req, result);
24843 /* If we see a semi-colon, consume it. */
24844 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24845 cp_lexer_consume_token (parser->lexer);
24847 /* Stop processing at the end of the list. */
24848 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24849 break;
24852 /* Reverse the order of requirements so they are analyzed in
24853 declaration order. */
24854 return nreverse (result);
24857 /* Parse a syntactic requirement or type requirement.
24859 requirement:
24860 simple-requirement
24861 compound-requirement
24862 type-requirement
24863 nested-requirement */
24864 static tree
24865 cp_parser_requirement (cp_parser *parser)
24867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24868 return cp_parser_compound_requirement (parser);
24869 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24870 return cp_parser_type_requirement (parser);
24871 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24872 return cp_parser_nested_requirement (parser);
24873 else
24874 return cp_parser_simple_requirement (parser);
24877 /* Parse a simple requirement.
24879 simple-requirement:
24880 expression ';' */
24881 static tree
24882 cp_parser_simple_requirement (cp_parser *parser)
24884 tree expr = cp_parser_expression (parser, NULL, false, false);
24885 if (!expr || expr == error_mark_node)
24886 return error_mark_node;
24888 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24889 return error_mark_node;
24891 return finish_simple_requirement (expr);
24894 /* Parse a type requirement
24896 type-requirement
24897 nested-name-specifier [opt] required-type-name ';'
24899 required-type-name:
24900 type-name
24901 'template' [opt] simple-template-id */
24902 static tree
24903 cp_parser_type_requirement (cp_parser *parser)
24905 cp_lexer_consume_token (parser->lexer);
24907 // Save the scope before parsing name specifiers.
24908 tree saved_scope = parser->scope;
24909 tree saved_object_scope = parser->object_scope;
24910 tree saved_qualifying_scope = parser->qualifying_scope;
24911 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24912 cp_parser_nested_name_specifier_opt (parser,
24913 /*typename_keyword_p=*/true,
24914 /*check_dependency_p=*/false,
24915 /*type_p=*/true,
24916 /*is_declaration=*/false);
24918 tree type;
24919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24921 cp_lexer_consume_token (parser->lexer);
24922 type = cp_parser_template_id (parser,
24923 /*template_keyword_p=*/true,
24924 /*check_dependency=*/false,
24925 /*tag_type=*/none_type,
24926 /*is_declaration=*/false);
24927 type = make_typename_type (parser->scope, type, typename_type,
24928 /*complain=*/tf_error);
24930 else
24931 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24933 if (TREE_CODE (type) == TYPE_DECL)
24934 type = TREE_TYPE (type);
24936 parser->scope = saved_scope;
24937 parser->object_scope = saved_object_scope;
24938 parser->qualifying_scope = saved_qualifying_scope;
24940 if (type == error_mark_node)
24941 cp_parser_skip_to_end_of_statement (parser);
24943 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24944 return error_mark_node;
24945 if (type == error_mark_node)
24946 return error_mark_node;
24948 return finish_type_requirement (type);
24951 /* Parse a compound requirement
24953 compound-requirement:
24954 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24955 static tree
24956 cp_parser_compound_requirement (cp_parser *parser)
24958 /* Parse an expression enclosed in '{ }'s. */
24959 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24960 return error_mark_node;
24962 tree expr = cp_parser_expression (parser, NULL, false, false);
24963 if (!expr || expr == error_mark_node)
24964 return error_mark_node;
24966 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24967 return error_mark_node;
24969 /* Parse the optional noexcept. */
24970 bool noexcept_p = false;
24971 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24973 cp_lexer_consume_token (parser->lexer);
24974 noexcept_p = true;
24977 /* Parse the optional trailing return type. */
24978 tree type = NULL_TREE;
24979 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24981 cp_lexer_consume_token (parser->lexer);
24982 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24983 parser->in_result_type_constraint_p = true;
24984 type = cp_parser_trailing_type_id (parser);
24985 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24986 if (type == error_mark_node)
24987 return error_mark_node;
24990 return finish_compound_requirement (expr, type, noexcept_p);
24993 /* Parse a nested requirement. This is the same as a requires clause.
24995 nested-requirement:
24996 requires-clause */
24997 static tree
24998 cp_parser_nested_requirement (cp_parser *parser)
25000 cp_lexer_consume_token (parser->lexer);
25001 tree req = cp_parser_requires_clause (parser);
25002 if (req == error_mark_node)
25003 return error_mark_node;
25004 return finish_nested_requirement (req);
25007 /* Support Functions */
25009 /* Return the appropriate prefer_type argument for lookup_name_real based on
25010 tag_type and template_mem_access. */
25012 static inline int
25013 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25015 /* DR 141: When looking in the current enclosing context for a template-name
25016 after -> or ., only consider class templates. */
25017 if (template_mem_access)
25018 return 2;
25019 switch (tag_type)
25021 case none_type: return 0; // No preference.
25022 case scope_type: return 1; // Type or namespace.
25023 default: return 2; // Type only.
25027 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25028 NAME should have one of the representations used for an
25029 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25030 is returned. If PARSER->SCOPE is a dependent type, then a
25031 SCOPE_REF is returned.
25033 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25034 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25035 was formed. Abstractly, such entities should not be passed to this
25036 function, because they do not need to be looked up, but it is
25037 simpler to check for this special case here, rather than at the
25038 call-sites.
25040 In cases not explicitly covered above, this function returns a
25041 DECL, OVERLOAD, or baselink representing the result of the lookup.
25042 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25043 is returned.
25045 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25046 (e.g., "struct") that was used. In that case bindings that do not
25047 refer to types are ignored.
25049 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25050 ignored.
25052 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25053 are ignored.
25055 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25056 types.
25058 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25059 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25060 NULL_TREE otherwise. */
25062 static cp_expr
25063 cp_parser_lookup_name (cp_parser *parser, tree name,
25064 enum tag_types tag_type,
25065 bool is_template,
25066 bool is_namespace,
25067 bool check_dependency,
25068 tree *ambiguous_decls,
25069 location_t name_location)
25071 tree decl;
25072 tree object_type = parser->context->object_type;
25074 /* Assume that the lookup will be unambiguous. */
25075 if (ambiguous_decls)
25076 *ambiguous_decls = NULL_TREE;
25078 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25079 no longer valid. Note that if we are parsing tentatively, and
25080 the parse fails, OBJECT_TYPE will be automatically restored. */
25081 parser->context->object_type = NULL_TREE;
25083 if (name == error_mark_node)
25084 return error_mark_node;
25086 /* A template-id has already been resolved; there is no lookup to
25087 do. */
25088 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25089 return name;
25090 if (BASELINK_P (name))
25092 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25093 == TEMPLATE_ID_EXPR);
25094 return name;
25097 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25098 it should already have been checked to make sure that the name
25099 used matches the type being destroyed. */
25100 if (TREE_CODE (name) == BIT_NOT_EXPR)
25102 tree type;
25104 /* Figure out to which type this destructor applies. */
25105 if (parser->scope)
25106 type = parser->scope;
25107 else if (object_type)
25108 type = object_type;
25109 else
25110 type = current_class_type;
25111 /* If that's not a class type, there is no destructor. */
25112 if (!type || !CLASS_TYPE_P (type))
25113 return error_mark_node;
25114 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25115 lazily_declare_fn (sfk_destructor, type);
25116 if (!CLASSTYPE_DESTRUCTORS (type))
25117 return error_mark_node;
25118 /* If it was a class type, return the destructor. */
25119 return CLASSTYPE_DESTRUCTORS (type);
25122 /* By this point, the NAME should be an ordinary identifier. If
25123 the id-expression was a qualified name, the qualifying scope is
25124 stored in PARSER->SCOPE at this point. */
25125 gcc_assert (identifier_p (name));
25127 /* Perform the lookup. */
25128 if (parser->scope)
25130 bool dependent_p;
25132 if (parser->scope == error_mark_node)
25133 return error_mark_node;
25135 /* If the SCOPE is dependent, the lookup must be deferred until
25136 the template is instantiated -- unless we are explicitly
25137 looking up names in uninstantiated templates. Even then, we
25138 cannot look up the name if the scope is not a class type; it
25139 might, for example, be a template type parameter. */
25140 dependent_p = (TYPE_P (parser->scope)
25141 && dependent_scope_p (parser->scope));
25142 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25143 && dependent_p)
25144 /* Defer lookup. */
25145 decl = error_mark_node;
25146 else
25148 tree pushed_scope = NULL_TREE;
25150 /* If PARSER->SCOPE is a dependent type, then it must be a
25151 class type, and we must not be checking dependencies;
25152 otherwise, we would have processed this lookup above. So
25153 that PARSER->SCOPE is not considered a dependent base by
25154 lookup_member, we must enter the scope here. */
25155 if (dependent_p)
25156 pushed_scope = push_scope (parser->scope);
25158 /* If the PARSER->SCOPE is a template specialization, it
25159 may be instantiated during name lookup. In that case,
25160 errors may be issued. Even if we rollback the current
25161 tentative parse, those errors are valid. */
25162 decl = lookup_qualified_name (parser->scope, name,
25163 prefer_type_arg (tag_type),
25164 /*complain=*/true);
25166 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25167 lookup result and the nested-name-specifier nominates a class C:
25168 * if the name specified after the nested-name-specifier, when
25169 looked up in C, is the injected-class-name of C (Clause 9), or
25170 * if the name specified after the nested-name-specifier is the
25171 same as the identifier or the simple-template-id's template-
25172 name in the last component of the nested-name-specifier,
25173 the name is instead considered to name the constructor of
25174 class C. [ Note: for example, the constructor is not an
25175 acceptable lookup result in an elaborated-type-specifier so
25176 the constructor would not be used in place of the
25177 injected-class-name. --end note ] Such a constructor name
25178 shall be used only in the declarator-id of a declaration that
25179 names a constructor or in a using-declaration. */
25180 if (tag_type == none_type
25181 && DECL_SELF_REFERENCE_P (decl)
25182 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25183 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25184 prefer_type_arg (tag_type),
25185 /*complain=*/true);
25187 /* If we have a single function from a using decl, pull it out. */
25188 if (TREE_CODE (decl) == OVERLOAD
25189 && !really_overloaded_fn (decl))
25190 decl = OVL_FUNCTION (decl);
25192 if (pushed_scope)
25193 pop_scope (pushed_scope);
25196 /* If the scope is a dependent type and either we deferred lookup or
25197 we did lookup but didn't find the name, rememeber the name. */
25198 if (decl == error_mark_node && TYPE_P (parser->scope)
25199 && dependent_type_p (parser->scope))
25201 if (tag_type)
25203 tree type;
25205 /* The resolution to Core Issue 180 says that `struct
25206 A::B' should be considered a type-name, even if `A'
25207 is dependent. */
25208 type = make_typename_type (parser->scope, name, tag_type,
25209 /*complain=*/tf_error);
25210 if (type != error_mark_node)
25211 decl = TYPE_NAME (type);
25213 else if (is_template
25214 && (cp_parser_next_token_ends_template_argument_p (parser)
25215 || cp_lexer_next_token_is (parser->lexer,
25216 CPP_CLOSE_PAREN)))
25217 decl = make_unbound_class_template (parser->scope,
25218 name, NULL_TREE,
25219 /*complain=*/tf_error);
25220 else
25221 decl = build_qualified_name (/*type=*/NULL_TREE,
25222 parser->scope, name,
25223 is_template);
25225 parser->qualifying_scope = parser->scope;
25226 parser->object_scope = NULL_TREE;
25228 else if (object_type)
25230 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25231 OBJECT_TYPE is not a class. */
25232 if (CLASS_TYPE_P (object_type))
25233 /* If the OBJECT_TYPE is a template specialization, it may
25234 be instantiated during name lookup. In that case, errors
25235 may be issued. Even if we rollback the current tentative
25236 parse, those errors are valid. */
25237 decl = lookup_member (object_type,
25238 name,
25239 /*protect=*/0,
25240 prefer_type_arg (tag_type),
25241 tf_warning_or_error);
25242 else
25243 decl = NULL_TREE;
25245 if (!decl)
25246 /* Look it up in the enclosing context. DR 141: When looking for a
25247 template-name after -> or ., only consider class templates. */
25248 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25249 /*nonclass=*/0,
25250 /*block_p=*/true, is_namespace, 0);
25251 if (object_type == unknown_type_node)
25252 /* The object is type-dependent, so we can't look anything up; we used
25253 this to get the DR 141 behavior. */
25254 object_type = NULL_TREE;
25255 parser->object_scope = object_type;
25256 parser->qualifying_scope = NULL_TREE;
25258 else
25260 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25261 /*nonclass=*/0,
25262 /*block_p=*/true, is_namespace, 0);
25263 parser->qualifying_scope = NULL_TREE;
25264 parser->object_scope = NULL_TREE;
25267 /* If the lookup failed, let our caller know. */
25268 if (!decl || decl == error_mark_node)
25269 return error_mark_node;
25271 /* Pull out the template from an injected-class-name (or multiple). */
25272 if (is_template)
25273 decl = maybe_get_template_decl_from_type_decl (decl);
25275 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25276 if (TREE_CODE (decl) == TREE_LIST)
25278 if (ambiguous_decls)
25279 *ambiguous_decls = decl;
25280 /* The error message we have to print is too complicated for
25281 cp_parser_error, so we incorporate its actions directly. */
25282 if (!cp_parser_simulate_error (parser))
25284 error_at (name_location, "reference to %qD is ambiguous",
25285 name);
25286 print_candidates (decl);
25288 return error_mark_node;
25291 gcc_assert (DECL_P (decl)
25292 || TREE_CODE (decl) == OVERLOAD
25293 || TREE_CODE (decl) == SCOPE_REF
25294 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25295 || BASELINK_P (decl));
25297 /* If we have resolved the name of a member declaration, check to
25298 see if the declaration is accessible. When the name resolves to
25299 set of overloaded functions, accessibility is checked when
25300 overload resolution is done.
25302 During an explicit instantiation, access is not checked at all,
25303 as per [temp.explicit]. */
25304 if (DECL_P (decl))
25305 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25307 maybe_record_typedef_use (decl);
25309 return cp_expr (decl, name_location);
25312 /* Like cp_parser_lookup_name, but for use in the typical case where
25313 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25314 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25316 static tree
25317 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25319 return cp_parser_lookup_name (parser, name,
25320 none_type,
25321 /*is_template=*/false,
25322 /*is_namespace=*/false,
25323 /*check_dependency=*/true,
25324 /*ambiguous_decls=*/NULL,
25325 location);
25328 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25329 the current context, return the TYPE_DECL. If TAG_NAME_P is
25330 true, the DECL indicates the class being defined in a class-head,
25331 or declared in an elaborated-type-specifier.
25333 Otherwise, return DECL. */
25335 static tree
25336 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25338 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25339 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25341 struct A {
25342 template <typename T> struct B;
25345 template <typename T> struct A::B {};
25347 Similarly, in an elaborated-type-specifier:
25349 namespace N { struct X{}; }
25351 struct A {
25352 template <typename T> friend struct N::X;
25355 However, if the DECL refers to a class type, and we are in
25356 the scope of the class, then the name lookup automatically
25357 finds the TYPE_DECL created by build_self_reference rather
25358 than a TEMPLATE_DECL. For example, in:
25360 template <class T> struct S {
25361 S s;
25364 there is no need to handle such case. */
25366 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25367 return DECL_TEMPLATE_RESULT (decl);
25369 return decl;
25372 /* If too many, or too few, template-parameter lists apply to the
25373 declarator, issue an error message. Returns TRUE if all went well,
25374 and FALSE otherwise. */
25376 static bool
25377 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25378 cp_declarator *declarator,
25379 location_t declarator_location)
25381 switch (declarator->kind)
25383 case cdk_id:
25385 unsigned num_templates = 0;
25386 tree scope = declarator->u.id.qualifying_scope;
25388 if (scope)
25389 num_templates = num_template_headers_for_class (scope);
25390 else if (TREE_CODE (declarator->u.id.unqualified_name)
25391 == TEMPLATE_ID_EXPR)
25392 /* If the DECLARATOR has the form `X<y>' then it uses one
25393 additional level of template parameters. */
25394 ++num_templates;
25396 return cp_parser_check_template_parameters
25397 (parser, num_templates, declarator_location, declarator);
25400 case cdk_function:
25401 case cdk_array:
25402 case cdk_pointer:
25403 case cdk_reference:
25404 case cdk_ptrmem:
25405 return (cp_parser_check_declarator_template_parameters
25406 (parser, declarator->declarator, declarator_location));
25408 case cdk_error:
25409 return true;
25411 default:
25412 gcc_unreachable ();
25414 return false;
25417 /* NUM_TEMPLATES were used in the current declaration. If that is
25418 invalid, return FALSE and issue an error messages. Otherwise,
25419 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25420 declarator and we can print more accurate diagnostics. */
25422 static bool
25423 cp_parser_check_template_parameters (cp_parser* parser,
25424 unsigned num_templates,
25425 location_t location,
25426 cp_declarator *declarator)
25428 /* If there are the same number of template classes and parameter
25429 lists, that's OK. */
25430 if (parser->num_template_parameter_lists == num_templates)
25431 return true;
25432 /* If there are more, but only one more, then we are referring to a
25433 member template. That's OK too. */
25434 if (parser->num_template_parameter_lists == num_templates + 1)
25435 return true;
25436 /* If there are more template classes than parameter lists, we have
25437 something like:
25439 template <class T> void S<T>::R<T>::f (); */
25440 if (parser->num_template_parameter_lists < num_templates)
25442 if (declarator && !current_function_decl)
25443 error_at (location, "specializing member %<%T::%E%> "
25444 "requires %<template<>%> syntax",
25445 declarator->u.id.qualifying_scope,
25446 declarator->u.id.unqualified_name);
25447 else if (declarator)
25448 error_at (location, "invalid declaration of %<%T::%E%>",
25449 declarator->u.id.qualifying_scope,
25450 declarator->u.id.unqualified_name);
25451 else
25452 error_at (location, "too few template-parameter-lists");
25453 return false;
25455 /* Otherwise, there are too many template parameter lists. We have
25456 something like:
25458 template <class T> template <class U> void S::f(); */
25459 error_at (location, "too many template-parameter-lists");
25460 return false;
25463 /* Parse an optional `::' token indicating that the following name is
25464 from the global namespace. If so, PARSER->SCOPE is set to the
25465 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25466 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25467 Returns the new value of PARSER->SCOPE, if the `::' token is
25468 present, and NULL_TREE otherwise. */
25470 static tree
25471 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25473 cp_token *token;
25475 /* Peek at the next token. */
25476 token = cp_lexer_peek_token (parser->lexer);
25477 /* If we're looking at a `::' token then we're starting from the
25478 global namespace, not our current location. */
25479 if (token->type == CPP_SCOPE)
25481 /* Consume the `::' token. */
25482 cp_lexer_consume_token (parser->lexer);
25483 /* Set the SCOPE so that we know where to start the lookup. */
25484 parser->scope = global_namespace;
25485 parser->qualifying_scope = global_namespace;
25486 parser->object_scope = NULL_TREE;
25488 return parser->scope;
25490 else if (!current_scope_valid_p)
25492 parser->scope = NULL_TREE;
25493 parser->qualifying_scope = NULL_TREE;
25494 parser->object_scope = NULL_TREE;
25497 return NULL_TREE;
25500 /* Returns TRUE if the upcoming token sequence is the start of a
25501 constructor declarator. If FRIEND_P is true, the declarator is
25502 preceded by the `friend' specifier. */
25504 static bool
25505 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25507 bool constructor_p;
25508 bool outside_class_specifier_p;
25509 tree nested_name_specifier;
25510 cp_token *next_token;
25512 /* The common case is that this is not a constructor declarator, so
25513 try to avoid doing lots of work if at all possible. It's not
25514 valid declare a constructor at function scope. */
25515 if (parser->in_function_body)
25516 return false;
25517 /* And only certain tokens can begin a constructor declarator. */
25518 next_token = cp_lexer_peek_token (parser->lexer);
25519 if (next_token->type != CPP_NAME
25520 && next_token->type != CPP_SCOPE
25521 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25522 && next_token->type != CPP_TEMPLATE_ID)
25523 return false;
25525 /* Parse tentatively; we are going to roll back all of the tokens
25526 consumed here. */
25527 cp_parser_parse_tentatively (parser);
25528 /* Assume that we are looking at a constructor declarator. */
25529 constructor_p = true;
25531 /* Look for the optional `::' operator. */
25532 cp_parser_global_scope_opt (parser,
25533 /*current_scope_valid_p=*/false);
25534 /* Look for the nested-name-specifier. */
25535 nested_name_specifier
25536 = (cp_parser_nested_name_specifier_opt (parser,
25537 /*typename_keyword_p=*/false,
25538 /*check_dependency_p=*/false,
25539 /*type_p=*/false,
25540 /*is_declaration=*/false));
25542 outside_class_specifier_p = (!at_class_scope_p ()
25543 || !TYPE_BEING_DEFINED (current_class_type)
25544 || friend_p);
25546 /* Outside of a class-specifier, there must be a
25547 nested-name-specifier. */
25548 if (!nested_name_specifier && outside_class_specifier_p)
25549 constructor_p = false;
25550 else if (nested_name_specifier == error_mark_node)
25551 constructor_p = false;
25553 /* If we have a class scope, this is easy; DR 147 says that S::S always
25554 names the constructor, and no other qualified name could. */
25555 if (constructor_p && nested_name_specifier
25556 && CLASS_TYPE_P (nested_name_specifier))
25558 tree id = cp_parser_unqualified_id (parser,
25559 /*template_keyword_p=*/false,
25560 /*check_dependency_p=*/false,
25561 /*declarator_p=*/true,
25562 /*optional_p=*/false);
25563 if (is_overloaded_fn (id))
25564 id = DECL_NAME (get_first_fn (id));
25565 if (!constructor_name_p (id, nested_name_specifier))
25566 constructor_p = false;
25568 /* If we still think that this might be a constructor-declarator,
25569 look for a class-name. */
25570 else if (constructor_p)
25572 /* If we have:
25574 template <typename T> struct S {
25575 S();
25578 we must recognize that the nested `S' names a class. */
25579 tree type_decl;
25580 type_decl = cp_parser_class_name (parser,
25581 /*typename_keyword_p=*/false,
25582 /*template_keyword_p=*/false,
25583 none_type,
25584 /*check_dependency_p=*/false,
25585 /*class_head_p=*/false,
25586 /*is_declaration=*/false);
25587 /* If there was no class-name, then this is not a constructor.
25588 Otherwise, if we are in a class-specifier and we aren't
25589 handling a friend declaration, check that its type matches
25590 current_class_type (c++/38313). Note: error_mark_node
25591 is left alone for error recovery purposes. */
25592 constructor_p = (!cp_parser_error_occurred (parser)
25593 && (outside_class_specifier_p
25594 || type_decl == error_mark_node
25595 || same_type_p (current_class_type,
25596 TREE_TYPE (type_decl))));
25598 /* If we're still considering a constructor, we have to see a `(',
25599 to begin the parameter-declaration-clause, followed by either a
25600 `)', an `...', or a decl-specifier. We need to check for a
25601 type-specifier to avoid being fooled into thinking that:
25603 S (f) (int);
25605 is a constructor. (It is actually a function named `f' that
25606 takes one parameter (of type `int') and returns a value of type
25607 `S'. */
25608 if (constructor_p
25609 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25610 constructor_p = false;
25612 if (constructor_p
25613 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25614 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25615 /* A parameter declaration begins with a decl-specifier,
25616 which is either the "attribute" keyword, a storage class
25617 specifier, or (usually) a type-specifier. */
25618 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25620 tree type;
25621 tree pushed_scope = NULL_TREE;
25622 unsigned saved_num_template_parameter_lists;
25624 /* Names appearing in the type-specifier should be looked up
25625 in the scope of the class. */
25626 if (current_class_type)
25627 type = NULL_TREE;
25628 else
25630 type = TREE_TYPE (type_decl);
25631 if (TREE_CODE (type) == TYPENAME_TYPE)
25633 type = resolve_typename_type (type,
25634 /*only_current_p=*/false);
25635 if (TREE_CODE (type) == TYPENAME_TYPE)
25637 cp_parser_abort_tentative_parse (parser);
25638 return false;
25641 pushed_scope = push_scope (type);
25644 /* Inside the constructor parameter list, surrounding
25645 template-parameter-lists do not apply. */
25646 saved_num_template_parameter_lists
25647 = parser->num_template_parameter_lists;
25648 parser->num_template_parameter_lists = 0;
25650 /* Look for the type-specifier. */
25651 cp_parser_type_specifier (parser,
25652 CP_PARSER_FLAGS_NONE,
25653 /*decl_specs=*/NULL,
25654 /*is_declarator=*/true,
25655 /*declares_class_or_enum=*/NULL,
25656 /*is_cv_qualifier=*/NULL);
25658 parser->num_template_parameter_lists
25659 = saved_num_template_parameter_lists;
25661 /* Leave the scope of the class. */
25662 if (pushed_scope)
25663 pop_scope (pushed_scope);
25665 constructor_p = !cp_parser_error_occurred (parser);
25669 /* We did not really want to consume any tokens. */
25670 cp_parser_abort_tentative_parse (parser);
25672 return constructor_p;
25675 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25676 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25677 they must be performed once we are in the scope of the function.
25679 Returns the function defined. */
25681 static tree
25682 cp_parser_function_definition_from_specifiers_and_declarator
25683 (cp_parser* parser,
25684 cp_decl_specifier_seq *decl_specifiers,
25685 tree attributes,
25686 const cp_declarator *declarator)
25688 tree fn;
25689 bool success_p;
25691 /* Begin the function-definition. */
25692 success_p = start_function (decl_specifiers, declarator, attributes);
25694 /* The things we're about to see are not directly qualified by any
25695 template headers we've seen thus far. */
25696 reset_specialization ();
25698 /* If there were names looked up in the decl-specifier-seq that we
25699 did not check, check them now. We must wait until we are in the
25700 scope of the function to perform the checks, since the function
25701 might be a friend. */
25702 perform_deferred_access_checks (tf_warning_or_error);
25704 if (success_p)
25706 cp_finalize_omp_declare_simd (parser, current_function_decl);
25707 parser->omp_declare_simd = NULL;
25708 cp_finalize_oacc_routine (parser, current_function_decl, true);
25709 parser->oacc_routine = NULL;
25712 if (!success_p)
25714 /* Skip the entire function. */
25715 cp_parser_skip_to_end_of_block_or_statement (parser);
25716 fn = error_mark_node;
25718 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25720 /* Seen already, skip it. An error message has already been output. */
25721 cp_parser_skip_to_end_of_block_or_statement (parser);
25722 fn = current_function_decl;
25723 current_function_decl = NULL_TREE;
25724 /* If this is a function from a class, pop the nested class. */
25725 if (current_class_name)
25726 pop_nested_class ();
25728 else
25730 timevar_id_t tv;
25731 if (DECL_DECLARED_INLINE_P (current_function_decl))
25732 tv = TV_PARSE_INLINE;
25733 else
25734 tv = TV_PARSE_FUNC;
25735 timevar_push (tv);
25736 fn = cp_parser_function_definition_after_declarator (parser,
25737 /*inline_p=*/false);
25738 timevar_pop (tv);
25741 return fn;
25744 /* Parse the part of a function-definition that follows the
25745 declarator. INLINE_P is TRUE iff this function is an inline
25746 function defined within a class-specifier.
25748 Returns the function defined. */
25750 static tree
25751 cp_parser_function_definition_after_declarator (cp_parser* parser,
25752 bool inline_p)
25754 tree fn;
25755 bool ctor_initializer_p = false;
25756 bool saved_in_unbraced_linkage_specification_p;
25757 bool saved_in_function_body;
25758 unsigned saved_num_template_parameter_lists;
25759 cp_token *token;
25760 bool fully_implicit_function_template_p
25761 = parser->fully_implicit_function_template_p;
25762 parser->fully_implicit_function_template_p = false;
25763 tree implicit_template_parms
25764 = parser->implicit_template_parms;
25765 parser->implicit_template_parms = 0;
25766 cp_binding_level* implicit_template_scope
25767 = parser->implicit_template_scope;
25768 parser->implicit_template_scope = 0;
25770 saved_in_function_body = parser->in_function_body;
25771 parser->in_function_body = true;
25772 /* If the next token is `return', then the code may be trying to
25773 make use of the "named return value" extension that G++ used to
25774 support. */
25775 token = cp_lexer_peek_token (parser->lexer);
25776 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25778 /* Consume the `return' keyword. */
25779 cp_lexer_consume_token (parser->lexer);
25780 /* Look for the identifier that indicates what value is to be
25781 returned. */
25782 cp_parser_identifier (parser);
25783 /* Issue an error message. */
25784 error_at (token->location,
25785 "named return values are no longer supported");
25786 /* Skip tokens until we reach the start of the function body. */
25787 while (true)
25789 cp_token *token = cp_lexer_peek_token (parser->lexer);
25790 if (token->type == CPP_OPEN_BRACE
25791 || token->type == CPP_EOF
25792 || token->type == CPP_PRAGMA_EOL)
25793 break;
25794 cp_lexer_consume_token (parser->lexer);
25797 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25798 anything declared inside `f'. */
25799 saved_in_unbraced_linkage_specification_p
25800 = parser->in_unbraced_linkage_specification_p;
25801 parser->in_unbraced_linkage_specification_p = false;
25802 /* Inside the function, surrounding template-parameter-lists do not
25803 apply. */
25804 saved_num_template_parameter_lists
25805 = parser->num_template_parameter_lists;
25806 parser->num_template_parameter_lists = 0;
25808 start_lambda_scope (current_function_decl);
25810 /* If the next token is `try', `__transaction_atomic', or
25811 `__transaction_relaxed`, then we are looking at either function-try-block
25812 or function-transaction-block. Note that all of these include the
25813 function-body. */
25814 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25815 ctor_initializer_p = cp_parser_function_transaction (parser,
25816 RID_TRANSACTION_ATOMIC);
25817 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25818 RID_TRANSACTION_RELAXED))
25819 ctor_initializer_p = cp_parser_function_transaction (parser,
25820 RID_TRANSACTION_RELAXED);
25821 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25822 ctor_initializer_p = cp_parser_function_try_block (parser);
25823 else
25824 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25825 (parser, /*in_function_try_block=*/false);
25827 finish_lambda_scope ();
25829 /* Finish the function. */
25830 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25831 (inline_p ? 2 : 0));
25832 /* Generate code for it, if necessary. */
25833 expand_or_defer_fn (fn);
25834 /* Restore the saved values. */
25835 parser->in_unbraced_linkage_specification_p
25836 = saved_in_unbraced_linkage_specification_p;
25837 parser->num_template_parameter_lists
25838 = saved_num_template_parameter_lists;
25839 parser->in_function_body = saved_in_function_body;
25841 parser->fully_implicit_function_template_p
25842 = fully_implicit_function_template_p;
25843 parser->implicit_template_parms
25844 = implicit_template_parms;
25845 parser->implicit_template_scope
25846 = implicit_template_scope;
25848 if (parser->fully_implicit_function_template_p)
25849 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25851 return fn;
25854 /* Parse a template-declaration body (following argument list). */
25856 static void
25857 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25858 tree parameter_list,
25859 bool member_p)
25861 tree decl = NULL_TREE;
25862 bool friend_p = false;
25864 /* We just processed one more parameter list. */
25865 ++parser->num_template_parameter_lists;
25867 /* Get the deferred access checks from the parameter list. These
25868 will be checked once we know what is being declared, as for a
25869 member template the checks must be performed in the scope of the
25870 class containing the member. */
25871 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25873 /* Tentatively parse for a new template parameter list, which can either be
25874 the template keyword or a template introduction. */
25875 if (cp_parser_template_declaration_after_export (parser, member_p))
25876 /* OK */;
25877 else if (cxx_dialect >= cxx11
25878 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25879 decl = cp_parser_alias_declaration (parser);
25880 else
25882 /* There are no access checks when parsing a template, as we do not
25883 know if a specialization will be a friend. */
25884 push_deferring_access_checks (dk_no_check);
25885 cp_token *token = cp_lexer_peek_token (parser->lexer);
25886 decl = cp_parser_single_declaration (parser,
25887 checks,
25888 member_p,
25889 /*explicit_specialization_p=*/false,
25890 &friend_p);
25891 pop_deferring_access_checks ();
25893 /* If this is a member template declaration, let the front
25894 end know. */
25895 if (member_p && !friend_p && decl)
25897 if (TREE_CODE (decl) == TYPE_DECL)
25898 cp_parser_check_access_in_redeclaration (decl, token->location);
25900 decl = finish_member_template_decl (decl);
25902 else if (friend_p && decl
25903 && DECL_DECLARES_TYPE_P (decl))
25904 make_friend_class (current_class_type, TREE_TYPE (decl),
25905 /*complain=*/true);
25907 /* We are done with the current parameter list. */
25908 --parser->num_template_parameter_lists;
25910 pop_deferring_access_checks ();
25912 /* Finish up. */
25913 finish_template_decl (parameter_list);
25915 /* Check the template arguments for a literal operator template. */
25916 if (decl
25917 && DECL_DECLARES_FUNCTION_P (decl)
25918 && UDLIT_OPER_P (DECL_NAME (decl)))
25920 bool ok = true;
25921 if (parameter_list == NULL_TREE)
25922 ok = false;
25923 else
25925 int num_parms = TREE_VEC_LENGTH (parameter_list);
25926 if (num_parms == 1)
25928 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25929 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25930 if (TREE_TYPE (parm) != char_type_node
25931 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25932 ok = false;
25934 else if (num_parms == 2 && cxx_dialect >= cxx14)
25936 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25937 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25938 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25939 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25940 if (parm == error_mark_node
25941 || TREE_TYPE (parm) != TREE_TYPE (type)
25942 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25943 ok = false;
25945 else
25946 ok = false;
25948 if (!ok)
25950 if (cxx_dialect >= cxx14)
25951 error ("literal operator template %qD has invalid parameter list."
25952 " Expected non-type template argument pack <char...>"
25953 " or <typename CharT, CharT...>",
25954 decl);
25955 else
25956 error ("literal operator template %qD has invalid parameter list."
25957 " Expected non-type template argument pack <char...>",
25958 decl);
25962 /* Register member declarations. */
25963 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25964 finish_member_declaration (decl);
25965 /* If DECL is a function template, we must return to parse it later.
25966 (Even though there is no definition, there might be default
25967 arguments that need handling.) */
25968 if (member_p && decl
25969 && DECL_DECLARES_FUNCTION_P (decl))
25970 vec_safe_push (unparsed_funs_with_definitions, decl);
25973 /* Parse a template introduction header for a template-declaration. Returns
25974 false if tentative parse fails. */
25976 static bool
25977 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25979 cp_parser_parse_tentatively (parser);
25981 tree saved_scope = parser->scope;
25982 tree saved_object_scope = parser->object_scope;
25983 tree saved_qualifying_scope = parser->qualifying_scope;
25985 /* Look for the optional `::' operator. */
25986 cp_parser_global_scope_opt (parser,
25987 /*current_scope_valid_p=*/false);
25988 /* Look for the nested-name-specifier. */
25989 cp_parser_nested_name_specifier_opt (parser,
25990 /*typename_keyword_p=*/false,
25991 /*check_dependency_p=*/true,
25992 /*type_p=*/false,
25993 /*is_declaration=*/false);
25995 cp_token *token = cp_lexer_peek_token (parser->lexer);
25996 tree concept_name = cp_parser_identifier (parser);
25998 /* Look up the concept for which we will be matching
25999 template parameters. */
26000 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26001 token->location);
26002 parser->scope = saved_scope;
26003 parser->object_scope = saved_object_scope;
26004 parser->qualifying_scope = saved_qualifying_scope;
26006 if (concept_name == error_mark_node)
26007 cp_parser_simulate_error (parser);
26009 /* Look for opening brace for introduction. */
26010 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26012 if (!cp_parser_parse_definitely (parser))
26013 return false;
26015 push_deferring_access_checks (dk_deferred);
26017 /* Build vector of placeholder parameters and grab
26018 matching identifiers. */
26019 tree introduction_list = cp_parser_introduction_list (parser);
26021 /* The introduction-list shall not be empty. */
26022 int nargs = TREE_VEC_LENGTH (introduction_list);
26023 if (nargs == 0)
26025 error ("empty introduction-list");
26026 return true;
26029 /* Look for closing brace for introduction. */
26030 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26031 return true;
26033 if (tmpl_decl == error_mark_node)
26035 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26036 token->location);
26037 return true;
26040 /* Build and associate the constraint. */
26041 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26042 if (parms && parms != error_mark_node)
26044 cp_parser_template_declaration_after_parameters (parser, parms,
26045 member_p);
26046 return true;
26049 error_at (token->location, "no matching concept for template-introduction");
26050 return true;
26053 /* Parse a normal template-declaration following the template keyword. */
26055 static void
26056 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26058 tree parameter_list;
26059 bool need_lang_pop;
26060 location_t location = input_location;
26062 /* Look for the `<' token. */
26063 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26064 return;
26065 if (at_class_scope_p () && current_function_decl)
26067 /* 14.5.2.2 [temp.mem]
26069 A local class shall not have member templates. */
26070 error_at (location,
26071 "invalid declaration of member template in local class");
26072 cp_parser_skip_to_end_of_block_or_statement (parser);
26073 return;
26075 /* [temp]
26077 A template ... shall not have C linkage. */
26078 if (current_lang_name == lang_name_c)
26080 error_at (location, "template with C linkage");
26081 /* Give it C++ linkage to avoid confusing other parts of the
26082 front end. */
26083 push_lang_context (lang_name_cplusplus);
26084 need_lang_pop = true;
26086 else
26087 need_lang_pop = false;
26089 /* We cannot perform access checks on the template parameter
26090 declarations until we know what is being declared, just as we
26091 cannot check the decl-specifier list. */
26092 push_deferring_access_checks (dk_deferred);
26094 /* If the next token is `>', then we have an invalid
26095 specialization. Rather than complain about an invalid template
26096 parameter, issue an error message here. */
26097 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26099 cp_parser_error (parser, "invalid explicit specialization");
26100 begin_specialization ();
26101 parameter_list = NULL_TREE;
26103 else
26105 /* Parse the template parameters. */
26106 parameter_list = cp_parser_template_parameter_list (parser);
26109 /* Look for the `>'. */
26110 cp_parser_skip_to_end_of_template_parameter_list (parser);
26112 /* Manage template requirements */
26113 if (flag_concepts)
26115 tree reqs = get_shorthand_constraints (current_template_parms);
26116 if (tree r = cp_parser_requires_clause_opt (parser))
26117 reqs = conjoin_constraints (reqs, normalize_expression (r));
26118 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26121 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26122 member_p);
26124 /* For the erroneous case of a template with C linkage, we pushed an
26125 implicit C++ linkage scope; exit that scope now. */
26126 if (need_lang_pop)
26127 pop_lang_context ();
26130 /* Parse a template-declaration, assuming that the `export' (and
26131 `extern') keywords, if present, has already been scanned. MEMBER_P
26132 is as for cp_parser_template_declaration. */
26134 static bool
26135 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26137 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26139 cp_lexer_consume_token (parser->lexer);
26140 cp_parser_explicit_template_declaration (parser, member_p);
26141 return true;
26143 else if (flag_concepts)
26144 return cp_parser_template_introduction (parser, member_p);
26146 return false;
26149 /* Perform the deferred access checks from a template-parameter-list.
26150 CHECKS is a TREE_LIST of access checks, as returned by
26151 get_deferred_access_checks. */
26153 static void
26154 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26156 ++processing_template_parmlist;
26157 perform_access_checks (checks, tf_warning_or_error);
26158 --processing_template_parmlist;
26161 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26162 `function-definition' sequence that follows a template header.
26163 If MEMBER_P is true, this declaration appears in a class scope.
26165 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26166 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26168 static tree
26169 cp_parser_single_declaration (cp_parser* parser,
26170 vec<deferred_access_check, va_gc> *checks,
26171 bool member_p,
26172 bool explicit_specialization_p,
26173 bool* friend_p)
26175 int declares_class_or_enum;
26176 tree decl = NULL_TREE;
26177 cp_decl_specifier_seq decl_specifiers;
26178 bool function_definition_p = false;
26179 cp_token *decl_spec_token_start;
26181 /* This function is only used when processing a template
26182 declaration. */
26183 gcc_assert (innermost_scope_kind () == sk_template_parms
26184 || innermost_scope_kind () == sk_template_spec);
26186 /* Defer access checks until we know what is being declared. */
26187 push_deferring_access_checks (dk_deferred);
26189 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26190 alternative. */
26191 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26192 cp_parser_decl_specifier_seq (parser,
26193 CP_PARSER_FLAGS_OPTIONAL,
26194 &decl_specifiers,
26195 &declares_class_or_enum);
26196 if (friend_p)
26197 *friend_p = cp_parser_friend_p (&decl_specifiers);
26199 /* There are no template typedefs. */
26200 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26202 error_at (decl_spec_token_start->location,
26203 "template declaration of %<typedef%>");
26204 decl = error_mark_node;
26207 /* Gather up the access checks that occurred the
26208 decl-specifier-seq. */
26209 stop_deferring_access_checks ();
26211 /* Check for the declaration of a template class. */
26212 if (declares_class_or_enum)
26214 if (cp_parser_declares_only_class_p (parser)
26215 || (declares_class_or_enum & 2))
26217 // If this is a declaration, but not a definition, associate
26218 // any constraints with the type declaration. Constraints
26219 // are associated with definitions in cp_parser_class_specifier.
26220 if (declares_class_or_enum == 1)
26221 associate_classtype_constraints (decl_specifiers.type);
26223 decl = shadow_tag (&decl_specifiers);
26225 /* In this case:
26227 struct C {
26228 friend template <typename T> struct A<T>::B;
26231 A<T>::B will be represented by a TYPENAME_TYPE, and
26232 therefore not recognized by shadow_tag. */
26233 if (friend_p && *friend_p
26234 && !decl
26235 && decl_specifiers.type
26236 && TYPE_P (decl_specifiers.type))
26237 decl = decl_specifiers.type;
26239 if (decl && decl != error_mark_node)
26240 decl = TYPE_NAME (decl);
26241 else
26242 decl = error_mark_node;
26244 /* Perform access checks for template parameters. */
26245 cp_parser_perform_template_parameter_access_checks (checks);
26247 /* Give a helpful diagnostic for
26248 template <class T> struct A { } a;
26249 if we aren't already recovering from an error. */
26250 if (!cp_parser_declares_only_class_p (parser)
26251 && !seen_error ())
26253 error_at (cp_lexer_peek_token (parser->lexer)->location,
26254 "a class template declaration must not declare "
26255 "anything else");
26256 cp_parser_skip_to_end_of_block_or_statement (parser);
26257 goto out;
26262 /* Complain about missing 'typename' or other invalid type names. */
26263 if (!decl_specifiers.any_type_specifiers_p
26264 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26266 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26267 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26268 the rest of this declaration. */
26269 decl = error_mark_node;
26270 goto out;
26273 /* If it's not a template class, try for a template function. If
26274 the next token is a `;', then this declaration does not declare
26275 anything. But, if there were errors in the decl-specifiers, then
26276 the error might well have come from an attempted class-specifier.
26277 In that case, there's no need to warn about a missing declarator. */
26278 if (!decl
26279 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26280 || decl_specifiers.type != error_mark_node))
26282 decl = cp_parser_init_declarator (parser,
26283 &decl_specifiers,
26284 checks,
26285 /*function_definition_allowed_p=*/true,
26286 member_p,
26287 declares_class_or_enum,
26288 &function_definition_p,
26289 NULL, NULL, NULL);
26291 /* 7.1.1-1 [dcl.stc]
26293 A storage-class-specifier shall not be specified in an explicit
26294 specialization... */
26295 if (decl
26296 && explicit_specialization_p
26297 && decl_specifiers.storage_class != sc_none)
26299 error_at (decl_spec_token_start->location,
26300 "explicit template specialization cannot have a storage class");
26301 decl = error_mark_node;
26304 if (decl && VAR_P (decl))
26305 check_template_variable (decl);
26308 /* Look for a trailing `;' after the declaration. */
26309 if (!function_definition_p
26310 && (decl == error_mark_node
26311 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26312 cp_parser_skip_to_end_of_block_or_statement (parser);
26314 out:
26315 pop_deferring_access_checks ();
26317 /* Clear any current qualification; whatever comes next is the start
26318 of something new. */
26319 parser->scope = NULL_TREE;
26320 parser->qualifying_scope = NULL_TREE;
26321 parser->object_scope = NULL_TREE;
26323 return decl;
26326 /* Parse a cast-expression that is not the operand of a unary "&". */
26328 static cp_expr
26329 cp_parser_simple_cast_expression (cp_parser *parser)
26331 return cp_parser_cast_expression (parser, /*address_p=*/false,
26332 /*cast_p=*/false, /*decltype*/false, NULL);
26335 /* Parse a functional cast to TYPE. Returns an expression
26336 representing the cast. */
26338 static cp_expr
26339 cp_parser_functional_cast (cp_parser* parser, tree type)
26341 vec<tree, va_gc> *vec;
26342 tree expression_list;
26343 cp_expr cast;
26344 bool nonconst_p;
26346 location_t start_loc = input_location;
26348 if (!type)
26349 type = error_mark_node;
26351 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26353 cp_lexer_set_source_position (parser->lexer);
26354 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26355 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26356 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26357 if (TREE_CODE (type) == TYPE_DECL)
26358 type = TREE_TYPE (type);
26360 cast = finish_compound_literal (type, expression_list,
26361 tf_warning_or_error);
26362 /* Create a location of the form:
26363 type_name{i, f}
26364 ^~~~~~~~~~~~~~~
26365 with caret == start at the start of the type name,
26366 finishing at the closing brace. */
26367 location_t finish_loc
26368 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26369 location_t combined_loc = make_location (start_loc, start_loc,
26370 finish_loc);
26371 cast.set_location (combined_loc);
26372 return cast;
26376 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26377 /*cast_p=*/true,
26378 /*allow_expansion_p=*/true,
26379 /*non_constant_p=*/NULL);
26380 if (vec == NULL)
26381 expression_list = error_mark_node;
26382 else
26384 expression_list = build_tree_list_vec (vec);
26385 release_tree_vector (vec);
26388 cast = build_functional_cast (type, expression_list,
26389 tf_warning_or_error);
26390 /* [expr.const]/1: In an integral constant expression "only type
26391 conversions to integral or enumeration type can be used". */
26392 if (TREE_CODE (type) == TYPE_DECL)
26393 type = TREE_TYPE (type);
26394 if (cast != error_mark_node
26395 && !cast_valid_in_integral_constant_expression_p (type)
26396 && cp_parser_non_integral_constant_expression (parser,
26397 NIC_CONSTRUCTOR))
26398 return error_mark_node;
26400 /* Create a location of the form:
26401 float(i)
26402 ^~~~~~~~
26403 with caret == start at the start of the type name,
26404 finishing at the closing paren. */
26405 location_t finish_loc
26406 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26407 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26408 cast.set_location (combined_loc);
26409 return cast;
26412 /* Save the tokens that make up the body of a member function defined
26413 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26414 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26415 specifiers applied to the declaration. Returns the FUNCTION_DECL
26416 for the member function. */
26418 static tree
26419 cp_parser_save_member_function_body (cp_parser* parser,
26420 cp_decl_specifier_seq *decl_specifiers,
26421 cp_declarator *declarator,
26422 tree attributes)
26424 cp_token *first;
26425 cp_token *last;
26426 tree fn;
26427 bool function_try_block = false;
26429 /* Create the FUNCTION_DECL. */
26430 fn = grokmethod (decl_specifiers, declarator, attributes);
26431 cp_finalize_omp_declare_simd (parser, fn);
26432 cp_finalize_oacc_routine (parser, fn, true);
26433 /* If something went badly wrong, bail out now. */
26434 if (fn == error_mark_node)
26436 /* If there's a function-body, skip it. */
26437 if (cp_parser_token_starts_function_definition_p
26438 (cp_lexer_peek_token (parser->lexer)))
26439 cp_parser_skip_to_end_of_block_or_statement (parser);
26440 return error_mark_node;
26443 /* Remember it, if there default args to post process. */
26444 cp_parser_save_default_args (parser, fn);
26446 /* Save away the tokens that make up the body of the
26447 function. */
26448 first = parser->lexer->next_token;
26450 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26451 cp_lexer_consume_token (parser->lexer);
26452 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26453 RID_TRANSACTION_ATOMIC))
26455 cp_lexer_consume_token (parser->lexer);
26456 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26458 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26459 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26460 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26461 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26462 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26464 cp_lexer_consume_token (parser->lexer);
26465 cp_lexer_consume_token (parser->lexer);
26466 cp_lexer_consume_token (parser->lexer);
26467 cp_lexer_consume_token (parser->lexer);
26468 cp_lexer_consume_token (parser->lexer);
26470 else
26471 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26472 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26474 cp_lexer_consume_token (parser->lexer);
26475 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26476 break;
26480 /* Handle function try blocks. */
26481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26483 cp_lexer_consume_token (parser->lexer);
26484 function_try_block = true;
26486 /* We can have braced-init-list mem-initializers before the fn body. */
26487 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26489 cp_lexer_consume_token (parser->lexer);
26490 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26492 /* cache_group will stop after an un-nested { } pair, too. */
26493 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26494 break;
26496 /* variadic mem-inits have ... after the ')'. */
26497 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26498 cp_lexer_consume_token (parser->lexer);
26501 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26502 /* Handle function try blocks. */
26503 if (function_try_block)
26504 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26505 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26506 last = parser->lexer->next_token;
26508 /* Save away the inline definition; we will process it when the
26509 class is complete. */
26510 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26511 DECL_PENDING_INLINE_P (fn) = 1;
26513 /* We need to know that this was defined in the class, so that
26514 friend templates are handled correctly. */
26515 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26517 /* Add FN to the queue of functions to be parsed later. */
26518 vec_safe_push (unparsed_funs_with_definitions, fn);
26520 return fn;
26523 /* Save the tokens that make up the in-class initializer for a non-static
26524 data member. Returns a DEFAULT_ARG. */
26526 static tree
26527 cp_parser_save_nsdmi (cp_parser* parser)
26529 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26532 /* Parse a template-argument-list, as well as the trailing ">" (but
26533 not the opening "<"). See cp_parser_template_argument_list for the
26534 return value. */
26536 static tree
26537 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26539 tree arguments;
26540 tree saved_scope;
26541 tree saved_qualifying_scope;
26542 tree saved_object_scope;
26543 bool saved_greater_than_is_operator_p;
26544 int saved_unevaluated_operand;
26545 int saved_inhibit_evaluation_warnings;
26547 /* [temp.names]
26549 When parsing a template-id, the first non-nested `>' is taken as
26550 the end of the template-argument-list rather than a greater-than
26551 operator. */
26552 saved_greater_than_is_operator_p
26553 = parser->greater_than_is_operator_p;
26554 parser->greater_than_is_operator_p = false;
26555 /* Parsing the argument list may modify SCOPE, so we save it
26556 here. */
26557 saved_scope = parser->scope;
26558 saved_qualifying_scope = parser->qualifying_scope;
26559 saved_object_scope = parser->object_scope;
26560 /* We need to evaluate the template arguments, even though this
26561 template-id may be nested within a "sizeof". */
26562 saved_unevaluated_operand = cp_unevaluated_operand;
26563 cp_unevaluated_operand = 0;
26564 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26565 c_inhibit_evaluation_warnings = 0;
26566 /* Parse the template-argument-list itself. */
26567 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26568 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26569 arguments = NULL_TREE;
26570 else
26571 arguments = cp_parser_template_argument_list (parser);
26572 /* Look for the `>' that ends the template-argument-list. If we find
26573 a '>>' instead, it's probably just a typo. */
26574 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26576 if (cxx_dialect != cxx98)
26578 /* In C++0x, a `>>' in a template argument list or cast
26579 expression is considered to be two separate `>'
26580 tokens. So, change the current token to a `>', but don't
26581 consume it: it will be consumed later when the outer
26582 template argument list (or cast expression) is parsed.
26583 Note that this replacement of `>' for `>>' is necessary
26584 even if we are parsing tentatively: in the tentative
26585 case, after calling
26586 cp_parser_enclosed_template_argument_list we will always
26587 throw away all of the template arguments and the first
26588 closing `>', either because the template argument list
26589 was erroneous or because we are replacing those tokens
26590 with a CPP_TEMPLATE_ID token. The second `>' (which will
26591 not have been thrown away) is needed either to close an
26592 outer template argument list or to complete a new-style
26593 cast. */
26594 cp_token *token = cp_lexer_peek_token (parser->lexer);
26595 token->type = CPP_GREATER;
26597 else if (!saved_greater_than_is_operator_p)
26599 /* If we're in a nested template argument list, the '>>' has
26600 to be a typo for '> >'. We emit the error message, but we
26601 continue parsing and we push a '>' as next token, so that
26602 the argument list will be parsed correctly. Note that the
26603 global source location is still on the token before the
26604 '>>', so we need to say explicitly where we want it. */
26605 cp_token *token = cp_lexer_peek_token (parser->lexer);
26606 gcc_rich_location richloc (token->location);
26607 richloc.add_fixit_replace ("> >");
26608 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
26609 "within a nested template argument list");
26611 token->type = CPP_GREATER;
26613 else
26615 /* If this is not a nested template argument list, the '>>'
26616 is a typo for '>'. Emit an error message and continue.
26617 Same deal about the token location, but here we can get it
26618 right by consuming the '>>' before issuing the diagnostic. */
26619 cp_token *token = cp_lexer_consume_token (parser->lexer);
26620 error_at (token->location,
26621 "spurious %<>>%>, use %<>%> to terminate "
26622 "a template argument list");
26625 else
26626 cp_parser_skip_to_end_of_template_parameter_list (parser);
26627 /* The `>' token might be a greater-than operator again now. */
26628 parser->greater_than_is_operator_p
26629 = saved_greater_than_is_operator_p;
26630 /* Restore the SAVED_SCOPE. */
26631 parser->scope = saved_scope;
26632 parser->qualifying_scope = saved_qualifying_scope;
26633 parser->object_scope = saved_object_scope;
26634 cp_unevaluated_operand = saved_unevaluated_operand;
26635 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26637 return arguments;
26640 /* MEMBER_FUNCTION is a member function, or a friend. If default
26641 arguments, or the body of the function have not yet been parsed,
26642 parse them now. */
26644 static void
26645 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26647 timevar_push (TV_PARSE_INMETH);
26648 /* If this member is a template, get the underlying
26649 FUNCTION_DECL. */
26650 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26651 member_function = DECL_TEMPLATE_RESULT (member_function);
26653 /* There should not be any class definitions in progress at this
26654 point; the bodies of members are only parsed outside of all class
26655 definitions. */
26656 gcc_assert (parser->num_classes_being_defined == 0);
26657 /* While we're parsing the member functions we might encounter more
26658 classes. We want to handle them right away, but we don't want
26659 them getting mixed up with functions that are currently in the
26660 queue. */
26661 push_unparsed_function_queues (parser);
26663 /* Make sure that any template parameters are in scope. */
26664 maybe_begin_member_template_processing (member_function);
26666 /* If the body of the function has not yet been parsed, parse it
26667 now. */
26668 if (DECL_PENDING_INLINE_P (member_function))
26670 tree function_scope;
26671 cp_token_cache *tokens;
26673 /* The function is no longer pending; we are processing it. */
26674 tokens = DECL_PENDING_INLINE_INFO (member_function);
26675 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26676 DECL_PENDING_INLINE_P (member_function) = 0;
26678 /* If this is a local class, enter the scope of the containing
26679 function. */
26680 function_scope = current_function_decl;
26681 if (function_scope)
26682 push_function_context ();
26684 /* Push the body of the function onto the lexer stack. */
26685 cp_parser_push_lexer_for_tokens (parser, tokens);
26687 /* Let the front end know that we going to be defining this
26688 function. */
26689 start_preparsed_function (member_function, NULL_TREE,
26690 SF_PRE_PARSED | SF_INCLASS_INLINE);
26692 /* Don't do access checking if it is a templated function. */
26693 if (processing_template_decl)
26694 push_deferring_access_checks (dk_no_check);
26696 /* #pragma omp declare reduction needs special parsing. */
26697 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26699 parser->lexer->in_pragma = true;
26700 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26701 finish_function (/*inline*/2);
26702 cp_check_omp_declare_reduction (member_function);
26704 else
26705 /* Now, parse the body of the function. */
26706 cp_parser_function_definition_after_declarator (parser,
26707 /*inline_p=*/true);
26709 if (processing_template_decl)
26710 pop_deferring_access_checks ();
26712 /* Leave the scope of the containing function. */
26713 if (function_scope)
26714 pop_function_context ();
26715 cp_parser_pop_lexer (parser);
26718 /* Remove any template parameters from the symbol table. */
26719 maybe_end_member_template_processing ();
26721 /* Restore the queue. */
26722 pop_unparsed_function_queues (parser);
26723 timevar_pop (TV_PARSE_INMETH);
26726 /* If DECL contains any default args, remember it on the unparsed
26727 functions queue. */
26729 static void
26730 cp_parser_save_default_args (cp_parser* parser, tree decl)
26732 tree probe;
26734 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26735 probe;
26736 probe = TREE_CHAIN (probe))
26737 if (TREE_PURPOSE (probe))
26739 cp_default_arg_entry entry = {current_class_type, decl};
26740 vec_safe_push (unparsed_funs_with_default_args, entry);
26741 break;
26745 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26746 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26747 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26748 from the parameter-type-list. */
26750 static tree
26751 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26752 tree default_arg, tree parmtype)
26754 cp_token_cache *tokens;
26755 tree parsed_arg;
26756 bool dummy;
26758 if (default_arg == error_mark_node)
26759 return error_mark_node;
26761 /* Push the saved tokens for the default argument onto the parser's
26762 lexer stack. */
26763 tokens = DEFARG_TOKENS (default_arg);
26764 cp_parser_push_lexer_for_tokens (parser, tokens);
26766 start_lambda_scope (decl);
26768 /* Parse the default argument. */
26769 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26770 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26771 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26773 finish_lambda_scope ();
26775 if (parsed_arg == error_mark_node)
26776 cp_parser_skip_to_end_of_statement (parser);
26778 if (!processing_template_decl)
26780 /* In a non-template class, check conversions now. In a template,
26781 we'll wait and instantiate these as needed. */
26782 if (TREE_CODE (decl) == PARM_DECL)
26783 parsed_arg = check_default_argument (parmtype, parsed_arg,
26784 tf_warning_or_error);
26785 else
26786 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26789 /* If the token stream has not been completely used up, then
26790 there was extra junk after the end of the default
26791 argument. */
26792 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26794 if (TREE_CODE (decl) == PARM_DECL)
26795 cp_parser_error (parser, "expected %<,%>");
26796 else
26797 cp_parser_error (parser, "expected %<;%>");
26800 /* Revert to the main lexer. */
26801 cp_parser_pop_lexer (parser);
26803 return parsed_arg;
26806 /* FIELD is a non-static data member with an initializer which we saved for
26807 later; parse it now. */
26809 static void
26810 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26812 tree def;
26814 maybe_begin_member_template_processing (field);
26816 push_unparsed_function_queues (parser);
26817 def = cp_parser_late_parse_one_default_arg (parser, field,
26818 DECL_INITIAL (field),
26819 NULL_TREE);
26820 pop_unparsed_function_queues (parser);
26822 maybe_end_member_template_processing ();
26824 DECL_INITIAL (field) = def;
26827 /* FN is a FUNCTION_DECL which may contains a parameter with an
26828 unparsed DEFAULT_ARG. Parse the default args now. This function
26829 assumes that the current scope is the scope in which the default
26830 argument should be processed. */
26832 static void
26833 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26835 bool saved_local_variables_forbidden_p;
26836 tree parm, parmdecl;
26838 /* While we're parsing the default args, we might (due to the
26839 statement expression extension) encounter more classes. We want
26840 to handle them right away, but we don't want them getting mixed
26841 up with default args that are currently in the queue. */
26842 push_unparsed_function_queues (parser);
26844 /* Local variable names (and the `this' keyword) may not appear
26845 in a default argument. */
26846 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26847 parser->local_variables_forbidden_p = true;
26849 push_defarg_context (fn);
26851 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26852 parmdecl = DECL_ARGUMENTS (fn);
26853 parm && parm != void_list_node;
26854 parm = TREE_CHAIN (parm),
26855 parmdecl = DECL_CHAIN (parmdecl))
26857 tree default_arg = TREE_PURPOSE (parm);
26858 tree parsed_arg;
26859 vec<tree, va_gc> *insts;
26860 tree copy;
26861 unsigned ix;
26863 if (!default_arg)
26864 continue;
26866 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26867 /* This can happen for a friend declaration for a function
26868 already declared with default arguments. */
26869 continue;
26871 parsed_arg
26872 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26873 default_arg,
26874 TREE_VALUE (parm));
26875 if (parsed_arg == error_mark_node)
26877 continue;
26880 TREE_PURPOSE (parm) = parsed_arg;
26882 /* Update any instantiations we've already created. */
26883 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26884 vec_safe_iterate (insts, ix, &copy); ix++)
26885 TREE_PURPOSE (copy) = parsed_arg;
26888 pop_defarg_context ();
26890 /* Make sure no default arg is missing. */
26891 check_default_args (fn);
26893 /* Restore the state of local_variables_forbidden_p. */
26894 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26896 /* Restore the queue. */
26897 pop_unparsed_function_queues (parser);
26900 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26902 sizeof ... ( identifier )
26904 where the 'sizeof' token has already been consumed. */
26906 static tree
26907 cp_parser_sizeof_pack (cp_parser *parser)
26909 /* Consume the `...'. */
26910 cp_lexer_consume_token (parser->lexer);
26911 maybe_warn_variadic_templates ();
26913 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26914 if (paren)
26915 cp_lexer_consume_token (parser->lexer);
26916 else
26917 permerror (cp_lexer_peek_token (parser->lexer)->location,
26918 "%<sizeof...%> argument must be surrounded by parentheses");
26920 cp_token *token = cp_lexer_peek_token (parser->lexer);
26921 tree name = cp_parser_identifier (parser);
26922 if (name == error_mark_node)
26923 return error_mark_node;
26924 /* The name is not qualified. */
26925 parser->scope = NULL_TREE;
26926 parser->qualifying_scope = NULL_TREE;
26927 parser->object_scope = NULL_TREE;
26928 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26929 if (expr == error_mark_node)
26930 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26931 token->location);
26932 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26933 expr = TREE_TYPE (expr);
26934 else if (TREE_CODE (expr) == CONST_DECL)
26935 expr = DECL_INITIAL (expr);
26936 expr = make_pack_expansion (expr);
26937 PACK_EXPANSION_SIZEOF_P (expr) = true;
26939 if (paren)
26940 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26942 return expr;
26945 /* Parse the operand of `sizeof' (or a similar operator). Returns
26946 either a TYPE or an expression, depending on the form of the
26947 input. The KEYWORD indicates which kind of expression we have
26948 encountered. */
26950 static tree
26951 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26953 tree expr = NULL_TREE;
26954 const char *saved_message;
26955 char *tmp;
26956 bool saved_integral_constant_expression_p;
26957 bool saved_non_integral_constant_expression_p;
26959 /* If it's a `...', then we are computing the length of a parameter
26960 pack. */
26961 if (keyword == RID_SIZEOF
26962 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26963 return cp_parser_sizeof_pack (parser);
26965 /* Types cannot be defined in a `sizeof' expression. Save away the
26966 old message. */
26967 saved_message = parser->type_definition_forbidden_message;
26968 /* And create the new one. */
26969 tmp = concat ("types may not be defined in %<",
26970 IDENTIFIER_POINTER (ridpointers[keyword]),
26971 "%> expressions", NULL);
26972 parser->type_definition_forbidden_message = tmp;
26974 /* The restrictions on constant-expressions do not apply inside
26975 sizeof expressions. */
26976 saved_integral_constant_expression_p
26977 = parser->integral_constant_expression_p;
26978 saved_non_integral_constant_expression_p
26979 = parser->non_integral_constant_expression_p;
26980 parser->integral_constant_expression_p = false;
26982 /* Do not actually evaluate the expression. */
26983 ++cp_unevaluated_operand;
26984 ++c_inhibit_evaluation_warnings;
26985 /* If it's a `(', then we might be looking at the type-id
26986 construction. */
26987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26989 tree type = NULL_TREE;
26991 /* We can't be sure yet whether we're looking at a type-id or an
26992 expression. */
26993 cp_parser_parse_tentatively (parser);
26994 /* Note: as a GNU Extension, compound literals are considered
26995 postfix-expressions as they are in C99, so they are valid
26996 arguments to sizeof. See comment in cp_parser_cast_expression
26997 for details. */
26998 if (cp_parser_compound_literal_p (parser))
26999 cp_parser_simulate_error (parser);
27000 else
27002 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27003 parser->in_type_id_in_expr_p = true;
27004 /* Look for the type-id. */
27005 type = cp_parser_type_id (parser);
27006 /* Look for the closing `)'. */
27007 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27008 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27011 /* If all went well, then we're done. */
27012 if (cp_parser_parse_definitely (parser))
27014 cp_decl_specifier_seq decl_specs;
27016 /* Build a trivial decl-specifier-seq. */
27017 clear_decl_specs (&decl_specs);
27018 decl_specs.type = type;
27020 /* Call grokdeclarator to figure out what type this is. */
27021 expr = grokdeclarator (NULL,
27022 &decl_specs,
27023 TYPENAME,
27024 /*initialized=*/0,
27025 /*attrlist=*/NULL);
27029 /* If the type-id production did not work out, then we must be
27030 looking at the unary-expression production. */
27031 if (!expr)
27032 expr = cp_parser_unary_expression (parser);
27034 /* Go back to evaluating expressions. */
27035 --cp_unevaluated_operand;
27036 --c_inhibit_evaluation_warnings;
27038 /* Free the message we created. */
27039 free (tmp);
27040 /* And restore the old one. */
27041 parser->type_definition_forbidden_message = saved_message;
27042 parser->integral_constant_expression_p
27043 = saved_integral_constant_expression_p;
27044 parser->non_integral_constant_expression_p
27045 = saved_non_integral_constant_expression_p;
27047 return expr;
27050 /* If the current declaration has no declarator, return true. */
27052 static bool
27053 cp_parser_declares_only_class_p (cp_parser *parser)
27055 /* If the next token is a `;' or a `,' then there is no
27056 declarator. */
27057 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27058 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27061 /* Update the DECL_SPECS to reflect the storage class indicated by
27062 KEYWORD. */
27064 static void
27065 cp_parser_set_storage_class (cp_parser *parser,
27066 cp_decl_specifier_seq *decl_specs,
27067 enum rid keyword,
27068 cp_token *token)
27070 cp_storage_class storage_class;
27072 if (parser->in_unbraced_linkage_specification_p)
27074 error_at (token->location, "invalid use of %qD in linkage specification",
27075 ridpointers[keyword]);
27076 return;
27078 else if (decl_specs->storage_class != sc_none)
27080 decl_specs->conflicting_specifiers_p = true;
27081 return;
27084 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27085 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27086 && decl_specs->gnu_thread_keyword_p)
27088 pedwarn (decl_specs->locations[ds_thread], 0,
27089 "%<__thread%> before %qD", ridpointers[keyword]);
27092 switch (keyword)
27094 case RID_AUTO:
27095 storage_class = sc_auto;
27096 break;
27097 case RID_REGISTER:
27098 storage_class = sc_register;
27099 break;
27100 case RID_STATIC:
27101 storage_class = sc_static;
27102 break;
27103 case RID_EXTERN:
27104 storage_class = sc_extern;
27105 break;
27106 case RID_MUTABLE:
27107 storage_class = sc_mutable;
27108 break;
27109 default:
27110 gcc_unreachable ();
27112 decl_specs->storage_class = storage_class;
27113 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27115 /* A storage class specifier cannot be applied alongside a typedef
27116 specifier. If there is a typedef specifier present then set
27117 conflicting_specifiers_p which will trigger an error later
27118 on in grokdeclarator. */
27119 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27120 decl_specs->conflicting_specifiers_p = true;
27123 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27124 is true, the type is a class or enum definition. */
27126 static void
27127 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27128 tree type_spec,
27129 cp_token *token,
27130 bool type_definition_p)
27132 decl_specs->any_specifiers_p = true;
27134 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27135 (with, for example, in "typedef int wchar_t;") we remember that
27136 this is what happened. In system headers, we ignore these
27137 declarations so that G++ can work with system headers that are not
27138 C++-safe. */
27139 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27140 && !type_definition_p
27141 && (type_spec == boolean_type_node
27142 || type_spec == char16_type_node
27143 || type_spec == char32_type_node
27144 || type_spec == wchar_type_node)
27145 && (decl_specs->type
27146 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27147 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27148 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27149 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27151 decl_specs->redefined_builtin_type = type_spec;
27152 set_and_check_decl_spec_loc (decl_specs,
27153 ds_redefined_builtin_type_spec,
27154 token);
27155 if (!decl_specs->type)
27157 decl_specs->type = type_spec;
27158 decl_specs->type_definition_p = false;
27159 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27162 else if (decl_specs->type)
27163 decl_specs->multiple_types_p = true;
27164 else
27166 decl_specs->type = type_spec;
27167 decl_specs->type_definition_p = type_definition_p;
27168 decl_specs->redefined_builtin_type = NULL_TREE;
27169 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27173 /* True iff TOKEN is the GNU keyword __thread. */
27175 static bool
27176 token_is__thread (cp_token *token)
27178 gcc_assert (token->keyword == RID_THREAD);
27179 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27182 /* Set the location for a declarator specifier and check if it is
27183 duplicated.
27185 DECL_SPECS is the sequence of declarator specifiers onto which to
27186 set the location.
27188 DS is the single declarator specifier to set which location is to
27189 be set onto the existing sequence of declarators.
27191 LOCATION is the location for the declarator specifier to
27192 consider. */
27194 static void
27195 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27196 cp_decl_spec ds, cp_token *token)
27198 gcc_assert (ds < ds_last);
27200 if (decl_specs == NULL)
27201 return;
27203 source_location location = token->location;
27205 if (decl_specs->locations[ds] == 0)
27207 decl_specs->locations[ds] = location;
27208 if (ds == ds_thread)
27209 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27211 else
27213 if (ds == ds_long)
27215 if (decl_specs->locations[ds_long_long] != 0)
27216 error_at (location,
27217 "%<long long long%> is too long for GCC");
27218 else
27220 decl_specs->locations[ds_long_long] = location;
27221 pedwarn_cxx98 (location,
27222 OPT_Wlong_long,
27223 "ISO C++ 1998 does not support %<long long%>");
27226 else if (ds == ds_thread)
27228 bool gnu = token_is__thread (token);
27229 if (gnu != decl_specs->gnu_thread_keyword_p)
27230 error_at (location,
27231 "both %<__thread%> and %<thread_local%> specified");
27232 else
27233 error_at (location, "duplicate %qD", token->u.value);
27235 else
27237 static const char *const decl_spec_names[] = {
27238 "signed",
27239 "unsigned",
27240 "short",
27241 "long",
27242 "const",
27243 "volatile",
27244 "restrict",
27245 "inline",
27246 "virtual",
27247 "explicit",
27248 "friend",
27249 "typedef",
27250 "using",
27251 "constexpr",
27252 "__complex"
27254 error_at (location,
27255 "duplicate %qs", decl_spec_names[ds]);
27260 /* Return true iff the declarator specifier DS is present in the
27261 sequence of declarator specifiers DECL_SPECS. */
27263 bool
27264 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27265 cp_decl_spec ds)
27267 gcc_assert (ds < ds_last);
27269 if (decl_specs == NULL)
27270 return false;
27272 return decl_specs->locations[ds] != 0;
27275 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27276 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27278 static bool
27279 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27281 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27284 /* Issue an error message indicating that TOKEN_DESC was expected.
27285 If KEYWORD is true, it indicated this function is called by
27286 cp_parser_require_keword and the required token can only be
27287 a indicated keyword. */
27289 static void
27290 cp_parser_required_error (cp_parser *parser,
27291 required_token token_desc,
27292 bool keyword)
27294 switch (token_desc)
27296 case RT_NEW:
27297 cp_parser_error (parser, "expected %<new%>");
27298 return;
27299 case RT_DELETE:
27300 cp_parser_error (parser, "expected %<delete%>");
27301 return;
27302 case RT_RETURN:
27303 cp_parser_error (parser, "expected %<return%>");
27304 return;
27305 case RT_WHILE:
27306 cp_parser_error (parser, "expected %<while%>");
27307 return;
27308 case RT_EXTERN:
27309 cp_parser_error (parser, "expected %<extern%>");
27310 return;
27311 case RT_STATIC_ASSERT:
27312 cp_parser_error (parser, "expected %<static_assert%>");
27313 return;
27314 case RT_DECLTYPE:
27315 cp_parser_error (parser, "expected %<decltype%>");
27316 return;
27317 case RT_OPERATOR:
27318 cp_parser_error (parser, "expected %<operator%>");
27319 return;
27320 case RT_CLASS:
27321 cp_parser_error (parser, "expected %<class%>");
27322 return;
27323 case RT_TEMPLATE:
27324 cp_parser_error (parser, "expected %<template%>");
27325 return;
27326 case RT_NAMESPACE:
27327 cp_parser_error (parser, "expected %<namespace%>");
27328 return;
27329 case RT_USING:
27330 cp_parser_error (parser, "expected %<using%>");
27331 return;
27332 case RT_ASM:
27333 cp_parser_error (parser, "expected %<asm%>");
27334 return;
27335 case RT_TRY:
27336 cp_parser_error (parser, "expected %<try%>");
27337 return;
27338 case RT_CATCH:
27339 cp_parser_error (parser, "expected %<catch%>");
27340 return;
27341 case RT_THROW:
27342 cp_parser_error (parser, "expected %<throw%>");
27343 return;
27344 case RT_LABEL:
27345 cp_parser_error (parser, "expected %<__label__%>");
27346 return;
27347 case RT_AT_TRY:
27348 cp_parser_error (parser, "expected %<@try%>");
27349 return;
27350 case RT_AT_SYNCHRONIZED:
27351 cp_parser_error (parser, "expected %<@synchronized%>");
27352 return;
27353 case RT_AT_THROW:
27354 cp_parser_error (parser, "expected %<@throw%>");
27355 return;
27356 case RT_TRANSACTION_ATOMIC:
27357 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27358 return;
27359 case RT_TRANSACTION_RELAXED:
27360 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27361 return;
27362 default:
27363 break;
27365 if (!keyword)
27367 switch (token_desc)
27369 case RT_SEMICOLON:
27370 cp_parser_error (parser, "expected %<;%>");
27371 return;
27372 case RT_OPEN_PAREN:
27373 cp_parser_error (parser, "expected %<(%>");
27374 return;
27375 case RT_CLOSE_BRACE:
27376 cp_parser_error (parser, "expected %<}%>");
27377 return;
27378 case RT_OPEN_BRACE:
27379 cp_parser_error (parser, "expected %<{%>");
27380 return;
27381 case RT_CLOSE_SQUARE:
27382 cp_parser_error (parser, "expected %<]%>");
27383 return;
27384 case RT_OPEN_SQUARE:
27385 cp_parser_error (parser, "expected %<[%>");
27386 return;
27387 case RT_COMMA:
27388 cp_parser_error (parser, "expected %<,%>");
27389 return;
27390 case RT_SCOPE:
27391 cp_parser_error (parser, "expected %<::%>");
27392 return;
27393 case RT_LESS:
27394 cp_parser_error (parser, "expected %<<%>");
27395 return;
27396 case RT_GREATER:
27397 cp_parser_error (parser, "expected %<>%>");
27398 return;
27399 case RT_EQ:
27400 cp_parser_error (parser, "expected %<=%>");
27401 return;
27402 case RT_ELLIPSIS:
27403 cp_parser_error (parser, "expected %<...%>");
27404 return;
27405 case RT_MULT:
27406 cp_parser_error (parser, "expected %<*%>");
27407 return;
27408 case RT_COMPL:
27409 cp_parser_error (parser, "expected %<~%>");
27410 return;
27411 case RT_COLON:
27412 cp_parser_error (parser, "expected %<:%>");
27413 return;
27414 case RT_COLON_SCOPE:
27415 cp_parser_error (parser, "expected %<:%> or %<::%>");
27416 return;
27417 case RT_CLOSE_PAREN:
27418 cp_parser_error (parser, "expected %<)%>");
27419 return;
27420 case RT_COMMA_CLOSE_PAREN:
27421 cp_parser_error (parser, "expected %<,%> or %<)%>");
27422 return;
27423 case RT_PRAGMA_EOL:
27424 cp_parser_error (parser, "expected end of line");
27425 return;
27426 case RT_NAME:
27427 cp_parser_error (parser, "expected identifier");
27428 return;
27429 case RT_SELECT:
27430 cp_parser_error (parser, "expected selection-statement");
27431 return;
27432 case RT_INTERATION:
27433 cp_parser_error (parser, "expected iteration-statement");
27434 return;
27435 case RT_JUMP:
27436 cp_parser_error (parser, "expected jump-statement");
27437 return;
27438 case RT_CLASS_KEY:
27439 cp_parser_error (parser, "expected class-key");
27440 return;
27441 case RT_CLASS_TYPENAME_TEMPLATE:
27442 cp_parser_error (parser,
27443 "expected %<class%>, %<typename%>, or %<template%>");
27444 return;
27445 default:
27446 gcc_unreachable ();
27449 else
27450 gcc_unreachable ();
27455 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27456 issue an error message indicating that TOKEN_DESC was expected.
27458 Returns the token consumed, if the token had the appropriate type.
27459 Otherwise, returns NULL. */
27461 static cp_token *
27462 cp_parser_require (cp_parser* parser,
27463 enum cpp_ttype type,
27464 required_token token_desc)
27466 if (cp_lexer_next_token_is (parser->lexer, type))
27467 return cp_lexer_consume_token (parser->lexer);
27468 else
27470 /* Output the MESSAGE -- unless we're parsing tentatively. */
27471 if (!cp_parser_simulate_error (parser))
27472 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27473 return NULL;
27477 /* An error message is produced if the next token is not '>'.
27478 All further tokens are skipped until the desired token is
27479 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27481 static void
27482 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27484 /* Current level of '< ... >'. */
27485 unsigned level = 0;
27486 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27487 unsigned nesting_depth = 0;
27489 /* Are we ready, yet? If not, issue error message. */
27490 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27491 return;
27493 /* Skip tokens until the desired token is found. */
27494 while (true)
27496 /* Peek at the next token. */
27497 switch (cp_lexer_peek_token (parser->lexer)->type)
27499 case CPP_LESS:
27500 if (!nesting_depth)
27501 ++level;
27502 break;
27504 case CPP_RSHIFT:
27505 if (cxx_dialect == cxx98)
27506 /* C++0x views the `>>' operator as two `>' tokens, but
27507 C++98 does not. */
27508 break;
27509 else if (!nesting_depth && level-- == 0)
27511 /* We've hit a `>>' where the first `>' closes the
27512 template argument list, and the second `>' is
27513 spurious. Just consume the `>>' and stop; we've
27514 already produced at least one error. */
27515 cp_lexer_consume_token (parser->lexer);
27516 return;
27518 /* Fall through for C++0x, so we handle the second `>' in
27519 the `>>'. */
27520 gcc_fallthrough ();
27522 case CPP_GREATER:
27523 if (!nesting_depth && level-- == 0)
27525 /* We've reached the token we want, consume it and stop. */
27526 cp_lexer_consume_token (parser->lexer);
27527 return;
27529 break;
27531 case CPP_OPEN_PAREN:
27532 case CPP_OPEN_SQUARE:
27533 ++nesting_depth;
27534 break;
27536 case CPP_CLOSE_PAREN:
27537 case CPP_CLOSE_SQUARE:
27538 if (nesting_depth-- == 0)
27539 return;
27540 break;
27542 case CPP_EOF:
27543 case CPP_PRAGMA_EOL:
27544 case CPP_SEMICOLON:
27545 case CPP_OPEN_BRACE:
27546 case CPP_CLOSE_BRACE:
27547 /* The '>' was probably forgotten, don't look further. */
27548 return;
27550 default:
27551 break;
27554 /* Consume this token. */
27555 cp_lexer_consume_token (parser->lexer);
27559 /* If the next token is the indicated keyword, consume it. Otherwise,
27560 issue an error message indicating that TOKEN_DESC was expected.
27562 Returns the token consumed, if the token had the appropriate type.
27563 Otherwise, returns NULL. */
27565 static cp_token *
27566 cp_parser_require_keyword (cp_parser* parser,
27567 enum rid keyword,
27568 required_token token_desc)
27570 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27572 if (token && token->keyword != keyword)
27574 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27575 return NULL;
27578 return token;
27581 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27582 function-definition. */
27584 static bool
27585 cp_parser_token_starts_function_definition_p (cp_token* token)
27587 return (/* An ordinary function-body begins with an `{'. */
27588 token->type == CPP_OPEN_BRACE
27589 /* A ctor-initializer begins with a `:'. */
27590 || token->type == CPP_COLON
27591 /* A function-try-block begins with `try'. */
27592 || token->keyword == RID_TRY
27593 /* A function-transaction-block begins with `__transaction_atomic'
27594 or `__transaction_relaxed'. */
27595 || token->keyword == RID_TRANSACTION_ATOMIC
27596 || token->keyword == RID_TRANSACTION_RELAXED
27597 /* The named return value extension begins with `return'. */
27598 || token->keyword == RID_RETURN);
27601 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27602 definition. */
27604 static bool
27605 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27607 cp_token *token;
27609 token = cp_lexer_peek_token (parser->lexer);
27610 return (token->type == CPP_OPEN_BRACE
27611 || (token->type == CPP_COLON
27612 && !parser->colon_doesnt_start_class_def_p));
27615 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27616 C++0x) ending a template-argument. */
27618 static bool
27619 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27621 cp_token *token;
27623 token = cp_lexer_peek_token (parser->lexer);
27624 return (token->type == CPP_COMMA
27625 || token->type == CPP_GREATER
27626 || token->type == CPP_ELLIPSIS
27627 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27630 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27631 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27633 static bool
27634 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27635 size_t n)
27637 cp_token *token;
27639 token = cp_lexer_peek_nth_token (parser->lexer, n);
27640 if (token->type == CPP_LESS)
27641 return true;
27642 /* Check for the sequence `<::' in the original code. It would be lexed as
27643 `[:', where `[' is a digraph, and there is no whitespace before
27644 `:'. */
27645 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27647 cp_token *token2;
27648 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27649 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27650 return true;
27652 return false;
27655 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27656 or none_type otherwise. */
27658 static enum tag_types
27659 cp_parser_token_is_class_key (cp_token* token)
27661 switch (token->keyword)
27663 case RID_CLASS:
27664 return class_type;
27665 case RID_STRUCT:
27666 return record_type;
27667 case RID_UNION:
27668 return union_type;
27670 default:
27671 return none_type;
27675 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27676 or none_type otherwise or if the token is null. */
27678 static enum tag_types
27679 cp_parser_token_is_type_parameter_key (cp_token* token)
27681 if (!token)
27682 return none_type;
27684 switch (token->keyword)
27686 case RID_CLASS:
27687 return class_type;
27688 case RID_TYPENAME:
27689 return typename_type;
27691 default:
27692 return none_type;
27696 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27698 static void
27699 cp_parser_check_class_key (enum tag_types class_key, tree type)
27701 if (type == error_mark_node)
27702 return;
27703 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27705 if (permerror (input_location, "%qs tag used in naming %q#T",
27706 class_key == union_type ? "union"
27707 : class_key == record_type ? "struct" : "class",
27708 type))
27709 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27710 "%q#T was previously declared here", type);
27714 /* Issue an error message if DECL is redeclared with different
27715 access than its original declaration [class.access.spec/3].
27716 This applies to nested classes, nested class templates and
27717 enumerations [class.mem/1]. */
27719 static void
27720 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27722 if (!decl
27723 || (!CLASS_TYPE_P (TREE_TYPE (decl))
27724 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
27725 return;
27727 if ((TREE_PRIVATE (decl)
27728 != (current_access_specifier == access_private_node))
27729 || (TREE_PROTECTED (decl)
27730 != (current_access_specifier == access_protected_node)))
27731 error_at (location, "%qD redeclared with different access", decl);
27734 /* Look for the `template' keyword, as a syntactic disambiguator.
27735 Return TRUE iff it is present, in which case it will be
27736 consumed. */
27738 static bool
27739 cp_parser_optional_template_keyword (cp_parser *parser)
27741 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27743 /* In C++98 the `template' keyword can only be used within templates;
27744 outside templates the parser can always figure out what is a
27745 template and what is not. In C++11, per the resolution of DR 468,
27746 `template' is allowed in cases where it is not strictly necessary. */
27747 if (!processing_template_decl
27748 && pedantic && cxx_dialect == cxx98)
27750 cp_token *token = cp_lexer_peek_token (parser->lexer);
27751 pedwarn (token->location, OPT_Wpedantic,
27752 "in C++98 %<template%> (as a disambiguator) is only "
27753 "allowed within templates");
27754 /* If this part of the token stream is rescanned, the same
27755 error message would be generated. So, we purge the token
27756 from the stream. */
27757 cp_lexer_purge_token (parser->lexer);
27758 return false;
27760 else
27762 /* Consume the `template' keyword. */
27763 cp_lexer_consume_token (parser->lexer);
27764 return true;
27767 return false;
27770 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27771 set PARSER->SCOPE, and perform other related actions. */
27773 static void
27774 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27776 struct tree_check *check_value;
27778 /* Get the stored value. */
27779 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27780 /* Set the scope from the stored value. */
27781 parser->scope = saved_checks_value (check_value);
27782 parser->qualifying_scope = check_value->qualifying_scope;
27783 parser->object_scope = NULL_TREE;
27786 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27787 encounter the end of a block before what we were looking for. */
27789 static bool
27790 cp_parser_cache_group (cp_parser *parser,
27791 enum cpp_ttype end,
27792 unsigned depth)
27794 while (true)
27796 cp_token *token = cp_lexer_peek_token (parser->lexer);
27798 /* Abort a parenthesized expression if we encounter a semicolon. */
27799 if ((end == CPP_CLOSE_PAREN || depth == 0)
27800 && token->type == CPP_SEMICOLON)
27801 return true;
27802 /* If we've reached the end of the file, stop. */
27803 if (token->type == CPP_EOF
27804 || (end != CPP_PRAGMA_EOL
27805 && token->type == CPP_PRAGMA_EOL))
27806 return true;
27807 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27808 /* We've hit the end of an enclosing block, so there's been some
27809 kind of syntax error. */
27810 return true;
27812 /* Consume the token. */
27813 cp_lexer_consume_token (parser->lexer);
27814 /* See if it starts a new group. */
27815 if (token->type == CPP_OPEN_BRACE)
27817 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27818 /* In theory this should probably check end == '}', but
27819 cp_parser_save_member_function_body needs it to exit
27820 after either '}' or ')' when called with ')'. */
27821 if (depth == 0)
27822 return false;
27824 else if (token->type == CPP_OPEN_PAREN)
27826 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27827 if (depth == 0 && end == CPP_CLOSE_PAREN)
27828 return false;
27830 else if (token->type == CPP_PRAGMA)
27831 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27832 else if (token->type == end)
27833 return false;
27837 /* Like above, for caching a default argument or NSDMI. Both of these are
27838 terminated by a non-nested comma, but it can be unclear whether or not a
27839 comma is nested in a template argument list unless we do more parsing.
27840 In order to handle this ambiguity, when we encounter a ',' after a '<'
27841 we try to parse what follows as a parameter-declaration-list (in the
27842 case of a default argument) or a member-declarator (in the case of an
27843 NSDMI). If that succeeds, then we stop caching. */
27845 static tree
27846 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27848 unsigned depth = 0;
27849 int maybe_template_id = 0;
27850 cp_token *first_token;
27851 cp_token *token;
27852 tree default_argument;
27854 /* Add tokens until we have processed the entire default
27855 argument. We add the range [first_token, token). */
27856 first_token = cp_lexer_peek_token (parser->lexer);
27857 if (first_token->type == CPP_OPEN_BRACE)
27859 /* For list-initialization, this is straightforward. */
27860 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27861 token = cp_lexer_peek_token (parser->lexer);
27863 else while (true)
27865 bool done = false;
27867 /* Peek at the next token. */
27868 token = cp_lexer_peek_token (parser->lexer);
27869 /* What we do depends on what token we have. */
27870 switch (token->type)
27872 /* In valid code, a default argument must be
27873 immediately followed by a `,' `)', or `...'. */
27874 case CPP_COMMA:
27875 if (depth == 0 && maybe_template_id)
27877 /* If we've seen a '<', we might be in a
27878 template-argument-list. Until Core issue 325 is
27879 resolved, we don't know how this situation ought
27880 to be handled, so try to DTRT. We check whether
27881 what comes after the comma is a valid parameter
27882 declaration list. If it is, then the comma ends
27883 the default argument; otherwise the default
27884 argument continues. */
27885 bool error = false;
27886 cp_token *peek;
27888 /* Set ITALP so cp_parser_parameter_declaration_list
27889 doesn't decide to commit to this parse. */
27890 bool saved_italp = parser->in_template_argument_list_p;
27891 parser->in_template_argument_list_p = true;
27893 cp_parser_parse_tentatively (parser);
27895 if (nsdmi)
27897 /* Parse declarators until we reach a non-comma or
27898 somthing that cannot be an initializer.
27899 Just checking whether we're looking at a single
27900 declarator is insufficient. Consider:
27901 int var = tuple<T,U>::x;
27902 The template parameter 'U' looks exactly like a
27903 declarator. */
27906 int ctor_dtor_or_conv_p;
27907 cp_lexer_consume_token (parser->lexer);
27908 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27909 &ctor_dtor_or_conv_p,
27910 /*parenthesized_p=*/NULL,
27911 /*member_p=*/true,
27912 /*friend_p=*/false);
27913 peek = cp_lexer_peek_token (parser->lexer);
27914 if (cp_parser_error_occurred (parser))
27915 break;
27917 while (peek->type == CPP_COMMA);
27918 /* If we met an '=' or ';' then the original comma
27919 was the end of the NSDMI. Otherwise assume
27920 we're still in the NSDMI. */
27921 error = (peek->type != CPP_EQ
27922 && peek->type != CPP_SEMICOLON);
27924 else
27926 cp_lexer_consume_token (parser->lexer);
27927 begin_scope (sk_function_parms, NULL_TREE);
27928 cp_parser_parameter_declaration_list (parser, &error);
27929 pop_bindings_and_leave_scope ();
27931 if (!cp_parser_error_occurred (parser) && !error)
27932 done = true;
27933 cp_parser_abort_tentative_parse (parser);
27935 parser->in_template_argument_list_p = saved_italp;
27936 break;
27938 /* FALLTHRU */
27939 case CPP_CLOSE_PAREN:
27940 case CPP_ELLIPSIS:
27941 /* If we run into a non-nested `;', `}', or `]',
27942 then the code is invalid -- but the default
27943 argument is certainly over. */
27944 case CPP_SEMICOLON:
27945 case CPP_CLOSE_BRACE:
27946 case CPP_CLOSE_SQUARE:
27947 if (depth == 0
27948 /* Handle correctly int n = sizeof ... ( p ); */
27949 && token->type != CPP_ELLIPSIS)
27950 done = true;
27951 /* Update DEPTH, if necessary. */
27952 else if (token->type == CPP_CLOSE_PAREN
27953 || token->type == CPP_CLOSE_BRACE
27954 || token->type == CPP_CLOSE_SQUARE)
27955 --depth;
27956 break;
27958 case CPP_OPEN_PAREN:
27959 case CPP_OPEN_SQUARE:
27960 case CPP_OPEN_BRACE:
27961 ++depth;
27962 break;
27964 case CPP_LESS:
27965 if (depth == 0)
27966 /* This might be the comparison operator, or it might
27967 start a template argument list. */
27968 ++maybe_template_id;
27969 break;
27971 case CPP_RSHIFT:
27972 if (cxx_dialect == cxx98)
27973 break;
27974 /* Fall through for C++0x, which treats the `>>'
27975 operator like two `>' tokens in certain
27976 cases. */
27977 gcc_fallthrough ();
27979 case CPP_GREATER:
27980 if (depth == 0)
27982 /* This might be an operator, or it might close a
27983 template argument list. But if a previous '<'
27984 started a template argument list, this will have
27985 closed it, so we can't be in one anymore. */
27986 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27987 if (maybe_template_id < 0)
27988 maybe_template_id = 0;
27990 break;
27992 /* If we run out of tokens, issue an error message. */
27993 case CPP_EOF:
27994 case CPP_PRAGMA_EOL:
27995 error_at (token->location, "file ends in default argument");
27996 return error_mark_node;
27998 case CPP_NAME:
27999 case CPP_SCOPE:
28000 /* In these cases, we should look for template-ids.
28001 For example, if the default argument is
28002 `X<int, double>()', we need to do name lookup to
28003 figure out whether or not `X' is a template; if
28004 so, the `,' does not end the default argument.
28006 That is not yet done. */
28007 break;
28009 default:
28010 break;
28013 /* If we've reached the end, stop. */
28014 if (done)
28015 break;
28017 /* Add the token to the token block. */
28018 token = cp_lexer_consume_token (parser->lexer);
28021 /* Create a DEFAULT_ARG to represent the unparsed default
28022 argument. */
28023 default_argument = make_node (DEFAULT_ARG);
28024 DEFARG_TOKENS (default_argument)
28025 = cp_token_cache_new (first_token, token);
28026 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28028 return default_argument;
28031 /* Begin parsing tentatively. We always save tokens while parsing
28032 tentatively so that if the tentative parsing fails we can restore the
28033 tokens. */
28035 static void
28036 cp_parser_parse_tentatively (cp_parser* parser)
28038 /* Enter a new parsing context. */
28039 parser->context = cp_parser_context_new (parser->context);
28040 /* Begin saving tokens. */
28041 cp_lexer_save_tokens (parser->lexer);
28042 /* In order to avoid repetitive access control error messages,
28043 access checks are queued up until we are no longer parsing
28044 tentatively. */
28045 push_deferring_access_checks (dk_deferred);
28048 /* Commit to the currently active tentative parse. */
28050 static void
28051 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28053 cp_parser_context *context;
28054 cp_lexer *lexer;
28056 /* Mark all of the levels as committed. */
28057 lexer = parser->lexer;
28058 for (context = parser->context; context->next; context = context->next)
28060 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28061 break;
28062 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28063 while (!cp_lexer_saving_tokens (lexer))
28064 lexer = lexer->next;
28065 cp_lexer_commit_tokens (lexer);
28069 /* Commit to the topmost currently active tentative parse.
28071 Note that this function shouldn't be called when there are
28072 irreversible side-effects while in a tentative state. For
28073 example, we shouldn't create a permanent entry in the symbol
28074 table, or issue an error message that might not apply if the
28075 tentative parse is aborted. */
28077 static void
28078 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28080 cp_parser_context *context = parser->context;
28081 cp_lexer *lexer = parser->lexer;
28083 if (context)
28085 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28086 return;
28087 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28089 while (!cp_lexer_saving_tokens (lexer))
28090 lexer = lexer->next;
28091 cp_lexer_commit_tokens (lexer);
28095 /* Abort the currently active tentative parse. All consumed tokens
28096 will be rolled back, and no diagnostics will be issued. */
28098 static void
28099 cp_parser_abort_tentative_parse (cp_parser* parser)
28101 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28102 || errorcount > 0);
28103 cp_parser_simulate_error (parser);
28104 /* Now, pretend that we want to see if the construct was
28105 successfully parsed. */
28106 cp_parser_parse_definitely (parser);
28109 /* Stop parsing tentatively. If a parse error has occurred, restore the
28110 token stream. Otherwise, commit to the tokens we have consumed.
28111 Returns true if no error occurred; false otherwise. */
28113 static bool
28114 cp_parser_parse_definitely (cp_parser* parser)
28116 bool error_occurred;
28117 cp_parser_context *context;
28119 /* Remember whether or not an error occurred, since we are about to
28120 destroy that information. */
28121 error_occurred = cp_parser_error_occurred (parser);
28122 /* Remove the topmost context from the stack. */
28123 context = parser->context;
28124 parser->context = context->next;
28125 /* If no parse errors occurred, commit to the tentative parse. */
28126 if (!error_occurred)
28128 /* Commit to the tokens read tentatively, unless that was
28129 already done. */
28130 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28131 cp_lexer_commit_tokens (parser->lexer);
28133 pop_to_parent_deferring_access_checks ();
28135 /* Otherwise, if errors occurred, roll back our state so that things
28136 are just as they were before we began the tentative parse. */
28137 else
28139 cp_lexer_rollback_tokens (parser->lexer);
28140 pop_deferring_access_checks ();
28142 /* Add the context to the front of the free list. */
28143 context->next = cp_parser_context_free_list;
28144 cp_parser_context_free_list = context;
28146 return !error_occurred;
28149 /* Returns true if we are parsing tentatively and are not committed to
28150 this tentative parse. */
28152 static bool
28153 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28155 return (cp_parser_parsing_tentatively (parser)
28156 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28159 /* Returns nonzero iff an error has occurred during the most recent
28160 tentative parse. */
28162 static bool
28163 cp_parser_error_occurred (cp_parser* parser)
28165 return (cp_parser_parsing_tentatively (parser)
28166 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28169 /* Returns nonzero if GNU extensions are allowed. */
28171 static bool
28172 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28174 return parser->allow_gnu_extensions_p;
28177 /* Objective-C++ Productions */
28180 /* Parse an Objective-C expression, which feeds into a primary-expression
28181 above.
28183 objc-expression:
28184 objc-message-expression
28185 objc-string-literal
28186 objc-encode-expression
28187 objc-protocol-expression
28188 objc-selector-expression
28190 Returns a tree representation of the expression. */
28192 static cp_expr
28193 cp_parser_objc_expression (cp_parser* parser)
28195 /* Try to figure out what kind of declaration is present. */
28196 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28198 switch (kwd->type)
28200 case CPP_OPEN_SQUARE:
28201 return cp_parser_objc_message_expression (parser);
28203 case CPP_OBJC_STRING:
28204 kwd = cp_lexer_consume_token (parser->lexer);
28205 return objc_build_string_object (kwd->u.value);
28207 case CPP_KEYWORD:
28208 switch (kwd->keyword)
28210 case RID_AT_ENCODE:
28211 return cp_parser_objc_encode_expression (parser);
28213 case RID_AT_PROTOCOL:
28214 return cp_parser_objc_protocol_expression (parser);
28216 case RID_AT_SELECTOR:
28217 return cp_parser_objc_selector_expression (parser);
28219 default:
28220 break;
28222 default:
28223 error_at (kwd->location,
28224 "misplaced %<@%D%> Objective-C++ construct",
28225 kwd->u.value);
28226 cp_parser_skip_to_end_of_block_or_statement (parser);
28229 return error_mark_node;
28232 /* Parse an Objective-C message expression.
28234 objc-message-expression:
28235 [ objc-message-receiver objc-message-args ]
28237 Returns a representation of an Objective-C message. */
28239 static tree
28240 cp_parser_objc_message_expression (cp_parser* parser)
28242 tree receiver, messageargs;
28244 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28245 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28246 receiver = cp_parser_objc_message_receiver (parser);
28247 messageargs = cp_parser_objc_message_args (parser);
28248 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28249 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28251 tree result = objc_build_message_expr (receiver, messageargs);
28253 /* Construct a location e.g.
28254 [self func1:5]
28255 ^~~~~~~~~~~~~~
28256 ranging from the '[' to the ']', with the caret at the start. */
28257 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28258 protected_set_expr_location (result, combined_loc);
28260 return result;
28263 /* Parse an objc-message-receiver.
28265 objc-message-receiver:
28266 expression
28267 simple-type-specifier
28269 Returns a representation of the type or expression. */
28271 static tree
28272 cp_parser_objc_message_receiver (cp_parser* parser)
28274 tree rcv;
28276 /* An Objective-C message receiver may be either (1) a type
28277 or (2) an expression. */
28278 cp_parser_parse_tentatively (parser);
28279 rcv = cp_parser_expression (parser);
28281 /* If that worked out, fine. */
28282 if (cp_parser_parse_definitely (parser))
28283 return rcv;
28285 cp_parser_parse_tentatively (parser);
28286 rcv = cp_parser_simple_type_specifier (parser,
28287 /*decl_specs=*/NULL,
28288 CP_PARSER_FLAGS_NONE);
28290 if (cp_parser_parse_definitely (parser))
28291 return objc_get_class_reference (rcv);
28293 cp_parser_error (parser, "objective-c++ message receiver expected");
28294 return error_mark_node;
28297 /* Parse the arguments and selectors comprising an Objective-C message.
28299 objc-message-args:
28300 objc-selector
28301 objc-selector-args
28302 objc-selector-args , objc-comma-args
28304 objc-selector-args:
28305 objc-selector [opt] : assignment-expression
28306 objc-selector-args objc-selector [opt] : assignment-expression
28308 objc-comma-args:
28309 assignment-expression
28310 objc-comma-args , assignment-expression
28312 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28313 selector arguments and TREE_VALUE containing a list of comma
28314 arguments. */
28316 static tree
28317 cp_parser_objc_message_args (cp_parser* parser)
28319 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28320 bool maybe_unary_selector_p = true;
28321 cp_token *token = cp_lexer_peek_token (parser->lexer);
28323 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28325 tree selector = NULL_TREE, arg;
28327 if (token->type != CPP_COLON)
28328 selector = cp_parser_objc_selector (parser);
28330 /* Detect if we have a unary selector. */
28331 if (maybe_unary_selector_p
28332 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28333 return build_tree_list (selector, NULL_TREE);
28335 maybe_unary_selector_p = false;
28336 cp_parser_require (parser, CPP_COLON, RT_COLON);
28337 arg = cp_parser_assignment_expression (parser);
28339 sel_args
28340 = chainon (sel_args,
28341 build_tree_list (selector, arg));
28343 token = cp_lexer_peek_token (parser->lexer);
28346 /* Handle non-selector arguments, if any. */
28347 while (token->type == CPP_COMMA)
28349 tree arg;
28351 cp_lexer_consume_token (parser->lexer);
28352 arg = cp_parser_assignment_expression (parser);
28354 addl_args
28355 = chainon (addl_args,
28356 build_tree_list (NULL_TREE, arg));
28358 token = cp_lexer_peek_token (parser->lexer);
28361 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28363 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28364 return build_tree_list (error_mark_node, error_mark_node);
28367 return build_tree_list (sel_args, addl_args);
28370 /* Parse an Objective-C encode expression.
28372 objc-encode-expression:
28373 @encode objc-typename
28375 Returns an encoded representation of the type argument. */
28377 static cp_expr
28378 cp_parser_objc_encode_expression (cp_parser* parser)
28380 tree type;
28381 cp_token *token;
28382 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28384 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28385 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28386 token = cp_lexer_peek_token (parser->lexer);
28387 type = complete_type (cp_parser_type_id (parser));
28388 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28390 if (!type)
28392 error_at (token->location,
28393 "%<@encode%> must specify a type as an argument");
28394 return error_mark_node;
28397 /* This happens if we find @encode(T) (where T is a template
28398 typename or something dependent on a template typename) when
28399 parsing a template. In that case, we can't compile it
28400 immediately, but we rather create an AT_ENCODE_EXPR which will
28401 need to be instantiated when the template is used.
28403 if (dependent_type_p (type))
28405 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28406 TREE_READONLY (value) = 1;
28407 return value;
28411 /* Build a location of the form:
28412 @encode(int)
28413 ^~~~~~~~~~~~
28414 with caret==start at the @ token, finishing at the close paren. */
28415 location_t combined_loc
28416 = make_location (start_loc, start_loc,
28417 cp_lexer_previous_token (parser->lexer)->location);
28419 return cp_expr (objc_build_encode_expr (type), combined_loc);
28422 /* Parse an Objective-C @defs expression. */
28424 static tree
28425 cp_parser_objc_defs_expression (cp_parser *parser)
28427 tree name;
28429 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28430 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28431 name = cp_parser_identifier (parser);
28432 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28434 return objc_get_class_ivars (name);
28437 /* Parse an Objective-C protocol expression.
28439 objc-protocol-expression:
28440 @protocol ( identifier )
28442 Returns a representation of the protocol expression. */
28444 static tree
28445 cp_parser_objc_protocol_expression (cp_parser* parser)
28447 tree proto;
28448 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28450 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28451 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28452 proto = cp_parser_identifier (parser);
28453 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28455 /* Build a location of the form:
28456 @protocol(prot)
28457 ^~~~~~~~~~~~~~~
28458 with caret==start at the @ token, finishing at the close paren. */
28459 location_t combined_loc
28460 = make_location (start_loc, start_loc,
28461 cp_lexer_previous_token (parser->lexer)->location);
28462 tree result = objc_build_protocol_expr (proto);
28463 protected_set_expr_location (result, combined_loc);
28464 return result;
28467 /* Parse an Objective-C selector expression.
28469 objc-selector-expression:
28470 @selector ( objc-method-signature )
28472 objc-method-signature:
28473 objc-selector
28474 objc-selector-seq
28476 objc-selector-seq:
28477 objc-selector :
28478 objc-selector-seq objc-selector :
28480 Returns a representation of the method selector. */
28482 static tree
28483 cp_parser_objc_selector_expression (cp_parser* parser)
28485 tree sel_seq = NULL_TREE;
28486 bool maybe_unary_selector_p = true;
28487 cp_token *token;
28488 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28490 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28491 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28492 token = cp_lexer_peek_token (parser->lexer);
28494 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28495 || token->type == CPP_SCOPE)
28497 tree selector = NULL_TREE;
28499 if (token->type != CPP_COLON
28500 || token->type == CPP_SCOPE)
28501 selector = cp_parser_objc_selector (parser);
28503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28504 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28506 /* Detect if we have a unary selector. */
28507 if (maybe_unary_selector_p)
28509 sel_seq = selector;
28510 goto finish_selector;
28512 else
28514 cp_parser_error (parser, "expected %<:%>");
28517 maybe_unary_selector_p = false;
28518 token = cp_lexer_consume_token (parser->lexer);
28520 if (token->type == CPP_SCOPE)
28522 sel_seq
28523 = chainon (sel_seq,
28524 build_tree_list (selector, NULL_TREE));
28525 sel_seq
28526 = chainon (sel_seq,
28527 build_tree_list (NULL_TREE, NULL_TREE));
28529 else
28530 sel_seq
28531 = chainon (sel_seq,
28532 build_tree_list (selector, NULL_TREE));
28534 token = cp_lexer_peek_token (parser->lexer);
28537 finish_selector:
28538 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28541 /* Build a location of the form:
28542 @selector(func)
28543 ^~~~~~~~~~~~~~~
28544 with caret==start at the @ token, finishing at the close paren. */
28545 location_t combined_loc
28546 = make_location (loc, loc,
28547 cp_lexer_previous_token (parser->lexer)->location);
28548 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28549 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28550 protected_set_expr_location (result, combined_loc);
28551 return result;
28554 /* Parse a list of identifiers.
28556 objc-identifier-list:
28557 identifier
28558 objc-identifier-list , identifier
28560 Returns a TREE_LIST of identifier nodes. */
28562 static tree
28563 cp_parser_objc_identifier_list (cp_parser* parser)
28565 tree identifier;
28566 tree list;
28567 cp_token *sep;
28569 identifier = cp_parser_identifier (parser);
28570 if (identifier == error_mark_node)
28571 return error_mark_node;
28573 list = build_tree_list (NULL_TREE, identifier);
28574 sep = cp_lexer_peek_token (parser->lexer);
28576 while (sep->type == CPP_COMMA)
28578 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28579 identifier = cp_parser_identifier (parser);
28580 if (identifier == error_mark_node)
28581 return list;
28583 list = chainon (list, build_tree_list (NULL_TREE,
28584 identifier));
28585 sep = cp_lexer_peek_token (parser->lexer);
28588 return list;
28591 /* Parse an Objective-C alias declaration.
28593 objc-alias-declaration:
28594 @compatibility_alias identifier identifier ;
28596 This function registers the alias mapping with the Objective-C front end.
28597 It returns nothing. */
28599 static void
28600 cp_parser_objc_alias_declaration (cp_parser* parser)
28602 tree alias, orig;
28604 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28605 alias = cp_parser_identifier (parser);
28606 orig = cp_parser_identifier (parser);
28607 objc_declare_alias (alias, orig);
28608 cp_parser_consume_semicolon_at_end_of_statement (parser);
28611 /* Parse an Objective-C class forward-declaration.
28613 objc-class-declaration:
28614 @class objc-identifier-list ;
28616 The function registers the forward declarations with the Objective-C
28617 front end. It returns nothing. */
28619 static void
28620 cp_parser_objc_class_declaration (cp_parser* parser)
28622 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28623 while (true)
28625 tree id;
28627 id = cp_parser_identifier (parser);
28628 if (id == error_mark_node)
28629 break;
28631 objc_declare_class (id);
28633 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28634 cp_lexer_consume_token (parser->lexer);
28635 else
28636 break;
28638 cp_parser_consume_semicolon_at_end_of_statement (parser);
28641 /* Parse a list of Objective-C protocol references.
28643 objc-protocol-refs-opt:
28644 objc-protocol-refs [opt]
28646 objc-protocol-refs:
28647 < objc-identifier-list >
28649 Returns a TREE_LIST of identifiers, if any. */
28651 static tree
28652 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28654 tree protorefs = NULL_TREE;
28656 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28658 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28659 protorefs = cp_parser_objc_identifier_list (parser);
28660 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28663 return protorefs;
28666 /* Parse a Objective-C visibility specification. */
28668 static void
28669 cp_parser_objc_visibility_spec (cp_parser* parser)
28671 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28673 switch (vis->keyword)
28675 case RID_AT_PRIVATE:
28676 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28677 break;
28678 case RID_AT_PROTECTED:
28679 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28680 break;
28681 case RID_AT_PUBLIC:
28682 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28683 break;
28684 case RID_AT_PACKAGE:
28685 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28686 break;
28687 default:
28688 return;
28691 /* Eat '@private'/'@protected'/'@public'. */
28692 cp_lexer_consume_token (parser->lexer);
28695 /* Parse an Objective-C method type. Return 'true' if it is a class
28696 (+) method, and 'false' if it is an instance (-) method. */
28698 static inline bool
28699 cp_parser_objc_method_type (cp_parser* parser)
28701 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28702 return true;
28703 else
28704 return false;
28707 /* Parse an Objective-C protocol qualifier. */
28709 static tree
28710 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28712 tree quals = NULL_TREE, node;
28713 cp_token *token = cp_lexer_peek_token (parser->lexer);
28715 node = token->u.value;
28717 while (node && identifier_p (node)
28718 && (node == ridpointers [(int) RID_IN]
28719 || node == ridpointers [(int) RID_OUT]
28720 || node == ridpointers [(int) RID_INOUT]
28721 || node == ridpointers [(int) RID_BYCOPY]
28722 || node == ridpointers [(int) RID_BYREF]
28723 || node == ridpointers [(int) RID_ONEWAY]))
28725 quals = tree_cons (NULL_TREE, node, quals);
28726 cp_lexer_consume_token (parser->lexer);
28727 token = cp_lexer_peek_token (parser->lexer);
28728 node = token->u.value;
28731 return quals;
28734 /* Parse an Objective-C typename. */
28736 static tree
28737 cp_parser_objc_typename (cp_parser* parser)
28739 tree type_name = NULL_TREE;
28741 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28743 tree proto_quals, cp_type = NULL_TREE;
28745 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28746 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28748 /* An ObjC type name may consist of just protocol qualifiers, in which
28749 case the type shall default to 'id'. */
28750 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28752 cp_type = cp_parser_type_id (parser);
28754 /* If the type could not be parsed, an error has already
28755 been produced. For error recovery, behave as if it had
28756 not been specified, which will use the default type
28757 'id'. */
28758 if (cp_type == error_mark_node)
28760 cp_type = NULL_TREE;
28761 /* We need to skip to the closing parenthesis as
28762 cp_parser_type_id() does not seem to do it for
28763 us. */
28764 cp_parser_skip_to_closing_parenthesis (parser,
28765 /*recovering=*/true,
28766 /*or_comma=*/false,
28767 /*consume_paren=*/false);
28771 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28772 type_name = build_tree_list (proto_quals, cp_type);
28775 return type_name;
28778 /* Check to see if TYPE refers to an Objective-C selector name. */
28780 static bool
28781 cp_parser_objc_selector_p (enum cpp_ttype type)
28783 return (type == CPP_NAME || type == CPP_KEYWORD
28784 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28785 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28786 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28787 || type == CPP_XOR || type == CPP_XOR_EQ);
28790 /* Parse an Objective-C selector. */
28792 static tree
28793 cp_parser_objc_selector (cp_parser* parser)
28795 cp_token *token = cp_lexer_consume_token (parser->lexer);
28797 if (!cp_parser_objc_selector_p (token->type))
28799 error_at (token->location, "invalid Objective-C++ selector name");
28800 return error_mark_node;
28803 /* C++ operator names are allowed to appear in ObjC selectors. */
28804 switch (token->type)
28806 case CPP_AND_AND: return get_identifier ("and");
28807 case CPP_AND_EQ: return get_identifier ("and_eq");
28808 case CPP_AND: return get_identifier ("bitand");
28809 case CPP_OR: return get_identifier ("bitor");
28810 case CPP_COMPL: return get_identifier ("compl");
28811 case CPP_NOT: return get_identifier ("not");
28812 case CPP_NOT_EQ: return get_identifier ("not_eq");
28813 case CPP_OR_OR: return get_identifier ("or");
28814 case CPP_OR_EQ: return get_identifier ("or_eq");
28815 case CPP_XOR: return get_identifier ("xor");
28816 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28817 default: return token->u.value;
28821 /* Parse an Objective-C params list. */
28823 static tree
28824 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28826 tree params = NULL_TREE;
28827 bool maybe_unary_selector_p = true;
28828 cp_token *token = cp_lexer_peek_token (parser->lexer);
28830 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28832 tree selector = NULL_TREE, type_name, identifier;
28833 tree parm_attr = NULL_TREE;
28835 if (token->keyword == RID_ATTRIBUTE)
28836 break;
28838 if (token->type != CPP_COLON)
28839 selector = cp_parser_objc_selector (parser);
28841 /* Detect if we have a unary selector. */
28842 if (maybe_unary_selector_p
28843 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28845 params = selector; /* Might be followed by attributes. */
28846 break;
28849 maybe_unary_selector_p = false;
28850 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28852 /* Something went quite wrong. There should be a colon
28853 here, but there is not. Stop parsing parameters. */
28854 break;
28856 type_name = cp_parser_objc_typename (parser);
28857 /* New ObjC allows attributes on parameters too. */
28858 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28859 parm_attr = cp_parser_attributes_opt (parser);
28860 identifier = cp_parser_identifier (parser);
28862 params
28863 = chainon (params,
28864 objc_build_keyword_decl (selector,
28865 type_name,
28866 identifier,
28867 parm_attr));
28869 token = cp_lexer_peek_token (parser->lexer);
28872 if (params == NULL_TREE)
28874 cp_parser_error (parser, "objective-c++ method declaration is expected");
28875 return error_mark_node;
28878 /* We allow tail attributes for the method. */
28879 if (token->keyword == RID_ATTRIBUTE)
28881 *attributes = cp_parser_attributes_opt (parser);
28882 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28883 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28884 return params;
28885 cp_parser_error (parser,
28886 "method attributes must be specified at the end");
28887 return error_mark_node;
28890 if (params == NULL_TREE)
28892 cp_parser_error (parser, "objective-c++ method declaration is expected");
28893 return error_mark_node;
28895 return params;
28898 /* Parse the non-keyword Objective-C params. */
28900 static tree
28901 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28902 tree* attributes)
28904 tree params = make_node (TREE_LIST);
28905 cp_token *token = cp_lexer_peek_token (parser->lexer);
28906 *ellipsisp = false; /* Initially, assume no ellipsis. */
28908 while (token->type == CPP_COMMA)
28910 cp_parameter_declarator *parmdecl;
28911 tree parm;
28913 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28914 token = cp_lexer_peek_token (parser->lexer);
28916 if (token->type == CPP_ELLIPSIS)
28918 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28919 *ellipsisp = true;
28920 token = cp_lexer_peek_token (parser->lexer);
28921 break;
28924 /* TODO: parse attributes for tail parameters. */
28925 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28926 parm = grokdeclarator (parmdecl->declarator,
28927 &parmdecl->decl_specifiers,
28928 PARM, /*initialized=*/0,
28929 /*attrlist=*/NULL);
28931 chainon (params, build_tree_list (NULL_TREE, parm));
28932 token = cp_lexer_peek_token (parser->lexer);
28935 /* We allow tail attributes for the method. */
28936 if (token->keyword == RID_ATTRIBUTE)
28938 if (*attributes == NULL_TREE)
28940 *attributes = cp_parser_attributes_opt (parser);
28941 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28942 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28943 return params;
28945 else
28946 /* We have an error, but parse the attributes, so that we can
28947 carry on. */
28948 *attributes = cp_parser_attributes_opt (parser);
28950 cp_parser_error (parser,
28951 "method attributes must be specified at the end");
28952 return error_mark_node;
28955 return params;
28958 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28960 static void
28961 cp_parser_objc_interstitial_code (cp_parser* parser)
28963 cp_token *token = cp_lexer_peek_token (parser->lexer);
28965 /* If the next token is `extern' and the following token is a string
28966 literal, then we have a linkage specification. */
28967 if (token->keyword == RID_EXTERN
28968 && cp_parser_is_pure_string_literal
28969 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28970 cp_parser_linkage_specification (parser);
28971 /* Handle #pragma, if any. */
28972 else if (token->type == CPP_PRAGMA)
28973 cp_parser_pragma (parser, pragma_objc_icode, NULL);
28974 /* Allow stray semicolons. */
28975 else if (token->type == CPP_SEMICOLON)
28976 cp_lexer_consume_token (parser->lexer);
28977 /* Mark methods as optional or required, when building protocols. */
28978 else if (token->keyword == RID_AT_OPTIONAL)
28980 cp_lexer_consume_token (parser->lexer);
28981 objc_set_method_opt (true);
28983 else if (token->keyword == RID_AT_REQUIRED)
28985 cp_lexer_consume_token (parser->lexer);
28986 objc_set_method_opt (false);
28988 else if (token->keyword == RID_NAMESPACE)
28989 cp_parser_namespace_definition (parser);
28990 /* Other stray characters must generate errors. */
28991 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28993 cp_lexer_consume_token (parser->lexer);
28994 error ("stray %qs between Objective-C++ methods",
28995 token->type == CPP_OPEN_BRACE ? "{" : "}");
28997 /* Finally, try to parse a block-declaration, or a function-definition. */
28998 else
28999 cp_parser_block_declaration (parser, /*statement_p=*/false);
29002 /* Parse a method signature. */
29004 static tree
29005 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29007 tree rettype, kwdparms, optparms;
29008 bool ellipsis = false;
29009 bool is_class_method;
29011 is_class_method = cp_parser_objc_method_type (parser);
29012 rettype = cp_parser_objc_typename (parser);
29013 *attributes = NULL_TREE;
29014 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29015 if (kwdparms == error_mark_node)
29016 return error_mark_node;
29017 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29018 if (optparms == error_mark_node)
29019 return error_mark_node;
29021 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29024 static bool
29025 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29027 tree tattr;
29028 cp_lexer_save_tokens (parser->lexer);
29029 tattr = cp_parser_attributes_opt (parser);
29030 gcc_assert (tattr) ;
29032 /* If the attributes are followed by a method introducer, this is not allowed.
29033 Dump the attributes and flag the situation. */
29034 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29035 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29036 return true;
29038 /* Otherwise, the attributes introduce some interstitial code, possibly so
29039 rewind to allow that check. */
29040 cp_lexer_rollback_tokens (parser->lexer);
29041 return false;
29044 /* Parse an Objective-C method prototype list. */
29046 static void
29047 cp_parser_objc_method_prototype_list (cp_parser* parser)
29049 cp_token *token = cp_lexer_peek_token (parser->lexer);
29051 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29053 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29055 tree attributes, sig;
29056 bool is_class_method;
29057 if (token->type == CPP_PLUS)
29058 is_class_method = true;
29059 else
29060 is_class_method = false;
29061 sig = cp_parser_objc_method_signature (parser, &attributes);
29062 if (sig == error_mark_node)
29064 cp_parser_skip_to_end_of_block_or_statement (parser);
29065 token = cp_lexer_peek_token (parser->lexer);
29066 continue;
29068 objc_add_method_declaration (is_class_method, sig, attributes);
29069 cp_parser_consume_semicolon_at_end_of_statement (parser);
29071 else if (token->keyword == RID_AT_PROPERTY)
29072 cp_parser_objc_at_property_declaration (parser);
29073 else if (token->keyword == RID_ATTRIBUTE
29074 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29075 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29076 OPT_Wattributes,
29077 "prefix attributes are ignored for methods");
29078 else
29079 /* Allow for interspersed non-ObjC++ code. */
29080 cp_parser_objc_interstitial_code (parser);
29082 token = cp_lexer_peek_token (parser->lexer);
29085 if (token->type != CPP_EOF)
29086 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29087 else
29088 cp_parser_error (parser, "expected %<@end%>");
29090 objc_finish_interface ();
29093 /* Parse an Objective-C method definition list. */
29095 static void
29096 cp_parser_objc_method_definition_list (cp_parser* parser)
29098 cp_token *token = cp_lexer_peek_token (parser->lexer);
29100 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29102 tree meth;
29104 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29106 cp_token *ptk;
29107 tree sig, attribute;
29108 bool is_class_method;
29109 if (token->type == CPP_PLUS)
29110 is_class_method = true;
29111 else
29112 is_class_method = false;
29113 push_deferring_access_checks (dk_deferred);
29114 sig = cp_parser_objc_method_signature (parser, &attribute);
29115 if (sig == error_mark_node)
29117 cp_parser_skip_to_end_of_block_or_statement (parser);
29118 token = cp_lexer_peek_token (parser->lexer);
29119 continue;
29121 objc_start_method_definition (is_class_method, sig, attribute,
29122 NULL_TREE);
29124 /* For historical reasons, we accept an optional semicolon. */
29125 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29126 cp_lexer_consume_token (parser->lexer);
29128 ptk = cp_lexer_peek_token (parser->lexer);
29129 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29130 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29132 perform_deferred_access_checks (tf_warning_or_error);
29133 stop_deferring_access_checks ();
29134 meth = cp_parser_function_definition_after_declarator (parser,
29135 false);
29136 pop_deferring_access_checks ();
29137 objc_finish_method_definition (meth);
29140 /* The following case will be removed once @synthesize is
29141 completely implemented. */
29142 else if (token->keyword == RID_AT_PROPERTY)
29143 cp_parser_objc_at_property_declaration (parser);
29144 else if (token->keyword == RID_AT_SYNTHESIZE)
29145 cp_parser_objc_at_synthesize_declaration (parser);
29146 else if (token->keyword == RID_AT_DYNAMIC)
29147 cp_parser_objc_at_dynamic_declaration (parser);
29148 else if (token->keyword == RID_ATTRIBUTE
29149 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29150 warning_at (token->location, OPT_Wattributes,
29151 "prefix attributes are ignored for methods");
29152 else
29153 /* Allow for interspersed non-ObjC++ code. */
29154 cp_parser_objc_interstitial_code (parser);
29156 token = cp_lexer_peek_token (parser->lexer);
29159 if (token->type != CPP_EOF)
29160 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29161 else
29162 cp_parser_error (parser, "expected %<@end%>");
29164 objc_finish_implementation ();
29167 /* Parse Objective-C ivars. */
29169 static void
29170 cp_parser_objc_class_ivars (cp_parser* parser)
29172 cp_token *token = cp_lexer_peek_token (parser->lexer);
29174 if (token->type != CPP_OPEN_BRACE)
29175 return; /* No ivars specified. */
29177 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29178 token = cp_lexer_peek_token (parser->lexer);
29180 while (token->type != CPP_CLOSE_BRACE
29181 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29183 cp_decl_specifier_seq declspecs;
29184 int decl_class_or_enum_p;
29185 tree prefix_attributes;
29187 cp_parser_objc_visibility_spec (parser);
29189 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29190 break;
29192 cp_parser_decl_specifier_seq (parser,
29193 CP_PARSER_FLAGS_OPTIONAL,
29194 &declspecs,
29195 &decl_class_or_enum_p);
29197 /* auto, register, static, extern, mutable. */
29198 if (declspecs.storage_class != sc_none)
29200 cp_parser_error (parser, "invalid type for instance variable");
29201 declspecs.storage_class = sc_none;
29204 /* thread_local. */
29205 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29207 cp_parser_error (parser, "invalid type for instance variable");
29208 declspecs.locations[ds_thread] = 0;
29211 /* typedef. */
29212 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29214 cp_parser_error (parser, "invalid type for instance variable");
29215 declspecs.locations[ds_typedef] = 0;
29218 prefix_attributes = declspecs.attributes;
29219 declspecs.attributes = NULL_TREE;
29221 /* Keep going until we hit the `;' at the end of the
29222 declaration. */
29223 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29225 tree width = NULL_TREE, attributes, first_attribute, decl;
29226 cp_declarator *declarator = NULL;
29227 int ctor_dtor_or_conv_p;
29229 /* Check for a (possibly unnamed) bitfield declaration. */
29230 token = cp_lexer_peek_token (parser->lexer);
29231 if (token->type == CPP_COLON)
29232 goto eat_colon;
29234 if (token->type == CPP_NAME
29235 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29236 == CPP_COLON))
29238 /* Get the name of the bitfield. */
29239 declarator = make_id_declarator (NULL_TREE,
29240 cp_parser_identifier (parser),
29241 sfk_none);
29243 eat_colon:
29244 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29245 /* Get the width of the bitfield. */
29246 width
29247 = cp_parser_constant_expression (parser);
29249 else
29251 /* Parse the declarator. */
29252 declarator
29253 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29254 &ctor_dtor_or_conv_p,
29255 /*parenthesized_p=*/NULL,
29256 /*member_p=*/false,
29257 /*friend_p=*/false);
29260 /* Look for attributes that apply to the ivar. */
29261 attributes = cp_parser_attributes_opt (parser);
29262 /* Remember which attributes are prefix attributes and
29263 which are not. */
29264 first_attribute = attributes;
29265 /* Combine the attributes. */
29266 attributes = chainon (prefix_attributes, attributes);
29268 if (width)
29269 /* Create the bitfield declaration. */
29270 decl = grokbitfield (declarator, &declspecs,
29271 width,
29272 attributes);
29273 else
29274 decl = grokfield (declarator, &declspecs,
29275 NULL_TREE, /*init_const_expr_p=*/false,
29276 NULL_TREE, attributes);
29278 /* Add the instance variable. */
29279 if (decl != error_mark_node && decl != NULL_TREE)
29280 objc_add_instance_variable (decl);
29282 /* Reset PREFIX_ATTRIBUTES. */
29283 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29284 attributes = TREE_CHAIN (attributes);
29285 if (attributes)
29286 TREE_CHAIN (attributes) = NULL_TREE;
29288 token = cp_lexer_peek_token (parser->lexer);
29290 if (token->type == CPP_COMMA)
29292 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29293 continue;
29295 break;
29298 cp_parser_consume_semicolon_at_end_of_statement (parser);
29299 token = cp_lexer_peek_token (parser->lexer);
29302 if (token->keyword == RID_AT_END)
29303 cp_parser_error (parser, "expected %<}%>");
29305 /* Do not consume the RID_AT_END, so it will be read again as terminating
29306 the @interface of @implementation. */
29307 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29308 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29310 /* For historical reasons, we accept an optional semicolon. */
29311 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29312 cp_lexer_consume_token (parser->lexer);
29315 /* Parse an Objective-C protocol declaration. */
29317 static void
29318 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29320 tree proto, protorefs;
29321 cp_token *tok;
29323 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29326 tok = cp_lexer_peek_token (parser->lexer);
29327 error_at (tok->location, "identifier expected after %<@protocol%>");
29328 cp_parser_consume_semicolon_at_end_of_statement (parser);
29329 return;
29332 /* See if we have a forward declaration or a definition. */
29333 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29335 /* Try a forward declaration first. */
29336 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29338 while (true)
29340 tree id;
29342 id = cp_parser_identifier (parser);
29343 if (id == error_mark_node)
29344 break;
29346 objc_declare_protocol (id, attributes);
29348 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29349 cp_lexer_consume_token (parser->lexer);
29350 else
29351 break;
29353 cp_parser_consume_semicolon_at_end_of_statement (parser);
29356 /* Ok, we got a full-fledged definition (or at least should). */
29357 else
29359 proto = cp_parser_identifier (parser);
29360 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29361 objc_start_protocol (proto, protorefs, attributes);
29362 cp_parser_objc_method_prototype_list (parser);
29366 /* Parse an Objective-C superclass or category. */
29368 static void
29369 cp_parser_objc_superclass_or_category (cp_parser *parser,
29370 bool iface_p,
29371 tree *super,
29372 tree *categ, bool *is_class_extension)
29374 cp_token *next = cp_lexer_peek_token (parser->lexer);
29376 *super = *categ = NULL_TREE;
29377 *is_class_extension = false;
29378 if (next->type == CPP_COLON)
29380 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29381 *super = cp_parser_identifier (parser);
29383 else if (next->type == CPP_OPEN_PAREN)
29385 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29387 /* If there is no category name, and this is an @interface, we
29388 have a class extension. */
29389 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29391 *categ = NULL_TREE;
29392 *is_class_extension = true;
29394 else
29395 *categ = cp_parser_identifier (parser);
29397 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29401 /* Parse an Objective-C class interface. */
29403 static void
29404 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29406 tree name, super, categ, protos;
29407 bool is_class_extension;
29409 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29410 name = cp_parser_identifier (parser);
29411 if (name == error_mark_node)
29413 /* It's hard to recover because even if valid @interface stuff
29414 is to follow, we can't compile it (or validate it) if we
29415 don't even know which class it refers to. Let's assume this
29416 was a stray '@interface' token in the stream and skip it.
29418 return;
29420 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29421 &is_class_extension);
29422 protos = cp_parser_objc_protocol_refs_opt (parser);
29424 /* We have either a class or a category on our hands. */
29425 if (categ || is_class_extension)
29426 objc_start_category_interface (name, categ, protos, attributes);
29427 else
29429 objc_start_class_interface (name, super, protos, attributes);
29430 /* Handle instance variable declarations, if any. */
29431 cp_parser_objc_class_ivars (parser);
29432 objc_continue_interface ();
29435 cp_parser_objc_method_prototype_list (parser);
29438 /* Parse an Objective-C class implementation. */
29440 static void
29441 cp_parser_objc_class_implementation (cp_parser* parser)
29443 tree name, super, categ;
29444 bool is_class_extension;
29446 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29447 name = cp_parser_identifier (parser);
29448 if (name == error_mark_node)
29450 /* It's hard to recover because even if valid @implementation
29451 stuff is to follow, we can't compile it (or validate it) if
29452 we don't even know which class it refers to. Let's assume
29453 this was a stray '@implementation' token in the stream and
29454 skip it.
29456 return;
29458 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29459 &is_class_extension);
29461 /* We have either a class or a category on our hands. */
29462 if (categ)
29463 objc_start_category_implementation (name, categ);
29464 else
29466 objc_start_class_implementation (name, super);
29467 /* Handle instance variable declarations, if any. */
29468 cp_parser_objc_class_ivars (parser);
29469 objc_continue_implementation ();
29472 cp_parser_objc_method_definition_list (parser);
29475 /* Consume the @end token and finish off the implementation. */
29477 static void
29478 cp_parser_objc_end_implementation (cp_parser* parser)
29480 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29481 objc_finish_implementation ();
29484 /* Parse an Objective-C declaration. */
29486 static void
29487 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29489 /* Try to figure out what kind of declaration is present. */
29490 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29492 if (attributes)
29493 switch (kwd->keyword)
29495 case RID_AT_ALIAS:
29496 case RID_AT_CLASS:
29497 case RID_AT_END:
29498 error_at (kwd->location, "attributes may not be specified before"
29499 " the %<@%D%> Objective-C++ keyword",
29500 kwd->u.value);
29501 attributes = NULL;
29502 break;
29503 case RID_AT_IMPLEMENTATION:
29504 warning_at (kwd->location, OPT_Wattributes,
29505 "prefix attributes are ignored before %<@%D%>",
29506 kwd->u.value);
29507 attributes = NULL;
29508 default:
29509 break;
29512 switch (kwd->keyword)
29514 case RID_AT_ALIAS:
29515 cp_parser_objc_alias_declaration (parser);
29516 break;
29517 case RID_AT_CLASS:
29518 cp_parser_objc_class_declaration (parser);
29519 break;
29520 case RID_AT_PROTOCOL:
29521 cp_parser_objc_protocol_declaration (parser, attributes);
29522 break;
29523 case RID_AT_INTERFACE:
29524 cp_parser_objc_class_interface (parser, attributes);
29525 break;
29526 case RID_AT_IMPLEMENTATION:
29527 cp_parser_objc_class_implementation (parser);
29528 break;
29529 case RID_AT_END:
29530 cp_parser_objc_end_implementation (parser);
29531 break;
29532 default:
29533 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29534 kwd->u.value);
29535 cp_parser_skip_to_end_of_block_or_statement (parser);
29539 /* Parse an Objective-C try-catch-finally statement.
29541 objc-try-catch-finally-stmt:
29542 @try compound-statement objc-catch-clause-seq [opt]
29543 objc-finally-clause [opt]
29545 objc-catch-clause-seq:
29546 objc-catch-clause objc-catch-clause-seq [opt]
29548 objc-catch-clause:
29549 @catch ( objc-exception-declaration ) compound-statement
29551 objc-finally-clause:
29552 @finally compound-statement
29554 objc-exception-declaration:
29555 parameter-declaration
29556 '...'
29558 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29560 Returns NULL_TREE.
29562 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29563 for C. Keep them in sync. */
29565 static tree
29566 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29568 location_t location;
29569 tree stmt;
29571 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29572 location = cp_lexer_peek_token (parser->lexer)->location;
29573 objc_maybe_warn_exceptions (location);
29574 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29575 node, lest it get absorbed into the surrounding block. */
29576 stmt = push_stmt_list ();
29577 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29578 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29580 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29582 cp_parameter_declarator *parm;
29583 tree parameter_declaration = error_mark_node;
29584 bool seen_open_paren = false;
29586 cp_lexer_consume_token (parser->lexer);
29587 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29588 seen_open_paren = true;
29589 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29591 /* We have "@catch (...)" (where the '...' are literally
29592 what is in the code). Skip the '...'.
29593 parameter_declaration is set to NULL_TREE, and
29594 objc_being_catch_clauses() knows that that means
29595 '...'. */
29596 cp_lexer_consume_token (parser->lexer);
29597 parameter_declaration = NULL_TREE;
29599 else
29601 /* We have "@catch (NSException *exception)" or something
29602 like that. Parse the parameter declaration. */
29603 parm = cp_parser_parameter_declaration (parser, false, NULL);
29604 if (parm == NULL)
29605 parameter_declaration = error_mark_node;
29606 else
29607 parameter_declaration = grokdeclarator (parm->declarator,
29608 &parm->decl_specifiers,
29609 PARM, /*initialized=*/0,
29610 /*attrlist=*/NULL);
29612 if (seen_open_paren)
29613 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29614 else
29616 /* If there was no open parenthesis, we are recovering from
29617 an error, and we are trying to figure out what mistake
29618 the user has made. */
29620 /* If there is an immediate closing parenthesis, the user
29621 probably forgot the opening one (ie, they typed "@catch
29622 NSException *e)". Parse the closing parenthesis and keep
29623 going. */
29624 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29625 cp_lexer_consume_token (parser->lexer);
29627 /* If these is no immediate closing parenthesis, the user
29628 probably doesn't know that parenthesis are required at
29629 all (ie, they typed "@catch NSException *e"). So, just
29630 forget about the closing parenthesis and keep going. */
29632 objc_begin_catch_clause (parameter_declaration);
29633 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29634 objc_finish_catch_clause ();
29636 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29638 cp_lexer_consume_token (parser->lexer);
29639 location = cp_lexer_peek_token (parser->lexer)->location;
29640 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29641 node, lest it get absorbed into the surrounding block. */
29642 stmt = push_stmt_list ();
29643 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29644 objc_build_finally_clause (location, pop_stmt_list (stmt));
29647 return objc_finish_try_stmt ();
29650 /* Parse an Objective-C synchronized statement.
29652 objc-synchronized-stmt:
29653 @synchronized ( expression ) compound-statement
29655 Returns NULL_TREE. */
29657 static tree
29658 cp_parser_objc_synchronized_statement (cp_parser *parser)
29660 location_t location;
29661 tree lock, stmt;
29663 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29665 location = cp_lexer_peek_token (parser->lexer)->location;
29666 objc_maybe_warn_exceptions (location);
29667 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29668 lock = cp_parser_expression (parser);
29669 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29671 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29672 node, lest it get absorbed into the surrounding block. */
29673 stmt = push_stmt_list ();
29674 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29676 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29679 /* Parse an Objective-C throw statement.
29681 objc-throw-stmt:
29682 @throw assignment-expression [opt] ;
29684 Returns a constructed '@throw' statement. */
29686 static tree
29687 cp_parser_objc_throw_statement (cp_parser *parser)
29689 tree expr = NULL_TREE;
29690 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29692 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29694 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29695 expr = cp_parser_expression (parser);
29697 cp_parser_consume_semicolon_at_end_of_statement (parser);
29699 return objc_build_throw_stmt (loc, expr);
29702 /* Parse an Objective-C statement. */
29704 static tree
29705 cp_parser_objc_statement (cp_parser * parser)
29707 /* Try to figure out what kind of declaration is present. */
29708 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29710 switch (kwd->keyword)
29712 case RID_AT_TRY:
29713 return cp_parser_objc_try_catch_finally_statement (parser);
29714 case RID_AT_SYNCHRONIZED:
29715 return cp_parser_objc_synchronized_statement (parser);
29716 case RID_AT_THROW:
29717 return cp_parser_objc_throw_statement (parser);
29718 default:
29719 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29720 kwd->u.value);
29721 cp_parser_skip_to_end_of_block_or_statement (parser);
29724 return error_mark_node;
29727 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29728 look ahead to see if an objc keyword follows the attributes. This
29729 is to detect the use of prefix attributes on ObjC @interface and
29730 @protocol. */
29732 static bool
29733 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29735 cp_lexer_save_tokens (parser->lexer);
29736 *attrib = cp_parser_attributes_opt (parser);
29737 gcc_assert (*attrib);
29738 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29740 cp_lexer_commit_tokens (parser->lexer);
29741 return true;
29743 cp_lexer_rollback_tokens (parser->lexer);
29744 return false;
29747 /* This routine is a minimal replacement for
29748 c_parser_struct_declaration () used when parsing the list of
29749 types/names or ObjC++ properties. For example, when parsing the
29750 code
29752 @property (readonly) int a, b, c;
29754 this function is responsible for parsing "int a, int b, int c" and
29755 returning the declarations as CHAIN of DECLs.
29757 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29758 similar parsing. */
29759 static tree
29760 cp_parser_objc_struct_declaration (cp_parser *parser)
29762 tree decls = NULL_TREE;
29763 cp_decl_specifier_seq declspecs;
29764 int decl_class_or_enum_p;
29765 tree prefix_attributes;
29767 cp_parser_decl_specifier_seq (parser,
29768 CP_PARSER_FLAGS_NONE,
29769 &declspecs,
29770 &decl_class_or_enum_p);
29772 if (declspecs.type == error_mark_node)
29773 return error_mark_node;
29775 /* auto, register, static, extern, mutable. */
29776 if (declspecs.storage_class != sc_none)
29778 cp_parser_error (parser, "invalid type for property");
29779 declspecs.storage_class = sc_none;
29782 /* thread_local. */
29783 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29785 cp_parser_error (parser, "invalid type for property");
29786 declspecs.locations[ds_thread] = 0;
29789 /* typedef. */
29790 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29792 cp_parser_error (parser, "invalid type for property");
29793 declspecs.locations[ds_typedef] = 0;
29796 prefix_attributes = declspecs.attributes;
29797 declspecs.attributes = NULL_TREE;
29799 /* Keep going until we hit the `;' at the end of the declaration. */
29800 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29802 tree attributes, first_attribute, decl;
29803 cp_declarator *declarator;
29804 cp_token *token;
29806 /* Parse the declarator. */
29807 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29808 NULL, NULL, false, false);
29810 /* Look for attributes that apply to the ivar. */
29811 attributes = cp_parser_attributes_opt (parser);
29812 /* Remember which attributes are prefix attributes and
29813 which are not. */
29814 first_attribute = attributes;
29815 /* Combine the attributes. */
29816 attributes = chainon (prefix_attributes, attributes);
29818 decl = grokfield (declarator, &declspecs,
29819 NULL_TREE, /*init_const_expr_p=*/false,
29820 NULL_TREE, attributes);
29822 if (decl == error_mark_node || decl == NULL_TREE)
29823 return error_mark_node;
29825 /* Reset PREFIX_ATTRIBUTES. */
29826 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29827 attributes = TREE_CHAIN (attributes);
29828 if (attributes)
29829 TREE_CHAIN (attributes) = NULL_TREE;
29831 DECL_CHAIN (decl) = decls;
29832 decls = decl;
29834 token = cp_lexer_peek_token (parser->lexer);
29835 if (token->type == CPP_COMMA)
29837 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29838 continue;
29840 else
29841 break;
29843 return decls;
29846 /* Parse an Objective-C @property declaration. The syntax is:
29848 objc-property-declaration:
29849 '@property' objc-property-attributes[opt] struct-declaration ;
29851 objc-property-attributes:
29852 '(' objc-property-attribute-list ')'
29854 objc-property-attribute-list:
29855 objc-property-attribute
29856 objc-property-attribute-list, objc-property-attribute
29858 objc-property-attribute
29859 'getter' = identifier
29860 'setter' = identifier
29861 'readonly'
29862 'readwrite'
29863 'assign'
29864 'retain'
29865 'copy'
29866 'nonatomic'
29868 For example:
29869 @property NSString *name;
29870 @property (readonly) id object;
29871 @property (retain, nonatomic, getter=getTheName) id name;
29872 @property int a, b, c;
29874 PS: This function is identical to
29875 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29876 static void
29877 cp_parser_objc_at_property_declaration (cp_parser *parser)
29879 /* The following variables hold the attributes of the properties as
29880 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29881 seen. When we see an attribute, we set them to 'true' (if they
29882 are boolean properties) or to the identifier (if they have an
29883 argument, ie, for getter and setter). Note that here we only
29884 parse the list of attributes, check the syntax and accumulate the
29885 attributes that we find. objc_add_property_declaration() will
29886 then process the information. */
29887 bool property_assign = false;
29888 bool property_copy = false;
29889 tree property_getter_ident = NULL_TREE;
29890 bool property_nonatomic = false;
29891 bool property_readonly = false;
29892 bool property_readwrite = false;
29893 bool property_retain = false;
29894 tree property_setter_ident = NULL_TREE;
29896 /* 'properties' is the list of properties that we read. Usually a
29897 single one, but maybe more (eg, in "@property int a, b, c;" there
29898 are three). */
29899 tree properties;
29900 location_t loc;
29902 loc = cp_lexer_peek_token (parser->lexer)->location;
29904 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29906 /* Parse the optional attribute list... */
29907 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29909 /* Eat the '('. */
29910 cp_lexer_consume_token (parser->lexer);
29912 while (true)
29914 bool syntax_error = false;
29915 cp_token *token = cp_lexer_peek_token (parser->lexer);
29916 enum rid keyword;
29918 if (token->type != CPP_NAME)
29920 cp_parser_error (parser, "expected identifier");
29921 break;
29923 keyword = C_RID_CODE (token->u.value);
29924 cp_lexer_consume_token (parser->lexer);
29925 switch (keyword)
29927 case RID_ASSIGN: property_assign = true; break;
29928 case RID_COPY: property_copy = true; break;
29929 case RID_NONATOMIC: property_nonatomic = true; break;
29930 case RID_READONLY: property_readonly = true; break;
29931 case RID_READWRITE: property_readwrite = true; break;
29932 case RID_RETAIN: property_retain = true; break;
29934 case RID_GETTER:
29935 case RID_SETTER:
29936 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29938 if (keyword == RID_GETTER)
29939 cp_parser_error (parser,
29940 "missing %<=%> (after %<getter%> attribute)");
29941 else
29942 cp_parser_error (parser,
29943 "missing %<=%> (after %<setter%> attribute)");
29944 syntax_error = true;
29945 break;
29947 cp_lexer_consume_token (parser->lexer); /* eat the = */
29948 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29950 cp_parser_error (parser, "expected identifier");
29951 syntax_error = true;
29952 break;
29954 if (keyword == RID_SETTER)
29956 if (property_setter_ident != NULL_TREE)
29958 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29959 cp_lexer_consume_token (parser->lexer);
29961 else
29962 property_setter_ident = cp_parser_objc_selector (parser);
29963 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29964 cp_parser_error (parser, "setter name must terminate with %<:%>");
29965 else
29966 cp_lexer_consume_token (parser->lexer);
29968 else
29970 if (property_getter_ident != NULL_TREE)
29972 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29973 cp_lexer_consume_token (parser->lexer);
29975 else
29976 property_getter_ident = cp_parser_objc_selector (parser);
29978 break;
29979 default:
29980 cp_parser_error (parser, "unknown property attribute");
29981 syntax_error = true;
29982 break;
29985 if (syntax_error)
29986 break;
29988 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29989 cp_lexer_consume_token (parser->lexer);
29990 else
29991 break;
29994 /* FIXME: "@property (setter, assign);" will generate a spurious
29995 "error: expected ‘)’ before ‘,’ token". This is because
29996 cp_parser_require, unlike the C counterpart, will produce an
29997 error even if we are in error recovery. */
29998 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30000 cp_parser_skip_to_closing_parenthesis (parser,
30001 /*recovering=*/true,
30002 /*or_comma=*/false,
30003 /*consume_paren=*/true);
30007 /* ... and the property declaration(s). */
30008 properties = cp_parser_objc_struct_declaration (parser);
30010 if (properties == error_mark_node)
30012 cp_parser_skip_to_end_of_statement (parser);
30013 /* If the next token is now a `;', consume it. */
30014 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30015 cp_lexer_consume_token (parser->lexer);
30016 return;
30019 if (properties == NULL_TREE)
30020 cp_parser_error (parser, "expected identifier");
30021 else
30023 /* Comma-separated properties are chained together in
30024 reverse order; add them one by one. */
30025 properties = nreverse (properties);
30027 for (; properties; properties = TREE_CHAIN (properties))
30028 objc_add_property_declaration (loc, copy_node (properties),
30029 property_readonly, property_readwrite,
30030 property_assign, property_retain,
30031 property_copy, property_nonatomic,
30032 property_getter_ident, property_setter_ident);
30035 cp_parser_consume_semicolon_at_end_of_statement (parser);
30038 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30040 objc-synthesize-declaration:
30041 @synthesize objc-synthesize-identifier-list ;
30043 objc-synthesize-identifier-list:
30044 objc-synthesize-identifier
30045 objc-synthesize-identifier-list, objc-synthesize-identifier
30047 objc-synthesize-identifier
30048 identifier
30049 identifier = identifier
30051 For example:
30052 @synthesize MyProperty;
30053 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30055 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30056 for C. Keep them in sync.
30058 static void
30059 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30061 tree list = NULL_TREE;
30062 location_t loc;
30063 loc = cp_lexer_peek_token (parser->lexer)->location;
30065 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30066 while (true)
30068 tree property, ivar;
30069 property = cp_parser_identifier (parser);
30070 if (property == error_mark_node)
30072 cp_parser_consume_semicolon_at_end_of_statement (parser);
30073 return;
30075 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30077 cp_lexer_consume_token (parser->lexer);
30078 ivar = cp_parser_identifier (parser);
30079 if (ivar == error_mark_node)
30081 cp_parser_consume_semicolon_at_end_of_statement (parser);
30082 return;
30085 else
30086 ivar = NULL_TREE;
30087 list = chainon (list, build_tree_list (ivar, property));
30088 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30089 cp_lexer_consume_token (parser->lexer);
30090 else
30091 break;
30093 cp_parser_consume_semicolon_at_end_of_statement (parser);
30094 objc_add_synthesize_declaration (loc, list);
30097 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30099 objc-dynamic-declaration:
30100 @dynamic identifier-list ;
30102 For example:
30103 @dynamic MyProperty;
30104 @dynamic MyProperty, AnotherProperty;
30106 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30107 for C. Keep them in sync.
30109 static void
30110 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30112 tree list = NULL_TREE;
30113 location_t loc;
30114 loc = cp_lexer_peek_token (parser->lexer)->location;
30116 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30117 while (true)
30119 tree property;
30120 property = cp_parser_identifier (parser);
30121 if (property == error_mark_node)
30123 cp_parser_consume_semicolon_at_end_of_statement (parser);
30124 return;
30126 list = chainon (list, build_tree_list (NULL, property));
30127 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30128 cp_lexer_consume_token (parser->lexer);
30129 else
30130 break;
30132 cp_parser_consume_semicolon_at_end_of_statement (parser);
30133 objc_add_dynamic_declaration (loc, list);
30137 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30139 /* Returns name of the next clause.
30140 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30141 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30142 returned and the token is consumed. */
30144 static pragma_omp_clause
30145 cp_parser_omp_clause_name (cp_parser *parser)
30147 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30149 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30150 result = PRAGMA_OACC_CLAUSE_AUTO;
30151 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30152 result = PRAGMA_OMP_CLAUSE_IF;
30153 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30154 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30155 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30156 result = PRAGMA_OACC_CLAUSE_DELETE;
30157 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30158 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30159 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30160 result = PRAGMA_OMP_CLAUSE_FOR;
30161 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30163 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30164 const char *p = IDENTIFIER_POINTER (id);
30166 switch (p[0])
30168 case 'a':
30169 if (!strcmp ("aligned", p))
30170 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30171 else if (!strcmp ("async", p))
30172 result = PRAGMA_OACC_CLAUSE_ASYNC;
30173 break;
30174 case 'c':
30175 if (!strcmp ("collapse", p))
30176 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30177 else if (!strcmp ("copy", p))
30178 result = PRAGMA_OACC_CLAUSE_COPY;
30179 else if (!strcmp ("copyin", p))
30180 result = PRAGMA_OMP_CLAUSE_COPYIN;
30181 else if (!strcmp ("copyout", p))
30182 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30183 else if (!strcmp ("copyprivate", p))
30184 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30185 else if (!strcmp ("create", p))
30186 result = PRAGMA_OACC_CLAUSE_CREATE;
30187 break;
30188 case 'd':
30189 if (!strcmp ("defaultmap", p))
30190 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30191 else if (!strcmp ("depend", p))
30192 result = PRAGMA_OMP_CLAUSE_DEPEND;
30193 else if (!strcmp ("device", p))
30194 result = PRAGMA_OMP_CLAUSE_DEVICE;
30195 else if (!strcmp ("deviceptr", p))
30196 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30197 else if (!strcmp ("device_resident", p))
30198 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30199 else if (!strcmp ("dist_schedule", p))
30200 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30201 break;
30202 case 'f':
30203 if (!strcmp ("final", p))
30204 result = PRAGMA_OMP_CLAUSE_FINAL;
30205 else if (!strcmp ("firstprivate", p))
30206 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30207 else if (!strcmp ("from", p))
30208 result = PRAGMA_OMP_CLAUSE_FROM;
30209 break;
30210 case 'g':
30211 if (!strcmp ("gang", p))
30212 result = PRAGMA_OACC_CLAUSE_GANG;
30213 else if (!strcmp ("grainsize", p))
30214 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30215 break;
30216 case 'h':
30217 if (!strcmp ("hint", p))
30218 result = PRAGMA_OMP_CLAUSE_HINT;
30219 else if (!strcmp ("host", p))
30220 result = PRAGMA_OACC_CLAUSE_HOST;
30221 break;
30222 case 'i':
30223 if (!strcmp ("inbranch", p))
30224 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30225 else if (!strcmp ("independent", p))
30226 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30227 else if (!strcmp ("is_device_ptr", p))
30228 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30229 break;
30230 case 'l':
30231 if (!strcmp ("lastprivate", p))
30232 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30233 else if (!strcmp ("linear", p))
30234 result = PRAGMA_OMP_CLAUSE_LINEAR;
30235 else if (!strcmp ("link", p))
30236 result = PRAGMA_OMP_CLAUSE_LINK;
30237 break;
30238 case 'm':
30239 if (!strcmp ("map", p))
30240 result = PRAGMA_OMP_CLAUSE_MAP;
30241 else if (!strcmp ("mergeable", p))
30242 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30243 else if (flag_cilkplus && !strcmp ("mask", p))
30244 result = PRAGMA_CILK_CLAUSE_MASK;
30245 break;
30246 case 'n':
30247 if (!strcmp ("nogroup", p))
30248 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30249 else if (!strcmp ("notinbranch", p))
30250 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30251 else if (!strcmp ("nowait", p))
30252 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30253 else if (flag_cilkplus && !strcmp ("nomask", p))
30254 result = PRAGMA_CILK_CLAUSE_NOMASK;
30255 else if (!strcmp ("num_gangs", p))
30256 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30257 else if (!strcmp ("num_tasks", p))
30258 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30259 else if (!strcmp ("num_teams", p))
30260 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30261 else if (!strcmp ("num_threads", p))
30262 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30263 else if (!strcmp ("num_workers", p))
30264 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30265 break;
30266 case 'o':
30267 if (!strcmp ("ordered", p))
30268 result = PRAGMA_OMP_CLAUSE_ORDERED;
30269 break;
30270 case 'p':
30271 if (!strcmp ("parallel", p))
30272 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30273 else if (!strcmp ("present", p))
30274 result = PRAGMA_OACC_CLAUSE_PRESENT;
30275 else if (!strcmp ("present_or_copy", p)
30276 || !strcmp ("pcopy", p))
30277 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30278 else if (!strcmp ("present_or_copyin", p)
30279 || !strcmp ("pcopyin", p))
30280 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30281 else if (!strcmp ("present_or_copyout", p)
30282 || !strcmp ("pcopyout", p))
30283 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30284 else if (!strcmp ("present_or_create", p)
30285 || !strcmp ("pcreate", p))
30286 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30287 else if (!strcmp ("priority", p))
30288 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30289 else if (!strcmp ("proc_bind", p))
30290 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30291 break;
30292 case 'r':
30293 if (!strcmp ("reduction", p))
30294 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30295 break;
30296 case 's':
30297 if (!strcmp ("safelen", p))
30298 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30299 else if (!strcmp ("schedule", p))
30300 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30301 else if (!strcmp ("sections", p))
30302 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30303 else if (!strcmp ("self", p))
30304 result = PRAGMA_OACC_CLAUSE_SELF;
30305 else if (!strcmp ("seq", p))
30306 result = PRAGMA_OACC_CLAUSE_SEQ;
30307 else if (!strcmp ("shared", p))
30308 result = PRAGMA_OMP_CLAUSE_SHARED;
30309 else if (!strcmp ("simd", p))
30310 result = PRAGMA_OMP_CLAUSE_SIMD;
30311 else if (!strcmp ("simdlen", p))
30312 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30313 break;
30314 case 't':
30315 if (!strcmp ("taskgroup", p))
30316 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30317 else if (!strcmp ("thread_limit", p))
30318 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30319 else if (!strcmp ("threads", p))
30320 result = PRAGMA_OMP_CLAUSE_THREADS;
30321 else if (!strcmp ("tile", p))
30322 result = PRAGMA_OACC_CLAUSE_TILE;
30323 else if (!strcmp ("to", p))
30324 result = PRAGMA_OMP_CLAUSE_TO;
30325 break;
30326 case 'u':
30327 if (!strcmp ("uniform", p))
30328 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30329 else if (!strcmp ("untied", p))
30330 result = PRAGMA_OMP_CLAUSE_UNTIED;
30331 else if (!strcmp ("use_device", p))
30332 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30333 else if (!strcmp ("use_device_ptr", p))
30334 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30335 break;
30336 case 'v':
30337 if (!strcmp ("vector", p))
30338 result = PRAGMA_OACC_CLAUSE_VECTOR;
30339 else if (!strcmp ("vector_length", p))
30340 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30341 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30342 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30343 break;
30344 case 'w':
30345 if (!strcmp ("wait", p))
30346 result = PRAGMA_OACC_CLAUSE_WAIT;
30347 else if (!strcmp ("worker", p))
30348 result = PRAGMA_OACC_CLAUSE_WORKER;
30349 break;
30353 if (result != PRAGMA_OMP_CLAUSE_NONE)
30354 cp_lexer_consume_token (parser->lexer);
30356 return result;
30359 /* Validate that a clause of the given type does not already exist. */
30361 static void
30362 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30363 const char *name, location_t location)
30365 tree c;
30367 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30368 if (OMP_CLAUSE_CODE (c) == code)
30370 error_at (location, "too many %qs clauses", name);
30371 break;
30375 /* OpenMP 2.5:
30376 variable-list:
30377 identifier
30378 variable-list , identifier
30380 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30381 colon). An opening parenthesis will have been consumed by the caller.
30383 If KIND is nonzero, create the appropriate node and install the decl
30384 in OMP_CLAUSE_DECL and add the node to the head of the list.
30386 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30387 return the list created.
30389 COLON can be NULL if only closing parenthesis should end the list,
30390 or pointer to bool which will receive false if the list is terminated
30391 by closing parenthesis or true if the list is terminated by colon. */
30393 static tree
30394 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30395 tree list, bool *colon)
30397 cp_token *token;
30398 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30399 if (colon)
30401 parser->colon_corrects_to_scope_p = false;
30402 *colon = false;
30404 while (1)
30406 tree name, decl;
30408 token = cp_lexer_peek_token (parser->lexer);
30409 if (kind != 0
30410 && current_class_ptr
30411 && cp_parser_is_keyword (token, RID_THIS))
30413 decl = finish_this_expr ();
30414 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30415 || CONVERT_EXPR_P (decl))
30416 decl = TREE_OPERAND (decl, 0);
30417 cp_lexer_consume_token (parser->lexer);
30419 else
30421 name = cp_parser_id_expression (parser, /*template_p=*/false,
30422 /*check_dependency_p=*/true,
30423 /*template_p=*/NULL,
30424 /*declarator_p=*/false,
30425 /*optional_p=*/false);
30426 if (name == error_mark_node)
30427 goto skip_comma;
30429 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30430 if (decl == error_mark_node)
30431 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30432 token->location);
30434 if (decl == error_mark_node)
30436 else if (kind != 0)
30438 switch (kind)
30440 case OMP_CLAUSE__CACHE_:
30441 /* The OpenACC cache directive explicitly only allows "array
30442 elements or subarrays". */
30443 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30445 error_at (token->location, "expected %<[%>");
30446 decl = error_mark_node;
30447 break;
30449 /* FALLTHROUGH. */
30450 case OMP_CLAUSE_MAP:
30451 case OMP_CLAUSE_FROM:
30452 case OMP_CLAUSE_TO:
30453 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30455 location_t loc
30456 = cp_lexer_peek_token (parser->lexer)->location;
30457 cp_id_kind idk = CP_ID_KIND_NONE;
30458 cp_lexer_consume_token (parser->lexer);
30459 decl = convert_from_reference (decl);
30460 decl
30461 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30462 decl, false,
30463 &idk, loc);
30465 /* FALLTHROUGH. */
30466 case OMP_CLAUSE_DEPEND:
30467 case OMP_CLAUSE_REDUCTION:
30468 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30470 tree low_bound = NULL_TREE, length = NULL_TREE;
30472 parser->colon_corrects_to_scope_p = false;
30473 cp_lexer_consume_token (parser->lexer);
30474 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30475 low_bound = cp_parser_expression (parser);
30476 if (!colon)
30477 parser->colon_corrects_to_scope_p
30478 = saved_colon_corrects_to_scope_p;
30479 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30480 length = integer_one_node;
30481 else
30483 /* Look for `:'. */
30484 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30485 goto skip_comma;
30486 if (!cp_lexer_next_token_is (parser->lexer,
30487 CPP_CLOSE_SQUARE))
30488 length = cp_parser_expression (parser);
30490 /* Look for the closing `]'. */
30491 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30492 RT_CLOSE_SQUARE))
30493 goto skip_comma;
30495 decl = tree_cons (low_bound, length, decl);
30497 break;
30498 default:
30499 break;
30502 tree u = build_omp_clause (token->location, kind);
30503 OMP_CLAUSE_DECL (u) = decl;
30504 OMP_CLAUSE_CHAIN (u) = list;
30505 list = u;
30507 else
30508 list = tree_cons (decl, NULL_TREE, list);
30510 get_comma:
30511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30512 break;
30513 cp_lexer_consume_token (parser->lexer);
30516 if (colon)
30517 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30519 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30521 *colon = true;
30522 cp_parser_require (parser, CPP_COLON, RT_COLON);
30523 return list;
30526 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30528 int ending;
30530 /* Try to resync to an unnested comma. Copied from
30531 cp_parser_parenthesized_expression_list. */
30532 skip_comma:
30533 if (colon)
30534 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30535 ending = cp_parser_skip_to_closing_parenthesis (parser,
30536 /*recovering=*/true,
30537 /*or_comma=*/true,
30538 /*consume_paren=*/true);
30539 if (ending < 0)
30540 goto get_comma;
30543 return list;
30546 /* Similarly, but expect leading and trailing parenthesis. This is a very
30547 common case for omp clauses. */
30549 static tree
30550 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30552 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30553 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30554 return list;
30557 /* OpenACC 2.0:
30558 copy ( variable-list )
30559 copyin ( variable-list )
30560 copyout ( variable-list )
30561 create ( variable-list )
30562 delete ( variable-list )
30563 present ( variable-list )
30564 present_or_copy ( variable-list )
30565 pcopy ( variable-list )
30566 present_or_copyin ( variable-list )
30567 pcopyin ( variable-list )
30568 present_or_copyout ( variable-list )
30569 pcopyout ( variable-list )
30570 present_or_create ( variable-list )
30571 pcreate ( variable-list ) */
30573 static tree
30574 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30575 tree list)
30577 enum gomp_map_kind kind;
30578 switch (c_kind)
30580 case PRAGMA_OACC_CLAUSE_COPY:
30581 kind = GOMP_MAP_FORCE_TOFROM;
30582 break;
30583 case PRAGMA_OACC_CLAUSE_COPYIN:
30584 kind = GOMP_MAP_FORCE_TO;
30585 break;
30586 case PRAGMA_OACC_CLAUSE_COPYOUT:
30587 kind = GOMP_MAP_FORCE_FROM;
30588 break;
30589 case PRAGMA_OACC_CLAUSE_CREATE:
30590 kind = GOMP_MAP_FORCE_ALLOC;
30591 break;
30592 case PRAGMA_OACC_CLAUSE_DELETE:
30593 kind = GOMP_MAP_DELETE;
30594 break;
30595 case PRAGMA_OACC_CLAUSE_DEVICE:
30596 kind = GOMP_MAP_FORCE_TO;
30597 break;
30598 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30599 kind = GOMP_MAP_DEVICE_RESIDENT;
30600 break;
30601 case PRAGMA_OACC_CLAUSE_HOST:
30602 case PRAGMA_OACC_CLAUSE_SELF:
30603 kind = GOMP_MAP_FORCE_FROM;
30604 break;
30605 case PRAGMA_OACC_CLAUSE_LINK:
30606 kind = GOMP_MAP_LINK;
30607 break;
30608 case PRAGMA_OACC_CLAUSE_PRESENT:
30609 kind = GOMP_MAP_FORCE_PRESENT;
30610 break;
30611 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30612 kind = GOMP_MAP_TOFROM;
30613 break;
30614 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30615 kind = GOMP_MAP_TO;
30616 break;
30617 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30618 kind = GOMP_MAP_FROM;
30619 break;
30620 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30621 kind = GOMP_MAP_ALLOC;
30622 break;
30623 default:
30624 gcc_unreachable ();
30626 tree nl, c;
30627 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30629 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30630 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30632 return nl;
30635 /* OpenACC 2.0:
30636 deviceptr ( variable-list ) */
30638 static tree
30639 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30641 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30642 tree vars, t;
30644 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30645 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30646 variable-list must only allow for pointer variables. */
30647 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30648 for (t = vars; t; t = TREE_CHAIN (t))
30650 tree v = TREE_PURPOSE (t);
30651 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30652 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30653 OMP_CLAUSE_DECL (u) = v;
30654 OMP_CLAUSE_CHAIN (u) = list;
30655 list = u;
30658 return list;
30661 /* OpenACC 2.0:
30662 auto
30663 independent
30664 nohost
30665 seq */
30667 static tree
30668 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30669 enum omp_clause_code code,
30670 tree list, location_t location)
30672 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30673 tree c = build_omp_clause (location, code);
30674 OMP_CLAUSE_CHAIN (c) = list;
30675 return c;
30678 /* OpenACC:
30679 num_gangs ( expression )
30680 num_workers ( expression )
30681 vector_length ( expression ) */
30683 static tree
30684 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30685 const char *str, tree list)
30687 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30689 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30690 return list;
30692 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30694 if (t == error_mark_node
30695 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30697 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30698 /*or_comma=*/false,
30699 /*consume_paren=*/true);
30700 return list;
30703 check_no_duplicate_clause (list, code, str, loc);
30705 tree c = build_omp_clause (loc, code);
30706 OMP_CLAUSE_OPERAND (c, 0) = t;
30707 OMP_CLAUSE_CHAIN (c) = list;
30708 return c;
30711 /* OpenACC:
30713 gang [( gang-arg-list )]
30714 worker [( [num:] int-expr )]
30715 vector [( [length:] int-expr )]
30717 where gang-arg is one of:
30719 [num:] int-expr
30720 static: size-expr
30722 and size-expr may be:
30725 int-expr
30728 static tree
30729 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30730 const char *str, tree list)
30732 const char *id = "num";
30733 cp_lexer *lexer = parser->lexer;
30734 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30735 location_t loc = cp_lexer_peek_token (lexer)->location;
30737 if (kind == OMP_CLAUSE_VECTOR)
30738 id = "length";
30740 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30742 cp_lexer_consume_token (lexer);
30746 cp_token *next = cp_lexer_peek_token (lexer);
30747 int idx = 0;
30749 /* Gang static argument. */
30750 if (kind == OMP_CLAUSE_GANG
30751 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30753 cp_lexer_consume_token (lexer);
30755 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30756 goto cleanup_error;
30758 idx = 1;
30759 if (ops[idx] != NULL)
30761 cp_parser_error (parser, "too many %<static%> arguments");
30762 goto cleanup_error;
30765 /* Check for the '*' argument. */
30766 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30767 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30768 || cp_lexer_nth_token_is (parser->lexer, 2,
30769 CPP_CLOSE_PAREN)))
30771 cp_lexer_consume_token (lexer);
30772 ops[idx] = integer_minus_one_node;
30774 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30776 cp_lexer_consume_token (lexer);
30777 continue;
30779 else break;
30782 /* Worker num: argument and vector length: arguments. */
30783 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30784 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30785 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30787 cp_lexer_consume_token (lexer); /* id */
30788 cp_lexer_consume_token (lexer); /* ':' */
30791 /* Now collect the actual argument. */
30792 if (ops[idx] != NULL_TREE)
30794 cp_parser_error (parser, "unexpected argument");
30795 goto cleanup_error;
30798 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30799 false);
30800 if (expr == error_mark_node)
30801 goto cleanup_error;
30803 mark_exp_read (expr);
30804 ops[idx] = expr;
30806 if (kind == OMP_CLAUSE_GANG
30807 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30809 cp_lexer_consume_token (lexer);
30810 continue;
30812 break;
30814 while (1);
30816 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30817 goto cleanup_error;
30820 check_no_duplicate_clause (list, kind, str, loc);
30822 c = build_omp_clause (loc, kind);
30824 if (ops[1])
30825 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30827 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30828 OMP_CLAUSE_CHAIN (c) = list;
30830 return c;
30832 cleanup_error:
30833 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30834 return list;
30837 /* OpenACC 2.0:
30838 tile ( size-expr-list ) */
30840 static tree
30841 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30843 tree c, expr = error_mark_node;
30844 tree tile = NULL_TREE;
30846 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30848 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30849 return list;
30853 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30854 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30855 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30857 cp_lexer_consume_token (parser->lexer);
30858 expr = integer_minus_one_node;
30860 else
30861 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30863 if (expr == error_mark_node)
30864 return list;
30866 tile = tree_cons (NULL_TREE, expr, tile);
30868 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30869 cp_lexer_consume_token (parser->lexer);
30871 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30873 /* Consume the trailing ')'. */
30874 cp_lexer_consume_token (parser->lexer);
30876 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30877 tile = nreverse (tile);
30878 OMP_CLAUSE_TILE_LIST (c) = tile;
30879 OMP_CLAUSE_CHAIN (c) = list;
30880 return c;
30883 /* OpenACC 2.0
30884 Parse wait clause or directive parameters. */
30886 static tree
30887 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30889 vec<tree, va_gc> *args;
30890 tree t, args_tree;
30892 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30893 /*cast_p=*/false,
30894 /*allow_expansion_p=*/true,
30895 /*non_constant_p=*/NULL);
30897 if (args == NULL || args->length () == 0)
30899 cp_parser_error (parser, "expected integer expression before ')'");
30900 if (args != NULL)
30901 release_tree_vector (args);
30902 return list;
30905 args_tree = build_tree_list_vec (args);
30907 release_tree_vector (args);
30909 for (t = args_tree; t; t = TREE_CHAIN (t))
30911 tree targ = TREE_VALUE (t);
30913 if (targ != error_mark_node)
30915 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30916 error ("%<wait%> expression must be integral");
30917 else
30919 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30921 mark_rvalue_use (targ);
30922 OMP_CLAUSE_DECL (c) = targ;
30923 OMP_CLAUSE_CHAIN (c) = list;
30924 list = c;
30929 return list;
30932 /* OpenACC:
30933 wait ( int-expr-list ) */
30935 static tree
30936 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30938 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30940 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30941 return list;
30943 list = cp_parser_oacc_wait_list (parser, location, list);
30945 return list;
30948 /* OpenMP 3.0:
30949 collapse ( constant-expression ) */
30951 static tree
30952 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30954 tree c, num;
30955 location_t loc;
30956 HOST_WIDE_INT n;
30958 loc = cp_lexer_peek_token (parser->lexer)->location;
30959 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30960 return list;
30962 num = cp_parser_constant_expression (parser);
30964 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30965 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30966 /*or_comma=*/false,
30967 /*consume_paren=*/true);
30969 if (num == error_mark_node)
30970 return list;
30971 num = fold_non_dependent_expr (num);
30972 if (!tree_fits_shwi_p (num)
30973 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30974 || (n = tree_to_shwi (num)) <= 0
30975 || (int) n != n)
30977 error_at (loc, "collapse argument needs positive constant integer expression");
30978 return list;
30981 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30982 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30983 OMP_CLAUSE_CHAIN (c) = list;
30984 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30986 return c;
30989 /* OpenMP 2.5:
30990 default ( shared | none )
30992 OpenACC 2.0
30993 default (none) */
30995 static tree
30996 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30997 location_t location, bool is_oacc)
30999 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31000 tree c;
31002 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31003 return list;
31004 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31006 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31007 const char *p = IDENTIFIER_POINTER (id);
31009 switch (p[0])
31011 case 'n':
31012 if (strcmp ("none", p) != 0)
31013 goto invalid_kind;
31014 kind = OMP_CLAUSE_DEFAULT_NONE;
31015 break;
31017 case 's':
31018 if (strcmp ("shared", p) != 0 || is_oacc)
31019 goto invalid_kind;
31020 kind = OMP_CLAUSE_DEFAULT_SHARED;
31021 break;
31023 default:
31024 goto invalid_kind;
31027 cp_lexer_consume_token (parser->lexer);
31029 else
31031 invalid_kind:
31032 if (is_oacc)
31033 cp_parser_error (parser, "expected %<none%>");
31034 else
31035 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31038 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31039 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31040 /*or_comma=*/false,
31041 /*consume_paren=*/true);
31043 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31044 return list;
31046 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31047 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31048 OMP_CLAUSE_CHAIN (c) = list;
31049 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31051 return c;
31054 /* OpenMP 3.1:
31055 final ( expression ) */
31057 static tree
31058 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31060 tree t, c;
31062 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31063 return list;
31065 t = cp_parser_condition (parser);
31067 if (t == error_mark_node
31068 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31069 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31070 /*or_comma=*/false,
31071 /*consume_paren=*/true);
31073 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31075 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31076 OMP_CLAUSE_FINAL_EXPR (c) = t;
31077 OMP_CLAUSE_CHAIN (c) = list;
31079 return c;
31082 /* OpenMP 2.5:
31083 if ( expression )
31085 OpenMP 4.5:
31086 if ( directive-name-modifier : expression )
31088 directive-name-modifier:
31089 parallel | task | taskloop | target data | target | target update
31090 | target enter data | target exit data */
31092 static tree
31093 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31094 bool is_omp)
31096 tree t, c;
31097 enum tree_code if_modifier = ERROR_MARK;
31099 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31100 return list;
31102 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31104 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31105 const char *p = IDENTIFIER_POINTER (id);
31106 int n = 2;
31108 if (strcmp ("parallel", p) == 0)
31109 if_modifier = OMP_PARALLEL;
31110 else if (strcmp ("task", p) == 0)
31111 if_modifier = OMP_TASK;
31112 else if (strcmp ("taskloop", p) == 0)
31113 if_modifier = OMP_TASKLOOP;
31114 else if (strcmp ("target", p) == 0)
31116 if_modifier = OMP_TARGET;
31117 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31119 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31120 p = IDENTIFIER_POINTER (id);
31121 if (strcmp ("data", p) == 0)
31122 if_modifier = OMP_TARGET_DATA;
31123 else if (strcmp ("update", p) == 0)
31124 if_modifier = OMP_TARGET_UPDATE;
31125 else if (strcmp ("enter", p) == 0)
31126 if_modifier = OMP_TARGET_ENTER_DATA;
31127 else if (strcmp ("exit", p) == 0)
31128 if_modifier = OMP_TARGET_EXIT_DATA;
31129 if (if_modifier != OMP_TARGET)
31130 n = 3;
31131 else
31133 location_t loc
31134 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31135 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31136 "or %<exit%>");
31137 if_modifier = ERROR_MARK;
31139 if (if_modifier == OMP_TARGET_ENTER_DATA
31140 || if_modifier == OMP_TARGET_EXIT_DATA)
31142 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31144 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31145 p = IDENTIFIER_POINTER (id);
31146 if (strcmp ("data", p) == 0)
31147 n = 4;
31149 if (n != 4)
31151 location_t loc
31152 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31153 error_at (loc, "expected %<data%>");
31154 if_modifier = ERROR_MARK;
31159 if (if_modifier != ERROR_MARK)
31161 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31163 while (n-- > 0)
31164 cp_lexer_consume_token (parser->lexer);
31166 else
31168 if (n > 2)
31170 location_t loc
31171 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31172 error_at (loc, "expected %<:%>");
31174 if_modifier = ERROR_MARK;
31179 t = cp_parser_condition (parser);
31181 if (t == error_mark_node
31182 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31183 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31184 /*or_comma=*/false,
31185 /*consume_paren=*/true);
31187 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31188 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31190 if (if_modifier != ERROR_MARK
31191 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31193 const char *p = NULL;
31194 switch (if_modifier)
31196 case OMP_PARALLEL: p = "parallel"; break;
31197 case OMP_TASK: p = "task"; break;
31198 case OMP_TASKLOOP: p = "taskloop"; break;
31199 case OMP_TARGET_DATA: p = "target data"; break;
31200 case OMP_TARGET: p = "target"; break;
31201 case OMP_TARGET_UPDATE: p = "target update"; break;
31202 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31203 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31204 default: gcc_unreachable ();
31206 error_at (location, "too many %<if%> clauses with %qs modifier",
31208 return list;
31210 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31212 if (!is_omp)
31213 error_at (location, "too many %<if%> clauses");
31214 else
31215 error_at (location, "too many %<if%> clauses without modifier");
31216 return list;
31218 else if (if_modifier == ERROR_MARK
31219 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31221 error_at (location, "if any %<if%> clause has modifier, then all "
31222 "%<if%> clauses have to use modifier");
31223 return list;
31227 c = build_omp_clause (location, OMP_CLAUSE_IF);
31228 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31229 OMP_CLAUSE_IF_EXPR (c) = t;
31230 OMP_CLAUSE_CHAIN (c) = list;
31232 return c;
31235 /* OpenMP 3.1:
31236 mergeable */
31238 static tree
31239 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31240 tree list, location_t location)
31242 tree c;
31244 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31245 location);
31247 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31248 OMP_CLAUSE_CHAIN (c) = list;
31249 return c;
31252 /* OpenMP 2.5:
31253 nowait */
31255 static tree
31256 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31257 tree list, location_t location)
31259 tree c;
31261 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31263 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31264 OMP_CLAUSE_CHAIN (c) = list;
31265 return c;
31268 /* OpenMP 2.5:
31269 num_threads ( expression ) */
31271 static tree
31272 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31273 location_t location)
31275 tree t, c;
31277 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31278 return list;
31280 t = cp_parser_expression (parser);
31282 if (t == error_mark_node
31283 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31284 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31285 /*or_comma=*/false,
31286 /*consume_paren=*/true);
31288 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31289 "num_threads", location);
31291 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31292 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31293 OMP_CLAUSE_CHAIN (c) = list;
31295 return c;
31298 /* OpenMP 4.5:
31299 num_tasks ( expression ) */
31301 static tree
31302 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31303 location_t location)
31305 tree t, c;
31307 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31308 return list;
31310 t = cp_parser_expression (parser);
31312 if (t == error_mark_node
31313 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31314 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31315 /*or_comma=*/false,
31316 /*consume_paren=*/true);
31318 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31319 "num_tasks", location);
31321 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31322 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31323 OMP_CLAUSE_CHAIN (c) = list;
31325 return c;
31328 /* OpenMP 4.5:
31329 grainsize ( expression ) */
31331 static tree
31332 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31333 location_t location)
31335 tree t, c;
31337 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31338 return list;
31340 t = cp_parser_expression (parser);
31342 if (t == error_mark_node
31343 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31344 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31345 /*or_comma=*/false,
31346 /*consume_paren=*/true);
31348 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31349 "grainsize", location);
31351 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31352 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31353 OMP_CLAUSE_CHAIN (c) = list;
31355 return c;
31358 /* OpenMP 4.5:
31359 priority ( expression ) */
31361 static tree
31362 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31363 location_t location)
31365 tree t, c;
31367 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31368 return list;
31370 t = cp_parser_expression (parser);
31372 if (t == error_mark_node
31373 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31374 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31375 /*or_comma=*/false,
31376 /*consume_paren=*/true);
31378 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31379 "priority", location);
31381 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31382 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31383 OMP_CLAUSE_CHAIN (c) = list;
31385 return c;
31388 /* OpenMP 4.5:
31389 hint ( expression ) */
31391 static tree
31392 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31393 location_t location)
31395 tree t, c;
31397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31398 return list;
31400 t = cp_parser_expression (parser);
31402 if (t == error_mark_node
31403 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31404 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31405 /*or_comma=*/false,
31406 /*consume_paren=*/true);
31408 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31410 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31411 OMP_CLAUSE_HINT_EXPR (c) = t;
31412 OMP_CLAUSE_CHAIN (c) = list;
31414 return c;
31417 /* OpenMP 4.5:
31418 defaultmap ( tofrom : scalar ) */
31420 static tree
31421 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31422 location_t location)
31424 tree c, id;
31425 const char *p;
31427 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31428 return list;
31430 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31432 cp_parser_error (parser, "expected %<tofrom%>");
31433 goto out_err;
31435 id = cp_lexer_peek_token (parser->lexer)->u.value;
31436 p = IDENTIFIER_POINTER (id);
31437 if (strcmp (p, "tofrom") != 0)
31439 cp_parser_error (parser, "expected %<tofrom%>");
31440 goto out_err;
31442 cp_lexer_consume_token (parser->lexer);
31443 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31444 goto out_err;
31446 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31448 cp_parser_error (parser, "expected %<scalar%>");
31449 goto out_err;
31451 id = cp_lexer_peek_token (parser->lexer)->u.value;
31452 p = IDENTIFIER_POINTER (id);
31453 if (strcmp (p, "scalar") != 0)
31455 cp_parser_error (parser, "expected %<scalar%>");
31456 goto out_err;
31458 cp_lexer_consume_token (parser->lexer);
31459 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31460 goto out_err;
31462 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31463 location);
31465 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31466 OMP_CLAUSE_CHAIN (c) = list;
31467 return c;
31469 out_err:
31470 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31471 /*or_comma=*/false,
31472 /*consume_paren=*/true);
31473 return list;
31476 /* OpenMP 2.5:
31477 ordered
31479 OpenMP 4.5:
31480 ordered ( constant-expression ) */
31482 static tree
31483 cp_parser_omp_clause_ordered (cp_parser *parser,
31484 tree list, location_t location)
31486 tree c, num = NULL_TREE;
31487 HOST_WIDE_INT n;
31489 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31490 "ordered", location);
31492 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31494 cp_lexer_consume_token (parser->lexer);
31496 num = cp_parser_constant_expression (parser);
31498 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31499 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31500 /*or_comma=*/false,
31501 /*consume_paren=*/true);
31503 if (num == error_mark_node)
31504 return list;
31505 num = fold_non_dependent_expr (num);
31506 if (!tree_fits_shwi_p (num)
31507 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31508 || (n = tree_to_shwi (num)) <= 0
31509 || (int) n != n)
31511 error_at (location,
31512 "ordered argument needs positive constant integer "
31513 "expression");
31514 return list;
31518 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31519 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31520 OMP_CLAUSE_CHAIN (c) = list;
31521 return c;
31524 /* OpenMP 2.5:
31525 reduction ( reduction-operator : variable-list )
31527 reduction-operator:
31528 One of: + * - & ^ | && ||
31530 OpenMP 3.1:
31532 reduction-operator:
31533 One of: + * - & ^ | && || min max
31535 OpenMP 4.0:
31537 reduction-operator:
31538 One of: + * - & ^ | && ||
31539 id-expression */
31541 static tree
31542 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31544 enum tree_code code = ERROR_MARK;
31545 tree nlist, c, id = NULL_TREE;
31547 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31548 return list;
31550 switch (cp_lexer_peek_token (parser->lexer)->type)
31552 case CPP_PLUS: code = PLUS_EXPR; break;
31553 case CPP_MULT: code = MULT_EXPR; break;
31554 case CPP_MINUS: code = MINUS_EXPR; break;
31555 case CPP_AND: code = BIT_AND_EXPR; break;
31556 case CPP_XOR: code = BIT_XOR_EXPR; break;
31557 case CPP_OR: code = BIT_IOR_EXPR; break;
31558 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31559 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31560 default: break;
31563 if (code != ERROR_MARK)
31564 cp_lexer_consume_token (parser->lexer);
31565 else
31567 bool saved_colon_corrects_to_scope_p;
31568 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31569 parser->colon_corrects_to_scope_p = false;
31570 id = cp_parser_id_expression (parser, /*template_p=*/false,
31571 /*check_dependency_p=*/true,
31572 /*template_p=*/NULL,
31573 /*declarator_p=*/false,
31574 /*optional_p=*/false);
31575 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31576 if (identifier_p (id))
31578 const char *p = IDENTIFIER_POINTER (id);
31580 if (strcmp (p, "min") == 0)
31581 code = MIN_EXPR;
31582 else if (strcmp (p, "max") == 0)
31583 code = MAX_EXPR;
31584 else if (id == ansi_opname (PLUS_EXPR))
31585 code = PLUS_EXPR;
31586 else if (id == ansi_opname (MULT_EXPR))
31587 code = MULT_EXPR;
31588 else if (id == ansi_opname (MINUS_EXPR))
31589 code = MINUS_EXPR;
31590 else if (id == ansi_opname (BIT_AND_EXPR))
31591 code = BIT_AND_EXPR;
31592 else if (id == ansi_opname (BIT_IOR_EXPR))
31593 code = BIT_IOR_EXPR;
31594 else if (id == ansi_opname (BIT_XOR_EXPR))
31595 code = BIT_XOR_EXPR;
31596 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31597 code = TRUTH_ANDIF_EXPR;
31598 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31599 code = TRUTH_ORIF_EXPR;
31600 id = omp_reduction_id (code, id, NULL_TREE);
31601 tree scope = parser->scope;
31602 if (scope)
31603 id = build_qualified_name (NULL_TREE, scope, id, false);
31604 parser->scope = NULL_TREE;
31605 parser->qualifying_scope = NULL_TREE;
31606 parser->object_scope = NULL_TREE;
31608 else
31610 error ("invalid reduction-identifier");
31611 resync_fail:
31612 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31613 /*or_comma=*/false,
31614 /*consume_paren=*/true);
31615 return list;
31619 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31620 goto resync_fail;
31622 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31623 NULL);
31624 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31626 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31627 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31630 return nlist;
31633 /* OpenMP 2.5:
31634 schedule ( schedule-kind )
31635 schedule ( schedule-kind , expression )
31637 schedule-kind:
31638 static | dynamic | guided | runtime | auto
31640 OpenMP 4.5:
31641 schedule ( schedule-modifier : schedule-kind )
31642 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31644 schedule-modifier:
31645 simd
31646 monotonic
31647 nonmonotonic */
31649 static tree
31650 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31652 tree c, t;
31653 int modifiers = 0, nmodifiers = 0;
31655 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31656 return list;
31658 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31660 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31662 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31663 const char *p = IDENTIFIER_POINTER (id);
31664 if (strcmp ("simd", p) == 0)
31665 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31666 else if (strcmp ("monotonic", p) == 0)
31667 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31668 else if (strcmp ("nonmonotonic", p) == 0)
31669 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31670 else
31671 break;
31672 cp_lexer_consume_token (parser->lexer);
31673 if (nmodifiers++ == 0
31674 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31675 cp_lexer_consume_token (parser->lexer);
31676 else
31678 cp_parser_require (parser, CPP_COLON, RT_COLON);
31679 break;
31683 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31685 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31686 const char *p = IDENTIFIER_POINTER (id);
31688 switch (p[0])
31690 case 'd':
31691 if (strcmp ("dynamic", p) != 0)
31692 goto invalid_kind;
31693 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31694 break;
31696 case 'g':
31697 if (strcmp ("guided", p) != 0)
31698 goto invalid_kind;
31699 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31700 break;
31702 case 'r':
31703 if (strcmp ("runtime", p) != 0)
31704 goto invalid_kind;
31705 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31706 break;
31708 default:
31709 goto invalid_kind;
31712 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31713 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31714 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31715 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31716 else
31717 goto invalid_kind;
31718 cp_lexer_consume_token (parser->lexer);
31720 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31721 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31722 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31723 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31725 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31726 "specified");
31727 modifiers = 0;
31730 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31732 cp_token *token;
31733 cp_lexer_consume_token (parser->lexer);
31735 token = cp_lexer_peek_token (parser->lexer);
31736 t = cp_parser_assignment_expression (parser);
31738 if (t == error_mark_node)
31739 goto resync_fail;
31740 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31741 error_at (token->location, "schedule %<runtime%> does not take "
31742 "a %<chunk_size%> parameter");
31743 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31744 error_at (token->location, "schedule %<auto%> does not take "
31745 "a %<chunk_size%> parameter");
31746 else
31747 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31749 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31750 goto resync_fail;
31752 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31753 goto resync_fail;
31755 OMP_CLAUSE_SCHEDULE_KIND (c)
31756 = (enum omp_clause_schedule_kind)
31757 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31759 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31760 OMP_CLAUSE_CHAIN (c) = list;
31761 return c;
31763 invalid_kind:
31764 cp_parser_error (parser, "invalid schedule kind");
31765 resync_fail:
31766 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31767 /*or_comma=*/false,
31768 /*consume_paren=*/true);
31769 return list;
31772 /* OpenMP 3.0:
31773 untied */
31775 static tree
31776 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31777 tree list, location_t location)
31779 tree c;
31781 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31783 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31784 OMP_CLAUSE_CHAIN (c) = list;
31785 return c;
31788 /* OpenMP 4.0:
31789 inbranch
31790 notinbranch */
31792 static tree
31793 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31794 tree list, location_t location)
31796 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31797 tree c = build_omp_clause (location, code);
31798 OMP_CLAUSE_CHAIN (c) = list;
31799 return c;
31802 /* OpenMP 4.0:
31803 parallel
31805 sections
31806 taskgroup */
31808 static tree
31809 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31810 enum omp_clause_code code,
31811 tree list, location_t location)
31813 tree c = build_omp_clause (location, code);
31814 OMP_CLAUSE_CHAIN (c) = list;
31815 return c;
31818 /* OpenMP 4.5:
31819 nogroup */
31821 static tree
31822 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31823 tree list, location_t location)
31825 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31826 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31827 OMP_CLAUSE_CHAIN (c) = list;
31828 return c;
31831 /* OpenMP 4.5:
31832 simd
31833 threads */
31835 static tree
31836 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31837 enum omp_clause_code code,
31838 tree list, location_t location)
31840 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31841 tree c = build_omp_clause (location, code);
31842 OMP_CLAUSE_CHAIN (c) = list;
31843 return c;
31846 /* OpenMP 4.0:
31847 num_teams ( expression ) */
31849 static tree
31850 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31851 location_t location)
31853 tree t, c;
31855 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31856 return list;
31858 t = cp_parser_expression (parser);
31860 if (t == error_mark_node
31861 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31862 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31863 /*or_comma=*/false,
31864 /*consume_paren=*/true);
31866 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31867 "num_teams", location);
31869 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31870 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31871 OMP_CLAUSE_CHAIN (c) = list;
31873 return c;
31876 /* OpenMP 4.0:
31877 thread_limit ( expression ) */
31879 static tree
31880 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31881 location_t location)
31883 tree t, c;
31885 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31886 return list;
31888 t = cp_parser_expression (parser);
31890 if (t == error_mark_node
31891 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31892 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31893 /*or_comma=*/false,
31894 /*consume_paren=*/true);
31896 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31897 "thread_limit", location);
31899 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31900 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31901 OMP_CLAUSE_CHAIN (c) = list;
31903 return c;
31906 /* OpenMP 4.0:
31907 aligned ( variable-list )
31908 aligned ( variable-list : constant-expression ) */
31910 static tree
31911 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31913 tree nlist, c, alignment = NULL_TREE;
31914 bool colon;
31916 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31917 return list;
31919 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31920 &colon);
31922 if (colon)
31924 alignment = cp_parser_constant_expression (parser);
31926 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31927 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31928 /*or_comma=*/false,
31929 /*consume_paren=*/true);
31931 if (alignment == error_mark_node)
31932 alignment = NULL_TREE;
31935 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31936 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31938 return nlist;
31941 /* OpenMP 4.0:
31942 linear ( variable-list )
31943 linear ( variable-list : expression )
31945 OpenMP 4.5:
31946 linear ( modifier ( variable-list ) )
31947 linear ( modifier ( variable-list ) : expression ) */
31949 static tree
31950 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31951 bool is_cilk_simd_fn, bool declare_simd)
31953 tree nlist, c, step = integer_one_node;
31954 bool colon;
31955 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31957 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31958 return list;
31960 if (!is_cilk_simd_fn
31961 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31963 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31964 const char *p = IDENTIFIER_POINTER (id);
31966 if (strcmp ("ref", p) == 0)
31967 kind = OMP_CLAUSE_LINEAR_REF;
31968 else if (strcmp ("val", p) == 0)
31969 kind = OMP_CLAUSE_LINEAR_VAL;
31970 else if (strcmp ("uval", p) == 0)
31971 kind = OMP_CLAUSE_LINEAR_UVAL;
31972 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31973 cp_lexer_consume_token (parser->lexer);
31974 else
31975 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31978 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31979 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31980 &colon);
31981 else
31983 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31984 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31985 if (colon)
31986 cp_parser_require (parser, CPP_COLON, RT_COLON);
31987 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31988 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31989 /*or_comma=*/false,
31990 /*consume_paren=*/true);
31993 if (colon)
31995 step = NULL_TREE;
31996 if (declare_simd
31997 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31998 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32000 cp_token *token = cp_lexer_peek_token (parser->lexer);
32001 cp_parser_parse_tentatively (parser);
32002 step = cp_parser_id_expression (parser, /*template_p=*/false,
32003 /*check_dependency_p=*/true,
32004 /*template_p=*/NULL,
32005 /*declarator_p=*/false,
32006 /*optional_p=*/false);
32007 if (step != error_mark_node)
32008 step = cp_parser_lookup_name_simple (parser, step, token->location);
32009 if (step == error_mark_node)
32011 step = NULL_TREE;
32012 cp_parser_abort_tentative_parse (parser);
32014 else if (!cp_parser_parse_definitely (parser))
32015 step = NULL_TREE;
32017 if (!step)
32018 step = cp_parser_expression (parser);
32020 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32022 sorry ("using parameters for %<linear%> step is not supported yet");
32023 step = integer_one_node;
32025 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32026 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32027 /*or_comma=*/false,
32028 /*consume_paren=*/true);
32030 if (step == error_mark_node)
32031 return list;
32034 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32036 OMP_CLAUSE_LINEAR_STEP (c) = step;
32037 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32040 return nlist;
32043 /* OpenMP 4.0:
32044 safelen ( constant-expression ) */
32046 static tree
32047 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32048 location_t location)
32050 tree t, c;
32052 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32053 return list;
32055 t = cp_parser_constant_expression (parser);
32057 if (t == error_mark_node
32058 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32059 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32060 /*or_comma=*/false,
32061 /*consume_paren=*/true);
32063 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32065 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32066 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32067 OMP_CLAUSE_CHAIN (c) = list;
32069 return c;
32072 /* OpenMP 4.0:
32073 simdlen ( constant-expression ) */
32075 static tree
32076 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32077 location_t location)
32079 tree t, c;
32081 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32082 return list;
32084 t = cp_parser_constant_expression (parser);
32086 if (t == error_mark_node
32087 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32088 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32089 /*or_comma=*/false,
32090 /*consume_paren=*/true);
32092 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32094 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32095 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32096 OMP_CLAUSE_CHAIN (c) = list;
32098 return c;
32101 /* OpenMP 4.5:
32102 vec:
32103 identifier [+/- integer]
32104 vec , identifier [+/- integer]
32107 static tree
32108 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32109 tree list)
32111 tree vec = NULL;
32113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32115 cp_parser_error (parser, "expected identifier");
32116 return list;
32119 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32121 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32122 tree t, identifier = cp_parser_identifier (parser);
32123 tree addend = NULL;
32125 if (identifier == error_mark_node)
32126 t = error_mark_node;
32127 else
32129 t = cp_parser_lookup_name_simple
32130 (parser, identifier,
32131 cp_lexer_peek_token (parser->lexer)->location);
32132 if (t == error_mark_node)
32133 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32134 id_loc);
32137 bool neg = false;
32138 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32139 neg = true;
32140 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32142 addend = integer_zero_node;
32143 goto add_to_vector;
32145 cp_lexer_consume_token (parser->lexer);
32147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32149 cp_parser_error (parser, "expected integer");
32150 return list;
32153 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32154 if (TREE_CODE (addend) != INTEGER_CST)
32156 cp_parser_error (parser, "expected integer");
32157 return list;
32159 cp_lexer_consume_token (parser->lexer);
32161 add_to_vector:
32162 if (t != error_mark_node)
32164 vec = tree_cons (addend, t, vec);
32165 if (neg)
32166 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32170 break;
32172 cp_lexer_consume_token (parser->lexer);
32175 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32177 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32178 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32179 OMP_CLAUSE_DECL (u) = nreverse (vec);
32180 OMP_CLAUSE_CHAIN (u) = list;
32181 return u;
32183 return list;
32186 /* OpenMP 4.0:
32187 depend ( depend-kind : variable-list )
32189 depend-kind:
32190 in | out | inout
32192 OpenMP 4.5:
32193 depend ( source )
32195 depend ( sink : vec ) */
32197 static tree
32198 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32200 tree nlist, c;
32201 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32203 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32204 return list;
32206 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32208 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32209 const char *p = IDENTIFIER_POINTER (id);
32211 if (strcmp ("in", p) == 0)
32212 kind = OMP_CLAUSE_DEPEND_IN;
32213 else if (strcmp ("inout", p) == 0)
32214 kind = OMP_CLAUSE_DEPEND_INOUT;
32215 else if (strcmp ("out", p) == 0)
32216 kind = OMP_CLAUSE_DEPEND_OUT;
32217 else if (strcmp ("source", p) == 0)
32218 kind = OMP_CLAUSE_DEPEND_SOURCE;
32219 else if (strcmp ("sink", p) == 0)
32220 kind = OMP_CLAUSE_DEPEND_SINK;
32221 else
32222 goto invalid_kind;
32224 else
32225 goto invalid_kind;
32227 cp_lexer_consume_token (parser->lexer);
32229 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32231 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32232 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32233 OMP_CLAUSE_DECL (c) = NULL_TREE;
32234 OMP_CLAUSE_CHAIN (c) = list;
32235 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32236 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32237 /*or_comma=*/false,
32238 /*consume_paren=*/true);
32239 return c;
32242 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32243 goto resync_fail;
32245 if (kind == OMP_CLAUSE_DEPEND_SINK)
32246 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32247 else
32249 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32250 list, NULL);
32252 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32253 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32255 return nlist;
32257 invalid_kind:
32258 cp_parser_error (parser, "invalid depend kind");
32259 resync_fail:
32260 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32261 /*or_comma=*/false,
32262 /*consume_paren=*/true);
32263 return list;
32266 /* OpenMP 4.0:
32267 map ( map-kind : variable-list )
32268 map ( variable-list )
32270 map-kind:
32271 alloc | to | from | tofrom
32273 OpenMP 4.5:
32274 map-kind:
32275 alloc | to | from | tofrom | release | delete
32277 map ( always [,] map-kind: variable-list ) */
32279 static tree
32280 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32282 tree nlist, c;
32283 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32284 bool always = false;
32286 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32287 return list;
32289 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32291 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32292 const char *p = IDENTIFIER_POINTER (id);
32294 if (strcmp ("always", p) == 0)
32296 int nth = 2;
32297 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32298 nth++;
32299 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32300 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32301 == RID_DELETE))
32302 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32303 == CPP_COLON))
32305 always = true;
32306 cp_lexer_consume_token (parser->lexer);
32307 if (nth == 3)
32308 cp_lexer_consume_token (parser->lexer);
32313 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32314 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32316 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32317 const char *p = IDENTIFIER_POINTER (id);
32319 if (strcmp ("alloc", p) == 0)
32320 kind = GOMP_MAP_ALLOC;
32321 else if (strcmp ("to", p) == 0)
32322 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32323 else if (strcmp ("from", p) == 0)
32324 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32325 else if (strcmp ("tofrom", p) == 0)
32326 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32327 else if (strcmp ("release", p) == 0)
32328 kind = GOMP_MAP_RELEASE;
32329 else
32331 cp_parser_error (parser, "invalid map kind");
32332 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32333 /*or_comma=*/false,
32334 /*consume_paren=*/true);
32335 return list;
32337 cp_lexer_consume_token (parser->lexer);
32338 cp_lexer_consume_token (parser->lexer);
32340 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32341 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32343 kind = GOMP_MAP_DELETE;
32344 cp_lexer_consume_token (parser->lexer);
32345 cp_lexer_consume_token (parser->lexer);
32348 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32349 NULL);
32351 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32352 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32354 return nlist;
32357 /* OpenMP 4.0:
32358 device ( expression ) */
32360 static tree
32361 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32362 location_t location)
32364 tree t, c;
32366 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32367 return list;
32369 t = cp_parser_expression (parser);
32371 if (t == error_mark_node
32372 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32373 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32374 /*or_comma=*/false,
32375 /*consume_paren=*/true);
32377 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32378 "device", location);
32380 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32381 OMP_CLAUSE_DEVICE_ID (c) = t;
32382 OMP_CLAUSE_CHAIN (c) = list;
32384 return c;
32387 /* OpenMP 4.0:
32388 dist_schedule ( static )
32389 dist_schedule ( static , expression ) */
32391 static tree
32392 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32393 location_t location)
32395 tree c, t;
32397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32398 return list;
32400 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32402 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32403 goto invalid_kind;
32404 cp_lexer_consume_token (parser->lexer);
32406 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32408 cp_lexer_consume_token (parser->lexer);
32410 t = cp_parser_assignment_expression (parser);
32412 if (t == error_mark_node)
32413 goto resync_fail;
32414 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32416 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32417 goto resync_fail;
32419 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32420 goto resync_fail;
32422 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32423 location);
32424 OMP_CLAUSE_CHAIN (c) = list;
32425 return c;
32427 invalid_kind:
32428 cp_parser_error (parser, "invalid dist_schedule kind");
32429 resync_fail:
32430 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32431 /*or_comma=*/false,
32432 /*consume_paren=*/true);
32433 return list;
32436 /* OpenMP 4.0:
32437 proc_bind ( proc-bind-kind )
32439 proc-bind-kind:
32440 master | close | spread */
32442 static tree
32443 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32444 location_t location)
32446 tree c;
32447 enum omp_clause_proc_bind_kind kind;
32449 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32450 return list;
32452 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32454 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32455 const char *p = IDENTIFIER_POINTER (id);
32457 if (strcmp ("master", p) == 0)
32458 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32459 else if (strcmp ("close", p) == 0)
32460 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32461 else if (strcmp ("spread", p) == 0)
32462 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32463 else
32464 goto invalid_kind;
32466 else
32467 goto invalid_kind;
32469 cp_lexer_consume_token (parser->lexer);
32470 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32471 goto resync_fail;
32473 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32474 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32475 location);
32476 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32477 OMP_CLAUSE_CHAIN (c) = list;
32478 return c;
32480 invalid_kind:
32481 cp_parser_error (parser, "invalid depend kind");
32482 resync_fail:
32483 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32484 /*or_comma=*/false,
32485 /*consume_paren=*/true);
32486 return list;
32489 /* OpenACC:
32490 async [( int-expr )] */
32492 static tree
32493 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32495 tree c, t;
32496 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32498 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32500 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32502 cp_lexer_consume_token (parser->lexer);
32504 t = cp_parser_expression (parser);
32505 if (t == error_mark_node
32506 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32507 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32508 /*or_comma=*/false,
32509 /*consume_paren=*/true);
32512 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32514 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32515 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32516 OMP_CLAUSE_CHAIN (c) = list;
32517 list = c;
32519 return list;
32522 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32523 is a bitmask in MASK. Return the list of clauses found. */
32525 static tree
32526 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32527 const char *where, cp_token *pragma_tok,
32528 bool finish_p = true)
32530 tree clauses = NULL;
32531 bool first = true;
32533 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32535 location_t here;
32536 pragma_omp_clause c_kind;
32537 omp_clause_code code;
32538 const char *c_name;
32539 tree prev = clauses;
32541 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32542 cp_lexer_consume_token (parser->lexer);
32544 here = cp_lexer_peek_token (parser->lexer)->location;
32545 c_kind = cp_parser_omp_clause_name (parser);
32547 switch (c_kind)
32549 case PRAGMA_OACC_CLAUSE_ASYNC:
32550 clauses = cp_parser_oacc_clause_async (parser, clauses);
32551 c_name = "async";
32552 break;
32553 case PRAGMA_OACC_CLAUSE_AUTO:
32554 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32555 clauses, here);
32556 c_name = "auto";
32557 break;
32558 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32559 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32560 c_name = "collapse";
32561 break;
32562 case PRAGMA_OACC_CLAUSE_COPY:
32563 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32564 c_name = "copy";
32565 break;
32566 case PRAGMA_OACC_CLAUSE_COPYIN:
32567 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32568 c_name = "copyin";
32569 break;
32570 case PRAGMA_OACC_CLAUSE_COPYOUT:
32571 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32572 c_name = "copyout";
32573 break;
32574 case PRAGMA_OACC_CLAUSE_CREATE:
32575 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32576 c_name = "create";
32577 break;
32578 case PRAGMA_OACC_CLAUSE_DELETE:
32579 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32580 c_name = "delete";
32581 break;
32582 case PRAGMA_OMP_CLAUSE_DEFAULT:
32583 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32584 c_name = "default";
32585 break;
32586 case PRAGMA_OACC_CLAUSE_DEVICE:
32587 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32588 c_name = "device";
32589 break;
32590 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32591 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32592 c_name = "deviceptr";
32593 break;
32594 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32595 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32596 c_name = "device_resident";
32597 break;
32598 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32599 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32600 clauses);
32601 c_name = "firstprivate";
32602 break;
32603 case PRAGMA_OACC_CLAUSE_GANG:
32604 c_name = "gang";
32605 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32606 c_name, clauses);
32607 break;
32608 case PRAGMA_OACC_CLAUSE_HOST:
32609 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32610 c_name = "host";
32611 break;
32612 case PRAGMA_OACC_CLAUSE_IF:
32613 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32614 c_name = "if";
32615 break;
32616 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32617 clauses = cp_parser_oacc_simple_clause (parser,
32618 OMP_CLAUSE_INDEPENDENT,
32619 clauses, here);
32620 c_name = "independent";
32621 break;
32622 case PRAGMA_OACC_CLAUSE_LINK:
32623 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32624 c_name = "link";
32625 break;
32626 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32627 code = OMP_CLAUSE_NUM_GANGS;
32628 c_name = "num_gangs";
32629 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32630 clauses);
32631 break;
32632 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32633 c_name = "num_workers";
32634 code = OMP_CLAUSE_NUM_WORKERS;
32635 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32636 clauses);
32637 break;
32638 case PRAGMA_OACC_CLAUSE_PRESENT:
32639 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32640 c_name = "present";
32641 break;
32642 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32643 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32644 c_name = "present_or_copy";
32645 break;
32646 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32647 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32648 c_name = "present_or_copyin";
32649 break;
32650 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32651 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32652 c_name = "present_or_copyout";
32653 break;
32654 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32655 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32656 c_name = "present_or_create";
32657 break;
32658 case PRAGMA_OACC_CLAUSE_PRIVATE:
32659 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32660 clauses);
32661 c_name = "private";
32662 break;
32663 case PRAGMA_OACC_CLAUSE_REDUCTION:
32664 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32665 c_name = "reduction";
32666 break;
32667 case PRAGMA_OACC_CLAUSE_SELF:
32668 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32669 c_name = "self";
32670 break;
32671 case PRAGMA_OACC_CLAUSE_SEQ:
32672 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32673 clauses, here);
32674 c_name = "seq";
32675 break;
32676 case PRAGMA_OACC_CLAUSE_TILE:
32677 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32678 c_name = "tile";
32679 break;
32680 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32681 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32682 clauses);
32683 c_name = "use_device";
32684 break;
32685 case PRAGMA_OACC_CLAUSE_VECTOR:
32686 c_name = "vector";
32687 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32688 c_name, clauses);
32689 break;
32690 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32691 c_name = "vector_length";
32692 code = OMP_CLAUSE_VECTOR_LENGTH;
32693 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32694 clauses);
32695 break;
32696 case PRAGMA_OACC_CLAUSE_WAIT:
32697 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32698 c_name = "wait";
32699 break;
32700 case PRAGMA_OACC_CLAUSE_WORKER:
32701 c_name = "worker";
32702 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32703 c_name, clauses);
32704 break;
32705 default:
32706 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32707 goto saw_error;
32710 first = false;
32712 if (((mask >> c_kind) & 1) == 0)
32714 /* Remove the invalid clause(s) from the list to avoid
32715 confusing the rest of the compiler. */
32716 clauses = prev;
32717 error_at (here, "%qs is not valid for %qs", c_name, where);
32721 saw_error:
32722 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32724 if (finish_p)
32725 return finish_omp_clauses (clauses, C_ORT_ACC);
32727 return clauses;
32730 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32731 is a bitmask in MASK. Return the list of clauses found; the result
32732 of clause default goes in *pdefault. */
32734 static tree
32735 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32736 const char *where, cp_token *pragma_tok,
32737 bool finish_p = true)
32739 tree clauses = NULL;
32740 bool first = true;
32741 cp_token *token = NULL;
32743 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32745 pragma_omp_clause c_kind;
32746 const char *c_name;
32747 tree prev = clauses;
32749 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32750 cp_lexer_consume_token (parser->lexer);
32752 token = cp_lexer_peek_token (parser->lexer);
32753 c_kind = cp_parser_omp_clause_name (parser);
32755 switch (c_kind)
32757 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32758 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32759 token->location);
32760 c_name = "collapse";
32761 break;
32762 case PRAGMA_OMP_CLAUSE_COPYIN:
32763 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32764 c_name = "copyin";
32765 break;
32766 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32767 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32768 clauses);
32769 c_name = "copyprivate";
32770 break;
32771 case PRAGMA_OMP_CLAUSE_DEFAULT:
32772 clauses = cp_parser_omp_clause_default (parser, clauses,
32773 token->location, false);
32774 c_name = "default";
32775 break;
32776 case PRAGMA_OMP_CLAUSE_FINAL:
32777 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32778 c_name = "final";
32779 break;
32780 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32781 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32782 clauses);
32783 c_name = "firstprivate";
32784 break;
32785 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32786 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32787 token->location);
32788 c_name = "grainsize";
32789 break;
32790 case PRAGMA_OMP_CLAUSE_HINT:
32791 clauses = cp_parser_omp_clause_hint (parser, clauses,
32792 token->location);
32793 c_name = "hint";
32794 break;
32795 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32796 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32797 token->location);
32798 c_name = "defaultmap";
32799 break;
32800 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32801 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32802 clauses);
32803 c_name = "use_device_ptr";
32804 break;
32805 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32806 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32807 clauses);
32808 c_name = "is_device_ptr";
32809 break;
32810 case PRAGMA_OMP_CLAUSE_IF:
32811 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32812 true);
32813 c_name = "if";
32814 break;
32815 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32816 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32817 clauses);
32818 c_name = "lastprivate";
32819 break;
32820 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32821 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32822 token->location);
32823 c_name = "mergeable";
32824 break;
32825 case PRAGMA_OMP_CLAUSE_NOWAIT:
32826 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32827 c_name = "nowait";
32828 break;
32829 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32830 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32831 token->location);
32832 c_name = "num_tasks";
32833 break;
32834 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32835 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32836 token->location);
32837 c_name = "num_threads";
32838 break;
32839 case PRAGMA_OMP_CLAUSE_ORDERED:
32840 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32841 token->location);
32842 c_name = "ordered";
32843 break;
32844 case PRAGMA_OMP_CLAUSE_PRIORITY:
32845 clauses = cp_parser_omp_clause_priority (parser, clauses,
32846 token->location);
32847 c_name = "priority";
32848 break;
32849 case PRAGMA_OMP_CLAUSE_PRIVATE:
32850 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32851 clauses);
32852 c_name = "private";
32853 break;
32854 case PRAGMA_OMP_CLAUSE_REDUCTION:
32855 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32856 c_name = "reduction";
32857 break;
32858 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32859 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32860 token->location);
32861 c_name = "schedule";
32862 break;
32863 case PRAGMA_OMP_CLAUSE_SHARED:
32864 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32865 clauses);
32866 c_name = "shared";
32867 break;
32868 case PRAGMA_OMP_CLAUSE_UNTIED:
32869 clauses = cp_parser_omp_clause_untied (parser, clauses,
32870 token->location);
32871 c_name = "untied";
32872 break;
32873 case PRAGMA_OMP_CLAUSE_INBRANCH:
32874 case PRAGMA_CILK_CLAUSE_MASK:
32875 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32876 clauses, token->location);
32877 c_name = "inbranch";
32878 break;
32879 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32880 case PRAGMA_CILK_CLAUSE_NOMASK:
32881 clauses = cp_parser_omp_clause_branch (parser,
32882 OMP_CLAUSE_NOTINBRANCH,
32883 clauses, token->location);
32884 c_name = "notinbranch";
32885 break;
32886 case PRAGMA_OMP_CLAUSE_PARALLEL:
32887 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32888 clauses, token->location);
32889 c_name = "parallel";
32890 if (!first)
32892 clause_not_first:
32893 error_at (token->location, "%qs must be the first clause of %qs",
32894 c_name, where);
32895 clauses = prev;
32897 break;
32898 case PRAGMA_OMP_CLAUSE_FOR:
32899 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32900 clauses, token->location);
32901 c_name = "for";
32902 if (!first)
32903 goto clause_not_first;
32904 break;
32905 case PRAGMA_OMP_CLAUSE_SECTIONS:
32906 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32907 clauses, token->location);
32908 c_name = "sections";
32909 if (!first)
32910 goto clause_not_first;
32911 break;
32912 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32913 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32914 clauses, token->location);
32915 c_name = "taskgroup";
32916 if (!first)
32917 goto clause_not_first;
32918 break;
32919 case PRAGMA_OMP_CLAUSE_LINK:
32920 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32921 c_name = "to";
32922 break;
32923 case PRAGMA_OMP_CLAUSE_TO:
32924 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32925 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32926 clauses);
32927 else
32928 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32929 c_name = "to";
32930 break;
32931 case PRAGMA_OMP_CLAUSE_FROM:
32932 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32933 c_name = "from";
32934 break;
32935 case PRAGMA_OMP_CLAUSE_UNIFORM:
32936 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32937 clauses);
32938 c_name = "uniform";
32939 break;
32940 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32941 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32942 token->location);
32943 c_name = "num_teams";
32944 break;
32945 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32946 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32947 token->location);
32948 c_name = "thread_limit";
32949 break;
32950 case PRAGMA_OMP_CLAUSE_ALIGNED:
32951 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32952 c_name = "aligned";
32953 break;
32954 case PRAGMA_OMP_CLAUSE_LINEAR:
32956 bool cilk_simd_fn = false, declare_simd = false;
32957 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32958 cilk_simd_fn = true;
32959 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32960 declare_simd = true;
32961 clauses = cp_parser_omp_clause_linear (parser, clauses,
32962 cilk_simd_fn, declare_simd);
32964 c_name = "linear";
32965 break;
32966 case PRAGMA_OMP_CLAUSE_DEPEND:
32967 clauses = cp_parser_omp_clause_depend (parser, clauses,
32968 token->location);
32969 c_name = "depend";
32970 break;
32971 case PRAGMA_OMP_CLAUSE_MAP:
32972 clauses = cp_parser_omp_clause_map (parser, clauses);
32973 c_name = "map";
32974 break;
32975 case PRAGMA_OMP_CLAUSE_DEVICE:
32976 clauses = cp_parser_omp_clause_device (parser, clauses,
32977 token->location);
32978 c_name = "device";
32979 break;
32980 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32981 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32982 token->location);
32983 c_name = "dist_schedule";
32984 break;
32985 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32986 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32987 token->location);
32988 c_name = "proc_bind";
32989 break;
32990 case PRAGMA_OMP_CLAUSE_SAFELEN:
32991 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32992 token->location);
32993 c_name = "safelen";
32994 break;
32995 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32996 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32997 token->location);
32998 c_name = "simdlen";
32999 break;
33000 case PRAGMA_OMP_CLAUSE_NOGROUP:
33001 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33002 token->location);
33003 c_name = "nogroup";
33004 break;
33005 case PRAGMA_OMP_CLAUSE_THREADS:
33006 clauses
33007 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33008 clauses, token->location);
33009 c_name = "threads";
33010 break;
33011 case PRAGMA_OMP_CLAUSE_SIMD:
33012 clauses
33013 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33014 clauses, token->location);
33015 c_name = "simd";
33016 break;
33017 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33018 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33019 c_name = "simdlen";
33020 break;
33021 default:
33022 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33023 goto saw_error;
33026 first = false;
33028 if (((mask >> c_kind) & 1) == 0)
33030 /* Remove the invalid clause(s) from the list to avoid
33031 confusing the rest of the compiler. */
33032 clauses = prev;
33033 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33036 saw_error:
33037 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33038 no reason to skip to the end. */
33039 if (!(flag_cilkplus && pragma_tok == NULL))
33040 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33041 if (finish_p)
33043 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33044 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33045 else
33046 return finish_omp_clauses (clauses, C_ORT_OMP);
33048 return clauses;
33051 /* OpenMP 2.5:
33052 structured-block:
33053 statement
33055 In practice, we're also interested in adding the statement to an
33056 outer node. So it is convenient if we work around the fact that
33057 cp_parser_statement calls add_stmt. */
33059 static unsigned
33060 cp_parser_begin_omp_structured_block (cp_parser *parser)
33062 unsigned save = parser->in_statement;
33064 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33065 This preserves the "not within loop or switch" style error messages
33066 for nonsense cases like
33067 void foo() {
33068 #pragma omp single
33069 break;
33072 if (parser->in_statement)
33073 parser->in_statement = IN_OMP_BLOCK;
33075 return save;
33078 static void
33079 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33081 parser->in_statement = save;
33084 static tree
33085 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33087 tree stmt = begin_omp_structured_block ();
33088 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33090 cp_parser_statement (parser, NULL_TREE, false, if_p);
33092 cp_parser_end_omp_structured_block (parser, save);
33093 return finish_omp_structured_block (stmt);
33096 /* OpenMP 2.5:
33097 # pragma omp atomic new-line
33098 expression-stmt
33100 expression-stmt:
33101 x binop= expr | x++ | ++x | x-- | --x
33102 binop:
33103 +, *, -, /, &, ^, |, <<, >>
33105 where x is an lvalue expression with scalar type.
33107 OpenMP 3.1:
33108 # pragma omp atomic new-line
33109 update-stmt
33111 # pragma omp atomic read new-line
33112 read-stmt
33114 # pragma omp atomic write new-line
33115 write-stmt
33117 # pragma omp atomic update new-line
33118 update-stmt
33120 # pragma omp atomic capture new-line
33121 capture-stmt
33123 # pragma omp atomic capture new-line
33124 capture-block
33126 read-stmt:
33127 v = x
33128 write-stmt:
33129 x = expr
33130 update-stmt:
33131 expression-stmt | x = x binop expr
33132 capture-stmt:
33133 v = expression-stmt
33134 capture-block:
33135 { v = x; update-stmt; } | { update-stmt; v = x; }
33137 OpenMP 4.0:
33138 update-stmt:
33139 expression-stmt | x = x binop expr | x = expr binop x
33140 capture-stmt:
33141 v = update-stmt
33142 capture-block:
33143 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33145 where x and v are lvalue expressions with scalar type. */
33147 static void
33148 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33150 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33151 tree rhs1 = NULL_TREE, orig_lhs;
33152 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33153 bool structured_block = false;
33154 bool seq_cst = false;
33156 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33158 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33159 const char *p = IDENTIFIER_POINTER (id);
33161 if (!strcmp (p, "seq_cst"))
33163 seq_cst = true;
33164 cp_lexer_consume_token (parser->lexer);
33165 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33166 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33167 cp_lexer_consume_token (parser->lexer);
33170 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33172 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33173 const char *p = IDENTIFIER_POINTER (id);
33175 if (!strcmp (p, "read"))
33176 code = OMP_ATOMIC_READ;
33177 else if (!strcmp (p, "write"))
33178 code = NOP_EXPR;
33179 else if (!strcmp (p, "update"))
33180 code = OMP_ATOMIC;
33181 else if (!strcmp (p, "capture"))
33182 code = OMP_ATOMIC_CAPTURE_NEW;
33183 else
33184 p = NULL;
33185 if (p)
33186 cp_lexer_consume_token (parser->lexer);
33188 if (!seq_cst)
33190 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33191 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33192 cp_lexer_consume_token (parser->lexer);
33194 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33196 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33197 const char *p = IDENTIFIER_POINTER (id);
33199 if (!strcmp (p, "seq_cst"))
33201 seq_cst = true;
33202 cp_lexer_consume_token (parser->lexer);
33206 cp_parser_require_pragma_eol (parser, pragma_tok);
33208 switch (code)
33210 case OMP_ATOMIC_READ:
33211 case NOP_EXPR: /* atomic write */
33212 v = cp_parser_unary_expression (parser);
33213 if (v == error_mark_node)
33214 goto saw_error;
33215 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33216 goto saw_error;
33217 if (code == NOP_EXPR)
33218 lhs = cp_parser_expression (parser);
33219 else
33220 lhs = cp_parser_unary_expression (parser);
33221 if (lhs == error_mark_node)
33222 goto saw_error;
33223 if (code == NOP_EXPR)
33225 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33226 opcode. */
33227 code = OMP_ATOMIC;
33228 rhs = lhs;
33229 lhs = v;
33230 v = NULL_TREE;
33232 goto done;
33233 case OMP_ATOMIC_CAPTURE_NEW:
33234 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33236 cp_lexer_consume_token (parser->lexer);
33237 structured_block = true;
33239 else
33241 v = cp_parser_unary_expression (parser);
33242 if (v == error_mark_node)
33243 goto saw_error;
33244 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33245 goto saw_error;
33247 default:
33248 break;
33251 restart:
33252 lhs = cp_parser_unary_expression (parser);
33253 orig_lhs = lhs;
33254 switch (TREE_CODE (lhs))
33256 case ERROR_MARK:
33257 goto saw_error;
33259 case POSTINCREMENT_EXPR:
33260 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33261 code = OMP_ATOMIC_CAPTURE_OLD;
33262 /* FALLTHROUGH */
33263 case PREINCREMENT_EXPR:
33264 lhs = TREE_OPERAND (lhs, 0);
33265 opcode = PLUS_EXPR;
33266 rhs = integer_one_node;
33267 break;
33269 case POSTDECREMENT_EXPR:
33270 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33271 code = OMP_ATOMIC_CAPTURE_OLD;
33272 /* FALLTHROUGH */
33273 case PREDECREMENT_EXPR:
33274 lhs = TREE_OPERAND (lhs, 0);
33275 opcode = MINUS_EXPR;
33276 rhs = integer_one_node;
33277 break;
33279 case COMPOUND_EXPR:
33280 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33281 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33282 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33283 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33284 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33285 (TREE_OPERAND (lhs, 1), 0), 0)))
33286 == BOOLEAN_TYPE)
33287 /* Undo effects of boolean_increment for post {in,de}crement. */
33288 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33289 /* FALLTHRU */
33290 case MODIFY_EXPR:
33291 if (TREE_CODE (lhs) == MODIFY_EXPR
33292 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33294 /* Undo effects of boolean_increment. */
33295 if (integer_onep (TREE_OPERAND (lhs, 1)))
33297 /* This is pre or post increment. */
33298 rhs = TREE_OPERAND (lhs, 1);
33299 lhs = TREE_OPERAND (lhs, 0);
33300 opcode = NOP_EXPR;
33301 if (code == OMP_ATOMIC_CAPTURE_NEW
33302 && !structured_block
33303 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33304 code = OMP_ATOMIC_CAPTURE_OLD;
33305 break;
33308 /* FALLTHRU */
33309 default:
33310 switch (cp_lexer_peek_token (parser->lexer)->type)
33312 case CPP_MULT_EQ:
33313 opcode = MULT_EXPR;
33314 break;
33315 case CPP_DIV_EQ:
33316 opcode = TRUNC_DIV_EXPR;
33317 break;
33318 case CPP_PLUS_EQ:
33319 opcode = PLUS_EXPR;
33320 break;
33321 case CPP_MINUS_EQ:
33322 opcode = MINUS_EXPR;
33323 break;
33324 case CPP_LSHIFT_EQ:
33325 opcode = LSHIFT_EXPR;
33326 break;
33327 case CPP_RSHIFT_EQ:
33328 opcode = RSHIFT_EXPR;
33329 break;
33330 case CPP_AND_EQ:
33331 opcode = BIT_AND_EXPR;
33332 break;
33333 case CPP_OR_EQ:
33334 opcode = BIT_IOR_EXPR;
33335 break;
33336 case CPP_XOR_EQ:
33337 opcode = BIT_XOR_EXPR;
33338 break;
33339 case CPP_EQ:
33340 enum cp_parser_prec oprec;
33341 cp_token *token;
33342 cp_lexer_consume_token (parser->lexer);
33343 cp_parser_parse_tentatively (parser);
33344 rhs1 = cp_parser_simple_cast_expression (parser);
33345 if (rhs1 == error_mark_node)
33347 cp_parser_abort_tentative_parse (parser);
33348 cp_parser_simple_cast_expression (parser);
33349 goto saw_error;
33351 token = cp_lexer_peek_token (parser->lexer);
33352 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33354 cp_parser_abort_tentative_parse (parser);
33355 cp_parser_parse_tentatively (parser);
33356 rhs = cp_parser_binary_expression (parser, false, true,
33357 PREC_NOT_OPERATOR, NULL);
33358 if (rhs == error_mark_node)
33360 cp_parser_abort_tentative_parse (parser);
33361 cp_parser_binary_expression (parser, false, true,
33362 PREC_NOT_OPERATOR, NULL);
33363 goto saw_error;
33365 switch (TREE_CODE (rhs))
33367 case MULT_EXPR:
33368 case TRUNC_DIV_EXPR:
33369 case RDIV_EXPR:
33370 case PLUS_EXPR:
33371 case MINUS_EXPR:
33372 case LSHIFT_EXPR:
33373 case RSHIFT_EXPR:
33374 case BIT_AND_EXPR:
33375 case BIT_IOR_EXPR:
33376 case BIT_XOR_EXPR:
33377 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33379 if (cp_parser_parse_definitely (parser))
33381 opcode = TREE_CODE (rhs);
33382 rhs1 = TREE_OPERAND (rhs, 0);
33383 rhs = TREE_OPERAND (rhs, 1);
33384 goto stmt_done;
33386 else
33387 goto saw_error;
33389 break;
33390 default:
33391 break;
33393 cp_parser_abort_tentative_parse (parser);
33394 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33396 rhs = cp_parser_expression (parser);
33397 if (rhs == error_mark_node)
33398 goto saw_error;
33399 opcode = NOP_EXPR;
33400 rhs1 = NULL_TREE;
33401 goto stmt_done;
33403 cp_parser_error (parser,
33404 "invalid form of %<#pragma omp atomic%>");
33405 goto saw_error;
33407 if (!cp_parser_parse_definitely (parser))
33408 goto saw_error;
33409 switch (token->type)
33411 case CPP_SEMICOLON:
33412 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33414 code = OMP_ATOMIC_CAPTURE_OLD;
33415 v = lhs;
33416 lhs = NULL_TREE;
33417 lhs1 = rhs1;
33418 rhs1 = NULL_TREE;
33419 cp_lexer_consume_token (parser->lexer);
33420 goto restart;
33422 else if (structured_block)
33424 opcode = NOP_EXPR;
33425 rhs = rhs1;
33426 rhs1 = NULL_TREE;
33427 goto stmt_done;
33429 cp_parser_error (parser,
33430 "invalid form of %<#pragma omp atomic%>");
33431 goto saw_error;
33432 case CPP_MULT:
33433 opcode = MULT_EXPR;
33434 break;
33435 case CPP_DIV:
33436 opcode = TRUNC_DIV_EXPR;
33437 break;
33438 case CPP_PLUS:
33439 opcode = PLUS_EXPR;
33440 break;
33441 case CPP_MINUS:
33442 opcode = MINUS_EXPR;
33443 break;
33444 case CPP_LSHIFT:
33445 opcode = LSHIFT_EXPR;
33446 break;
33447 case CPP_RSHIFT:
33448 opcode = RSHIFT_EXPR;
33449 break;
33450 case CPP_AND:
33451 opcode = BIT_AND_EXPR;
33452 break;
33453 case CPP_OR:
33454 opcode = BIT_IOR_EXPR;
33455 break;
33456 case CPP_XOR:
33457 opcode = BIT_XOR_EXPR;
33458 break;
33459 default:
33460 cp_parser_error (parser,
33461 "invalid operator for %<#pragma omp atomic%>");
33462 goto saw_error;
33464 oprec = TOKEN_PRECEDENCE (token);
33465 gcc_assert (oprec != PREC_NOT_OPERATOR);
33466 if (commutative_tree_code (opcode))
33467 oprec = (enum cp_parser_prec) (oprec - 1);
33468 cp_lexer_consume_token (parser->lexer);
33469 rhs = cp_parser_binary_expression (parser, false, false,
33470 oprec, NULL);
33471 if (rhs == error_mark_node)
33472 goto saw_error;
33473 goto stmt_done;
33474 /* FALLTHROUGH */
33475 default:
33476 cp_parser_error (parser,
33477 "invalid operator for %<#pragma omp atomic%>");
33478 goto saw_error;
33480 cp_lexer_consume_token (parser->lexer);
33482 rhs = cp_parser_expression (parser);
33483 if (rhs == error_mark_node)
33484 goto saw_error;
33485 break;
33487 stmt_done:
33488 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33490 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33491 goto saw_error;
33492 v = cp_parser_unary_expression (parser);
33493 if (v == error_mark_node)
33494 goto saw_error;
33495 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33496 goto saw_error;
33497 lhs1 = cp_parser_unary_expression (parser);
33498 if (lhs1 == error_mark_node)
33499 goto saw_error;
33501 if (structured_block)
33503 cp_parser_consume_semicolon_at_end_of_statement (parser);
33504 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33506 done:
33507 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33508 if (!structured_block)
33509 cp_parser_consume_semicolon_at_end_of_statement (parser);
33510 return;
33512 saw_error:
33513 cp_parser_skip_to_end_of_block_or_statement (parser);
33514 if (structured_block)
33516 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33517 cp_lexer_consume_token (parser->lexer);
33518 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33520 cp_parser_skip_to_end_of_block_or_statement (parser);
33521 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33522 cp_lexer_consume_token (parser->lexer);
33528 /* OpenMP 2.5:
33529 # pragma omp barrier new-line */
33531 static void
33532 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33534 cp_parser_require_pragma_eol (parser, pragma_tok);
33535 finish_omp_barrier ();
33538 /* OpenMP 2.5:
33539 # pragma omp critical [(name)] new-line
33540 structured-block
33542 OpenMP 4.5:
33543 # pragma omp critical [(name) [hint(expression)]] new-line
33544 structured-block */
33546 #define OMP_CRITICAL_CLAUSE_MASK \
33547 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33549 static tree
33550 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33552 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33554 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33556 cp_lexer_consume_token (parser->lexer);
33558 name = cp_parser_identifier (parser);
33560 if (name == error_mark_node
33561 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33562 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33563 /*or_comma=*/false,
33564 /*consume_paren=*/true);
33565 if (name == error_mark_node)
33566 name = NULL;
33568 clauses = cp_parser_omp_all_clauses (parser,
33569 OMP_CRITICAL_CLAUSE_MASK,
33570 "#pragma omp critical", pragma_tok);
33572 else
33573 cp_parser_require_pragma_eol (parser, pragma_tok);
33575 stmt = cp_parser_omp_structured_block (parser, if_p);
33576 return c_finish_omp_critical (input_location, stmt, name, clauses);
33579 /* OpenMP 2.5:
33580 # pragma omp flush flush-vars[opt] new-line
33582 flush-vars:
33583 ( variable-list ) */
33585 static void
33586 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33588 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33589 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33590 cp_parser_require_pragma_eol (parser, pragma_tok);
33592 finish_omp_flush ();
33595 /* Helper function, to parse omp for increment expression. */
33597 static tree
33598 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33600 tree cond = cp_parser_binary_expression (parser, false, true,
33601 PREC_NOT_OPERATOR, NULL);
33602 if (cond == error_mark_node
33603 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33605 cp_parser_skip_to_end_of_statement (parser);
33606 return error_mark_node;
33609 switch (TREE_CODE (cond))
33611 case GT_EXPR:
33612 case GE_EXPR:
33613 case LT_EXPR:
33614 case LE_EXPR:
33615 break;
33616 case NE_EXPR:
33617 if (code == CILK_SIMD || code == CILK_FOR)
33618 break;
33619 /* Fall through: OpenMP disallows NE_EXPR. */
33620 gcc_fallthrough ();
33621 default:
33622 return error_mark_node;
33625 /* If decl is an iterator, preserve LHS and RHS of the relational
33626 expr until finish_omp_for. */
33627 if (decl
33628 && (type_dependent_expression_p (decl)
33629 || CLASS_TYPE_P (TREE_TYPE (decl))))
33630 return cond;
33632 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33633 TREE_CODE (cond),
33634 TREE_OPERAND (cond, 0), ERROR_MARK,
33635 TREE_OPERAND (cond, 1), ERROR_MARK,
33636 /*overload=*/NULL, tf_warning_or_error);
33639 /* Helper function, to parse omp for increment expression. */
33641 static tree
33642 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33644 cp_token *token = cp_lexer_peek_token (parser->lexer);
33645 enum tree_code op;
33646 tree lhs, rhs;
33647 cp_id_kind idk;
33648 bool decl_first;
33650 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33652 op = (token->type == CPP_PLUS_PLUS
33653 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33654 cp_lexer_consume_token (parser->lexer);
33655 lhs = cp_parser_simple_cast_expression (parser);
33656 if (lhs != decl
33657 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33658 return error_mark_node;
33659 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33662 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33663 if (lhs != decl
33664 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33665 return error_mark_node;
33667 token = cp_lexer_peek_token (parser->lexer);
33668 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33670 op = (token->type == CPP_PLUS_PLUS
33671 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33672 cp_lexer_consume_token (parser->lexer);
33673 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33676 op = cp_parser_assignment_operator_opt (parser);
33677 if (op == ERROR_MARK)
33678 return error_mark_node;
33680 if (op != NOP_EXPR)
33682 rhs = cp_parser_assignment_expression (parser);
33683 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33684 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33687 lhs = cp_parser_binary_expression (parser, false, false,
33688 PREC_ADDITIVE_EXPRESSION, NULL);
33689 token = cp_lexer_peek_token (parser->lexer);
33690 decl_first = (lhs == decl
33691 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33692 if (decl_first)
33693 lhs = NULL_TREE;
33694 if (token->type != CPP_PLUS
33695 && token->type != CPP_MINUS)
33696 return error_mark_node;
33700 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33701 cp_lexer_consume_token (parser->lexer);
33702 rhs = cp_parser_binary_expression (parser, false, false,
33703 PREC_ADDITIVE_EXPRESSION, NULL);
33704 token = cp_lexer_peek_token (parser->lexer);
33705 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33707 if (lhs == NULL_TREE)
33709 if (op == PLUS_EXPR)
33710 lhs = rhs;
33711 else
33712 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33713 tf_warning_or_error);
33715 else
33716 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33717 ERROR_MARK, NULL, tf_warning_or_error);
33720 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33722 if (!decl_first)
33724 if ((rhs != decl
33725 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33726 || op == MINUS_EXPR)
33727 return error_mark_node;
33728 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33730 else
33731 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33733 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33736 /* Parse the initialization statement of either an OpenMP for loop or
33737 a Cilk Plus for loop.
33739 Return true if the resulting construct should have an
33740 OMP_CLAUSE_PRIVATE added to it. */
33742 static tree
33743 cp_parser_omp_for_loop_init (cp_parser *parser,
33744 enum tree_code code,
33745 tree &this_pre_body,
33746 vec<tree, va_gc> *for_block,
33747 tree &init,
33748 tree &orig_init,
33749 tree &decl,
33750 tree &real_decl)
33752 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33753 return NULL_TREE;
33755 tree add_private_clause = NULL_TREE;
33757 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33759 init-expr:
33760 var = lb
33761 integer-type var = lb
33762 random-access-iterator-type var = lb
33763 pointer-type var = lb
33765 cp_decl_specifier_seq type_specifiers;
33767 /* First, try to parse as an initialized declaration. See
33768 cp_parser_condition, from whence the bulk of this is copied. */
33770 cp_parser_parse_tentatively (parser);
33771 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33772 /*is_trailing_return=*/false,
33773 &type_specifiers);
33774 if (cp_parser_parse_definitely (parser))
33776 /* If parsing a type specifier seq succeeded, then this
33777 MUST be a initialized declaration. */
33778 tree asm_specification, attributes;
33779 cp_declarator *declarator;
33781 declarator = cp_parser_declarator (parser,
33782 CP_PARSER_DECLARATOR_NAMED,
33783 /*ctor_dtor_or_conv_p=*/NULL,
33784 /*parenthesized_p=*/NULL,
33785 /*member_p=*/false,
33786 /*friend_p=*/false);
33787 attributes = cp_parser_attributes_opt (parser);
33788 asm_specification = cp_parser_asm_specification_opt (parser);
33790 if (declarator == cp_error_declarator)
33791 cp_parser_skip_to_end_of_statement (parser);
33793 else
33795 tree pushed_scope, auto_node;
33797 decl = start_decl (declarator, &type_specifiers,
33798 SD_INITIALIZED, attributes,
33799 /*prefix_attributes=*/NULL_TREE,
33800 &pushed_scope);
33802 auto_node = type_uses_auto (TREE_TYPE (decl));
33803 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33805 if (cp_lexer_next_token_is (parser->lexer,
33806 CPP_OPEN_PAREN))
33808 if (code != CILK_SIMD && code != CILK_FOR)
33809 error ("parenthesized initialization is not allowed in "
33810 "OpenMP %<for%> loop");
33811 else
33812 error ("parenthesized initialization is "
33813 "not allowed in for-loop");
33815 else
33816 /* Trigger an error. */
33817 cp_parser_require (parser, CPP_EQ, RT_EQ);
33819 init = error_mark_node;
33820 cp_parser_skip_to_end_of_statement (parser);
33822 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33823 || type_dependent_expression_p (decl)
33824 || auto_node)
33826 bool is_direct_init, is_non_constant_init;
33828 init = cp_parser_initializer (parser,
33829 &is_direct_init,
33830 &is_non_constant_init);
33832 if (auto_node)
33834 TREE_TYPE (decl)
33835 = do_auto_deduction (TREE_TYPE (decl), init,
33836 auto_node);
33838 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33839 && !type_dependent_expression_p (decl))
33840 goto non_class;
33843 cp_finish_decl (decl, init, !is_non_constant_init,
33844 asm_specification,
33845 LOOKUP_ONLYCONVERTING);
33846 orig_init = init;
33847 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33849 vec_safe_push (for_block, this_pre_body);
33850 init = NULL_TREE;
33852 else
33854 init = pop_stmt_list (this_pre_body);
33855 if (init && TREE_CODE (init) == STATEMENT_LIST)
33857 tree_stmt_iterator i = tsi_start (init);
33858 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
33859 while (!tsi_end_p (i))
33861 tree t = tsi_stmt (i);
33862 if (TREE_CODE (t) == DECL_EXPR
33863 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
33865 tsi_delink (&i);
33866 vec_safe_push (for_block, t);
33867 continue;
33869 break;
33871 if (tsi_one_before_end_p (i))
33873 tree t = tsi_stmt (i);
33874 tsi_delink (&i);
33875 free_stmt_list (init);
33876 init = t;
33880 this_pre_body = NULL_TREE;
33882 else
33884 /* Consume '='. */
33885 cp_lexer_consume_token (parser->lexer);
33886 init = cp_parser_assignment_expression (parser);
33888 non_class:
33889 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33890 init = error_mark_node;
33891 else
33892 cp_finish_decl (decl, NULL_TREE,
33893 /*init_const_expr_p=*/false,
33894 asm_specification,
33895 LOOKUP_ONLYCONVERTING);
33898 if (pushed_scope)
33899 pop_scope (pushed_scope);
33902 else
33904 cp_id_kind idk;
33905 /* If parsing a type specifier sequence failed, then
33906 this MUST be a simple expression. */
33907 if (code == CILK_FOR)
33908 error ("%<_Cilk_for%> allows expression instead of declaration only "
33909 "in C, not in C++");
33910 cp_parser_parse_tentatively (parser);
33911 decl = cp_parser_primary_expression (parser, false, false,
33912 false, &idk);
33913 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33914 if (!cp_parser_error_occurred (parser)
33915 && decl
33916 && (TREE_CODE (decl) == COMPONENT_REF
33917 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33919 cp_parser_abort_tentative_parse (parser);
33920 cp_parser_parse_tentatively (parser);
33921 cp_token *token = cp_lexer_peek_token (parser->lexer);
33922 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33923 /*check_dependency_p=*/true,
33924 /*template_p=*/NULL,
33925 /*declarator_p=*/false,
33926 /*optional_p=*/false);
33927 if (name != error_mark_node
33928 && last_tok == cp_lexer_peek_token (parser->lexer))
33930 decl = cp_parser_lookup_name_simple (parser, name,
33931 token->location);
33932 if (TREE_CODE (decl) == FIELD_DECL)
33933 add_private_clause = omp_privatize_field (decl, false);
33935 cp_parser_abort_tentative_parse (parser);
33936 cp_parser_parse_tentatively (parser);
33937 decl = cp_parser_primary_expression (parser, false, false,
33938 false, &idk);
33940 if (!cp_parser_error_occurred (parser)
33941 && decl
33942 && DECL_P (decl)
33943 && CLASS_TYPE_P (TREE_TYPE (decl)))
33945 tree rhs;
33947 cp_parser_parse_definitely (parser);
33948 cp_parser_require (parser, CPP_EQ, RT_EQ);
33949 rhs = cp_parser_assignment_expression (parser);
33950 orig_init = rhs;
33951 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33952 decl, NOP_EXPR,
33953 rhs,
33954 tf_warning_or_error));
33955 if (!add_private_clause)
33956 add_private_clause = decl;
33958 else
33960 decl = NULL;
33961 cp_parser_abort_tentative_parse (parser);
33962 init = cp_parser_expression (parser);
33963 if (init)
33965 if (TREE_CODE (init) == MODIFY_EXPR
33966 || TREE_CODE (init) == MODOP_EXPR)
33967 real_decl = TREE_OPERAND (init, 0);
33971 return add_private_clause;
33974 /* Parse the restricted form of the for statement allowed by OpenMP. */
33976 static tree
33977 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33978 tree *cclauses, bool *if_p)
33980 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33981 tree real_decl, initv, condv, incrv, declv;
33982 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33983 location_t loc_first;
33984 bool collapse_err = false;
33985 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33986 vec<tree, va_gc> *for_block = make_tree_vector ();
33987 auto_vec<tree, 4> orig_inits;
33989 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33990 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33991 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33992 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33993 && OMP_CLAUSE_ORDERED_EXPR (cl))
33995 ordered_cl = cl;
33996 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33999 if (ordered && ordered < collapse)
34001 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34002 "%<ordered%> clause parameter is less than %<collapse%>");
34003 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34004 = build_int_cst (NULL_TREE, collapse);
34005 ordered = collapse;
34007 if (ordered)
34009 for (tree *pc = &clauses; *pc; )
34010 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34012 error_at (OMP_CLAUSE_LOCATION (*pc),
34013 "%<linear%> clause may not be specified together "
34014 "with %<ordered%> clause with a parameter");
34015 *pc = OMP_CLAUSE_CHAIN (*pc);
34017 else
34018 pc = &OMP_CLAUSE_CHAIN (*pc);
34021 gcc_assert (collapse >= 1 && ordered >= 0);
34022 count = ordered ? ordered : collapse;
34024 declv = make_tree_vec (count);
34025 initv = make_tree_vec (count);
34026 condv = make_tree_vec (count);
34027 incrv = make_tree_vec (count);
34029 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34031 for (i = 0; i < count; i++)
34033 int bracecount = 0;
34034 tree add_private_clause = NULL_TREE;
34035 location_t loc;
34037 if (code != CILK_FOR
34038 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34040 cp_parser_error (parser, "for statement expected");
34041 return NULL;
34043 if (code == CILK_FOR
34044 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34046 cp_parser_error (parser, "_Cilk_for statement expected");
34047 return NULL;
34049 loc = cp_lexer_consume_token (parser->lexer)->location;
34051 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34052 return NULL;
34054 init = orig_init = decl = real_decl = NULL;
34055 this_pre_body = push_stmt_list ();
34057 add_private_clause
34058 = cp_parser_omp_for_loop_init (parser, code,
34059 this_pre_body, for_block,
34060 init, orig_init, decl, real_decl);
34062 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34063 if (this_pre_body)
34065 this_pre_body = pop_stmt_list (this_pre_body);
34066 if (pre_body)
34068 tree t = pre_body;
34069 pre_body = push_stmt_list ();
34070 add_stmt (t);
34071 add_stmt (this_pre_body);
34072 pre_body = pop_stmt_list (pre_body);
34074 else
34075 pre_body = this_pre_body;
34078 if (decl)
34079 real_decl = decl;
34080 if (cclauses != NULL
34081 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34082 && real_decl != NULL_TREE)
34084 tree *c;
34085 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34086 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34087 && OMP_CLAUSE_DECL (*c) == real_decl)
34089 error_at (loc, "iteration variable %qD"
34090 " should not be firstprivate", real_decl);
34091 *c = OMP_CLAUSE_CHAIN (*c);
34093 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34094 && OMP_CLAUSE_DECL (*c) == real_decl)
34096 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34097 tree l = *c;
34098 *c = OMP_CLAUSE_CHAIN (*c);
34099 if (code == OMP_SIMD)
34101 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34102 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34104 else
34106 OMP_CLAUSE_CHAIN (l) = clauses;
34107 clauses = l;
34109 add_private_clause = NULL_TREE;
34111 else
34113 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34114 && OMP_CLAUSE_DECL (*c) == real_decl)
34115 add_private_clause = NULL_TREE;
34116 c = &OMP_CLAUSE_CHAIN (*c);
34120 if (add_private_clause)
34122 tree c;
34123 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34125 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34126 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34127 && OMP_CLAUSE_DECL (c) == decl)
34128 break;
34129 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34130 && OMP_CLAUSE_DECL (c) == decl)
34131 error_at (loc, "iteration variable %qD "
34132 "should not be firstprivate",
34133 decl);
34134 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34135 && OMP_CLAUSE_DECL (c) == decl)
34136 error_at (loc, "iteration variable %qD should not be reduction",
34137 decl);
34139 if (c == NULL)
34141 if (code != OMP_SIMD)
34142 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34143 else if (collapse == 1)
34144 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34145 else
34146 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34147 OMP_CLAUSE_DECL (c) = add_private_clause;
34148 c = finish_omp_clauses (c, C_ORT_OMP);
34149 if (c)
34151 OMP_CLAUSE_CHAIN (c) = clauses;
34152 clauses = c;
34153 /* For linear, signal that we need to fill up
34154 the so far unknown linear step. */
34155 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34156 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34161 cond = NULL;
34162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34163 cond = cp_parser_omp_for_cond (parser, decl, code);
34164 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34166 incr = NULL;
34167 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34169 /* If decl is an iterator, preserve the operator on decl
34170 until finish_omp_for. */
34171 if (real_decl
34172 && ((processing_template_decl
34173 && (TREE_TYPE (real_decl) == NULL_TREE
34174 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34175 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34176 incr = cp_parser_omp_for_incr (parser, real_decl);
34177 else
34178 incr = cp_parser_expression (parser);
34179 if (!EXPR_HAS_LOCATION (incr))
34180 protected_set_expr_location (incr, input_location);
34183 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34184 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34185 /*or_comma=*/false,
34186 /*consume_paren=*/true);
34188 TREE_VEC_ELT (declv, i) = decl;
34189 TREE_VEC_ELT (initv, i) = init;
34190 TREE_VEC_ELT (condv, i) = cond;
34191 TREE_VEC_ELT (incrv, i) = incr;
34192 if (orig_init)
34194 orig_inits.safe_grow_cleared (i + 1);
34195 orig_inits[i] = orig_init;
34198 if (i == count - 1)
34199 break;
34201 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34202 in between the collapsed for loops to be still considered perfectly
34203 nested. Hopefully the final version clarifies this.
34204 For now handle (multiple) {'s and empty statements. */
34205 cp_parser_parse_tentatively (parser);
34208 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34209 break;
34210 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34212 cp_lexer_consume_token (parser->lexer);
34213 bracecount++;
34215 else if (bracecount
34216 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34217 cp_lexer_consume_token (parser->lexer);
34218 else
34220 loc = cp_lexer_peek_token (parser->lexer)->location;
34221 error_at (loc, "not enough collapsed for loops");
34222 collapse_err = true;
34223 cp_parser_abort_tentative_parse (parser);
34224 declv = NULL_TREE;
34225 break;
34228 while (1);
34230 if (declv)
34232 cp_parser_parse_definitely (parser);
34233 nbraces += bracecount;
34237 if (nbraces)
34238 if_p = NULL;
34240 /* Note that we saved the original contents of this flag when we entered
34241 the structured block, and so we don't need to re-save it here. */
34242 if (code == CILK_SIMD || code == CILK_FOR)
34243 parser->in_statement = IN_CILK_SIMD_FOR;
34244 else
34245 parser->in_statement = IN_OMP_FOR;
34247 /* Note that the grammar doesn't call for a structured block here,
34248 though the loop as a whole is a structured block. */
34249 body = push_stmt_list ();
34250 cp_parser_statement (parser, NULL_TREE, false, if_p);
34251 body = pop_stmt_list (body);
34253 if (declv == NULL_TREE)
34254 ret = NULL_TREE;
34255 else
34256 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34257 body, pre_body, &orig_inits, clauses);
34259 while (nbraces)
34261 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34263 cp_lexer_consume_token (parser->lexer);
34264 nbraces--;
34266 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34267 cp_lexer_consume_token (parser->lexer);
34268 else
34270 if (!collapse_err)
34272 error_at (cp_lexer_peek_token (parser->lexer)->location,
34273 "collapsed loops not perfectly nested");
34275 collapse_err = true;
34276 cp_parser_statement_seq_opt (parser, NULL);
34277 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34278 break;
34282 while (!for_block->is_empty ())
34284 tree t = for_block->pop ();
34285 if (TREE_CODE (t) == STATEMENT_LIST)
34286 add_stmt (pop_stmt_list (t));
34287 else
34288 add_stmt (t);
34290 release_tree_vector (for_block);
34292 return ret;
34295 /* Helper function for OpenMP parsing, split clauses and call
34296 finish_omp_clauses on each of the set of clauses afterwards. */
34298 static void
34299 cp_omp_split_clauses (location_t loc, enum tree_code code,
34300 omp_clause_mask mask, tree clauses, tree *cclauses)
34302 int i;
34303 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34304 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34305 if (cclauses[i])
34306 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34309 /* OpenMP 4.0:
34310 #pragma omp simd simd-clause[optseq] new-line
34311 for-loop */
34313 #define OMP_SIMD_CLAUSE_MASK \
34314 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34323 static tree
34324 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34325 char *p_name, omp_clause_mask mask, tree *cclauses,
34326 bool *if_p)
34328 tree clauses, sb, ret;
34329 unsigned int save;
34330 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34332 strcat (p_name, " simd");
34333 mask |= OMP_SIMD_CLAUSE_MASK;
34335 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34336 cclauses == NULL);
34337 if (cclauses)
34339 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34340 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34341 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34342 OMP_CLAUSE_ORDERED);
34343 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34345 error_at (OMP_CLAUSE_LOCATION (c),
34346 "%<ordered%> clause with parameter may not be specified "
34347 "on %qs construct", p_name);
34348 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34352 sb = begin_omp_structured_block ();
34353 save = cp_parser_begin_omp_structured_block (parser);
34355 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34357 cp_parser_end_omp_structured_block (parser, save);
34358 add_stmt (finish_omp_structured_block (sb));
34360 return ret;
34363 /* OpenMP 2.5:
34364 #pragma omp for for-clause[optseq] new-line
34365 for-loop
34367 OpenMP 4.0:
34368 #pragma omp for simd for-simd-clause[optseq] new-line
34369 for-loop */
34371 #define OMP_FOR_CLAUSE_MASK \
34372 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34382 static tree
34383 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34384 char *p_name, omp_clause_mask mask, tree *cclauses,
34385 bool *if_p)
34387 tree clauses, sb, ret;
34388 unsigned int save;
34389 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34391 strcat (p_name, " for");
34392 mask |= OMP_FOR_CLAUSE_MASK;
34393 /* parallel for{, simd} disallows nowait clause, but for
34394 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34395 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34396 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34397 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34398 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34399 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34401 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34403 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34404 const char *p = IDENTIFIER_POINTER (id);
34406 if (strcmp (p, "simd") == 0)
34408 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34409 if (cclauses == NULL)
34410 cclauses = cclauses_buf;
34412 cp_lexer_consume_token (parser->lexer);
34413 if (!flag_openmp) /* flag_openmp_simd */
34414 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34415 cclauses, if_p);
34416 sb = begin_omp_structured_block ();
34417 save = cp_parser_begin_omp_structured_block (parser);
34418 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34419 cclauses, if_p);
34420 cp_parser_end_omp_structured_block (parser, save);
34421 tree body = finish_omp_structured_block (sb);
34422 if (ret == NULL)
34423 return ret;
34424 ret = make_node (OMP_FOR);
34425 TREE_TYPE (ret) = void_type_node;
34426 OMP_FOR_BODY (ret) = body;
34427 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34428 SET_EXPR_LOCATION (ret, loc);
34429 add_stmt (ret);
34430 return ret;
34433 if (!flag_openmp) /* flag_openmp_simd */
34435 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34436 return NULL_TREE;
34439 /* Composite distribute parallel for disallows linear clause. */
34440 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34441 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34443 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34444 cclauses == NULL);
34445 if (cclauses)
34447 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34448 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34451 sb = begin_omp_structured_block ();
34452 save = cp_parser_begin_omp_structured_block (parser);
34454 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34456 cp_parser_end_omp_structured_block (parser, save);
34457 add_stmt (finish_omp_structured_block (sb));
34459 return ret;
34462 /* OpenMP 2.5:
34463 # pragma omp master new-line
34464 structured-block */
34466 static tree
34467 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34469 cp_parser_require_pragma_eol (parser, pragma_tok);
34470 return c_finish_omp_master (input_location,
34471 cp_parser_omp_structured_block (parser, if_p));
34474 /* OpenMP 2.5:
34475 # pragma omp ordered new-line
34476 structured-block
34478 OpenMP 4.5:
34479 # pragma omp ordered ordered-clauses new-line
34480 structured-block */
34482 #define OMP_ORDERED_CLAUSE_MASK \
34483 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34486 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34487 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34489 static bool
34490 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34491 enum pragma_context context, bool *if_p)
34493 location_t loc = pragma_tok->location;
34495 if (context != pragma_stmt && context != pragma_compound)
34497 cp_parser_error (parser, "expected declaration specifiers");
34498 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34499 return false;
34502 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34504 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34505 const char *p = IDENTIFIER_POINTER (id);
34507 if (strcmp (p, "depend") == 0)
34509 if (context == pragma_stmt)
34511 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34512 "%<depend%> clause may only be used in compound "
34513 "statements");
34514 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34515 return false;
34517 tree clauses
34518 = cp_parser_omp_all_clauses (parser,
34519 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34520 "#pragma omp ordered", pragma_tok);
34521 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34522 return false;
34526 tree clauses
34527 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34528 "#pragma omp ordered", pragma_tok);
34529 c_finish_omp_ordered (loc, clauses,
34530 cp_parser_omp_structured_block (parser, if_p));
34531 return true;
34534 /* OpenMP 2.5:
34536 section-scope:
34537 { section-sequence }
34539 section-sequence:
34540 section-directive[opt] structured-block
34541 section-sequence section-directive structured-block */
34543 static tree
34544 cp_parser_omp_sections_scope (cp_parser *parser)
34546 tree stmt, substmt;
34547 bool error_suppress = false;
34548 cp_token *tok;
34550 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34551 return NULL_TREE;
34553 stmt = push_stmt_list ();
34555 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34556 != PRAGMA_OMP_SECTION)
34558 substmt = cp_parser_omp_structured_block (parser, NULL);
34559 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34560 add_stmt (substmt);
34563 while (1)
34565 tok = cp_lexer_peek_token (parser->lexer);
34566 if (tok->type == CPP_CLOSE_BRACE)
34567 break;
34568 if (tok->type == CPP_EOF)
34569 break;
34571 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34573 cp_lexer_consume_token (parser->lexer);
34574 cp_parser_require_pragma_eol (parser, tok);
34575 error_suppress = false;
34577 else if (!error_suppress)
34579 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34580 error_suppress = true;
34583 substmt = cp_parser_omp_structured_block (parser, NULL);
34584 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34585 add_stmt (substmt);
34587 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34589 substmt = pop_stmt_list (stmt);
34591 stmt = make_node (OMP_SECTIONS);
34592 TREE_TYPE (stmt) = void_type_node;
34593 OMP_SECTIONS_BODY (stmt) = substmt;
34595 add_stmt (stmt);
34596 return stmt;
34599 /* OpenMP 2.5:
34600 # pragma omp sections sections-clause[optseq] newline
34601 sections-scope */
34603 #define OMP_SECTIONS_CLAUSE_MASK \
34604 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34610 static tree
34611 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34612 char *p_name, omp_clause_mask mask, tree *cclauses)
34614 tree clauses, ret;
34615 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34617 strcat (p_name, " sections");
34618 mask |= OMP_SECTIONS_CLAUSE_MASK;
34619 if (cclauses)
34620 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34622 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34623 cclauses == NULL);
34624 if (cclauses)
34626 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34627 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34630 ret = cp_parser_omp_sections_scope (parser);
34631 if (ret)
34632 OMP_SECTIONS_CLAUSES (ret) = clauses;
34634 return ret;
34637 /* OpenMP 2.5:
34638 # pragma omp parallel parallel-clause[optseq] new-line
34639 structured-block
34640 # pragma omp parallel for parallel-for-clause[optseq] new-line
34641 structured-block
34642 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34643 structured-block
34645 OpenMP 4.0:
34646 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34647 structured-block */
34649 #define OMP_PARALLEL_CLAUSE_MASK \
34650 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34660 static tree
34661 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34662 char *p_name, omp_clause_mask mask, tree *cclauses,
34663 bool *if_p)
34665 tree stmt, clauses, block;
34666 unsigned int save;
34667 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34669 strcat (p_name, " parallel");
34670 mask |= OMP_PARALLEL_CLAUSE_MASK;
34671 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34672 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34673 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34674 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34676 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34678 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34679 if (cclauses == NULL)
34680 cclauses = cclauses_buf;
34682 cp_lexer_consume_token (parser->lexer);
34683 if (!flag_openmp) /* flag_openmp_simd */
34684 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34685 if_p);
34686 block = begin_omp_parallel ();
34687 save = cp_parser_begin_omp_structured_block (parser);
34688 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34689 if_p);
34690 cp_parser_end_omp_structured_block (parser, save);
34691 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34692 block);
34693 if (ret == NULL_TREE)
34694 return ret;
34695 OMP_PARALLEL_COMBINED (stmt) = 1;
34696 return stmt;
34698 /* When combined with distribute, parallel has to be followed by for.
34699 #pragma omp target parallel is allowed though. */
34700 else if (cclauses
34701 && (mask & (OMP_CLAUSE_MASK_1
34702 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34704 error_at (loc, "expected %<for%> after %qs", p_name);
34705 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34706 return NULL_TREE;
34708 else if (!flag_openmp) /* flag_openmp_simd */
34710 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34711 return NULL_TREE;
34713 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34715 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34716 const char *p = IDENTIFIER_POINTER (id);
34717 if (strcmp (p, "sections") == 0)
34719 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34720 cclauses = cclauses_buf;
34722 cp_lexer_consume_token (parser->lexer);
34723 block = begin_omp_parallel ();
34724 save = cp_parser_begin_omp_structured_block (parser);
34725 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34726 cp_parser_end_omp_structured_block (parser, save);
34727 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34728 block);
34729 OMP_PARALLEL_COMBINED (stmt) = 1;
34730 return stmt;
34734 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34735 cclauses == NULL);
34736 if (cclauses)
34738 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34739 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34742 block = begin_omp_parallel ();
34743 save = cp_parser_begin_omp_structured_block (parser);
34744 cp_parser_statement (parser, NULL_TREE, false, if_p);
34745 cp_parser_end_omp_structured_block (parser, save);
34746 stmt = finish_omp_parallel (clauses, block);
34747 return stmt;
34750 /* OpenMP 2.5:
34751 # pragma omp single single-clause[optseq] new-line
34752 structured-block */
34754 #define OMP_SINGLE_CLAUSE_MASK \
34755 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34760 static tree
34761 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34763 tree stmt = make_node (OMP_SINGLE);
34764 TREE_TYPE (stmt) = void_type_node;
34766 OMP_SINGLE_CLAUSES (stmt)
34767 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34768 "#pragma omp single", pragma_tok);
34769 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34771 return add_stmt (stmt);
34774 /* OpenMP 3.0:
34775 # pragma omp task task-clause[optseq] new-line
34776 structured-block */
34778 #define OMP_TASK_CLAUSE_MASK \
34779 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34790 static tree
34791 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34793 tree clauses, block;
34794 unsigned int save;
34796 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34797 "#pragma omp task", pragma_tok);
34798 block = begin_omp_task ();
34799 save = cp_parser_begin_omp_structured_block (parser);
34800 cp_parser_statement (parser, NULL_TREE, false, if_p);
34801 cp_parser_end_omp_structured_block (parser, save);
34802 return finish_omp_task (clauses, block);
34805 /* OpenMP 3.0:
34806 # pragma omp taskwait new-line */
34808 static void
34809 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34811 cp_parser_require_pragma_eol (parser, pragma_tok);
34812 finish_omp_taskwait ();
34815 /* OpenMP 3.1:
34816 # pragma omp taskyield new-line */
34818 static void
34819 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34821 cp_parser_require_pragma_eol (parser, pragma_tok);
34822 finish_omp_taskyield ();
34825 /* OpenMP 4.0:
34826 # pragma omp taskgroup new-line
34827 structured-block */
34829 static tree
34830 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34832 cp_parser_require_pragma_eol (parser, pragma_tok);
34833 return c_finish_omp_taskgroup (input_location,
34834 cp_parser_omp_structured_block (parser,
34835 if_p));
34839 /* OpenMP 2.5:
34840 # pragma omp threadprivate (variable-list) */
34842 static void
34843 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34845 tree vars;
34847 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34848 cp_parser_require_pragma_eol (parser, pragma_tok);
34850 finish_omp_threadprivate (vars);
34853 /* OpenMP 4.0:
34854 # pragma omp cancel cancel-clause[optseq] new-line */
34856 #define OMP_CANCEL_CLAUSE_MASK \
34857 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34863 static void
34864 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34866 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34867 "#pragma omp cancel", pragma_tok);
34868 finish_omp_cancel (clauses);
34871 /* OpenMP 4.0:
34872 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34874 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34875 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34880 static void
34881 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
34882 enum pragma_context context)
34884 tree clauses;
34885 bool point_seen = false;
34887 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34889 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34890 const char *p = IDENTIFIER_POINTER (id);
34892 if (strcmp (p, "point") == 0)
34894 cp_lexer_consume_token (parser->lexer);
34895 point_seen = true;
34898 if (!point_seen)
34900 cp_parser_error (parser, "expected %<point%>");
34901 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34902 return;
34905 if (context != pragma_compound)
34907 if (context == pragma_stmt)
34908 error_at (pragma_tok->location,
34909 "%<#pragma omp cancellation point%> may only be used in"
34910 " compound statements");
34911 else
34912 cp_parser_error (parser, "expected declaration specifiers");
34913 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34914 return;
34917 clauses = cp_parser_omp_all_clauses (parser,
34918 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34919 "#pragma omp cancellation point",
34920 pragma_tok);
34921 finish_omp_cancellation_point (clauses);
34924 /* OpenMP 4.0:
34925 #pragma omp distribute distribute-clause[optseq] new-line
34926 for-loop */
34928 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34929 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34935 static tree
34936 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34937 char *p_name, omp_clause_mask mask, tree *cclauses,
34938 bool *if_p)
34940 tree clauses, sb, ret;
34941 unsigned int save;
34942 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34944 strcat (p_name, " distribute");
34945 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34947 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34949 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34950 const char *p = IDENTIFIER_POINTER (id);
34951 bool simd = false;
34952 bool parallel = false;
34954 if (strcmp (p, "simd") == 0)
34955 simd = true;
34956 else
34957 parallel = strcmp (p, "parallel") == 0;
34958 if (parallel || simd)
34960 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34961 if (cclauses == NULL)
34962 cclauses = cclauses_buf;
34963 cp_lexer_consume_token (parser->lexer);
34964 if (!flag_openmp) /* flag_openmp_simd */
34966 if (simd)
34967 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34968 cclauses, if_p);
34969 else
34970 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34971 cclauses, if_p);
34973 sb = begin_omp_structured_block ();
34974 save = cp_parser_begin_omp_structured_block (parser);
34975 if (simd)
34976 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34977 cclauses, if_p);
34978 else
34979 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34980 cclauses, if_p);
34981 cp_parser_end_omp_structured_block (parser, save);
34982 tree body = finish_omp_structured_block (sb);
34983 if (ret == NULL)
34984 return ret;
34985 ret = make_node (OMP_DISTRIBUTE);
34986 TREE_TYPE (ret) = void_type_node;
34987 OMP_FOR_BODY (ret) = body;
34988 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34989 SET_EXPR_LOCATION (ret, loc);
34990 add_stmt (ret);
34991 return ret;
34994 if (!flag_openmp) /* flag_openmp_simd */
34996 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34997 return NULL_TREE;
35000 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35001 cclauses == NULL);
35002 if (cclauses)
35004 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35005 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35008 sb = begin_omp_structured_block ();
35009 save = cp_parser_begin_omp_structured_block (parser);
35011 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35013 cp_parser_end_omp_structured_block (parser, save);
35014 add_stmt (finish_omp_structured_block (sb));
35016 return ret;
35019 /* OpenMP 4.0:
35020 # pragma omp teams teams-clause[optseq] new-line
35021 structured-block */
35023 #define OMP_TEAMS_CLAUSE_MASK \
35024 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35032 static tree
35033 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35034 char *p_name, omp_clause_mask mask, tree *cclauses,
35035 bool *if_p)
35037 tree clauses, sb, ret;
35038 unsigned int save;
35039 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35041 strcat (p_name, " teams");
35042 mask |= OMP_TEAMS_CLAUSE_MASK;
35044 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35046 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35047 const char *p = IDENTIFIER_POINTER (id);
35048 if (strcmp (p, "distribute") == 0)
35050 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35051 if (cclauses == NULL)
35052 cclauses = cclauses_buf;
35054 cp_lexer_consume_token (parser->lexer);
35055 if (!flag_openmp) /* flag_openmp_simd */
35056 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35057 cclauses, if_p);
35058 sb = begin_omp_structured_block ();
35059 save = cp_parser_begin_omp_structured_block (parser);
35060 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35061 cclauses, if_p);
35062 cp_parser_end_omp_structured_block (parser, save);
35063 tree body = finish_omp_structured_block (sb);
35064 if (ret == NULL)
35065 return ret;
35066 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35067 ret = make_node (OMP_TEAMS);
35068 TREE_TYPE (ret) = void_type_node;
35069 OMP_TEAMS_CLAUSES (ret) = clauses;
35070 OMP_TEAMS_BODY (ret) = body;
35071 OMP_TEAMS_COMBINED (ret) = 1;
35072 return add_stmt (ret);
35075 if (!flag_openmp) /* flag_openmp_simd */
35077 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35078 return NULL_TREE;
35081 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35082 cclauses == NULL);
35083 if (cclauses)
35085 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35086 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35089 tree stmt = make_node (OMP_TEAMS);
35090 TREE_TYPE (stmt) = void_type_node;
35091 OMP_TEAMS_CLAUSES (stmt) = clauses;
35092 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35094 return add_stmt (stmt);
35097 /* OpenMP 4.0:
35098 # pragma omp target data target-data-clause[optseq] new-line
35099 structured-block */
35101 #define OMP_TARGET_DATA_CLAUSE_MASK \
35102 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35107 static tree
35108 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35110 tree clauses
35111 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35112 "#pragma omp target data", pragma_tok);
35113 int map_seen = 0;
35114 for (tree *pc = &clauses; *pc;)
35116 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35117 switch (OMP_CLAUSE_MAP_KIND (*pc))
35119 case GOMP_MAP_TO:
35120 case GOMP_MAP_ALWAYS_TO:
35121 case GOMP_MAP_FROM:
35122 case GOMP_MAP_ALWAYS_FROM:
35123 case GOMP_MAP_TOFROM:
35124 case GOMP_MAP_ALWAYS_TOFROM:
35125 case GOMP_MAP_ALLOC:
35126 map_seen = 3;
35127 break;
35128 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35129 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35130 case GOMP_MAP_ALWAYS_POINTER:
35131 break;
35132 default:
35133 map_seen |= 1;
35134 error_at (OMP_CLAUSE_LOCATION (*pc),
35135 "%<#pragma omp target data%> with map-type other "
35136 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35137 "on %<map%> clause");
35138 *pc = OMP_CLAUSE_CHAIN (*pc);
35139 continue;
35141 pc = &OMP_CLAUSE_CHAIN (*pc);
35144 if (map_seen != 3)
35146 if (map_seen == 0)
35147 error_at (pragma_tok->location,
35148 "%<#pragma omp target data%> must contain at least "
35149 "one %<map%> clause");
35150 return NULL_TREE;
35153 tree stmt = make_node (OMP_TARGET_DATA);
35154 TREE_TYPE (stmt) = void_type_node;
35155 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35157 keep_next_level (true);
35158 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35160 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35161 return add_stmt (stmt);
35164 /* OpenMP 4.5:
35165 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35166 structured-block */
35168 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35169 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35175 static tree
35176 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35177 enum pragma_context context)
35179 bool data_seen = false;
35180 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35182 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35183 const char *p = IDENTIFIER_POINTER (id);
35185 if (strcmp (p, "data") == 0)
35187 cp_lexer_consume_token (parser->lexer);
35188 data_seen = true;
35191 if (!data_seen)
35193 cp_parser_error (parser, "expected %<data%>");
35194 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35195 return NULL_TREE;
35198 if (context == pragma_stmt)
35200 error_at (pragma_tok->location,
35201 "%<#pragma omp target enter data%> may only be "
35202 "used in compound statements");
35203 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35204 return NULL_TREE;
35207 tree clauses
35208 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35209 "#pragma omp target enter data", pragma_tok);
35210 int map_seen = 0;
35211 for (tree *pc = &clauses; *pc;)
35213 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35214 switch (OMP_CLAUSE_MAP_KIND (*pc))
35216 case GOMP_MAP_TO:
35217 case GOMP_MAP_ALWAYS_TO:
35218 case GOMP_MAP_ALLOC:
35219 map_seen = 3;
35220 break;
35221 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35222 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35223 case GOMP_MAP_ALWAYS_POINTER:
35224 break;
35225 default:
35226 map_seen |= 1;
35227 error_at (OMP_CLAUSE_LOCATION (*pc),
35228 "%<#pragma omp target enter data%> with map-type other "
35229 "than %<to%> or %<alloc%> on %<map%> clause");
35230 *pc = OMP_CLAUSE_CHAIN (*pc);
35231 continue;
35233 pc = &OMP_CLAUSE_CHAIN (*pc);
35236 if (map_seen != 3)
35238 if (map_seen == 0)
35239 error_at (pragma_tok->location,
35240 "%<#pragma omp target enter data%> must contain at least "
35241 "one %<map%> clause");
35242 return NULL_TREE;
35245 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35246 TREE_TYPE (stmt) = void_type_node;
35247 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35248 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35249 return add_stmt (stmt);
35252 /* OpenMP 4.5:
35253 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35254 structured-block */
35256 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35257 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35263 static tree
35264 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35265 enum pragma_context context)
35267 bool data_seen = false;
35268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35270 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35271 const char *p = IDENTIFIER_POINTER (id);
35273 if (strcmp (p, "data") == 0)
35275 cp_lexer_consume_token (parser->lexer);
35276 data_seen = true;
35279 if (!data_seen)
35281 cp_parser_error (parser, "expected %<data%>");
35282 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35283 return NULL_TREE;
35286 if (context == pragma_stmt)
35288 error_at (pragma_tok->location,
35289 "%<#pragma omp target exit data%> may only be "
35290 "used in compound statements");
35291 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35292 return NULL_TREE;
35295 tree clauses
35296 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35297 "#pragma omp target exit data", pragma_tok);
35298 int map_seen = 0;
35299 for (tree *pc = &clauses; *pc;)
35301 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35302 switch (OMP_CLAUSE_MAP_KIND (*pc))
35304 case GOMP_MAP_FROM:
35305 case GOMP_MAP_ALWAYS_FROM:
35306 case GOMP_MAP_RELEASE:
35307 case GOMP_MAP_DELETE:
35308 map_seen = 3;
35309 break;
35310 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35311 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35312 case GOMP_MAP_ALWAYS_POINTER:
35313 break;
35314 default:
35315 map_seen |= 1;
35316 error_at (OMP_CLAUSE_LOCATION (*pc),
35317 "%<#pragma omp target exit data%> with map-type other "
35318 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35319 " clause");
35320 *pc = OMP_CLAUSE_CHAIN (*pc);
35321 continue;
35323 pc = &OMP_CLAUSE_CHAIN (*pc);
35326 if (map_seen != 3)
35328 if (map_seen == 0)
35329 error_at (pragma_tok->location,
35330 "%<#pragma omp target exit data%> must contain at least "
35331 "one %<map%> clause");
35332 return NULL_TREE;
35335 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35336 TREE_TYPE (stmt) = void_type_node;
35337 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35338 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35339 return add_stmt (stmt);
35342 /* OpenMP 4.0:
35343 # pragma omp target update target-update-clause[optseq] new-line */
35345 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35346 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35353 static bool
35354 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35355 enum pragma_context context)
35357 if (context == pragma_stmt)
35359 error_at (pragma_tok->location,
35360 "%<#pragma omp target update%> may only be "
35361 "used in compound statements");
35362 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35363 return false;
35366 tree clauses
35367 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35368 "#pragma omp target update", pragma_tok);
35369 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35370 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35372 error_at (pragma_tok->location,
35373 "%<#pragma omp target update%> must contain at least one "
35374 "%<from%> or %<to%> clauses");
35375 return false;
35378 tree stmt = make_node (OMP_TARGET_UPDATE);
35379 TREE_TYPE (stmt) = void_type_node;
35380 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35381 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35382 add_stmt (stmt);
35383 return false;
35386 /* OpenMP 4.0:
35387 # pragma omp target target-clause[optseq] new-line
35388 structured-block */
35390 #define OMP_TARGET_CLAUSE_MASK \
35391 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35401 static bool
35402 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35403 enum pragma_context context, bool *if_p)
35405 tree *pc = NULL, stmt;
35407 if (context != pragma_stmt && context != pragma_compound)
35409 cp_parser_error (parser, "expected declaration specifiers");
35410 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35411 return false;
35414 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35416 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35417 const char *p = IDENTIFIER_POINTER (id);
35418 enum tree_code ccode = ERROR_MARK;
35420 if (strcmp (p, "teams") == 0)
35421 ccode = OMP_TEAMS;
35422 else if (strcmp (p, "parallel") == 0)
35423 ccode = OMP_PARALLEL;
35424 else if (strcmp (p, "simd") == 0)
35425 ccode = OMP_SIMD;
35426 if (ccode != ERROR_MARK)
35428 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35429 char p_name[sizeof ("#pragma omp target teams distribute "
35430 "parallel for simd")];
35432 cp_lexer_consume_token (parser->lexer);
35433 strcpy (p_name, "#pragma omp target");
35434 if (!flag_openmp) /* flag_openmp_simd */
35436 tree stmt;
35437 switch (ccode)
35439 case OMP_TEAMS:
35440 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35441 OMP_TARGET_CLAUSE_MASK,
35442 cclauses, if_p);
35443 break;
35444 case OMP_PARALLEL:
35445 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35446 OMP_TARGET_CLAUSE_MASK,
35447 cclauses, if_p);
35448 break;
35449 case OMP_SIMD:
35450 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35451 OMP_TARGET_CLAUSE_MASK,
35452 cclauses, if_p);
35453 break;
35454 default:
35455 gcc_unreachable ();
35457 return stmt != NULL_TREE;
35459 keep_next_level (true);
35460 tree sb = begin_omp_structured_block (), ret;
35461 unsigned save = cp_parser_begin_omp_structured_block (parser);
35462 switch (ccode)
35464 case OMP_TEAMS:
35465 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35466 OMP_TARGET_CLAUSE_MASK, cclauses,
35467 if_p);
35468 break;
35469 case OMP_PARALLEL:
35470 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35471 OMP_TARGET_CLAUSE_MASK, cclauses,
35472 if_p);
35473 break;
35474 case OMP_SIMD:
35475 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35476 OMP_TARGET_CLAUSE_MASK, cclauses,
35477 if_p);
35478 break;
35479 default:
35480 gcc_unreachable ();
35482 cp_parser_end_omp_structured_block (parser, save);
35483 tree body = finish_omp_structured_block (sb);
35484 if (ret == NULL_TREE)
35485 return false;
35486 if (ccode == OMP_TEAMS && !processing_template_decl)
35488 /* For combined target teams, ensure the num_teams and
35489 thread_limit clause expressions are evaluated on the host,
35490 before entering the target construct. */
35491 tree c;
35492 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35493 c; c = OMP_CLAUSE_CHAIN (c))
35494 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35495 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35496 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35498 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35499 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35500 if (expr == error_mark_node)
35501 continue;
35502 tree tmp = TARGET_EXPR_SLOT (expr);
35503 add_stmt (expr);
35504 OMP_CLAUSE_OPERAND (c, 0) = expr;
35505 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35506 OMP_CLAUSE_FIRSTPRIVATE);
35507 OMP_CLAUSE_DECL (tc) = tmp;
35508 OMP_CLAUSE_CHAIN (tc)
35509 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35510 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35513 tree stmt = make_node (OMP_TARGET);
35514 TREE_TYPE (stmt) = void_type_node;
35515 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35516 OMP_TARGET_BODY (stmt) = body;
35517 OMP_TARGET_COMBINED (stmt) = 1;
35518 add_stmt (stmt);
35519 pc = &OMP_TARGET_CLAUSES (stmt);
35520 goto check_clauses;
35522 else if (!flag_openmp) /* flag_openmp_simd */
35524 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35525 return false;
35527 else if (strcmp (p, "data") == 0)
35529 cp_lexer_consume_token (parser->lexer);
35530 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35531 return true;
35533 else if (strcmp (p, "enter") == 0)
35535 cp_lexer_consume_token (parser->lexer);
35536 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35537 return false;
35539 else if (strcmp (p, "exit") == 0)
35541 cp_lexer_consume_token (parser->lexer);
35542 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35543 return false;
35545 else if (strcmp (p, "update") == 0)
35547 cp_lexer_consume_token (parser->lexer);
35548 return cp_parser_omp_target_update (parser, pragma_tok, context);
35552 stmt = make_node (OMP_TARGET);
35553 TREE_TYPE (stmt) = void_type_node;
35555 OMP_TARGET_CLAUSES (stmt)
35556 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35557 "#pragma omp target", pragma_tok);
35558 pc = &OMP_TARGET_CLAUSES (stmt);
35559 keep_next_level (true);
35560 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35562 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35563 add_stmt (stmt);
35565 check_clauses:
35566 while (*pc)
35568 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35569 switch (OMP_CLAUSE_MAP_KIND (*pc))
35571 case GOMP_MAP_TO:
35572 case GOMP_MAP_ALWAYS_TO:
35573 case GOMP_MAP_FROM:
35574 case GOMP_MAP_ALWAYS_FROM:
35575 case GOMP_MAP_TOFROM:
35576 case GOMP_MAP_ALWAYS_TOFROM:
35577 case GOMP_MAP_ALLOC:
35578 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35579 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35580 case GOMP_MAP_ALWAYS_POINTER:
35581 break;
35582 default:
35583 error_at (OMP_CLAUSE_LOCATION (*pc),
35584 "%<#pragma omp target%> with map-type other "
35585 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35586 "on %<map%> clause");
35587 *pc = OMP_CLAUSE_CHAIN (*pc);
35588 continue;
35590 pc = &OMP_CLAUSE_CHAIN (*pc);
35592 return true;
35595 /* OpenACC 2.0:
35596 # pragma acc cache (variable-list) new-line
35599 static tree
35600 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35602 tree stmt, clauses;
35604 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35605 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35607 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35609 stmt = make_node (OACC_CACHE);
35610 TREE_TYPE (stmt) = void_type_node;
35611 OACC_CACHE_CLAUSES (stmt) = clauses;
35612 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35613 add_stmt (stmt);
35615 return stmt;
35618 /* OpenACC 2.0:
35619 # pragma acc data oacc-data-clause[optseq] new-line
35620 structured-block */
35622 #define OACC_DATA_CLAUSE_MASK \
35623 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35635 static tree
35636 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35638 tree stmt, clauses, block;
35639 unsigned int save;
35641 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35642 "#pragma acc data", pragma_tok);
35644 block = begin_omp_parallel ();
35645 save = cp_parser_begin_omp_structured_block (parser);
35646 cp_parser_statement (parser, NULL_TREE, false, if_p);
35647 cp_parser_end_omp_structured_block (parser, save);
35648 stmt = finish_oacc_data (clauses, block);
35649 return stmt;
35652 /* OpenACC 2.0:
35653 # pragma acc host_data <clauses> new-line
35654 structured-block */
35656 #define OACC_HOST_DATA_CLAUSE_MASK \
35657 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35659 static tree
35660 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35662 tree stmt, clauses, block;
35663 unsigned int save;
35665 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35666 "#pragma acc host_data", pragma_tok);
35668 block = begin_omp_parallel ();
35669 save = cp_parser_begin_omp_structured_block (parser);
35670 cp_parser_statement (parser, NULL_TREE, false, if_p);
35671 cp_parser_end_omp_structured_block (parser, save);
35672 stmt = finish_oacc_host_data (clauses, block);
35673 return stmt;
35676 /* OpenACC 2.0:
35677 # pragma acc declare oacc-data-clause[optseq] new-line
35680 #define OACC_DECLARE_CLAUSE_MASK \
35681 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35694 static tree
35695 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35697 tree clauses, stmt;
35698 bool error = false;
35700 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35701 "#pragma acc declare", pragma_tok, true);
35704 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35706 error_at (pragma_tok->location,
35707 "no valid clauses specified in %<#pragma acc declare%>");
35708 return NULL_TREE;
35711 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35713 location_t loc = OMP_CLAUSE_LOCATION (t);
35714 tree decl = OMP_CLAUSE_DECL (t);
35715 if (!DECL_P (decl))
35717 error_at (loc, "array section in %<#pragma acc declare%>");
35718 error = true;
35719 continue;
35721 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35722 switch (OMP_CLAUSE_MAP_KIND (t))
35724 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35725 case GOMP_MAP_FORCE_ALLOC:
35726 case GOMP_MAP_FORCE_TO:
35727 case GOMP_MAP_FORCE_DEVICEPTR:
35728 case GOMP_MAP_DEVICE_RESIDENT:
35729 break;
35731 case GOMP_MAP_LINK:
35732 if (!global_bindings_p ()
35733 && (TREE_STATIC (decl)
35734 || !DECL_EXTERNAL (decl)))
35736 error_at (loc,
35737 "%qD must be a global variable in"
35738 "%<#pragma acc declare link%>",
35739 decl);
35740 error = true;
35741 continue;
35743 break;
35745 default:
35746 if (global_bindings_p ())
35748 error_at (loc, "invalid OpenACC clause at file scope");
35749 error = true;
35750 continue;
35752 if (DECL_EXTERNAL (decl))
35754 error_at (loc,
35755 "invalid use of %<extern%> variable %qD "
35756 "in %<#pragma acc declare%>", decl);
35757 error = true;
35758 continue;
35760 else if (TREE_PUBLIC (decl))
35762 error_at (loc,
35763 "invalid use of %<global%> variable %qD "
35764 "in %<#pragma acc declare%>", decl);
35765 error = true;
35766 continue;
35768 break;
35771 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35772 || lookup_attribute ("omp declare target link",
35773 DECL_ATTRIBUTES (decl)))
35775 error_at (loc, "variable %qD used more than once with "
35776 "%<#pragma acc declare%>", decl);
35777 error = true;
35778 continue;
35781 if (!error)
35783 tree id;
35785 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35786 id = get_identifier ("omp declare target link");
35787 else
35788 id = get_identifier ("omp declare target");
35790 DECL_ATTRIBUTES (decl)
35791 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35792 if (global_bindings_p ())
35794 symtab_node *node = symtab_node::get (decl);
35795 if (node != NULL)
35797 node->offloadable = 1;
35798 if (ENABLE_OFFLOADING)
35800 g->have_offload = true;
35801 if (is_a <varpool_node *> (node))
35802 vec_safe_push (offload_vars, decl);
35809 if (error || global_bindings_p ())
35810 return NULL_TREE;
35812 stmt = make_node (OACC_DECLARE);
35813 TREE_TYPE (stmt) = void_type_node;
35814 OACC_DECLARE_CLAUSES (stmt) = clauses;
35815 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35817 add_stmt (stmt);
35819 return NULL_TREE;
35822 /* OpenACC 2.0:
35823 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35827 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35829 LOC is the location of the #pragma token.
35832 #define OACC_ENTER_DATA_CLAUSE_MASK \
35833 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35841 #define OACC_EXIT_DATA_CLAUSE_MASK \
35842 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35848 static tree
35849 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35850 bool enter)
35852 tree stmt, clauses;
35854 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35855 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35857 cp_parser_error (parser, enter
35858 ? "expected %<data%> in %<#pragma acc enter data%>"
35859 : "expected %<data%> in %<#pragma acc exit data%>");
35860 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35861 return NULL_TREE;
35864 const char *p =
35865 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35866 if (strcmp (p, "data") != 0)
35868 cp_parser_error (parser, "invalid pragma");
35869 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35870 return NULL_TREE;
35873 cp_lexer_consume_token (parser->lexer);
35875 if (enter)
35876 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35877 "#pragma acc enter data", pragma_tok);
35878 else
35879 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35880 "#pragma acc exit data", pragma_tok);
35882 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35884 error_at (pragma_tok->location,
35885 "%<#pragma acc enter data%> has no data movement clause");
35886 return NULL_TREE;
35889 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35890 TREE_TYPE (stmt) = void_type_node;
35891 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35892 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35893 add_stmt (stmt);
35894 return stmt;
35897 /* OpenACC 2.0:
35898 # pragma acc loop oacc-loop-clause[optseq] new-line
35899 structured-block */
35901 #define OACC_LOOP_CLAUSE_MASK \
35902 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35913 static tree
35914 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35915 omp_clause_mask mask, tree *cclauses, bool *if_p)
35917 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35919 strcat (p_name, " loop");
35920 mask |= OACC_LOOP_CLAUSE_MASK;
35922 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35923 cclauses == NULL);
35924 if (cclauses)
35926 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35927 if (*cclauses)
35928 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
35929 if (clauses)
35930 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35933 tree block = begin_omp_structured_block ();
35934 int save = cp_parser_begin_omp_structured_block (parser);
35935 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35936 cp_parser_end_omp_structured_block (parser, save);
35937 add_stmt (finish_omp_structured_block (block));
35939 return stmt;
35942 /* OpenACC 2.0:
35943 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35944 structured-block
35948 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35949 structured-block
35952 #define OACC_KERNELS_CLAUSE_MASK \
35953 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35968 #define OACC_PARALLEL_CLAUSE_MASK \
35969 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35990 static tree
35991 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35992 char *p_name, bool *if_p)
35994 omp_clause_mask mask;
35995 enum tree_code code;
35996 switch (cp_parser_pragma_kind (pragma_tok))
35998 case PRAGMA_OACC_KERNELS:
35999 strcat (p_name, " kernels");
36000 mask = OACC_KERNELS_CLAUSE_MASK;
36001 code = OACC_KERNELS;
36002 break;
36003 case PRAGMA_OACC_PARALLEL:
36004 strcat (p_name, " parallel");
36005 mask = OACC_PARALLEL_CLAUSE_MASK;
36006 code = OACC_PARALLEL;
36007 break;
36008 default:
36009 gcc_unreachable ();
36012 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36014 const char *p
36015 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36016 if (strcmp (p, "loop") == 0)
36018 cp_lexer_consume_token (parser->lexer);
36019 tree block = begin_omp_parallel ();
36020 tree clauses;
36021 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36022 if_p);
36023 return finish_omp_construct (code, block, clauses);
36027 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36029 tree block = begin_omp_parallel ();
36030 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36031 cp_parser_statement (parser, NULL_TREE, false, if_p);
36032 cp_parser_end_omp_structured_block (parser, save);
36033 return finish_omp_construct (code, block, clauses);
36036 /* OpenACC 2.0:
36037 # pragma acc update oacc-update-clause[optseq] new-line
36040 #define OACC_UPDATE_CLAUSE_MASK \
36041 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36048 static tree
36049 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36051 tree stmt, clauses;
36053 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36054 "#pragma acc update", pragma_tok);
36056 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36058 error_at (pragma_tok->location,
36059 "%<#pragma acc update%> must contain at least one "
36060 "%<device%> or %<host%> or %<self%> clause");
36061 return NULL_TREE;
36064 stmt = make_node (OACC_UPDATE);
36065 TREE_TYPE (stmt) = void_type_node;
36066 OACC_UPDATE_CLAUSES (stmt) = clauses;
36067 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36068 add_stmt (stmt);
36069 return stmt;
36072 /* OpenACC 2.0:
36073 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36075 LOC is the location of the #pragma token.
36078 #define OACC_WAIT_CLAUSE_MASK \
36079 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36081 static tree
36082 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36084 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36085 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36087 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36088 list = cp_parser_oacc_wait_list (parser, loc, list);
36090 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36091 "#pragma acc wait", pragma_tok);
36093 stmt = c_finish_oacc_wait (loc, list, clauses);
36094 stmt = finish_expr_stmt (stmt);
36096 return stmt;
36099 /* OpenMP 4.0:
36100 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36102 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36103 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36110 static void
36111 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36112 enum pragma_context context)
36114 bool first_p = parser->omp_declare_simd == NULL;
36115 cp_omp_declare_simd_data data;
36116 if (first_p)
36118 data.error_seen = false;
36119 data.fndecl_seen = false;
36120 data.tokens = vNULL;
36121 data.clauses = NULL_TREE;
36122 /* It is safe to take the address of a local variable; it will only be
36123 used while this scope is live. */
36124 parser->omp_declare_simd = &data;
36127 /* Store away all pragma tokens. */
36128 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36129 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36130 cp_lexer_consume_token (parser->lexer);
36131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36132 parser->omp_declare_simd->error_seen = true;
36133 cp_parser_require_pragma_eol (parser, pragma_tok);
36134 struct cp_token_cache *cp
36135 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36136 parser->omp_declare_simd->tokens.safe_push (cp);
36138 if (first_p)
36140 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36141 cp_parser_pragma (parser, context, NULL);
36142 switch (context)
36144 case pragma_external:
36145 cp_parser_declaration (parser);
36146 break;
36147 case pragma_member:
36148 cp_parser_member_declaration (parser);
36149 break;
36150 case pragma_objc_icode:
36151 cp_parser_block_declaration (parser, /*statement_p=*/false);
36152 break;
36153 default:
36154 cp_parser_declaration_statement (parser);
36155 break;
36157 if (parser->omp_declare_simd
36158 && !parser->omp_declare_simd->error_seen
36159 && !parser->omp_declare_simd->fndecl_seen)
36160 error_at (pragma_tok->location,
36161 "%<#pragma omp declare simd%> not immediately followed by "
36162 "function declaration or definition");
36163 data.tokens.release ();
36164 parser->omp_declare_simd = NULL;
36168 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36169 This function is modelled similar to the late parsing of omp declare
36170 simd. */
36172 static tree
36173 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36175 struct cp_token_cache *ce;
36176 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36177 int ii = 0;
36179 if (parser->omp_declare_simd != NULL
36180 || lookup_attribute ("simd", attrs))
36182 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36183 "used in the same function marked as a Cilk Plus SIMD-enabled "
36184 "function");
36185 parser->cilk_simd_fn_info->tokens.release ();
36186 XDELETE (parser->cilk_simd_fn_info);
36187 parser->cilk_simd_fn_info = NULL;
36188 return attrs;
36190 if (!info->error_seen && info->fndecl_seen)
36192 error ("vector attribute not immediately followed by a single function"
36193 " declaration or definition");
36194 info->error_seen = true;
36196 if (info->error_seen)
36197 return attrs;
36199 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36201 tree c, cl;
36203 cp_parser_push_lexer_for_tokens (parser, ce);
36204 parser->lexer->in_pragma = true;
36205 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36206 "SIMD-enabled functions attribute",
36207 NULL);
36208 cp_parser_pop_lexer (parser);
36209 if (cl)
36210 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36212 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36213 TREE_CHAIN (c) = attrs;
36214 attrs = c;
36216 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36217 TREE_CHAIN (c) = attrs;
36218 if (processing_template_decl)
36219 ATTR_IS_DEPENDENT (c) = 1;
36220 attrs = c;
36222 info->fndecl_seen = true;
36223 parser->cilk_simd_fn_info->tokens.release ();
36224 XDELETE (parser->cilk_simd_fn_info);
36225 parser->cilk_simd_fn_info = NULL;
36226 return attrs;
36229 /* Finalize #pragma omp declare simd clauses after direct declarator has
36230 been parsed, and put that into "omp declare simd" attribute. */
36232 static tree
36233 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36235 struct cp_token_cache *ce;
36236 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36237 int i;
36239 if (!data->error_seen && data->fndecl_seen)
36241 error ("%<#pragma omp declare simd%> not immediately followed by "
36242 "a single function declaration or definition");
36243 data->error_seen = true;
36245 if (data->error_seen)
36246 return attrs;
36248 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36250 tree c, cl;
36252 cp_parser_push_lexer_for_tokens (parser, ce);
36253 parser->lexer->in_pragma = true;
36254 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36255 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36256 cp_lexer_consume_token (parser->lexer);
36257 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36258 "#pragma omp declare simd", pragma_tok);
36259 cp_parser_pop_lexer (parser);
36260 if (cl)
36261 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36262 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36263 TREE_CHAIN (c) = attrs;
36264 if (processing_template_decl)
36265 ATTR_IS_DEPENDENT (c) = 1;
36266 attrs = c;
36269 data->fndecl_seen = true;
36270 return attrs;
36274 /* OpenMP 4.0:
36275 # pragma omp declare target new-line
36276 declarations and definitions
36277 # pragma omp end declare target new-line
36279 OpenMP 4.5:
36280 # pragma omp declare target ( extended-list ) new-line
36282 # pragma omp declare target declare-target-clauses[seq] new-line */
36284 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36288 static void
36289 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36291 tree clauses = NULL_TREE;
36292 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36293 clauses
36294 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36295 "#pragma omp declare target", pragma_tok);
36296 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36298 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36299 clauses);
36300 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36301 cp_parser_require_pragma_eol (parser, pragma_tok);
36303 else
36305 cp_parser_require_pragma_eol (parser, pragma_tok);
36306 scope_chain->omp_declare_target_attribute++;
36307 return;
36309 if (scope_chain->omp_declare_target_attribute)
36310 error_at (pragma_tok->location,
36311 "%<#pragma omp declare target%> with clauses in between "
36312 "%<#pragma omp declare target%> without clauses and "
36313 "%<#pragma omp end declare target%>");
36314 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36316 tree t = OMP_CLAUSE_DECL (c), id;
36317 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36318 tree at2 = lookup_attribute ("omp declare target link",
36319 DECL_ATTRIBUTES (t));
36320 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36322 id = get_identifier ("omp declare target link");
36323 std::swap (at1, at2);
36325 else
36326 id = get_identifier ("omp declare target");
36327 if (at2)
36329 error_at (OMP_CLAUSE_LOCATION (c),
36330 "%qD specified both in declare target %<link%> and %<to%>"
36331 " clauses", t);
36332 continue;
36334 if (!at1)
36336 symtab_node *node = symtab_node::get (t);
36337 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36338 if (node != NULL)
36340 node->offloadable = 1;
36341 if (ENABLE_OFFLOADING)
36343 g->have_offload = true;
36344 if (is_a <varpool_node *> (node))
36345 vec_safe_push (offload_vars, t);
36352 static void
36353 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36355 const char *p = "";
36356 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36358 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36359 p = IDENTIFIER_POINTER (id);
36361 if (strcmp (p, "declare") == 0)
36363 cp_lexer_consume_token (parser->lexer);
36364 p = "";
36365 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36367 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36368 p = IDENTIFIER_POINTER (id);
36370 if (strcmp (p, "target") == 0)
36371 cp_lexer_consume_token (parser->lexer);
36372 else
36374 cp_parser_error (parser, "expected %<target%>");
36375 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36376 return;
36379 else
36381 cp_parser_error (parser, "expected %<declare%>");
36382 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36383 return;
36385 cp_parser_require_pragma_eol (parser, pragma_tok);
36386 if (!scope_chain->omp_declare_target_attribute)
36387 error_at (pragma_tok->location,
36388 "%<#pragma omp end declare target%> without corresponding "
36389 "%<#pragma omp declare target%>");
36390 else
36391 scope_chain->omp_declare_target_attribute--;
36394 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36395 expression and optional initializer clause of
36396 #pragma omp declare reduction. We store the expression(s) as
36397 either 3, 6 or 7 special statements inside of the artificial function's
36398 body. The first two statements are DECL_EXPRs for the artificial
36399 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36400 expression that uses those variables.
36401 If there was any INITIALIZER clause, this is followed by further statements,
36402 the fourth and fifth statements are DECL_EXPRs for the artificial
36403 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36404 constructor variant (first token after open paren is not omp_priv),
36405 then the sixth statement is a statement with the function call expression
36406 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36407 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36408 to initialize the OMP_PRIV artificial variable and there is seventh
36409 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36411 static bool
36412 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36414 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36415 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36416 type = TREE_TYPE (type);
36417 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36418 DECL_ARTIFICIAL (omp_out) = 1;
36419 pushdecl (omp_out);
36420 add_decl_expr (omp_out);
36421 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36422 DECL_ARTIFICIAL (omp_in) = 1;
36423 pushdecl (omp_in);
36424 add_decl_expr (omp_in);
36425 tree combiner;
36426 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36428 keep_next_level (true);
36429 tree block = begin_omp_structured_block ();
36430 combiner = cp_parser_expression (parser);
36431 finish_expr_stmt (combiner);
36432 block = finish_omp_structured_block (block);
36433 add_stmt (block);
36435 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36436 return false;
36438 const char *p = "";
36439 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36441 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36442 p = IDENTIFIER_POINTER (id);
36445 if (strcmp (p, "initializer") == 0)
36447 cp_lexer_consume_token (parser->lexer);
36448 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36449 return false;
36451 p = "";
36452 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36454 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36455 p = IDENTIFIER_POINTER (id);
36458 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36459 DECL_ARTIFICIAL (omp_priv) = 1;
36460 pushdecl (omp_priv);
36461 add_decl_expr (omp_priv);
36462 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36463 DECL_ARTIFICIAL (omp_orig) = 1;
36464 pushdecl (omp_orig);
36465 add_decl_expr (omp_orig);
36467 keep_next_level (true);
36468 block = begin_omp_structured_block ();
36470 bool ctor = false;
36471 if (strcmp (p, "omp_priv") == 0)
36473 bool is_direct_init, is_non_constant_init;
36474 ctor = true;
36475 cp_lexer_consume_token (parser->lexer);
36476 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36477 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36478 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36479 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36480 == CPP_CLOSE_PAREN
36481 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36482 == CPP_CLOSE_PAREN))
36484 finish_omp_structured_block (block);
36485 error ("invalid initializer clause");
36486 return false;
36488 initializer = cp_parser_initializer (parser, &is_direct_init,
36489 &is_non_constant_init);
36490 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36491 NULL_TREE, LOOKUP_ONLYCONVERTING);
36493 else
36495 cp_parser_parse_tentatively (parser);
36496 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36497 /*check_dependency_p=*/true,
36498 /*template_p=*/NULL,
36499 /*declarator_p=*/false,
36500 /*optional_p=*/false);
36501 vec<tree, va_gc> *args;
36502 if (fn_name == error_mark_node
36503 || cp_parser_error_occurred (parser)
36504 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36505 || ((args = cp_parser_parenthesized_expression_list
36506 (parser, non_attr, /*cast_p=*/false,
36507 /*allow_expansion_p=*/true,
36508 /*non_constant_p=*/NULL)),
36509 cp_parser_error_occurred (parser)))
36511 finish_omp_structured_block (block);
36512 cp_parser_abort_tentative_parse (parser);
36513 cp_parser_error (parser, "expected id-expression (arguments)");
36514 return false;
36516 unsigned int i;
36517 tree arg;
36518 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36519 if (arg == omp_priv
36520 || (TREE_CODE (arg) == ADDR_EXPR
36521 && TREE_OPERAND (arg, 0) == omp_priv))
36522 break;
36523 cp_parser_abort_tentative_parse (parser);
36524 if (arg == NULL_TREE)
36525 error ("one of the initializer call arguments should be %<omp_priv%>"
36526 " or %<&omp_priv%>");
36527 initializer = cp_parser_postfix_expression (parser, false, false, false,
36528 false, NULL);
36529 finish_expr_stmt (initializer);
36532 block = finish_omp_structured_block (block);
36533 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36534 add_stmt (block);
36536 if (ctor)
36537 add_decl_expr (omp_orig);
36539 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36540 return false;
36543 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36544 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36546 return true;
36549 /* OpenMP 4.0
36550 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36551 initializer-clause[opt] new-line
36553 initializer-clause:
36554 initializer (omp_priv initializer)
36555 initializer (function-name (argument-list)) */
36557 static void
36558 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36559 enum pragma_context)
36561 auto_vec<tree> types;
36562 enum tree_code reduc_code = ERROR_MARK;
36563 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36564 unsigned int i;
36565 cp_token *first_token;
36566 cp_token_cache *cp;
36567 int errs;
36568 void *p;
36570 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36571 p = obstack_alloc (&declarator_obstack, 0);
36573 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36574 goto fail;
36576 switch (cp_lexer_peek_token (parser->lexer)->type)
36578 case CPP_PLUS:
36579 reduc_code = PLUS_EXPR;
36580 break;
36581 case CPP_MULT:
36582 reduc_code = MULT_EXPR;
36583 break;
36584 case CPP_MINUS:
36585 reduc_code = MINUS_EXPR;
36586 break;
36587 case CPP_AND:
36588 reduc_code = BIT_AND_EXPR;
36589 break;
36590 case CPP_XOR:
36591 reduc_code = BIT_XOR_EXPR;
36592 break;
36593 case CPP_OR:
36594 reduc_code = BIT_IOR_EXPR;
36595 break;
36596 case CPP_AND_AND:
36597 reduc_code = TRUTH_ANDIF_EXPR;
36598 break;
36599 case CPP_OR_OR:
36600 reduc_code = TRUTH_ORIF_EXPR;
36601 break;
36602 case CPP_NAME:
36603 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36604 break;
36605 default:
36606 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36607 "%<|%>, %<&&%>, %<||%> or identifier");
36608 goto fail;
36611 if (reduc_code != ERROR_MARK)
36612 cp_lexer_consume_token (parser->lexer);
36614 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36615 if (reduc_id == error_mark_node)
36616 goto fail;
36618 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36619 goto fail;
36621 /* Types may not be defined in declare reduction type list. */
36622 const char *saved_message;
36623 saved_message = parser->type_definition_forbidden_message;
36624 parser->type_definition_forbidden_message
36625 = G_("types may not be defined in declare reduction type list");
36626 bool saved_colon_corrects_to_scope_p;
36627 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36628 parser->colon_corrects_to_scope_p = false;
36629 bool saved_colon_doesnt_start_class_def_p;
36630 saved_colon_doesnt_start_class_def_p
36631 = parser->colon_doesnt_start_class_def_p;
36632 parser->colon_doesnt_start_class_def_p = true;
36634 while (true)
36636 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36637 type = cp_parser_type_id (parser);
36638 if (type == error_mark_node)
36640 else if (ARITHMETIC_TYPE_P (type)
36641 && (orig_reduc_id == NULL_TREE
36642 || (TREE_CODE (type) != COMPLEX_TYPE
36643 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36644 "min") == 0
36645 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36646 "max") == 0))))
36647 error_at (loc, "predeclared arithmetic type %qT in "
36648 "%<#pragma omp declare reduction%>", type);
36649 else if (TREE_CODE (type) == FUNCTION_TYPE
36650 || TREE_CODE (type) == METHOD_TYPE
36651 || TREE_CODE (type) == ARRAY_TYPE)
36652 error_at (loc, "function or array type %qT in "
36653 "%<#pragma omp declare reduction%>", type);
36654 else if (TREE_CODE (type) == REFERENCE_TYPE)
36655 error_at (loc, "reference type %qT in "
36656 "%<#pragma omp declare reduction%>", type);
36657 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36658 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36659 "%<#pragma omp declare reduction%>", type);
36660 else
36661 types.safe_push (type);
36663 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36664 cp_lexer_consume_token (parser->lexer);
36665 else
36666 break;
36669 /* Restore the saved message. */
36670 parser->type_definition_forbidden_message = saved_message;
36671 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36672 parser->colon_doesnt_start_class_def_p
36673 = saved_colon_doesnt_start_class_def_p;
36675 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36676 || types.is_empty ())
36678 fail:
36679 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36680 goto done;
36683 first_token = cp_lexer_peek_token (parser->lexer);
36684 cp = NULL;
36685 errs = errorcount;
36686 FOR_EACH_VEC_ELT (types, i, type)
36688 tree fntype
36689 = build_function_type_list (void_type_node,
36690 cp_build_reference_type (type, false),
36691 NULL_TREE);
36692 tree this_reduc_id = reduc_id;
36693 if (!dependent_type_p (type))
36694 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36695 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36696 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36697 DECL_ARTIFICIAL (fndecl) = 1;
36698 DECL_EXTERNAL (fndecl) = 1;
36699 DECL_DECLARED_INLINE_P (fndecl) = 1;
36700 DECL_IGNORED_P (fndecl) = 1;
36701 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36702 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36703 DECL_ATTRIBUTES (fndecl)
36704 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36705 DECL_ATTRIBUTES (fndecl));
36706 if (processing_template_decl)
36707 fndecl = push_template_decl (fndecl);
36708 bool block_scope = false;
36709 tree block = NULL_TREE;
36710 if (current_function_decl)
36712 block_scope = true;
36713 DECL_CONTEXT (fndecl) = global_namespace;
36714 if (!processing_template_decl)
36715 pushdecl (fndecl);
36717 else if (current_class_type)
36719 if (cp == NULL)
36721 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36722 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36723 cp_lexer_consume_token (parser->lexer);
36724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36725 goto fail;
36726 cp = cp_token_cache_new (first_token,
36727 cp_lexer_peek_nth_token (parser->lexer,
36728 2));
36730 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36731 finish_member_declaration (fndecl);
36732 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36733 DECL_PENDING_INLINE_P (fndecl) = 1;
36734 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36735 continue;
36737 else
36739 DECL_CONTEXT (fndecl) = current_namespace;
36740 pushdecl (fndecl);
36742 if (!block_scope)
36743 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36744 else
36745 block = begin_omp_structured_block ();
36746 if (cp)
36748 cp_parser_push_lexer_for_tokens (parser, cp);
36749 parser->lexer->in_pragma = true;
36751 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36753 if (!block_scope)
36754 finish_function (0);
36755 else
36756 DECL_CONTEXT (fndecl) = current_function_decl;
36757 if (cp)
36758 cp_parser_pop_lexer (parser);
36759 goto fail;
36761 if (cp)
36762 cp_parser_pop_lexer (parser);
36763 if (!block_scope)
36764 finish_function (0);
36765 else
36767 DECL_CONTEXT (fndecl) = current_function_decl;
36768 block = finish_omp_structured_block (block);
36769 if (TREE_CODE (block) == BIND_EXPR)
36770 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36771 else if (TREE_CODE (block) == STATEMENT_LIST)
36772 DECL_SAVED_TREE (fndecl) = block;
36773 if (processing_template_decl)
36774 add_decl_expr (fndecl);
36776 cp_check_omp_declare_reduction (fndecl);
36777 if (cp == NULL && types.length () > 1)
36778 cp = cp_token_cache_new (first_token,
36779 cp_lexer_peek_nth_token (parser->lexer, 2));
36780 if (errs != errorcount)
36781 break;
36784 cp_parser_require_pragma_eol (parser, pragma_tok);
36786 done:
36787 /* Free any declarators allocated. */
36788 obstack_free (&declarator_obstack, p);
36791 /* OpenMP 4.0
36792 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36793 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36794 initializer-clause[opt] new-line
36795 #pragma omp declare target new-line */
36797 static void
36798 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36799 enum pragma_context context)
36801 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36803 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36804 const char *p = IDENTIFIER_POINTER (id);
36806 if (strcmp (p, "simd") == 0)
36808 cp_lexer_consume_token (parser->lexer);
36809 cp_parser_omp_declare_simd (parser, pragma_tok,
36810 context);
36811 return;
36813 cp_ensure_no_omp_declare_simd (parser);
36814 if (strcmp (p, "reduction") == 0)
36816 cp_lexer_consume_token (parser->lexer);
36817 cp_parser_omp_declare_reduction (parser, pragma_tok,
36818 context);
36819 return;
36821 if (!flag_openmp) /* flag_openmp_simd */
36823 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36824 return;
36826 if (strcmp (p, "target") == 0)
36828 cp_lexer_consume_token (parser->lexer);
36829 cp_parser_omp_declare_target (parser, pragma_tok);
36830 return;
36833 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36834 "or %<target%>");
36835 cp_parser_require_pragma_eol (parser, pragma_tok);
36838 /* OpenMP 4.5:
36839 #pragma omp taskloop taskloop-clause[optseq] new-line
36840 for-loop
36842 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36843 for-loop */
36845 #define OMP_TASKLOOP_CLAUSE_MASK \
36846 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36861 static tree
36862 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36863 char *p_name, omp_clause_mask mask, tree *cclauses,
36864 bool *if_p)
36866 tree clauses, sb, ret;
36867 unsigned int save;
36868 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36870 strcat (p_name, " taskloop");
36871 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36873 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36875 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36876 const char *p = IDENTIFIER_POINTER (id);
36878 if (strcmp (p, "simd") == 0)
36880 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36881 if (cclauses == NULL)
36882 cclauses = cclauses_buf;
36884 cp_lexer_consume_token (parser->lexer);
36885 if (!flag_openmp) /* flag_openmp_simd */
36886 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36887 cclauses, if_p);
36888 sb = begin_omp_structured_block ();
36889 save = cp_parser_begin_omp_structured_block (parser);
36890 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36891 cclauses, if_p);
36892 cp_parser_end_omp_structured_block (parser, save);
36893 tree body = finish_omp_structured_block (sb);
36894 if (ret == NULL)
36895 return ret;
36896 ret = make_node (OMP_TASKLOOP);
36897 TREE_TYPE (ret) = void_type_node;
36898 OMP_FOR_BODY (ret) = body;
36899 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36900 SET_EXPR_LOCATION (ret, loc);
36901 add_stmt (ret);
36902 return ret;
36905 if (!flag_openmp) /* flag_openmp_simd */
36907 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36908 return NULL_TREE;
36911 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36912 cclauses == NULL);
36913 if (cclauses)
36915 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36916 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36919 sb = begin_omp_structured_block ();
36920 save = cp_parser_begin_omp_structured_block (parser);
36922 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36923 if_p);
36925 cp_parser_end_omp_structured_block (parser, save);
36926 add_stmt (finish_omp_structured_block (sb));
36928 return ret;
36932 /* OpenACC 2.0:
36933 # pragma acc routine oacc-routine-clause[optseq] new-line
36934 function-definition
36936 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36939 #define OACC_ROUTINE_CLAUSE_MASK \
36940 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36946 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36947 component, which must resolve to a declared namespace-scope
36948 function. The clauses are either processed directly (for a named
36949 function), or defered until the immediatley following declaration
36950 is parsed. */
36952 static void
36953 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36954 enum pragma_context context)
36956 gcc_checking_assert (context == pragma_external);
36957 /* The checking for "another pragma following this one" in the "no optional
36958 '( name )'" case makes sure that we dont re-enter. */
36959 gcc_checking_assert (parser->oacc_routine == NULL);
36961 cp_oacc_routine_data data;
36962 data.error_seen = false;
36963 data.fndecl_seen = false;
36964 data.tokens = vNULL;
36965 data.clauses = NULL_TREE;
36966 data.loc = pragma_tok->location;
36967 /* It is safe to take the address of a local variable; it will only be
36968 used while this scope is live. */
36969 parser->oacc_routine = &data;
36971 /* Look for optional '( name )'. */
36972 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36974 cp_lexer_consume_token (parser->lexer); /* '(' */
36976 /* We parse the name as an id-expression. If it resolves to
36977 anything other than a non-overloaded function at namespace
36978 scope, it's an error. */
36979 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
36980 tree name = cp_parser_id_expression (parser,
36981 /*template_keyword_p=*/false,
36982 /*check_dependency_p=*/false,
36983 /*template_p=*/NULL,
36984 /*declarator_p=*/false,
36985 /*optional_p=*/false);
36986 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
36987 if (name != error_mark_node && decl == error_mark_node)
36988 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
36990 if (decl == error_mark_node
36991 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36993 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36994 parser->oacc_routine = NULL;
36995 return;
36998 data.clauses
36999 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37000 "#pragma acc routine",
37001 cp_lexer_peek_token (parser->lexer));
37003 if (decl && is_overloaded_fn (decl)
37004 && (TREE_CODE (decl) != FUNCTION_DECL
37005 || DECL_FUNCTION_TEMPLATE_P (decl)))
37007 error_at (name_loc,
37008 "%<#pragma acc routine%> names a set of overloads");
37009 parser->oacc_routine = NULL;
37010 return;
37013 /* Perhaps we should use the same rule as declarations in different
37014 namespaces? */
37015 if (!DECL_NAMESPACE_SCOPE_P (decl))
37017 error_at (name_loc,
37018 "%qD does not refer to a namespace scope function", decl);
37019 parser->oacc_routine = NULL;
37020 return;
37023 if (TREE_CODE (decl) != FUNCTION_DECL)
37025 error_at (name_loc, "%qD does not refer to a function", decl);
37026 parser->oacc_routine = NULL;
37027 return;
37030 cp_finalize_oacc_routine (parser, decl, false);
37031 parser->oacc_routine = NULL;
37033 else /* No optional '( name )'. */
37035 /* Store away all pragma tokens. */
37036 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37037 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37038 cp_lexer_consume_token (parser->lexer);
37039 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37040 parser->oacc_routine->error_seen = true;
37041 cp_parser_require_pragma_eol (parser, pragma_tok);
37042 struct cp_token_cache *cp
37043 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37044 parser->oacc_routine->tokens.safe_push (cp);
37046 /* Emit a helpful diagnostic if there's another pragma following this
37047 one. */
37048 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37050 cp_ensure_no_oacc_routine (parser);
37051 data.tokens.release ();
37052 /* ..., and then just keep going. */
37053 return;
37056 /* We only have to consider the pragma_external case here. */
37057 cp_parser_declaration (parser);
37058 if (parser->oacc_routine
37059 && !parser->oacc_routine->fndecl_seen)
37060 cp_ensure_no_oacc_routine (parser);
37061 else
37062 parser->oacc_routine = NULL;
37063 data.tokens.release ();
37067 /* Finalize #pragma acc routine clauses after direct declarator has
37068 been parsed. */
37070 static tree
37071 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37073 struct cp_token_cache *ce;
37074 cp_oacc_routine_data *data = parser->oacc_routine;
37076 if (!data->error_seen && data->fndecl_seen)
37078 error_at (data->loc,
37079 "%<#pragma acc routine%> not immediately followed by "
37080 "a single function declaration or definition");
37081 data->error_seen = true;
37083 if (data->error_seen)
37084 return attrs;
37086 gcc_checking_assert (data->tokens.length () == 1);
37087 ce = data->tokens[0];
37089 cp_parser_push_lexer_for_tokens (parser, ce);
37090 parser->lexer->in_pragma = true;
37091 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37093 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37094 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37095 parser->oacc_routine->clauses
37096 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37097 "#pragma acc routine", pragma_tok);
37098 cp_parser_pop_lexer (parser);
37099 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37100 fndecl_seen. */
37102 return attrs;
37105 /* Apply any saved OpenACC routine clauses to a just-parsed
37106 declaration. */
37108 static void
37109 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37111 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37113 /* Keep going if we're in error reporting mode. */
37114 if (parser->oacc_routine->error_seen
37115 || fndecl == error_mark_node)
37116 return;
37118 if (parser->oacc_routine->fndecl_seen)
37120 error_at (parser->oacc_routine->loc,
37121 "%<#pragma acc routine%> not immediately followed by"
37122 " a single function declaration or definition");
37123 parser->oacc_routine = NULL;
37124 return;
37126 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37128 cp_ensure_no_oacc_routine (parser);
37129 return;
37132 if (get_oacc_fn_attrib (fndecl))
37134 error_at (parser->oacc_routine->loc,
37135 "%<#pragma acc routine%> already applied to %qD", fndecl);
37136 parser->oacc_routine = NULL;
37137 return;
37140 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37142 error_at (parser->oacc_routine->loc,
37143 "%<#pragma acc routine%> must be applied before %s",
37144 TREE_USED (fndecl) ? "use" : "definition");
37145 parser->oacc_routine = NULL;
37146 return;
37149 /* Process the routine's dimension clauses. */
37150 tree dims = build_oacc_routine_dims (parser->oacc_routine->clauses);
37151 replace_oacc_fn_attrib (fndecl, dims);
37153 /* Add an "omp declare target" attribute. */
37154 DECL_ATTRIBUTES (fndecl)
37155 = tree_cons (get_identifier ("omp declare target"),
37156 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37158 /* Don't unset parser->oacc_routine here: we may still need it to
37159 diagnose wrong usage. But, remember that we've used this "#pragma acc
37160 routine". */
37161 parser->oacc_routine->fndecl_seen = true;
37165 /* Main entry point to OpenMP statement pragmas. */
37167 static void
37168 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37170 tree stmt;
37171 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37172 omp_clause_mask mask (0);
37174 switch (cp_parser_pragma_kind (pragma_tok))
37176 case PRAGMA_OACC_ATOMIC:
37177 cp_parser_omp_atomic (parser, pragma_tok);
37178 return;
37179 case PRAGMA_OACC_CACHE:
37180 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37181 break;
37182 case PRAGMA_OACC_DATA:
37183 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37184 break;
37185 case PRAGMA_OACC_ENTER_DATA:
37186 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37187 break;
37188 case PRAGMA_OACC_EXIT_DATA:
37189 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37190 break;
37191 case PRAGMA_OACC_HOST_DATA:
37192 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37193 break;
37194 case PRAGMA_OACC_KERNELS:
37195 case PRAGMA_OACC_PARALLEL:
37196 strcpy (p_name, "#pragma acc");
37197 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37198 if_p);
37199 break;
37200 case PRAGMA_OACC_LOOP:
37201 strcpy (p_name, "#pragma acc");
37202 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37203 if_p);
37204 break;
37205 case PRAGMA_OACC_UPDATE:
37206 stmt = cp_parser_oacc_update (parser, pragma_tok);
37207 break;
37208 case PRAGMA_OACC_WAIT:
37209 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37210 break;
37211 case PRAGMA_OMP_ATOMIC:
37212 cp_parser_omp_atomic (parser, pragma_tok);
37213 return;
37214 case PRAGMA_OMP_CRITICAL:
37215 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37216 break;
37217 case PRAGMA_OMP_DISTRIBUTE:
37218 strcpy (p_name, "#pragma omp");
37219 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37220 if_p);
37221 break;
37222 case PRAGMA_OMP_FOR:
37223 strcpy (p_name, "#pragma omp");
37224 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37225 if_p);
37226 break;
37227 case PRAGMA_OMP_MASTER:
37228 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37229 break;
37230 case PRAGMA_OMP_PARALLEL:
37231 strcpy (p_name, "#pragma omp");
37232 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37233 if_p);
37234 break;
37235 case PRAGMA_OMP_SECTIONS:
37236 strcpy (p_name, "#pragma omp");
37237 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37238 break;
37239 case PRAGMA_OMP_SIMD:
37240 strcpy (p_name, "#pragma omp");
37241 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37242 if_p);
37243 break;
37244 case PRAGMA_OMP_SINGLE:
37245 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37246 break;
37247 case PRAGMA_OMP_TASK:
37248 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37249 break;
37250 case PRAGMA_OMP_TASKGROUP:
37251 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37252 break;
37253 case PRAGMA_OMP_TASKLOOP:
37254 strcpy (p_name, "#pragma omp");
37255 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37256 if_p);
37257 break;
37258 case PRAGMA_OMP_TEAMS:
37259 strcpy (p_name, "#pragma omp");
37260 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37261 if_p);
37262 break;
37263 default:
37264 gcc_unreachable ();
37267 protected_set_expr_location (stmt, pragma_tok->location);
37270 /* Transactional Memory parsing routines. */
37272 /* Parse a transaction attribute.
37274 txn-attribute:
37275 attribute
37276 [ [ identifier ] ]
37278 We use this instead of cp_parser_attributes_opt for transactions to avoid
37279 the pedwarn in C++98 mode. */
37281 static tree
37282 cp_parser_txn_attribute_opt (cp_parser *parser)
37284 cp_token *token;
37285 tree attr_name, attr = NULL;
37287 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37288 return cp_parser_attributes_opt (parser);
37290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37291 return NULL_TREE;
37292 cp_lexer_consume_token (parser->lexer);
37293 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37294 goto error1;
37296 token = cp_lexer_peek_token (parser->lexer);
37297 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37299 token = cp_lexer_consume_token (parser->lexer);
37301 attr_name = (token->type == CPP_KEYWORD
37302 /* For keywords, use the canonical spelling,
37303 not the parsed identifier. */
37304 ? ridpointers[(int) token->keyword]
37305 : token->u.value);
37306 attr = build_tree_list (attr_name, NULL_TREE);
37308 else
37309 cp_parser_error (parser, "expected identifier");
37311 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37312 error1:
37313 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37314 return attr;
37317 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37319 transaction-statement:
37320 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37321 compound-statement
37322 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37325 static tree
37326 cp_parser_transaction (cp_parser *parser, cp_token *token)
37328 unsigned char old_in = parser->in_transaction;
37329 unsigned char this_in = 1, new_in;
37330 enum rid keyword = token->keyword;
37331 tree stmt, attrs, noex;
37333 cp_lexer_consume_token (parser->lexer);
37335 if (keyword == RID_TRANSACTION_RELAXED
37336 || keyword == RID_SYNCHRONIZED)
37337 this_in |= TM_STMT_ATTR_RELAXED;
37338 else
37340 attrs = cp_parser_txn_attribute_opt (parser);
37341 if (attrs)
37342 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37345 /* Parse a noexcept specification. */
37346 if (keyword == RID_ATOMIC_NOEXCEPT)
37347 noex = boolean_true_node;
37348 else if (keyword == RID_ATOMIC_CANCEL)
37350 /* cancel-and-throw is unimplemented. */
37351 sorry ("atomic_cancel");
37352 noex = NULL_TREE;
37354 else
37355 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37357 /* Keep track if we're in the lexical scope of an outer transaction. */
37358 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37360 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37362 parser->in_transaction = new_in;
37363 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37364 parser->in_transaction = old_in;
37366 finish_transaction_stmt (stmt, NULL, this_in, noex);
37368 return stmt;
37371 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37373 transaction-expression:
37374 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37375 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37378 static tree
37379 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37381 unsigned char old_in = parser->in_transaction;
37382 unsigned char this_in = 1;
37383 cp_token *token;
37384 tree expr, noex;
37385 bool noex_expr;
37386 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37388 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37389 || keyword == RID_TRANSACTION_RELAXED);
37391 if (!flag_tm)
37392 error_at (loc,
37393 keyword == RID_TRANSACTION_RELAXED
37394 ? G_("%<__transaction_relaxed%> without transactional memory "
37395 "support enabled")
37396 : G_("%<__transaction_atomic%> without transactional memory "
37397 "support enabled"));
37399 token = cp_parser_require_keyword (parser, keyword,
37400 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37401 : RT_TRANSACTION_RELAXED));
37402 gcc_assert (token != NULL);
37404 if (keyword == RID_TRANSACTION_RELAXED)
37405 this_in |= TM_STMT_ATTR_RELAXED;
37407 /* Set this early. This might mean that we allow transaction_cancel in
37408 an expression that we find out later actually has to be a constexpr.
37409 However, we expect that cxx_constant_value will be able to deal with
37410 this; also, if the noexcept has no constexpr, then what we parse next
37411 really is a transaction's body. */
37412 parser->in_transaction = this_in;
37414 /* Parse a noexcept specification. */
37415 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37416 true);
37418 if (!noex || !noex_expr
37419 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37421 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37423 expr = cp_parser_expression (parser);
37424 expr = finish_parenthesized_expr (expr);
37426 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37428 else
37430 /* The only expression that is available got parsed for the noexcept
37431 already. noexcept is true then. */
37432 expr = noex;
37433 noex = boolean_true_node;
37436 expr = build_transaction_expr (token->location, expr, this_in, noex);
37437 parser->in_transaction = old_in;
37439 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37440 return error_mark_node;
37442 return (flag_tm ? expr : error_mark_node);
37445 /* Parse a function-transaction-block.
37447 function-transaction-block:
37448 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37449 function-body
37450 __transaction_atomic txn-attribute[opt] function-try-block
37451 __transaction_relaxed ctor-initializer[opt] function-body
37452 __transaction_relaxed function-try-block
37455 static bool
37456 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37458 unsigned char old_in = parser->in_transaction;
37459 unsigned char new_in = 1;
37460 tree compound_stmt, stmt, attrs;
37461 bool ctor_initializer_p;
37462 cp_token *token;
37464 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37465 || keyword == RID_TRANSACTION_RELAXED);
37466 token = cp_parser_require_keyword (parser, keyword,
37467 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37468 : RT_TRANSACTION_RELAXED));
37469 gcc_assert (token != NULL);
37471 if (keyword == RID_TRANSACTION_RELAXED)
37472 new_in |= TM_STMT_ATTR_RELAXED;
37473 else
37475 attrs = cp_parser_txn_attribute_opt (parser);
37476 if (attrs)
37477 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37480 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37482 parser->in_transaction = new_in;
37484 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37485 ctor_initializer_p = cp_parser_function_try_block (parser);
37486 else
37487 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37488 (parser, /*in_function_try_block=*/false);
37490 parser->in_transaction = old_in;
37492 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37494 return ctor_initializer_p;
37497 /* Parse a __transaction_cancel statement.
37499 cancel-statement:
37500 __transaction_cancel txn-attribute[opt] ;
37501 __transaction_cancel txn-attribute[opt] throw-expression ;
37503 ??? Cancel and throw is not yet implemented. */
37505 static tree
37506 cp_parser_transaction_cancel (cp_parser *parser)
37508 cp_token *token;
37509 bool is_outer = false;
37510 tree stmt, attrs;
37512 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37513 RT_TRANSACTION_CANCEL);
37514 gcc_assert (token != NULL);
37516 attrs = cp_parser_txn_attribute_opt (parser);
37517 if (attrs)
37518 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37520 /* ??? Parse cancel-and-throw here. */
37522 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37524 if (!flag_tm)
37526 error_at (token->location, "%<__transaction_cancel%> without "
37527 "transactional memory support enabled");
37528 return error_mark_node;
37530 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37532 error_at (token->location, "%<__transaction_cancel%> within a "
37533 "%<__transaction_relaxed%>");
37534 return error_mark_node;
37536 else if (is_outer)
37538 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37539 && !is_tm_may_cancel_outer (current_function_decl))
37541 error_at (token->location, "outer %<__transaction_cancel%> not "
37542 "within outer %<__transaction_atomic%>");
37543 error_at (token->location,
37544 " or a %<transaction_may_cancel_outer%> function");
37545 return error_mark_node;
37548 else if (parser->in_transaction == 0)
37550 error_at (token->location, "%<__transaction_cancel%> not within "
37551 "%<__transaction_atomic%>");
37552 return error_mark_node;
37555 stmt = build_tm_abort_call (token->location, is_outer);
37556 add_stmt (stmt);
37558 return stmt;
37561 /* The parser. */
37563 static GTY (()) cp_parser *the_parser;
37566 /* Special handling for the first token or line in the file. The first
37567 thing in the file might be #pragma GCC pch_preprocess, which loads a
37568 PCH file, which is a GC collection point. So we need to handle this
37569 first pragma without benefit of an existing lexer structure.
37571 Always returns one token to the caller in *FIRST_TOKEN. This is
37572 either the true first token of the file, or the first token after
37573 the initial pragma. */
37575 static void
37576 cp_parser_initial_pragma (cp_token *first_token)
37578 tree name = NULL;
37580 cp_lexer_get_preprocessor_token (NULL, first_token);
37581 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37582 return;
37584 cp_lexer_get_preprocessor_token (NULL, first_token);
37585 if (first_token->type == CPP_STRING)
37587 name = first_token->u.value;
37589 cp_lexer_get_preprocessor_token (NULL, first_token);
37590 if (first_token->type != CPP_PRAGMA_EOL)
37591 error_at (first_token->location,
37592 "junk at end of %<#pragma GCC pch_preprocess%>");
37594 else
37595 error_at (first_token->location, "expected string literal");
37597 /* Skip to the end of the pragma. */
37598 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37599 cp_lexer_get_preprocessor_token (NULL, first_token);
37601 /* Now actually load the PCH file. */
37602 if (name)
37603 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37605 /* Read one more token to return to our caller. We have to do this
37606 after reading the PCH file in, since its pointers have to be
37607 live. */
37608 cp_lexer_get_preprocessor_token (NULL, first_token);
37611 /* Parses the grainsize pragma for the _Cilk_for statement.
37612 Syntax:
37613 #pragma cilk grainsize = <VALUE>. */
37615 static void
37616 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37618 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37620 tree exp = cp_parser_binary_expression (parser, false, false,
37621 PREC_NOT_OPERATOR, NULL);
37622 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37623 if (!exp || exp == error_mark_node)
37625 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37626 return;
37629 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37631 cp_parser_cilk_for (parser, exp, if_p);
37632 else
37633 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37634 "%<#pragma cilk grainsize%> is not followed by "
37635 "%<_Cilk_for%>");
37636 return;
37638 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37641 /* Normal parsing of a pragma token. Here we can (and must) use the
37642 regular lexer. */
37644 static bool
37645 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37647 cp_token *pragma_tok;
37648 unsigned int id;
37649 tree stmt;
37650 bool ret;
37652 pragma_tok = cp_lexer_consume_token (parser->lexer);
37653 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37654 parser->lexer->in_pragma = true;
37656 id = cp_parser_pragma_kind (pragma_tok);
37657 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
37658 cp_ensure_no_omp_declare_simd (parser);
37659 switch (id)
37661 case PRAGMA_GCC_PCH_PREPROCESS:
37662 error_at (pragma_tok->location,
37663 "%<#pragma GCC pch_preprocess%> must be first");
37664 break;
37666 case PRAGMA_OMP_BARRIER:
37667 switch (context)
37669 case pragma_compound:
37670 cp_parser_omp_barrier (parser, pragma_tok);
37671 return false;
37672 case pragma_stmt:
37673 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37674 "used in compound statements");
37675 break;
37676 default:
37677 goto bad_stmt;
37679 break;
37681 case PRAGMA_OMP_FLUSH:
37682 switch (context)
37684 case pragma_compound:
37685 cp_parser_omp_flush (parser, pragma_tok);
37686 return false;
37687 case pragma_stmt:
37688 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37689 "used in compound statements");
37690 break;
37691 default:
37692 goto bad_stmt;
37694 break;
37696 case PRAGMA_OMP_TASKWAIT:
37697 switch (context)
37699 case pragma_compound:
37700 cp_parser_omp_taskwait (parser, pragma_tok);
37701 return false;
37702 case pragma_stmt:
37703 error_at (pragma_tok->location,
37704 "%<#pragma omp taskwait%> may only be "
37705 "used in compound statements");
37706 break;
37707 default:
37708 goto bad_stmt;
37710 break;
37712 case PRAGMA_OMP_TASKYIELD:
37713 switch (context)
37715 case pragma_compound:
37716 cp_parser_omp_taskyield (parser, pragma_tok);
37717 return false;
37718 case pragma_stmt:
37719 error_at (pragma_tok->location,
37720 "%<#pragma omp taskyield%> may only be "
37721 "used in compound statements");
37722 break;
37723 default:
37724 goto bad_stmt;
37726 break;
37728 case PRAGMA_OMP_CANCEL:
37729 switch (context)
37731 case pragma_compound:
37732 cp_parser_omp_cancel (parser, pragma_tok);
37733 return false;
37734 case pragma_stmt:
37735 error_at (pragma_tok->location,
37736 "%<#pragma omp cancel%> may only be "
37737 "used in compound statements");
37738 break;
37739 default:
37740 goto bad_stmt;
37742 break;
37744 case PRAGMA_OMP_CANCELLATION_POINT:
37745 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
37746 return false;
37748 case PRAGMA_OMP_THREADPRIVATE:
37749 cp_parser_omp_threadprivate (parser, pragma_tok);
37750 return false;
37752 case PRAGMA_OMP_DECLARE:
37753 cp_parser_omp_declare (parser, pragma_tok, context);
37754 return false;
37756 case PRAGMA_OACC_DECLARE:
37757 cp_parser_oacc_declare (parser, pragma_tok);
37758 return false;
37760 case PRAGMA_OACC_ROUTINE:
37761 if (context != pragma_external)
37763 error_at (pragma_tok->location,
37764 "%<#pragma acc routine%> must be at file scope");
37765 break;
37767 cp_parser_oacc_routine (parser, pragma_tok, context);
37768 return false;
37770 case PRAGMA_OACC_ATOMIC:
37771 case PRAGMA_OACC_CACHE:
37772 case PRAGMA_OACC_DATA:
37773 case PRAGMA_OACC_ENTER_DATA:
37774 case PRAGMA_OACC_EXIT_DATA:
37775 case PRAGMA_OACC_HOST_DATA:
37776 case PRAGMA_OACC_KERNELS:
37777 case PRAGMA_OACC_PARALLEL:
37778 case PRAGMA_OACC_LOOP:
37779 case PRAGMA_OACC_UPDATE:
37780 case PRAGMA_OACC_WAIT:
37781 case PRAGMA_OMP_ATOMIC:
37782 case PRAGMA_OMP_CRITICAL:
37783 case PRAGMA_OMP_DISTRIBUTE:
37784 case PRAGMA_OMP_FOR:
37785 case PRAGMA_OMP_MASTER:
37786 case PRAGMA_OMP_PARALLEL:
37787 case PRAGMA_OMP_SECTIONS:
37788 case PRAGMA_OMP_SIMD:
37789 case PRAGMA_OMP_SINGLE:
37790 case PRAGMA_OMP_TASK:
37791 case PRAGMA_OMP_TASKGROUP:
37792 case PRAGMA_OMP_TASKLOOP:
37793 case PRAGMA_OMP_TEAMS:
37794 if (context != pragma_stmt && context != pragma_compound)
37795 goto bad_stmt;
37796 stmt = push_omp_privatization_clauses (false);
37797 cp_parser_omp_construct (parser, pragma_tok, if_p);
37798 pop_omp_privatization_clauses (stmt);
37799 return true;
37801 case PRAGMA_OMP_ORDERED:
37802 stmt = push_omp_privatization_clauses (false);
37803 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37804 pop_omp_privatization_clauses (stmt);
37805 return ret;
37807 case PRAGMA_OMP_TARGET:
37808 stmt = push_omp_privatization_clauses (false);
37809 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37810 pop_omp_privatization_clauses (stmt);
37811 return ret;
37813 case PRAGMA_OMP_END_DECLARE_TARGET:
37814 cp_parser_omp_end_declare_target (parser, pragma_tok);
37815 return false;
37817 case PRAGMA_OMP_SECTION:
37818 error_at (pragma_tok->location,
37819 "%<#pragma omp section%> may only be used in "
37820 "%<#pragma omp sections%> construct");
37821 break;
37823 case PRAGMA_IVDEP:
37825 if (context == pragma_external)
37827 error_at (pragma_tok->location,
37828 "%<#pragma GCC ivdep%> must be inside a function");
37829 break;
37831 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37832 cp_token *tok;
37833 tok = cp_lexer_peek_token (the_parser->lexer);
37834 if (tok->type != CPP_KEYWORD
37835 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37836 && tok->keyword != RID_DO))
37838 cp_parser_error (parser, "for, while or do statement expected");
37839 return false;
37841 cp_parser_iteration_statement (parser, if_p, true);
37842 return true;
37845 case PRAGMA_CILK_SIMD:
37846 if (context == pragma_external)
37848 error_at (pragma_tok->location,
37849 "%<#pragma simd%> must be inside a function");
37850 break;
37852 stmt = push_omp_privatization_clauses (false);
37853 cp_parser_cilk_simd (parser, pragma_tok, if_p);
37854 pop_omp_privatization_clauses (stmt);
37855 return true;
37857 case PRAGMA_CILK_GRAINSIZE:
37858 if (context == pragma_external)
37860 error_at (pragma_tok->location,
37861 "%<#pragma cilk grainsize%> must be inside a function");
37862 break;
37865 /* Ignore the pragma if Cilk Plus is not enabled. */
37866 if (flag_cilkplus)
37868 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37869 return true;
37871 else
37873 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37874 "%<#pragma cilk grainsize%>");
37875 break;
37878 default:
37879 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37880 c_invoke_pragma_handler (id);
37881 break;
37883 bad_stmt:
37884 cp_parser_error (parser, "expected declaration specifiers");
37885 break;
37888 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37889 return false;
37892 /* The interface the pragma parsers have to the lexer. */
37894 enum cpp_ttype
37895 pragma_lex (tree *value, location_t *loc)
37897 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37898 enum cpp_ttype ret = tok->type;
37900 *value = tok->u.value;
37901 if (loc)
37902 *loc = tok->location;
37904 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37905 ret = CPP_EOF;
37906 else if (ret == CPP_STRING)
37907 *value = cp_parser_string_literal (the_parser, false, false);
37908 else
37910 if (ret == CPP_KEYWORD)
37911 ret = CPP_NAME;
37912 cp_lexer_consume_token (the_parser->lexer);
37915 return ret;
37919 /* External interface. */
37921 /* Parse one entire translation unit. */
37923 void
37924 c_parse_file (void)
37926 static bool already_called = false;
37928 if (already_called)
37929 fatal_error (input_location,
37930 "inter-module optimizations not implemented for C++");
37931 already_called = true;
37933 the_parser = cp_parser_new ();
37934 push_deferring_access_checks (flag_access_control
37935 ? dk_no_deferred : dk_no_check);
37936 cp_parser_translation_unit (the_parser);
37937 the_parser = NULL;
37940 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37941 vectorlength clause:
37942 Syntax:
37943 vectorlength ( constant-expression ) */
37945 static tree
37946 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37947 bool is_simd_fn)
37949 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37950 tree expr;
37951 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37952 safelen clause. Thus, vectorlength is represented as OMP 4.0
37953 safelen. For SIMD-enabled function it is represented by OMP 4.0
37954 simdlen. */
37955 if (!is_simd_fn)
37956 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37957 loc);
37958 else
37959 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37960 loc);
37962 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37963 return error_mark_node;
37965 expr = cp_parser_constant_expression (parser);
37966 expr = maybe_constant_value (expr);
37968 /* If expr == error_mark_node, then don't emit any errors nor
37969 create a clause. if any of the above functions returns
37970 error mark node then they would have emitted an error message. */
37971 if (expr == error_mark_node)
37973 else if (!TREE_TYPE (expr)
37974 || !TREE_CONSTANT (expr)
37975 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37976 error_at (loc, "vectorlength must be an integer constant");
37977 else if (TREE_CONSTANT (expr)
37978 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
37979 error_at (loc, "vectorlength must be a power of 2");
37980 else
37982 tree c;
37983 if (!is_simd_fn)
37985 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37986 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37987 OMP_CLAUSE_CHAIN (c) = clauses;
37988 clauses = c;
37990 else
37992 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37993 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37994 OMP_CLAUSE_CHAIN (c) = clauses;
37995 clauses = c;
37999 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38000 return error_mark_node;
38001 return clauses;
38004 /* Handles the Cilk Plus #pragma simd linear clause.
38005 Syntax:
38006 linear ( simd-linear-variable-list )
38008 simd-linear-variable-list:
38009 simd-linear-variable
38010 simd-linear-variable-list , simd-linear-variable
38012 simd-linear-variable:
38013 id-expression
38014 id-expression : simd-linear-step
38016 simd-linear-step:
38017 conditional-expression */
38019 static tree
38020 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38022 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38024 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38025 return clauses;
38026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38028 cp_parser_error (parser, "expected identifier");
38029 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38030 return error_mark_node;
38033 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38034 parser->colon_corrects_to_scope_p = false;
38035 while (1)
38037 cp_token *token = cp_lexer_peek_token (parser->lexer);
38038 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38040 cp_parser_error (parser, "expected variable-name");
38041 clauses = error_mark_node;
38042 break;
38045 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38046 false, false);
38047 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38048 token->location);
38049 if (decl == error_mark_node)
38051 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38052 token->location);
38053 clauses = error_mark_node;
38055 else
38057 tree e = NULL_TREE;
38058 tree step_size = integer_one_node;
38060 /* If present, parse the linear step. Otherwise, assume the default
38061 value of 1. */
38062 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38064 cp_lexer_consume_token (parser->lexer);
38066 e = cp_parser_assignment_expression (parser);
38067 e = maybe_constant_value (e);
38069 if (e == error_mark_node)
38071 /* If an error has occurred, then the whole pragma is
38072 considered ill-formed. Thus, no reason to keep
38073 parsing. */
38074 clauses = error_mark_node;
38075 break;
38077 else if (type_dependent_expression_p (e)
38078 || value_dependent_expression_p (e)
38079 || (TREE_TYPE (e)
38080 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38081 && (TREE_CONSTANT (e)
38082 || DECL_P (e))))
38083 step_size = e;
38084 else
38085 cp_parser_error (parser,
38086 "step size must be an integer constant "
38087 "expression or an integer variable");
38090 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38091 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38092 OMP_CLAUSE_DECL (l) = decl;
38093 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38094 OMP_CLAUSE_CHAIN (l) = clauses;
38095 clauses = l;
38097 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38098 cp_lexer_consume_token (parser->lexer);
38099 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38100 break;
38101 else
38103 error_at (cp_lexer_peek_token (parser->lexer)->location,
38104 "expected %<,%> or %<)%> after %qE", decl);
38105 clauses = error_mark_node;
38106 break;
38109 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38110 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38111 return clauses;
38114 /* Returns the name of the next clause. If the clause is not
38115 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38116 token is not consumed. Otherwise, the appropriate enum from the
38117 pragma_simd_clause is returned and the token is consumed. */
38119 static pragma_omp_clause
38120 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38122 pragma_omp_clause clause_type;
38123 cp_token *token = cp_lexer_peek_token (parser->lexer);
38125 if (token->keyword == RID_PRIVATE)
38126 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38127 else if (!token->u.value || token->type != CPP_NAME)
38128 return PRAGMA_CILK_CLAUSE_NONE;
38129 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
38130 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38131 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38132 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38133 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38134 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38135 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38136 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38137 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38138 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38139 else
38140 return PRAGMA_CILK_CLAUSE_NONE;
38142 cp_lexer_consume_token (parser->lexer);
38143 return clause_type;
38146 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38148 static tree
38149 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38151 tree clauses = NULL_TREE;
38153 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38154 && clauses != error_mark_node)
38156 pragma_omp_clause c_kind;
38157 c_kind = cp_parser_cilk_simd_clause_name (parser);
38158 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38159 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38160 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38161 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38162 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38163 /* Use the OpenMP 4.0 equivalent function. */
38164 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38165 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38166 /* Use the OpenMP 4.0 equivalent function. */
38167 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38168 clauses);
38169 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38170 /* Use the OMP 4.0 equivalent function. */
38171 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38172 clauses);
38173 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38174 /* Use the OMP 4.0 equivalent function. */
38175 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38176 else
38178 clauses = error_mark_node;
38179 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38180 break;
38184 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38186 if (clauses == error_mark_node)
38187 return error_mark_node;
38188 else
38189 return finish_omp_clauses (clauses, C_ORT_CILK);
38192 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38194 static void
38195 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38197 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38199 if (clauses == error_mark_node)
38200 return;
38202 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38204 error_at (cp_lexer_peek_token (parser->lexer)->location,
38205 "for statement expected");
38206 return;
38209 tree sb = begin_omp_structured_block ();
38210 int save = cp_parser_begin_omp_structured_block (parser);
38211 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38212 if (ret)
38213 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38214 cp_parser_end_omp_structured_block (parser, save);
38215 add_stmt (finish_omp_structured_block (sb));
38218 /* Main entry-point for parsing Cilk Plus _Cilk_for
38219 loops. The return value is error_mark_node
38220 when errors happen and CILK_FOR tree on success. */
38222 static tree
38223 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38225 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38226 gcc_unreachable ();
38228 tree sb = begin_omp_structured_block ();
38229 int save = cp_parser_begin_omp_structured_block (parser);
38231 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38232 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38233 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38234 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38236 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38237 if (ret)
38238 cpp_validate_cilk_plus_loop (ret);
38239 else
38240 ret = error_mark_node;
38242 cp_parser_end_omp_structured_block (parser, save);
38243 add_stmt (finish_omp_structured_block (sb));
38244 return ret;
38247 /* Create an identifier for a generic parameter type (a synthesized
38248 template parameter implied by `auto' or a concept identifier). */
38250 static GTY(()) int generic_parm_count;
38251 static tree
38252 make_generic_type_name ()
38254 char buf[32];
38255 sprintf (buf, "auto:%d", ++generic_parm_count);
38256 return get_identifier (buf);
38259 /* Predicate that behaves as is_auto_or_concept but matches the parent
38260 node of the generic type rather than the generic type itself. This
38261 allows for type transformation in add_implicit_template_parms. */
38263 static inline bool
38264 tree_type_is_auto_or_concept (const_tree t)
38266 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
38269 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38270 (creating a new template parameter list if necessary). Returns the newly
38271 created template type parm. */
38273 static tree
38274 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38276 gcc_assert (current_binding_level->kind == sk_function_parms);
38278 /* Before committing to modifying any scope, if we're in an
38279 implicit template scope, and we're trying to synthesize a
38280 constrained parameter, try to find a previous parameter with
38281 the same name. This is the same-type rule for abbreviated
38282 function templates.
38284 NOTE: We can generate implicit parameters when tentatively
38285 parsing a nested name specifier, only to reject that parse
38286 later. However, matching the same template-id as part of a
38287 direct-declarator should generate an identical template
38288 parameter, so this rule will merge them. */
38289 if (parser->implicit_template_scope && constr)
38291 tree t = parser->implicit_template_parms;
38292 while (t)
38294 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38296 tree d = TREE_VALUE (t);
38297 if (TREE_CODE (d) == PARM_DECL)
38298 /* Return the TEMPLATE_PARM_INDEX. */
38299 d = DECL_INITIAL (d);
38300 return d;
38302 t = TREE_CHAIN (t);
38306 /* We are either continuing a function template that already contains implicit
38307 template parameters, creating a new fully-implicit function template, or
38308 extending an existing explicit function template with implicit template
38309 parameters. */
38311 cp_binding_level *const entry_scope = current_binding_level;
38313 bool become_template = false;
38314 cp_binding_level *parent_scope = 0;
38316 if (parser->implicit_template_scope)
38318 gcc_assert (parser->implicit_template_parms);
38320 current_binding_level = parser->implicit_template_scope;
38322 else
38324 /* Roll back to the existing template parameter scope (in the case of
38325 extending an explicit function template) or introduce a new template
38326 parameter scope ahead of the function parameter scope (or class scope
38327 in the case of out-of-line member definitions). The function scope is
38328 added back after template parameter synthesis below. */
38330 cp_binding_level *scope = entry_scope;
38332 while (scope->kind == sk_function_parms)
38334 parent_scope = scope;
38335 scope = scope->level_chain;
38337 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38339 /* If not defining a class, then any class scope is a scope level in
38340 an out-of-line member definition. In this case simply wind back
38341 beyond the first such scope to inject the template parameter list.
38342 Otherwise wind back to the class being defined. The latter can
38343 occur in class member friend declarations such as:
38345 class A {
38346 void foo (auto);
38348 class B {
38349 friend void A::foo (auto);
38352 The template parameter list synthesized for the friend declaration
38353 must be injected in the scope of 'B'. This can also occur in
38354 erroneous cases such as:
38356 struct A {
38357 struct B {
38358 void foo (auto);
38360 void B::foo (auto) {}
38363 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38364 but, nevertheless, the template parameter list synthesized for the
38365 declarator should be injected into the scope of 'A' as if the
38366 ill-formed template was specified explicitly. */
38368 while (scope->kind == sk_class && !scope->defining_class_p)
38370 parent_scope = scope;
38371 scope = scope->level_chain;
38375 current_binding_level = scope;
38377 if (scope->kind != sk_template_parms
38378 || !function_being_declared_is_template_p (parser))
38380 /* Introduce a new template parameter list for implicit template
38381 parameters. */
38383 become_template = true;
38385 parser->implicit_template_scope
38386 = begin_scope (sk_template_parms, NULL);
38388 ++processing_template_decl;
38390 parser->fully_implicit_function_template_p = true;
38391 ++parser->num_template_parameter_lists;
38393 else
38395 /* Synthesize implicit template parameters at the end of the explicit
38396 template parameter list. */
38398 gcc_assert (current_template_parms);
38400 parser->implicit_template_scope = scope;
38402 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38403 parser->implicit_template_parms
38404 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38408 /* Synthesize a new template parameter and track the current template
38409 parameter chain with implicit_template_parms. */
38411 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38412 tree synth_id = make_generic_type_name ();
38413 tree synth_tmpl_parm;
38414 bool non_type = false;
38416 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38417 synth_tmpl_parm
38418 = finish_template_type_parm (class_type_node, synth_id);
38419 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38420 synth_tmpl_parm
38421 = finish_constrained_template_template_parm (proto, synth_id);
38422 else
38424 synth_tmpl_parm = copy_decl (proto);
38425 DECL_NAME (synth_tmpl_parm) = synth_id;
38426 non_type = true;
38429 // Attach the constraint to the parm before processing.
38430 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38431 TREE_TYPE (node) = constr;
38432 tree new_parm
38433 = process_template_parm (parser->implicit_template_parms,
38434 input_location,
38435 node,
38436 /*non_type=*/non_type,
38437 /*param_pack=*/false);
38439 // Chain the new parameter to the list of implicit parameters.
38440 if (parser->implicit_template_parms)
38441 parser->implicit_template_parms
38442 = TREE_CHAIN (parser->implicit_template_parms);
38443 else
38444 parser->implicit_template_parms = new_parm;
38446 tree new_decl = getdecls ();
38447 if (non_type)
38448 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38449 new_decl = DECL_INITIAL (new_decl);
38451 /* If creating a fully implicit function template, start the new implicit
38452 template parameter list with this synthesized type, otherwise grow the
38453 current template parameter list. */
38455 if (become_template)
38457 parent_scope->level_chain = current_binding_level;
38459 tree new_parms = make_tree_vec (1);
38460 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38461 current_template_parms = tree_cons (size_int (processing_template_decl),
38462 new_parms, current_template_parms);
38464 else
38466 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38467 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38468 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38469 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38472 // If the new parameter was constrained, we need to add that to the
38473 // constraints in the template parameter list.
38474 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38476 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38477 reqs = conjoin_constraints (reqs, req);
38478 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38481 current_binding_level = entry_scope;
38483 return new_decl;
38486 /* Finish the declaration of a fully implicit function template. Such a
38487 template has no explicit template parameter list so has not been through the
38488 normal template head and tail processing. synthesize_implicit_template_parm
38489 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38490 provided if the declaration is a class member such that its template
38491 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38492 form is returned. Otherwise NULL_TREE is returned. */
38494 static tree
38495 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38497 gcc_assert (parser->fully_implicit_function_template_p);
38499 if (member_decl_opt && member_decl_opt != error_mark_node
38500 && DECL_VIRTUAL_P (member_decl_opt))
38502 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38503 "implicit templates may not be %<virtual%>");
38504 DECL_VIRTUAL_P (member_decl_opt) = false;
38507 if (member_decl_opt)
38508 member_decl_opt = finish_member_template_decl (member_decl_opt);
38509 end_template_decl ();
38511 parser->fully_implicit_function_template_p = false;
38512 --parser->num_template_parameter_lists;
38514 return member_decl_opt;
38517 #include "gt-cp-parser.h"